3 # Copyright (c) 2006 Junio C Hamano
6 test_description
='git checkout tests.
8 Creates master, forks renamer and side branches from it.
9 Test switching across them.
11 ! [master] 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 +*++ [master] Initial A one, A two
35 test_expect_success setup
'
38 fill 1 2 3 4 5 6 7 8 >one &&
39 fill a b c d e >two &&
40 git add same one two &&
41 git commit -m "Initial A one, A two" &&
43 git checkout -b renamer &&
45 fill 1 3 4 5 6 7 8 >uno &&
47 fill a b c d e f >two &&
48 git commit -a -m "Renamer R one->uno, M two" &&
50 git checkout -b side master &&
51 fill 1 2 3 4 5 6 7 >one &&
52 fill A B C D E >three &&
54 git update-index --add --remove one two three &&
55 git commit -m "Side M one, D two, A three" &&
57 git checkout -b simple master &&
60 git commit -a -m "Simple D one, M two" &&
65 test_expect_success
"checkout from non-existing branch" '
67 git checkout -b delete-me master &&
68 git update-ref -d --no-deref refs/heads/delete-me &&
69 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
70 git checkout master &&
71 test refs/heads/master = "$(git symbolic-ref HEAD)"
74 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"
87 test_expect_success
"checkout with unrelated dirty tree without -m" '
89 git checkout -f master &&
90 fill 0 1 2 3 4 5 6 7 8 >same &&
92 git checkout side >messages &&
94 printf "M\t%s\n" same >messages.expect &&
95 test_cmp messages.expect messages
98 test_expect_success
"checkout -m with dirty tree" '
100 git checkout -f master &&
103 fill 0 1 2 3 4 5 6 7 8 >one &&
104 git checkout -m side > messages &&
106 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
108 printf "M\t%s\n" one >expect.messages &&
109 test_cmp expect.messages messages &&
111 fill "M one" "A three" "D two" >expect.master &&
112 git diff --name-status master >current.master &&
113 test_cmp expect.master current.master &&
115 fill "M one" >expect.side &&
116 git diff --name-status side >current.side &&
117 test_cmp expect.side current.side &&
119 git diff --cached >current.index &&
120 test_must_be_empty current.index
123 test_expect_success
"checkout -m with dirty tree, renamed" '
125 git checkout -f master && git clean -f &&
127 fill 1 2 3 4 5 7 8 >one &&
128 if git checkout renamer
133 echo "happy - failed correctly"
136 git checkout -m renamer &&
137 fill 1 3 4 5 7 8 >expect &&
138 test_cmp expect uno &&
140 git diff --cached >current &&
141 test_must_be_empty current
145 test_expect_success
'checkout -m with merge conflict' '
147 git checkout -f master && git clean -f &&
149 fill 1 T 3 4 5 6 S 8 >one &&
150 if git checkout renamer
155 echo "happy - failed correctly"
158 git checkout -m renamer &&
160 git diff master:one :3:uno |
161 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
162 fill d2 aT d7 aS >expect &&
163 test_cmp expect current &&
164 git diff --cached two >current &&
165 test_must_be_empty current
168 test_expect_success
'format of merge conflict from checkout -m' '
170 git checkout -f master && git clean -f &&
173 git checkout -m simple &&
175 git ls-files >current &&
176 fill same two two two >expect &&
177 test_cmp expect current &&
179 cat <<-EOF >expect &&
192 test_expect_success
'checkout --merge --conflict=diff3 <branch>' '
194 git checkout -f master && git reset --hard && git clean -f &&
197 git checkout --merge --conflict=diff3 simple &&
199 cat <<-EOF >expect &&
218 test_expect_success
'switch to another branch while carrying a deletion' '
220 git checkout -f master && git reset --hard && git clean -f &&
223 test_must_fail git checkout simple 2>errs &&
224 test_i18ngrep overwritten errs &&
226 git checkout --merge simple 2>errs &&
227 test_i18ngrep ! overwritten errs &&
229 test_must_fail git cat-file -t :0:two &&
230 test "$(git cat-file -t :1:two)" = blob &&
231 test "$(git cat-file -t :2:two)" = blob &&
232 test_must_fail git cat-file -t :3:two
235 test_expect_success
'checkout to detach HEAD (with advice declined)' '
237 git config advice.detachedHead false &&
238 git checkout -f renamer && git clean -f &&
239 git checkout renamer^ 2>messages &&
240 test_i18ngrep "HEAD is now at 7329388" messages &&
241 test_line_count = 1 messages &&
242 H=$(git rev-parse --verify HEAD) &&
243 M=$(git show-ref -s --verify refs/heads/master) &&
244 test "z$H" = "z$M" &&
245 if git symbolic-ref HEAD >/dev/null 2>&1
247 echo "OOPS, HEAD is still symbolic???"
254 test_expect_success
'checkout to detach HEAD' '
255 git config advice.detachedHead true &&
256 git checkout -f renamer && git clean -f &&
257 GIT_TEST_GETTEXT_POISON= git checkout renamer^ 2>messages &&
258 grep "HEAD is now at 7329388" messages &&
259 test_line_count -gt 1 messages &&
260 H=$(git rev-parse --verify HEAD) &&
261 M=$(git show-ref -s --verify refs/heads/master) &&
262 test "z$H" = "z$M" &&
263 if git symbolic-ref HEAD >/dev/null 2>&1
265 echo "OOPS, HEAD is still symbolic???"
272 test_expect_success
'checkout to detach HEAD with branchname^' '
274 git checkout -f master && git clean -f &&
275 git checkout renamer^ &&
276 H=$(git rev-parse --verify HEAD) &&
277 M=$(git show-ref -s --verify refs/heads/master) &&
278 test "z$H" = "z$M" &&
279 if git symbolic-ref HEAD >/dev/null 2>&1
281 echo "OOPS, HEAD is still symbolic???"
288 test_expect_success
'checkout to detach HEAD with :/message' '
290 git checkout -f master && git clean -f &&
291 git checkout ":/Initial" &&
292 H=$(git rev-parse --verify HEAD) &&
293 M=$(git show-ref -s --verify refs/heads/master) &&
294 test "z$H" = "z$M" &&
295 if git symbolic-ref HEAD >/dev/null 2>&1
297 echo "OOPS, HEAD is still symbolic???"
304 test_expect_success
'checkout to detach HEAD with HEAD^0' '
306 git checkout -f master && git clean -f &&
307 git checkout HEAD^0 &&
308 H=$(git rev-parse --verify HEAD) &&
309 M=$(git show-ref -s --verify refs/heads/master) &&
310 test "z$H" = "z$M" &&
311 if git symbolic-ref HEAD >/dev/null 2>&1
313 echo "OOPS, HEAD is still symbolic???"
320 test_expect_success
'checkout with ambiguous tag/branch names' '
323 git branch both master &&
325 git checkout master &&
328 H=$(git rev-parse --verify HEAD) &&
329 M=$(git show-ref -s --verify refs/heads/master) &&
330 test "z$H" = "z$M" &&
331 name=$(git symbolic-ref HEAD 2>/dev/null) &&
332 test "z$name" = zrefs/heads/both
336 test_expect_success
'checkout with ambiguous tag/branch names' '
339 git checkout master &&
341 git tag frotz side &&
342 git branch frotz master &&
344 git checkout master &&
346 git checkout tags/frotz &&
347 H=$(git rev-parse --verify HEAD) &&
348 S=$(git show-ref -s --verify refs/heads/side) &&
349 test "z$H" = "z$S" &&
350 if name=$(git symbolic-ref HEAD 2>/dev/null)
352 echo "Bad -- should have detached"
360 test_expect_success
'switch branches while in subdirectory' '
363 git checkout master &&
370 ! test -f subs/one &&
375 test_expect_success
'checkout specific path while in subdirectory' '
382 git commit -m "add subs/bero" &&
384 git checkout master &&
388 git checkout side -- bero
394 test_expect_success \
395 'checkout w/--track sets up tracking' '
396 git config branch.autosetupmerge false &&
397 git checkout master &&
398 git checkout --track -b track1 &&
399 test "$(git config branch.track1.remote)" &&
400 test "$(git config branch.track1.merge)"'
402 test_expect_success \
403 'checkout w/autosetupmerge=always sets up tracking' '
404 test_when_finished git config branch.autosetupmerge false &&
405 git config branch.autosetupmerge always &&
406 git checkout master &&
407 git checkout -b track2 &&
408 test "$(git config branch.track2.remote)" &&
409 test "$(git config branch.track2.merge)"'
411 test_expect_success
'checkout w/--track from non-branch HEAD fails' '
412 git checkout master^0 &&
413 test_must_fail git symbolic-ref HEAD &&
414 test_must_fail git checkout --track -b track &&
415 test_must_fail git rev-parse --verify track &&
416 test_must_fail git symbolic-ref HEAD &&
417 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
420 test_expect_success
'checkout w/--track from tag fails' '
421 git checkout master^0 &&
422 test_must_fail git symbolic-ref HEAD &&
423 test_must_fail git checkout --track -b track frotz &&
424 test_must_fail git rev-parse --verify track &&
425 test_must_fail git symbolic-ref HEAD &&
426 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
429 test_expect_success
'detach a symbolic link HEAD' '
430 git checkout master &&
431 git config --bool core.prefersymlinkrefs yes &&
433 git checkout master &&
434 it=$(git symbolic-ref HEAD) &&
435 test "z$it" = zrefs/heads/master &&
436 here=$(git rev-parse --verify refs/heads/master) &&
437 git checkout side^ &&
438 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
441 test_expect_success \
442 'checkout with --track fakes a sensible -b <name>' '
443 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
444 git update-ref refs/remotes/origin/koala/bear renamer &&
446 git checkout --track origin/koala/bear &&
447 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
448 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
450 git checkout master && git branch -D koala/bear &&
452 git checkout --track refs/remotes/origin/koala/bear &&
453 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
454 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
456 git checkout master && git branch -D koala/bear &&
458 git checkout --track remotes/origin/koala/bear &&
459 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
460 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
463 test_expect_success \
464 'checkout with --track, but without -b, fails with too short tracked name' '
465 test_must_fail git checkout --track renamer'
467 setup_conflicting_index
() {
469 O
=$
(echo original | git hash-object
-w --stdin) &&
470 A
=$
(echo ourside | git hash-object
-w --stdin) &&
471 B
=$
(echo theirside | git hash-object
-w --stdin) &&
473 echo "100644 $A 0 fild" &&
474 echo "100644 $O 1 file" &&
475 echo "100644 $A 2 file" &&
476 echo "100644 $B 3 file" &&
477 echo "100644 $A 0 filf"
478 ) | git update-index
--index-info
481 test_expect_success
'checkout an unmerged path should fail' '
482 setup_conflicting_index &&
483 echo "none of the above" >sample &&
487 test_must_fail git checkout fild file filf &&
488 test_cmp sample fild &&
489 test_cmp sample filf &&
493 test_expect_success
'checkout with an unmerged path can be ignored' '
494 setup_conflicting_index &&
495 echo "none of the above" >sample &&
496 echo ourside >expect &&
500 git checkout -f fild file filf &&
501 test_cmp expect fild &&
502 test_cmp expect filf &&
506 test_expect_success
'checkout unmerged stage' '
507 setup_conflicting_index &&
508 echo "none of the above" >sample &&
509 echo ourside >expect &&
513 git checkout --ours . &&
514 test_cmp expect fild &&
515 test_cmp expect filf &&
516 test_cmp expect file &&
517 git checkout --theirs file &&
518 test ztheirside = "z$(cat file)"
521 test_expect_success
'checkout with --merge' '
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" &&
534 echo ">>>>>>> theirs"
536 test_cmp expect fild &&
537 test_cmp expect filf &&
541 test_expect_success
'checkout with --merge, in diff3 -m style' '
542 git config merge.conflictstyle diff3 &&
543 setup_conflicting_index &&
544 echo "none of the above" >sample &&
545 echo ourside >expect &&
549 git checkout -m -- fild file filf &&
551 echo "<<<<<<< ours" &&
553 echo "||||||| base" &&
557 echo ">>>>>>> theirs"
559 test_cmp expect fild &&
560 test_cmp expect filf &&
564 test_expect_success
'checkout --conflict=merge, overriding config' '
565 git config merge.conflictstyle diff3 &&
566 setup_conflicting_index &&
567 echo "none of the above" >sample &&
568 echo ourside >expect &&
572 git checkout --conflict=merge -- fild file filf &&
574 echo "<<<<<<< ours" &&
578 echo ">>>>>>> theirs"
580 test_cmp expect fild &&
581 test_cmp expect filf &&
585 test_expect_success
'checkout --conflict=diff3' '
586 test_unconfig merge.conflictstyle &&
587 setup_conflicting_index &&
588 echo "none of the above" >sample &&
589 echo ourside >expect &&
593 git checkout --conflict=diff3 -- fild file filf &&
595 echo "<<<<<<< ours" &&
597 echo "||||||| base" &&
601 echo ">>>>>>> theirs"
603 test_cmp expect fild &&
604 test_cmp expect filf &&
608 test_expect_success
'failing checkout -b should not break working tree' '
609 git reset --hard master &&
610 git symbolic-ref HEAD refs/heads/master &&
611 test_must_fail git checkout -b renamer side^ &&
612 test $(git symbolic-ref HEAD) = refs/heads/master &&
613 git diff --exit-code &&
614 git diff --cached --exit-code
618 test_expect_success
'switch out of non-branch' '
619 git reset --hard master &&
620 git checkout master^0 &&
621 echo modified >one &&
622 test_must_fail git checkout renamer 2>error.log &&
623 ! grep "^Previous HEAD" error.log
632 echo '<<<<<<< filfre-theirs'
634 echo '||||||| filfre-common
'
638 echo '>>>>>>> filfre-ours
'
645 test_expect_success 'custom merge driver with checkout
-m' '
648 git config merge.filfre.driver
"./filfre.sh %O %A %B" &&
649 git config merge.filfre.name
"Feel-free merge driver" &&
650 git config merge.filfre.recursive binary
&&
651 echo "arm merge=filfre" >.gitattributes
&&
653 git checkout
-b left
&&
655 git add arm .gitattributes
&&
657 git commit
-m neutral
&&
662 git commit
-a -m left
&&
663 git checkout right
&&
667 git commit
-a -m right
&&
669 test_must_fail git merge left
&&
671 for t
in filfre-common left right
673 grep $t arm ||
exit 1
678 git checkout
-m arm
&&