]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7201-co.sh
Merge branch 'es/perf-export-fix'
[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' '
1c0ab5c7
CM
160 git checkout -f master &&
161 git clean -f &&
47349a8c 162
c327762f 163 fill b d >two &&
47349a8c
JN
164 git checkout -m simple &&
165
166 git ls-files >current &&
167 fill same two two two >expect &&
dcbaa0b3 168 test_cmp expect current &&
47349a8c
JN
169
170 cat <<-EOF >expect &&
171 <<<<<<< simple
172 a
173 c
174 e
175 =======
176 b
177 d
178 >>>>>>> local
179 EOF
9c5b2fab 180 test_cmp expect two
47349a8c
JN
181'
182
183test_expect_success 'checkout --merge --conflict=diff3 <branch>' '
1c0ab5c7
CM
184 git checkout -f master &&
185 git reset --hard &&
186 git clean -f &&
47349a8c 187
c327762f 188 fill b d >two &&
47349a8c
JN
189 git checkout --merge --conflict=diff3 simple &&
190
191 cat <<-EOF >expect &&
192 <<<<<<< simple
193 a
194 c
195 e
c4151629 196 ||||||| master
47349a8c
JN
197 a
198 b
199 c
200 d
201 e
202 =======
203 b
204 d
205 >>>>>>> local
206 EOF
9c5b2fab 207 test_cmp expect two
47349a8c
JN
208'
209
6a143aa2 210test_expect_success 'switch to another branch while carrying a deletion' '
1c0ab5c7
CM
211 git checkout -f master &&
212 git reset --hard &&
213 git clean -f &&
6a143aa2
JN
214 git rm two &&
215
216 test_must_fail git checkout simple 2>errs &&
217 test_i18ngrep overwritten errs &&
218
3e41485d 219 test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs &&
6eff409e 220 test_must_be_empty errs
6a143aa2
JN
221'
222
69382976 223test_expect_success 'checkout to detach HEAD (with advice declined)' '
13be3e31 224 git config advice.detachedHead false &&
d482c234 225 rev=$(git rev-parse --short renamer^) &&
1c0ab5c7
CM
226 git checkout -f renamer &&
227 git clean -f &&
c07c7bf6 228 git checkout renamer^ 2>messages &&
d482c234 229 test_i18ngrep "HEAD is now at $rev" messages &&
3fb0459b 230 test_line_count = 1 messages &&
13be3e31
JH
231 H=$(git rev-parse --verify HEAD) &&
232 M=$(git show-ref -s --verify refs/heads/master) &&
233 test "z$H" = "z$M" &&
234 if git symbolic-ref HEAD >/dev/null 2>&1
235 then
236 echo "OOPS, HEAD is still symbolic???"
237 false
238 else
239 : happy
240 fi
241'
242
69382976 243test_expect_success 'checkout to detach HEAD' '
13be3e31 244 git config advice.detachedHead true &&
d482c234 245 rev=$(git rev-parse --short renamer^) &&
1c0ab5c7
CM
246 git checkout -f renamer &&
247 git clean -f &&
1ff750b1 248 GIT_TEST_GETTEXT_POISON=false git checkout renamer^ 2>messages &&
d482c234 249 grep "HEAD is now at $rev" messages &&
6cdccfce 250 test_line_count -gt 1 messages &&
3e0318a3
JH
251 H=$(git rev-parse --verify HEAD) &&
252 M=$(git show-ref -s --verify refs/heads/master) &&
253 test "z$H" = "z$M" &&
254 if git symbolic-ref HEAD >/dev/null 2>&1
255 then
256 echo "OOPS, HEAD is still symbolic???"
257 false
258 else
259 : happy
260 fi
261'
262
69382976 263test_expect_success 'checkout to detach HEAD with branchname^' '
1c0ab5c7
CM
264 git checkout -f master &&
265 git clean -f &&
3e0318a3
JH
266 git checkout renamer^ &&
267 H=$(git rev-parse --verify HEAD) &&
268 M=$(git show-ref -s --verify refs/heads/master) &&
269 test "z$H" = "z$M" &&
270 if git symbolic-ref HEAD >/dev/null 2>&1
271 then
272 echo "OOPS, HEAD is still symbolic???"
273 false
274 else
275 : happy
276 fi
277'
278
69382976 279test_expect_success 'checkout to detach HEAD with :/message' '
1c0ab5c7
CM
280 git checkout -f master &&
281 git clean -f &&
b1e9efa7
DB
282 git checkout ":/Initial" &&
283 H=$(git rev-parse --verify HEAD) &&
284 M=$(git show-ref -s --verify refs/heads/master) &&
285 test "z$H" = "z$M" &&
286 if git symbolic-ref HEAD >/dev/null 2>&1
287 then
288 echo "OOPS, HEAD is still symbolic???"
289 false
290 else
291 : happy
292 fi
293'
294
69382976 295test_expect_success 'checkout to detach HEAD with HEAD^0' '
1c0ab5c7
CM
296 git checkout -f master &&
297 git clean -f &&
3e0318a3
JH
298 git checkout HEAD^0 &&
299 H=$(git rev-parse --verify HEAD) &&
300 M=$(git show-ref -s --verify refs/heads/master) &&
301 test "z$H" = "z$M" &&
302 if git symbolic-ref HEAD >/dev/null 2>&1
303 then
304 echo "OOPS, HEAD is still symbolic???"
305 false
306 else
307 : happy
308 fi
309'
310
5035242c 311test_expect_success 'checkout with ambiguous tag/branch names' '
5035242c
JH
312 git tag both side &&
313 git branch both master &&
314 git reset --hard &&
315 git checkout master &&
316
317 git checkout both &&
318 H=$(git rev-parse --verify HEAD) &&
319 M=$(git show-ref -s --verify refs/heads/master) &&
320 test "z$H" = "z$M" &&
321 name=$(git symbolic-ref HEAD 2>/dev/null) &&
322 test "z$name" = zrefs/heads/both
5035242c
JH
323'
324
325test_expect_success 'checkout with ambiguous tag/branch names' '
5035242c
JH
326 git reset --hard &&
327 git checkout master &&
328
329 git tag frotz side &&
330 git branch frotz master &&
331 git reset --hard &&
332 git checkout master &&
333
334 git checkout tags/frotz &&
335 H=$(git rev-parse --verify HEAD) &&
336 S=$(git show-ref -s --verify refs/heads/side) &&
337 test "z$H" = "z$S" &&
338 if name=$(git symbolic-ref HEAD 2>/dev/null)
339 then
340 echo "Bad -- should have detached"
341 false
342 else
343 : happy
344 fi
5035242c
JH
345'
346
6010d2d9 347test_expect_success 'switch branches while in subdirectory' '
6010d2d9
JH
348 git reset --hard &&
349 git checkout master &&
350
351 mkdir subs &&
627f2d79 352 git -C subs checkout side &&
6010d2d9
JH
353 ! test -f subs/one &&
354 rm -fr subs
6010d2d9
JH
355'
356
357test_expect_success 'checkout specific path while in subdirectory' '
6010d2d9
JH
358 git reset --hard &&
359 git checkout side &&
360 mkdir subs &&
361 >subs/bero &&
362 git add subs/bero &&
363 git commit -m "add subs/bero" &&
364
365 git checkout master &&
366 mkdir -p subs &&
627f2d79 367 git -C subs checkout side -- bero &&
6010d2d9 368 test -f subs/bero
6010d2d9
JH
369'
370
e166fe36 371test_expect_success 'checkout w/--track sets up tracking' '
9ed36cfa
JS
372 git config branch.autosetupmerge false &&
373 git checkout master &&
374 git checkout --track -b track1 &&
375 test "$(git config branch.track1.remote)" &&
e166fe36
CM
376 test "$(git config branch.track1.merge)"
377'
9ed36cfa 378
e166fe36 379test_expect_success 'checkout w/autosetupmerge=always sets up tracking' '
aef591a0 380 test_when_finished git config branch.autosetupmerge false &&
9ed36cfa
JS
381 git config branch.autosetupmerge always &&
382 git checkout master &&
383 git checkout -b track2 &&
384 test "$(git config branch.track2.remote)" &&
e166fe36
CM
385 test "$(git config branch.track2.merge)"
386'
9ed36cfa 387
51a94af8
JH
388test_expect_success 'checkout w/--track from non-branch HEAD fails' '
389 git checkout master^0 &&
390 test_must_fail git symbolic-ref HEAD &&
391 test_must_fail git checkout --track -b track &&
392 test_must_fail git rev-parse --verify track &&
393 test_must_fail git symbolic-ref HEAD &&
394 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
395'
9ed36cfa 396
21b5b1e8
JH
397test_expect_success 'checkout w/--track from tag fails' '
398 git checkout master^0 &&
399 test_must_fail git symbolic-ref HEAD &&
400 test_must_fail git checkout --track -b track frotz &&
401 test_must_fail git rev-parse --verify track &&
402 test_must_fail git symbolic-ref HEAD &&
403 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
404'
405
acd3b9ec 406test_expect_success 'detach a symbolic link HEAD' '
f5637549
JH
407 git checkout master &&
408 git config --bool core.prefersymlinkrefs yes &&
409 git checkout side &&
410 git checkout master &&
411 it=$(git symbolic-ref HEAD) &&
412 test "z$it" = zrefs/heads/master &&
413 here=$(git rev-parse --verify refs/heads/master) &&
414 git checkout side^ &&
415 test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
416'
9ed36cfa 417
e166fe36 418test_expect_success 'checkout with --track fakes a sensible -b <name>' '
88a9f72f 419 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
bb0ceb62 420 git update-ref refs/remotes/origin/koala/bear renamer &&
9188ed89 421
bb0ceb62
JS
422 git checkout --track origin/koala/bear &&
423 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
9188ed89
AR
424 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
425
426 git checkout master && git branch -D koala/bear &&
427
428 git checkout --track refs/remotes/origin/koala/bear &&
429 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
430 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
431
432 git checkout master && git branch -D koala/bear &&
433
434 git checkout --track remotes/origin/koala/bear &&
435 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
9188ed89
AR
436 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
437'
bb0ceb62 438
e166fe36
CM
439test_expect_success 'checkout with --track, but without -b, fails with too short tracked name' '
440 test_must_fail git checkout --track renamer
441'
bb0ceb62 442
eac5a401 443setup_conflicting_index () {
8fdcf312
JH
444 rm -f .git/index &&
445 O=$(echo original | git hash-object -w --stdin) &&
446 A=$(echo ourside | git hash-object -w --stdin) &&
447 B=$(echo theirside | git hash-object -w --stdin) &&
448 (
449 echo "100644 $A 0 fild" &&
450 echo "100644 $O 1 file" &&
451 echo "100644 $A 2 file" &&
452 echo "100644 $B 3 file" &&
453 echo "100644 $A 0 filf"
eac5a401
JH
454 ) | git update-index --index-info
455}
456
457test_expect_success 'checkout an unmerged path should fail' '
458 setup_conflicting_index &&
8fdcf312
JH
459 echo "none of the above" >sample &&
460 cat sample >fild &&
461 cat sample >file &&
462 cat sample >filf &&
463 test_must_fail git checkout fild file filf &&
464 test_cmp sample fild &&
465 test_cmp sample filf &&
466 test_cmp sample file
467'
468
db941099 469test_expect_success 'checkout with an unmerged path can be ignored' '
eac5a401 470 setup_conflicting_index &&
db941099
JH
471 echo "none of the above" >sample &&
472 echo ourside >expect &&
473 cat sample >fild &&
474 cat sample >file &&
475 cat sample >filf &&
476 git checkout -f fild file filf &&
477 test_cmp expect fild &&
478 test_cmp expect filf &&
479 test_cmp sample file
480'
481
38901a48 482test_expect_success 'checkout unmerged stage' '
eac5a401 483 setup_conflicting_index &&
38901a48
JH
484 echo "none of the above" >sample &&
485 echo ourside >expect &&
486 cat sample >fild &&
487 cat sample >file &&
488 cat sample >filf &&
489 git checkout --ours . &&
490 test_cmp expect fild &&
491 test_cmp expect filf &&
492 test_cmp expect file &&
493 git checkout --theirs file &&
494 test ztheirside = "z$(cat file)"
495'
496
0cf8581e 497test_expect_success 'checkout with --merge' '
eac5a401 498 setup_conflicting_index &&
0cf8581e
JH
499 echo "none of the above" >sample &&
500 echo ourside >expect &&
501 cat sample >fild &&
502 cat sample >file &&
503 cat sample >filf &&
504 git checkout -m -- fild file filf &&
505 (
e974e06d
ES
506 echo "<<<<<<< ours" &&
507 echo ourside &&
508 echo "=======" &&
509 echo theirside &&
0cf8581e
JH
510 echo ">>>>>>> theirs"
511 ) >merged &&
512 test_cmp expect fild &&
513 test_cmp expect filf &&
514 test_cmp merged file
515'
516
517test_expect_success 'checkout with --merge, in diff3 -m style' '
518 git config merge.conflictstyle diff3 &&
eac5a401 519 setup_conflicting_index &&
0cf8581e
JH
520 echo "none of the above" >sample &&
521 echo ourside >expect &&
522 cat sample >fild &&
523 cat sample >file &&
524 cat sample >filf &&
525 git checkout -m -- fild file filf &&
526 (
e974e06d
ES
527 echo "<<<<<<< ours" &&
528 echo ourside &&
529 echo "||||||| base" &&
530 echo original &&
531 echo "=======" &&
532 echo theirside &&
0cf8581e
JH
533 echo ">>>>>>> theirs"
534 ) >merged &&
535 test_cmp expect fild &&
536 test_cmp expect filf &&
537 test_cmp merged file
538'
539
eac5a401
JH
540test_expect_success 'checkout --conflict=merge, overriding config' '
541 git config merge.conflictstyle diff3 &&
542 setup_conflicting_index &&
543 echo "none of the above" >sample &&
544 echo ourside >expect &&
545 cat sample >fild &&
546 cat sample >file &&
547 cat sample >filf &&
548 git checkout --conflict=merge -- fild file filf &&
549 (
e974e06d
ES
550 echo "<<<<<<< ours" &&
551 echo ourside &&
552 echo "=======" &&
553 echo theirside &&
eac5a401
JH
554 echo ">>>>>>> theirs"
555 ) >merged &&
556 test_cmp expect fild &&
557 test_cmp expect filf &&
558 test_cmp merged file
559'
560
561test_expect_success 'checkout --conflict=diff3' '
a6a4a88a 562 test_unconfig merge.conflictstyle &&
eac5a401
JH
563 setup_conflicting_index &&
564 echo "none of the above" >sample &&
565 echo ourside >expect &&
566 cat sample >fild &&
567 cat sample >file &&
568 cat sample >filf &&
569 git checkout --conflict=diff3 -- fild file filf &&
570 (
e974e06d
ES
571 echo "<<<<<<< ours" &&
572 echo ourside &&
573 echo "||||||| base" &&
574 echo original &&
575 echo "=======" &&
576 echo theirside &&
eac5a401
JH
577 echo ">>>>>>> theirs"
578 ) >merged &&
579 test_cmp expect fild &&
580 test_cmp expect filf &&
581 test_cmp merged file
582'
583
352eadc4
DB
584test_expect_success 'failing checkout -b should not break working tree' '
585 git reset --hard master &&
586 git symbolic-ref HEAD refs/heads/master &&
587 test_must_fail git checkout -b renamer side^ &&
588 test $(git symbolic-ref HEAD) = refs/heads/master &&
589 git diff --exit-code &&
590 git diff --cached --exit-code
352eadc4
DB
591'
592
e4b09dad
NS
593test_expect_success 'switch out of non-branch' '
594 git reset --hard master &&
595 git checkout master^0 &&
596 echo modified >one &&
597 test_must_fail git checkout renamer 2>error.log &&
598 ! grep "^Previous HEAD" error.log
599'
600
6d6f9acc
JH
601(
602 echo "#!$SHELL_PATH"
603 cat <<\EOF
604O=$1 A=$2 B=$3
605cat "$A" >.tmp
606exec >"$A"
607echo '<<<<<<< filfre-theirs'
608cat "$B"
609echo '||||||| filfre-common'
610cat "$O"
611echo '======='
612cat ".tmp"
613echo '>>>>>>> filfre-ours'
614rm -f .tmp
615exit 1
616EOF
617) >filfre.sh
618chmod +x filfre.sh
619
620test_expect_success 'custom merge driver with checkout -m' '
621 git reset --hard &&
622
623 git config merge.filfre.driver "./filfre.sh %O %A %B" &&
624 git config merge.filfre.name "Feel-free merge driver" &&
625 git config merge.filfre.recursive binary &&
626 echo "arm merge=filfre" >.gitattributes &&
627
628 git checkout -b left &&
629 echo neutral >arm &&
630 git add arm .gitattributes &&
631 test_tick &&
632 git commit -m neutral &&
633 git branch right &&
634
635 echo left >arm &&
636 test_tick &&
637 git commit -a -m left &&
638 git checkout right &&
639
640 echo right >arm &&
641 test_tick &&
642 git commit -a -m right &&
643
644 test_must_fail git merge left &&
645 (
646 for t in filfre-common left right
647 do
648 grep $t arm || exit 1
649 done
6d6f9acc
JH
650 ) &&
651
652 mv arm expect &&
653 git checkout -m arm &&
654 test_cmp expect arm
655'
656
abc02670 657test_done