Explorar o código

Fix layering violation in missing HEAD fallback

The previous fix for handling missing HEAD in dumb repositories
imported dulwich.porcelain from dulwich.client, which breaks
layering as porcelain is a higher-level module.

Instead, read the default branch directly from the config at
(b"init", b"defaultBranch") with fallback to b"master", matching
the behavior of porcelain.var() but without the import.

The fallback logic only applies when origin_head is None (i.e.,
when the remote repository has no HEAD file).
Jelmer Vernooij hai 1 mes
pai
achega
f169e67ba3
Modificáronse 1 ficheiros con 16 adicións e 9 borrados
  1. 16 9
      dulwich/client.py

+ 16 - 9
dulwich/client.py

@@ -1164,17 +1164,24 @@ class GitClient:
                 else:
                     head = None
             else:
-                from dulwich import porcelain
-
                 _set_origin_head(target.refs, origin.encode("utf-8"), origin_head)
 
-                default_branch_name = porcelain.var(
-                    target, variable="GIT_DEFAULT_BRANCH"
-                ).encode("utf-8")
-                default_branch: bytes | None = default_branch_name
-                default_ref = Ref(b"refs/remotes/origin/" + default_branch_name)
-                if default_ref not in target.refs:
-                    default_branch = None
+                # If origin_head is None (missing HEAD), fall back to configured default branch
+                default_branch: bytes | None = None
+                if origin_head is None:
+                    target_config = target.get_config()
+                    try:
+                        default_branch_name = target_config.get(
+                            (b"init",), b"defaultBranch"
+                        )
+                    except KeyError:
+                        # Git's default is "master"
+                        default_branch_name = b"master"
+
+                    default_ref = Ref(b"refs/remotes/origin/" + default_branch_name)
+                    if default_ref in target.refs:
+                        default_branch = default_branch_name
+
                 head_ref = _set_default_branch(
                     target.refs,
                     origin.encode("utf-8"),