Commit 4e15e3c3 authored by Stan Hu's avatar Stan Hu

Merge branch '588-delete-only-merged-branches-when-syncing-remote-mirror' into 'master'

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

Closes #588

See merge request !968
parents 559dbe16 fc28ad13
...@@ -57,13 +57,7 @@ module Projects ...@@ -57,13 +57,7 @@ module Projects
end end
def deleted_branches def deleted_branches
@deleted_branches ||= remote_branches.each_with_object([]) do |(name, branch), branches| @deleted_branches ||= refs_to_delete(:branches)
local_branch = local_branches[name]
if local_branch.nil? && project.commit(branch.dereferenced_target)
branches << name
end
end
end end
def changed_branches def changed_branches
...@@ -119,8 +113,22 @@ module Projects ...@@ -119,8 +113,22 @@ module Projects
end end
def deleted_tags def deleted_tags
@deleted_tags ||= remote_tags.each_with_object([]) do |(name, _), tags| @deleted_tags ||= refs_to_delete(:tags)
tags << name if local_tags[name].nil? 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 end
end end
......
---
title: Don't delete branches/tags that contains changes only in the remote mirror.
merge_request: 968
author:
...@@ -73,7 +73,9 @@ describe Projects::UpdateRemoteMirrorService do ...@@ -73,7 +73,9 @@ describe Projects::UpdateRemoteMirrorService do
subject.execute(remote_mirror) subject.execute(remote_mirror)
end end
it "sync deleted branches" do 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 allow(repository).to receive(:fetch_remote) do
sync_remote(repository, remote_mirror.ref_name, local_branch_names) sync_remote(repository, remote_mirror.ref_name, local_branch_names)
delete_branch(repository, 'existing-branch') delete_branch(repository, 'existing-branch')
...@@ -85,6 +87,40 @@ describe Projects::UpdateRemoteMirrorService do ...@@ -85,6 +87,40 @@ describe Projects::UpdateRemoteMirrorService do
end end
end end
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)
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
describe 'Syncing tags' do describe 'Syncing tags' do
before do before do
allow(repository).to receive(:fetch_remote) { sync_remote(repository, remote_mirror.ref_name, local_branch_names) } allow(repository).to receive(:fetch_remote) { sync_remote(repository, remote_mirror.ref_name, local_branch_names) }
...@@ -113,7 +149,9 @@ describe Projects::UpdateRemoteMirrorService do ...@@ -113,7 +149,9 @@ describe Projects::UpdateRemoteMirrorService do
context 'when there are some tags to delete' do context 'when there are some tags to delete' do
it 'deletes tags from remote' 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') repository.rm_tag('v1.0.0')
expect(repository).to receive(:delete_remote_branches).with(remote_mirror.ref_name, ['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 ...@@ -134,6 +172,12 @@ describe Projects::UpdateRemoteMirrorService do
repository.expire_branches_cache repository.expire_branches_cache
end 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) def sync_remote(repository, remote_name, local_branch_names)
rugged = repository.rugged 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