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 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 &&
120 git diff --cached >current.index &&
121 test_cmp expect.index current.index
124 test_expect_success "checkout -m with dirty tree, renamed" '
126 git checkout -f master && git clean -f &&
128 fill 1 2 3 4 5 7 8 >one &&
129 if git checkout renamer
134 echo "happy - failed correctly"
137 git checkout -m renamer &&
138 fill 1 3 4 5 7 8 >expect &&
139 test_cmp expect uno &&
141 git diff --cached >current &&
146 test_expect_success 'checkout -m with merge conflict' '
148 git checkout -f master && git clean -f &&
150 fill 1 T 3 4 5 6 S 8 >one &&
151 if git checkout renamer
156 echo "happy - failed correctly"
159 git checkout -m renamer &&
161 git diff master:one :3:uno |
162 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
163 fill d2 aT d7 aS >expect &&
164 test_cmp current expect &&
165 git diff --cached two >current &&
169 test_expect_success 'format of merge conflict from checkout -m' '
171 git checkout -f master && git clean -f &&
174 git checkout -m simple &&
176 git ls-files >current &&
177 fill same two two two >expect &&
178 test_cmp current expect &&
180 cat <<-EOF >expect &&
193 test_expect_success 'checkout --merge --conflict=diff3 <branch>' '
195 git checkout -f master && git reset --hard && git clean -f &&
198 git checkout --merge --conflict=diff3 simple &&
200 cat <<-EOF >expect &&
219 test_expect_success 'switch to another branch while carrying a deletion' '
221 git checkout -f master && git reset --hard && git clean -f &&
224 test_must_fail git checkout simple 2>errs &&
225 test_i18ngrep overwritten errs &&
227 git checkout --merge simple 2>errs &&
228 test_i18ngrep ! overwritten errs &&
230 test_must_fail git cat-file -t :0:two &&
231 test "$(git cat-file -t :1:two)" = blob &&
232 test "$(git cat-file -t :2:two)" = blob &&
233 test_must_fail git cat-file -t :3:two
236 test_expect_success 'checkout to detach HEAD (with advice declined)' '
238 git config advice.detachedHead false &&
239 git checkout -f renamer && git clean -f &&
240 git checkout renamer^ 2>messages &&
241 test_i18ngrep "HEAD is now at 7329388" messages &&
242 test_line_count = 1 messages &&
243 H=$(git rev-parse --verify HEAD) &&
244 M=$(git show-ref -s --verify refs/heads/master) &&
245 test "z$H" = "z$M" &&
246 if git symbolic-ref HEAD >/dev/null 2>&1
248 echo "OOPS, HEAD is still symbolic???"
255 test_expect_success 'checkout to detach HEAD' '
256 git config advice.detachedHead true &&
257 git checkout -f renamer && git clean -f &&
258 git checkout renamer^ 2>messages &&
259 test_i18ngrep "HEAD is now at 7329388" messages &&
260 (test_line_count -gt 1 messages || test -n "$GETTEXT_POISON") &&
261 H=$(git rev-parse --verify HEAD) &&
262 M=$(git show-ref -s --verify refs/heads/master) &&
263 test "z$H" = "z$M" &&
264 if git symbolic-ref HEAD >/dev/null 2>&1
266 echo "OOPS, HEAD is still symbolic???"
273 test_expect_success 'checkout to detach HEAD with branchname^' '
275 git checkout -f master && git clean -f &&
276 git checkout renamer^ &&
277 H=$(git rev-parse --verify HEAD) &&
278 M=$(git show-ref -s --verify refs/heads/master) &&
279 test "z$H" = "z$M" &&
280 if git symbolic-ref HEAD >/dev/null 2>&1
282 echo "OOPS, HEAD is still symbolic???"
289 test_expect_success 'checkout to detach HEAD with :/message' '
291 git checkout -f master && git clean -f &&
292 git checkout ":/Initial" &&
293 H=$(git rev-parse --verify HEAD) &&
294 M=$(git show-ref -s --verify refs/heads/master) &&
295 test "z$H" = "z$M" &&
296 if git symbolic-ref HEAD >/dev/null 2>&1
298 echo "OOPS, HEAD is still symbolic???"
305 test_expect_success 'checkout to detach HEAD with HEAD^0' '
307 git checkout -f master && git clean -f &&
308 git checkout HEAD^0 &&
309 H=$(git rev-parse --verify HEAD) &&
310 M=$(git show-ref -s --verify refs/heads/master) &&
311 test "z$H" = "z$M" &&
312 if git symbolic-ref HEAD >/dev/null 2>&1
314 echo "OOPS, HEAD is still symbolic???"
321 test_expect_success 'checkout with ambiguous tag/branch names' '
324 git branch both master &&
326 git checkout master &&
329 H=$(git rev-parse --verify HEAD) &&
330 M=$(git show-ref -s --verify refs/heads/master) &&
331 test "z$H" = "z$M" &&
332 name=$(git symbolic-ref HEAD 2>/dev/null) &&
333 test "z$name" = zrefs/heads/both
337 test_expect_success 'checkout with ambiguous tag/branch names' '
340 git checkout master &&
342 git tag frotz side &&
343 git branch frotz master &&
345 git checkout master &&
347 git checkout tags/frotz &&
348 H=$(git rev-parse --verify HEAD) &&
349 S=$(git show-ref -s --verify refs/heads/side) &&
350 test "z$H" = "z$S" &&
351 if name=$(git symbolic-ref HEAD 2>/dev/null)
353 echo "Bad -- should have detached"
361 test_expect_success 'switch branches while in subdirectory' '
364 git checkout master &&
371 ! test -f subs/one &&
376 test_expect_success 'checkout specific path while in subdirectory' '
383 git commit -m "add subs/bero" &&
385 git checkout master &&
389 git checkout side -- bero
395 test_expect_success \
396 'checkout w/--track sets up tracking' '
397 git config branch.autosetupmerge false &&
398 git checkout master &&
399 git checkout --track -b track1 &&
400 test "$(git config branch.track1.remote)" &&
401 test "$(git config branch.track1.merge)"'
403 test_expect_success \
404 'checkout w/autosetupmerge=always sets up tracking' '
405 test_when_finished git config branch.autosetupmerge false &&
406 git config branch.autosetupmerge always &&
407 git checkout master &&
408 git checkout -b track2 &&
409 test "$(git config branch.track2.remote)" &&
410 test "$(git config branch.track2.merge)"'
412 test_expect_success 'checkout w/--track from non-branch HEAD fails' '
413 git checkout master^0 &&
414 test_must_fail git symbolic-ref HEAD &&
415 test_must_fail git checkout --track -b track &&
416 test_must_fail git rev-parse --verify track &&
417 test_must_fail git symbolic-ref HEAD &&
418 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
421 test_expect_success 'checkout w/--track from tag fails' '
422 git checkout master^0 &&
423 test_must_fail git symbolic-ref HEAD &&
424 test_must_fail git checkout --track -b track frotz &&
425 test_must_fail git rev-parse --verify track &&
426 test_must_fail git symbolic-ref HEAD &&
427 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
430 test_expect_success 'detach a symbolic link HEAD' '
431 git checkout master &&
432 git config --bool core.prefersymlinkrefs yes &&
434 git checkout master &&
435 it=$(git symbolic-ref HEAD) &&
436 test "z$it" = zrefs/heads/master &&
437 here=$(git rev-parse --verify refs/heads/master) &&
438 git checkout side^ &&
439 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
442 test_expect_success \
443 'checkout with --track fakes a sensible -b <name>' '
444 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
445 git update-ref refs/remotes/origin/koala/bear renamer &&
447 git checkout --track origin/koala/bear &&
448 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
449 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
451 git checkout master && git branch -D koala/bear &&
453 git checkout --track refs/remotes/origin/koala/bear &&
454 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
455 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
457 git checkout master && git branch -D koala/bear &&
459 git checkout --track remotes/origin/koala/bear &&
460 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
461 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
464 test_expect_success \
465 'checkout with --track, but without -b, fails with too short tracked name' '
466 test_must_fail git checkout --track renamer'
468 setup_conflicting_index () {
470 O=$(echo original | git hash-object -w --stdin) &&
471 A=$(echo ourside | git hash-object -w --stdin) &&
472 B=$(echo theirside | git hash-object -w --stdin) &&
474 echo "100644 $A 0 fild" &&
475 echo "100644 $O 1 file" &&
476 echo "100644 $A 2 file" &&
477 echo "100644 $B 3 file" &&
478 echo "100644 $A 0 filf"
479 ) | git update-index --index-info
482 test_expect_success 'checkout an unmerged path should fail' '
483 setup_conflicting_index &&
484 echo "none of the above" >sample &&
488 test_must_fail git checkout fild file filf &&
489 test_cmp sample fild &&
490 test_cmp sample filf &&
494 test_expect_success 'checkout with an unmerged path can be ignored' '
495 setup_conflicting_index &&
496 echo "none of the above" >sample &&
497 echo ourside >expect &&
501 git checkout -f fild file filf &&
502 test_cmp expect fild &&
503 test_cmp expect filf &&
507 test_expect_success 'checkout unmerged stage' '
508 setup_conflicting_index &&
509 echo "none of the above" >sample &&
510 echo ourside >expect &&
514 git checkout --ours . &&
515 test_cmp expect fild &&
516 test_cmp expect filf &&
517 test_cmp expect file &&
518 git checkout --theirs file &&
519 test ztheirside = "z$(cat file)"
522 test_expect_success 'checkout with --merge' '
523 setup_conflicting_index &&
524 echo "none of the above" >sample &&
525 echo ourside >expect &&
529 git checkout -m -- fild file filf &&
535 echo ">>>>>>> theirs"
537 test_cmp expect fild &&
538 test_cmp expect filf &&
542 test_expect_success 'checkout with --merge, in diff3 -m style' '
543 git config merge.conflictstyle diff3 &&
544 setup_conflicting_index &&
545 echo "none of the above" >sample &&
546 echo ourside >expect &&
550 git checkout -m -- fild file filf &&
558 echo ">>>>>>> theirs"
560 test_cmp expect fild &&
561 test_cmp expect filf &&
565 test_expect_success 'checkout --conflict=merge, overriding config' '
566 git config merge.conflictstyle diff3 &&
567 setup_conflicting_index &&
568 echo "none of the above" >sample &&
569 echo ourside >expect &&
573 git checkout --conflict=merge -- fild file filf &&
579 echo ">>>>>>> theirs"
581 test_cmp expect fild &&
582 test_cmp expect filf &&
586 test_expect_success 'checkout --conflict=diff3' '
587 test_unconfig merge.conflictstyle &&
588 setup_conflicting_index &&
589 echo "none of the above" >sample &&
590 echo ourside >expect &&
594 git checkout --conflict=diff3 -- fild file filf &&
602 echo ">>>>>>> theirs"
604 test_cmp expect fild &&
605 test_cmp expect filf &&
609 test_expect_success 'failing checkout -b should not break working tree' '
610 git reset --hard master &&
611 git symbolic-ref HEAD refs/heads/master &&
612 test_must_fail git checkout -b renamer side^ &&
613 test $(git symbolic-ref HEAD) = refs/heads/master &&
614 git diff --exit-code &&
615 git diff --cached --exit-code
619 test_expect_success 'switch out of non-branch' '
620 git reset --hard master &&
621 git checkout master^0 &&
622 echo modified >one &&
623 test_must_fail git checkout renamer 2>error.log &&
624 ! grep "^Previous HEAD" error.log
633 echo '<<<<<<< filfre-theirs'
635 echo '||||||| filfre-common'
639 echo '>>>>>>> filfre-ours'
646 test_expect_success 'custom merge driver with checkout -m' '
649 git config merge.filfre.driver "./filfre.sh %O %A %B" &&
650 git config merge.filfre.name "Feel-free merge driver" &&
651 git config merge.filfre.recursive binary &&
652 echo "arm merge=filfre" >.gitattributes &&
654 git checkout -b left &&
656 git add arm .gitattributes &&
658 git commit -m neutral &&
663 git commit -a -m left &&
664 git checkout right &&
668 git commit -a -m right &&
670 test_must_fail git merge left &&
672 for t in filfre-common left right
674 grep $t arm || exit 1
680 git checkout -m arm &&