Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='git checkout tests.
7
8 Creates main, forks renamer and side branches from it.
9 Test switching across them.
10
11 ! [main] Initial A one, A two
12 * [renamer] Renamer R one->uno, M two
13 ! [side] Side M one, D two, A three
14 ! [simple] Simple D one, M two
15 ----
16 + [simple] Simple D one, M two
17 + [side] Side M one, D two, A three
18 * [renamer] Renamer R one->uno, M two
19 +*++ [main] Initial A one, A two
20
21 '
22
23 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
24 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
25
26 . ./test-lib.sh
27
28 test_tick
29
30 fill () {
31 for i
32 do
33 echo "$i"
34 done
35 }
36
37
38 test_expect_success setup '
39 fill x y z >same &&
40 fill 1 2 3 4 5 6 7 8 >one &&
41 fill a b c d e >two &&
42 git add same one two &&
43 git commit -m "Initial A one, A two" &&
44
45 git checkout -b renamer &&
46 rm -f one &&
47 fill 1 3 4 5 6 7 8 >uno &&
48 git add uno &&
49 fill a b c d e f >two &&
50 git commit -a -m "Renamer R one->uno, M two" &&
51
52 git checkout -b side main &&
53 fill 1 2 3 4 5 6 7 >one &&
54 fill A B C D E >three &&
55 rm -f two &&
56 git update-index --add --remove one two three &&
57 git commit -m "Side M one, D two, A three" &&
58
59 git checkout -b simple main &&
60 rm -f one &&
61 fill a c e >two &&
62 git commit -a -m "Simple D one, M two" &&
63
64 git checkout main
65 '
66
67 test_expect_success 'checkout from non-existing branch' '
68 git checkout -b delete-me main &&
69 git update-ref -d --no-deref refs/heads/delete-me &&
70 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
71 git checkout main &&
72 test refs/heads/main = "$(git symbolic-ref HEAD)"
73 '
74
75 test_expect_success 'checkout with dirty tree without -m' '
76 fill 0 1 2 3 4 5 6 7 8 >one &&
77 if git checkout side
78 then
79 echo Not happy
80 false
81 else
82 echo "happy - failed correctly"
83 fi
84 '
85
86 test_expect_success 'checkout with unrelated dirty tree without -m' '
87 git checkout -f main &&
88 fill 0 1 2 3 4 5 6 7 8 >same &&
89 cp same kept &&
90 git checkout side >messages &&
91 test_cmp same kept &&
92 printf "M\t%s\n" same >messages.expect &&
93 test_cmp messages.expect messages
94 '
95
96 test_expect_success 'checkout -m with dirty tree' '
97 git checkout -f main &&
98 git clean -f &&
99
100 fill 0 1 2 3 4 5 6 7 8 >one &&
101 git checkout -m side >messages &&
102
103 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
104
105 cat >expect.messages <<-\EOF &&
106 The following paths have local changes:
107 M one
108 EOF
109 test_cmp expect.messages messages &&
110
111 fill "M one" "A three" "D two" >expect.main &&
112 git diff --name-status main >current.main &&
113 test_cmp expect.main current.main &&
114
115 fill "M one" >expect.side &&
116 git diff --name-status side >current.side &&
117 test_cmp expect.side current.side &&
118
119 git diff --cached >current.index &&
120 test_must_be_empty current.index
121 '
122
123 test_expect_success 'checkout -m with dirty tree, renamed' '
124 git checkout -f main && git clean -f &&
125
126 fill 1 2 3 4 5 7 8 >one &&
127 if git checkout renamer
128 then
129 echo Not happy
130 false
131 else
132 echo "happy - failed correctly"
133 fi &&
134
135 git checkout -m renamer &&
136 fill 1 3 4 5 7 8 >expect &&
137 test_cmp expect uno &&
138 ! test -f one &&
139 git diff --cached >current &&
140 test_must_be_empty current
141 '
142
143 test_expect_success 'checkout -m with merge conflict' '
144 git checkout -f main && git clean -f &&
145
146 fill 1 T 3 4 5 6 S 8 >one &&
147 if git checkout renamer
148 then
149 echo Not happy
150 false
151 else
152 echo "happy - failed correctly"
153 fi &&
154
155 git checkout -m renamer &&
156
157 git diff main:one :3:uno |
158 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
159 fill d2 aT d7 aS >expect &&
160 test_cmp expect current &&
161 git diff --cached two >current &&
162 test_must_be_empty current
163 '
164
165 test_expect_success 'format of merge conflict from checkout -m' '
166 git checkout -f main &&
167 git clean -f &&
168
169 fill b d >two &&
170 git checkout -m simple &&
171
172 git ls-files >current &&
173 fill same two two two >expect &&
174 test_cmp expect current &&
175
176 cat <<-EOF >expect &&
177 <<<<<<< simple
178 a
179 c
180 e
181 =======
182 b
183 d
184 >>>>>>> local
185 EOF
186 test_cmp expect two
187 '
188
189 test_expect_success 'checkout --merge --conflict=diff3 <branch>' '
190 git checkout -f main &&
191 git reset --hard &&
192 git clean -f &&
193
194 fill b d >two &&
195 git checkout --merge --conflict=diff3 simple &&
196
197 cat <<-EOF >expect &&
198 <<<<<<< simple
199 a
200 c
201 e
202 ||||||| main
203 a
204 b
205 c
206 d
207 e
208 =======
209 b
210 d
211 >>>>>>> local
212 EOF
213 test_cmp expect two
214 '
215
216 test_expect_success 'checkout -m with mixed staged and unstaged changes' '
217 git checkout -f main &&
218 git clean -f &&
219
220 fill 0 x y z >same &&
221 git add same &&
222 fill 1 2 3 4 5 6 7 >one &&
223 git checkout -m side >actual 2>&1 &&
224 test_grep "Applied autostash" actual &&
225 fill 0 x y z >expect &&
226 test_cmp expect same &&
227 fill 1 2 3 4 5 6 7 >expect &&
228 test_cmp expect one
229 '
230
231 test_expect_success 'checkout -m creates a recoverable stash on conflict' '
232 git checkout -f main &&
233 git clean -f &&
234
235 fill 1 2 3 4 5 >one &&
236 test_must_fail git checkout side 2>stderr &&
237 test_grep "Your local changes" stderr &&
238 git checkout -m side >actual 2>&1 &&
239 test_grep "resulted in conflicts" actual &&
240 test_grep "git stash drop" actual &&
241 test_grep "git stash pop" actual &&
242 test_grep "The following paths have local changes" actual &&
243 sed -n "/apply the local changes later/,/Switched to branch/p" \
244 actual >separator.actual &&
245 cat >separator.expect <<-EOF &&
246 apply the local changes later by running "git stash pop".
247
248 Switched to branch ${SQ}side${SQ}
249 EOF
250 test_cmp separator.expect separator.actual &&
251 git log -p -1 --format="%gs%n%B" -g --diff-merges=1 refs/stash >actual &&
252 sed /^index/d actual >actual.trimmed &&
253 cat >expect <<-EOF &&
254 autostash while switching to ${SQ}side${SQ}
255 On main: autostash while switching to ${SQ}side${SQ}
256
257 diff --git a/one b/one
258 --- a/one
259 +++ b/one
260 @@ -3,6 +3,3 @@
261 3
262 4
263 5
264 -6
265 -7
266 -8
267 EOF
268 test_cmp expect actual.trimmed &&
269 git stash drop &&
270 git reset --hard
271 '
272
273 test_expect_success 'checkout -m only retries untracked-file failure with local changes' '
274 git checkout -f --detach main &&
275 test_commit another-file &&
276 git checkout HEAD^ &&
277 >another-file.t &&
278 test_must_fail env GIT_TRACE2_EVENT="$(pwd)/trace" \
279 git checkout -m @{-1} 2>err &&
280 test_grep "untracked working tree files" err &&
281 grep "\"region_enter\".*\"category\":\"index\",\"label\":\"refresh\"" \
282 trace >refresh.events &&
283 test_line_count = 1 refresh.events &&
284
285 fill 1 2 3 4 5 >one &&
286 test_must_fail git checkout -m @{-1} 2>err &&
287 q_to_tab >expect <<-\EOF &&
288 error: The following untracked working tree files would be overwritten by checkout:
289 Qanother-file.t
290 Please move or remove them before you switch branches.
291 Aborting
292 Applied autostash.
293 EOF
294 test_cmp expect err
295 '
296
297 test_expect_success 'switch to another branch while carrying a deletion' '
298 git checkout -f main &&
299 git reset --hard &&
300 git clean -f &&
301 git rm two &&
302
303 test_must_fail git checkout simple 2>errs &&
304 test_grep overwritten errs &&
305
306 test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs &&
307 test_must_be_empty errs
308 '
309
310 test_expect_success 'checkout to detach HEAD (with advice declined)' '
311 git config set advice.detachedHead false &&
312 rev=$(git rev-parse --short renamer^) &&
313 git checkout -f renamer &&
314 git clean -f &&
315 git checkout renamer^ 2>messages &&
316 test_grep "HEAD is now at $rev" messages &&
317 test_line_count = 1 messages &&
318 H=$(git rev-parse --verify HEAD) &&
319 M=$(git show-ref -s --verify refs/heads/main) &&
320 test "z$H" = "z$M" &&
321 if git symbolic-ref HEAD >/dev/null 2>&1
322 then
323 echo "OOPS, HEAD is still symbolic???"
324 false
325 else
326 : happy
327 fi
328 '
329
330 test_expect_success 'checkout to detach HEAD' '
331 git config set advice.detachedHead true &&
332 rev=$(git rev-parse --short renamer^) &&
333 git checkout -f renamer &&
334 git clean -f &&
335 git checkout renamer^ 2>messages &&
336 test_grep "HEAD is now at $rev" messages &&
337 test_line_count -gt 1 messages &&
338 H=$(git rev-parse --verify HEAD) &&
339 M=$(git show-ref -s --verify refs/heads/main) &&
340 test "z$H" = "z$M" &&
341 if git symbolic-ref HEAD >/dev/null 2>&1
342 then
343 echo "OOPS, HEAD is still symbolic???"
344 false
345 else
346 : happy
347 fi
348 '
349
350 test_expect_success 'checkout to detach HEAD with branchname^' '
351 git checkout -f main &&
352 git clean -f &&
353 git checkout renamer^ &&
354 H=$(git rev-parse --verify HEAD) &&
355 M=$(git show-ref -s --verify refs/heads/main) &&
356 test "z$H" = "z$M" &&
357 if git symbolic-ref HEAD >/dev/null 2>&1
358 then
359 echo "OOPS, HEAD is still symbolic???"
360 false
361 else
362 : happy
363 fi
364 '
365
366 test_expect_success 'checkout to detach HEAD with :/message' '
367 git checkout -f main &&
368 git clean -f &&
369 git checkout ":/Initial" &&
370 H=$(git rev-parse --verify HEAD) &&
371 M=$(git show-ref -s --verify refs/heads/main) &&
372 test "z$H" = "z$M" &&
373 if git symbolic-ref HEAD >/dev/null 2>&1
374 then
375 echo "OOPS, HEAD is still symbolic???"
376 false
377 else
378 : happy
379 fi
380 '
381
382 test_expect_success 'checkout to detach HEAD with HEAD^0' '
383 git checkout -f main &&
384 git clean -f &&
385 git checkout HEAD^0 &&
386 H=$(git rev-parse --verify HEAD) &&
387 M=$(git show-ref -s --verify refs/heads/main) &&
388 test "z$H" = "z$M" &&
389 if git symbolic-ref HEAD >/dev/null 2>&1
390 then
391 echo "OOPS, HEAD is still symbolic???"
392 false
393 else
394 : happy
395 fi
396 '
397
398 test_expect_success 'checkout with ambiguous tag/branch names' '
399 git tag both side &&
400 git branch both main &&
401 git reset --hard &&
402 git checkout main &&
403
404 git checkout both &&
405 H=$(git rev-parse --verify HEAD) &&
406 M=$(git show-ref -s --verify refs/heads/main) &&
407 test "z$H" = "z$M" &&
408 name=$(git symbolic-ref HEAD 2>/dev/null) &&
409 test "z$name" = zrefs/heads/both
410 '
411
412 test_expect_success 'checkout with ambiguous tag/branch names' '
413 git reset --hard &&
414 git checkout main &&
415
416 git tag frotz side &&
417 git branch frotz main &&
418 git reset --hard &&
419 git checkout main &&
420
421 git checkout tags/frotz &&
422 H=$(git rev-parse --verify HEAD) &&
423 S=$(git show-ref -s --verify refs/heads/side) &&
424 test "z$H" = "z$S" &&
425 if name=$(git symbolic-ref HEAD 2>/dev/null)
426 then
427 echo "Bad -- should have detached"
428 false
429 else
430 : happy
431 fi
432 '
433
434 test_expect_success 'switch branches while in subdirectory' '
435 git reset --hard &&
436 git checkout main &&
437
438 mkdir subs &&
439 git -C subs checkout side &&
440 ! test -f subs/one &&
441 rm -fr subs
442 '
443
444 test_expect_success 'checkout specific path while in subdirectory' '
445 git reset --hard &&
446 git checkout side &&
447 mkdir subs &&
448 >subs/bero &&
449 git add subs/bero &&
450 git commit -m "add subs/bero" &&
451
452 git checkout main &&
453 mkdir -p subs &&
454 git -C subs checkout side -- bero &&
455 test -f subs/bero
456 '
457
458 test_expect_success 'checkout w/--track sets up tracking' '
459 git config branch.autosetupmerge false &&
460 git checkout main &&
461 git checkout --track -b track1 &&
462 test "$(git config branch.track1.remote)" &&
463 test "$(git config branch.track1.merge)"
464 '
465
466 test_expect_success 'checkout w/autosetupmerge=always sets up tracking' '
467 test_when_finished git config branch.autosetupmerge false &&
468 git config branch.autosetupmerge always &&
469 git checkout main &&
470 git checkout -b track2 &&
471 test "$(git config branch.track2.remote)" &&
472 test "$(git config branch.track2.merge)"
473 '
474
475 test_expect_success 'checkout w/--track from non-branch HEAD fails' '
476 git checkout main^0 &&
477 test_must_fail git symbolic-ref HEAD &&
478 test_must_fail git checkout --track -b track &&
479 test_must_fail git rev-parse --verify track &&
480 test_must_fail git symbolic-ref HEAD &&
481 test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)"
482 '
483
484 test_expect_success 'checkout w/--track from tag fails' '
485 git checkout main^0 &&
486 test_must_fail git symbolic-ref HEAD &&
487 test_must_fail git checkout --track -b track frotz &&
488 test_must_fail git rev-parse --verify track &&
489 test_must_fail git symbolic-ref HEAD &&
490 test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)"
491 '
492
493 test_expect_success 'detach a symbolic link HEAD' '
494 git checkout main &&
495 git config --bool core.prefersymlinkrefs yes &&
496 git checkout side &&
497 git checkout main &&
498 it=$(git symbolic-ref HEAD) &&
499 test "z$it" = zrefs/heads/main &&
500 here=$(git rev-parse --verify refs/heads/main) &&
501 git checkout side^ &&
502 test "z$(git rev-parse --verify refs/heads/main)" = "z$here"
503 '
504
505 test_expect_success 'checkout with --track fakes a sensible -b <name>' '
506 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
507 git update-ref refs/remotes/origin/koala/bear renamer &&
508
509 git checkout --track origin/koala/bear &&
510 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
511 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
512
513 git checkout main && git branch -D koala/bear &&
514
515 git checkout --track refs/remotes/origin/koala/bear &&
516 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
517 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
518
519 git checkout main && git branch -D koala/bear &&
520
521 git checkout --track remotes/origin/koala/bear &&
522 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
523 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
524 '
525
526 test_expect_success 'checkout with --track, but without -b, fails with too short tracked name' '
527 test_must_fail git checkout --track renamer
528 '
529
530 setup_conflicting_index () {
531 rm -f .git/index &&
532 O=$(echo original | git hash-object -w --stdin) &&
533 A=$(echo ourside | git hash-object -w --stdin) &&
534 B=$(echo theirside | git hash-object -w --stdin) &&
535 (
536 echo "100644 $A 0 fild" &&
537 echo "100644 $O 1 file" &&
538 echo "100644 $A 2 file" &&
539 echo "100644 $B 3 file" &&
540 echo "100644 $A 0 filf"
541 ) | git update-index --index-info
542 }
543
544 test_expect_success 'checkout an unmerged path should fail' '
545 setup_conflicting_index &&
546 echo "none of the above" >sample &&
547 cat sample >fild &&
548 cat sample >file &&
549 cat sample >filf &&
550 test_must_fail git checkout fild file filf &&
551 test_cmp sample fild &&
552 test_cmp sample filf &&
553 test_cmp sample file
554 '
555
556 test_expect_success 'checkout with an unmerged path can be ignored' '
557 setup_conflicting_index &&
558 echo "none of the above" >sample &&
559 echo ourside >expect &&
560 cat sample >fild &&
561 cat sample >file &&
562 cat sample >filf &&
563 git checkout -f fild file filf &&
564 test_cmp expect fild &&
565 test_cmp expect filf &&
566 test_cmp sample file
567 '
568
569 test_expect_success 'checkout unmerged stage' '
570 setup_conflicting_index &&
571 echo "none of the above" >sample &&
572 echo ourside >expect &&
573 cat sample >fild &&
574 cat sample >file &&
575 cat sample >filf &&
576 git checkout --ours . &&
577 test_cmp expect fild &&
578 test_cmp expect filf &&
579 test_cmp expect file &&
580 git checkout --theirs file &&
581 test ztheirside = "z$(cat file)"
582 '
583
584 test_expect_success 'checkout --ours is incompatible with switching' '
585 test_must_fail git checkout --ours 2>error &&
586 test_grep "needs the paths to check out" error &&
587
588 test_must_fail git checkout --ours HEAD 2>error &&
589 test_grep "cannot be used with switching" error &&
590
591 test_must_fail git checkout --ours main 2>error &&
592 test_grep "cannot be used with switching" error &&
593
594 git checkout --ours file
595 '
596
597 test_expect_success 'checkout path with --merge from tree-ish is a no-no' '
598 setup_conflicting_index &&
599 test_must_fail git checkout -m HEAD -- file
600 '
601
602 test_expect_success 'checkout with --merge' '
603 setup_conflicting_index &&
604 echo "none of the above" >sample &&
605 echo ourside >expect &&
606 cat sample >fild &&
607 cat sample >file &&
608 cat sample >filf &&
609 git checkout -m -- fild file filf &&
610 (
611 echo "<<<<<<< ours" &&
612 echo ourside &&
613 echo "=======" &&
614 echo theirside &&
615 echo ">>>>>>> theirs"
616 ) >merged &&
617 test_cmp expect fild &&
618 test_cmp expect filf &&
619 test_cmp merged file
620 '
621
622 test_expect_success 'checkout -m works after (mistaken) resolution' '
623 setup_conflicting_index &&
624 echo "none of the above" >sample &&
625 cat sample >fild &&
626 cat sample >file &&
627 cat sample >filf &&
628 # resolve to something
629 git add file &&
630 git checkout --merge -- fild file filf &&
631 {
632 echo "<<<<<<< ours" &&
633 echo ourside &&
634 echo "=======" &&
635 echo theirside &&
636 echo ">>>>>>> theirs"
637 } >merged &&
638 test_cmp expect fild &&
639 test_cmp expect filf &&
640 test_cmp merged file
641 '
642
643 test_expect_success 'checkout -m works after (mistaken) resolution to remove' '
644 setup_conflicting_index &&
645 echo "none of the above" >sample &&
646 cat sample >fild &&
647 cat sample >file &&
648 cat sample >filf &&
649 # resolve to remove
650 git rm file &&
651 git checkout --merge -- fild file filf &&
652 {
653 echo "<<<<<<< ours" &&
654 echo ourside &&
655 echo "=======" &&
656 echo theirside &&
657 echo ">>>>>>> theirs"
658 } >merged &&
659 test_cmp expect fild &&
660 test_cmp expect filf &&
661 test_cmp merged file
662 '
663
664 test_expect_success 'checkout with --merge, in diff3 -m style' '
665 git config merge.conflictstyle diff3 &&
666 setup_conflicting_index &&
667 echo "none of the above" >sample &&
668 echo ourside >expect &&
669 cat sample >fild &&
670 cat sample >file &&
671 cat sample >filf &&
672 git checkout -m -- fild file filf &&
673 (
674 echo "<<<<<<< ours" &&
675 echo ourside &&
676 echo "||||||| base" &&
677 echo original &&
678 echo "=======" &&
679 echo theirside &&
680 echo ">>>>>>> theirs"
681 ) >merged &&
682 test_cmp expect fild &&
683 test_cmp expect filf &&
684 test_cmp merged file
685 '
686
687 test_expect_success 'checkout --conflict=merge, overriding config' '
688 git config merge.conflictstyle diff3 &&
689 setup_conflicting_index &&
690 echo "none of the above" >sample &&
691 echo ourside >expect &&
692 cat sample >fild &&
693 cat sample >file &&
694 cat sample >filf &&
695 git checkout --conflict=merge -- fild file filf &&
696 (
697 echo "<<<<<<< ours" &&
698 echo ourside &&
699 echo "=======" &&
700 echo theirside &&
701 echo ">>>>>>> theirs"
702 ) >merged &&
703 test_cmp expect fild &&
704 test_cmp expect filf &&
705 test_cmp merged file
706 '
707
708 test_expect_success 'checkout --conflict=diff3' '
709 test_unconfig merge.conflictstyle &&
710 setup_conflicting_index &&
711 echo "none of the above" >sample &&
712 echo ourside >expect &&
713 cat sample >fild &&
714 cat sample >file &&
715 cat sample >filf &&
716 git checkout --conflict=diff3 -- fild file filf &&
717 (
718 echo "<<<<<<< ours" &&
719 echo ourside &&
720 echo "||||||| base" &&
721 echo original &&
722 echo "=======" &&
723 echo theirside &&
724 echo ">>>>>>> theirs"
725 ) >merged &&
726 test_cmp expect fild &&
727 test_cmp expect filf &&
728 test_cmp merged file
729 '
730
731 test_expect_success 'checkout --conflict=diff3 --no-conflict does not merge' '
732 setup_conflicting_index &&
733 echo "none of the above" >expect &&
734 cat expect >fild &&
735 cat expect >file &&
736 test_must_fail git checkout --conflict=diff3 --no-conflict -- fild file 2>err &&
737 test_cmp expect file &&
738 test_cmp expect fild &&
739 echo "error: path ${SQ}file${SQ} is unmerged" >expect &&
740 test_cmp expect err
741 '
742
743 test_expect_success 'checkout --conflict=diff3 --no-merge does not merge' '
744 setup_conflicting_index &&
745 echo "none of the above" >expect &&
746 cat expect >fild &&
747 cat expect >file &&
748 test_must_fail git checkout --conflict=diff3 --no-merge -- fild file 2>err &&
749 test_cmp expect file &&
750 test_cmp expect fild &&
751 echo "error: path ${SQ}file${SQ} is unmerged" >expect &&
752 test_cmp expect err
753 '
754
755 test_expect_success 'checkout --no-merge --conflict=diff3 does merge' '
756 setup_conflicting_index &&
757 echo "none of the above" >fild &&
758 echo "none of the above" >file &&
759 git checkout --no-merge --conflict=diff3 -- fild file &&
760 echo "ourside" >expect &&
761 test_cmp expect fild &&
762 cat >expect <<-\EOF &&
763 <<<<<<< ours
764 ourside
765 ||||||| base
766 original
767 =======
768 theirside
769 >>>>>>> theirs
770 EOF
771 test_cmp expect file
772 '
773
774 test_expect_success 'checkout --merge --conflict=diff3 --no-conflict does merge' '
775 setup_conflicting_index &&
776 echo "none of the above" >fild &&
777 echo "none of the above" >file &&
778 git checkout --merge --conflict=diff3 --no-conflict -- fild file &&
779 echo "ourside" >expect &&
780 test_cmp expect fild &&
781 cat >expect <<-\EOF &&
782 <<<<<<< ours
783 ourside
784 =======
785 theirside
786 >>>>>>> theirs
787 EOF
788 test_cmp expect file
789 '
790
791 test_expect_success 'checkout with invalid conflict style' '
792 test_must_fail git checkout --conflict=bad 2>actual -- file &&
793 echo "error: unknown conflict style ${SQ}bad${SQ}" >expect &&
794 test_cmp expect actual
795 '
796
797 test_expect_success 'failing checkout -b should not break working tree' '
798 git clean -fd && # Remove untracked files in the way
799 git reset --hard main &&
800 git symbolic-ref HEAD refs/heads/main &&
801 test_must_fail git checkout -b renamer side^ &&
802 test $(git symbolic-ref HEAD) = refs/heads/main &&
803 git diff --exit-code &&
804 git diff --cached --exit-code
805 '
806
807 test_expect_success 'switch out of non-branch' '
808 git reset --hard main &&
809 git checkout main^0 &&
810 echo modified >one &&
811 test_must_fail git checkout renamer 2>error.log &&
812 test_grep ! "^Previous HEAD" error.log
813 '
814
815 (
816 echo "#!$SHELL_PATH"
817 cat <<\EOF
818 O=$1 A=$2 B=$3
819 cat "$A" >.tmp
820 exec >"$A"
821 echo '<<<<<<< filfre-theirs'
822 cat "$B"
823 echo '||||||| filfre-common'
824 cat "$O"
825 echo '======='
826 cat ".tmp"
827 echo '>>>>>>> filfre-ours'
828 rm -f .tmp
829 exit 1
830 EOF
831 ) >filfre.sh
832 chmod +x filfre.sh
833
834 test_expect_success 'custom merge driver with checkout -m' '
835 git reset --hard &&
836
837 git config merge.filfre.driver "./filfre.sh %O %A %B" &&
838 git config merge.filfre.name "Feel-free merge driver" &&
839 git config merge.filfre.recursive binary &&
840 echo "arm merge=filfre" >.gitattributes &&
841
842 git checkout -b left &&
843 echo neutral >arm &&
844 git add arm .gitattributes &&
845 test_tick &&
846 git commit -m neutral &&
847 git branch right &&
848
849 echo left >arm &&
850 test_tick &&
851 git commit -a -m left &&
852 git checkout right &&
853
854 echo right >arm &&
855 test_tick &&
856 git commit -a -m right &&
857
858 test_must_fail git merge left &&
859 (
860 for t in filfre-common left right
861 do
862 test_grep $t arm || exit 1
863 done
864 ) &&
865
866 mv arm expect &&
867 git checkout -m arm &&
868 test_cmp expect arm
869 '
870
871 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
872 git reset --hard main &&
873 # default config does not copy tracking info
874 git checkout -b foo-no-inherit koala/bear &&
875 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
876 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
877 # with autoSetupMerge=inherit, we copy tracking info from koala/bear
878 test_config branch.autoSetupMerge inherit &&
879 git checkout -b foo koala/bear &&
880 test_cmp_config origin branch.foo.remote &&
881 test_cmp_config refs/heads/koala/bear branch.foo.merge &&
882 # no tracking info to inherit from main
883 git checkout -b main2 main &&
884 test_cmp_config "" --default "" branch.main2.remote &&
885 test_cmp_config "" --default "" branch.main2.merge
886 '
887
888 test_expect_success 'setup upstream for --track=fetch tests' '
889 git checkout main &&
890 git init fetch_upstream &&
891 test_commit -C fetch_upstream u_main &&
892 git remote add fetch_upstream fetch_upstream &&
893 git fetch fetch_upstream &&
894 git -C fetch_upstream checkout -b fetch_new &&
895 test_commit -C fetch_upstream u_new
896 '
897
898 test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '
899 git checkout main &&
900 test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&
901 git checkout --track=fetch -b local_new fetch_upstream/fetch_new &&
902 test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&
903 test_cmp_config fetch_upstream branch.local_new.remote &&
904 test_cmp_config refs/heads/fetch_new branch.local_new.merge
905 '
906
907 test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '
908 git checkout main &&
909 git -C fetch_upstream checkout -b fetch_target &&
910 test_commit -C fetch_upstream u_target_pre &&
911 git -C fetch_upstream checkout -b fetch_other &&
912 test_commit -C fetch_upstream u_other_pre &&
913 git fetch fetch_upstream &&
914 git update-ref refs/heads/snapshot_other refs/remotes/fetch_upstream/fetch_other &&
915 git -C fetch_upstream checkout fetch_target &&
916 test_commit -C fetch_upstream u_target_post &&
917 git -C fetch_upstream checkout fetch_other &&
918 test_commit -C fetch_upstream u_other_post &&
919 git checkout --track=fetch -b local_target fetch_upstream/fetch_target &&
920 test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&
921 test_cmp_rev refs/remotes/fetch_upstream/fetch_other snapshot_other
922 '
923
924 test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '
925 git checkout main &&
926 git -C fetch_upstream checkout main &&
927 git remote set-head fetch_upstream main &&
928 git -C fetch_upstream checkout -b fetch_unrelated &&
929 test_commit -C fetch_upstream u_unrelated_pre &&
930 git fetch fetch_upstream fetch_unrelated &&
931 git update-ref refs/heads/snapshot_unrelated \
932 refs/remotes/fetch_upstream/fetch_unrelated &&
933 git -C fetch_upstream checkout main &&
934 test_commit -C fetch_upstream u_main_post &&
935 git -C fetch_upstream checkout fetch_unrelated &&
936 test_commit -C fetch_upstream u_unrelated_post &&
937 git checkout --track=fetch -b local_from_remote fetch_upstream &&
938 test_cmp_rev refs/remotes/fetch_upstream/main HEAD &&
939 test_cmp_rev refs/remotes/fetch_upstream/fetch_unrelated snapshot_unrelated
940 '
941
942 test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '
943 git checkout main &&
944 test_might_fail git branch -D bogus &&
945 test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&
946 test_must_fail git rev-parse --verify refs/heads/bogus
947 '
948
949 test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '
950 git checkout main &&
951 git -C fetch_upstream checkout -b fetch_offline &&
952 test_commit -C fetch_upstream u_offline &&
953 git fetch fetch_upstream fetch_offline &&
954 saved_url=$(git config remote.fetch_upstream.url) &&
955 test_when_finished "git config remote.fetch_upstream.url \"$saved_url\"" &&
956 git config remote.fetch_upstream.url ./does-not-exist &&
957 git checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&
958 test_grep "failed to fetch" err &&
959 test_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD
960 '
961
962 test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '
963 git checkout main &&
964 git remote add fetch_custom ./fetch_upstream &&
965 test_when_finished "git remote remove fetch_custom" &&
966 git config --replace-all remote.fetch_custom.fetch \
967 "+refs/heads/*:refs/remotes/custom-ns/*" &&
968 git -C fetch_upstream checkout -b fetch_refspec &&
969 test_commit -C fetch_upstream u_refspec &&
970 test_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&
971 git checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&
972 test_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD
973 '
974
975 test_expect_success 'checkout --track=fetch on bare remote-tracking prefix follows <prefix>/HEAD' '
976 git checkout main &&
977 git remote add fetch_ns ./fetch_upstream &&
978 test_when_finished "git remote remove fetch_ns" &&
979 test_when_finished "git update-ref -d refs/remotes/ns_alias/HEAD" &&
980 git config --replace-all remote.fetch_ns.fetch \
981 "+refs/heads/*:refs/remotes/ns_alias/*" &&
982 git fetch fetch_ns &&
983 git symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&
984 git -C fetch_upstream checkout main &&
985 test_commit -C fetch_upstream u_ns_post &&
986 git checkout --track=fetch -b local_ns ns_alias &&
987 test_cmp_rev refs/remotes/ns_alias/main HEAD &&
988 test_cmp_config fetch_ns branch.local_ns.remote &&
989 test_cmp_config refs/heads/main branch.local_ns.merge
990 '
991
992 test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '
993 git checkout main &&
994 git remote add fetch_nohead ./fetch_upstream &&
995 test_when_finished "git remote remove fetch_nohead" &&
996 test_might_fail git symbolic-ref -d refs/remotes/fetch_nohead/HEAD &&
997 test_must_fail git checkout --track=fetch -b local_nohead fetch_nohead 2>err &&
998 test_grep "refs/remotes/fetch_nohead/HEAD" err &&
999 test_grep "git remote set-head fetch_nohead --auto" err &&
1000 test_must_fail git rev-parse --verify refs/heads/local_nohead
1001 '
1002
1003 test_expect_success 'checkout --track=fetch on bare unknown name does not suggest set-head' '
1004 git checkout main &&
1005 test_must_fail git rev-parse --verify refs/remotes/no_such_ns/HEAD &&
1006 test_must_fail git config --get remote.no_such_ns.url &&
1007 test_must_fail git checkout --track=fetch -b local_unknown no_such_ns 2>err &&
1008 test_grep "no configured remote" err &&
1009 test_grep ! "set-head" err &&
1010 test_must_fail git rev-parse --verify refs/heads/local_unknown
1011 '
1012
1013 test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside the tracking prefix' '
1014 git checkout main &&
1015 git remote add fetch_crossns ./fetch_upstream &&
1016 test_when_finished "git remote remove fetch_crossns" &&
1017 test_when_finished "git update-ref -d refs/remotes/fetch_crossns/HEAD" &&
1018 git fetch fetch_crossns &&
1019 git symbolic-ref refs/remotes/fetch_crossns/HEAD \
1020 refs/remotes/fetch_upstream/u_main &&
1021 test_must_fail git checkout --track=fetch -b local_crossns fetch_crossns 2>err &&
1022 test_grep "refs/remotes/fetch_crossns/HEAD" err &&
1023 test_must_fail git rev-parse --verify refs/heads/local_crossns
1024 '
1025
1026 test_expect_success 'checkout --track=fetch dies on ambiguous fetch refspec match' '
1027 git checkout main &&
1028 git remote add fetch_ambig_a ./fetch_upstream &&
1029 git remote add fetch_ambig_b ./fetch_upstream &&
1030 test_when_finished "git remote remove fetch_ambig_a" &&
1031 test_when_finished "git remote remove fetch_ambig_b" &&
1032 git config --replace-all remote.fetch_ambig_a.fetch \
1033 "+refs/heads/*:refs/remotes/ambig_ns/*" &&
1034 git config --replace-all remote.fetch_ambig_b.fetch \
1035 "+refs/heads/*:refs/remotes/ambig_ns/*" &&
1036 git -C fetch_upstream checkout -b fetch_ambig &&
1037 test_commit -C fetch_upstream u_ambig &&
1038 test_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&
1039 test_grep "fetch_ambig_a" err &&
1040 test_grep "fetch_ambig_b" err &&
1041 test_grep "tracking namespaces" err &&
1042 test_must_fail git rev-parse --verify refs/heads/local_ambig
1043 '
1044
1045 test_expect_success 'checkout --track=fetch rejects invalid refname components' '
1046 git checkout main &&
1047 test_must_fail git checkout --track=fetch -b local_invalid "foo..bar" 2>err &&
1048 test_grep "valid" err &&
1049 test_must_fail git rev-parse --verify refs/heads/local_invalid
1050 '
1051
1052 test_expect_success 'checkout --track=inherit,direct is rejected' '
1053 test_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&
1054 test_grep "cannot combine" err
1055 '
1056
1057 test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '
1058 git checkout main &&
1059 git -C fetch_upstream checkout -b fetch_lastwin &&
1060 test_commit -C fetch_upstream u_lastwin &&
1061 test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&
1062 test_must_fail git checkout --track=fetch --track=direct \
1063 -b local_lastwin fetch_upstream/fetch_lastwin &&
1064 test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin
1065 '
1066
1067 test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '
1068 git checkout main &&
1069 git -C fetch_upstream checkout -b fetch_inherit &&
1070 test_commit -C fetch_upstream u_inherit &&
1071 test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_inherit &&
1072 git checkout --track=fetch,inherit -b local_inherit \
1073 fetch_upstream/fetch_inherit &&
1074 test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD
1075 '
1076
1077 test_expect_success 'checkout --track=fetch on local start-point errors' '
1078 git checkout main &&
1079 test_must_fail git checkout --track=fetch -b bad main 2>err &&
1080 test_grep "no configured remote" err &&
1081 test_must_fail git rev-parse --verify refs/heads/bad
1082 '
1083
1084 test_expect_success 'checkout --track=bogus reports an error' '
1085 git checkout main &&
1086 test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&
1087 test_grep "expects" err
1088 '
1089
1090 test_expect_success 'checkout -q --track=fetch silences the fetch output' '
1091 git checkout main &&
1092 git -C fetch_upstream checkout -b fetch_quiet &&
1093 test_commit -C fetch_upstream u_quiet &&
1094 test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_quiet &&
1095 git checkout -q --track=fetch -b local_quiet \
1096 fetch_upstream/fetch_quiet 2>err &&
1097 test_grep ! "-> fetch_upstream/fetch_quiet" err &&
1098 test_cmp_rev refs/remotes/fetch_upstream/fetch_quiet HEAD
1099 '
1100
1101 test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '
1102 git checkout main &&
1103 git -C fetch_upstream checkout -b fetch_switch &&
1104 test_commit -C fetch_upstream u_switch &&
1105 test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&
1106 git switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&
1107 test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD
1108 '
1109
1110 test_done