Skip to content

Commit f2d1c4c

Browse files
codexByron
authored andcommitted
fix: initialize common directory before config
Capture GIT_COMMON_DIR before the first repository config read so bare-state detection uses the same metadata location as discovery. Resolve relative environment values immediately so later working-directory changes cannot retarget the Repo. This addresses both substantive review findings on ebe02652. The regression now covers relative common storage, bare config, and use after leaving the construction directory. Validation: 9 focused tests and 4 subtests; Ruff check/format; mypy; git diff --check.
1 parent e003731 commit f2d1c4c

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

git/repo/base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,23 +356,23 @@ def __init__(
356356
raise InvalidGitRepositoryError(epath)
357357
self.git_dir = git_dir
358358

359-
self._bare = False
360-
try:
361-
self._bare = self.config_reader("repository").getboolean("core", "bare")
362-
except Exception:
363-
# Let's not assume the option exists, although it should.
364-
pass
365-
366359
common_dir = os.getenv("GIT_COMMON_DIR")
367360
if common_dir is not None:
368-
self._common_dir = common_dir
361+
self._common_dir = osp.abspath(common_dir)
369362
else:
370363
try:
371364
common_dir = (Path(self.git_dir) / "commondir").read_text().splitlines()[0].strip()
372365
self._common_dir = osp.join(self.git_dir, common_dir)
373366
except OSError:
374367
self._common_dir = ""
375368

369+
self._bare = False
370+
try:
371+
self._bare = self.config_reader("repository").getboolean("core", "bare")
372+
except Exception:
373+
# Let's not assume the option exists, although it should.
374+
pass
375+
376376
# Adjust the working directory in case we are actually bare - we didn't know
377377
# that in the first place.
378378
if self._bare:

test/test_repo.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,15 @@ def test_repo_discovery_uses_git_common_dir(self):
171171
(git_dir / "HEAD").write_text("ref: refs/heads/main\n")
172172
(common_dir / "objects").mkdir()
173173
(common_dir / "refs").mkdir()
174+
(common_dir / "config").write_text("[core]\n\tbare = true\n")
174175

175-
with mock.patch.dict(os.environ, {"GIT_DIR": str(git_dir), "GIT_COMMON_DIR": str(common_dir)}):
176-
repo = Repo()
176+
with cwd(tdir):
177+
with mock.patch.dict(os.environ, {"GIT_DIR": "git", "GIT_COMMON_DIR": "common"}):
178+
repo = Repo()
177179

178180
assert osp.samefile(repo.common_dir, common_dir)
179181
assert osp.samefile(repo.odb.root_path(), common_dir / "objects")
182+
assert repo.bare
180183

181184
@with_rw_repo("0.3.2.1")
182185
def test_repo_creation_from_different_paths(self, rw_repo):

0 commit comments

Comments
 (0)