Address review feedback about repository discovery · gitpython-developers/GitPython@32baeab · GitHub
Skip to content

Commit 32baeab

Browse files
codexByron
authored andcommitted
Address review feedback about repository discovery
Review feedback: relative GIT_COMMON_DIR left Git subprocesses resolving GIT_DIR and GIT_COMMON_DIR from a different working directory; malformed commondir data, empty GIT_OBJECT_DIRECTORY, and the linked-worktree signature were also handled inconsistently. Pin the repository environment to resolved paths, reject invalid metadata without consulting the process working directory, and restore HEAD-based linked-worktree detection. Keep the loose HEAD and dangling .git behavior because both match Git setup.c at baseline 15c6308cf7ad276b306aa5b3ababfbdebfb1a917. Validation: 3 focused tests and 9 subtests; Ruff check and format; mypy; compileall; git diff --check.
1 parent f2d1c4c commit 32baeab

3 files changed

Lines changed: 40 additions & 13 deletions

File tree

git/repo/base.py

Lines changed: 5 additions & 3 deletions

git/repo/fun.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,29 @@ def is_git_dir(d: PathLike) -> bool:
8484
)
8585

8686
common_dir = os.getenv("GIT_COMMON_DIR")
87+
if common_dir == "":
88+
return False
8789
if common_dir is None:
8890
try:
89-
common_dir = (Path(d) / "commondir").read_text().rstrip("\r\n")
91+
common_dir = os.fsdecode((Path(d) / "commondir").read_bytes()).rstrip("\r\n")
9092
except FileNotFoundError:
9193
common_dir = os.fspath(d)
9294
except OSError:
93-
common_dir = ""
95+
return False
9496
else:
95-
common_dir = osp.realpath(osp.join(d, common_dir)) if common_dir else ""
96-
97-
object_dir = os.getenv("GIT_OBJECT_DIRECTORY") or osp.join(common_dir, "objects")
97+
if not common_dir:
98+
return False
99+
try:
100+
common_dir = osp.realpath(osp.join(d, common_dir))
101+
except (OSError, ValueError):
102+
return False
103+
104+
object_dir = os.getenv("GIT_OBJECT_DIRECTORY")
105+
if object_dir is None:
106+
object_dir = osp.join(common_dir, "objects")
98107
if valid_head and osp.isdir(object_dir) and osp.isdir(osp.join(common_dir, "refs")):
99108
return True
100-
if (
101-
osp.isfile(osp.join(d, "gitdir"))
102-
and osp.isfile(osp.join(d, "commondir"))
103-
and osp.isfile(osp.join(d, "gitfile"))
104-
):
109+
if osp.isfile(osp.join(d, "gitdir")) and osp.isfile(osp.join(d, "commondir")) and osp.isfile(headref):
105110
raise WorkTreeRepositoryUnsupported(d)
106111
return False
107112

test/test_repo.py

Lines changed: 20 additions & 0 deletions

0 commit comments

Comments
 (0)