Sync with 'maint'
[alt-git.git] / t / t4200-rerere.sh
blob213b36fb96242b4e0ed1a59457a94b4fda5ed7a5
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
6 test_description='git rerere
8 ! [fifth] version1
9 ! [first] first
10 ! [fourth] version1
11 ! [main] initial
12 ! [second] prefer first over second
13 ! [third] version2
14 ------
15 + [third] version2
16 + [fifth] version1
17 + [fourth] version1
18 + + + [third^] third
19 - [second] prefer first over second
20 + + [first] first
21 + [second^] second
22 ++++++ [main] initial
25 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
26 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
28 TEST_PASSES_SANITIZE_LEAK=true
29 . ./test-lib.sh
31 test_expect_success 'setup' '
32 cat >a1 <<-\EOF &&
33 Some title
34 ==========
35 Whether '\''tis nobler in the mind to suffer
36 The slings and arrows of outrageous fortune,
37 Or to take arms against a sea of troubles,
38 And by opposing end them? To die: to sleep;
39 No more; and by a sleep to say we end
40 The heart-ache and the thousand natural shocks
41 That flesh is heir to, '\''tis a consummation
42 Devoutly to be wish'\''d.
43 EOF
45 git add a1 &&
46 test_tick &&
47 git commit -q -a -m initial &&
49 cat >>a1 <<-\EOF &&
50 Some title
51 ==========
52 To die, to sleep;
53 To sleep: perchance to dream: ay, there'\''s the rub;
54 For in that sleep of death what dreams may come
55 When we have shuffled off this mortal coil,
56 Must give us pause: there'\''s the respect
57 That makes calamity of so long life;
58 EOF
60 git checkout -b first &&
61 test_tick &&
62 git commit -q -a -m first &&
64 git checkout -b second main &&
65 git show first:a1 |
66 sed -e "s/To die, t/To die! T/" -e "s/Some title/Some Title/" >a1 &&
67 echo "* END *" >>a1 &&
68 test_tick &&
69 git commit -q -a -m second
72 test_expect_success 'nothing recorded without rerere' '
73 rm -rf .git/rr-cache &&
74 git config rerere.enabled false &&
75 test_must_fail git merge first &&
76 ! test -d .git/rr-cache
79 test_expect_success 'activate rerere, old style (conflicting merge)' '
80 git reset --hard &&
81 mkdir .git/rr-cache &&
82 test_might_fail git config --unset rerere.enabled &&
83 test_must_fail git merge first &&
85 sha1=$(perl -pe "s/ .*//" .git/MERGE_RR) &&
86 rr=.git/rr-cache/$sha1 &&
87 grep "^=======\$" $rr/preimage &&
88 ! test -f $rr/postimage &&
89 ! test -f $rr/thisimage
92 test_expect_success 'rerere.enabled works, too' '
93 rm -rf .git/rr-cache &&
94 git config rerere.enabled true &&
95 git reset --hard &&
96 test_must_fail git merge first &&
98 sha1=$(perl -pe "s/ .*//" .git/MERGE_RR) &&
99 rr=.git/rr-cache/$sha1 &&
100 grep ^=======$ $rr/preimage
103 test_expect_success 'set up rr-cache' '
104 rm -rf .git/rr-cache &&
105 git config rerere.enabled true &&
106 git reset --hard &&
107 test_must_fail git merge first &&
108 sha1=$(perl -pe "s/ .*//" .git/MERGE_RR) &&
109 rr=.git/rr-cache/$sha1
112 test_expect_success 'rr-cache looks sane' '
113 # no postimage or thisimage yet
114 ! test -f $rr/postimage &&
115 ! test -f $rr/thisimage &&
117 # preimage has right number of lines
118 cnt=$(sed -ne "/^<<<<<<</,/^>>>>>>>/p" $rr/preimage | wc -l) &&
119 echo $cnt &&
120 test $cnt = 13
123 test_expect_success 'rerere diff' '
124 git show first:a1 >a1 &&
125 cat >expect <<-\EOF &&
126 --- a/a1
127 +++ b/a1
128 @@ -1,4 +1,4 @@
129 -Some Title
130 +Some title
131 ==========
132 Whether '\''tis nobler in the mind to suffer
133 The slings and arrows of outrageous fortune,
134 @@ -8,21 +8,11 @@
135 The heart-ache and the thousand natural shocks
136 That flesh is heir to, '\''tis a consummation
137 Devoutly to be wish'\''d.
138 -<<<<<<<
139 -Some Title
140 -==========
141 -To die! To sleep;
142 -=======
143 Some title
144 ==========
145 To die, to sleep;
146 ->>>>>>>
147 To sleep: perchance to dream: ay, there'\''s the rub;
148 For in that sleep of death what dreams may come
149 When we have shuffled off this mortal coil,
150 Must give us pause: there'\''s the respect
151 That makes calamity of so long life;
152 -<<<<<<<
153 -=======
154 -* END *
155 ->>>>>>>
157 git rerere diff >out &&
158 test_cmp expect out
161 test_expect_success 'rerere status' '
162 echo a1 >expect &&
163 git rerere status >out &&
164 test_cmp expect out
167 test_expect_success 'first postimage wins' '
168 git show first:a1 | sed "s/To die: t/To die! T/" >expect &&
170 git commit -q -a -m "prefer first over second" &&
171 test -f $rr/postimage &&
173 oldmtimepost=$(test-tool chmtime --get -60 $rr/postimage) &&
175 git checkout -b third main &&
176 git show second^:a1 | sed "s/To die: t/To die! T/" >a1 &&
177 git commit -q -a -m third &&
179 test_must_fail git merge first &&
180 # rerere kicked in
181 ! grep "^=======\$" a1 &&
182 test_cmp expect a1
185 test_expect_success 'rerere updates postimage timestamp' '
186 newmtimepost=$(test-tool chmtime --get $rr/postimage) &&
187 test $oldmtimepost -lt $newmtimepost
190 test_expect_success 'rerere clear' '
191 mv $rr/postimage .git/post-saved &&
192 echo "$sha1 a1" | perl -pe "y/\012/\000/" >.git/MERGE_RR &&
193 git rerere clear &&
194 ! test -d $rr
197 test_expect_success 'leftover directory' '
198 git reset --hard &&
199 mkdir -p $rr &&
200 test_must_fail git merge first &&
201 test -f $rr/preimage
204 test_expect_success 'missing preimage' '
205 git reset --hard &&
206 mkdir -p $rr &&
207 cp .git/post-saved $rr/postimage &&
208 test_must_fail git merge first &&
209 test -f $rr/preimage
212 test_expect_success 'set up for garbage collection tests' '
213 mkdir -p $rr &&
214 echo Hello >$rr/preimage &&
215 echo World >$rr/postimage &&
217 sha2=$(test_oid deadbeef) &&
218 rr2=.git/rr-cache/$sha2 &&
219 mkdir $rr2 &&
220 echo Hello >$rr2/preimage &&
222 almost_15_days_ago=$((60-15*86400)) &&
223 just_over_15_days_ago=$((-1-15*86400)) &&
224 almost_60_days_ago=$((60-60*86400)) &&
225 just_over_60_days_ago=$((-1-60*86400)) &&
227 test-tool chmtime =$just_over_60_days_ago $rr/preimage &&
228 test-tool chmtime =$almost_60_days_ago $rr/postimage &&
229 test-tool chmtime =$almost_15_days_ago $rr2/preimage
232 test_expect_success 'gc preserves young or recently used records' '
233 git rerere gc &&
234 test -f $rr/preimage &&
235 test -f $rr2/preimage
238 test_expect_success 'old records rest in peace' '
239 test-tool chmtime =$just_over_60_days_ago $rr/postimage &&
240 test-tool chmtime =$just_over_15_days_ago $rr2/preimage &&
241 git rerere gc &&
242 ! test -f $rr/preimage &&
243 ! test -f $rr2/preimage
246 rerere_gc_custom_expiry_test () {
247 five_days="$1" right_now="$2"
248 test_expect_success "rerere gc with custom expiry ($five_days, $right_now)" '
249 rm -fr .git/rr-cache &&
250 rr=.git/rr-cache/$ZERO_OID &&
251 mkdir -p "$rr" &&
252 >"$rr/preimage" &&
253 >"$rr/postimage" &&
255 two_days_ago=$((-2*86400)) &&
256 test-tool chmtime =$two_days_ago "$rr/preimage" &&
257 test-tool chmtime =$two_days_ago "$rr/postimage" &&
259 find .git/rr-cache -type f | sort >original &&
261 git -c "gc.rerereresolved=$five_days" \
262 -c "gc.rerereunresolved=$five_days" rerere gc &&
263 find .git/rr-cache -type f | sort >actual &&
264 test_cmp original actual &&
266 git -c "gc.rerereresolved=$five_days" \
267 -c "gc.rerereunresolved=$right_now" rerere gc &&
268 find .git/rr-cache -type f | sort >actual &&
269 test_cmp original actual &&
271 git -c "gc.rerereresolved=$right_now" \
272 -c "gc.rerereunresolved=$right_now" rerere gc &&
273 find .git/rr-cache -type f | sort >actual &&
274 test_must_be_empty actual
278 rerere_gc_custom_expiry_test 5 0
280 rerere_gc_custom_expiry_test 5.days.ago now
282 test_expect_success 'setup: file2 added differently in two branches' '
283 git reset --hard &&
285 git checkout -b fourth &&
286 echo Hallo >file2 &&
287 git add file2 &&
288 test_tick &&
289 git commit -m version1 &&
291 git checkout third &&
292 echo Bello >file2 &&
293 git add file2 &&
294 test_tick &&
295 git commit -m version2 &&
297 test_must_fail git merge fourth &&
298 echo Cello >file2 &&
299 git add file2 &&
300 git commit -m resolution
303 test_expect_success 'resolution was recorded properly' '
304 echo Cello >expected &&
306 git reset --hard HEAD~2 &&
307 git checkout -b fifth &&
309 echo Hallo >file3 &&
310 git add file3 &&
311 test_tick &&
312 git commit -m version1 &&
314 git checkout third &&
315 echo Bello >file3 &&
316 git add file3 &&
317 test_tick &&
318 git commit -m version2 &&
319 git tag version2 &&
321 test_must_fail git merge fifth &&
322 test_cmp expected file3 &&
323 test_must_fail git update-index --refresh
326 test_expect_success 'rerere.autoupdate' '
327 git config rerere.autoupdate true &&
328 git reset --hard &&
329 git checkout version2 &&
330 test_must_fail git merge fifth &&
331 git update-index --refresh
334 test_expect_success 'merge --rerere-autoupdate' '
335 test_might_fail git config --unset rerere.autoupdate &&
336 git reset --hard &&
337 git checkout version2 &&
338 test_must_fail git merge --rerere-autoupdate fifth &&
339 git update-index --refresh
342 test_expect_success 'merge --no-rerere-autoupdate' '
343 headblob=$(git rev-parse version2:file3) &&
344 mergeblob=$(git rev-parse fifth:file3) &&
345 cat >expected <<-EOF &&
346 100644 $headblob 2 file3
347 100644 $mergeblob 3 file3
350 git config rerere.autoupdate true &&
351 git reset --hard &&
352 git checkout version2 &&
353 test_must_fail git merge --no-rerere-autoupdate fifth &&
354 git ls-files -u >actual &&
355 test_cmp expected actual
358 test_expect_success 'set up an unresolved merge' '
359 headblob=$(git rev-parse version2:file3) &&
360 mergeblob=$(git rev-parse fifth:file3) &&
361 cat >expected.unresolved <<-EOF &&
362 100644 $headblob 2 file3
363 100644 $mergeblob 3 file3
366 test_might_fail git config --unset rerere.autoupdate &&
367 git reset --hard &&
368 git checkout version2 &&
369 ancestor=$(git merge-base version2 fifth) &&
370 test_must_fail git merge-recursive "$ancestor" -- HEAD fifth &&
372 git ls-files --stage >failedmerge &&
373 cp file3 file3.conflict &&
375 git ls-files -u >actual &&
376 test_cmp expected.unresolved actual
379 test_expect_success 'explicit rerere' '
380 test_might_fail git config --unset rerere.autoupdate &&
381 git rm -fr --cached . &&
382 git update-index --index-info <failedmerge &&
383 cp file3.conflict file3 &&
384 test_must_fail git update-index --refresh -q &&
386 git rerere &&
387 git ls-files -u >actual &&
388 test_cmp expected.unresolved actual
391 test_expect_success 'explicit rerere with autoupdate' '
392 git config rerere.autoupdate true &&
393 git rm -fr --cached . &&
394 git update-index --index-info <failedmerge &&
395 cp file3.conflict file3 &&
396 test_must_fail git update-index --refresh -q &&
398 git rerere &&
399 git update-index --refresh
402 test_expect_success 'explicit rerere --rerere-autoupdate overrides' '
403 git config rerere.autoupdate false &&
404 git rm -fr --cached . &&
405 git update-index --index-info <failedmerge &&
406 cp file3.conflict file3 &&
407 git rerere &&
408 git ls-files -u >actual1 &&
410 git rm -fr --cached . &&
411 git update-index --index-info <failedmerge &&
412 cp file3.conflict file3 &&
413 git rerere --rerere-autoupdate &&
414 git update-index --refresh &&
416 git rm -fr --cached . &&
417 git update-index --index-info <failedmerge &&
418 cp file3.conflict file3 &&
419 git rerere --rerere-autoupdate --no-rerere-autoupdate &&
420 git ls-files -u >actual2 &&
422 git rm -fr --cached . &&
423 git update-index --index-info <failedmerge &&
424 cp file3.conflict file3 &&
425 git rerere --rerere-autoupdate --no-rerere-autoupdate --rerere-autoupdate &&
426 git update-index --refresh &&
428 test_cmp expected.unresolved actual1 &&
429 test_cmp expected.unresolved actual2
432 test_expect_success 'rerere --no-no-rerere-autoupdate' '
433 git rm -fr --cached . &&
434 git update-index --index-info <failedmerge &&
435 cp file3.conflict file3 &&
436 test_must_fail git rerere --no-no-rerere-autoupdate 2>err &&
437 test_grep [Uu]sage err &&
438 test_must_fail git update-index --refresh
441 test_expect_success 'rerere -h' '
442 test_must_fail git rerere -h >help &&
443 test_grep [Uu]sage help
446 concat_insert () {
447 last=$1
448 shift
449 cat early && printf "%s\n" "$@" && cat late "$last"
452 count_pre_post () {
453 find .git/rr-cache/ -type f -name "preimage*" >actual &&
454 test_line_count = "$1" actual &&
455 find .git/rr-cache/ -type f -name "postimage*" >actual &&
456 test_line_count = "$2" actual
459 merge_conflict_resolve () {
460 git reset --hard &&
461 test_must_fail git merge six.1 &&
462 # Resolution is to replace 7 with 6.1 and 6.2 (i.e. take both)
463 concat_insert short 6.1 6.2 >file1 &&
464 concat_insert long 6.1 6.2 >file2
467 test_expect_success 'multiple identical conflicts' '
468 rm -fr .git/rr-cache &&
469 mkdir .git/rr-cache &&
470 git reset --hard &&
472 test_seq 1 6 >early &&
473 >late &&
474 test_seq 11 15 >short &&
475 test_seq 111 120 >long &&
476 concat_insert short >file1 &&
477 concat_insert long >file2 &&
478 git add file1 file2 &&
479 git commit -m base &&
480 git tag base &&
481 git checkout -b six.1 &&
482 concat_insert short 6.1 >file1 &&
483 concat_insert long 6.1 >file2 &&
484 git add file1 file2 &&
485 git commit -m 6.1 &&
486 git checkout -b six.2 HEAD^ &&
487 concat_insert short 6.2 >file1 &&
488 concat_insert long 6.2 >file2 &&
489 git add file1 file2 &&
490 git commit -m 6.2 &&
492 # At this point, six.1 and six.2
493 # - derive from common ancestor that has two files
494 # 1...6 7 11..15 (file1) and 1...6 7 111..120 (file2)
495 # - six.1 replaces these 7s with 6.1
496 # - six.2 replaces these 7s with 6.2
498 merge_conflict_resolve &&
500 # Check that rerere knows that file1 and file2 have conflicts
502 printf "%s\n" file1 file2 >expect &&
503 git ls-files -u | sed -e "s/^.* //" | sort -u >actual &&
504 test_cmp expect actual &&
506 git rerere status | sort >actual &&
507 test_cmp expect actual &&
509 git rerere remaining >actual &&
510 test_cmp expect actual &&
512 count_pre_post 2 0 &&
514 # Pretend that the conflicts were made quite some time ago
515 test-tool chmtime -172800 $(find .git/rr-cache/ -type f) &&
517 # Unresolved entries have not expired yet
518 git -c gc.rerereresolved=5 -c gc.rerereunresolved=5 rerere gc &&
519 count_pre_post 2 0 &&
521 # Unresolved entries have expired
522 git -c gc.rerereresolved=5 -c gc.rerereunresolved=1 rerere gc &&
523 count_pre_post 0 0 &&
525 # Recreate the conflicted state
526 merge_conflict_resolve &&
527 count_pre_post 2 0 &&
529 # Clear it
530 git rerere clear &&
531 count_pre_post 0 0 &&
533 # Recreate the conflicted state
534 merge_conflict_resolve &&
535 count_pre_post 2 0 &&
537 # We resolved file1 and file2
538 git rerere &&
539 git rerere remaining >actual &&
540 test_must_be_empty actual &&
542 # We must have recorded both of them
543 count_pre_post 2 2 &&
545 # Now we should be able to resolve them both
546 git reset --hard &&
547 test_must_fail git merge six.1 &&
548 git rerere &&
550 git rerere remaining >actual &&
551 test_must_be_empty actual &&
553 concat_insert short 6.1 6.2 >file1.expect &&
554 concat_insert long 6.1 6.2 >file2.expect &&
555 test_cmp file1.expect file1 &&
556 test_cmp file2.expect file2 &&
558 # Forget resolution for file2
559 git rerere forget file2 &&
560 echo file2 >expect &&
561 git rerere status >actual &&
562 test_cmp expect actual &&
563 count_pre_post 2 1 &&
565 # file2 already has correct resolution, so record it again
566 git rerere &&
568 # Pretend that the resolutions are old again
569 test-tool chmtime -172800 $(find .git/rr-cache/ -type f) &&
571 # Resolved entries have not expired yet
572 git -c gc.rerereresolved=5 -c gc.rerereunresolved=5 rerere gc &&
574 count_pre_post 2 2 &&
576 # Resolved entries have expired
577 git -c gc.rerereresolved=1 -c gc.rerereunresolved=5 rerere gc &&
578 count_pre_post 0 0
581 test_expect_success 'rerere with unexpected conflict markers does not crash' '
582 git reset --hard &&
584 git checkout -b branch-1 main &&
585 echo "bar" >test &&
586 git add test &&
587 git commit -q -m two &&
589 git reset --hard &&
590 git checkout -b branch-2 main &&
591 echo "foo" >test &&
592 git add test &&
593 git commit -q -a -m one &&
595 test_must_fail git merge branch-1 &&
596 echo "<<<<<<< a" >test &&
597 git rerere &&
599 git rerere clear
602 test_expect_success 'rerere with inner conflict markers' '
603 git reset --hard &&
605 git checkout -b A main &&
606 echo "bar" >test &&
607 git add test &&
608 git commit -q -m two &&
609 echo "baz" >test &&
610 git add test &&
611 git commit -q -m three &&
613 git reset --hard &&
614 git checkout -b B main &&
615 echo "foo" >test &&
616 git add test &&
617 git commit -q -a -m one &&
619 test_must_fail git merge A~ &&
620 git add test &&
621 git commit -q -m "will solve conflicts later" &&
622 test_must_fail git merge A &&
624 echo "resolved" >test &&
625 git add test &&
626 git commit -q -m "solved conflict" &&
628 echo "resolved" >expect &&
630 git reset --hard HEAD~~ &&
631 test_must_fail git merge A~ &&
632 git add test &&
633 git commit -q -m "will solve conflicts later" &&
634 test_must_fail git merge A &&
635 cat test >actual &&
636 test_cmp expect actual &&
638 git add test &&
639 git commit -m "rerere solved conflict" &&
640 git reset --hard HEAD~ &&
641 test_must_fail git merge A &&
642 cat test >actual &&
643 test_cmp expect actual
646 test_expect_success 'setup simple stage 1 handling' '
647 test_create_repo stage_1_handling &&
649 cd stage_1_handling &&
651 test_seq 1 10 >original &&
652 git add original &&
653 git commit -m original &&
655 git checkout -b A main &&
656 git mv original A &&
657 git commit -m "rename to A" &&
659 git checkout -b B main &&
660 git mv original B &&
661 git commit -m "rename to B"
665 test_expect_success 'test simple stage 1 handling' '
667 cd stage_1_handling &&
669 git config rerere.enabled true &&
670 git checkout A^0 &&
671 test_must_fail git merge B^0
675 test_expect_success 'rerere does not crash with missing preimage' '
676 git config rerere.enabled true &&
678 echo bar >test &&
679 git add test &&
680 git commit -m "one" &&
681 git branch rerere_no_crash &&
683 echo foo >>test &&
684 git add test &&
685 git commit -m "two" &&
687 git checkout rerere_no_crash &&
688 echo "bar" >>test &&
689 git add test &&
690 git commit -m "three" &&
692 test_must_fail git rebase main &&
693 rm .git/rr-cache/*/preimage &&
694 git rebase --abort
697 test_expect_success 'rerere does not crash with unmatched conflict marker' '
698 git config rerere.enabled true &&
700 echo bar >test &&
701 git add test &&
702 git commit -m "one" &&
703 git branch rerere_no_preimage &&
705 cat >test <<-EOF &&
706 test
708 foobar
710 git add test &&
711 git commit -m "two" &&
713 git checkout rerere_no_preimage &&
714 echo "bar" >>test &&
715 git add test &&
716 git commit -m "three" &&
718 cat >test <<-EOF &&
719 foobar
723 git add test &&
724 git commit -m "four" &&
726 test_must_fail git rebase main &&
727 cat >test <<-EOF &&
728 test
730 <<<<<<< HEAD
731 foobar
734 git add test &&
735 test_must_fail git rebase --continue
738 test_done