]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7201-co.sh
t7201: use 'git -C' to avoid subshell
[thirdparty/git.git] / t / t7201-co.sh
CommitLineData
abc02670
JH
1#!/bin/sh
2#
3# Copyright (c) 2006 Junio C Hamano
4#
5
47a528ad 6test_description='git checkout tests.
3e0318a3
JH
7
8Creates master, forks renamer and side branches from it.
9Test switching across them.
10
11 ! [master] Initial A one, A two
12 * [renamer] Renamer R one->uno, M two
13 ! [side] Side M one, D two, A three
47349a8c
JN
14 ! [simple] Simple D one, M two
15 ----
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
3e0318a3
JH
20
21'
abc02670
JH
22
23. ./test-lib.sh
24
c07c7bf6
DB
25test_tick
26
abc02670
JH
27fill () {
28 for i
29 do
30 echo "$i"
31 done
32}
33
c1a4278e 34
abc02670 35test_expect_success setup '
c327762f 36 fill x y z >same &&
c1a4278e 37 fill 1 2 3 4 5 6 7 8 >one &&
abc02670 38 fill a b c d e >two &&
c07c7bf6 39 git add same one two &&
abc02670
JH
40 git commit -m "Initial A one, A two" &&
41
c1a4278e
JH
42 git checkout -b renamer &&
43 rm -f one &&
44 fill 1 3 4 5 6 7 8 >uno &&
45 git add uno &&
46 fill a b c d e f >two &&
47 git commit -a -m "Renamer R one->uno, M two" &&
48
49 git checkout -b side master &&
50 fill 1 2 3 4 5 6 7 >one &&
abc02670
JH
51 fill A B C D E >three &&
52 rm -f two &&
53 git update-index --add --remove one two three &&
54 git commit -m "Side M one, D two, A three" &&
55
47349a8c
JN
56 git checkout -b simple master &&
57 rm -f one &&
c327762f 58 fill a c e >two &&
47349a8c
JN
59 git commit -a -m "Simple D one, M two" &&
60
abc02670
JH
61 git checkout master
62'
63
e166fe36 64test_expect_success 'checkout from non-existing branch' '
5a03e7f2 65 git checkout -b delete-me master &&
cbc5cf7c 66 git update-ref -d --no-deref refs/heads/delete-me &&
5a03e7f2
SP
67 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
68 git checkout master &&
69 test refs/heads/master = "$(git symbolic-ref HEAD)"
70'
71
e166fe36 72test_expect_success 'checkout with dirty tree without -m' '
c1a4278e 73 fill 0 1 2 3 4 5 6 7 8 >one &&
abc02670
JH
74 if git checkout side
75 then
76 echo Not happy
77 false
78 else
79 echo "happy - failed correctly"
80 fi
abc02670
JH
81'
82
e166fe36 83test_expect_success 'checkout with unrelated dirty tree without -m' '
c07c7bf6
DB
84 git checkout -f master &&
85 fill 0 1 2 3 4 5 6 7 8 >same &&
aef591a0 86 cp same kept &&
c07c7bf6 87 git checkout side >messages &&
aef591a0
JK
88 test_cmp same kept &&
89 printf "M\t%s\n" same >messages.expect &&
82ebb0b6 90 test_cmp messages.expect messages
c07c7bf6
DB
91'
92
e166fe36 93test_expect_success 'checkout -m with dirty tree' '
abc02670 94 git checkout -f master &&
562ca192 95 git clean -f &&
abc02670 96
c1a4278e 97 fill 0 1 2 3 4 5 6 7 8 >one &&
c327762f 98 git checkout -m side >messages &&
abc02670 99
c1a4278e 100 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
abc02670 101
aef591a0 102 printf "M\t%s\n" one >expect.messages &&
82ebb0b6 103 test_cmp expect.messages messages &&
c07c7bf6 104
abc02670
JH
105 fill "M one" "A three" "D two" >expect.master &&
106 git diff --name-status master >current.master &&
82ebb0b6 107 test_cmp expect.master current.master &&
abc02670
JH
108
109 fill "M one" >expect.side &&
110 git diff --name-status side >current.side &&
82ebb0b6 111 test_cmp expect.side current.side &&
abc02670 112
abc02670 113 git diff --cached >current.index &&
1c5e94f4 114 test_must_be_empty current.index
abc02670
JH
115'
116
e166fe36 117test_expect_success 'checkout -m with dirty tree, renamed' '
562ca192 118 git checkout -f master && git clean -f &&
c1a4278e
JH
119
120 fill 1 2 3 4 5 7 8 >one &&
121 if git checkout renamer
122 then
123 echo Not happy
124 false
125 else
126 echo "happy - failed correctly"
127 fi &&
128
129 git checkout -m renamer &&
130 fill 1 3 4 5 7 8 >expect &&
82ebb0b6 131 test_cmp expect uno &&
d7ebd53d
JH
132 ! test -f one &&
133 git diff --cached >current &&
ec10b018 134 test_must_be_empty current
d7ebd53d
JH
135'
136
137test_expect_success 'checkout -m with merge conflict' '
562ca192 138 git checkout -f master && git clean -f &&
d7ebd53d
JH
139
140 fill 1 T 3 4 5 6 S 8 >one &&
141 if git checkout renamer
142 then
143 echo Not happy
144 false
145 else
146 echo "happy - failed correctly"
147 fi &&
148
149 git checkout -m renamer &&
150
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 &&
dcbaa0b3 154 test_cmp expect current &&
d7ebd53d 155 git diff --cached two >current &&
ec10b018 156 test_must_be_empty current
c1a4278e
JH
157'
158
47349a8c 159test_expect_success 'format of merge conflict from checkout -m' '
47349a8c
JN
160 git checkout -f master && git clean -f &&
161
c327762f 162 fill b d >two &&
47349a8c
JN
163 git checkout -m simple &&
164
165 git ls-files >current &&
166 fill same two two two >expect &&
dcbaa0b3 167 test_cmp expect current &&
47349a8c
JN
168
169 cat <<-EOF >expect &&
170 <<<<<<< simple
171 a
172 c
173 e
174 =======
175 b
176 d
177 >>>>>>> local
178 EOF
9c5b2fab 179 test_cmp expect two
47349a8c
JN
180'
181
182test_expect_success 'checkout --merge --conflict=diff3 <branch>' '
47349a8c
JN
183 git checkout -f master && git reset --hard && git clean -f &&
184
c327762f 185 fill b d >two &&
47349a8c
JN
186 git checkout --merge --conflict=diff3 simple &&
187
188 cat <<-EOF >expect &&
189 <<<<<<< simple
190 a
191 c
192 e
c4151629 193 ||||||| master
47349a8c
JN
194 a
195 b
196 c
197 d
198 e
199 =======
200 b
201 d
202 >>>>>>> local
203 EOF
9c5b2fab 204 test_cmp expect two
47349a8c
JN
205'
206
6a143aa2 207test_expect_success 'switch to another branch while carrying a deletion' '
6a143aa2
JN
208 git checkout -f master && git reset --hard && git clean -f &&
209 git rm two &&
210
211 test_must_fail git checkout simple 2>errs &&
212 test_i18ngrep overwritten errs &&
213
3e41485d 214 test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs &&
6eff409e 215 test_must_be_empty errs
6a143aa2
JN
216'
217
69382976 218test_expect_success 'checkout to detach HEAD (with advice declined)' '
13be3e31 219 git config advice.detachedHead false &&
d482c234 220 rev=$(git rev-parse --short renamer^) &&
562ca192 221 git checkout -f renamer && git clean -f &&
c07c7bf6 222 git checkout renamer^ 2>messages &&
d482c234 223 test_i18ngrep "HEAD is now at $rev" messages &&
3fb0459b 224 test_line_count = 1 messages &&
13be3e31
JH
225 H=$(git rev-parse --verify HEAD) &&
226 M=$(git show-ref -s --verify refs/heads/master) &&
227 test "z$H" = "z$M" &&
228 if git symbolic-ref HEAD >/dev/null 2>&1
229 then
230 echo "OOPS, HEAD is still symbolic???"
231 false
232 else
233 : happy
234 fi
235'
236
69382976 237test_expect_success 'checkout to detach HEAD' '
13be3e31 238 git config advice.detachedHead true &&
d482c234 239 rev=$(git rev-parse --short renamer^) &&
13be3e31 240 git checkout -f renamer && git clean -f &&
1ff750b1 241 GIT_TEST_GETTEXT_POISON=false git checkout renamer^ 2>messages &&
d482c234 242 grep "HEAD is now at $rev" messages &&
6cdccfce 243 test_line_count -gt 1 messages &&
3e0318a3
JH
244 H=$(git rev-parse --verify HEAD) &&
245 M=$(git show-ref -s --verify refs/heads/master) &&
246 test "z$H" = "z$M" &&
247 if git symbolic-ref HEAD >/dev/null 2>&1
248 then
249 echo "OOPS, HEAD is still symbolic???"
250 false
251 else
252 : happy
253 fi
254'
255
69382976 256test_expect_success 'checkout to detach HEAD with branchname^' '
562ca192 257 git checkout -f master && git clean -f &&
3e0318a3
JH
258 git checkout renamer^ &&
259 H=$(git rev-parse --verify HEAD) &&
260 M=$(git show-ref -s --verify refs/heads/master) &&
261 test "z$H" = "z$M" &&
262 if git symbolic-ref HEAD >/dev/null 2>&1
263 then
264 echo "OOPS, HEAD is still symbolic???"
265 false
266 else
267 : happy
268 fi
269'
270
69382976 271test_expect_success 'checkout to detach HEAD with :/message' '
b1e9efa7
DB
272 git checkout -f master && git clean -f &&
273 git checkout ":/Initial" &&
274 H=$(git rev-parse --verify HEAD) &&
275 M=$(git show-ref -s --verify refs/heads/master) &&
276 test "z$H" = "z$M" &&
277 if git symbolic-ref HEAD >/dev/null 2>&1
278 then
279 echo "OOPS, HEAD is still symbolic???"
280 false
281 else
282 : happy
283 fi
284'
285
69382976 286test_expect_success 'checkout to detach HEAD with HEAD^0' '
562ca192 287 git checkout -f master && git clean -f &&
3e0318a3
JH
288 git checkout HEAD^0 &&
289 H=$(git rev-parse --verify HEAD) &&
290 M=$(git show-ref -s --verify refs/heads/master) &&
291 test "z$H" = "z$M" &&
292 if git symbolic-ref HEAD >/dev/null 2>&1
293 then
294 echo "OOPS, HEAD is still symbolic???"
295 false
296 else
297 : happy
298 fi
299'
300
5035242c 301test_expect_success 'checkout with ambiguous tag/branch names' '
5035242c
JH
302 git tag both side &&
303 git branch both master &&
304 git reset --hard &&
305 git checkout master &&
306
307 git checkout both &&
308 H=$(git rev-parse --verify HEAD) &&
309 M=$(git show-ref -s --verify refs/heads/master) &&
310 test "z$H" = "z$M" &&
311 name=$(git symbolic-ref HEAD 2>/dev/null) &&
312 test "z$name" = zrefs/heads/both
5035242c
JH
313'
314
315test_expect_success 'checkout with ambiguous tag/branch names' '
5035242c
JH
316 git reset --hard &&
317 git checkout master &&
318
319 git tag frotz side &&
320 git branch frotz master &&
321 git reset --hard &&
322 git checkout master &&
323
324 git checkout tags/frotz &&
325 H=$(git rev-parse --verify HEAD) &&
326 S=$(git show-ref -s --verify refs/heads/side) &&
327 test "z$H" = "z$S" &&
328 if name=$(git symbolic-ref HEAD 2>/dev/null)
329 then
330 echo "Bad -- should have detached"
331 false
332 else
333 : happy
334 fi
5035242c
JH
335'
336
6010d2d9 337test_expect_success 'switch branches while in subdirectory' '
6010d2d9
JH
338 git reset --hard &&
339 git checkout master &&
340
341 mkdir subs &&
627f2d79 342 git -C subs checkout side &&
6010d2d9
JH
343 ! test -f subs/one &&
344 rm -fr subs
6010d2d9
JH
345'
346
347test_expect_success 'checkout specific path while in subdirectory' '
6010d2d9
JH
348 git reset --hard &&
349 git checkout side &&
350 mkdir subs &&
351 >subs/bero &&
352 git add subs/bero &&
353 git commit -m "add subs/bero" &&
354
355 git checkout master &&
356 mkdir -p subs &&
627f2d79 357 git -C subs checkout side -- bero &&
6010d2d9 358 test -f subs/bero
6010d2d9
JH
359'
360
e166fe36 361test_expect_success 'checkout w/--track sets up tracking' '
9ed36cfa
JS
362 git config branch.autosetupmerge false &&
363 git checkout master &&
364 git checkout --track -b track1 &&
365 test "$(git config branch.track1.remote)" &&
e166fe36
CM
366 test "$(git config branch.track1.merge)"
367'
9ed36cfa 368
e166fe36 369test_expect_success 'checkout w/autosetupmerge=always sets up tracking' '
aef591a0 370 test_when_finished git config branch.autosetupmerge false &&
9ed36cfa
JS
371 git config branch.autosetupmerge always &&
372 git checkout master &&
373 git checkout -b track2 &&
374 test "$(git config branch.track2.remote)" &&
e166fe36
CM
375 test "$(git config branch.track2.merge)"
376'
9ed36cfa 377
51a94af8
JH
378test_expect_success 'checkout w/--track from non-branch HEAD fails' '
379 git checkout master^0 &&
380 test_must_fail git symbolic-ref HEAD &&
381 test_must_fail git checkout --track -b track &&
382 test_must_fail git rev-parse --verify track &&
383 test_must_fail git symbolic-ref HEAD &&
384 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
385'
9ed36cfa 386
21b5b1e8
JH
387test_expect_success 'checkout w/--track from tag fails' '
388 git checkout master^0 &&
389 test_must_fail git symbolic-ref HEAD &&
390 test_must_fail git checkout --track -b track frotz &&
391 test_must_fail git rev-parse --verify track &&
392 test_must_fail git symbolic-ref HEAD &&
393 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
394'
395
acd3b9ec 396test_expect_success 'detach a symbolic link HEAD' '
f5637549
JH
397 git checkout master &&
398 git config --bool core.prefersymlinkrefs yes &&
399 git checkout side &&
400 git checkout master &&
401 it=$(git symbolic-ref HEAD) &&
402 test "z$it" = zrefs/heads/master &&
403 here=$(git rev-parse --verify refs/heads/master) &&
404 git checkout side^ &&
405 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
406'
9ed36cfa 407
e166fe36 408test_expect_success 'checkout with --track fakes a sensible -b <name>' '
88a9f72f 409 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
bb0ceb62 410 git update-ref refs/remotes/origin/koala/bear renamer &&
9188ed89 411
bb0ceb62
JS
412 git checkout --track origin/koala/bear &&
413 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
9188ed89
AR
414 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
415
416 git checkout master && git branch -D koala/bear &&
417
418 git checkout --track refs/remotes/origin/koala/bear &&
419 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
420 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
421
422 git checkout master && git branch -D koala/bear &&
423
424 git checkout --track remotes/origin/koala/bear &&
425 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
9188ed89
AR
426 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
427'
bb0ceb62 428
e166fe36
CM
429test_expect_success 'checkout with --track, but without -b, fails with too short tracked name' '
430 test_must_fail git checkout --track renamer
431'
bb0ceb62 432
eac5a401 433setup_conflicting_index () {
8fdcf312
JH
434 rm -f .git/index &&
435 O=$(echo original | git hash-object -w --stdin) &&
436 A=$(echo ourside | git hash-object -w --stdin) &&
437 B=$(echo theirside | git hash-object -w --stdin) &&
438 (
439 echo "100644 $A 0 fild" &&
440 echo "100644 $O 1 file" &&
441 echo "100644 $A 2 file" &&
442 echo "100644 $B 3 file" &&
443 echo "100644 $A 0 filf"
eac5a401
JH
444 ) | git update-index --index-info
445}
446
447test_expect_success 'checkout an unmerged path should fail' '
448 setup_conflicting_index &&
8fdcf312
JH
449 echo "none of the above" >sample &&
450 cat sample >fild &&
451 cat sample >file &&
452 cat sample >filf &&
453 test_must_fail git checkout fild file filf &&
454 test_cmp sample fild &&
455 test_cmp sample filf &&
456 test_cmp sample file
457'
458
db941099 459test_expect_success 'checkout with an unmerged path can be ignored' '
eac5a401 460 setup_conflicting_index &&
db941099
JH
461 echo "none of the above" >sample &&
462 echo ourside >expect &&
463 cat sample >fild &&
464 cat sample >file &&
465 cat sample >filf &&
466 git checkout -f fild file filf &&
467 test_cmp expect fild &&
468 test_cmp expect filf &&
469 test_cmp sample file
470'
471
38901a48 472test_expect_success 'checkout unmerged stage' '
eac5a401 473 setup_conflicting_index &&
38901a48
JH
474 echo "none of the above" >sample &&
475 echo ourside >expect &&
476 cat sample >fild &&
477 cat sample >file &&
478 cat sample >filf &&
479 git checkout --ours . &&
480 test_cmp expect fild &&
481 test_cmp expect filf &&
482 test_cmp expect file &&
483 git checkout --theirs file &&
484 test ztheirside = "z$(cat file)"
485'
486
0cf8581e 487test_expect_success 'checkout with --merge' '
eac5a401 488 setup_conflicting_index &&
0cf8581e
JH
489 echo "none of the above" >sample &&
490 echo ourside >expect &&
491 cat sample >fild &&
492 cat sample >file &&
493 cat sample >filf &&
494 git checkout -m -- fild file filf &&
495 (
e974e06d
ES
496 echo "<<<<<<< ours" &&
497 echo ourside &&
498 echo "=======" &&
499 echo theirside &&
0cf8581e
JH
500 echo ">>>>>>> theirs"
501 ) >merged &&
502 test_cmp expect fild &&
503 test_cmp expect filf &&
504 test_cmp merged file
505'
506
507test_expect_success 'checkout with --merge, in diff3 -m style' '
508 git config merge.conflictstyle diff3 &&
eac5a401 509 setup_conflicting_index &&
0cf8581e
JH
510 echo "none of the above" >sample &&
511 echo ourside >expect &&
512 cat sample >fild &&
513 cat sample >file &&
514 cat sample >filf &&
515 git checkout -m -- fild file filf &&
516 (
e974e06d
ES
517 echo "<<<<<<< ours" &&
518 echo ourside &&
519 echo "||||||| base" &&
520 echo original &&
521 echo "=======" &&
522 echo theirside &&
0cf8581e
JH
523 echo ">>>>>>> theirs"
524 ) >merged &&
525 test_cmp expect fild &&
526 test_cmp expect filf &&
527 test_cmp merged file
528'
529
eac5a401
JH
530test_expect_success 'checkout --conflict=merge, overriding config' '
531 git config merge.conflictstyle diff3 &&
532 setup_conflicting_index &&
533 echo "none of the above" >sample &&
534 echo ourside >expect &&
535 cat sample >fild &&
536 cat sample >file &&
537 cat sample >filf &&
538 git checkout --conflict=merge -- fild file filf &&
539 (
e974e06d
ES
540 echo "<<<<<<< ours" &&
541 echo ourside &&
542 echo "=======" &&
543 echo theirside &&
eac5a401
JH
544 echo ">>>>>>> theirs"
545 ) >merged &&
546 test_cmp expect fild &&
547 test_cmp expect filf &&
548 test_cmp merged file
549'
550
551test_expect_success 'checkout --conflict=diff3' '
a6a4a88a 552 test_unconfig merge.conflictstyle &&
eac5a401
JH
553 setup_conflicting_index &&
554 echo "none of the above" >sample &&
555 echo ourside >expect &&
556 cat sample >fild &&
557 cat sample >file &&
558 cat sample >filf &&
559 git checkout --conflict=diff3 -- fild file filf &&
560 (
e974e06d
ES
561 echo "<<<<<<< ours" &&
562 echo ourside &&
563 echo "||||||| base" &&
564 echo original &&
565 echo "=======" &&
566 echo theirside &&
eac5a401
JH
567 echo ">>>>>>> theirs"
568 ) >merged &&
569 test_cmp expect fild &&
570 test_cmp expect filf &&
571 test_cmp merged file
572'
573
352eadc4
DB
574test_expect_success 'failing checkout -b should not break working tree' '
575 git reset --hard master &&
576 git symbolic-ref HEAD refs/heads/master &&
577 test_must_fail git checkout -b renamer side^ &&
578 test $(git symbolic-ref HEAD) = refs/heads/master &&
579 git diff --exit-code &&
580 git diff --cached --exit-code
352eadc4
DB
581'
582
e4b09dad
NS
583test_expect_success 'switch out of non-branch' '
584 git reset --hard master &&
585 git checkout master^0 &&
586 echo modified >one &&
587 test_must_fail git checkout renamer 2>error.log &&
588 ! grep "^Previous HEAD" error.log
589'
590
6d6f9acc
JH
591(
592 echo "#!$SHELL_PATH"
593 cat <<\EOF
594O=$1 A=$2 B=$3
595cat "$A" >.tmp
596exec >"$A"
597echo '<<<<<<< filfre-theirs'
598cat "$B"
599echo '||||||| filfre-common'
600cat "$O"
601echo '======='
602cat ".tmp"
603echo '>>>>>>> filfre-ours'
604rm -f .tmp
605exit 1
606EOF
607) >filfre.sh
608chmod +x filfre.sh
609
610test_expect_success 'custom merge driver with checkout -m' '
611 git reset --hard &&
612
613 git config merge.filfre.driver "./filfre.sh %O %A %B" &&
614 git config merge.filfre.name "Feel-free merge driver" &&
615 git config merge.filfre.recursive binary &&
616 echo "arm merge=filfre" >.gitattributes &&
617
618 git checkout -b left &&
619 echo neutral >arm &&
620 git add arm .gitattributes &&
621 test_tick &&
622 git commit -m neutral &&
623 git branch right &&
624
625 echo left >arm &&
626 test_tick &&
627 git commit -a -m left &&
628 git checkout right &&
629
630 echo right >arm &&
631 test_tick &&
632 git commit -a -m right &&
633
634 test_must_fail git merge left &&
635 (
636 for t in filfre-common left right
637 do
638 grep $t arm || exit 1
639 done
6d6f9acc
JH
640 ) &&
641
642 mv arm expect &&
643 git checkout -m arm &&
644 test_cmp expect arm
645'
646
abc02670 647test_done