Prune git tags on fetch

Cleanup local git tags that no longer exist on the remote end.

Change-Id: Ia3787ca081cad1cfdfcec6d92bf9595eac0dc193
This commit is contained in:
Simon Westphahl 2020-11-06 16:02:12 +01:00 committed by James E. Blair
parent d691eb9db4
commit b70a04d120
2 changed files with 75 additions and 1 deletions

View File

@ -500,6 +500,79 @@ class TestMergerRepo(ZuulTestCase):
commit = work_repo_object.commit('FETCH_HEAD')
self.assertIsNotNone(commit)
def test_delete_upstream_tag(self):
# Test that we can delete a tag from upstream and that our
# working dir will prune it.
parent_path = os.path.join(self.upstream_root, 'org/project1')
parent_repo = git.Repo(parent_path)
# Tag upstream
self.addTagToRepo('org/project1', 'testtag', 'HEAD')
commit = parent_repo.commit('testtag')
# Update downstream and verify tag matches
work_repo = Repo(parent_path, self.workspace_root,
'none@example.org', 'User Name', '0', '0')
work_repo_underlying = git.Repo(work_repo.local_path)
work_repo.update()
result = work_repo_underlying.commit('testtag')
self.assertEqual(commit, result)
# Delete tag upstream
self.delTagFromRepo('org/project1', 'testtag')
# Update downstream and verify tag is gone
work_repo.update()
with testtools.ExpectedException(git.exc.BadName):
result = work_repo_underlying.commit('testtag')
# Make a new empty commit
new_commit = parent_repo.index.commit('test commit')
self.assertNotEqual(commit, new_commit)
# Tag the new commit
self.addTagToRepo('org/project1', 'testtag', new_commit)
new_tag_commit = parent_repo.commit('testtag')
self.assertEqual(new_commit, new_tag_commit)
# Verify that the downstream tag matches
work_repo.update()
new_result = work_repo_underlying.commit('testtag')
self.assertEqual(new_commit, new_result)
def test_move_upstream_tag(self):
# Test that if an upstream tag moves, our local copy moves
# too.
parent_path = os.path.join(self.upstream_root, 'org/project1')
parent_repo = git.Repo(parent_path)
# Tag upstream
self.addTagToRepo('org/project1', 'testtag', 'HEAD')
commit = parent_repo.commit('testtag')
# Update downstream and verify tag matches
work_repo = Repo(parent_path, self.workspace_root,
'none@example.org', 'User Name', '0', '0')
work_repo_underlying = git.Repo(work_repo.local_path)
work_repo.update()
result = work_repo_underlying.commit('testtag')
self.assertEqual(commit, result)
# Make an empty commit
new_commit = parent_repo.index.commit('test commit')
self.assertNotEqual(commit, new_commit)
# Re-tag upstream
self.delTagFromRepo('org/project1', 'testtag')
self.addTagToRepo('org/project1', 'testtag', new_commit)
new_tag_commit = parent_repo.commit('testtag')
self.assertEqual(new_commit, new_tag_commit)
# Verify our downstream tag has moved
work_repo.update()
new_result = work_repo_underlying.commit('testtag')
self.assertEqual(new_commit, new_result)
class TestMergerWithAuthUrl(ZuulTestCase):
config_file = 'zuul-github-driver.conf'

View File

@ -598,7 +598,8 @@ class Repo(object):
# --tags' is all that is necessary. See
# https://github.com/git/git/blob/master/Documentation/RelNotes/1.9.0.txt#L18-L20
self._git_fetch(repo, 'origin', zuul_event_id)
self._git_fetch(repo, 'origin', zuul_event_id, tags=True, prune=True)
self._git_fetch(repo, 'origin', zuul_event_id, tags=True,
prune=True, prune_tags=True)
def isUpdateNeeded(self, repo_state, zuul_event_id=None):
repo = self.createRepoObject(zuul_event_id)