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 rm .git/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 (cat > messages.expect <<EOF
98 touch messages.expect &&
99 test_cmp messages.expect messages
102 test_expect_success
"checkout -m with dirty tree" '
104 git checkout -f master &&
107 fill 0 1 2 3 4 5 6 7 8 >one &&
108 git checkout -m side > messages &&
110 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
112 (cat >expect.messages <<EOF
116 test_cmp expect.messages messages &&
118 fill "M one" "A three" "D two" >expect.master &&
119 git diff --name-status master >current.master &&
120 test_cmp expect.master current.master &&
122 fill "M one" >expect.side &&
123 git diff --name-status side >current.side &&
124 test_cmp expect.side current.side &&
127 git diff --cached >current.index &&
128 test_cmp expect.index current.index
131 test_expect_success
"checkout -m with dirty tree, renamed" '
133 git checkout -f master && git clean -f &&
135 fill 1 2 3 4 5 7 8 >one &&
136 if git checkout renamer
141 echo "happy - failed correctly"
144 git checkout -m renamer &&
145 fill 1 3 4 5 7 8 >expect &&
146 test_cmp expect uno &&
148 git diff --cached >current &&
153 test_expect_success
'checkout -m with merge conflict' '
155 git checkout -f master && git clean -f &&
157 fill 1 T 3 4 5 6 S 8 >one &&
158 if git checkout renamer
163 echo "happy - failed correctly"
166 git checkout -m renamer &&
168 git diff master:one :3:uno |
169 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
170 fill d2 aT d7 aS >expect &&
171 test_cmp current expect &&
172 git diff --cached two >current &&
176 test_expect_success
'format of merge conflict from checkout -m' '
178 git checkout -f master && git clean -f &&
181 git checkout -m simple &&
183 git ls-files >current &&
184 fill same two two two >expect &&
185 test_cmp current expect &&
187 cat <<-EOF >expect &&
200 test_expect_success
'checkout --merge --conflict=diff3 <branch>' '
202 git checkout -f master && git reset --hard && git clean -f &&
205 git checkout --merge --conflict=diff3 simple &&
207 cat <<-EOF >expect &&
226 test_expect_success
'switch to another branch while carrying a deletion' '
228 git checkout -f master && git reset --hard && git clean -f &&
231 test_must_fail git checkout simple 2>errs &&
232 test_i18ngrep overwritten errs &&
234 git checkout --merge simple 2>errs &&
235 test_i18ngrep ! overwritten errs &&
237 test_must_fail git cat-file -t :0:two &&
238 test "$(git cat-file -t :1:two)" = blob &&
239 test "$(git cat-file -t :2:two)" = blob &&
240 test_must_fail git cat-file -t :3:two
243 test_expect_success
'checkout to detach HEAD (with advice declined)' '
245 git config advice.detachedHead false &&
246 git checkout -f renamer && git clean -f &&
247 git checkout renamer^ 2>messages &&
248 test_i18ngrep "HEAD is now at 7329388" messages &&
249 test_line_count = 1 messages &&
250 H=$(git rev-parse --verify HEAD) &&
251 M=$(git show-ref -s --verify refs/heads/master) &&
252 test "z$H" = "z$M" &&
253 if git symbolic-ref HEAD >/dev/null 2>&1
255 echo "OOPS, HEAD is still symbolic???"
262 test_expect_success
'checkout to detach HEAD' '
263 git config advice.detachedHead true &&
264 git checkout -f renamer && git clean -f &&
265 git checkout renamer^ 2>messages &&
266 test_i18ngrep "HEAD is now at 7329388" messages &&
267 test_line_count -gt 1 messages &&
268 H=$(git rev-parse --verify HEAD) &&
269 M=$(git show-ref -s --verify refs/heads/master) &&
270 test "z$H" = "z$M" &&
271 if git symbolic-ref HEAD >/dev/null 2>&1
273 echo "OOPS, HEAD is still symbolic???"
280 test_expect_success
'checkout to detach HEAD with branchname^' '
282 git checkout -f master && git clean -f &&
283 git checkout renamer^ &&
284 H=$(git rev-parse --verify HEAD) &&
285 M=$(git show-ref -s --verify refs/heads/master) &&
286 test "z$H" = "z$M" &&
287 if git symbolic-ref HEAD >/dev/null 2>&1
289 echo "OOPS, HEAD is still symbolic???"
296 test_expect_success
'checkout to detach HEAD with :/message' '
298 git checkout -f master && git clean -f &&
299 git checkout ":/Initial" &&
300 H=$(git rev-parse --verify HEAD) &&
301 M=$(git show-ref -s --verify refs/heads/master) &&
302 test "z$H" = "z$M" &&
303 if git symbolic-ref HEAD >/dev/null 2>&1
305 echo "OOPS, HEAD is still symbolic???"
312 test_expect_success
'checkout to detach HEAD with HEAD^0' '
314 git checkout -f master && git clean -f &&
315 git checkout HEAD^0 &&
316 H=$(git rev-parse --verify HEAD) &&
317 M=$(git show-ref -s --verify refs/heads/master) &&
318 test "z$H" = "z$M" &&
319 if git symbolic-ref HEAD >/dev/null 2>&1
321 echo "OOPS, HEAD is still symbolic???"
328 test_expect_success
'checkout with ambiguous tag/branch names' '
331 git branch both master &&
333 git checkout master &&
336 H=$(git rev-parse --verify HEAD) &&
337 M=$(git show-ref -s --verify refs/heads/master) &&
338 test "z$H" = "z$M" &&
339 name=$(git symbolic-ref HEAD 2>/dev/null) &&
340 test "z$name" = zrefs/heads/both
344 test_expect_success
'checkout with ambiguous tag/branch names' '
347 git checkout master &&
349 git tag frotz side &&
350 git branch frotz master &&
352 git checkout master &&
354 git checkout tags/frotz &&
355 H=$(git rev-parse --verify HEAD) &&
356 S=$(git show-ref -s --verify refs/heads/side) &&
357 test "z$H" = "z$S" &&
358 if name=$(git symbolic-ref HEAD 2>/dev/null)
360 echo "Bad -- should have detached"
368 test_expect_success
'switch branches while in subdirectory' '
371 git checkout master &&
378 ! test -f subs/one &&
383 test_expect_success
'checkout specific path while in subdirectory' '
390 git commit -m "add subs/bero" &&
392 git checkout master &&
396 git checkout side -- bero
402 test_expect_success \
403 'checkout w/--track sets up tracking' '
404 git config branch.autosetupmerge false &&
405 git checkout master &&
406 git checkout --track -b track1 &&
407 test "$(git config branch.track1.remote)" &&
408 test "$(git config branch.track1.merge)"'
410 test_expect_success \
411 'checkout w/autosetupmerge=always sets up tracking' '
412 git config branch.autosetupmerge always &&
413 git checkout master &&
414 git checkout -b track2 &&
415 test "$(git config branch.track2.remote)" &&
416 test "$(git config branch.track2.merge)"
417 git config branch.autosetupmerge false'
419 test_expect_success
'checkout w/--track from non-branch HEAD fails' '
420 git checkout master^0 &&
421 test_must_fail git symbolic-ref HEAD &&
422 test_must_fail git checkout --track -b track &&
423 test_must_fail git rev-parse --verify track &&
424 test_must_fail git symbolic-ref HEAD &&
425 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
428 test_expect_success
'checkout w/--track from tag fails' '
429 git checkout master^0 &&
430 test_must_fail git symbolic-ref HEAD &&
431 test_must_fail git checkout --track -b track frotz &&
432 test_must_fail git rev-parse --verify track &&
433 test_must_fail git symbolic-ref HEAD &&
434 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
437 test_expect_success
'detach a symbolic link HEAD' '
438 git checkout master &&
439 git config --bool core.prefersymlinkrefs yes &&
441 git checkout master &&
442 it=$(git symbolic-ref HEAD) &&
443 test "z$it" = zrefs/heads/master &&
444 here=$(git rev-parse --verify refs/heads/master) &&
445 git checkout side^ &&
446 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
449 test_expect_success \
450 'checkout with --track fakes a sensible -b <name>' '
451 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
452 git update-ref refs/remotes/origin/koala/bear renamer &&
454 git checkout --track origin/koala/bear &&
455 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
456 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
458 git checkout master && git branch -D koala/bear &&
460 git checkout --track refs/remotes/origin/koala/bear &&
461 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
462 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
464 git checkout master && git branch -D koala/bear &&
466 git checkout --track remotes/origin/koala/bear &&
467 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
468 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
471 test_expect_success \
472 'checkout with --track, but without -b, fails with too short tracked name' '
473 test_must_fail git checkout --track renamer'
475 setup_conflicting_index
() {
477 O
=$
(echo original | git hash-object
-w --stdin) &&
478 A
=$
(echo ourside | git hash-object
-w --stdin) &&
479 B
=$
(echo theirside | git hash-object
-w --stdin) &&
481 echo "100644 $A 0 fild" &&
482 echo "100644 $O 1 file" &&
483 echo "100644 $A 2 file" &&
484 echo "100644 $B 3 file" &&
485 echo "100644 $A 0 filf"
486 ) | git update-index
--index-info
489 test_expect_success
'checkout an unmerged path should fail' '
490 setup_conflicting_index &&
491 echo "none of the above" >sample &&
495 test_must_fail git checkout fild file filf &&
496 test_cmp sample fild &&
497 test_cmp sample filf &&
501 test_expect_success
'checkout with an unmerged path can be ignored' '
502 setup_conflicting_index &&
503 echo "none of the above" >sample &&
504 echo ourside >expect &&
508 git checkout -f fild file filf &&
509 test_cmp expect fild &&
510 test_cmp expect filf &&
514 test_expect_success
'checkout unmerged stage' '
515 setup_conflicting_index &&
516 echo "none of the above" >sample &&
517 echo ourside >expect &&
521 git checkout --ours . &&
522 test_cmp expect fild &&
523 test_cmp expect filf &&
524 test_cmp expect file &&
525 git checkout --theirs file &&
526 test ztheirside = "z$(cat file)"
529 test_expect_success
'checkout with --merge' '
530 setup_conflicting_index &&
531 echo "none of the above" >sample &&
532 echo ourside >expect &&
536 git checkout -m -- fild file filf &&
542 echo ">>>>>>> theirs"
544 test_cmp expect fild &&
545 test_cmp expect filf &&
549 test_expect_success
'checkout with --merge, in diff3 -m style' '
550 git config merge.conflictstyle diff3 &&
551 setup_conflicting_index &&
552 echo "none of the above" >sample &&
553 echo ourside >expect &&
557 git checkout -m -- fild file filf &&
565 echo ">>>>>>> theirs"
567 test_cmp expect fild &&
568 test_cmp expect filf &&
572 test_expect_success
'checkout --conflict=merge, overriding config' '
573 git config merge.conflictstyle diff3 &&
574 setup_conflicting_index &&
575 echo "none of the above" >sample &&
576 echo ourside >expect &&
580 git checkout --conflict=merge -- fild file filf &&
586 echo ">>>>>>> theirs"
588 test_cmp expect fild &&
589 test_cmp expect filf &&
593 test_expect_success
'checkout --conflict=diff3' '
594 git config --unset merge.conflictstyle
595 setup_conflicting_index &&
596 echo "none of the above" >sample &&
597 echo ourside >expect &&
601 git checkout --conflict=diff3 -- fild file filf &&
609 echo ">>>>>>> theirs"
611 test_cmp expect fild &&
612 test_cmp expect filf &&
616 test_expect_success
'failing checkout -b should not break working tree' '
617 git reset --hard master &&
618 git symbolic-ref HEAD refs/heads/master &&
619 test_must_fail git checkout -b renamer side^ &&
620 test $(git symbolic-ref HEAD) = refs/heads/master &&
621 git diff --exit-code &&
622 git diff --cached --exit-code
626 test_expect_success
'switch out of non-branch' '
627 git reset --hard master &&
628 git checkout master^0 &&
629 echo modified >one &&
630 test_must_fail git checkout renamer 2>error.log &&
631 ! grep "^Previous HEAD" error.log
640 echo '<<<<<<< filfre-theirs'
642 echo '||||||| filfre-common
'
646 echo '>>>>>>> filfre-ours
'
653 test_expect_success 'custom merge driver with checkout
-m' '
656 git config merge.filfre.driver
"./filfre.sh %O %A %B" &&
657 git config merge.filfre.name
"Feel-free merge driver" &&
658 git config merge.filfre.recursive binary
&&
659 echo "arm merge=filfre" >.gitattributes
&&
661 git checkout
-b left
&&
663 git add arm .gitattributes
&&
665 git commit
-m neutral
&&
670 git commit
-a -m left
&&
671 git checkout right
&&
675 git commit
-a -m right
&&
677 test_must_fail git merge left
&&
679 for t
in filfre-common left right
681 grep $t arm ||
exit 1
687 git checkout
-m arm
&&