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 test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs &&
227 test_must_be_empty errs
230 test_expect_success
'checkout to detach HEAD (with advice declined)' '
232 git config advice.detachedHead false &&
233 rev=$(git rev-parse --short renamer^) &&
234 git checkout -f renamer && git clean -f &&
235 git checkout renamer^ 2>messages &&
236 test_i18ngrep "HEAD is now at $rev" messages &&
237 test_line_count = 1 messages &&
238 H=$(git rev-parse --verify HEAD) &&
239 M=$(git show-ref -s --verify refs/heads/master) &&
240 test "z$H" = "z$M" &&
241 if git symbolic-ref HEAD >/dev/null 2>&1
243 echo "OOPS, HEAD is still symbolic???"
250 test_expect_success
'checkout to detach HEAD' '
251 git config advice.detachedHead true &&
252 rev=$(git rev-parse --short renamer^) &&
253 git checkout -f renamer && git clean -f &&
254 GIT_TEST_GETTEXT_POISON=false git checkout renamer^ 2>messages &&
255 grep "HEAD is now at $rev" messages &&
256 test_line_count -gt 1 messages &&
257 H=$(git rev-parse --verify HEAD) &&
258 M=$(git show-ref -s --verify refs/heads/master) &&
259 test "z$H" = "z$M" &&
260 if git symbolic-ref HEAD >/dev/null 2>&1
262 echo "OOPS, HEAD is still symbolic???"
269 test_expect_success
'checkout to detach HEAD with branchname^' '
271 git checkout -f master && git clean -f &&
272 git checkout renamer^ &&
273 H=$(git rev-parse --verify HEAD) &&
274 M=$(git show-ref -s --verify refs/heads/master) &&
275 test "z$H" = "z$M" &&
276 if git symbolic-ref HEAD >/dev/null 2>&1
278 echo "OOPS, HEAD is still symbolic???"
285 test_expect_success
'checkout to detach HEAD with :/message' '
287 git checkout -f master && git clean -f &&
288 git checkout ":/Initial" &&
289 H=$(git rev-parse --verify HEAD) &&
290 M=$(git show-ref -s --verify refs/heads/master) &&
291 test "z$H" = "z$M" &&
292 if git symbolic-ref HEAD >/dev/null 2>&1
294 echo "OOPS, HEAD is still symbolic???"
301 test_expect_success
'checkout to detach HEAD with HEAD^0' '
303 git checkout -f master && git clean -f &&
304 git checkout HEAD^0 &&
305 H=$(git rev-parse --verify HEAD) &&
306 M=$(git show-ref -s --verify refs/heads/master) &&
307 test "z$H" = "z$M" &&
308 if git symbolic-ref HEAD >/dev/null 2>&1
310 echo "OOPS, HEAD is still symbolic???"
317 test_expect_success
'checkout with ambiguous tag/branch names' '
320 git branch both master &&
322 git checkout master &&
325 H=$(git rev-parse --verify HEAD) &&
326 M=$(git show-ref -s --verify refs/heads/master) &&
327 test "z$H" = "z$M" &&
328 name=$(git symbolic-ref HEAD 2>/dev/null) &&
329 test "z$name" = zrefs/heads/both
333 test_expect_success
'checkout with ambiguous tag/branch names' '
336 git checkout master &&
338 git tag frotz side &&
339 git branch frotz master &&
341 git checkout master &&
343 git checkout tags/frotz &&
344 H=$(git rev-parse --verify HEAD) &&
345 S=$(git show-ref -s --verify refs/heads/side) &&
346 test "z$H" = "z$S" &&
347 if name=$(git symbolic-ref HEAD 2>/dev/null)
349 echo "Bad -- should have detached"
357 test_expect_success
'switch branches while in subdirectory' '
360 git checkout master &&
367 ! test -f subs/one &&
372 test_expect_success
'checkout specific path while in subdirectory' '
379 git commit -m "add subs/bero" &&
381 git checkout master &&
385 git checkout side -- bero
391 test_expect_success \
392 'checkout w/--track sets up tracking' '
393 git config branch.autosetupmerge false &&
394 git checkout master &&
395 git checkout --track -b track1 &&
396 test "$(git config branch.track1.remote)" &&
397 test "$(git config branch.track1.merge)"'
399 test_expect_success \
400 'checkout w/autosetupmerge=always sets up tracking' '
401 test_when_finished git config branch.autosetupmerge false &&
402 git config branch.autosetupmerge always &&
403 git checkout master &&
404 git checkout -b track2 &&
405 test "$(git config branch.track2.remote)" &&
406 test "$(git config branch.track2.merge)"'
408 test_expect_success
'checkout w/--track from non-branch HEAD fails' '
409 git checkout master^0 &&
410 test_must_fail git symbolic-ref HEAD &&
411 test_must_fail git checkout --track -b track &&
412 test_must_fail git rev-parse --verify track &&
413 test_must_fail git symbolic-ref HEAD &&
414 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
417 test_expect_success
'checkout w/--track from tag fails' '
418 git checkout master^0 &&
419 test_must_fail git symbolic-ref HEAD &&
420 test_must_fail git checkout --track -b track frotz &&
421 test_must_fail git rev-parse --verify track &&
422 test_must_fail git symbolic-ref HEAD &&
423 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
426 test_expect_success
'detach a symbolic link HEAD' '
427 git checkout master &&
428 git config --bool core.prefersymlinkrefs yes &&
430 git checkout master &&
431 it=$(git symbolic-ref HEAD) &&
432 test "z$it" = zrefs/heads/master &&
433 here=$(git rev-parse --verify refs/heads/master) &&
434 git checkout side^ &&
435 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
438 test_expect_success \
439 'checkout with --track fakes a sensible -b <name>' '
440 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
441 git update-ref refs/remotes/origin/koala/bear renamer &&
443 git checkout --track origin/koala/bear &&
444 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
445 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
447 git checkout master && git branch -D koala/bear &&
449 git checkout --track refs/remotes/origin/koala/bear &&
450 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
451 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
453 git checkout master && git branch -D koala/bear &&
455 git checkout --track remotes/origin/koala/bear &&
456 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
457 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
460 test_expect_success \
461 'checkout with --track, but without -b, fails with too short tracked name' '
462 test_must_fail git checkout --track renamer'
464 setup_conflicting_index
() {
466 O
=$
(echo original | git hash-object
-w --stdin) &&
467 A
=$
(echo ourside | git hash-object
-w --stdin) &&
468 B
=$
(echo theirside | git hash-object
-w --stdin) &&
470 echo "100644 $A 0 fild" &&
471 echo "100644 $O 1 file" &&
472 echo "100644 $A 2 file" &&
473 echo "100644 $B 3 file" &&
474 echo "100644 $A 0 filf"
475 ) | git update-index
--index-info
478 test_expect_success
'checkout an unmerged path should fail' '
479 setup_conflicting_index &&
480 echo "none of the above" >sample &&
484 test_must_fail git checkout fild file filf &&
485 test_cmp sample fild &&
486 test_cmp sample filf &&
490 test_expect_success
'checkout with an unmerged path can be ignored' '
491 setup_conflicting_index &&
492 echo "none of the above" >sample &&
493 echo ourside >expect &&
497 git checkout -f fild file filf &&
498 test_cmp expect fild &&
499 test_cmp expect filf &&
503 test_expect_success
'checkout unmerged stage' '
504 setup_conflicting_index &&
505 echo "none of the above" >sample &&
506 echo ourside >expect &&
510 git checkout --ours . &&
511 test_cmp expect fild &&
512 test_cmp expect filf &&
513 test_cmp expect file &&
514 git checkout --theirs file &&
515 test ztheirside = "z$(cat file)"
518 test_expect_success
'checkout with --merge' '
519 setup_conflicting_index &&
520 echo "none of the above" >sample &&
521 echo ourside >expect &&
525 git checkout -m -- fild file filf &&
527 echo "<<<<<<< ours" &&
531 echo ">>>>>>> theirs"
533 test_cmp expect fild &&
534 test_cmp expect filf &&
538 test_expect_success
'checkout with --merge, in diff3 -m style' '
539 git config merge.conflictstyle diff3 &&
540 setup_conflicting_index &&
541 echo "none of the above" >sample &&
542 echo ourside >expect &&
546 git checkout -m -- fild file filf &&
548 echo "<<<<<<< ours" &&
550 echo "||||||| base" &&
554 echo ">>>>>>> theirs"
556 test_cmp expect fild &&
557 test_cmp expect filf &&
561 test_expect_success
'checkout --conflict=merge, overriding config' '
562 git config merge.conflictstyle diff3 &&
563 setup_conflicting_index &&
564 echo "none of the above" >sample &&
565 echo ourside >expect &&
569 git checkout --conflict=merge -- fild file filf &&
571 echo "<<<<<<< ours" &&
575 echo ">>>>>>> theirs"
577 test_cmp expect fild &&
578 test_cmp expect filf &&
582 test_expect_success
'checkout --conflict=diff3' '
583 test_unconfig merge.conflictstyle &&
584 setup_conflicting_index &&
585 echo "none of the above" >sample &&
586 echo ourside >expect &&
590 git checkout --conflict=diff3 -- fild file filf &&
592 echo "<<<<<<< ours" &&
594 echo "||||||| base" &&
598 echo ">>>>>>> theirs"
600 test_cmp expect fild &&
601 test_cmp expect filf &&
605 test_expect_success
'failing checkout -b should not break working tree' '
606 git reset --hard master &&
607 git symbolic-ref HEAD refs/heads/master &&
608 test_must_fail git checkout -b renamer side^ &&
609 test $(git symbolic-ref HEAD) = refs/heads/master &&
610 git diff --exit-code &&
611 git diff --cached --exit-code
615 test_expect_success
'switch out of non-branch' '
616 git reset --hard master &&
617 git checkout master^0 &&
618 echo modified >one &&
619 test_must_fail git checkout renamer 2>error.log &&
620 ! grep "^Previous HEAD" error.log
629 echo '<<<<<<< filfre-theirs'
631 echo '||||||| filfre-common
'
635 echo '>>>>>>> filfre-ours
'
642 test_expect_success 'custom merge driver with checkout
-m' '
645 git config merge.filfre.driver
"./filfre.sh %O %A %B" &&
646 git config merge.filfre.name
"Feel-free merge driver" &&
647 git config merge.filfre.recursive binary
&&
648 echo "arm merge=filfre" >.gitattributes
&&
650 git checkout
-b left
&&
652 git add arm .gitattributes
&&
654 git commit
-m neutral
&&
659 git commit
-a -m left
&&
660 git checkout right
&&
664 git commit
-a -m right
&&
666 test_must_fail git merge left
&&
668 for t
in filfre-common left right
670 grep $t arm ||
exit 1
675 git checkout
-m arm
&&