Commit e10437de authored by Ahmad Sherif's avatar Ahmad Sherif Committed by Robert Speicher

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

parent 063e285e
...@@ -397,7 +397,7 @@ group :ed25519 do ...@@ -397,7 +397,7 @@ group :ed25519 do
end end
# Gitaly GRPC client # 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 gem 'toml-rb', '~> 0.3.15', require: false
......
...@@ -275,7 +275,7 @@ GEM ...@@ -275,7 +275,7 @@ GEM
po_to_json (>= 1.0.0) po_to_json (>= 1.0.0)
rails (>= 3.2.0) rails (>= 3.2.0)
gherkin-ruby (0.3.2) gherkin-ruby (0.3.2)
gitaly-proto (0.31.0) gitaly-proto (0.32.0)
google-protobuf (~> 3.1) google-protobuf (~> 3.1)
grpc (~> 1.0) grpc (~> 1.0)
github-linguist (4.7.6) github-linguist (4.7.6)
...@@ -1020,7 +1020,7 @@ DEPENDENCIES ...@@ -1020,7 +1020,7 @@ DEPENDENCIES
gettext (~> 3.2.2) gettext (~> 3.2.2)
gettext_i18n_rails (~> 1.8.0) gettext_i18n_rails (~> 1.8.0)
gettext_i18n_rails_js (~> 1.2.0) gettext_i18n_rails_js (~> 1.2.0)
gitaly-proto (~> 0.31.0) gitaly-proto (~> 0.32.0)
github-linguist (~> 4.7.0) github-linguist (~> 4.7.0)
gitlab-flowdock-git-hook (~> 1.0.1) gitlab-flowdock-git-hook (~> 1.0.1)
gitlab-markup (~> 1.5.1) gitlab-markup (~> 1.5.1)
......
...@@ -134,15 +134,19 @@ module Gitlab ...@@ -134,15 +134,19 @@ module Gitlab
# This is to work around a bug in libgit2 that causes in-memory refs to # This is to work around a bug in libgit2 that causes in-memory refs to
# be stale/invalid when packed-refs is changed. # be stale/invalid when packed-refs is changed.
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/15392#note_14538333 # 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) def find_branch(name, force_reload = false)
reload_rugged if force_reload 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] rugged_ref = rugged.branches[name]
if rugged_ref if rugged_ref
target_commit = Gitlab::Git::Commit.find(self, rugged_ref.target) target_commit = Gitlab::Git::Commit.find(self, rugged_ref.target)
Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target, target_commit) Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target, target_commit)
end
end
end end
end end
......
...@@ -78,6 +78,20 @@ module Gitlab ...@@ -78,6 +78,20 @@ module Gitlab
raise ArgumentError, e.message raise ArgumentError, e.message
end 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 private
def consume_refs_response(response) def consume_refs_response(response)
......
...@@ -916,27 +916,37 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -916,27 +916,37 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
describe '#find_branch' do describe '#find_branch' do
it 'should return a Branch for master' do shared_examples 'finding a branch' do
branch = repository.find_branch('master') it 'should return a Branch for master' do
branch = repository.find_branch('master')
expect(branch).to be_a_kind_of(Gitlab::Git::Branch) expect(branch).to be_a_kind_of(Gitlab::Git::Branch)
expect(branch.name).to eq('master') expect(branch.name).to eq('master')
end end
it 'should handle non-existent branch' do it 'should handle non-existent branch' do
branch = repository.find_branch('this-is-garbage') branch = repository.find_branch('this-is-garbage')
expect(branch).to eq(nil) expect(branch).to eq(nil)
end
end end
it 'should reload Rugged::Repository and return master' do context 'when Gitaly find_branch feature is enabled' do
expect(Rugged::Repository).to receive(:new).twice.and_call_original it_behaves_like 'finding a branch'
end
repository.find_branch('master') context 'when Gitaly find_branch feature is disabled', skip_gitaly_mock: true do
branch = repository.find_branch('master', force_reload: true) it_behaves_like 'finding a branch'
expect(branch).to be_a_kind_of(Gitlab::Git::Branch) it 'should reload Rugged::Repository and return master' do
expect(branch.name).to eq('master') expect(Rugged::Repository).to receive(:new).twice.and_call_original
repository.find_branch('master')
branch = repository.find_branch('master', force_reload: true)
expect(branch).to be_a_kind_of(Gitlab::Git::Branch)
expect(branch.name).to eq('master')
end
end 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