提交 cbdf372e 编写于 作者: B Brett Walker

implemented using an ivar, and added specs

上级 528f9cde
......@@ -16,6 +16,8 @@ class Projects::CommitController < Projects::ApplicationController
before_action :define_note_vars, only: [:show, :diff_for_path]
before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
BRANCH_SEARCH_LIMIT = 1000
def show
apply_diff_view_cookie!
......@@ -59,8 +61,11 @@ class Projects::CommitController < Projects::ApplicationController
# branch_names_contains/tag_names_contains can take a long time when there are thousands of
# branches/tags - each `git branch --contains xxx` request can consume a cpu core.
# so only do the query when there are a manageable number of branches/tags
@branches = @project.repository.branch_count > 1000 ? [:limit_exceeded] : @project.repository.branch_names_contains(commit.id)
@tags = @project.repository.tag_count > 1000 ? [:limit_exceeded] : @project.repository.tag_names_contains(commit.id)
@branches_limit_exceeded = @project.repository.branch_count > BRANCH_SEARCH_LIMIT
@branches = @branches_limit_exceeded ? [] : @project.repository.branch_names_contains(commit.id)
@tags_limit_exceeded = @project.repository.tag_count > BRANCH_SEARCH_LIMIT
@tags = @tags_limit_exceeded ? [] : @project.repository.tag_names_contains(commit.id)
render layout: false
end
......
- if @branches.any?
- branch = commit_default_branch(@project, @branches)
- if branch == :limit_exceeded
- if @branches.any? || @branches_limit_exceeded
- if @branches_limit_exceeded
= commit_branch_link('#', _('Too many branches to search'))
- else
- branch = commit_default_branch(@project, @branches)
= commit_branch_link(project_ref_path(@project, branch), branch)
-# `commit_default_branch` deletes the default branch from `@branches`,
-# so only render this if we have more branches or tags left
- if @branches.any? || @tags.any?
- if @branches.any? || @tags.any? || @tags_limit_exceeded
%span
= link_to "…", "#", class: "js-details-expand label label-gray"
%span.js-details-content.hide
= commit_branches_links(@project, @branches) if @branches.any?
- if @tags.first == :limit_exceeded
- if @tags_limit_exceeded
= commit_tag_link('#', _('Too many tags to search'))
- elsif @tags.any?
= commit_tags_links(@project, @tags)
......@@ -134,8 +134,8 @@ describe Projects::CommitController do
end
end
describe "GET branches" do
it "contains branch and tags information" do
describe 'GET branches' do
it 'contains branch and tags information' do
commit = project.commit('5937ac0a7beb003549fc5fd26fc247adbce4a52e')
get(:branches,
......@@ -143,8 +143,26 @@ describe Projects::CommitController do
project_id: project,
id: commit.id)
expect(assigns(:branches)).to include("master", "feature_conflict")
expect(assigns(:tags)).to include("v1.1.0")
expect(assigns(:branches)).to include('master', 'feature_conflict')
expect(assigns(:branches_limit_exceeded)).to be_falsey
expect(assigns(:tags)).to include('v1.1.0')
expect(assigns(:tags_limit_exceeded)).to be_falsey
end
it 'returns :limit_exceeded when number of branches/tags reach a threshhold' do
commit = project.commit('5937ac0a7beb003549fc5fd26fc247adbce4a52e')
allow_any_instance_of(Repository).to receive(:branch_count).and_return(1001)
allow_any_instance_of(Repository).to receive(:tag_count).and_return(1001)
get(:branches,
namespace_id: project.namespace,
project_id: project,
id: commit.id)
expect(assigns(:branches)).to eq([])
expect(assigns(:branches_limit_exceeded)).to be_truthy
expect(assigns(:tags)).to eq([])
expect(assigns(:tags_limit_exceeded)).to be_truthy
end
end
......
require 'spec_helper'
describe 'projects/commit/branches.html.haml' do
let(:project) { create(:project, :repository) }
before do
assign(:project, project)
end
context 'branches and tags' do
before do
assign(:branches, ['master', 'test-branch'])
assign(:branches_limit_exceeded, false)
assign(:tags, ['tag1'])
assign(:tags_limit_exceeded, false)
render
end
it 'shows branch and tag links' do
expect(rendered).to have_selector('.js-details-expand')
expect(rendered).to have_link('master')
expect(rendered).to have_link('test-branch')
expect(rendered).to have_link('tag1')
end
end
context 'throttled branches and tags' do
before do
assign(:branches, [])
assign(:branches_limit_exceeded, true)
assign(:tags, [])
assign(:tags_limit_exceeded, true)
render
end
it 'shows too many to search' do
expect(rendered).to have_selector('.js-details-expand')
expect(rendered).to have_link('Too many branches to search', href: '#')
expect(rendered).to have_link('Too many tags to search', href: '#')
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册