blob: d587e0b20db22423ef1a1d204076c50b769e0375 [file] [log] [blame]
Eric Sunshinef194b1e2015-07-06 13:30:54 -04001#!/bin/sh
2
3test_description='test git worktree add'
4
Johannes Schindelin883b98e2020-11-18 23:44:22 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Ævar Arnfjörð Bjarmason93e02b62022-06-03 13:15:05 +02008TEST_CREATE_REPO_NO_TEMPLATE=1
Eric Sunshinef194b1e2015-07-06 13:30:54 -04009. ./test-lib.sh
10
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +070011. "$TEST_DIRECTORY"/lib-rebase.sh
12
Eric Sunshinef194b1e2015-07-06 13:30:54 -040013test_expect_success 'setup' '
14 test_commit init
15'
16
17test_expect_success '"add" an existing worktree' '
18 mkdir -p existing/subtree &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000019 test_must_fail git worktree add --detach existing main
Eric Sunshinef194b1e2015-07-06 13:30:54 -040020'
21
22test_expect_success '"add" an existing empty worktree' '
23 mkdir existing_empty &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000024 git worktree add --detach existing_empty main
Eric Sunshinef194b1e2015-07-06 13:30:54 -040025'
26
Jordan DE GEA1a450e22016-05-27 15:17:08 +020027test_expect_success '"add" using shorthand - fails when no previous branch' '
28 test_must_fail git worktree add existing_short -
29'
30
31test_expect_success '"add" using - shorthand' '
32 git checkout -b newbranch &&
33 echo hello >myworld &&
34 git add myworld &&
35 git commit -m myworld &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000036 git checkout main &&
Jordan DE GEA1a450e22016-05-27 15:17:08 +020037 git worktree add short-hand - &&
38 echo refs/heads/newbranch >expect &&
39 git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
40 test_cmp expect actual
41'
42
Eric Sunshinef194b1e2015-07-06 13:30:54 -040043test_expect_success '"add" refuses to checkout locked branch' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +000044 test_must_fail git worktree add zere main &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -040045 ! test -d zere &&
46 ! test -d .git/worktrees/zere
47'
48
49test_expect_success 'checking out paths not complaining about linked checkouts' '
50 (
51 cd existing_empty &&
52 echo dirty >>init.t &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000053 git checkout main -- init.t
Eric Sunshinef194b1e2015-07-06 13:30:54 -040054 )
55'
56
57test_expect_success '"add" worktree' '
58 git rev-parse HEAD >expect &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000059 git worktree add --detach here main &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -040060 (
61 cd here &&
62 test_cmp ../init.t init.t &&
63 test_must_fail git symbolic-ref HEAD &&
64 git rev-parse HEAD >actual &&
65 test_cmp ../expect actual &&
66 git fsck
67 )
68'
69
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +070070test_expect_success '"add" worktree with lock' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +000071 git worktree add --detach --lock here-with-lock main &&
Stephen Manzf9365c02021-07-11 00:27:18 +000072 test_when_finished "git worktree unlock here-with-lock || :" &&
Nguyễn Thái Ngọc Duy507e6e92017-04-12 20:58:05 +070073 test -f .git/worktrees/here-with-lock/locked
74'
75
Stephen Manz0db49612021-07-15 02:32:30 +000076test_expect_success '"add" worktree with lock and reason' '
77 lock_reason="why not" &&
78 git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main &&
79 test_when_finished "git worktree unlock here-with-lock-reason || :" &&
80 test -f .git/worktrees/here-with-lock-reason/locked &&
81 echo "$lock_reason" >expect &&
82 test_cmp expect .git/worktrees/here-with-lock-reason/locked
83'
84
85test_expect_success '"add" worktree with reason but no lock' '
86 test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main &&
87 test_path_is_missing .git/worktrees/here-with-reason-only/locked
88'
89
Eric Sunshinef194b1e2015-07-06 13:30:54 -040090test_expect_success '"add" worktree from a subdir' '
91 (
92 mkdir sub &&
93 cd sub &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000094 git worktree add --detach here main &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -040095 cd here &&
96 test_cmp ../../init.t init.t
97 )
98'
99
100test_expect_success '"add" from a linked checkout' '
101 (
102 cd here &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000103 git worktree add --detach nested-here main &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400104 cd nested-here &&
105 git fsck
106 )
107'
108
109test_expect_success '"add" worktree creating new branch' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000110 git worktree add -b newmain there main &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400111 (
112 cd there &&
113 test_cmp ../init.t init.t &&
114 git symbolic-ref HEAD >actual &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000115 echo refs/heads/newmain >expect &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400116 test_cmp expect actual &&
117 git fsck
118 )
119'
120
121test_expect_success 'die the same branch is already checked out' '
122 (
123 cd here &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000124 test_must_fail git checkout newmain
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400125 )
126'
127
Eric Sunshine746bbdc2015-07-17 19:00:03 -0400128test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
129 head=$(git -C there rev-parse --git-path HEAD) &&
130 ref=$(git -C there symbolic-ref HEAD) &&
131 rm "$head" &&
132 ln -s "$ref" "$head" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000133 test_must_fail git -C here checkout newmain
Eric Sunshine746bbdc2015-07-17 19:00:03 -0400134'
135
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400136test_expect_success 'not die the same branch is already checked out' '
137 (
138 cd here &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000139 git worktree add --force anothernewmain newmain
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400140 )
141'
142
143test_expect_success 'not die on re-checking out current branch' '
144 (
145 cd there &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000146 git checkout newmain
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400147 )
148'
149
150test_expect_success '"add" from a bare repo' '
151 (
152 git clone --bare . bare &&
153 cd bare &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000154 git worktree add -b bare-main ../there2 main
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400155 )
156'
157
158test_expect_success 'checkout from a bare repo without "add"' '
159 (
160 cd bare &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000161 test_must_fail git checkout main
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400162 )
163'
164
Dennis Kaarsemaker171c6462016-10-12 18:41:07 +0200165test_expect_success '"add" default branch of a bare repo' '
166 (
167 git clone --bare . bare2 &&
168 cd bare2 &&
Derrick Stolee53255912022-02-07 21:33:02 +0000169 git worktree add ../there3 main &&
170 cd ../there3 &&
171 # Simple check that a Git command does not
172 # immediately fail with the current setup
173 git status
174 ) &&
175 cat >expect <<-EOF &&
176 init.t
177 EOF
178 ls there3 >actual &&
179 test_cmp expect actual
180'
181
182test_expect_success '"add" to bare repo with worktree config' '
183 (
184 git clone --bare . bare3 &&
185 cd bare3 &&
186 git config extensions.worktreeconfig true &&
187
188 # Add config values that are erroneous to have in
189 # a config.worktree file outside of the main
190 # working tree, to check that Git filters them out
191 # when copying config during "git worktree add".
192 git config --worktree core.bare true &&
193 git config --worktree core.worktree "$(pwd)" &&
194
195 # We want to check that bogus.key is copied
196 git config --worktree bogus.key value &&
197 git config --unset core.bare &&
198 git worktree add ../there4 main &&
199 cd ../there4 &&
200
201 # Simple check that a Git command does not
202 # immediately fail with the current setup
203 git status &&
204 git worktree add --detach ../there5 &&
205 cd ../there5 &&
206 git status
207 ) &&
208
209 # the worktree has the arbitrary value copied.
210 test_cmp_config -C there4 value bogus.key &&
211 test_cmp_config -C there5 value bogus.key &&
212
213 # however, core.bare and core.worktree were removed.
214 test_must_fail git -C there4 config core.bare &&
215 test_must_fail git -C there4 config core.worktree &&
216
217 cat >expect <<-EOF &&
218 init.t
219 EOF
220
221 ls there4 >actual &&
222 test_cmp expect actual &&
223 ls there5 >actual &&
224 test_cmp expect actual
Dennis Kaarsemaker171c6462016-10-12 18:41:07 +0200225'
226
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400227test_expect_success 'checkout with grafts' '
228 test_when_finished rm .git/info/grafts &&
229 test_commit abc &&
Elia Pinto697b90d2015-12-22 16:05:52 +0100230 SHA1=$(git rev-parse HEAD) &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400231 test_commit def &&
232 test_commit xyz &&
Ævar Arnfjörð Bjarmason93e02b62022-06-03 13:15:05 +0200233 mkdir .git/info &&
Elia Pinto697b90d2015-12-22 16:05:52 +0100234 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400235 cat >expected <<-\EOF &&
236 xyz
237 abc
238 EOF
239 git log --format=%s -2 >actual &&
240 test_cmp expected actual &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000241 git worktree add --detach grafted main &&
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400242 git --git-dir=grafted/.git log --format=%s -2 >actual &&
243 test_cmp expected actual
244'
245
246test_expect_success '"add" from relative HEAD' '
247 test_commit a &&
248 test_commit b &&
249 test_commit c &&
250 git rev-parse HEAD~1 >expected &&
251 git worktree add relhead HEAD~1 &&
252 git -C relhead rev-parse HEAD >actual &&
253 test_cmp expected actual
254'
255
Eric Sunshine0f4af3b2015-07-06 13:30:58 -0400256test_expect_success '"add -b" with <branch> omitted' '
257 git worktree add -b burble flornk &&
258 test_cmp_rev HEAD burble
259'
260
Eric Sunshine5c942572015-07-17 19:00:09 -0400261test_expect_success '"add --detach" with <branch> omitted' '
262 git worktree add --detach fishhook &&
263 git rev-parse HEAD >expected &&
264 git -C fishhook rev-parse HEAD >actual &&
265 test_cmp expected actual &&
266 test_must_fail git -C fishhook symbolic-ref HEAD
267'
268
Eric Sunshine1eb07d82015-07-06 13:30:59 -0400269test_expect_success '"add" with <branch> omitted' '
270 git worktree add wiffle/bat &&
271 test_cmp_rev HEAD bat
272'
273
Thomas Gummererf60a7b72018-04-24 22:56:35 +0100274test_expect_success '"add" checks out existing branch of dwimd name' '
275 git branch dwim HEAD~1 &&
276 git worktree add dwim &&
277 test_cmp_rev HEAD~1 dwim &&
278 (
279 cd dwim &&
280 test_cmp_rev HEAD dwim
281 )
282'
283
284test_expect_success '"add <path>" dwim fails with checked out branch' '
285 git checkout -b test-branch &&
286 test_must_fail git worktree add test-branch &&
287 test_path_is_missing test-branch
288'
289
290test_expect_success '"add --force" with existing dwimd name doesnt die' '
291 git checkout test-branch &&
292 git worktree add --force test-branch
Eric Sunshine1eb07d82015-07-06 13:30:59 -0400293'
294
Eric Sunshine5c942572015-07-17 19:00:09 -0400295test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
296 git worktree add --detach mish/mash &&
297 test_must_fail git rev-parse mash -- &&
298 test_must_fail git -C mish/mash symbolic-ref HEAD
299'
300
Eric Sunshineab0b2c52015-07-17 19:00:08 -0400301test_expect_success '"add" -b/-B mutually exclusive' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000302 test_must_fail git worktree add -b poodle -B poodle bamboo main
Eric Sunshineab0b2c52015-07-17 19:00:08 -0400303'
304
305test_expect_success '"add" -b/--detach mutually exclusive' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000306 test_must_fail git worktree add -b poodle --detach bamboo main
Eric Sunshineab0b2c52015-07-17 19:00:08 -0400307'
308
309test_expect_success '"add" -B/--detach mutually exclusive' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000310 test_must_fail git worktree add -B poodle --detach bamboo main
Eric Sunshineab0b2c52015-07-17 19:00:08 -0400311'
312
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700313test_expect_success '"add -B" fails if the branch is checked out' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000314 git rev-parse newmain >before &&
315 test_must_fail git worktree add -B newmain bamboo main &&
316 git rev-parse newmain >after &&
Nguyễn Thái Ngọc Duybeb6f242016-02-15 20:35:33 +0700317 test_cmp before after
318'
319
Nguyễn Thái Ngọc Duy0ebf4a22016-02-15 20:35:32 +0700320test_expect_success 'add -B' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000321 git worktree add -B poodle bamboo2 main^ &&
Nguyễn Thái Ngọc Duy0ebf4a22016-02-15 20:35:32 +0700322 git -C bamboo2 symbolic-ref HEAD >actual &&
323 echo refs/heads/poodle >expected &&
324 test_cmp expected actual &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000325 test_cmp_rev main^ poodle
Nguyễn Thái Ngọc Duy0ebf4a22016-02-15 20:35:32 +0700326'
327
Elia Pinto371979c2018-08-15 20:56:30 +0000328test_expect_success 'add --quiet' '
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000329 git worktree add --quiet another-worktree main 2>actual &&
Elia Pinto371979c2018-08-15 20:56:30 +0000330 test_must_be_empty actual
331'
332
Nguyễn Thái Ngọc Duy744e4692015-09-28 20:06:15 +0700333test_expect_success 'local clone from linked checkout' '
334 git clone --local here here-clone &&
335 ( cd here-clone && git fsck )
336'
337
Eric Sunshineb3b05972017-12-11 18:16:12 -0500338test_expect_success 'local clone --shared from linked checkout' '
339 git -C bare worktree add --detach ../baretree &&
340 git clone --local --shared baretree bare-clone &&
341 grep /bare/ bare-clone/.git/objects/info/alternates
342'
343
Ray Zhangef2a0ac2016-03-29 10:11:01 +0000344test_expect_success '"add" worktree with --no-checkout' '
345 git worktree add --no-checkout -b swamp swamp &&
346 ! test -e swamp/init.t &&
347 git -C swamp reset --hard &&
348 test_cmp init.t swamp/init.t
349'
350
351test_expect_success '"add" worktree with --checkout' '
352 git worktree add --checkout -b swmap2 swamp2 &&
353 test_cmp init.t swamp2/init.t
354'
355
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700356test_expect_success 'put a worktree under rebase' '
357 git worktree add under-rebase &&
358 (
359 cd under-rebase &&
360 set_fake_editor &&
361 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
362 git worktree list | grep "under-rebase.*detached HEAD"
363 )
364'
365
366test_expect_success 'add a worktree, checking out a rebased branch' '
367 test_must_fail git worktree add new-rebase under-rebase &&
368 ! test -d new-rebase
369'
370
371test_expect_success 'checking out a rebased branch from another worktree' '
372 git worktree add new-place &&
373 test_must_fail git -C new-place checkout under-rebase
374'
375
376test_expect_success 'not allow to delete a branch under rebase' '
377 (
378 cd under-rebase &&
379 test_must_fail git branch -D under-rebase
380 )
381'
382
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700383test_expect_success 'rename a branch under rebase not allowed' '
384 test_must_fail git branch -M under-rebase rebase-with-new-name
385'
386
Nguyễn Thái Ngọc Duy8d9fdd72016-04-22 20:01:33 +0700387test_expect_success 'check out from current worktree branch ok' '
388 (
389 cd under-rebase &&
390 git checkout under-rebase &&
391 git checkout - &&
392 git rebase --abort
393 )
394'
395
Nguyễn Thái Ngọc Duy04a3dfb2016-04-22 20:01:35 +0700396test_expect_success 'checkout a branch under bisect' '
397 git worktree add under-bisect &&
398 (
399 cd under-bisect &&
400 git bisect start &&
401 git bisect bad &&
402 git bisect good HEAD~2 &&
403 git worktree list | grep "under-bisect.*detached HEAD" &&
404 test_must_fail git worktree add new-bisect under-bisect &&
405 ! test -d new-bisect
406 )
407'
408
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700409test_expect_success 'rename a branch under bisect not allowed' '
410 test_must_fail git branch -M under-bisect bisect-with-new-name
411'
Thomas Gummerere284e892017-11-26 19:43:53 +0000412# Is branch "refs/heads/$1" set to pull from "$2/$3"?
413test_branch_upstream () {
414 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
415 {
416 git config "branch.$1.remote" &&
417 git config "branch.$1.merge"
418 } >actual.upstream &&
419 test_cmp expect.upstream actual.upstream
420}
421
422test_expect_success '--track sets up tracking' '
423 test_when_finished rm -rf track &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000424 git worktree add --track -b track track main &&
425 test_branch_upstream track . main
Thomas Gummerere284e892017-11-26 19:43:53 +0000426'
427
428# setup remote repository $1 and repository $2 with $1 set up as
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000429# remote. The remote has two branches, main and foo.
Thomas Gummerere284e892017-11-26 19:43:53 +0000430setup_remote_repo () {
431 git init $1 &&
432 (
433 cd $1 &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000434 test_commit $1_main &&
Thomas Gummerere284e892017-11-26 19:43:53 +0000435 git checkout -b foo &&
436 test_commit upstream_foo
437 ) &&
438 git init $2 &&
439 (
440 cd $2 &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000441 test_commit $2_main &&
Thomas Gummerere284e892017-11-26 19:43:53 +0000442 git remote add $1 ../$1 &&
443 git config remote.$1.fetch \
444 "refs/heads/*:refs/remotes/$1/*" &&
445 git fetch --all
446 )
447}
448
449test_expect_success '--no-track avoids setting up tracking' '
450 test_when_finished rm -rf repo_upstream repo_local foo &&
451 setup_remote_repo repo_upstream repo_local &&
452 (
453 cd repo_local &&
454 git worktree add --no-track -b foo ../foo repo_upstream/foo
455 ) &&
456 (
457 cd foo &&
458 test_must_fail git config "branch.foo.remote" &&
459 test_must_fail git config "branch.foo.merge" &&
460 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
461 )
462'
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700463
Thomas Gummerer4e853332017-11-26 19:43:54 +0000464test_expect_success '"add" <path> <non-existent-branch> fails' '
465 test_must_fail git worktree add foo non-existent
466'
467
468test_expect_success '"add" <path> <branch> dwims' '
469 test_when_finished rm -rf repo_upstream repo_dwim foo &&
470 setup_remote_repo repo_upstream repo_dwim &&
471 git init repo_dwim &&
472 (
473 cd repo_dwim &&
474 git worktree add ../foo foo
475 ) &&
476 (
477 cd foo &&
478 test_branch_upstream foo repo_upstream foo &&
479 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
480 )
481'
482
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +0000483test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
484 test_when_finished rm -rf repo_upstream repo_dwim foo &&
485 setup_remote_repo repo_upstream repo_dwim &&
486 git init repo_dwim &&
487 (
488 cd repo_dwim &&
489 git remote add repo_upstream2 ../repo_upstream &&
490 git fetch repo_upstream2 &&
491 test_must_fail git worktree add ../foo foo &&
492 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +0000493 git status -uno --porcelain >status.actual &&
Ævar Arnfjörð Bjarmason9f4bcf82018-07-27 17:48:11 +0000494 test_must_be_empty status.actual
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +0000495 ) &&
496 (
497 cd foo &&
498 test_branch_upstream foo repo_upstream foo &&
499 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
500 )
501'
502
Thomas Gummerer71d66822017-11-29 20:04:50 +0000503test_expect_success 'git worktree add does not match remote' '
504 test_when_finished rm -rf repo_a repo_b foo &&
505 setup_remote_repo repo_a repo_b &&
506 (
507 cd repo_b &&
508 git worktree add ../foo
509 ) &&
510 (
511 cd foo &&
512 test_must_fail git config "branch.foo.remote" &&
513 test_must_fail git config "branch.foo.merge" &&
Denton Liu2c9e1252019-11-12 15:07:45 -0800514 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
Thomas Gummerer71d66822017-11-29 20:04:50 +0000515 )
516'
517
518test_expect_success 'git worktree add --guess-remote sets up tracking' '
519 test_when_finished rm -rf repo_a repo_b foo &&
520 setup_remote_repo repo_a repo_b &&
521 (
522 cd repo_b &&
523 git worktree add --guess-remote ../foo
524 ) &&
525 (
526 cd foo &&
527 test_branch_upstream foo repo_a foo &&
528 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
529 )
530'
531
Thomas Gummerere92445a2017-11-29 20:04:51 +0000532test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
533 test_when_finished rm -rf repo_a repo_b foo &&
534 setup_remote_repo repo_a repo_b &&
535 (
536 cd repo_b &&
537 git config worktree.guessRemote true &&
538 git worktree add ../foo
539 ) &&
540 (
541 cd foo &&
542 test_branch_upstream foo repo_a foo &&
543 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
544 )
545'
546
547test_expect_success 'git worktree --no-guess-remote option overrides config' '
548 test_when_finished rm -rf repo_a repo_b foo &&
549 setup_remote_repo repo_a repo_b &&
550 (
551 cd repo_b &&
552 git config worktree.guessRemote true &&
553 git worktree add --no-guess-remote ../foo
554 ) &&
555 (
556 cd foo &&
557 test_must_fail git config "branch.foo.remote" &&
558 test_must_fail git config "branch.foo.merge" &&
Denton Liu2c9e1252019-11-12 15:07:45 -0800559 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
Thomas Gummerere92445a2017-11-29 20:04:51 +0000560 )
561'
562
Eric Sunshineade546b2017-12-07 16:20:17 -0500563post_checkout_hook () {
Ævar Arnfjörð Bjarmason93e02b62022-06-03 13:15:05 +0200564 test_when_finished "rm -rf .git/hooks" &&
565 mkdir .git/hooks &&
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100566 test_hook -C "$1" post-checkout <<-\EOF
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500567 {
568 echo $*
569 git rev-parse --git-dir --show-toplevel
570 } >hook.actual
Eric Sunshineade546b2017-12-07 16:20:17 -0500571 EOF
572}
573
574test_expect_success '"add" invokes post-checkout hook (branch)' '
575 post_checkout_hook &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500576 {
brian m. carlson8125a582018-05-13 02:24:13 +0000577 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500578 echo $(pwd)/.git/worktrees/gumby &&
579 echo $(pwd)/gumby
580 } >hook.expect &&
Eric Sunshineade546b2017-12-07 16:20:17 -0500581 git worktree add gumby &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500582 test_cmp hook.expect gumby/hook.actual
Eric Sunshineade546b2017-12-07 16:20:17 -0500583'
584
585test_expect_success '"add" invokes post-checkout hook (detached)' '
586 post_checkout_hook &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500587 {
brian m. carlson8125a582018-05-13 02:24:13 +0000588 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500589 echo $(pwd)/.git/worktrees/grumpy &&
590 echo $(pwd)/grumpy
591 } >hook.expect &&
Eric Sunshineade546b2017-12-07 16:20:17 -0500592 git worktree add --detach grumpy &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500593 test_cmp hook.expect grumpy/hook.actual
Eric Sunshineade546b2017-12-07 16:20:17 -0500594'
595
596test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
597 post_checkout_hook &&
598 rm -f hook.actual &&
599 git worktree add --no-checkout gloopy &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500600 test_path_is_missing gloopy/hook.actual
601'
602
603test_expect_success '"add" in other worktree invokes post-checkout hook' '
604 post_checkout_hook &&
605 {
brian m. carlson8125a582018-05-13 02:24:13 +0000606 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500607 echo $(pwd)/.git/worktrees/guppy &&
608 echo $(pwd)/guppy
609 } >hook.expect &&
610 git -C gloopy worktree add --detach ../guppy &&
611 test_cmp hook.expect guppy/hook.actual
612'
613
614test_expect_success '"add" in bare repo invokes post-checkout hook' '
615 rm -rf bare &&
616 git clone --bare . bare &&
617 {
brian m. carlson8125a582018-05-13 02:24:13 +0000618 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
Eric Sunshinea4bf1e32018-02-15 14:18:41 -0500619 echo $(pwd)/bare/worktrees/goozy &&
620 echo $(pwd)/goozy
621 } >hook.expect &&
622 post_checkout_hook bare &&
623 git -C bare worktree add --detach ../goozy &&
624 test_cmp hook.expect goozy/hook.actual
Eric Sunshineade546b2017-12-07 16:20:17 -0500625'
626
Eric Sunshinecb56f552018-08-28 17:20:22 -0400627test_expect_success '"add" an existing but missing worktree' '
628 git worktree add --detach pneu &&
629 test_must_fail git worktree add --detach pneu &&
630 rm -fr pneu &&
Eric Sunshinee19831c2018-08-28 17:20:23 -0400631 test_must_fail git worktree add --detach pneu &&
632 git worktree add --force --detach pneu
633'
634
635test_expect_success '"add" an existing locked but missing worktree' '
636 git worktree add --detach gnoo &&
637 git worktree lock gnoo &&
638 test_when_finished "git worktree unlock gnoo || :" &&
639 rm -fr gnoo &&
640 test_must_fail git worktree add --detach gnoo &&
641 test_must_fail git worktree add --force --detach gnoo &&
642 git worktree add --force --force --detach gnoo
Eric Sunshinecb56f552018-08-28 17:20:22 -0400643'
644
Eric Sunshinebb69b3b2020-02-24 04:08:48 -0500645test_expect_success '"add" not tripped up by magic worktree matching"' '
646 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
647 # directory `sub2` should not mistakenly complain that `bar` is an
648 # already-registered worktree
649 mkdir sub1 sub2 &&
650 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
651 git -C sub2 --git-dir=../.git worktree add --detach bozo
652'
653
Nguyễn Thái Ngọc Duy1de16ae2019-03-08 16:28:34 +0700654test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
655 git worktree add --detach ". weird*..?.lock.lock" &&
656 test -d .git/worktrees/---weird-.-
657'
658
Nguyễn Thái Ngọc Duy105df732019-05-13 17:49:44 +0700659test_expect_success '"add" should not fail because of another bad worktree' '
660 git init add-fail &&
661 (
662 cd add-fail &&
663 test_commit first &&
664 mkdir sub &&
665 git worktree add sub/to-be-deleted &&
666 rm -rf sub &&
667 git worktree add second
668 )
669'
670
Philippe Blain4782cf22019-10-27 17:16:25 +0000671test_expect_success '"add" with uninitialized submodule, with submodule.recurse unset' '
Taylor Blau99f4abb2022-07-29 15:20:03 -0400672 test_config_global protocol.file.allow always &&
Philippe Blain4782cf22019-10-27 17:16:25 +0000673 test_create_repo submodule &&
674 test_commit -C submodule first &&
675 test_create_repo project &&
676 git -C project submodule add ../submodule &&
677 git -C project add submodule &&
678 test_tick &&
679 git -C project commit -m add_sub &&
680 git clone project project-clone &&
681 git -C project-clone worktree add ../project-2
682'
683test_expect_success '"add" with uninitialized submodule, with submodule.recurse set' '
684 git -C project-clone -c submodule.recurse worktree add ../project-3
685'
686
687test_expect_success '"add" with initialized submodule, with submodule.recurse unset' '
Taylor Blau99f4abb2022-07-29 15:20:03 -0400688 test_config_global protocol.file.allow always &&
Philippe Blain4782cf22019-10-27 17:16:25 +0000689 git -C project-clone submodule update --init &&
690 git -C project-clone worktree add ../project-4
691'
692
693test_expect_success '"add" with initialized submodule, with submodule.recurse set' '
694 git -C project-clone -c submodule.recurse worktree add ../project-5
695'
696
Eric Sunshinef194b1e2015-07-06 13:30:54 -0400697test_done