Commit 88d4639f authored by Robert Speicher's avatar Robert Speicher

Merge branch 'feature/migrate-find-branch-to-gitaly' into 'master'

Migrate Gitlab::Git::Repository#find_branch to Gitaly

Closes gitaly#502

See merge request !13873
parents 063e285e e10437de
......@@ -397,7 +397,7 @@ group :ed25519 do
end
# Gitaly GRPC client
gem 'gitaly-proto', '~> 0.31.0', require: 'gitaly'
gem 'gitaly-proto', '~> 0.32.0', require: 'gitaly'
gem 'toml-rb', '~> 0.3.15', require: false
......
......@@ -275,7 +275,7 @@ GEM
po_to_json (>= 1.0.0)
rails (>= 3.2.0)
gherkin-ruby (0.3.2)
gitaly-proto (0.31.0)
gitaly-proto (0.32.0)
google-protobuf (~> 3.1)
grpc (~> 1.0)
github-linguist (4.7.6)
......@@ -1020,7 +1020,7 @@ DEPENDENCIES
gettext (~> 3.2.2)
gettext_i18n_rails (~> 1.8.0)
gettext_i18n_rails_js (~> 1.2.0)
gitaly-proto (~> 0.31.0)
gitaly-proto (~> 0.32.0)
github-linguist (~> 4.7.0)
gitlab-flowdock-git-hook (~> 1.0.1)
gitlab-markup (~> 1.5.1)
......
......@@ -134,9 +134,11 @@ module Gitlab
# This is to work around a bug in libgit2 that causes in-memory refs to
# be stale/invalid when packed-refs is changed.
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/15392#note_14538333
#
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/474
def find_branch(name, force_reload = false)
gitaly_migrate(:find_branch) do |is_enabled|
if is_enabled
gitaly_ref_client.find_branch(name)
else
reload_rugged if force_reload
rugged_ref = rugged.branches[name]
......@@ -145,6 +147,8 @@ module Gitlab
Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target, target_commit)
end
end
end
end
def local_branches(sort_by: nil)
gitaly_migrate(:local_branches) do |is_enabled|
......
......@@ -78,6 +78,20 @@ module Gitlab
raise ArgumentError, e.message
end
def find_branch(branch_name)
request = Gitaly::DeleteBranchRequest.new(
repository: @gitaly_repo,
name: GitalyClient.encode(branch_name)
)
response = GitalyClient.call(@repository.storage, :ref_service, :find_branch, request)
branch = response.branch
return unless branch
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
Gitlab::Git::Branch.new(@repository, encode!(branch.name.dup), branch.target_commit.id, target_commit)
end
private
def consume_refs_response(response)
......
......@@ -916,6 +916,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
end
describe '#find_branch' do
shared_examples 'finding a branch' do
it 'should return a Branch for master' do
branch = repository.find_branch('master')
......@@ -928,6 +929,14 @@ describe Gitlab::Git::Repository, seed_helper: true do
expect(branch).to eq(nil)
end
end
context 'when Gitaly find_branch feature is enabled' do
it_behaves_like 'finding a branch'
end
context 'when Gitaly find_branch feature is disabled', skip_gitaly_mock: true do
it_behaves_like 'finding a branch'
it 'should reload Rugged::Repository and return master' do
expect(Rugged::Repository).to receive(:new).twice.and_call_original
......@@ -939,6 +948,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
expect(branch.name).to eq('master')
end
end
end
describe '#ref_name_for_sha' do
let(:ref_path) { 'refs/heads' }
......
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