Merge "Replace reset_repo_to_head(repo) with GitPython."

This commit is contained in:
Zuul 2021-02-23 17:35:58 +00:00 committed by Gerrit Code Review
commit 03056858d0
2 changed files with 10 additions and 25 deletions

View File

@ -434,7 +434,7 @@ class FakeGerritChange(object):
self.latest_patchset), self.latest_patchset),
parent) parent)
repo.head.reference = ref repo.head.reference = ref
zuul.merger.merger.reset_repo_to_head(repo) repo.head.reset(working_tree=True)
repo.git.clean('-x', '-f', '-d') repo.git.clean('-x', '-f', '-d')
path = os.path.join(self.upstream_root, self.project) path = os.path.join(self.upstream_root, self.project)
@ -462,7 +462,7 @@ class FakeGerritChange(object):
r = repo.index.commit(msg) r = repo.index.commit(msg)
repo.head.reference = 'master' 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.git.clean('-x', '-f', '-d')
repo.heads['master'].checkout() repo.heads['master'].checkout()
return r return r
@ -864,7 +864,7 @@ class FakeGerritChange(object):
repo = git.Repo(path) repo = git.Repo(path)
repo.head.reference = self.branch 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.git.merge('-s', 'resolve', self.patchsets[-1]['ref'])
repo.heads[self.branch].commit = repo.head.commit repo.heads[self.branch].commit = repo.head.commit
@ -2424,7 +2424,7 @@ class FakeGithubPullRequest(object):
ref.set_object('refs/tags/init') ref.set_object('refs/tags/init')
self.number_of_commits += 1 self.number_of_commits += 1
repo.head.reference = ref repo.head.reference = ref
zuul.merger.merger.reset_repo_to_head(repo) repo.head.reset(working_tree=True)
repo.git.clean('-x', '-f', '-d') repo.git.clean('-x', '-f', '-d')
if files: if files:
@ -2445,7 +2445,7 @@ class FakeGithubPullRequest(object):
# each sha on a PR may have a status set on it # each sha on a PR may have a status set on it
self.statuses[self.head_sha] = [] self.statuses[self.head_sha] = []
repo.head.reference = 'master' 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.git.clean('-x', '-f', '-d')
repo.heads['master'].checkout() repo.heads['master'].checkout()
@ -4687,7 +4687,7 @@ class ZuulTestCase(BaseTestCase):
repo.create_tag(tag) repo.create_tag(tag)
repo.head.reference = master 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.git.clean('-x', '-f', '-d')
def create_branch(self, project, branch, commit_filename='README'): def create_branch(self, project, branch, commit_filename='README'):
@ -4704,14 +4704,14 @@ class ZuulTestCase(BaseTestCase):
repo.index.commit('%s commit' % branch) repo.index.commit('%s commit' % branch)
repo.head.reference = repo.heads['master'] 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') repo.git.clean('-x', '-f', '-d')
def delete_branch(self, project, branch): def delete_branch(self, project, branch):
path = os.path.join(self.upstream_root, project) path = os.path.join(self.upstream_root, project)
repo = git.Repo(path) repo = git.Repo(path)
repo.head.reference = repo.heads['master'] 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) repo.delete_head(repo.heads[branch], force=True)
def create_commit(self, project, files=None, head='master', def create_commit(self, project, files=None, head='master',
@ -5223,7 +5223,7 @@ class ZuulTestCase(BaseTestCase):
path = os.path.join(self.upstream_root, project) path = os.path.join(self.upstream_root, project)
repo = git.Repo(path) repo = git.Repo(path)
repo.head.reference = branch repo.head.reference = branch
zuul.merger.merger.reset_repo_to_head(repo) repo.head.reset(working_tree=True)
for fn, content in files.items(): for fn, content in files.items():
fn = os.path.join(path, fn) fn = os.path.join(path, fn)
try: try:

View File

@ -36,21 +36,6 @@ from zuul.lib.logutil import get_annotated_logger
NULL_REF = '0000000000000000000000000000000000000000' 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): def redact_url(url):
parsed = urlsplit(url) parsed = urlsplit(url)
if parsed.password is None: 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 # that we clean up anything that might be left over from a merge
# while still only preparing the working copy once. # while still only preparing the working copy once.
repo.head.reference = ref repo.head.reference = ref
reset_repo_to_head(repo) repo.head.reset(working_tree=True)
repo.git.clean('-x', '-f', '-d') repo.git.clean('-x', '-f', '-d')
repo.git.checkout(ref) repo.git.checkout(ref)