Commit fc28ad13 authored by Ruben Davila's avatar Ruben Davila

Don't delete branches/tags that contains changes only in the remote mirror.

parent ff307843
......@@ -57,13 +57,7 @@ module Projects
end
def deleted_branches
@deleted_branches ||= remote_branches.each_with_object([]) do |(name, branch), branches|
local_branch = local_branches[name]
if local_branch.nil? && project.commit(branch.dereferenced_target)
branches << name
end
end
@deleted_branches ||= refs_to_delete(:branches)
end
def changed_branches
......@@ -119,8 +113,22 @@ module Projects
end
def deleted_tags
@deleted_tags ||= remote_tags.each_with_object([]) do |(name, _), tags|
tags << name if local_tags[name].nil?
@deleted_tags ||= refs_to_delete(:tags)
end
def refs_to_delete(type)
remote_refs = send("remote_#{type}")
local_refs = send("local_#{type}")
default_branch_id = project.commit.id
remote_refs.each_with_object([]) do |(name, remote_ref), refs_to_delete|
next if local_refs[name] # skip if branch or tag exist in local repo
remote_ref_id = remote_ref.dereferenced_target.try(:id)
if remote_ref_id && project.repository.is_ancestor?(remote_ref_id, default_branch_id)
refs_to_delete << name
end
end
end
end
......
---
title: Don't delete branches/tags that contains changes only in the remote mirror.
merge_request: 968
author:
......@@ -73,15 +73,51 @@ describe Projects::UpdateRemoteMirrorService do
subject.execute(remote_mirror)
end
it "sync deleted branches" do
allow(repository).to receive(:fetch_remote) do
sync_remote(repository, remote_mirror.ref_name, local_branch_names)
delete_branch(repository, 'existing-branch')
describe 'for delete' do
context 'when branch exists in local and remote repo' do
it 'deletes the branch from remote repo' do
allow(repository).to receive(:fetch_remote) do
sync_remote(repository, remote_mirror.ref_name, local_branch_names)
delete_branch(repository, 'existing-branch')
end
expect(repository).to receive(:delete_remote_branches).with(remote_mirror.ref_name, ['existing-branch'])
subject.execute(remote_mirror)
end
end
expect(repository).to receive(:delete_remote_branches).with(remote_mirror.ref_name, ['existing-branch'])
context 'when branch only exists on remote repo' do
context 'when it has diverged' do
it 'does not delete the remote branch' do
allow(repository).to receive(:fetch_remote) do
sync_remote(repository, remote_mirror.ref_name, local_branch_names)
subject.execute(remote_mirror)
blob_id = 'c74175afd117781cbc983664339a0f599b5bb34e'
create_remote_branch(repository, remote_mirror.ref_name, 'remote-branch', blob_id)
end
expect(repository).not_to receive(:delete_remote_branches)
subject.execute(remote_mirror)
end
end
context 'when it has not diverged' do
it 'deletes the remote branch' do
allow(repository).to receive(:fetch_remote) do
sync_remote(repository, remote_mirror.ref_name, local_branch_names)
masterrev = repository.find_branch('master').dereferenced_target
create_remote_branch(repository, remote_mirror.ref_name, 'remote-branch', masterrev.id)
end
expect(repository).to receive(:delete_remote_branches).with(remote_mirror.ref_name, ['remote-branch'])
subject.execute(remote_mirror)
end
end
end
end
end
......@@ -113,7 +149,9 @@ describe Projects::UpdateRemoteMirrorService do
context 'when there are some tags to delete' do
it 'deletes tags from remote' do
allow(repository).to receive(:remote_tags) { generate_tags(repository, 'v1.0.0', 'v1.1.0') }
remote_tags = generate_tags(repository, 'v1.0.0', 'v1.1.0')
allow(repository).to receive(:remote_tags) { remote_tags }
repository.rm_tag('v1.0.0')
expect(repository).to receive(:delete_remote_branches).with(remote_mirror.ref_name, ['v1.0.0'])
......@@ -134,6 +172,12 @@ describe Projects::UpdateRemoteMirrorService do
repository.expire_branches_cache
end
def create_remote_branch(repository, remote_name, branch_name, source_id)
rugged = repository.rugged
rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", source_id)
end
def sync_remote(repository, remote_name, local_branch_names)
rugged = repository.rugged
......
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