Skip to content

Commit 514a9de

Browse files
authored
Merge pull request #2151 from elovelan/relative_worktree_support
Support relative worktree paths (git 2.48+ worktree.useRelativePaths)
2 parents e196f44 + 29c9861 commit 514a9de

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

git/cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ def _call_process(
16121612
16131613
turns into::
16141614
1615-
git rev-list --max-count=10 --header=master
1615+
git rev-list --max-count=10 --header master
16161616
16171617
:return:
16181618
Same as :meth:`execute`. If no args are given, used :meth:`execute`'s

git/repo/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ def __init__(
295295
sm_gitpath = find_worktree_git_dir(dotgit)
296296

297297
if sm_gitpath is not None:
298-
git_dir = expand_path(sm_gitpath, expand_vars)
298+
# worktrees can use relative paths as of Git 2.48, so we join to curpath
299+
git_dir = osp.normpath(osp.join(curpath, sm_gitpath))
299300
self._working_tree_dir = curpath
300301
break
301302

test/test_repo.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ def test_is_valid_object(self):
10941094
self.assertFalse(repo.is_valid_object(tag_sha, "commit"))
10951095

10961096
@with_rw_directory
1097-
def test_git_work_tree_dotgit(self, rw_dir):
1097+
def test_git_work_tree_dotgit(self, rw_dir, use_relative_paths=False):
10981098
"""Check that we find .git as a worktree file and find the worktree
10991099
based on it."""
11001100
git = Git(rw_dir)
@@ -1106,7 +1106,11 @@ def test_git_work_tree_dotgit(self, rw_dir):
11061106
worktree_path = join_path_native(rw_dir, "worktree_repo")
11071107
if Git.is_cygwin():
11081108
worktree_path = cygpath(worktree_path)
1109-
rw_master.git.worktree("add", worktree_path, branch.name)
1109+
wt_add_kwargs = {"insert_kwargs_after": "add"}
1110+
# relative worktree paths introduced in git 2.48.0
1111+
if use_relative_paths and git.version_info[:3] >= (2, 48, 0):
1112+
wt_add_kwargs["relative_paths"] = True
1113+
rw_master.git.worktree("add", worktree_path, branch.name, **wt_add_kwargs)
11101114

11111115
# This ensures that we can read the repo's gitdir correctly.
11121116
repo = Repo(worktree_path)
@@ -1124,6 +1128,15 @@ def test_git_work_tree_dotgit(self, rw_dir):
11241128

11251129
self.assertIsInstance(repo.heads["aaaaaaaa"], Head)
11261130

1131+
def test_git_work_tree_dotgit_relative(self):
1132+
"""Check that we find .git as a worktree file containing a relative path
1133+
and find the worktree based on it."""
1134+
if Git().version_info[:3] < (2, 48, 0):
1135+
pytest.skip("relative worktree feature unsupported, needs git 2.48.0 or later")
1136+
# this class inherits from TestCase so we can't use pytest.mark.parametrize on
1137+
# test_git_work_tree_dotgit; delegate instead
1138+
self.test_git_work_tree_dotgit(use_relative_paths=True)
1139+
11271140
@with_rw_directory
11281141
def test_git_work_tree_env(self, rw_dir):
11291142
"""Check that we yield to GIT_WORK_TREE."""

0 commit comments

Comments
 (0)