Commit f4c008bd authored by Tiago Botelho's avatar Tiago Botelho

Mirroring to remote repository no longer fails after a force push.

parent fd326fc3
...@@ -11,10 +11,6 @@ module Projects ...@@ -11,10 +11,6 @@ module Projects
begin begin
repository.fetch_remote(mirror.ref_name, no_tags: true) repository.fetch_remote(mirror.ref_name, no_tags: true)
if divergent_branches.present?
errors << "The following branches have diverged from their local counterparts: #{divergent_branches.to_sentence}"
end
push_branches if changed_branches.present? push_branches if changed_branches.present?
delete_branches if deleted_branches.present? delete_branches if deleted_branches.present?
...@@ -70,15 +66,7 @@ module Projects ...@@ -70,15 +66,7 @@ module Projects
branches << name branches << name
elsif branch.dereferenced_target == remote_branch.dereferenced_target elsif branch.dereferenced_target == remote_branch.dereferenced_target
# Already up to date # Already up to date
elsif !repository.upstream_has_diverged?(name, mirror.ref_name) else
branches << name
end
end
end
def divergent_branches
remote_branches.each_with_object([]) do |(name, _), branches|
if local_branches[name] && repository.upstream_has_diverged?(name, mirror.ref_name)
branches << name branches << name
end end
end end
......
---
title: Mirroring to remote repository no longer fails after a force push.
merge_request: 2919
author:
type: fixed
...@@ -388,10 +388,14 @@ module Gitlab ...@@ -388,10 +388,14 @@ module Gitlab
# Ex. # Ex.
# push_remote_branches('upstream', 'feature') # push_remote_branches('upstream', 'feature')
# #
def push_remote_branches(storage, project_name, remote_name, branch_names) def push_remote_branches(storage, project_name, remote_name, branch_names, forced: true)
args = [gitlab_shell_projects_path, 'push-branches', storage, "#{project_name}.git", remote_name, '600', *branch_names] args = [gitlab_shell_projects_path, 'push-branches', storage, "#{project_name}.git", remote_name, '600']
args << '--force' if forced
args += [*branch_names]
output, status = Popen.popen(args) output, status = Popen.popen(args)
raise Error, output unless status.zero? raise Error, output unless status.zero?
true true
end end
......
...@@ -77,16 +77,16 @@ describe Gitlab::Shell do ...@@ -77,16 +77,16 @@ describe Gitlab::Shell do
end end
describe '#push_remote_branches' do describe '#push_remote_branches' do
let!(:args) { [projects_path, 'push-branches', 'current/storage', 'project/path.git', 'new/storage', '600', '--force', 'master'] }
it 'executes the command' do it 'executes the command' do
expect(Gitlab::Popen).to receive(:popen) expect(Gitlab::Popen).to receive(:popen).with(args).and_return([nil, 0])
.with([projects_path, 'push-branches', 'current/storage', 'project/path.git', 'new/storage', '600', 'master']).and_return([nil, 0])
expect(gitlab_shell.push_remote_branches('current/storage', 'project/path', 'new/storage', ['master'])).to be true expect(gitlab_shell.push_remote_branches('current/storage', 'project/path', 'new/storage', ['master'])).to be true
end end
it 'fails to execute the command' do it 'fails to execute the command' do
expect(Gitlab::Popen).to receive(:popen) expect(Gitlab::Popen).to receive(:popen).with(args).and_return(["error", 1])
.with([projects_path, 'push-branches', 'current/storage', 'project/path.git', 'new/storage', '600', 'master']).and_return(["error", 1])
expect { gitlab_shell.push_remote_branches('current/storage', 'project/path', 'new/storage', ['master']) }.to raise_error(Gitlab::Shell::Error, "error") expect { gitlab_shell.push_remote_branches('current/storage', 'project/path', 'new/storage', ['master']) }.to raise_error(Gitlab::Shell::Error, "error")
end end
......
...@@ -81,6 +81,21 @@ describe Projects::UpdateRemoteMirrorService do ...@@ -81,6 +81,21 @@ describe Projects::UpdateRemoteMirrorService do
subject.execute(remote_mirror) subject.execute(remote_mirror)
end end
context 'when branch exists in local and remote repo' do
context 'when it has diverged' do
it 'syncs branches' do
allow(repository).to receive(:fetch_remote) do
sync_remote(repository, remote_mirror.ref_name, local_branch_names)
update_remote_branch(repository, remote_mirror.ref_name, 'markdown')
end
expect(repository).to receive(:push_remote_branches).with(remote_mirror.ref_name, ['markdown'])
subject.execute(remote_mirror)
end
end
end
describe 'for delete' do describe 'for delete' do
context 'when branch exists in local and remote repo' do context 'when branch exists in local and remote repo' do
it 'deletes the branch from remote repo' do it 'deletes the branch from remote repo' do
...@@ -195,6 +210,14 @@ describe Projects::UpdateRemoteMirrorService do ...@@ -195,6 +210,14 @@ describe Projects::UpdateRemoteMirrorService do
end end
end end
def update_remote_branch(repository, remote_name, branch)
rugged = repository.rugged
masterrev = repository.find_branch('master').dereferenced_target.id
rugged.references.create("refs/remotes/#{remote_name}/#{branch}", masterrev, force: true)
repository.expire_branches_cache
end
def update_branch(repository, branch) def update_branch(repository, branch)
rugged = repository.rugged rugged = repository.rugged
masterrev = repository.find_branch('master').dereferenced_target.id masterrev = repository.find_branch('master').dereferenced_target.id
......
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