From 713b99d20bf8f92a894e86a601b4188baf2de71c Mon Sep 17 00:00:00 2001 From: mjgaughan Date: Fri, 28 Jun 2024 11:36:41 -0400 Subject: [PATCH] [git] Add deletion of old tag references Co-authored-by: Venu Vardhan Reddy Tekula Signed-off-by: mjgaughan --- perceval/backends/core/git.py | 2 +- releases/unreleased/fix-issue-#782.yml | 6 ++++ tests/test_git.py | 40 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 releases/unreleased/fix-issue-#782.yml diff --git a/perceval/backends/core/git.py b/perceval/backends/core/git.py index cac293cf6..2d00f7f0c 100644 --- a/perceval/backends/core/git.py +++ b/perceval/backends/core/git.py @@ -1297,7 +1297,7 @@ def _update_references(self, refs): # Delete old references for old_ref in self._discover_refs(): - if not old_ref.refname.startswith('refs/heads/'): + if not old_ref.refname.startswith('refs/heads/') and not old_ref.refname.startswith('refs/tags/'): continue if old_ref.refname in new_refs: continue diff --git a/releases/unreleased/fix-issue-#782.yml b/releases/unreleased/fix-issue-#782.yml new file mode 100644 index 000000000..882b8635f --- /dev/null +++ b/releases/unreleased/fix-issue-#782.yml @@ -0,0 +1,6 @@ +title: 'Fix issue #782' +category: fixed +author: Matt Gaughan +issue: 782 +notes: > + The issue was that perceval would not delete old tags from upstream references. This change deletes tags locally if tags are deleted upstream. \ No newline at end of file diff --git a/tests/test_git.py b/tests/test_git.py index f3b17f107..83c5d94d3 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -1800,6 +1800,46 @@ def test_sync(self): shutil.rmtree(editable_path) shutil.rmtree(new_path) + def test_tag_removal_sync(self): + """Test sync process for tag removal""" + origin_path = os.path.join(self.tmp_repo_path, 'gittest') + editable_path = os.path.join(self.tmp_path, 'editgit') + new_path = os.path.join(self.tmp_path, 'newgit') + + shutil.copytree(origin_path, editable_path) + + repo = GitRepository.clone(editable_path, new_path) + repo.sync() + + # Add a tag 'v.0.0-lw' to the refs and check that it exists + cmd = ['git', 'tag', 'v.0.0-lw'] + subprocess.check_output(cmd, stderr=subprocess.STDOUT, + cwd=editable_path, env={'LANG': 'C'}) + + new_commits = repo.sync() + self.assertEqual(len(new_commits), 0) + + # Verify that the new tag 'v.0.0-lw' exists in the refs + refs = discover_refs(new_path) + self.assertIn('refs/tags/v.0.0-lw', refs) + + # Delete the tag 'v.0.0-lw' and check that it has been deleted from refs + cmd = ['git', 'tag', '-d', 'v.0.0-lw'] + subprocess.check_output(cmd, stderr=subprocess.STDOUT, + cwd=editable_path, env={'LANG': 'C'}) + + new_commits = repo.sync() + self.assertEqual(len(new_commits), 0) + + # Verify that the tag 'v.0.0-lw' is no longer in the refs + refs = discover_refs(new_path) + self.assertNotIn('refs/tags/v.0.0-lw', refs) + self.assertIn('refs/heads/master', refs) + + # Cleanup + shutil.rmtree(editable_path) + shutil.rmtree(new_path) + def test_sync_from_empty_repos(self): """Test sync process on empty repositories"""