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 'checkout to detach HEAD (with advice declined)' '
228 git config advice.detachedHead false &&
229 git checkout -f renamer && git clean -f &&
230 git checkout renamer^ 2>messages &&
231 grep "HEAD is now at 7329388" messages &&
232 test 1 -eq $(wc -l <messages) &&
233 H=$(git rev-parse --verify HEAD) &&
234 M=$(git show-ref -s --verify refs/heads/master) &&
235 test "z$H" = "z$M" &&
236 if git symbolic-ref HEAD >/dev/null 2>&1
238 echo "OOPS, HEAD is still symbolic???"
245 test_expect_success 'checkout to detach HEAD' '
246 git config advice.detachedHead true &&
247 git checkout -f renamer && git clean -f &&
248 git checkout renamer^ 2>messages &&
249 grep "HEAD is now at 7329388" messages &&
250 test 1 -lt $(wc -l <messages) &&
251 H=$(git rev-parse --verify HEAD) &&
252 M=$(git show-ref -s --verify refs/heads/master) &&
253 test "z$H" = "z$M" &&
254 if git symbolic-ref HEAD >/dev/null 2>&1
256 echo "OOPS, HEAD is still symbolic???"
263 test_expect_success 'checkout to detach HEAD with branchname^' '
265 git checkout -f master && git clean -f &&
266 git checkout renamer^ &&
267 H=$(git rev-parse --verify HEAD) &&
268 M=$(git show-ref -s --verify refs/heads/master) &&
269 test "z$H" = "z$M" &&
270 if git symbolic-ref HEAD >/dev/null 2>&1
272 echo "OOPS, HEAD is still symbolic???"
279 test_expect_success 'checkout to detach HEAD with :/message' '
281 git checkout -f master && git clean -f &&
282 git checkout ":/Initial" &&
283 H=$(git rev-parse --verify HEAD) &&
284 M=$(git show-ref -s --verify refs/heads/master) &&
285 test "z$H" = "z$M" &&
286 if git symbolic-ref HEAD >/dev/null 2>&1
288 echo "OOPS, HEAD is still symbolic???"
295 test_expect_success 'checkout to detach HEAD with HEAD^0' '
297 git checkout -f master && git clean -f &&
298 git checkout HEAD^0 &&
299 H=$(git rev-parse --verify HEAD) &&
300 M=$(git show-ref -s --verify refs/heads/master) &&
301 test "z$H" = "z$M" &&
302 if git symbolic-ref HEAD >/dev/null 2>&1
304 echo "OOPS, HEAD is still symbolic???"
311 test_expect_success 'checkout with ambiguous tag/branch names' '
314 git branch both master &&
316 git checkout master &&
319 H=$(git rev-parse --verify HEAD) &&
320 M=$(git show-ref -s --verify refs/heads/master) &&
321 test "z$H" = "z$M" &&
322 name=$(git symbolic-ref HEAD 2>/dev/null) &&
323 test "z$name" = zrefs/heads/both
327 test_expect_success 'checkout with ambiguous tag/branch names' '
330 git checkout master &&
332 git tag frotz side &&
333 git branch frotz master &&
335 git checkout master &&
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"
351 test_expect_success 'switch branches while in subdirectory' '
354 git checkout master &&
361 ! test -f subs/one &&
366 test_expect_success 'checkout specific path while in subdirectory' '
373 git commit -m "add subs/bero" &&
375 git checkout master &&
379 git checkout side -- bero
385 test_expect_success \
386 'checkout w/--track sets up tracking' '
387 git config branch.autosetupmerge false &&
388 git checkout master &&
389 git checkout --track -b track1 &&
390 test "$(git config branch.track1.remote)" &&
391 test "$(git config branch.track1.merge)"'
393 test_expect_success \
394 'checkout w/autosetupmerge=always sets up tracking' '
395 git config branch.autosetupmerge always &&
396 git checkout master &&
397 git checkout -b track2 &&
398 test "$(git config branch.track2.remote)" &&
399 test "$(git config branch.track2.merge)"
400 git config branch.autosetupmerge false'
402 test_expect_success 'checkout w/--track from non-branch HEAD fails' '
403 git checkout master^0 &&
404 test_must_fail git symbolic-ref HEAD &&
405 test_must_fail git checkout --track -b track &&
406 test_must_fail git rev-parse --verify track &&
407 test_must_fail git symbolic-ref HEAD &&
408 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
411 test_expect_success 'detach a symbolic link HEAD' '
412 git checkout master &&
413 git config --bool core.prefersymlinkrefs yes &&
415 git checkout master &&
416 it=$(git symbolic-ref HEAD) &&
417 test "z$it" = zrefs/heads/master &&
418 here=$(git rev-parse --verify refs/heads/master) &&
419 git checkout side^ &&
420 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
423 test_expect_success \
424 'checkout with --track fakes a sensible -b <name>' '
425 git update-ref refs/remotes/origin/koala/bear renamer &&
426 git update-ref refs/new/koala/bear renamer &&
428 git checkout --track origin/koala/bear &&
429 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
430 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
432 git checkout master && git branch -D koala/bear &&
434 git checkout --track refs/remotes/origin/koala/bear &&
435 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
436 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
438 git checkout master && git branch -D koala/bear &&
440 git checkout --track remotes/origin/koala/bear &&
441 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
442 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
444 git checkout master && git branch -D koala/bear &&
446 git checkout --track refs/new/koala/bear &&
447 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
448 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
451 test_expect_success \
452 'checkout with --track, but without -b, fails with too short tracked name' '
453 test_must_fail git checkout --track renamer'
455 setup_conflicting_index () {
457 O=$(echo original | git hash-object -w --stdin) &&
458 A=$(echo ourside | git hash-object -w --stdin) &&
459 B=$(echo theirside | git hash-object -w --stdin) &&
461 echo "100644 $A 0 fild" &&
462 echo "100644 $O 1 file" &&
463 echo "100644 $A 2 file" &&
464 echo "100644 $B 3 file" &&
465 echo "100644 $A 0 filf"
466 ) | git update-index --index-info
469 test_expect_success 'checkout an unmerged path should fail' '
470 setup_conflicting_index &&
471 echo "none of the above" >sample &&
475 test_must_fail git checkout fild file filf &&
476 test_cmp sample fild &&
477 test_cmp sample filf &&
481 test_expect_success 'checkout with an unmerged path can be ignored' '
482 setup_conflicting_index &&
483 echo "none of the above" >sample &&
484 echo ourside >expect &&
488 git checkout -f fild file filf &&
489 test_cmp expect fild &&
490 test_cmp expect filf &&
494 test_expect_success 'checkout unmerged stage' '
495 setup_conflicting_index &&
496 echo "none of the above" >sample &&
497 echo ourside >expect &&
501 git checkout --ours . &&
502 test_cmp expect fild &&
503 test_cmp expect filf &&
504 test_cmp expect file &&
505 git checkout --theirs file &&
506 test ztheirside = "z$(cat file)"
509 test_expect_success 'checkout with --merge' '
510 setup_conflicting_index &&
511 echo "none of the above" >sample &&
512 echo ourside >expect &&
516 git checkout -m -- fild file filf &&
522 echo ">>>>>>> theirs"
524 test_cmp expect fild &&
525 test_cmp expect filf &&
529 test_expect_success 'checkout with --merge, in diff3 -m style' '
530 git config merge.conflictstyle diff3 &&
531 setup_conflicting_index &&
532 echo "none of the above" >sample &&
533 echo ourside >expect &&
537 git checkout -m -- fild file filf &&
545 echo ">>>>>>> theirs"
547 test_cmp expect fild &&
548 test_cmp expect filf &&
552 test_expect_success 'checkout --conflict=merge, overriding config' '
553 git config merge.conflictstyle diff3 &&
554 setup_conflicting_index &&
555 echo "none of the above" >sample &&
556 echo ourside >expect &&
560 git checkout --conflict=merge -- fild file filf &&
566 echo ">>>>>>> theirs"
568 test_cmp expect fild &&
569 test_cmp expect filf &&
573 test_expect_success 'checkout --conflict=diff3' '
574 git config --unset merge.conflictstyle
575 setup_conflicting_index &&
576 echo "none of the above" >sample &&
577 echo ourside >expect &&
581 git checkout --conflict=diff3 -- fild file filf &&
589 echo ">>>>>>> theirs"
591 test_cmp expect fild &&
592 test_cmp expect filf &&
596 test_expect_success 'failing checkout -b should not break working tree' '
597 git reset --hard master &&
598 git symbolic-ref HEAD refs/heads/master &&
599 test_must_fail git checkout -b renamer side^ &&
600 test $(git symbolic-ref HEAD) = refs/heads/master &&
601 git diff --exit-code &&
602 git diff --cached --exit-code
606 test_expect_success 'switch out of non-branch' '
607 git reset --hard master &&
608 git checkout master^0 &&
609 echo modified >one &&
610 test_must_fail git checkout renamer 2>error.log &&
611 ! grep "^Previous HEAD" error.log
620 echo '<<<<<<< filfre-theirs'
622 echo '||||||| filfre-common'
626 echo '>>>>>>> filfre-ours'
633 test_expect_success 'custom merge driver with checkout -m' '
636 git config merge.filfre.driver "./filfre.sh %O %A %B" &&
637 git config merge.filfre.name "Feel-free merge driver" &&
638 git config merge.filfre.recursive binary &&
639 echo "arm merge=filfre" >.gitattributes &&
641 git checkout -b left &&
643 git add arm .gitattributes &&
645 git commit -m neutral &&
650 git commit -a -m left &&
651 git checkout right &&
655 git commit -a -m right &&
657 test_must_fail git merge left &&
659 for t in filfre-common left right
661 grep $t arm || exit 1
667 git checkout -m arm &&