Commit ccb3fe1f authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'feature/migrate-merged-branch-names-to-gitaly' into 'master'

Migrate merged_branch_names to Gitaly

Closes gitaly#851

See merge request gitlab-org/gitlab-ce!16157
parents 27732df3 83101941
...@@ -571,7 +571,21 @@ module Gitlab ...@@ -571,7 +571,21 @@ module Gitlab
end end
def merged_branch_names(branch_names = []) def merged_branch_names(branch_names = [])
Set.new(git_merged_branch_names(branch_names)) return [] unless root_ref
root_sha = find_branch(root_ref)&.target
return [] unless root_sha
branches = gitaly_migrate(:merged_branch_names) do |is_enabled|
if is_enabled
gitaly_merged_branch_names(branch_names, root_sha)
else
git_merged_branch_names(branch_names, root_sha)
end
end
Set.new(branches)
end end
# Return an array of Diff objects that represent the diff # Return an array of Diff objects that represent the diff
...@@ -1488,14 +1502,7 @@ module Gitlab ...@@ -1488,14 +1502,7 @@ module Gitlab
sort_branches(branches, sort_by) sort_branches(branches, sort_by)
end end
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/695 def git_merged_branch_names(branch_names, root_sha)
def git_merged_branch_names(branch_names = [])
return [] unless root_ref
root_sha = find_branch(root_ref)&.target
return [] unless root_sha
git_arguments = git_arguments =
%W[branch --merged #{root_sha} %W[branch --merged #{root_sha}
--format=%(refname:short)\ %(objectname)] + branch_names --format=%(refname:short)\ %(objectname)] + branch_names
...@@ -1509,6 +1516,14 @@ module Gitlab ...@@ -1509,6 +1516,14 @@ module Gitlab
end end
end end
def gitaly_merged_branch_names(branch_names, root_sha)
qualified_branch_names = branch_names.map { |b| "refs/heads/#{b}" }
gitaly_ref_client.merged_branches(qualified_branch_names)
.reject { |b| b.target == root_sha }
.map(&:name)
end
def process_count_commits_options(options) def process_count_commits_options(options)
if options[:from] || options[:to] if options[:from] || options[:to]
ref = ref =
......
...@@ -14,12 +14,18 @@ module Gitlab ...@@ -14,12 +14,18 @@ module Gitlab
request = Gitaly::FindAllBranchesRequest.new(repository: @gitaly_repo) request = Gitaly::FindAllBranchesRequest.new(repository: @gitaly_repo)
response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request) response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request)
response.flat_map do |message| consume_find_all_branches_response(response)
message.branches.map do |branch|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target)
Gitlab::Git::Branch.new(@repository, branch.name, branch.target.id, target_commit)
end
end end
def merged_branches(branch_names = [])
request = Gitaly::FindAllBranchesRequest.new(
repository: @gitaly_repo,
merged_only: true,
merged_branches: branch_names.map { |s| encode_binary(s) }
)
response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request)
consume_find_all_branches_response(response)
end end
def default_branch_name def default_branch_name
...@@ -62,7 +68,7 @@ module Gitlab ...@@ -62,7 +68,7 @@ module Gitlab
request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo) request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo)
request.sort_by = sort_by_param(sort_by) if sort_by request.sort_by = sort_by_param(sort_by) if sort_by
response = GitalyClient.call(@storage, :ref_service, :find_local_branches, request) response = GitalyClient.call(@storage, :ref_service, :find_local_branches, request)
consume_branches_response(response) consume_find_local_branches_response(response)
end end
def tags def tags
...@@ -151,7 +157,7 @@ module Gitlab ...@@ -151,7 +157,7 @@ module Gitlab
enum_value enum_value
end end
def consume_branches_response(response) def consume_find_local_branches_response(response)
response.flat_map do |message| response.flat_map do |message|
message.branches.map do |gitaly_branch| message.branches.map do |gitaly_branch|
Gitlab::Git::Branch.new( Gitlab::Git::Branch.new(
...@@ -164,6 +170,15 @@ module Gitlab ...@@ -164,6 +170,15 @@ module Gitlab
end end
end end
def consume_find_all_branches_response(response)
response.flat_map do |message|
message.branches.map do |branch|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target)
Gitlab::Git::Branch.new(@repository, branch.name, branch.target.id, target_commit)
end
end
end
def consume_tags_response(response) def consume_tags_response(response)
response.flat_map do |message| response.flat_map do |message|
message.tags.map { |gitaly_tag| Util.gitlab_tag_from_gitaly_tag(@repository, gitaly_tag) } message.tags.map { |gitaly_tag| Util.gitlab_tag_from_gitaly_tag(@repository, gitaly_tag) }
......
...@@ -1283,6 +1283,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -1283,6 +1283,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
describe '#merged_branch_names' do describe '#merged_branch_names' do
shared_examples 'finding merged branch names' do
context 'when branch names are passed' do context 'when branch names are passed' do
it 'only returns the names we are asking' do it 'only returns the names we are asking' do
names = repository.merged_branch_names(%w[merge-test]) names = repository.merged_branch_names(%w[merge-test])
...@@ -1327,6 +1328,15 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -1327,6 +1328,15 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
end end
context 'when Gitaly merged_branch_names feature is enabled' do
it_behaves_like 'finding merged branch names'
end
context 'when Gitaly merged_branch_names feature is disabled', :disable_gitaly do
it_behaves_like 'finding merged branch names'
end
end
describe "#ls_files" do describe "#ls_files" do
let(:master_file_paths) { repository.ls_files("master") } let(:master_file_paths) { repository.ls_files("master") }
let(:utf8_file_paths) { repository.ls_files("ls-files-utf8") } let(:utf8_file_paths) { repository.ls_files("ls-files-utf8") }
......
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