Merge branch 'jk/test-helper-v-output-fix' into jch
[git] / t / t2025-worktree-add.sh
1 #!/bin/sh
2
3 test_description='test git worktree add'
4
5 . ./test-lib.sh
6
7 . "$TEST_DIRECTORY"/lib-rebase.sh
8
9 test_expect_success 'setup' '
10         test_commit init
11 '
12
13 test_expect_success '"add" an existing worktree' '
14         mkdir -p existing/subtree &&
15         test_must_fail git worktree add --detach existing master
16 '
17
18 test_expect_success '"add" an existing empty worktree' '
19         mkdir existing_empty &&
20         git worktree add --detach existing_empty master
21 '
22
23 test_expect_success '"add" using shorthand - fails when no previous branch' '
24         test_must_fail git worktree add existing_short -
25 '
26
27 test_expect_success '"add" using - shorthand' '
28         git checkout -b newbranch &&
29         echo hello >myworld &&
30         git add myworld &&
31         git commit -m myworld &&
32         git checkout master &&
33         git worktree add short-hand - &&
34         echo refs/heads/newbranch >expect &&
35         git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
36         test_cmp expect actual
37 '
38
39 test_expect_success '"add" refuses to checkout locked branch' '
40         test_must_fail git worktree add zere master &&
41         ! test -d zere &&
42         ! test -d .git/worktrees/zere
43 '
44
45 test_expect_success 'checking out paths not complaining about linked checkouts' '
46         (
47         cd existing_empty &&
48         echo dirty >>init.t &&
49         git checkout master -- init.t
50         )
51 '
52
53 test_expect_success '"add" worktree' '
54         git rev-parse HEAD >expect &&
55         git worktree add --detach here master &&
56         (
57                 cd here &&
58                 test_cmp ../init.t init.t &&
59                 test_must_fail git symbolic-ref HEAD &&
60                 git rev-parse HEAD >actual &&
61                 test_cmp ../expect actual &&
62                 git fsck
63         )
64 '
65
66 test_expect_success '"add" worktree with lock' '
67         git rev-parse HEAD >expect &&
68         git worktree add --detach --lock here-with-lock master &&
69         test -f .git/worktrees/here-with-lock/locked
70 '
71
72 test_expect_success '"add" worktree from a subdir' '
73         (
74                 mkdir sub &&
75                 cd sub &&
76                 git worktree add --detach here master &&
77                 cd here &&
78                 test_cmp ../../init.t init.t
79         )
80 '
81
82 test_expect_success '"add" from a linked checkout' '
83         (
84                 cd here &&
85                 git worktree add --detach nested-here master &&
86                 cd nested-here &&
87                 git fsck
88         )
89 '
90
91 test_expect_success '"add" worktree creating new branch' '
92         git worktree add -b newmaster there master &&
93         (
94                 cd there &&
95                 test_cmp ../init.t init.t &&
96                 git symbolic-ref HEAD >actual &&
97                 echo refs/heads/newmaster >expect &&
98                 test_cmp expect actual &&
99                 git fsck
100         )
101 '
102
103 test_expect_success 'die the same branch is already checked out' '
104         (
105                 cd here &&
106                 test_must_fail git checkout newmaster
107         )
108 '
109
110 test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
111         head=$(git -C there rev-parse --git-path HEAD) &&
112         ref=$(git -C there symbolic-ref HEAD) &&
113         rm "$head" &&
114         ln -s "$ref" "$head" &&
115         test_must_fail git -C here checkout newmaster
116 '
117
118 test_expect_success 'not die the same branch is already checked out' '
119         (
120                 cd here &&
121                 git worktree add --force anothernewmaster newmaster
122         )
123 '
124
125 test_expect_success 'not die on re-checking out current branch' '
126         (
127                 cd there &&
128                 git checkout newmaster
129         )
130 '
131
132 test_expect_success '"add" from a bare repo' '
133         (
134                 git clone --bare . bare &&
135                 cd bare &&
136                 git worktree add -b bare-master ../there2 master
137         )
138 '
139
140 test_expect_success 'checkout from a bare repo without "add"' '
141         (
142                 cd bare &&
143                 test_must_fail git checkout master
144         )
145 '
146
147 test_expect_success '"add" default branch of a bare repo' '
148         (
149                 git clone --bare . bare2 &&
150                 cd bare2 &&
151                 git worktree add ../there3 master
152         )
153 '
154
155 test_expect_success 'checkout with grafts' '
156         test_when_finished rm .git/info/grafts &&
157         test_commit abc &&
158         SHA1=$(git rev-parse HEAD) &&
159         test_commit def &&
160         test_commit xyz &&
161         echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
162         cat >expected <<-\EOF &&
163         xyz
164         abc
165         EOF
166         git log --format=%s -2 >actual &&
167         test_cmp expected actual &&
168         git worktree add --detach grafted master &&
169         git --git-dir=grafted/.git log --format=%s -2 >actual &&
170         test_cmp expected actual
171 '
172
173 test_expect_success '"add" from relative HEAD' '
174         test_commit a &&
175         test_commit b &&
176         test_commit c &&
177         git rev-parse HEAD~1 >expected &&
178         git worktree add relhead HEAD~1 &&
179         git -C relhead rev-parse HEAD >actual &&
180         test_cmp expected actual
181 '
182
183 test_expect_success '"add -b" with <branch> omitted' '
184         git worktree add -b burble flornk &&
185         test_cmp_rev HEAD burble
186 '
187
188 test_expect_success '"add --detach" with <branch> omitted' '
189         git worktree add --detach fishhook &&
190         git rev-parse HEAD >expected &&
191         git -C fishhook rev-parse HEAD >actual &&
192         test_cmp expected actual &&
193         test_must_fail git -C fishhook symbolic-ref HEAD
194 '
195
196 test_expect_success '"add" with <branch> omitted' '
197         git worktree add wiffle/bat &&
198         test_cmp_rev HEAD bat
199 '
200
201 test_expect_success '"add" auto-vivify checks out existing branch' '
202         test_commit c1 &&
203         test_commit c2 &&
204         git branch precious HEAD~1 &&
205         git worktree add precious &&
206         test_cmp_rev HEAD~1 precious &&
207         (
208                 cd precious &&
209                 test_cmp_rev precious HEAD
210         )
211 '
212
213 test_expect_success '"add" auto-vivify fails with checked out branch' '
214         git checkout -b test-branch &&
215         test_must_fail git worktree add test-branch &&
216         test_path_is_missing test-branch
217 '
218
219 test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
220         git worktree add --detach mish/mash &&
221         test_must_fail git rev-parse mash -- &&
222         test_must_fail git -C mish/mash symbolic-ref HEAD
223 '
224
225 test_expect_success '"add" -b/-B mutually exclusive' '
226         test_must_fail git worktree add -b poodle -B poodle bamboo master
227 '
228
229 test_expect_success '"add" -b/--detach mutually exclusive' '
230         test_must_fail git worktree add -b poodle --detach bamboo master
231 '
232
233 test_expect_success '"add" -B/--detach mutually exclusive' '
234         test_must_fail git worktree add -B poodle --detach bamboo master
235 '
236
237 test_expect_success '"add -B" fails if the branch is checked out' '
238         git rev-parse newmaster >before &&
239         test_must_fail git worktree add -B newmaster bamboo master &&
240         git rev-parse newmaster >after &&
241         test_cmp before after
242 '
243
244 test_expect_success 'add -B' '
245         git worktree add -B poodle bamboo2 master^ &&
246         git -C bamboo2 symbolic-ref HEAD >actual &&
247         echo refs/heads/poodle >expected &&
248         test_cmp expected actual &&
249         test_cmp_rev master^ poodle
250 '
251
252 test_expect_success 'local clone from linked checkout' '
253         git clone --local here here-clone &&
254         ( cd here-clone && git fsck )
255 '
256
257 test_expect_success 'local clone --shared from linked checkout' '
258         git -C bare worktree add --detach ../baretree &&
259         git clone --local --shared baretree bare-clone &&
260         grep /bare/ bare-clone/.git/objects/info/alternates
261 '
262
263 test_expect_success '"add" worktree with --no-checkout' '
264         git worktree add --no-checkout -b swamp swamp &&
265         ! test -e swamp/init.t &&
266         git -C swamp reset --hard &&
267         test_cmp init.t swamp/init.t
268 '
269
270 test_expect_success '"add" worktree with --checkout' '
271         git worktree add --checkout -b swmap2 swamp2 &&
272         test_cmp init.t swamp2/init.t
273 '
274
275 test_expect_success 'put a worktree under rebase' '
276         git worktree add under-rebase &&
277         (
278                 cd under-rebase &&
279                 set_fake_editor &&
280                 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
281                 git worktree list | grep "under-rebase.*detached HEAD"
282         )
283 '
284
285 test_expect_success 'add a worktree, checking out a rebased branch' '
286         test_must_fail git worktree add new-rebase under-rebase &&
287         ! test -d new-rebase
288 '
289
290 test_expect_success 'checking out a rebased branch from another worktree' '
291         git worktree add new-place &&
292         test_must_fail git -C new-place checkout under-rebase
293 '
294
295 test_expect_success 'not allow to delete a branch under rebase' '
296         (
297                 cd under-rebase &&
298                 test_must_fail git branch -D under-rebase
299         )
300 '
301
302 test_expect_success 'rename a branch under rebase not allowed' '
303         test_must_fail git branch -M under-rebase rebase-with-new-name
304 '
305
306 test_expect_success 'check out from current worktree branch ok' '
307         (
308                 cd under-rebase &&
309                 git checkout under-rebase &&
310                 git checkout - &&
311                 git rebase --abort
312         )
313 '
314
315 test_expect_success 'checkout a branch under bisect' '
316         git worktree add under-bisect &&
317         (
318                 cd under-bisect &&
319                 git bisect start &&
320                 git bisect bad &&
321                 git bisect good HEAD~2 &&
322                 git worktree list | grep "under-bisect.*detached HEAD" &&
323                 test_must_fail git worktree add new-bisect under-bisect &&
324                 ! test -d new-bisect
325         )
326 '
327
328 test_expect_success 'rename a branch under bisect not allowed' '
329         test_must_fail git branch -M under-bisect bisect-with-new-name
330 '
331 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
332 test_branch_upstream () {
333         printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
334         {
335                 git config "branch.$1.remote" &&
336                 git config "branch.$1.merge"
337         } >actual.upstream &&
338         test_cmp expect.upstream actual.upstream
339 }
340
341 test_expect_success '--track sets up tracking' '
342         test_when_finished rm -rf track &&
343         git worktree add --track -b track track master &&
344         test_branch_upstream track . master
345 '
346
347 # setup remote repository $1 and repository $2 with $1 set up as
348 # remote.  The remote has two branches, master and foo.
349 setup_remote_repo () {
350         git init $1 &&
351         (
352                 cd $1 &&
353                 test_commit $1_master &&
354                 git checkout -b foo &&
355                 test_commit upstream_foo
356         ) &&
357         git init $2 &&
358         (
359                 cd $2 &&
360                 test_commit $2_master &&
361                 git remote add $1 ../$1 &&
362                 git config remote.$1.fetch \
363                         "refs/heads/*:refs/remotes/$1/*" &&
364                 git fetch --all
365         )
366 }
367
368 test_expect_success '--no-track avoids setting up tracking' '
369         test_when_finished rm -rf repo_upstream repo_local foo &&
370         setup_remote_repo repo_upstream repo_local &&
371         (
372                 cd repo_local &&
373                 git worktree add --no-track -b foo ../foo repo_upstream/foo
374         ) &&
375         (
376                 cd foo &&
377                 test_must_fail git config "branch.foo.remote" &&
378                 test_must_fail git config "branch.foo.merge" &&
379                 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
380         )
381 '
382
383 test_expect_success '"add" <path> <non-existent-branch> fails' '
384         test_must_fail git worktree add foo non-existent
385 '
386
387 test_expect_success '"add" <path> <branch> dwims' '
388         test_when_finished rm -rf repo_upstream repo_dwim foo &&
389         setup_remote_repo repo_upstream repo_dwim &&
390         git init repo_dwim &&
391         (
392                 cd repo_dwim &&
393                 git worktree add ../foo foo
394         ) &&
395         (
396                 cd foo &&
397                 test_branch_upstream foo repo_upstream foo &&
398                 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
399         )
400 '
401
402 test_expect_success 'git worktree add does not match remote' '
403         test_when_finished rm -rf repo_a repo_b foo &&
404         setup_remote_repo repo_a repo_b &&
405         (
406                 cd repo_b &&
407                 git worktree add ../foo
408         ) &&
409         (
410                 cd foo &&
411                 test_must_fail git config "branch.foo.remote" &&
412                 test_must_fail git config "branch.foo.merge" &&
413                 ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
414         )
415 '
416
417 test_expect_success 'git worktree add --guess-remote sets up tracking' '
418         test_when_finished rm -rf repo_a repo_b foo &&
419         setup_remote_repo repo_a repo_b &&
420         (
421                 cd repo_b &&
422                 git worktree add --guess-remote ../foo
423         ) &&
424         (
425                 cd foo &&
426                 test_branch_upstream foo repo_a foo &&
427                 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
428         )
429 '
430
431 test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
432         test_when_finished rm -rf repo_a repo_b foo &&
433         setup_remote_repo repo_a repo_b &&
434         (
435                 cd repo_b &&
436                 git config worktree.guessRemote true &&
437                 git worktree add ../foo
438         ) &&
439         (
440                 cd foo &&
441                 test_branch_upstream foo repo_a foo &&
442                 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
443         )
444 '
445
446 test_expect_success 'git worktree --no-guess-remote option overrides config' '
447         test_when_finished rm -rf repo_a repo_b foo &&
448         setup_remote_repo repo_a repo_b &&
449         (
450                 cd repo_b &&
451                 git config worktree.guessRemote true &&
452                 git worktree add --no-guess-remote ../foo
453         ) &&
454         (
455                 cd foo &&
456                 test_must_fail git config "branch.foo.remote" &&
457                 test_must_fail git config "branch.foo.merge" &&
458                 ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
459         )
460 '
461
462 post_checkout_hook () {
463         gitdir=${1:-.git}
464         test_when_finished "rm -f $gitdir/hooks/post-checkout" &&
465         mkdir -p $gitdir/hooks &&
466         write_script $gitdir/hooks/post-checkout <<-\EOF
467         {
468                 echo $*
469                 git rev-parse --git-dir --show-toplevel
470         } >hook.actual
471         EOF
472 }
473
474 test_expect_success '"add" invokes post-checkout hook (branch)' '
475         post_checkout_hook &&
476         {
477                 echo $_z40 $(git rev-parse HEAD) 1 &&
478                 echo $(pwd)/.git/worktrees/gumby &&
479                 echo $(pwd)/gumby
480         } >hook.expect &&
481         git worktree add gumby &&
482         test_cmp hook.expect gumby/hook.actual
483 '
484
485 test_expect_success '"add" invokes post-checkout hook (detached)' '
486         post_checkout_hook &&
487         {
488                 echo $_z40 $(git rev-parse HEAD) 1 &&
489                 echo $(pwd)/.git/worktrees/grumpy &&
490                 echo $(pwd)/grumpy
491         } >hook.expect &&
492         git worktree add --detach grumpy &&
493         test_cmp hook.expect grumpy/hook.actual
494 '
495
496 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
497         post_checkout_hook &&
498         rm -f hook.actual &&
499         git worktree add --no-checkout gloopy &&
500         test_path_is_missing gloopy/hook.actual
501 '
502
503 test_expect_success '"add" in other worktree invokes post-checkout hook' '
504         post_checkout_hook &&
505         {
506                 echo $_z40 $(git rev-parse HEAD) 1 &&
507                 echo $(pwd)/.git/worktrees/guppy &&
508                 echo $(pwd)/guppy
509         } >hook.expect &&
510         git -C gloopy worktree add --detach ../guppy &&
511         test_cmp hook.expect guppy/hook.actual
512 '
513
514 test_expect_success '"add" in bare repo invokes post-checkout hook' '
515         rm -rf bare &&
516         git clone --bare . bare &&
517         {
518                 echo $_z40 $(git --git-dir=bare rev-parse HEAD) 1 &&
519                 echo $(pwd)/bare/worktrees/goozy &&
520                 echo $(pwd)/goozy
521         } >hook.expect &&
522         post_checkout_hook bare &&
523         git -C bare worktree add --detach ../goozy &&
524         test_cmp hook.expect goozy/hook.actual
525 '
526
527 test_done