Commit e47b7a28 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'feature/migrate-delete-refs-to-gitaly' into 'master'

Migrate Git::Repository#delete_refs to Gitaly

Closes gitaly#967

See merge request gitlab-org/gitlab-ce!16723
parents a7441398 1ebb781f
...@@ -406,7 +406,7 @@ group :ed25519 do ...@@ -406,7 +406,7 @@ group :ed25519 do
end end
# Gitaly GRPC client # Gitaly GRPC client
gem 'gitaly-proto', '~> 0.82.0', require: 'gitaly' gem 'gitaly-proto', '~> 0.83.0', require: 'gitaly'
gem 'toml-rb', '~> 0.3.15', require: false gem 'toml-rb', '~> 0.3.15', require: false
......
...@@ -285,7 +285,7 @@ GEM ...@@ -285,7 +285,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.82.0) gitaly-proto (0.83.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)
...@@ -1056,7 +1056,7 @@ DEPENDENCIES ...@@ -1056,7 +1056,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.82.0) gitaly-proto (~> 0.83.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.6.2) gitlab-markup (~> 1.6.2)
......
...@@ -888,16 +888,12 @@ module Gitlab ...@@ -888,16 +888,12 @@ module Gitlab
end end
def delete_refs(*ref_names) def delete_refs(*ref_names)
instructions = ref_names.map do |ref| gitaly_migrate(:delete_refs) do |is_enabled|
"delete #{ref}\x00\x00" if is_enabled
end gitaly_delete_refs(*ref_names)
else
message, status = run_git(%w[update-ref --stdin -z]) do |stdin| git_delete_refs(*ref_names)
stdin.write(instructions.join)
end end
unless status.zero?
raise GitError.new("Could not delete refs #{ref_names}: #{message}")
end end
end end
...@@ -2204,6 +2200,24 @@ module Gitlab ...@@ -2204,6 +2200,24 @@ module Gitlab
remote_update(remote_name, url: url) remote_update(remote_name, url: url)
end end
def git_delete_refs(*ref_names)
instructions = ref_names.map do |ref|
"delete #{ref}\x00\x00"
end
message, status = run_git(%w[update-ref --stdin -z]) do |stdin|
stdin.write(instructions.join)
end
unless status.zero?
raise GitError.new("Could not delete refs #{ref_names}: #{message}")
end
end
def gitaly_delete_refs(*ref_names)
gitaly_ref_client.delete_refs(refs: ref_names)
end
def rugged_remove_remote(remote_name) def rugged_remove_remote(remote_name)
# When a remote is deleted all its remote refs are deleted too, but in # When a remote is deleted all its remote refs are deleted too, but in
# the case of mirrors we map its refs (that would usualy go under # the case of mirrors we map its refs (that would usualy go under
......
...@@ -133,13 +133,16 @@ module Gitlab ...@@ -133,13 +133,16 @@ module Gitlab
GitalyClient.call(@repository.storage, :ref_service, :delete_branch, request) GitalyClient.call(@repository.storage, :ref_service, :delete_branch, request)
end end
def delete_refs(except_with_prefixes:) def delete_refs(refs: [], except_with_prefixes: [])
request = Gitaly::DeleteRefsRequest.new( request = Gitaly::DeleteRefsRequest.new(
repository: @gitaly_repo, repository: @gitaly_repo,
except_with_prefix: except_with_prefixes refs: refs.map { |r| encode_binary(r) },
except_with_prefix: except_with_prefixes.map { |r| encode_binary(r) }
) )
GitalyClient.call(@repository.storage, :ref_service, :delete_refs, request) response = GitalyClient.call(@repository.storage, :ref_service, :delete_refs, request)
raise Gitlab::Git::Repository::GitError, response.git_error if response.git_error.present?
end end
private private
......
...@@ -562,35 +562,39 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -562,35 +562,39 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
describe '#delete_refs' do describe '#delete_refs' do
before(:all) do shared_examples 'deleting refs' do
@repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '') let(:repo) { Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '') }
after do
ensure_seeds
end end
it 'deletes the ref' do it 'deletes the ref' do
@repo.delete_refs('refs/heads/feature') repo.delete_refs('refs/heads/feature')
expect(@repo.rugged.references['refs/heads/feature']).to be_nil expect(repo.rugged.references['refs/heads/feature']).to be_nil
end end
it 'deletes all refs' do it 'deletes all refs' do
refs = %w[refs/heads/wip refs/tags/v1.1.0] refs = %w[refs/heads/wip refs/tags/v1.1.0]
@repo.delete_refs(*refs) repo.delete_refs(*refs)
refs.each do |ref| refs.each do |ref|
expect(@repo.rugged.references[ref]).to be_nil expect(repo.rugged.references[ref]).to be_nil
end end
end end
it 'raises an error if it failed' do it 'raises an error if it failed' do
expect(@repo).to receive(:popen).and_return(['Error', 1]) expect { repo.delete_refs('refs\heads\fix') }.to raise_error(Gitlab::Git::Repository::GitError)
end
end
expect do context 'when Gitaly delete_refs feature is enabled' do
@repo.delete_refs('refs/heads/fix') it_behaves_like 'deleting refs'
end.to raise_error(Gitlab::Git::Repository::GitError)
end end
after(:all) do context 'when Gitaly delete_refs feature is disabled', :disable_gitaly do
ensure_seeds it_behaves_like 'deleting refs'
end end
end end
......
...@@ -112,7 +112,7 @@ describe Gitlab::GitalyClient::RefService do ...@@ -112,7 +112,7 @@ describe Gitlab::GitalyClient::RefService do
expect_any_instance_of(Gitaly::RefService::Stub) expect_any_instance_of(Gitaly::RefService::Stub)
.to receive(:delete_refs) .to receive(:delete_refs)
.with(gitaly_request_with_params(except_with_prefix: prefixes), kind_of(Hash)) .with(gitaly_request_with_params(except_with_prefix: prefixes), kind_of(Hash))
.and_return(double('delete_refs_response')) .and_return(double('delete_refs_response', git_error: ""))
client.delete_refs(except_with_prefixes: prefixes) client.delete_refs(except_with_prefixes: prefixes)
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