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
'
37 fill 1 2 3 4 5 6 7 8 >one &&
38 fill a b c d e >two &&
39 git add same one two &&
40 git commit -m "Initial A one, A two" &&
42 git checkout -b renamer &&
44 fill 1 3 4 5 6 7 8 >uno &&
46 fill a b c d e f >two &&
47 git commit -a -m "Renamer R one->uno, M two" &&
49 git checkout -b side master &&
50 fill 1 2 3 4 5 6 7 >one &&
51 fill A B C D E >three &&
53 git update-index --add --remove one two three &&
54 git commit -m "Side M one, D two, A three" &&
56 git checkout -b simple master &&
59 git commit -a -m "Simple D one, M two" &&
64 test_expect_success
'checkout from non-existing branch' '
65 git checkout -b delete-me master &&
66 git update-ref -d --no-deref refs/heads/delete-me &&
67 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
68 git checkout master &&
69 test refs/heads/master = "$(git symbolic-ref HEAD)"
72 test_expect_success
'checkout with dirty tree without -m' '
73 fill 0 1 2 3 4 5 6 7 8 >one &&
79 echo "happy - failed correctly"
83 test_expect_success
'checkout with unrelated dirty tree without -m' '
84 git checkout -f master &&
85 fill 0 1 2 3 4 5 6 7 8 >same &&
87 git checkout side >messages &&
89 printf "M\t%s\n" same >messages.expect &&
90 test_cmp messages.expect messages
93 test_expect_success
'checkout -m with dirty tree' '
94 git checkout -f master &&
97 fill 0 1 2 3 4 5 6 7 8 >one &&
98 git checkout -m side >messages &&
100 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
102 printf "M\t%s\n" one >expect.messages &&
103 test_cmp expect.messages messages &&
105 fill "M one" "A three" "D two" >expect.master &&
106 git diff --name-status master >current.master &&
107 test_cmp expect.master current.master &&
109 fill "M one" >expect.side &&
110 git diff --name-status side >current.side &&
111 test_cmp expect.side current.side &&
113 git diff --cached >current.index &&
114 test_must_be_empty current.index
117 test_expect_success
'checkout -m with dirty tree, renamed' '
118 git checkout -f master && git clean -f &&
120 fill 1 2 3 4 5 7 8 >one &&
121 if git checkout renamer
126 echo "happy - failed correctly"
129 git checkout -m renamer &&
130 fill 1 3 4 5 7 8 >expect &&
131 test_cmp expect uno &&
133 git diff --cached >current &&
134 test_must_be_empty current
137 test_expect_success
'checkout -m with merge conflict' '
138 git checkout -f master && git clean -f &&
140 fill 1 T 3 4 5 6 S 8 >one &&
141 if git checkout renamer
146 echo "happy - failed correctly"
149 git checkout -m renamer &&
151 git diff master:one :3:uno |
152 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
153 fill d2 aT d7 aS >expect &&
154 test_cmp expect current &&
155 git diff --cached two >current &&
156 test_must_be_empty current
159 test_expect_success
'format of merge conflict from checkout -m' '
160 git checkout -f master &&
164 git checkout -m simple &&
166 git ls-files >current &&
167 fill same two two two >expect &&
168 test_cmp expect current &&
170 cat <<-EOF >expect &&
183 test_expect_success
'checkout --merge --conflict=diff3 <branch>' '
184 git checkout -f master &&
189 git checkout --merge --conflict=diff3 simple &&
191 cat <<-EOF >expect &&
210 test_expect_success
'switch to another branch while carrying a deletion' '
211 git checkout -f master &&
216 test_must_fail git checkout simple 2>errs &&
217 test_i18ngrep overwritten errs &&
219 test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs &&
220 test_must_be_empty errs
223 test_expect_success
'checkout to detach HEAD (with advice declined)' '
224 git config advice.detachedHead false &&
225 rev=$(git rev-parse --short renamer^) &&
226 git checkout -f renamer &&
228 git checkout renamer^ 2>messages &&
229 test_i18ngrep "HEAD is now at $rev" messages &&
230 test_line_count = 1 messages &&
231 H=$(git rev-parse --verify HEAD) &&
232 M=$(git show-ref -s --verify refs/heads/master) &&
233 test "z$H" = "z$M" &&
234 if git symbolic-ref HEAD >/dev/null 2>&1
236 echo "OOPS, HEAD is still symbolic???"
243 test_expect_success
'checkout to detach HEAD' '
244 git config advice.detachedHead true &&
245 rev=$(git rev-parse --short renamer^) &&
246 git checkout -f renamer &&
248 GIT_TEST_GETTEXT_POISON=false git checkout renamer^ 2>messages &&
249 grep "HEAD is now at $rev" messages &&
250 test_line_count -gt 1 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^' '
264 git checkout -f master &&
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' '
280 git checkout -f master &&
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' '
296 git checkout -f master &&
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' '
313 git branch both master &&
315 git checkout master &&
318 H=$(git rev-parse --verify HEAD) &&
319 M=$(git show-ref -s --verify refs/heads/master) &&
320 test "z$H" = "z$M" &&
321 name=$(git symbolic-ref HEAD 2>/dev/null) &&
322 test "z$name" = zrefs/heads/both
325 test_expect_success
'checkout with ambiguous tag/branch names' '
327 git checkout master &&
329 git tag frotz side &&
330 git branch frotz master &&
332 git checkout master &&
334 git checkout tags/frotz &&
335 H=$(git rev-parse --verify HEAD) &&
336 S=$(git show-ref -s --verify refs/heads/side) &&
337 test "z$H" = "z$S" &&
338 if name=$(git symbolic-ref HEAD 2>/dev/null)
340 echo "Bad -- should have detached"
347 test_expect_success
'switch branches while in subdirectory' '
349 git checkout master &&
352 git -C subs checkout side &&
353 ! test -f subs/one &&
357 test_expect_success
'checkout specific path while in subdirectory' '
363 git commit -m "add subs/bero" &&
365 git checkout master &&
367 git -C subs checkout side -- bero &&
371 test_expect_success
'checkout w/--track sets up tracking' '
372 git config branch.autosetupmerge false &&
373 git checkout master &&
374 git checkout --track -b track1 &&
375 test "$(git config branch.track1.remote)" &&
376 test "$(git config branch.track1.merge)"
379 test_expect_success
'checkout w/autosetupmerge=always sets up tracking' '
380 test_when_finished git config branch.autosetupmerge false &&
381 git config branch.autosetupmerge always &&
382 git checkout master &&
383 git checkout -b track2 &&
384 test "$(git config branch.track2.remote)" &&
385 test "$(git config branch.track2.merge)"
388 test_expect_success
'checkout w/--track from non-branch HEAD fails' '
389 git checkout master^0 &&
390 test_must_fail git symbolic-ref HEAD &&
391 test_must_fail git checkout --track -b track &&
392 test_must_fail git rev-parse --verify track &&
393 test_must_fail git symbolic-ref HEAD &&
394 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
397 test_expect_success
'checkout w/--track from tag fails' '
398 git checkout master^0 &&
399 test_must_fail git symbolic-ref HEAD &&
400 test_must_fail git checkout --track -b track frotz &&
401 test_must_fail git rev-parse --verify track &&
402 test_must_fail git symbolic-ref HEAD &&
403 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
406 test_expect_success
'detach a symbolic link HEAD' '
407 git checkout master &&
408 git config --bool core.prefersymlinkrefs yes &&
410 git checkout master &&
411 it=$(git symbolic-ref HEAD) &&
412 test "z$it" = zrefs/heads/master &&
413 here=$(git rev-parse --verify refs/heads/master) &&
414 git checkout side^ &&
415 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
418 test_expect_success
'checkout with --track fakes a sensible -b <name>' '
419 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
420 git update-ref refs/remotes/origin/koala/bear renamer &&
422 git checkout --track origin/koala/bear &&
423 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
424 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
426 git checkout master && git branch -D koala/bear &&
428 git checkout --track refs/remotes/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 remotes/origin/koala/bear &&
435 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
436 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
439 test_expect_success
'checkout with --track, but without -b, fails with too short tracked name' '
440 test_must_fail git checkout --track renamer
443 setup_conflicting_index
() {
445 O
=$
(echo original | git hash-object
-w --stdin) &&
446 A
=$
(echo ourside | git hash-object
-w --stdin) &&
447 B
=$
(echo theirside | git hash-object
-w --stdin) &&
449 echo "100644 $A 0 fild" &&
450 echo "100644 $O 1 file" &&
451 echo "100644 $A 2 file" &&
452 echo "100644 $B 3 file" &&
453 echo "100644 $A 0 filf"
454 ) | git update-index
--index-info
457 test_expect_success
'checkout an unmerged path should fail' '
458 setup_conflicting_index &&
459 echo "none of the above" >sample &&
463 test_must_fail git checkout fild file filf &&
464 test_cmp sample fild &&
465 test_cmp sample filf &&
469 test_expect_success
'checkout with an unmerged path can be ignored' '
470 setup_conflicting_index &&
471 echo "none of the above" >sample &&
472 echo ourside >expect &&
476 git checkout -f fild file filf &&
477 test_cmp expect fild &&
478 test_cmp expect filf &&
482 test_expect_success
'checkout unmerged stage' '
483 setup_conflicting_index &&
484 echo "none of the above" >sample &&
485 echo ourside >expect &&
489 git checkout --ours . &&
490 test_cmp expect fild &&
491 test_cmp expect filf &&
492 test_cmp expect file &&
493 git checkout --theirs file &&
494 test ztheirside = "z$(cat file)"
497 test_expect_success
'checkout with --merge' '
498 setup_conflicting_index &&
499 echo "none of the above" >sample &&
500 echo ourside >expect &&
504 git checkout -m -- fild file filf &&
506 echo "<<<<<<< ours" &&
510 echo ">>>>>>> theirs"
512 test_cmp expect fild &&
513 test_cmp expect filf &&
517 test_expect_success
'checkout with --merge, in diff3 -m style' '
518 git config merge.conflictstyle diff3 &&
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" &&
529 echo "||||||| base" &&
533 echo ">>>>>>> theirs"
535 test_cmp expect fild &&
536 test_cmp expect filf &&
540 test_expect_success
'checkout --conflict=merge, overriding config' '
541 git config merge.conflictstyle diff3 &&
542 setup_conflicting_index &&
543 echo "none of the above" >sample &&
544 echo ourside >expect &&
548 git checkout --conflict=merge -- fild file filf &&
550 echo "<<<<<<< ours" &&
554 echo ">>>>>>> theirs"
556 test_cmp expect fild &&
557 test_cmp expect filf &&
561 test_expect_success
'checkout --conflict=diff3' '
562 test_unconfig merge.conflictstyle &&
563 setup_conflicting_index &&
564 echo "none of the above" >sample &&
565 echo ourside >expect &&
569 git checkout --conflict=diff3 -- fild file filf &&
571 echo "<<<<<<< ours" &&
573 echo "||||||| base" &&
577 echo ">>>>>>> theirs"
579 test_cmp expect fild &&
580 test_cmp expect filf &&
584 test_expect_success
'failing checkout -b should not break working tree' '
585 git reset --hard master &&
586 git symbolic-ref HEAD refs/heads/master &&
587 test_must_fail git checkout -b renamer side^ &&
588 test $(git symbolic-ref HEAD) = refs/heads/master &&
589 git diff --exit-code &&
590 git diff --cached --exit-code
593 test_expect_success
'switch out of non-branch' '
594 git reset --hard master &&
595 git checkout master^0 &&
596 echo modified >one &&
597 test_must_fail git checkout renamer 2>error.log &&
598 ! grep "^Previous HEAD" error.log
607 echo '<<<<<<< filfre-theirs'
609 echo '||||||| filfre-common
'
613 echo '>>>>>>> filfre-ours
'
620 test_expect_success 'custom merge driver with checkout
-m' '
623 git config merge.filfre.driver
"./filfre.sh %O %A %B" &&
624 git config merge.filfre.name
"Feel-free merge driver" &&
625 git config merge.filfre.recursive binary
&&
626 echo "arm merge=filfre" >.gitattributes
&&
628 git checkout
-b left
&&
630 git add arm .gitattributes
&&
632 git commit
-m neutral
&&
637 git commit
-a -m left
&&
638 git checkout right
&&
642 git commit
-a -m right
&&
644 test_must_fail git merge left
&&
646 for t
in filfre-common left right
648 grep $t arm ||
exit 1
653 git checkout
-m arm
&&