Skip to content

Commit 5b6ea0f

Browse files
codexByron
authored andcommitted
Address review feedback about custom object directories
Review feedback noted that GIT_OBJECT_DIRECTORY made discovery succeed without becoming the Repo ODB root, while a relative value could later be resolved from the Git wrapper's different working directory. Resolve the environment value against the construction directory, use it as the ODB root, and preserve the absolute value for later Git commands. The regression moves the only object store outside the git directory and verifies access through both GitDB and git cat-file after the original environment and current directory are restored. Validation: 6 focused tests and 12 subtests; Ruff check and format; mypy; compileall; git diff --check.
1 parent d1cce0f commit 5b6ea0f

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

git/repo/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ def __init__(
268268
"""
269269

270270
git_dir_env = os.getenv("GIT_DIR")
271+
object_dir_env = os.getenv("GIT_OBJECT_DIRECTORY")
272+
if object_dir_env is not None:
273+
object_dir_env = osp.abspath(object_dir_env)
271274
epath = path or git_dir_env
272275
if not epath:
273276
epath = os.getcwd()
@@ -386,9 +389,11 @@ def __init__(
386389
self.git.update_environment(GIT_DIR=os.fspath(self.git_dir), GIT_COMMON_DIR=os.fspath(self.common_dir))
387390
elif git_dir_env is not None:
388391
self.git.update_environment(GIT_DIR=os.fspath(self.git_dir))
392+
if object_dir_env is not None:
393+
self.git.update_environment(GIT_OBJECT_DIRECTORY=object_dir_env)
389394

390395
# Special handling, in special times.
391-
rootpath = osp.join(self.common_dir, "objects")
396+
rootpath = object_dir_env if object_dir_env is not None else osp.join(self.common_dir, "objects")
392397
if issubclass(odbt, GitCmdObjectDB):
393398
self.odb = odbt(rootpath, self.git)
394399
else:

test/test_repo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,26 @@ def test_repo_discovery_uses_storage_environment(self):
231231

232232
assert osp.samefile(Repo(git_dir).common_dir, byte_common_dir)
233233

234+
def test_repo_discovery_preserves_object_directory(self):
235+
with tempfile.TemporaryDirectory() as tdir:
236+
git_dir = Path(tdir) / "git"
237+
payload = b"custom object database"
238+
payload_file = Path(tdir) / "payload"
239+
payload_file.write_bytes(payload)
240+
241+
source_repo = Repo.init(git_dir, bare=True)
242+
blob_hexsha = source_repo.git.hash_object("-w", payload_file)
243+
source_repo.close()
244+
object_dir = Path(tdir) / "objects"
245+
(git_dir / "objects").rename(object_dir)
246+
247+
with cwd(tdir), mock.patch.dict(os.environ, {"GIT_DIR": "git", "GIT_OBJECT_DIRECTORY": "objects"}):
248+
repo = Repo(odbt=GitDB)
249+
250+
assert osp.samefile(repo.odb.root_path(), object_dir)
251+
assert repo.odb.stream(bytes.fromhex(blob_hexsha)).read() == payload
252+
assert repo.git.cat_file("blob", blob_hexsha) == payload.decode()
253+
234254
@with_rw_repo("0.3.2.1")
235255
def test_repo_creation_from_different_paths(self, rw_repo):
236256
r_from_gitdir = Repo(rw_repo.git_dir)

0 commit comments

Comments
 (0)