Skip to content

Commit ca27c05

Browse files
calebdwttaylorr
authored andcommitted
worktree: add tests for worktrees with relative paths
This patch expands the test coverage by adding cases that specifically handle relative paths. These tests verify correct behavior in a variety of operations, including: adding, listing, pruning, moving, and repairing worktrees with relative paths configured. This also adds a test case for reinitializing a repository that has relative worktrees. Signed-off-by: Caleb White <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent aec7fa5 commit ca27c05

File tree

6 files changed

+126
-5
lines changed

6 files changed

+126
-5
lines changed

t/t0001-init.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,19 +435,28 @@ sep_git_dir_worktree () {
435435
test_when_finished "rm -rf mainwt linkwt seprepo" &&
436436
git init mainwt &&
437437
test_commit -C mainwt gumby &&
438+
git -C mainwt config worktree.useRelativePaths "$([ "$2" = "relative" ] && echo true || echo false)" &&
438439
git -C mainwt worktree add --detach ../linkwt &&
439440
git -C "$1" init --separate-git-dir ../seprepo &&
440441
git -C mainwt rev-parse --git-common-dir >expect &&
441442
git -C linkwt rev-parse --git-common-dir >actual &&
442443
test_cmp expect actual
443444
}
444445

445-
test_expect_success 're-init to move gitdir with linked worktrees' '
446-
sep_git_dir_worktree mainwt
446+
test_expect_success 're-init to move gitdir with linked worktrees (absolute)' '
447+
sep_git_dir_worktree mainwt absolute
447448
'
448449

449-
test_expect_success 're-init to move gitdir within linked worktree' '
450-
sep_git_dir_worktree linkwt
450+
test_expect_success 're-init to move gitdir within linked worktree (absolute)' '
451+
sep_git_dir_worktree linkwt absolute
452+
'
453+
454+
test_expect_success 're-init to move gitdir with linked worktrees (relative)' '
455+
sep_git_dir_worktree mainwt relative
456+
'
457+
458+
test_expect_success 're-init to move gitdir within linked worktree (relative)' '
459+
sep_git_dir_worktree linkwt relative
451460
'
452461

453462
test_expect_success MINGW '.git hidden' '

t/t2400-worktree-add.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,4 +1207,45 @@ test_expect_success '"add" with initialized submodule, with submodule.recurse se
12071207
git -C project-clone -c submodule.recurse worktree add ../project-5
12081208
'
12091209

1210+
test_expect_success 'can create worktrees with relative paths' '
1211+
test_when_finished "git worktree remove relative" &&
1212+
git config worktree.useRelativePaths false &&
1213+
git worktree add --relative-paths ./relative &&
1214+
cat relative/.git >actual &&
1215+
echo "gitdir: ../.git/worktrees/relative" >expect &&
1216+
test_cmp expect actual &&
1217+
cat .git/worktrees/relative/gitdir >actual &&
1218+
echo "../../../relative/.git" >expect &&
1219+
test_cmp expect actual
1220+
1221+
'
1222+
1223+
test_expect_success 'can create worktrees with absolute paths' '
1224+
git config worktree.useRelativePaths true &&
1225+
git worktree add ./relative &&
1226+
cat relative/.git >actual &&
1227+
echo "gitdir: ../.git/worktrees/relative" >expect &&
1228+
test_cmp expect actual &&
1229+
git worktree add --no-relative-paths ./absolute &&
1230+
cat absolute/.git >actual &&
1231+
echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
1232+
test_cmp expect actual
1233+
'
1234+
1235+
test_expect_success 'move repo without breaking relative internal links' '
1236+
test_when_finished rm -rf repo moved &&
1237+
git init repo &&
1238+
(
1239+
cd repo &&
1240+
git config worktree.useRelativePaths true &&
1241+
test_commit initial &&
1242+
git worktree add wt1 &&
1243+
cd .. &&
1244+
mv repo moved &&
1245+
cd moved/wt1 &&
1246+
git status >out 2>err &&
1247+
test_must_be_empty err
1248+
)
1249+
'
1250+
12101251
test_done

t/t2401-worktree-prune.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ test_expect_success 'prune duplicate (main/linked)' '
120120
! test -d .git/worktrees/wt
121121
'
122122

123-
test_expect_success 'not prune proper worktrees when run inside linked worktree' '
123+
test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '
124124
test_when_finished rm -rf repo wt_ext &&
125125
git init repo &&
126126
(
127127
cd repo &&
128+
git config worktree.useRelativePaths true &&
128129
echo content >file &&
129130
git add file &&
130131
git commit -m msg &&

t/t2402-worktree-list.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ test_expect_success 'broken main worktree still at the top' '
261261
'
262262

263263
test_expect_success 'linked worktrees are sorted' '
264+
test_when_finished "rm -rf sorted" &&
264265
mkdir sorted &&
265266
git init sorted/main &&
266267
(
@@ -280,6 +281,27 @@ test_expect_success 'linked worktrees are sorted' '
280281
test_cmp expected sorted/main/actual
281282
'
282283

284+
test_expect_success 'linked worktrees with relative paths are shown with absolute paths' '
285+
test_when_finished "rm -rf sorted" &&
286+
mkdir sorted &&
287+
git init sorted/main &&
288+
(
289+
cd sorted/main &&
290+
test_tick &&
291+
test_commit new &&
292+
git worktree add --relative-paths ../first &&
293+
git worktree add ../second &&
294+
git worktree list --porcelain >out &&
295+
grep ^worktree out >actual
296+
) &&
297+
cat >expected <<-EOF &&
298+
worktree $(pwd)/sorted/main
299+
worktree $(pwd)/sorted/first
300+
worktree $(pwd)/sorted/second
301+
EOF
302+
test_cmp expected sorted/main/actual
303+
'
304+
283305
test_expect_success 'worktree path when called in .git directory' '
284306
git worktree list >list1 &&
285307
git -C .git worktree list >list2 &&

t/t2403-worktree-move.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,26 @@ test_expect_success 'not remove a repo with initialized submodule' '
247247
)
248248
'
249249

250+
test_expect_success 'move worktree with absolute path to relative path' '
251+
git config worktree.useRelativePaths false &&
252+
git worktree add ./absolute &&
253+
git worktree move --relative-paths absolute relative &&
254+
cat relative/.git >actual &&
255+
echo "gitdir: ../.git/worktrees/absolute" >expect &&
256+
test_cmp expect actual &&
257+
git config worktree.useRelativePaths true &&
258+
git worktree move relative relative2 &&
259+
cat relative2/.git >actual &&
260+
echo "gitdir: ../.git/worktrees/absolute" >expect &&
261+
test_cmp expect actual
262+
'
263+
264+
test_expect_success 'move worktree with relative path to absolute path' '
265+
git config worktree.useRelativePaths true &&
266+
git worktree move --no-relative-paths relative2 absolute &&
267+
cat absolute/.git >actual &&
268+
echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
269+
test_cmp expect actual
270+
'
271+
250272
test_done

t/t2406-worktree-repair.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,30 @@ test_expect_success 'repair copied main and linked worktrees' '
216216
test_cmp dup/linked.expect dup/linked/.git
217217
'
218218

219+
test_expect_success 'repair absolute worktree to use relative paths' '
220+
test_when_finished "rm -rf main side sidemoved" &&
221+
test_create_repo main &&
222+
test_commit -C main init &&
223+
git -C main worktree add --detach ../side &&
224+
echo "../../../../sidemoved/.git" >expect-gitdir &&
225+
echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile &&
226+
mv side sidemoved &&
227+
git -C main worktree repair --relative-paths ../sidemoved &&
228+
test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
229+
test_cmp expect-gitfile sidemoved/.git
230+
'
231+
232+
test_expect_success 'repair relative worktree to use absolute paths' '
233+
test_when_finished "rm -rf main side sidemoved" &&
234+
test_create_repo main &&
235+
test_commit -C main init &&
236+
git -C main worktree add --relative-paths --detach ../side &&
237+
echo "$(pwd)/sidemoved/.git" >expect-gitdir &&
238+
echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile &&
239+
mv side sidemoved &&
240+
git -C main worktree repair ../sidemoved &&
241+
test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
242+
test_cmp expect-gitfile sidemoved/.git
243+
'
244+
219245
test_done

0 commit comments

Comments
 (0)