3 # Copyright (c) 2006 Junio C Hamano
6 test_description
='git checkout tests.
8 Creates main, forks renamer and side branches from it.
9 Test switching across them.
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
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
23 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
24 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
38 test_expect_success setup
'
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" &&
45 git checkout -b renamer &&
47 fill 1 3 4 5 6 7 8 >uno &&
49 fill a b c d e f >two &&
50 git commit -a -m "Renamer R one->uno, M two" &&
52 git checkout -b side main &&
53 fill 1 2 3 4 5 6 7 >one &&
54 fill A B C D E >three &&
56 git update-index --add --remove one two three &&
57 git commit -m "Side M one, D two, A three" &&
59 git checkout -b simple main &&
62 git commit -a -m "Simple D one, M two" &&
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)" &&
72 test refs/heads/main = "$(git symbolic-ref HEAD)"
75 test_expect_success
'checkout with dirty tree without -m' '
76 fill 0 1 2 3 4 5 6 7 8 >one &&
82 echo "happy - failed correctly"
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 &&
90 git checkout side >messages &&
92 printf "M\t%s\n" same >messages.expect &&
93 test_cmp messages.expect messages
96 test_expect_success
'checkout -m with dirty tree' '
97 git checkout -f main &&
100 fill 0 1 2 3 4 5 6 7 8 >one &&
101 git checkout -m side >messages &&
103 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
105 printf "M\t%s\n" one >expect.messages &&
106 test_cmp expect.messages messages &&
108 fill "M one" "A three" "D two" >expect.main &&
109 git diff --name-status main >current.main &&
110 test_cmp expect.main current.main &&
112 fill "M one" >expect.side &&
113 git diff --name-status side >current.side &&
114 test_cmp expect.side current.side &&
116 git diff --cached >current.index &&
117 test_must_be_empty current.index
120 test_expect_success
'checkout -m with dirty tree, renamed' '
121 git checkout -f main && git clean -f &&
123 fill 1 2 3 4 5 7 8 >one &&
124 if git checkout renamer
129 echo "happy - failed correctly"
132 git checkout -m renamer &&
133 fill 1 3 4 5 7 8 >expect &&
134 test_cmp expect uno &&
136 git diff --cached >current &&
137 test_must_be_empty current
140 test_expect_success
'checkout -m with merge conflict' '
141 git checkout -f main && git clean -f &&
143 fill 1 T 3 4 5 6 S 8 >one &&
144 if git checkout renamer
149 echo "happy - failed correctly"
152 git checkout -m renamer &&
154 git diff main:one :3:uno |
155 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
156 fill d2 aT d7 aS >expect &&
157 test_cmp expect current &&
158 git diff --cached two >current &&
159 test_must_be_empty current
162 test_expect_success
'format of merge conflict from checkout -m' '
163 git checkout -f main &&
167 git checkout -m simple &&
169 git ls-files >current &&
170 fill same two two two >expect &&
171 test_cmp expect current &&
173 cat <<-EOF >expect &&
186 test_expect_success
'checkout --merge --conflict=diff3 <branch>' '
187 git checkout -f main &&
192 git checkout --merge --conflict=diff3 simple &&
194 cat <<-EOF >expect &&
213 test_expect_success
'switch to another branch while carrying a deletion' '
214 git checkout -f main &&
219 test_must_fail git checkout simple 2>errs &&
220 test_i18ngrep overwritten errs &&
222 test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs &&
223 test_must_be_empty errs
226 test_expect_success
'checkout to detach HEAD (with advice declined)' '
227 git config advice.detachedHead false &&
228 rev=$(git rev-parse --short renamer^) &&
229 git checkout -f renamer &&
231 git checkout renamer^ 2>messages &&
232 test_i18ngrep "HEAD is now at $rev" messages &&
233 test_line_count = 1 messages &&
234 H=$(git rev-parse --verify HEAD) &&
235 M=$(git show-ref -s --verify refs/heads/main) &&
236 test "z$H" = "z$M" &&
237 if git symbolic-ref HEAD >/dev/null 2>&1
239 echo "OOPS, HEAD is still symbolic???"
246 test_expect_success
'checkout to detach HEAD' '
247 git config advice.detachedHead true &&
248 rev=$(git rev-parse --short renamer^) &&
249 git checkout -f renamer &&
251 git checkout renamer^ 2>messages &&
252 grep "HEAD is now at $rev" messages &&
253 test_line_count -gt 1 messages &&
254 H=$(git rev-parse --verify HEAD) &&
255 M=$(git show-ref -s --verify refs/heads/main) &&
256 test "z$H" = "z$M" &&
257 if git symbolic-ref HEAD >/dev/null 2>&1
259 echo "OOPS, HEAD is still symbolic???"
266 test_expect_success
'checkout to detach HEAD with branchname^' '
267 git checkout -f main &&
269 git checkout renamer^ &&
270 H=$(git rev-parse --verify HEAD) &&
271 M=$(git show-ref -s --verify refs/heads/main) &&
272 test "z$H" = "z$M" &&
273 if git symbolic-ref HEAD >/dev/null 2>&1
275 echo "OOPS, HEAD is still symbolic???"
282 test_expect_success
'checkout to detach HEAD with :/message' '
283 git checkout -f main &&
285 git checkout ":/Initial" &&
286 H=$(git rev-parse --verify HEAD) &&
287 M=$(git show-ref -s --verify refs/heads/main) &&
288 test "z$H" = "z$M" &&
289 if git symbolic-ref HEAD >/dev/null 2>&1
291 echo "OOPS, HEAD is still symbolic???"
298 test_expect_success
'checkout to detach HEAD with HEAD^0' '
299 git checkout -f main &&
301 git checkout HEAD^0 &&
302 H=$(git rev-parse --verify HEAD) &&
303 M=$(git show-ref -s --verify refs/heads/main) &&
304 test "z$H" = "z$M" &&
305 if git symbolic-ref HEAD >/dev/null 2>&1
307 echo "OOPS, HEAD is still symbolic???"
314 test_expect_success
'checkout with ambiguous tag/branch names' '
316 git branch both main &&
321 H=$(git rev-parse --verify HEAD) &&
322 M=$(git show-ref -s --verify refs/heads/main) &&
323 test "z$H" = "z$M" &&
324 name=$(git symbolic-ref HEAD 2>/dev/null) &&
325 test "z$name" = zrefs/heads/both
328 test_expect_success
'checkout with ambiguous tag/branch names' '
332 git tag frotz side &&
333 git branch frotz main &&
337 git checkout tags/frotz &&
338 H=$(git rev-parse --verify HEAD) &&
339 S=$(git show-ref -s --verify refs/heads/side) &&
340 test "z$H" = "z$S" &&
341 if name=$(git symbolic-ref HEAD 2>/dev/null)
343 echo "Bad -- should have detached"
350 test_expect_success
'switch branches while in subdirectory' '
355 git -C subs checkout side &&
356 ! test -f subs/one &&
360 test_expect_success
'checkout specific path while in subdirectory' '
366 git commit -m "add subs/bero" &&
370 git -C subs checkout side -- bero &&
374 test_expect_success
'checkout w/--track sets up tracking' '
375 git config branch.autosetupmerge false &&
377 git checkout --track -b track1 &&
378 test "$(git config branch.track1.remote)" &&
379 test "$(git config branch.track1.merge)"
382 test_expect_success
'checkout w/autosetupmerge=always sets up tracking' '
383 test_when_finished git config branch.autosetupmerge false &&
384 git config branch.autosetupmerge always &&
386 git checkout -b track2 &&
387 test "$(git config branch.track2.remote)" &&
388 test "$(git config branch.track2.merge)"
391 test_expect_success
'checkout w/--track from non-branch HEAD fails' '
392 git checkout main^0 &&
393 test_must_fail git symbolic-ref HEAD &&
394 test_must_fail git checkout --track -b track &&
395 test_must_fail git rev-parse --verify track &&
396 test_must_fail git symbolic-ref HEAD &&
397 test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)"
400 test_expect_success
'checkout w/--track from tag fails' '
401 git checkout main^0 &&
402 test_must_fail git symbolic-ref HEAD &&
403 test_must_fail git checkout --track -b track frotz &&
404 test_must_fail git rev-parse --verify track &&
405 test_must_fail git symbolic-ref HEAD &&
406 test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)"
409 test_expect_success
'detach a symbolic link HEAD' '
411 git config --bool core.prefersymlinkrefs yes &&
414 it=$(git symbolic-ref HEAD) &&
415 test "z$it" = zrefs/heads/main &&
416 here=$(git rev-parse --verify refs/heads/main) &&
417 git checkout side^ &&
418 test "z$(git rev-parse --verify refs/heads/main)" = "z$here"
421 test_expect_success
'checkout with --track fakes a sensible -b <name>' '
422 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
423 git update-ref refs/remotes/origin/koala/bear renamer &&
425 git checkout --track origin/koala/bear &&
426 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
427 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
429 git checkout main && git branch -D koala/bear &&
431 git checkout --track refs/remotes/origin/koala/bear &&
432 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
433 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
435 git checkout main && git branch -D koala/bear &&
437 git checkout --track remotes/origin/koala/bear &&
438 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
439 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
442 test_expect_success
'checkout with --track, but without -b, fails with too short tracked name' '
443 test_must_fail git checkout --track renamer
446 setup_conflicting_index
() {
448 O
=$
(echo original | git hash-object
-w --stdin) &&
449 A
=$
(echo ourside | git hash-object
-w --stdin) &&
450 B
=$
(echo theirside | git hash-object
-w --stdin) &&
452 echo "100644 $A 0 fild" &&
453 echo "100644 $O 1 file" &&
454 echo "100644 $A 2 file" &&
455 echo "100644 $B 3 file" &&
456 echo "100644 $A 0 filf"
457 ) | git update-index
--index-info
460 test_expect_success
'checkout an unmerged path should fail' '
461 setup_conflicting_index &&
462 echo "none of the above" >sample &&
466 test_must_fail git checkout fild file filf &&
467 test_cmp sample fild &&
468 test_cmp sample filf &&
472 test_expect_success
'checkout with an unmerged path can be ignored' '
473 setup_conflicting_index &&
474 echo "none of the above" >sample &&
475 echo ourside >expect &&
479 git checkout -f fild file filf &&
480 test_cmp expect fild &&
481 test_cmp expect filf &&
485 test_expect_success
'checkout unmerged stage' '
486 setup_conflicting_index &&
487 echo "none of the above" >sample &&
488 echo ourside >expect &&
492 git checkout --ours . &&
493 test_cmp expect fild &&
494 test_cmp expect filf &&
495 test_cmp expect file &&
496 git checkout --theirs file &&
497 test ztheirside = "z$(cat file)"
500 test_expect_success
'checkout with --merge' '
501 setup_conflicting_index &&
502 echo "none of the above" >sample &&
503 echo ourside >expect &&
507 git checkout -m -- fild file filf &&
509 echo "<<<<<<< ours" &&
513 echo ">>>>>>> theirs"
515 test_cmp expect fild &&
516 test_cmp expect filf &&
520 test_expect_success
'checkout with --merge, in diff3 -m style' '
521 git config merge.conflictstyle diff3 &&
522 setup_conflicting_index &&
523 echo "none of the above" >sample &&
524 echo ourside >expect &&
528 git checkout -m -- fild file filf &&
530 echo "<<<<<<< ours" &&
532 echo "||||||| base" &&
536 echo ">>>>>>> theirs"
538 test_cmp expect fild &&
539 test_cmp expect filf &&
543 test_expect_success
'checkout --conflict=merge, overriding config' '
544 git config merge.conflictstyle diff3 &&
545 setup_conflicting_index &&
546 echo "none of the above" >sample &&
547 echo ourside >expect &&
551 git checkout --conflict=merge -- fild file filf &&
553 echo "<<<<<<< ours" &&
557 echo ">>>>>>> theirs"
559 test_cmp expect fild &&
560 test_cmp expect filf &&
564 test_expect_success
'checkout --conflict=diff3' '
565 test_unconfig merge.conflictstyle &&
566 setup_conflicting_index &&
567 echo "none of the above" >sample &&
568 echo ourside >expect &&
572 git checkout --conflict=diff3 -- fild file filf &&
574 echo "<<<<<<< ours" &&
576 echo "||||||| base" &&
580 echo ">>>>>>> theirs"
582 test_cmp expect fild &&
583 test_cmp expect filf &&
587 test_expect_success
'failing checkout -b should not break working tree' '
588 git reset --hard main &&
589 git symbolic-ref HEAD refs/heads/main &&
590 test_must_fail git checkout -b renamer side^ &&
591 test $(git symbolic-ref HEAD) = refs/heads/main &&
592 git diff --exit-code &&
593 git diff --cached --exit-code
596 test_expect_success
'switch out of non-branch' '
597 git reset --hard main &&
598 git checkout main^0 &&
599 echo modified >one &&
600 test_must_fail git checkout renamer 2>error.log &&
601 ! grep "^Previous HEAD" error.log
610 echo '<<<<<<< filfre-theirs'
612 echo '||||||| filfre-common
'
616 echo '>>>>>>> filfre-ours
'
623 test_expect_success 'custom merge driver with checkout
-m' '
626 git config merge.filfre.driver
"./filfre.sh %O %A %B" &&
627 git config merge.filfre.name
"Feel-free merge driver" &&
628 git config merge.filfre.recursive binary
&&
629 echo "arm merge=filfre" >.gitattributes
&&
631 git checkout
-b left
&&
633 git add arm .gitattributes
&&
635 git commit
-m neutral
&&
640 git commit
-a -m left
&&
641 git checkout right
&&
645 git commit
-a -m right
&&
647 test_must_fail git merge left
&&
649 for t
in filfre-common left right
651 grep $t arm ||
exit 1
656 git checkout
-m arm
&&