Skip to content

Commit ee117eb

Browse files
committed
Use the common Git directory for alternates
1 parent d40b356 commit ee117eb

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

git/repo/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,7 @@ def _get_alternates(self) -> List[str]:
961961
:return:
962962
List of strings being pathnames of alternates
963963
"""
964-
if self.git_dir:
965-
alternates_path = osp.join(self.git_dir, "objects", "info", "alternates")
964+
alternates_path = osp.join(self.common_dir, "objects", "info", "alternates")
966965

967966
if osp.exists(alternates_path):
968967
with open(alternates_path, "rb") as f:

test/test_repo.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,29 @@ def test_daemon_export(self):
327327

328328
def test_alternates(self):
329329
cur_alternates = self.rorepo.alternates
330-
# empty alternates
331-
self.rorepo.alternates = []
332-
self.assertEqual(self.rorepo.alternates, [])
330+
try:
331+
# Empty alternates.
332+
self.rorepo.alternates = []
333+
self.assertEqual(self.rorepo.alternates, [])
334+
alts = ["other/location", "this/location"]
335+
self.rorepo.alternates = alts
336+
self.assertEqual(alts, self.rorepo.alternates)
337+
finally:
338+
self.rorepo.alternates = cur_alternates
339+
340+
@with_rw_directory
341+
def test_alternates_use_common_dir(self, rw_dir):
342+
common_dir = osp.join(rw_dir, "common")
343+
git_dir = osp.join(rw_dir, "worktrees", "linked")
344+
os.makedirs(osp.join(common_dir, "objects", "info"))
345+
os.makedirs(osp.join(git_dir, "objects", "info"))
346+
repo = mock.Mock(common_dir=common_dir, git_dir=git_dir)
347+
333348
alts = ["other/location", "this/location"]
334-
self.rorepo.alternates = alts
335-
self.assertEqual(alts, self.rorepo.alternates)
336-
self.rorepo.alternates = cur_alternates
349+
Repo._set_alternates(repo, alts)
350+
351+
self.assertEqual(Repo._get_alternates(repo), alts)
352+
self.assertFalse(osp.exists(osp.join(git_dir, "objects", "info", "alternates")))
337353

338354
def test_repr(self):
339355
assert repr(self.rorepo).startswith("<git.repo.base.Repo ")

0 commit comments

Comments
 (0)