diff --git a/app/models/remote_mirror.rb b/app/models/remote_mirror.rb index 1e5c93cd9139b74a9e42bd22d4b7c280e4b366d5..0334d63dd369057a0902a652c9ffdcffd74546c0 100644 --- a/app/models/remote_mirror.rb +++ b/app/models/remote_mirror.rb @@ -117,6 +117,8 @@ def update_repository(options) end end + options[:keep_divergent_refs] = keep_divergent_refs? + Gitlab::Git::RemoteMirror.new( project.repository.raw, remote_name, diff --git a/lib/gitlab/git/remote_mirror.rb b/lib/gitlab/git/remote_mirror.rb index df3cd422527d671e2bbc1929fb799a6fd8a8a43a..d9f51a7e84446bdd2aa293f0d6260bcd9a1c62f2 100644 --- a/lib/gitlab/git/remote_mirror.rb +++ b/lib/gitlab/git/remote_mirror.rb @@ -5,14 +5,15 @@ module Git class RemoteMirror include Gitlab::Git::WrapsGitalyErrors - attr_reader :repository, :ref_name, :only_branches_matching, :ssh_key, :known_hosts + attr_reader :repository, :ref_name, :only_branches_matching, :ssh_key, :known_hosts, :keep_divergent_refs - def initialize(repository, ref_name, only_branches_matching: [], ssh_key: nil, known_hosts: nil) + def initialize(repository, ref_name, only_branches_matching: [], ssh_key: nil, known_hosts: nil, keep_divergent_refs: false) @repository = repository @ref_name = ref_name @only_branches_matching = only_branches_matching @ssh_key = ssh_key @known_hosts = known_hosts + @keep_divergent_refs = keep_divergent_refs end def update @@ -21,7 +22,8 @@ def update ref_name, only_branches_matching, ssh_key: ssh_key, - known_hosts: known_hosts + known_hosts: known_hosts, + keep_divergent_refs: keep_divergent_refs ) end end diff --git a/lib/gitlab/gitaly_client/remote_service.rb b/lib/gitlab/gitaly_client/remote_service.rb index 2405f3be197a852b98fd87a59e2139199a2b486d..4566c59bbe0ed28cac07a01a5bfe384bf92120d7 100644 --- a/lib/gitlab/gitaly_client/remote_service.rb +++ b/lib/gitlab/gitaly_client/remote_service.rb @@ -53,7 +53,7 @@ def find_remote_root_ref(remote_name) encode_utf8(response.ref) end - def update_remote_mirror(ref_name, only_branches_matching, ssh_key: nil, known_hosts: nil) + def update_remote_mirror(ref_name, only_branches_matching, ssh_key: nil, known_hosts: nil, keep_divergent_refs: false) req_enum = Enumerator.new do |y| first_request = Gitaly::UpdateRemoteMirrorRequest.new( repository: @gitaly_repo, @@ -62,6 +62,7 @@ def update_remote_mirror(ref_name, only_branches_matching, ssh_key: nil, known_h first_request.ssh_key = ssh_key if ssh_key.present? first_request.known_hosts = known_hosts if known_hosts.present? + first_request.keep_divergent_refs = keep_divergent_refs y.yield(first_request) diff --git a/spec/lib/gitlab/git/remote_mirror_spec.rb b/spec/lib/gitlab/git/remote_mirror_spec.rb index 9744562b51bb613383d53f2c7fce1d139fcd8c1a..edef91b8bc6a7ccbbb3186ca8d5f2f596cdc1a68 100644 --- a/spec/lib/gitlab/git/remote_mirror_spec.rb +++ b/spec/lib/gitlab/git/remote_mirror_spec.rb @@ -7,14 +7,14 @@ let(:project) { create(:project, :repository) } let(:repository) { project.repository } let(:ref_name) { 'foo' } - let(:options) { { only_branches_matching: ['master'], ssh_key: 'KEY', known_hosts: 'KNOWN HOSTS' } } + let(:options) { { only_branches_matching: ['master'], ssh_key: 'KEY', known_hosts: 'KNOWN HOSTS', keep_divergent_refs: true } } subject(:remote_mirror) { described_class.new(repository, ref_name, **options) } it 'delegates to the Gitaly client' do expect(repository.gitaly_remote_client) .to receive(:update_remote_mirror) - .with(ref_name, ['master'], ssh_key: 'KEY', known_hosts: 'KNOWN HOSTS') + .with(ref_name, ['master'], ssh_key: 'KEY', known_hosts: 'KNOWN HOSTS', keep_divergent_refs: true) remote_mirror.update end diff --git a/spec/lib/gitlab/gitaly_client/remote_service_spec.rb b/spec/lib/gitlab/gitaly_client/remote_service_spec.rb index 2658414d9b02cd920fd01875c37cb8a72e7e92b0..2bddec739fc478e210ddde3f5427dc9242cf0efb 100644 --- a/spec/lib/gitlab/gitaly_client/remote_service_spec.rb +++ b/spec/lib/gitlab/gitaly_client/remote_service_spec.rb @@ -66,7 +66,7 @@ .with(kind_of(Enumerator), kind_of(Hash)) .and_return(double(:update_remote_mirror_response)) - client.update_remote_mirror(ref_name, only_branches_matching, ssh_key: ssh_key, known_hosts: known_hosts) + client.update_remote_mirror(ref_name, only_branches_matching, ssh_key: ssh_key, known_hosts: known_hosts, keep_divergent_refs: true) end end diff --git a/spec/models/remote_mirror_spec.rb b/spec/models/remote_mirror_spec.rb index f5e718e0e099128b95a411ed894667c6f9921d57..15b162ae87a8b8677bfd1c0e134fd819bd0ed087 100644 --- a/spec/models/remote_mirror_spec.rb +++ b/spec/models/remote_mirror_spec.rb @@ -142,6 +142,26 @@ end end + describe '#update_repository' do + let(:git_remote_mirror) { spy } + + before do + stub_const('Gitlab::Git::RemoteMirror', git_remote_mirror) + end + + it 'includes the `keep_divergent_refs` setting' do + mirror = build_stubbed(:remote_mirror, keep_divergent_refs: true) + + mirror.update_repository({}) + + expect(git_remote_mirror).to have_received(:new).with( + anything, + mirror.remote_name, + hash_including(keep_divergent_refs: true) + ) + end + end + describe '#safe_url' do context 'when URL contains credentials' do it 'masks the credentials' do