Quiet make: do not leave Windows behind
[git/platforms.git] / t / t7201-co.sh
blob528b9f65b16d6d0cfd12ff0e8b1d3eec9df7d1d0
1 #!/bin/sh
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 ---
15 + [side] Side M one, D two, A three
16 * [renamer] Renamer R one->uno, M two
17 +*+ [master] Initial A one, A two
21 . ./test-lib.sh
23 test_tick
25 fill () {
26 for i
28 echo "$i"
29 done
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
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 &&
70 if git checkout side
71 then
72 echo Not happy
73 false
74 else
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 &&
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
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
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
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
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
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
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
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
210 case $(uname -s) in
211 *MINGW*) test_expect=test_expect_failure;;
212 *) test_expect=test_expect_success;;
213 esac
214 $test_expect 'checkout to detach HEAD with :/message' '
216 git checkout -f master && git clean -f &&
217 git checkout ":/Initial" &&
218 H=$(git rev-parse --verify HEAD) &&
219 M=$(git show-ref -s --verify refs/heads/master) &&
220 test "z$H" = "z$M" &&
221 if git symbolic-ref HEAD >/dev/null 2>&1
222 then
223 echo "OOPS, HEAD is still symbolic???"
224 false
225 else
226 : happy
230 test_expect_success 'checkout to detach HEAD with HEAD^0' '
232 git checkout -f master && git clean -f &&
233 git checkout HEAD^0 &&
234 H=$(git rev-parse --verify HEAD) &&
235 M=$(git show-ref -s --verify refs/heads/master) &&
236 test "z$H" = "z$M" &&
237 if git symbolic-ref HEAD >/dev/null 2>&1
238 then
239 echo "OOPS, HEAD is still symbolic???"
240 false
241 else
242 : happy
246 test_expect_success 'checkout with ambiguous tag/branch names' '
248 git tag both side &&
249 git branch both master &&
250 git reset --hard &&
251 git checkout master &&
253 git checkout both &&
254 H=$(git rev-parse --verify HEAD) &&
255 M=$(git show-ref -s --verify refs/heads/master) &&
256 test "z$H" = "z$M" &&
257 name=$(git symbolic-ref HEAD 2>/dev/null) &&
258 test "z$name" = zrefs/heads/both
262 test_expect_success 'checkout with ambiguous tag/branch names' '
264 git reset --hard &&
265 git checkout master &&
267 git tag frotz side &&
268 git branch frotz master &&
269 git reset --hard &&
270 git checkout master &&
272 git checkout tags/frotz &&
273 H=$(git rev-parse --verify HEAD) &&
274 S=$(git show-ref -s --verify refs/heads/side) &&
275 test "z$H" = "z$S" &&
276 if name=$(git symbolic-ref HEAD 2>/dev/null)
277 then
278 echo "Bad -- should have detached"
279 false
280 else
281 : happy
286 test_expect_success 'switch branches while in subdirectory' '
288 git reset --hard &&
289 git checkout master &&
291 mkdir subs &&
293 cd subs &&
294 git checkout side
295 ) &&
296 ! test -f subs/one &&
297 rm -fr subs
301 test_expect_success 'checkout specific path while in subdirectory' '
303 git reset --hard &&
304 git checkout side &&
305 mkdir subs &&
306 >subs/bero &&
307 git add subs/bero &&
308 git commit -m "add subs/bero" &&
310 git checkout master &&
311 mkdir -p subs &&
313 cd subs &&
314 git checkout side -- bero
315 ) &&
316 test -f subs/bero
320 test_expect_success \
321 'checkout w/--track sets up tracking' '
322 git config branch.autosetupmerge false &&
323 git checkout master &&
324 git checkout --track -b track1 &&
325 test "$(git config branch.track1.remote)" &&
326 test "$(git config branch.track1.merge)"'
328 test_expect_success \
329 'checkout w/autosetupmerge=always sets up tracking' '
330 git config branch.autosetupmerge always &&
331 git checkout master &&
332 git checkout -b track2 &&
333 test "$(git config branch.track2.remote)" &&
334 test "$(git config branch.track2.merge)"
335 git config branch.autosetupmerge false'
337 test_expect_success 'checkout w/--track from non-branch HEAD fails' '
338 git checkout master^0 &&
339 test_must_fail git symbolic-ref HEAD &&
340 test_must_fail git checkout --track -b track &&
341 test_must_fail git rev-parse --verify track &&
342 test_must_fail git symbolic-ref HEAD &&
343 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
346 test_expect_success 'detach a symbolic link HEAD' '
347 git checkout master &&
348 git config --bool core.prefersymlinkrefs yes &&
349 git checkout side &&
350 git checkout master &&
351 it=$(git symbolic-ref HEAD) &&
352 test "z$it" = zrefs/heads/master &&
353 here=$(git rev-parse --verify refs/heads/master) &&
354 git checkout side^ &&
355 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
358 test_expect_success \
359 'checkout with --track fakes a sensible -b <name>' '
360 git update-ref refs/remotes/origin/koala/bear renamer &&
361 git update-ref refs/new/koala/bear renamer &&
363 git checkout --track origin/koala/bear &&
364 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
365 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
367 git checkout master && git branch -D koala/bear &&
369 git checkout --track refs/remotes/origin/koala/bear &&
370 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
371 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
373 git checkout master && git branch -D koala/bear &&
375 git checkout --track remotes/origin/koala/bear &&
376 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
377 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
379 git checkout master && git branch -D koala/bear &&
381 git checkout --track refs/new/koala/bear &&
382 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
383 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
386 test_expect_success \
387 'checkout with --track, but without -b, fails with too short tracked name' '
388 test_must_fail git checkout --track renamer'
390 setup_conflicting_index () {
391 rm -f .git/index &&
392 O=$(echo original | git hash-object -w --stdin) &&
393 A=$(echo ourside | git hash-object -w --stdin) &&
394 B=$(echo theirside | git hash-object -w --stdin) &&
396 echo "100644 $A 0 fild" &&
397 echo "100644 $O 1 file" &&
398 echo "100644 $A 2 file" &&
399 echo "100644 $B 3 file" &&
400 echo "100644 $A 0 filf"
401 ) | git update-index --index-info
404 test_expect_success 'checkout an unmerged path should fail' '
405 setup_conflicting_index &&
406 echo "none of the above" >sample &&
407 cat sample >fild &&
408 cat sample >file &&
409 cat sample >filf &&
410 test_must_fail git checkout fild file filf &&
411 test_cmp sample fild &&
412 test_cmp sample filf &&
413 test_cmp sample file
416 test_expect_success 'checkout with an unmerged path can be ignored' '
417 setup_conflicting_index &&
418 echo "none of the above" >sample &&
419 echo ourside >expect &&
420 cat sample >fild &&
421 cat sample >file &&
422 cat sample >filf &&
423 git checkout -f fild file filf &&
424 test_cmp expect fild &&
425 test_cmp expect filf &&
426 test_cmp sample file
429 test_expect_success 'checkout unmerged stage' '
430 setup_conflicting_index &&
431 echo "none of the above" >sample &&
432 echo ourside >expect &&
433 cat sample >fild &&
434 cat sample >file &&
435 cat sample >filf &&
436 git checkout --ours . &&
437 test_cmp expect fild &&
438 test_cmp expect filf &&
439 test_cmp expect file &&
440 git checkout --theirs file &&
441 test ztheirside = "z$(cat file)"
444 test_expect_success 'checkout with --merge' '
445 setup_conflicting_index &&
446 echo "none of the above" >sample &&
447 echo ourside >expect &&
448 cat sample >fild &&
449 cat sample >file &&
450 cat sample >filf &&
451 git checkout -m -- fild file filf &&
453 echo "<<<<<<< ours"
454 echo ourside
455 echo "======="
456 echo theirside
457 echo ">>>>>>> theirs"
458 ) >merged &&
459 test_cmp expect fild &&
460 test_cmp expect filf &&
461 test_cmp merged file
464 test_expect_success 'checkout with --merge, in diff3 -m style' '
465 git config merge.conflictstyle diff3 &&
466 setup_conflicting_index &&
467 echo "none of the above" >sample &&
468 echo ourside >expect &&
469 cat sample >fild &&
470 cat sample >file &&
471 cat sample >filf &&
472 git checkout -m -- fild file filf &&
474 echo "<<<<<<< ours"
475 echo ourside
476 echo "|||||||"
477 echo original
478 echo "======="
479 echo theirside
480 echo ">>>>>>> theirs"
481 ) >merged &&
482 test_cmp expect fild &&
483 test_cmp expect filf &&
484 test_cmp merged file
487 test_expect_success 'checkout --conflict=merge, overriding config' '
488 git config merge.conflictstyle diff3 &&
489 setup_conflicting_index &&
490 echo "none of the above" >sample &&
491 echo ourside >expect &&
492 cat sample >fild &&
493 cat sample >file &&
494 cat sample >filf &&
495 git checkout --conflict=merge -- fild file filf &&
497 echo "<<<<<<< ours"
498 echo ourside
499 echo "======="
500 echo theirside
501 echo ">>>>>>> theirs"
502 ) >merged &&
503 test_cmp expect fild &&
504 test_cmp expect filf &&
505 test_cmp merged file
508 test_expect_success 'checkout --conflict=diff3' '
509 git config --unset merge.conflictstyle
510 setup_conflicting_index &&
511 echo "none of the above" >sample &&
512 echo ourside >expect &&
513 cat sample >fild &&
514 cat sample >file &&
515 cat sample >filf &&
516 git checkout --conflict=diff3 -- fild file filf &&
518 echo "<<<<<<< ours"
519 echo ourside
520 echo "|||||||"
521 echo original
522 echo "======="
523 echo theirside
524 echo ">>>>>>> theirs"
525 ) >merged &&
526 test_cmp expect fild &&
527 test_cmp expect filf &&
528 test_cmp merged file
531 test_expect_success 'failing checkout -b should not break working tree' '
532 git reset --hard master &&
533 git symbolic-ref HEAD refs/heads/master &&
534 test_must_fail git checkout -b renamer side^ &&
535 test $(git symbolic-ref HEAD) = refs/heads/master &&
536 git diff --exit-code &&
537 git diff --cached --exit-code
541 test_done