summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/projects/commit_controller.rb9
-rw-r--r--app/views/projects/commit/branches.html.haml10
-rw-r--r--spec/controllers/projects/commit_controller_spec.rb26
-rw-r--r--spec/views/projects/commit/branches.html.haml_spec.rb44
4 files changed, 78 insertions, 11 deletions
diff --git a/app/controllers/projects/commit_controller.rb b/app/controllers/projects/commit_controller.rb
index 5b8a8159123..494d412b532 100644
--- a/app/controllers/projects/commit_controller.rb
+++ b/app/controllers/projects/commit_controller.rb
@@ -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
diff --git a/app/views/projects/commit/branches.html.haml b/app/views/projects/commit/branches.html.haml
index 398927fb50d..933cb2f73ce 100644
--- a/app/views/projects/commit/branches.html.haml
+++ b/app/views/projects/commit/branches.html.haml
@@ -1,19 +1,19 @@
-- 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)
diff --git a/spec/controllers/projects/commit_controller_spec.rb b/spec/controllers/projects/commit_controller_spec.rb
index df53863482d..f6ecde88b85 100644
--- a/spec/controllers/projects/commit_controller_spec.rb
+++ b/spec/controllers/projects/commit_controller_spec.rb
@@ -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
diff --git a/spec/views/projects/commit/branches.html.haml_spec.rb b/spec/views/projects/commit/branches.html.haml_spec.rb
new file mode 100644
index 00000000000..4199d2fd962
--- /dev/null
+++ b/spec/views/projects/commit/branches.html.haml_spec.rb
@@ -0,0 +1,44 @@
+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