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
15 + [side] Side M one, D two, A three
16 * [renamer] Renamer R one->uno, M two
17 +*+ [master] Initial A one, A two
33 test_expect_success setup '
36 fill 1 2 3 4 5 6 7 8 >one &&
37 fill a b c d e >two &&
38 git add same one two &&
39 git commit -m "Initial A one, A two" &&
41 git checkout -b renamer &&
43 fill 1 3 4 5 6 7 8 >uno &&
45 fill a b c d e f >two &&
46 git commit -a -m "Renamer R one->uno, M two" &&
48 git checkout -b side master &&
49 fill 1 2 3 4 5 6 7 >one &&
50 fill A B C D E >three &&
52 git update-index --add --remove one two three &&
53 git commit -m "Side M one, D two, A three" &&
58 test_expect_success "checkout from non-existing branch" '
60 git checkout -b delete-me master &&
61 rm .git/refs/heads/delete-me &&
62 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
63 git checkout master &&
64 test refs/heads/master = "$(git symbolic-ref HEAD)"
67 test_expect_success "checkout with dirty tree without -m" '
69 fill 0 1 2 3 4 5 6 7 8 >one &&
75 echo "happy - failed correctly"
80 test_expect_success "checkout with unrelated dirty tree without -m" '
82 git checkout -f master &&
83 fill 0 1 2 3 4 5 6 7 8 >same &&
85 git checkout side >messages &&
87 (cat > messages.expect <<EOF
91 touch messages.expect &&
92 test_cmp messages.expect messages
95 test_expect_success "checkout -m with dirty tree" '
97 git checkout -f master &&
100 fill 0 1 2 3 4 5 6 7 8 >one &&
101 git checkout -m side > messages &&
103 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
105 (cat >expect.messages <<EOF
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 'checkout to detach HEAD (with advice declined)' '
171 git config advice.detachedHead false &&
172 git checkout -f renamer && git clean -f &&
173 git checkout renamer^ 2>messages &&
174 grep "HEAD is now at 7329388" messages &&
175 test 1 -eq $(wc -l <messages) &&
176 H=$(git rev-parse --verify HEAD) &&
177 M=$(git show-ref -s --verify refs/heads/master) &&
178 test "z$H" = "z$M" &&
179 if git symbolic-ref HEAD >/dev/null 2>&1
181 echo "OOPS, HEAD is still symbolic???"
188 test_expect_success 'checkout to detach HEAD' '
189 git config advice.detachedHead true &&
190 git checkout -f renamer && git clean -f &&
191 git checkout renamer^ 2>messages &&
192 grep "HEAD is now at 7329388" messages &&
193 test 1 -lt $(wc -l <messages) &&
194 H=$(git rev-parse --verify HEAD) &&
195 M=$(git show-ref -s --verify refs/heads/master) &&
196 test "z$H" = "z$M" &&
197 if git symbolic-ref HEAD >/dev/null 2>&1
199 echo "OOPS, HEAD is still symbolic???"
206 test_expect_success 'checkout to detach HEAD with branchname^' '
208 git checkout -f master && git clean -f &&
209 git checkout renamer^ &&
210 H=$(git rev-parse --verify HEAD) &&
211 M=$(git show-ref -s --verify refs/heads/master) &&
212 test "z$H" = "z$M" &&
213 if git symbolic-ref HEAD >/dev/null 2>&1
215 echo "OOPS, HEAD is still symbolic???"
222 test_expect_success 'checkout to detach HEAD with :/message' '
224 git checkout -f master && git clean -f &&
225 git checkout ":/Initial" &&
226 H=$(git rev-parse --verify HEAD) &&
227 M=$(git show-ref -s --verify refs/heads/master) &&
228 test "z$H" = "z$M" &&
229 if git symbolic-ref HEAD >/dev/null 2>&1
231 echo "OOPS, HEAD is still symbolic???"
238 test_expect_success 'checkout to detach HEAD with HEAD^0' '
240 git checkout -f master && git clean -f &&
241 git checkout HEAD^0 &&
242 H=$(git rev-parse --verify HEAD) &&
243 M=$(git show-ref -s --verify refs/heads/master) &&
244 test "z$H" = "z$M" &&
245 if git symbolic-ref HEAD >/dev/null 2>&1
247 echo "OOPS, HEAD is still symbolic???"
254 test_expect_success 'checkout with ambiguous tag/branch names' '
257 git branch both master &&
259 git checkout master &&
262 H=$(git rev-parse --verify HEAD) &&
263 M=$(git show-ref -s --verify refs/heads/master) &&
264 test "z$H" = "z$M" &&
265 name=$(git symbolic-ref HEAD 2>/dev/null) &&
266 test "z$name" = zrefs/heads/both
270 test_expect_success 'checkout with ambiguous tag/branch names' '
273 git checkout master &&
275 git tag frotz side &&
276 git branch frotz master &&
278 git checkout master &&
280 git checkout tags/frotz &&
281 H=$(git rev-parse --verify HEAD) &&
282 S=$(git show-ref -s --verify refs/heads/side) &&
283 test "z$H" = "z$S" &&
284 if name=$(git symbolic-ref HEAD 2>/dev/null)
286 echo "Bad -- should have detached"
294 test_expect_success 'switch branches while in subdirectory' '
297 git checkout master &&
304 ! test -f subs/one &&
309 test_expect_success 'checkout specific path while in subdirectory' '
316 git commit -m "add subs/bero" &&
318 git checkout master &&
322 git checkout side -- bero
328 test_expect_success \
329 'checkout w/--track sets up tracking' '
330 git config branch.autosetupmerge false &&
331 git checkout master &&
332 git checkout --track -b track1 &&
333 test "$(git config branch.track1.remote)" &&
334 test "$(git config branch.track1.merge)"'
336 test_expect_success \
337 'checkout w/autosetupmerge=always sets up tracking' '
338 git config branch.autosetupmerge always &&
339 git checkout master &&
340 git checkout -b track2 &&
341 test "$(git config branch.track2.remote)" &&
342 test "$(git config branch.track2.merge)"
343 git config branch.autosetupmerge false'
345 test_expect_success 'checkout w/--track from non-branch HEAD fails' '
346 git checkout master^0 &&
347 test_must_fail git symbolic-ref HEAD &&
348 test_must_fail git checkout --track -b track &&
349 test_must_fail git rev-parse --verify track &&
350 test_must_fail git symbolic-ref HEAD &&
351 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
354 test_expect_success 'detach a symbolic link HEAD' '
355 git checkout master &&
356 git config --bool core.prefersymlinkrefs yes &&
358 git checkout master &&
359 it=$(git symbolic-ref HEAD) &&
360 test "z$it" = zrefs/heads/master &&
361 here=$(git rev-parse --verify refs/heads/master) &&
362 git checkout side^ &&
363 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
366 test_expect_success \
367 'checkout with --track fakes a sensible -b <name>' '
368 git update-ref refs/remotes/origin/koala/bear renamer &&
369 git update-ref refs/new/koala/bear renamer &&
371 git checkout --track origin/koala/bear &&
372 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
373 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
375 git checkout master && git branch -D koala/bear &&
377 git checkout --track refs/remotes/origin/koala/bear &&
378 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
379 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
381 git checkout master && git branch -D koala/bear &&
383 git checkout --track remotes/origin/koala/bear &&
384 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
385 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
387 git checkout master && git branch -D koala/bear &&
389 git checkout --track refs/new/koala/bear &&
390 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
391 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
394 test_expect_success \
395 'checkout with --track, but without -b, fails with too short tracked name' '
396 test_must_fail git checkout --track renamer'
398 setup_conflicting_index () {
400 O=$(echo original | git hash-object -w --stdin) &&
401 A=$(echo ourside | git hash-object -w --stdin) &&
402 B=$(echo theirside | git hash-object -w --stdin) &&
404 echo "100644 $A 0 fild" &&
405 echo "100644 $O 1 file" &&
406 echo "100644 $A 2 file" &&
407 echo "100644 $B 3 file" &&
408 echo "100644 $A 0 filf"
409 ) | git update-index --index-info
412 test_expect_success 'checkout an unmerged path should fail' '
413 setup_conflicting_index &&
414 echo "none of the above" >sample &&
418 test_must_fail git checkout fild file filf &&
419 test_cmp sample fild &&
420 test_cmp sample filf &&
424 test_expect_success 'checkout with an unmerged path can be ignored' '
425 setup_conflicting_index &&
426 echo "none of the above" >sample &&
427 echo ourside >expect &&
431 git checkout -f fild file filf &&
432 test_cmp expect fild &&
433 test_cmp expect filf &&
437 test_expect_success 'checkout unmerged stage' '
438 setup_conflicting_index &&
439 echo "none of the above" >sample &&
440 echo ourside >expect &&
444 git checkout --ours . &&
445 test_cmp expect fild &&
446 test_cmp expect filf &&
447 test_cmp expect file &&
448 git checkout --theirs file &&
449 test ztheirside = "z$(cat file)"
452 test_expect_success 'checkout with --merge' '
453 setup_conflicting_index &&
454 echo "none of the above" >sample &&
455 echo ourside >expect &&
459 git checkout -m -- fild file filf &&
465 echo ">>>>>>> theirs"
467 test_cmp expect fild &&
468 test_cmp expect filf &&
472 test_expect_success 'checkout with --merge, in diff3 -m style' '
473 git config merge.conflictstyle diff3 &&
474 setup_conflicting_index &&
475 echo "none of the above" >sample &&
476 echo ourside >expect &&
480 git checkout -m -- fild file filf &&
488 echo ">>>>>>> theirs"
490 test_cmp expect fild &&
491 test_cmp expect filf &&
495 test_expect_success 'checkout --conflict=merge, overriding config' '
496 git config merge.conflictstyle diff3 &&
497 setup_conflicting_index &&
498 echo "none of the above" >sample &&
499 echo ourside >expect &&
503 git checkout --conflict=merge -- fild file filf &&
509 echo ">>>>>>> theirs"
511 test_cmp expect fild &&
512 test_cmp expect filf &&
516 test_expect_success 'checkout --conflict=diff3' '
517 git config --unset merge.conflictstyle
518 setup_conflicting_index &&
519 echo "none of the above" >sample &&
520 echo ourside >expect &&
524 git checkout --conflict=diff3 -- fild file filf &&
532 echo ">>>>>>> theirs"
534 test_cmp expect fild &&
535 test_cmp expect filf &&
539 test_expect_success 'failing checkout -b should not break working tree' '
540 git reset --hard master &&
541 git symbolic-ref HEAD refs/heads/master &&
542 test_must_fail git checkout -b renamer side^ &&
543 test $(git symbolic-ref HEAD) = refs/heads/master &&
544 git diff --exit-code &&
545 git diff --cached --exit-code
549 test_expect_success 'switch out of non-branch' '
550 git reset --hard master &&
551 git checkout master^0 &&
552 echo modified >one &&
553 test_must_fail git checkout renamer 2>error.log &&
554 ! grep "^Previous HEAD" error.log
563 echo '<<<<<<< filfre-theirs'
565 echo '||||||| filfre-common'
569 echo '>>>>>>> filfre-ours'
576 test_expect_success 'custom merge driver with checkout -m' '
579 git config merge.filfre.driver "./filfre.sh %O %A %B" &&
580 git config merge.filfre.name "Feel-free merge driver" &&
581 git config merge.filfre.recursive binary &&
582 echo "arm merge=filfre" >.gitattributes &&
584 git checkout -b left &&
586 git add arm .gitattributes &&
588 git commit -m neutral &&
593 git commit -a -m left &&
594 git checkout right &&
598 git commit -a -m right &&
600 test_must_fail git merge left &&
602 for t in filfre-common left right
604 grep $t arm || exit 1
610 git checkout -m arm &&