t / t4200-rerere.shon commit t4200: parameterize "rerere gc" custom expiry test (e579aaa)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Johannes E. Schindelin
   4#
   5
   6test_description='git rerere
   7
   8! [fifth] version1
   9 ! [first] first
  10  ! [fourth] version1
  11   ! [master] 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++++++ [master] initial
  23'
  24
  25. ./test-lib.sh
  26
  27test_expect_success 'setup' '
  28        cat >a1 <<-\EOF &&
  29        Some title
  30        ==========
  31        Whether '\''tis nobler in the mind to suffer
  32        The slings and arrows of outrageous fortune,
  33        Or to take arms against a sea of troubles,
  34        And by opposing end them? To die: to sleep;
  35        No more; and by a sleep to say we end
  36        The heart-ache and the thousand natural shocks
  37        That flesh is heir to, '\''tis a consummation
  38        Devoutly to be wish'\''d.
  39        EOF
  40
  41        git add a1 &&
  42        test_tick &&
  43        git commit -q -a -m initial &&
  44
  45        cat >>a1 <<-\EOF &&
  46        Some title
  47        ==========
  48        To die, to sleep;
  49        To sleep: perchance to dream: ay, there'\''s the rub;
  50        For in that sleep of death what dreams may come
  51        When we have shuffled off this mortal coil,
  52        Must give us pause: there'\''s the respect
  53        That makes calamity of so long life;
  54        EOF
  55
  56        git checkout -b first &&
  57        test_tick &&
  58        git commit -q -a -m first &&
  59
  60        git checkout -b second master &&
  61        git show first:a1 |
  62        sed -e "s/To die, t/To die! T/" -e "s/Some title/Some Title/" >a1 &&
  63        echo "* END *" >>a1 &&
  64        test_tick &&
  65        git commit -q -a -m second
  66'
  67
  68test_expect_success 'nothing recorded without rerere' '
  69        rm -rf .git/rr-cache &&
  70        git config rerere.enabled false &&
  71        test_must_fail git merge first &&
  72        ! test -d .git/rr-cache
  73'
  74
  75test_expect_success 'activate rerere, old style (conflicting merge)' '
  76        git reset --hard &&
  77        mkdir .git/rr-cache &&
  78        test_might_fail git config --unset rerere.enabled &&
  79        test_must_fail git merge first &&
  80
  81        sha1=$(perl -pe "s/     .*//" .git/MERGE_RR) &&
  82        rr=.git/rr-cache/$sha1 &&
  83        grep "^=======\$" $rr/preimage &&
  84        ! test -f $rr/postimage &&
  85        ! test -f $rr/thisimage
  86'
  87
  88test_expect_success 'rerere.enabled works, too' '
  89        rm -rf .git/rr-cache &&
  90        git config rerere.enabled true &&
  91        git reset --hard &&
  92        test_must_fail git merge first &&
  93
  94        sha1=$(perl -pe "s/     .*//" .git/MERGE_RR) &&
  95        rr=.git/rr-cache/$sha1 &&
  96        grep ^=======$ $rr/preimage
  97'
  98
  99test_expect_success 'set up rr-cache' '
 100        rm -rf .git/rr-cache &&
 101        git config rerere.enabled true &&
 102        git reset --hard &&
 103        test_must_fail git merge first &&
 104        sha1=$(perl -pe "s/     .*//" .git/MERGE_RR) &&
 105        rr=.git/rr-cache/$sha1
 106'
 107
 108test_expect_success 'rr-cache looks sane' '
 109        # no postimage or thisimage yet
 110        ! test -f $rr/postimage &&
 111        ! test -f $rr/thisimage &&
 112
 113        # preimage has right number of lines
 114        cnt=$(sed -ne "/^<<<<<<</,/^>>>>>>>/p" $rr/preimage | wc -l) &&
 115        echo $cnt &&
 116        test $cnt = 13
 117'
 118
 119test_expect_success 'rerere diff' '
 120        git show first:a1 >a1 &&
 121        cat >expect <<-\EOF &&
 122        --- a/a1
 123        +++ b/a1
 124        @@ -1,4 +1,4 @@
 125        -Some Title
 126        +Some title
 127         ==========
 128         Whether '\''tis nobler in the mind to suffer
 129         The slings and arrows of outrageous fortune,
 130        @@ -8,21 +8,11 @@
 131         The heart-ache and the thousand natural shocks
 132         That flesh is heir to, '\''tis a consummation
 133         Devoutly to be wish'\''d.
 134        -<<<<<<<
 135        -Some Title
 136        -==========
 137        -To die! To sleep;
 138        -=======
 139         Some title
 140         ==========
 141         To die, to sleep;
 142        ->>>>>>>
 143         To sleep: perchance to dream: ay, there'\''s the rub;
 144         For in that sleep of death what dreams may come
 145         When we have shuffled off this mortal coil,
 146         Must give us pause: there'\''s the respect
 147         That makes calamity of so long life;
 148        -<<<<<<<
 149        -=======
 150        -* END *
 151        ->>>>>>>
 152        EOF
 153        git rerere diff >out &&
 154        test_cmp expect out
 155'
 156
 157test_expect_success 'rerere status' '
 158        echo a1 >expect &&
 159        git rerere status >out &&
 160        test_cmp expect out
 161'
 162
 163test_expect_success 'first postimage wins' '
 164        git show first:a1 | sed "s/To die: t/To die! T/" >expect &&
 165
 166        git commit -q -a -m "prefer first over second" &&
 167        test -f $rr/postimage &&
 168
 169        oldmtimepost=$(test-chmtime -v -60 $rr/postimage | cut -f 1) &&
 170
 171        git checkout -b third master &&
 172        git show second^:a1 | sed "s/To die: t/To die! T/" >a1 &&
 173        git commit -q -a -m third &&
 174
 175        test_must_fail git merge first &&
 176        # rerere kicked in
 177        ! grep "^=======\$" a1 &&
 178        test_cmp expect a1
 179'
 180
 181test_expect_success 'rerere updates postimage timestamp' '
 182        newmtimepost=$(test-chmtime -v +0 $rr/postimage | cut -f 1) &&
 183        test $oldmtimepost -lt $newmtimepost
 184'
 185
 186test_expect_success 'rerere clear' '
 187        mv $rr/postimage .git/post-saved &&
 188        echo "$sha1     a1" | perl -pe "y/\012/\000/" >.git/MERGE_RR &&
 189        git rerere clear &&
 190        ! test -d $rr
 191'
 192
 193test_expect_success 'leftover directory' '
 194        git reset --hard &&
 195        mkdir -p $rr &&
 196        test_must_fail git merge first &&
 197        test -f $rr/preimage
 198'
 199
 200test_expect_success 'missing preimage' '
 201        git reset --hard &&
 202        mkdir -p $rr &&
 203        cp .git/post-saved $rr/postimage &&
 204        test_must_fail git merge first &&
 205        test -f $rr/preimage
 206'
 207
 208test_expect_success 'set up for garbage collection tests' '
 209        mkdir -p $rr &&
 210        echo Hello >$rr/preimage &&
 211        echo World >$rr/postimage &&
 212
 213        sha2=4000000000000000000000000000000000000000 &&
 214        rr2=.git/rr-cache/$sha2 &&
 215        mkdir $rr2 &&
 216        echo Hello >$rr2/preimage &&
 217
 218        almost_15_days_ago=$((60-15*86400)) &&
 219        just_over_15_days_ago=$((-1-15*86400)) &&
 220        almost_60_days_ago=$((60-60*86400)) &&
 221        just_over_60_days_ago=$((-1-60*86400)) &&
 222
 223        test-chmtime =$just_over_60_days_ago $rr/preimage &&
 224        test-chmtime =$almost_60_days_ago $rr/postimage &&
 225        test-chmtime =$almost_15_days_ago $rr2/preimage
 226'
 227
 228test_expect_success 'gc preserves young or recently used records' '
 229        git rerere gc &&
 230        test -f $rr/preimage &&
 231        test -f $rr2/preimage
 232'
 233
 234test_expect_success 'old records rest in peace' '
 235        test-chmtime =$just_over_60_days_ago $rr/postimage &&
 236        test-chmtime =$just_over_15_days_ago $rr2/preimage &&
 237        git rerere gc &&
 238        ! test -f $rr/preimage &&
 239        ! test -f $rr2/preimage
 240'
 241
 242rerere_gc_custom_expiry_test () {
 243        five_days="$1" right_now="$2"
 244        test_expect_success "rerere gc with custom expiry ($five_days, $right_now)" '
 245                rm -fr .git/rr-cache &&
 246                rr=.git/rr-cache/$_z40 &&
 247                mkdir -p "$rr" &&
 248                >"$rr/preimage" &&
 249                >"$rr/postimage" &&
 250
 251                two_days_ago=$((-2*86400)) &&
 252                test-chmtime =$two_days_ago "$rr/preimage" &&
 253                test-chmtime =$two_days_ago "$rr/postimage" &&
 254
 255                find .git/rr-cache -type f | sort >original &&
 256
 257                git -c "gc.rerereresolved=$five_days" \
 258                    -c "gc.rerereunresolved=$five_days" rerere gc &&
 259                find .git/rr-cache -type f | sort >actual &&
 260                test_cmp original actual &&
 261
 262                git -c "gc.rerereresolved=$five_days" \
 263                    -c "gc.rerereunresolved=$right_now" rerere gc &&
 264                find .git/rr-cache -type f | sort >actual &&
 265                test_cmp original actual &&
 266
 267                git -c "gc.rerereresolved=$right_now" \
 268                    -c "gc.rerereunresolved=$right_now" rerere gc &&
 269                find .git/rr-cache -type f | sort >actual &&
 270                >expect &&
 271                test_cmp expect actual
 272        '
 273}
 274
 275rerere_gc_custom_expiry_test 5 0
 276
 277test_expect_success 'setup: file2 added differently in two branches' '
 278        git reset --hard &&
 279
 280        git checkout -b fourth &&
 281        echo Hallo >file2 &&
 282        git add file2 &&
 283        test_tick &&
 284        git commit -m version1 &&
 285
 286        git checkout third &&
 287        echo Bello >file2 &&
 288        git add file2 &&
 289        test_tick &&
 290        git commit -m version2 &&
 291
 292        test_must_fail git merge fourth &&
 293        echo Cello >file2 &&
 294        git add file2 &&
 295        git commit -m resolution
 296'
 297
 298test_expect_success 'resolution was recorded properly' '
 299        echo Cello >expected &&
 300
 301        git reset --hard HEAD~2 &&
 302        git checkout -b fifth &&
 303
 304        echo Hallo >file3 &&
 305        git add file3 &&
 306        test_tick &&
 307        git commit -m version1 &&
 308
 309        git checkout third &&
 310        echo Bello >file3 &&
 311        git add file3 &&
 312        test_tick &&
 313        git commit -m version2 &&
 314        git tag version2 &&
 315
 316        test_must_fail git merge fifth &&
 317        test_cmp expected file3 &&
 318        test_must_fail git update-index --refresh
 319'
 320
 321test_expect_success 'rerere.autoupdate' '
 322        git config rerere.autoupdate true &&
 323        git reset --hard &&
 324        git checkout version2 &&
 325        test_must_fail git merge fifth &&
 326        git update-index --refresh
 327'
 328
 329test_expect_success 'merge --rerere-autoupdate' '
 330        test_might_fail git config --unset rerere.autoupdate &&
 331        git reset --hard &&
 332        git checkout version2 &&
 333        test_must_fail git merge --rerere-autoupdate fifth &&
 334        git update-index --refresh
 335'
 336
 337test_expect_success 'merge --no-rerere-autoupdate' '
 338        headblob=$(git rev-parse version2:file3) &&
 339        mergeblob=$(git rev-parse fifth:file3) &&
 340        cat >expected <<-EOF &&
 341        100644 $headblob 2      file3
 342        100644 $mergeblob 3     file3
 343        EOF
 344
 345        git config rerere.autoupdate true &&
 346        git reset --hard &&
 347        git checkout version2 &&
 348        test_must_fail git merge --no-rerere-autoupdate fifth &&
 349        git ls-files -u >actual &&
 350        test_cmp expected actual
 351'
 352
 353test_expect_success 'set up an unresolved merge' '
 354        headblob=$(git rev-parse version2:file3) &&
 355        mergeblob=$(git rev-parse fifth:file3) &&
 356        cat >expected.unresolved <<-EOF &&
 357        100644 $headblob 2      file3
 358        100644 $mergeblob 3     file3
 359        EOF
 360
 361        test_might_fail git config --unset rerere.autoupdate &&
 362        git reset --hard &&
 363        git checkout version2 &&
 364        fifth=$(git rev-parse fifth) &&
 365        echo "$fifth            branch 'fifth' of ." |
 366        git fmt-merge-msg >msg &&
 367        ancestor=$(git merge-base version2 fifth) &&
 368        test_must_fail git merge-recursive "$ancestor" -- HEAD fifth &&
 369
 370        git ls-files --stage >failedmerge &&
 371        cp file3 file3.conflict &&
 372
 373        git ls-files -u >actual &&
 374        test_cmp expected.unresolved actual
 375'
 376
 377test_expect_success 'explicit rerere' '
 378        test_might_fail git config --unset rerere.autoupdate &&
 379        git rm -fr --cached . &&
 380        git update-index --index-info <failedmerge &&
 381        cp file3.conflict file3 &&
 382        test_must_fail git update-index --refresh -q &&
 383
 384        git rerere &&
 385        git ls-files -u >actual &&
 386        test_cmp expected.unresolved actual
 387'
 388
 389test_expect_success 'explicit rerere with autoupdate' '
 390        git config rerere.autoupdate true &&
 391        git rm -fr --cached . &&
 392        git update-index --index-info <failedmerge &&
 393        cp file3.conflict file3 &&
 394        test_must_fail git update-index --refresh -q &&
 395
 396        git rerere &&
 397        git update-index --refresh
 398'
 399
 400test_expect_success 'explicit rerere --rerere-autoupdate overrides' '
 401        git config rerere.autoupdate false &&
 402        git rm -fr --cached . &&
 403        git update-index --index-info <failedmerge &&
 404        cp file3.conflict file3 &&
 405        git rerere &&
 406        git ls-files -u >actual1 &&
 407
 408        git rm -fr --cached . &&
 409        git update-index --index-info <failedmerge &&
 410        cp file3.conflict file3 &&
 411        git rerere --rerere-autoupdate &&
 412        git update-index --refresh &&
 413
 414        git rm -fr --cached . &&
 415        git update-index --index-info <failedmerge &&
 416        cp file3.conflict file3 &&
 417        git rerere --rerere-autoupdate --no-rerere-autoupdate &&
 418        git ls-files -u >actual2 &&
 419
 420        git rm -fr --cached . &&
 421        git update-index --index-info <failedmerge &&
 422        cp file3.conflict file3 &&
 423        git rerere --rerere-autoupdate --no-rerere-autoupdate --rerere-autoupdate &&
 424        git update-index --refresh &&
 425
 426        test_cmp expected.unresolved actual1 &&
 427        test_cmp expected.unresolved actual2
 428'
 429
 430test_expect_success 'rerere --no-no-rerere-autoupdate' '
 431        git rm -fr --cached . &&
 432        git update-index --index-info <failedmerge &&
 433        cp file3.conflict file3 &&
 434        test_must_fail git rerere --no-no-rerere-autoupdate 2>err &&
 435        test_i18ngrep [Uu]sage err &&
 436        test_must_fail git update-index --refresh
 437'
 438
 439test_expect_success 'rerere -h' '
 440        test_must_fail git rerere -h >help &&
 441        test_i18ngrep [Uu]sage help
 442'
 443
 444concat_insert () {
 445        last=$1
 446        shift
 447        cat early && printf "%s\n" "$@" && cat late "$last"
 448}
 449
 450count_pre_post () {
 451        find .git/rr-cache/ -type f -name "preimage*" >actual &&
 452        test_line_count = "$1" actual &&
 453        find .git/rr-cache/ -type f -name "postimage*" >actual &&
 454        test_line_count = "$2" actual
 455}
 456
 457merge_conflict_resolve () {
 458        git reset --hard &&
 459        test_must_fail git merge six.1 &&
 460        # Resolution is to replace 7 with 6.1 and 6.2 (i.e. take both)
 461        concat_insert short 6.1 6.2 >file1 &&
 462        concat_insert long 6.1 6.2 >file2
 463}
 464
 465test_expect_success 'multiple identical conflicts' '
 466        rm -fr .git/rr-cache &&
 467        mkdir .git/rr-cache &&
 468        git reset --hard &&
 469
 470        test_seq 1 6 >early &&
 471        >late &&
 472        test_seq 11 15 >short &&
 473        test_seq 111 120 >long &&
 474        concat_insert short >file1 &&
 475        concat_insert long >file2 &&
 476        git add file1 file2 &&
 477        git commit -m base &&
 478        git tag base &&
 479        git checkout -b six.1 &&
 480        concat_insert short 6.1 >file1 &&
 481        concat_insert long 6.1 >file2 &&
 482        git add file1 file2 &&
 483        git commit -m 6.1 &&
 484        git checkout -b six.2 HEAD^ &&
 485        concat_insert short 6.2 >file1 &&
 486        concat_insert long 6.2 >file2 &&
 487        git add file1 file2 &&
 488        git commit -m 6.2 &&
 489
 490        # At this point, six.1 and six.2
 491        # - derive from common ancestor that has two files
 492        #   1...6 7 11..15 (file1) and 1...6 7 111..120 (file2)
 493        # - six.1 replaces these 7s with 6.1
 494        # - six.2 replaces these 7s with 6.2
 495
 496        merge_conflict_resolve &&
 497
 498        # Check that rerere knows that file1 and file2 have conflicts
 499
 500        printf "%s\n" file1 file2 >expect &&
 501        git ls-files -u | sed -e "s/^.* //" | sort -u >actual &&
 502        test_cmp expect actual &&
 503
 504        git rerere status | sort >actual &&
 505        test_cmp expect actual &&
 506
 507        git rerere remaining >actual &&
 508        test_cmp expect actual &&
 509
 510        count_pre_post 2 0 &&
 511
 512        # Pretend that the conflicts were made quite some time ago
 513        find .git/rr-cache/ -type f | xargs test-chmtime -172800 &&
 514
 515        # Unresolved entries have not expired yet
 516        git -c gc.rerereresolved=5 -c gc.rerereunresolved=5 rerere gc &&
 517        count_pre_post 2 0 &&
 518
 519        # Unresolved entries have expired
 520        git -c gc.rerereresolved=5 -c gc.rerereunresolved=1 rerere gc &&
 521        count_pre_post 0 0 &&
 522
 523        # Recreate the conflicted state
 524        merge_conflict_resolve &&
 525        count_pre_post 2 0 &&
 526
 527        # Clear it
 528        git rerere clear &&
 529        count_pre_post 0 0 &&
 530
 531        # Recreate the conflicted state
 532        merge_conflict_resolve &&
 533        count_pre_post 2 0 &&
 534
 535        # We resolved file1 and file2
 536        git rerere &&
 537        >expect &&
 538        git rerere remaining >actual &&
 539        test_cmp expect actual &&
 540
 541        # We must have recorded both of them
 542        count_pre_post 2 2 &&
 543
 544        # Now we should be able to resolve them both
 545        git reset --hard &&
 546        test_must_fail git merge six.1 &&
 547        git rerere &&
 548
 549        >expect &&
 550        git rerere remaining >actual &&
 551        test_cmp expect actual &&
 552
 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 &&
 557
 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 &&
 564
 565        # file2 already has correct resolution, so record it again
 566        git rerere &&
 567
 568        # Pretend that the resolutions are old again
 569        find .git/rr-cache/ -type f | xargs test-chmtime -172800 &&
 570
 571        # Resolved entries have not expired yet
 572        git -c gc.rerereresolved=5 -c gc.rerereunresolved=5 rerere gc &&
 573
 574        count_pre_post 2 2 &&
 575
 576        # Resolved entries have expired
 577        git -c gc.rerereresolved=1 -c gc.rerereunresolved=5 rerere gc &&
 578        count_pre_post 0 0
 579'
 580
 581test_done