Skip to content

Commit e003731

Browse files
codexByron
authored andcommitted
fix: retain GIT_COMMON_DIR during discovery
Repository validation can use GIT_COMMON_DIR for refs and objects. Preserve the same value on Repo so later config, ref, and object access uses the directory that made discovery succeed. This addresses the substantive review finding on a6a38a85 and adds focused coverage for environment-provided common storage. Validation: 9 focused tests and 4 subtests; Ruff check/format; mypy; compileall; git diff --check.
1 parent 1424148 commit e003731

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

git/repo/base.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,15 @@ def __init__(
363363
# Let's not assume the option exists, although it should.
364364
pass
365365

366-
try:
367-
common_dir = (Path(self.git_dir) / "commondir").read_text().splitlines()[0].strip()
368-
self._common_dir = osp.join(self.git_dir, common_dir)
369-
except OSError:
370-
self._common_dir = ""
366+
common_dir = os.getenv("GIT_COMMON_DIR")
367+
if common_dir is not None:
368+
self._common_dir = common_dir
369+
else:
370+
try:
371+
common_dir = (Path(self.git_dir) / "commondir").read_text().splitlines()[0].strip()
372+
self._common_dir = osp.join(self.git_dir, common_dir)
373+
except OSError:
374+
self._common_dir = ""
371375

372376
# Adjust the working directory in case we are actually bare - we didn't know
373377
# that in the first place.

test/test_repo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,22 @@ def test_repo_discovery_rejects_invalid_metadata(self):
162162
with self.subTest(metadata=".git"):
163163
self.assertRaises(InvalidGitRepositoryError, Repo, path)
164164

165+
def test_repo_discovery_uses_git_common_dir(self):
166+
with tempfile.TemporaryDirectory() as tdir:
167+
git_dir = Path(tdir) / "git"
168+
common_dir = Path(tdir) / "common"
169+
git_dir.mkdir()
170+
common_dir.mkdir()
171+
(git_dir / "HEAD").write_text("ref: refs/heads/main\n")
172+
(common_dir / "objects").mkdir()
173+
(common_dir / "refs").mkdir()
174+
175+
with mock.patch.dict(os.environ, {"GIT_DIR": str(git_dir), "GIT_COMMON_DIR": str(common_dir)}):
176+
repo = Repo()
177+
178+
assert osp.samefile(repo.common_dir, common_dir)
179+
assert osp.samefile(repo.odb.root_path(), common_dir / "objects")
180+
165181
@with_rw_repo("0.3.2.1")
166182
def test_repo_creation_from_different_paths(self, rw_repo):
167183
r_from_gitdir = Repo(rw_repo.git_dir)

0 commit comments

Comments
 (0)