Commit cbdf372e authored by Brett Walker's avatar Brett Walker

implemented using an ivar, and added specs

parent 528f9cde
...@@ -16,6 +16,8 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -16,6 +16,8 @@ class Projects::CommitController < Projects::ApplicationController
before_action :define_note_vars, only: [:show, :diff_for_path] before_action :define_note_vars, only: [:show, :diff_for_path]
before_action :authorize_edit_tree!, only: [:revert, :cherry_pick] before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
BRANCH_SEARCH_LIMIT = 1000
def show def show
apply_diff_view_cookie! apply_diff_view_cookie!
...@@ -59,8 +61,11 @@ class Projects::CommitController < Projects::ApplicationController ...@@ -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 # 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. # 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 # 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) @branches_limit_exceeded = @project.repository.branch_count > BRANCH_SEARCH_LIMIT
@tags = @project.repository.tag_count > 1000 ? [:limit_exceeded] : @project.repository.tag_names_contains(commit.id) @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 render layout: false
end end
......
- if @branches.any? - if @branches.any? || @branches_limit_exceeded
- branch = commit_default_branch(@project, @branches) - if @branches_limit_exceeded
- if branch == :limit_exceeded
= commit_branch_link('#', _('Too many branches to search')) = commit_branch_link('#', _('Too many branches to search'))
- else - else
- branch = commit_default_branch(@project, @branches)
= commit_branch_link(project_ref_path(@project, branch), branch) = commit_branch_link(project_ref_path(@project, branch), branch)
-# `commit_default_branch` deletes the default branch from `@branches`, -# `commit_default_branch` deletes the default branch from `@branches`,
-# so only render this if we have more branches or tags left -# 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 %span
= link_to "…", "#", class: "js-details-expand label label-gray" = link_to "…", "#", class: "js-details-expand label label-gray"
%span.js-details-content.hide %span.js-details-content.hide
= commit_branches_links(@project, @branches) if @branches.any? = 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')) = commit_tag_link('#', _('Too many tags to search'))
- elsif @tags.any? - elsif @tags.any?
= commit_tags_links(@project, @tags) = commit_tags_links(@project, @tags)
...@@ -134,8 +134,8 @@ describe Projects::CommitController do ...@@ -134,8 +134,8 @@ describe Projects::CommitController do
end end
end end
describe "GET branches" do describe 'GET branches' do
it "contains branch and tags information" do it 'contains branch and tags information' do
commit = project.commit('5937ac0a7beb003549fc5fd26fc247adbce4a52e') commit = project.commit('5937ac0a7beb003549fc5fd26fc247adbce4a52e')
get(:branches, get(:branches,
...@@ -143,8 +143,26 @@ describe Projects::CommitController do ...@@ -143,8 +143,26 @@ describe Projects::CommitController do
project_id: project, project_id: project,
id: commit.id) id: commit.id)
expect(assigns(:branches)).to include("master", "feature_conflict") expect(assigns(:branches)).to include('master', 'feature_conflict')
expect(assigns(:tags)).to include("v1.1.0") 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
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%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment