diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-09-29 17:40:13 +0400 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-09-29 17:40:13 +0400 |
commit | 88b1e0ffcd27ab3d9ff9ce1fe5b493e3f6296b21 (patch) | |
tree | 1d79d32b2d5a73367a8fb6e101a8bcefff18e2a6 | |
parent | 2ebd2d41a52dd6401d86cb57a5f64372da968c41 (diff) |
Faster branch contains detection for commit
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r-- | app/controllers/projects/commit_controller.rb | 8 | ||||
-rw-r--r-- | app/models/repository.rb | 17 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 15 |
3 files changed, 33 insertions, 7 deletions
diff --git a/app/controllers/projects/commit_controller.rb b/app/controllers/projects/commit_controller.rb index 34bd682bd90..66c67b661db 100644 --- a/app/controllers/projects/commit_controller.rb +++ b/app/controllers/projects/commit_controller.rb @@ -12,13 +12,7 @@ class Projects::CommitController < Projects::ApplicationController return git_not_found! unless @commit @line_notes = project.notes.for_commit_id(commit.id).inline - - @branches = begin - project.repository.branch_names_contains(commit.id) - rescue Grit::Git::GitTimeout - [] - end - + @branches = project.repository.branch_names_contains(commit.id) @diffs = @commit.diffs @note = project.build_commit_note(commit) @notes_count = project.notes.for_commit_id(commit.id).count diff --git a/app/models/repository.rb b/app/models/repository.rb index ea87ea88fdc..4108c4ee96e 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -284,4 +284,21 @@ class Repository blob_at(commit.parent_id, diff.old_path) end end + + def branch_names_contains(sha) + args = %W(git branch --contains #{sha}) + names = Gitlab::Popen.popen(args, path_to_repo).first + + if names.respond_to?(:split) + names = names.split("\n").map(&:strip) + + names.each do |name| + name.slice! '* ' + end + + names + else + [] + end + end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb new file mode 100644 index 00000000000..6a95b5b33c0 --- /dev/null +++ b/spec/models/repository_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe Repository do + include RepoHelpers + + let(:repository) { create(:project).repository } + + describe :branch_names_contains do + subject { repository.branch_names_contains(sample_commit.id) } + + it { should include('master') } + it { should_not include('feature') } + it { should_not include('fix') } + end +end |