t / t4108-apply-threeway.shon commit Merge branch 'jn/doc-api-errors' (32464d3)
   1#!/bin/sh
   2
   3test_description='git apply --3way'
   4
   5. ./test-lib.sh
   6
   7create_file () {
   8        for i
   9        do
  10                echo "$i"
  11        done
  12}
  13
  14sanitize_conflicted_diff () {
  15        sed -e '
  16                /^index /d
  17                s/^\(+[<>][<>][<>][<>]*\) .*/\1/
  18        '
  19}
  20
  21test_expect_success setup '
  22        test_tick &&
  23        create_file >one 1 2 3 4 5 6 7 &&
  24        cat one >two &&
  25        git add one two &&
  26        git commit -m initial &&
  27
  28        git branch side &&
  29
  30        test_tick &&
  31        create_file >one 1 two 3 4 5 six 7 &&
  32        create_file >two 1 two 3 4 5 6 7 &&
  33        git commit -a -m master &&
  34
  35        git checkout side &&
  36        create_file >one 1 2 3 4 five 6 7 &&
  37        create_file >two 1 2 3 4 five 6 7 &&
  38        git commit -a -m side &&
  39
  40        git checkout master
  41'
  42
  43test_expect_success 'apply without --3way' '
  44        git diff side^ side >P.diff &&
  45
  46        # should fail to apply
  47        git reset --hard &&
  48        git checkout master^0 &&
  49        test_must_fail git apply --index P.diff &&
  50        # should leave things intact
  51        git diff-files --exit-code &&
  52        git diff-index --exit-code --cached HEAD
  53'
  54
  55test_expect_success 'apply with --3way' '
  56        # Merging side should be similar to applying this patch
  57        git diff ...side >P.diff &&
  58
  59        # The corresponding conflicted merge
  60        git reset --hard &&
  61        git checkout master^0 &&
  62        test_must_fail git merge --no-commit side &&
  63        git ls-files -s >expect.ls &&
  64        git diff HEAD | sanitize_conflicted_diff >expect.diff &&
  65
  66        # should fail to apply
  67        git reset --hard &&
  68        git checkout master^0 &&
  69        test_must_fail git apply --index --3way P.diff &&
  70        git ls-files -s >actual.ls &&
  71        git diff HEAD | sanitize_conflicted_diff >actual.diff &&
  72
  73        # The result should resemble the corresponding merge
  74        test_cmp expect.ls actual.ls &&
  75        test_cmp expect.diff actual.diff
  76'
  77
  78test_expect_success 'apply with --3way with rerere enabled' '
  79        git config rerere.enabled true &&
  80
  81        # Merging side should be similar to applying this patch
  82        git diff ...side >P.diff &&
  83
  84        # The corresponding conflicted merge
  85        git reset --hard &&
  86        git checkout master^0 &&
  87        test_must_fail git merge --no-commit side &&
  88
  89        # Manually resolve and record the resolution
  90        create_file 1 two 3 4 five six 7 >one &&
  91        git rerere &&
  92        cat one >expect &&
  93
  94        # should fail to apply
  95        git reset --hard &&
  96        git checkout master^0 &&
  97        test_must_fail git apply --index --3way P.diff &&
  98
  99        # but rerere should have replayed the recorded resolution
 100        test_cmp expect one
 101'
 102
 103test_expect_success 'apply -3 with add/add conflict setup' '
 104        git reset --hard &&
 105
 106        git checkout -b adder &&
 107        create_file 1 2 3 4 5 6 7 >three &&
 108        create_file 1 2 3 4 5 6 7 >four &&
 109        git add three four &&
 110        git commit -m "add three and four" &&
 111
 112        git checkout -b another adder^ &&
 113        create_file 1 2 3 4 5 6 7 >three &&
 114        create_file 1 2 3 four 5 6 7 >four &&
 115        git add three four &&
 116        git commit -m "add three and four" &&
 117
 118        # Merging another should be similar to applying this patch
 119        git diff adder...another >P.diff &&
 120
 121        git checkout adder^0 &&
 122        test_must_fail git merge --no-commit another &&
 123        git ls-files -s >expect.ls &&
 124        git diff HEAD | sanitize_conflicted_diff >expect.diff
 125'
 126
 127test_expect_success 'apply -3 with add/add conflict' '
 128        # should fail to apply ...
 129        git reset --hard &&
 130        git checkout adder^0 &&
 131        test_must_fail git apply --index --3way P.diff &&
 132        # ... and leave conflicts in the index and in the working tree
 133        git ls-files -s >actual.ls &&
 134        git diff HEAD | sanitize_conflicted_diff >actual.diff &&
 135
 136        # The result should resemble the corresponding merge
 137        test_cmp expect.ls actual.ls &&
 138        test_cmp expect.diff actual.diff
 139'
 140
 141test_expect_success 'apply -3 with add/add conflict (dirty working tree)' '
 142        # should fail to apply ...
 143        git reset --hard &&
 144        git checkout adder^0 &&
 145        echo >>four &&
 146        cat four >four.save &&
 147        cat three >three.save &&
 148        git ls-files -s >expect.ls &&
 149        test_must_fail git apply --index --3way P.diff &&
 150        # ... and should not touch anything
 151        git ls-files -s >actual.ls &&
 152        test_cmp expect.ls actual.ls &&
 153        test_cmp four.save four &&
 154        test_cmp three.save three
 155'
 156
 157test_done