Cloning with a remote callback results in HEAD pointing to the user's init.defaultBranch instead of the default branch of the cloned repository. See repro below.
Looks like an upstream bug to me, just confirming with you... thanks :)
I'll also note that HEAD already contains the wrong reference before the callback is executed. (Reproduce this e.g. by placing a breakpoint() in remote_cb below, and inspecting HEAD from the debugger prompt.) Still, the issue does not reproduce without the callback.
import contextlib
import pygit2
def _fix_repository_head(repository: pygit2.Repository) -> pygit2.Reference:
"""Work around a bug in libgit2 resulting in a bogus HEAD reference.
Cloning with a remote callback results in HEAD pointing to the user's
`init.defaultBranch` instead of the default branch of the cloned repository.
"""
head = repository.references["HEAD"]
with contextlib.suppress(KeyError):
return head.resolve()
for branch in ["main", "master"]:
ref = f"refs/heads/{branch}"
if head.target != ref and ref in repository.references:
head.set_target(ref, message="repair broken HEAD after clone")
return head.resolve()
Cloning with a remote callback results in
HEAD
pointing to the user'sinit.defaultBranch
instead of the default branch of the cloned repository. See repro below.Looks like an upstream bug to me, just confirming with you... thanks :)
I'll also note that
HEAD
already contains the wrong reference before the callback is executed. (Reproduce this e.g. by placing abreakpoint()
inremote_cb
below, and inspectingHEAD
from the debugger prompt.) Still, the issue does not reproduce without the callback.Repro
Workaround