Create remote ref when it does not exist

It can happen that the remote ref (corresponding to the branch in
cache) is not available when local workspace is cloned.

Fix this issue by creating the remote ref when it does not exist.

Change-Id: I68244e0b5aa3c8b6e15693ffc2897d4f416e0d5c
This commit is contained in:
Dong Zhang 2022-03-18 15:59:24 +01:00
parent 5b3d0bbe48
commit f991c3fdc6
2 changed files with 8 additions and 7 deletions

View File

@ -184,12 +184,14 @@ class TestMergerRepo(ZuulTestCase):
work_repo = Repo(parent_path, self.workspace_root,
'none@example.org', 'User Name', '0', '0')
work_repo.setRemoteRef('master', commit_sha)
work_repo.setRemoteRef('invalid', commit_sha)
# missing remote ref would be created
work_repo.setRemoteRef('missing', commit_sha)
repo = git.Repo(self.workspace_root)
self.assertEqual(repo.remotes.origin.refs.master.commit.hexsha,
commit_sha)
self.assertNotIn('invalid', repo.remotes.origin.refs)
self.assertEqual(repo.remotes.origin.refs.missing.commit.hexsha,
commit_sha)
def test_clone_timeout(self):
parent_path = os.path.join(self.upstream_root, 'org/project1')

View File

@ -497,12 +497,11 @@ class Repo(object):
log = get_annotated_logger(self.log, zuul_event_id)
repo = self.createRepoObject(zuul_event_id)
try:
origin_ref = repo.remotes.origin.refs[branch]
log.debug("Updating remote reference origin/%s to %s", branch, rev)
repo.remotes.origin.refs[branch].commit = rev
except IndexError:
log.warning("No remote ref found for branch %s", branch)
return
log.debug("Updating remote reference %s to %s", origin_ref, rev)
origin_ref.commit = rev
log.warning("No remote ref found for branch %s, creating", branch)
Repo._setRef(f"refs/remotes/origin/{branch}", str(rev), repo)
def deleteRef(self, path, repo=None, zuul_event_id=None):
ref_log = get_annotated_logger(