Detach head before reset

During repo reset we currently try to reset the head to the HEAD of
the remote repo. In some cases this doesn't work so simply reset
the current HEAD revision as a commit to detach the head upfront.

Change-Id: I167e6ce92680c40d2067939cedaa5ed98b950515
This commit is contained in:
Tobias Henkel 2020-04-16 14:48:32 +02:00
parent f5368eb4a4
commit febf3e9b7d
No known key found for this signature in database
GPG Key ID: 03750DEC158E5FA2
1 changed files with 13 additions and 13 deletions

View File

@ -287,20 +287,20 @@ class Repo(object):
repo = Repo._createRepoObject(local_path, env)
origin = repo.remotes.origin
# Reset the working directory to the default remote branch.
# Detach HEAD so we can work with references without interfering
# with any active branch. Any remote ref will do as long as it can
# be dereferenced to an existing commit.
for ref in origin.refs:
if ref.remote_head != "HEAD":
continue
# Use the ref the remote HEAD is pointing to
head_ref = ref.ref
head = head_ref.remote_head
repo.create_head(head, head_ref, force=True)
if log:
log.debug("Reset to %s", head)
else:
messages.append("Reset to %s" % head)
repo.head.reference = head
break
try:
repo.head.reference = ref.commit
break
except Exception:
if log:
log.debug("Unable to detach HEAD to %s", ref)
else:
messages.append("Unable to detach HEAD to %s" % ref)
else:
raise Exception("Couldn't detach HEAD to any existing commit")
# Delete local heads that no longer exist on the remote end
remote_heads = {r.remote_head for r in origin.refs}