1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
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 ---
15 + [side] Side M one, D two, A three
16 * [renamer] Renamer R one->uno, M two
17 +*+ [master] Initial A one, A two
19 '
21 . ./test-lib.sh
23 test_tick
25 fill () {
26 for i
27 do
28 echo "$i"
29 done
30 }
33 test_expect_success setup '
35 fill x y z > same &&
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 &&
42 rm -f one &&
43 fill 1 3 4 5 6 7 8 >uno &&
44 git add 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 &&
51 rm -f two &&
52 git update-index --add --remove one two three &&
53 git commit -m "Side M one, D two, A three" &&
55 git checkout master
56 '
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)"
65 '
67 test_expect_success "checkout with dirty tree without -m" '
69 fill 0 1 2 3 4 5 6 7 8 >one &&
70 if git checkout side
71 then
72 echo Not happy
73 false
74 else
75 echo "happy - failed correctly"
76 fi
78 '
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 &&
84 cp same kept
85 git checkout side >messages &&
86 test_cmp same kept
87 (cat > messages.expect <<EOF
88 M same
89 EOF
90 ) &&
91 touch messages.expect &&
92 test_cmp messages.expect messages
93 '
95 test_expect_success "checkout -m with dirty tree" '
97 git checkout -f master &&
98 git clean -f &&
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
106 M one
107 EOF
108 ) &&
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 : >expect.index &&
120 git diff --cached >current.index &&
121 test_cmp expect.index current.index
122 '
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
130 then
131 echo Not happy
132 false
133 else
134 echo "happy - failed correctly"
135 fi &&
137 git checkout -m renamer &&
138 fill 1 3 4 5 7 8 >expect &&
139 test_cmp expect uno &&
140 ! test -f one &&
141 git diff --cached >current &&
142 ! test -s current
144 '
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
152 then
153 echo Not happy
154 false
155 else
156 echo "happy - failed correctly"
157 fi &&
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 &&
166 ! test -s current
167 '
169 test_expect_success 'checkout to detach HEAD' '
171 git checkout -f renamer && git clean -f &&
172 git checkout renamer^ 2>messages &&
173 (cat >messages.expect <<EOF
174 Note: moving to '\''renamer^'\'' which isn'\''t a local branch
175 If you want to create a new branch from this checkout, you may do so
176 (now or later) by using -b with the checkout command again. Example:
177 git checkout -b <new_branch_name>
178 HEAD is now at 7329388... Initial A one, A two
179 EOF
180 ) &&
181 test_cmp messages.expect messages &&
182 H=$(git rev-parse --verify HEAD) &&
183 M=$(git show-ref -s --verify refs/heads/master) &&
184 test "z$H" = "z$M" &&
185 if git symbolic-ref HEAD >/dev/null 2>&1
186 then
187 echo "OOPS, HEAD is still symbolic???"
188 false
189 else
190 : happy
191 fi
192 '
194 test_expect_success 'checkout to detach HEAD with branchname^' '
196 git checkout -f master && git clean -f &&
197 git checkout renamer^ &&
198 H=$(git rev-parse --verify HEAD) &&
199 M=$(git show-ref -s --verify refs/heads/master) &&
200 test "z$H" = "z$M" &&
201 if git symbolic-ref HEAD >/dev/null 2>&1
202 then
203 echo "OOPS, HEAD is still symbolic???"
204 false
205 else
206 : happy
207 fi
208 '
210 test_expect_success 'checkout to detach HEAD with :/message' '
212 git checkout -f master && git clean -f &&
213 git checkout ":/Initial" &&
214 H=$(git rev-parse --verify HEAD) &&
215 M=$(git show-ref -s --verify refs/heads/master) &&
216 test "z$H" = "z$M" &&
217 if git symbolic-ref HEAD >/dev/null 2>&1
218 then
219 echo "OOPS, HEAD is still symbolic???"
220 false
221 else
222 : happy
223 fi
224 '
226 test_expect_success 'checkout to detach HEAD with HEAD^0' '
228 git checkout -f master && git clean -f &&
229 git checkout HEAD^0 &&
230 H=$(git rev-parse --verify HEAD) &&
231 M=$(git show-ref -s --verify refs/heads/master) &&
232 test "z$H" = "z$M" &&
233 if git symbolic-ref HEAD >/dev/null 2>&1
234 then
235 echo "OOPS, HEAD is still symbolic???"
236 false
237 else
238 : happy
239 fi
240 '
242 test_expect_success 'checkout with ambiguous tag/branch names' '
244 git tag both side &&
245 git branch both master &&
246 git reset --hard &&
247 git checkout master &&
249 git checkout both &&
250 H=$(git rev-parse --verify HEAD) &&
251 M=$(git show-ref -s --verify refs/heads/master) &&
252 test "z$H" = "z$M" &&
253 name=$(git symbolic-ref HEAD 2>/dev/null) &&
254 test "z$name" = zrefs/heads/both
256 '
258 test_expect_success 'checkout with ambiguous tag/branch names' '
260 git reset --hard &&
261 git checkout master &&
263 git tag frotz side &&
264 git branch frotz master &&
265 git reset --hard &&
266 git checkout master &&
268 git checkout tags/frotz &&
269 H=$(git rev-parse --verify HEAD) &&
270 S=$(git show-ref -s --verify refs/heads/side) &&
271 test "z$H" = "z$S" &&
272 if name=$(git symbolic-ref HEAD 2>/dev/null)
273 then
274 echo "Bad -- should have detached"
275 false
276 else
277 : happy
278 fi
280 '
282 test_expect_success 'switch branches while in subdirectory' '
284 git reset --hard &&
285 git checkout master &&
287 mkdir subs &&
288 (
289 cd subs &&
290 git checkout side
291 ) &&
292 ! test -f subs/one &&
293 rm -fr subs
295 '
297 test_expect_success 'checkout specific path while in subdirectory' '
299 git reset --hard &&
300 git checkout side &&
301 mkdir subs &&
302 >subs/bero &&
303 git add subs/bero &&
304 git commit -m "add subs/bero" &&
306 git checkout master &&
307 mkdir -p subs &&
308 (
309 cd subs &&
310 git checkout side -- bero
311 ) &&
312 test -f subs/bero
314 '
316 test_expect_success \
317 'checkout w/--track sets up tracking' '
318 git config branch.autosetupmerge false &&
319 git checkout master &&
320 git checkout --track -b track1 &&
321 test "$(git config branch.track1.remote)" &&
322 test "$(git config branch.track1.merge)"'
324 test_expect_success \
325 'checkout w/autosetupmerge=always sets up tracking' '
326 git config branch.autosetupmerge always &&
327 git checkout master &&
328 git checkout -b track2 &&
329 test "$(git config branch.track2.remote)" &&
330 test "$(git config branch.track2.merge)"
331 git config branch.autosetupmerge false'
333 test_expect_success 'checkout w/--track from non-branch HEAD fails' '
334 git checkout master^0 &&
335 test_must_fail git symbolic-ref HEAD &&
336 test_must_fail git checkout --track -b track &&
337 test_must_fail git rev-parse --verify track &&
338 test_must_fail git symbolic-ref HEAD &&
339 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
340 '
342 test_expect_success 'detach a symbolic link HEAD' '
343 git checkout master &&
344 git config --bool core.prefersymlinkrefs yes &&
345 git checkout side &&
346 git checkout master &&
347 it=$(git symbolic-ref HEAD) &&
348 test "z$it" = zrefs/heads/master &&
349 here=$(git rev-parse --verify refs/heads/master) &&
350 git checkout side^ &&
351 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
352 '
354 test_expect_success \
355 'checkout with --track fakes a sensible -b <name>' '
356 git update-ref refs/remotes/origin/koala/bear renamer &&
357 git update-ref refs/new/koala/bear renamer &&
359 git checkout --track origin/koala/bear &&
360 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
361 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
363 git checkout master && git branch -D koala/bear &&
365 git checkout --track refs/remotes/origin/koala/bear &&
366 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
367 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
369 git checkout master && git branch -D koala/bear &&
371 git checkout --track remotes/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/new/koala/bear &&
378 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
379 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
380 '
382 test_expect_success \
383 'checkout with --track, but without -b, fails with too short tracked name' '
384 test_must_fail git checkout --track renamer'
386 setup_conflicting_index () {
387 rm -f .git/index &&
388 O=$(echo original | git hash-object -w --stdin) &&
389 A=$(echo ourside | git hash-object -w --stdin) &&
390 B=$(echo theirside | git hash-object -w --stdin) &&
391 (
392 echo "100644 $A 0 fild" &&
393 echo "100644 $O 1 file" &&
394 echo "100644 $A 2 file" &&
395 echo "100644 $B 3 file" &&
396 echo "100644 $A 0 filf"
397 ) | git update-index --index-info
398 }
400 test_expect_success 'checkout an unmerged path should fail' '
401 setup_conflicting_index &&
402 echo "none of the above" >sample &&
403 cat sample >fild &&
404 cat sample >file &&
405 cat sample >filf &&
406 test_must_fail git checkout fild file filf &&
407 test_cmp sample fild &&
408 test_cmp sample filf &&
409 test_cmp sample file
410 '
412 test_expect_success 'checkout with an unmerged path can be ignored' '
413 setup_conflicting_index &&
414 echo "none of the above" >sample &&
415 echo ourside >expect &&
416 cat sample >fild &&
417 cat sample >file &&
418 cat sample >filf &&
419 git checkout -f fild file filf &&
420 test_cmp expect fild &&
421 test_cmp expect filf &&
422 test_cmp sample file
423 '
425 test_expect_success 'checkout unmerged stage' '
426 setup_conflicting_index &&
427 echo "none of the above" >sample &&
428 echo ourside >expect &&
429 cat sample >fild &&
430 cat sample >file &&
431 cat sample >filf &&
432 git checkout --ours . &&
433 test_cmp expect fild &&
434 test_cmp expect filf &&
435 test_cmp expect file &&
436 git checkout --theirs file &&
437 test ztheirside = "z$(cat file)"
438 '
440 test_expect_success 'checkout with --merge' '
441 setup_conflicting_index &&
442 echo "none of the above" >sample &&
443 echo ourside >expect &&
444 cat sample >fild &&
445 cat sample >file &&
446 cat sample >filf &&
447 git checkout -m -- fild file filf &&
448 (
449 echo "<<<<<<< ours"
450 echo ourside
451 echo "======="
452 echo theirside
453 echo ">>>>>>> theirs"
454 ) >merged &&
455 test_cmp expect fild &&
456 test_cmp expect filf &&
457 test_cmp merged file
458 '
460 test_expect_success 'checkout with --merge, in diff3 -m style' '
461 git config merge.conflictstyle diff3 &&
462 setup_conflicting_index &&
463 echo "none of the above" >sample &&
464 echo ourside >expect &&
465 cat sample >fild &&
466 cat sample >file &&
467 cat sample >filf &&
468 git checkout -m -- fild file filf &&
469 (
470 echo "<<<<<<< ours"
471 echo ourside
472 echo "|||||||"
473 echo original
474 echo "======="
475 echo theirside
476 echo ">>>>>>> theirs"
477 ) >merged &&
478 test_cmp expect fild &&
479 test_cmp expect filf &&
480 test_cmp merged file
481 '
483 test_expect_success 'checkout --conflict=merge, overriding config' '
484 git config merge.conflictstyle diff3 &&
485 setup_conflicting_index &&
486 echo "none of the above" >sample &&
487 echo ourside >expect &&
488 cat sample >fild &&
489 cat sample >file &&
490 cat sample >filf &&
491 git checkout --conflict=merge -- fild file filf &&
492 (
493 echo "<<<<<<< ours"
494 echo ourside
495 echo "======="
496 echo theirside
497 echo ">>>>>>> theirs"
498 ) >merged &&
499 test_cmp expect fild &&
500 test_cmp expect filf &&
501 test_cmp merged file
502 '
504 test_expect_success 'checkout --conflict=diff3' '
505 git config --unset merge.conflictstyle
506 setup_conflicting_index &&
507 echo "none of the above" >sample &&
508 echo ourside >expect &&
509 cat sample >fild &&
510 cat sample >file &&
511 cat sample >filf &&
512 git checkout --conflict=diff3 -- fild file filf &&
513 (
514 echo "<<<<<<< ours"
515 echo ourside
516 echo "|||||||"
517 echo original
518 echo "======="
519 echo theirside
520 echo ">>>>>>> theirs"
521 ) >merged &&
522 test_cmp expect fild &&
523 test_cmp expect filf &&
524 test_cmp merged file
525 '
527 test_expect_success 'failing checkout -b should not break working tree' '
528 git reset --hard master &&
529 git symbolic-ref HEAD refs/heads/master &&
530 test_must_fail git checkout -b renamer side^ &&
531 test $(git symbolic-ref HEAD) = refs/heads/master &&
532 git diff --exit-code &&
533 git diff --cached --exit-code
535 '
537 test_expect_success 'switch out of non-branch' '
538 git reset --hard master &&
539 git checkout master^0 &&
540 echo modified >one &&
541 test_must_fail git checkout renamer 2>error.log &&
542 ! grep "^Previous HEAD" error.log
543 '
545 test_done