1
0
Fork 0

project: Use plumbing commands to manage HEAD

Don't directly manipulate `.git/HEAD` since it bypasses Git's internal
state management.

Bug: 432200791
Change-Id: I1c9264bcf107d34574a82b60a22ea2c83792951b
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/491841
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Scott Lee <ddoman@google.com>
Tested-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Gavin Mak 2025-07-17 17:54:22 +00:00 committed by LUCI
parent 5d95ba8d85
commit 74edacd8e5

View file

@ -2061,10 +2061,7 @@ class Project:
if head == revid:
# Same revision; just update HEAD to point to the new
# target branch, but otherwise take no other action.
_lwrite(
self.work_git.GetDotgitPath(subpath=HEAD),
f"ref: {R_HEADS}{name}\n",
)
self.work_git.SetHead(R_HEADS + name)
return True
GitCommand(
@ -2100,9 +2097,7 @@ class Project:
revid = self.GetRevisionId(all_refs)
if head == revid:
_lwrite(
self.work_git.GetDotgitPath(subpath=HEAD), "%s\n" % revid
)
self.work_git.DetachHead(revid)
else:
self._Checkout(revid, quiet=True)
GitCommand(
@ -3492,9 +3487,7 @@ class Project:
self._createDotGit(dotgit)
if init_dotgit:
_lwrite(
os.path.join(self.gitdir, HEAD), f"{self.GetRevisionId()}\n"
)
self.work_git.UpdateRef(HEAD, self.GetRevisionId(), detach=True)
# Finish checking out the worktree.
cmd = ["read-tree", "--reset", "-u", "-v", HEAD]