Skip to content

Commit c270a04

Browse files
codexByron
authored andcommitted
Address review feedback about empty repository paths
The commit review noted that treating every present GIT_DIR as explicit broke two documented cases: an empty GIT_DIR must fall back to current-directory discovery, while an empty Repo path must still use a nonempty GIT_DIR. Base explicit-mode selection on the same truthiness rules used to choose epath. Cover both boundaries alongside the nested-.git regression. Validation: focused test with both subtests; Ruff check and format; mypy; git diff --check.
1 parent fe2ae00 commit c270a04

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

git/repo/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def __init__(
295295
# Walk up the path to find the `.git` dir.
296296
curpath = os.fspath(epath) if epath is not None else ""
297297
git_dir: Optional[str] = None
298-
explicit_git_dir = path is None and git_dir_env is not None
298+
explicit_git_dir = not path and bool(git_dir_env)
299299
while curpath:
300300
# ABOUT osp.NORMPATH
301301
# It's important to normalize the paths, as submodules will otherwise

test/test_repo.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,15 @@ def test_repo_discovery_honors_explicit_git_dir(self):
155155
Repo.init(git_dir / ".git", bare=True).close()
156156

157157
with mock.patch.dict(os.environ, {"GIT_DIR": os.fspath(git_dir)}):
158+
for path in (None, ""):
159+
with Repo(path) as repo, self.subTest(path=path):
160+
assert osp.samefile(repo.git_dir, git_dir)
161+
162+
worktree = Path(tdir) / "worktree"
163+
Repo.init(worktree).close()
164+
with cwd(worktree), mock.patch.dict(os.environ, {"GIT_DIR": ""}):
158165
with Repo() as repo:
159-
assert osp.samefile(repo.git_dir, git_dir)
166+
assert osp.samefile(repo.git_dir, worktree / ".git")
160167

161168
def test_repo_discovery_rejects_invalid_metadata(self):
162169
with tempfile.TemporaryDirectory() as tdir:

0 commit comments

Comments
 (0)