From 75d5dc2289d4b85b5e2d721d3fdbafdf5779e02b Mon Sep 17 00:00:00 2001 From: Albin Vass Date: Sat, 13 Feb 2021 12:13:05 +0100 Subject: [PATCH] Replace reset_repo_to_head(repo) with GitPython. The bugfix to disambiguate filepath from ref has been merged for a long time so it should be safe to replace the method with gitpython. See: https://github.com/gitpython-developers/GitPython/pull/319 Change-Id: Ic0c5253273ea47da6567047c9172adbf513c1500 --- tests/base.py | 18 +++++++++--------- zuul/merger/merger.py | 17 +---------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/tests/base.py b/tests/base.py index a58a9306d1..3d88e8aebf 100644 --- a/tests/base.py +++ b/tests/base.py @@ -434,7 +434,7 @@ class FakeGerritChange(object): self.latest_patchset), parent) repo.head.reference = ref - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.git.clean('-x', '-f', '-d') path = os.path.join(self.upstream_root, self.project) @@ -462,7 +462,7 @@ class FakeGerritChange(object): r = repo.index.commit(msg) repo.head.reference = 'master' - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.git.clean('-x', '-f', '-d') repo.heads['master'].checkout() return r @@ -864,7 +864,7 @@ class FakeGerritChange(object): repo = git.Repo(path) repo.head.reference = self.branch - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.git.merge('-s', 'resolve', self.patchsets[-1]['ref']) repo.heads[self.branch].commit = repo.head.commit @@ -2424,7 +2424,7 @@ class FakeGithubPullRequest(object): ref.set_object('refs/tags/init') self.number_of_commits += 1 repo.head.reference = ref - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.git.clean('-x', '-f', '-d') if files: @@ -2445,7 +2445,7 @@ class FakeGithubPullRequest(object): # each sha on a PR may have a status set on it self.statuses[self.head_sha] = [] repo.head.reference = 'master' - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.git.clean('-x', '-f', '-d') repo.heads['master'].checkout() @@ -4687,7 +4687,7 @@ class ZuulTestCase(BaseTestCase): repo.create_tag(tag) repo.head.reference = master - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.git.clean('-x', '-f', '-d') def create_branch(self, project, branch, commit_filename='README'): @@ -4704,14 +4704,14 @@ class ZuulTestCase(BaseTestCase): repo.index.commit('%s commit' % branch) repo.head.reference = repo.heads['master'] - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.git.clean('-x', '-f', '-d') def delete_branch(self, project, branch): path = os.path.join(self.upstream_root, project) repo = git.Repo(path) repo.head.reference = repo.heads['master'] - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.delete_head(repo.heads[branch], force=True) def create_commit(self, project, files=None, head='master', @@ -5223,7 +5223,7 @@ class ZuulTestCase(BaseTestCase): path = os.path.join(self.upstream_root, project) repo = git.Repo(path) repo.head.reference = branch - zuul.merger.merger.reset_repo_to_head(repo) + repo.head.reset(working_tree=True) for fn, content in files.items(): fn = os.path.join(path, fn) try: diff --git a/zuul/merger/merger.py b/zuul/merger/merger.py index f7017f9851..32a87e79c1 100644 --- a/zuul/merger/merger.py +++ b/zuul/merger/merger.py @@ -36,21 +36,6 @@ from zuul.lib.logutil import get_annotated_logger NULL_REF = '0000000000000000000000000000000000000000' -def reset_repo_to_head(repo): - # This lets us reset the repo even if there is a file in the root - # directory named 'HEAD'. Currently, GitPython does not allow us - # to instruct it to always include the '--' to disambiguate. This - # should no longer be necessary if this PR merges: - # https://github.com/gitpython-developers/GitPython/pull/319 - try: - repo.git.reset('--hard', 'HEAD', '--') - except git.GitCommandError as e: - # git nowadays may use 1 as status to indicate there are still unstaged - # modifications after the reset - if e.status != 1: - raise - - def redact_url(url): parsed = urlsplit(url) if parsed.password is None: @@ -542,7 +527,7 @@ class Repo(object): # that we clean up anything that might be left over from a merge # while still only preparing the working copy once. repo.head.reference = ref - reset_repo_to_head(repo) + repo.head.reset(working_tree=True) repo.git.clean('-x', '-f', '-d') repo.git.checkout(ref)