51c2f87202cc1ae5636a3398dd099ef70fda040c
   1#!/bin/sh
   2
   3test_description='merge-recursive rename options
   4
   5Test rename detection by examining rename/delete conflicts.
   6
   7* (HEAD -> rename) rename
   8| * (master) delete
   9|/
  10* base
  11
  12git diff --name-status base master
  13D       0-old
  14D       1-old
  15D       2-old
  16D       3-old
  17
  18git diff --name-status -M01 base rename
  19R025    0-old   0-new
  20R050    1-old   1-new
  21R075    2-old   2-new
  22R100    3-old   3-new
  23
  24Actual similarity indices are parsed from diff output. We rely on the fact that
  25they are rounded down (see, e.g., Documentation/diff-generate-patch.txt, which
  26mentions this in a different context).
  27'
  28
  29. ./test-lib.sh
  30
  31get_expected_stages () {
  32        git checkout rename -- $1-new &&
  33        git ls-files --stage $1-new >expected-stages-undetected-$1 &&
  34        sed "s/ 0       / 2     /" <expected-stages-undetected-$1 \
  35                >expected-stages-detected-$1 &&
  36        git read-tree -u --reset HEAD
  37}
  38
  39rename_detected () {
  40        git ls-files --stage $1-old $1-new >stages-actual-$1 &&
  41        test_cmp expected-stages-detected-$1 stages-actual-$1
  42}
  43
  44rename_undetected () {
  45        git ls-files --stage $1-old $1-new >stages-actual-$1 &&
  46        test_cmp expected-stages-undetected-$1 stages-actual-$1
  47}
  48
  49check_common () {
  50        git ls-files --stage >stages-actual &&
  51        test_line_count = 4 stages-actual
  52}
  53
  54check_threshold_0 () {
  55        check_common &&
  56        rename_detected 0 &&
  57        rename_detected 1 &&
  58        rename_detected 2 &&
  59        rename_detected 3
  60}
  61
  62check_threshold_1 () {
  63        check_common &&
  64        rename_undetected 0 &&
  65        rename_detected 1 &&
  66        rename_detected 2 &&
  67        rename_detected 3
  68}
  69
  70check_threshold_2 () {
  71        check_common &&
  72        rename_undetected 0 &&
  73        rename_undetected 1 &&
  74        rename_detected 2 &&
  75        rename_detected 3
  76}
  77
  78check_exact_renames () {
  79        check_common &&
  80        rename_undetected 0 &&
  81        rename_undetected 1 &&
  82        rename_undetected 2 &&
  83        rename_detected 3
  84}
  85
  86test_expect_success 'setup repo' '
  87        cat <<-\EOF >3-old &&
  88        33a
  89        33b
  90        33c
  91        33d
  92        EOF
  93        sed s/33/22/ <3-old >2-old &&
  94        sed s/33/11/ <3-old >1-old &&
  95        sed s/33/00/ <3-old >0-old &&
  96        git add [0-3]-old &&
  97        git commit -m base &&
  98        git rm [0-3]-old &&
  99        git commit -m delete &&
 100        git checkout -b rename HEAD^ &&
 101        cp 3-old 3-new &&
 102        sed 1,1s/./x/ <2-old >2-new &&
 103        sed 1,2s/./x/ <1-old >1-new &&
 104        sed 1,3s/./x/ <0-old >0-new &&
 105        git add [0-3]-new &&
 106        git rm [0-3]-old &&
 107        git commit -m rename &&
 108        get_expected_stages 0 &&
 109        get_expected_stages 1 &&
 110        get_expected_stages 2 &&
 111        get_expected_stages 3 &&
 112        check_50="false" &&
 113        tail="HEAD^ -- HEAD master"
 114'
 115
 116test_expect_success 'setup thresholds' '
 117        git diff --name-status -M01 HEAD^ HEAD >diff-output &&
 118        test_debug "cat diff-output" &&
 119        test_line_count = 4 diff-output &&
 120        grep "R[0-9][0-9][0-9]  \([0-3]\)-old   \1-new" diff-output \
 121                >grep-output &&
 122        test_cmp diff-output grep-output &&
 123        th0=$(sed -n "s/R\(...\)        0-old   0-new/\1/p" <diff-output) &&
 124        th1=$(sed -n "s/R\(...\)        1-old   1-new/\1/p" <diff-output) &&
 125        th2=$(sed -n "s/R\(...\)        2-old   2-new/\1/p" <diff-output) &&
 126        th3=$(sed -n "s/R\(...\)        3-old   3-new/\1/p" <diff-output) &&
 127        test "$th0" -lt "$th1" &&
 128        test "$th1" -lt "$th2" &&
 129        test "$th2" -lt "$th3" &&
 130        test "$th3" = 100 &&
 131        if test 50 -le "$th0"
 132        then
 133                check_50=check_threshold_0
 134        elif test 50 -le "$th1"
 135        then
 136                check_50=check_threshold_1
 137        elif test 50 -le "$th2"
 138        then
 139                check_50=check_threshold_2
 140        fi &&
 141        th0="$th0%" &&
 142        th1="$th1%" &&
 143        th2="$th2%" &&
 144        th3="$th3%"
 145'
 146
 147test_expect_success 'assumption for tests: rename detection with diff' '
 148        git diff --name-status -M$th0 --diff-filter=R HEAD^ HEAD \
 149                >diff-output-0 &&
 150        git diff --name-status -M$th1 --diff-filter=R HEAD^ HEAD \
 151                >diff-output-1 &&
 152        git diff --name-status -M$th2 --diff-filter=R HEAD^ HEAD \
 153                >diff-output-2 &&
 154        git diff --name-status -M100% --diff-filter=R HEAD^ HEAD \
 155                >diff-output-3 &&
 156        test_line_count = 4 diff-output-0 &&
 157        test_line_count = 3 diff-output-1 &&
 158        test_line_count = 2 diff-output-2 &&
 159        test_line_count = 1 diff-output-3
 160'
 161
 162test_expect_success 'default similarity threshold is 50%' '
 163        git read-tree --reset -u HEAD &&
 164        test_must_fail git merge-recursive $tail &&
 165        $check_50
 166'
 167
 168test_expect_success 'low rename threshold' '
 169        git read-tree --reset -u HEAD &&
 170        test_must_fail git merge-recursive --find-renames=$th0 $tail &&
 171        check_threshold_0
 172'
 173
 174test_expect_success 'medium rename threshold' '
 175        git read-tree --reset -u HEAD &&
 176        test_must_fail git merge-recursive --find-renames=$th1 $tail &&
 177        check_threshold_1
 178'
 179
 180test_expect_success 'high rename threshold' '
 181        git read-tree --reset -u HEAD &&
 182        test_must_fail git merge-recursive --find-renames=$th2 $tail &&
 183        check_threshold_2
 184'
 185
 186test_expect_success 'exact renames only' '
 187        git read-tree --reset -u HEAD &&
 188        test_must_fail git merge-recursive --find-renames=100% $tail &&
 189        check_exact_renames
 190'
 191
 192test_expect_success 'rename threshold is truncated' '
 193        git read-tree --reset -u HEAD &&
 194        test_must_fail git merge-recursive --find-renames=200% $tail &&
 195        check_exact_renames
 196'
 197
 198test_expect_success 'last wins in --find-renames=<m> --find-renames=<n>' '
 199        git read-tree --reset -u HEAD &&
 200        test_must_fail git merge-recursive \
 201                --find-renames=$th0 --find-renames=$th2 $tail &&
 202        check_threshold_2
 203'
 204
 205test_expect_success '--find-renames resets threshold' '
 206        git read-tree --reset -u HEAD &&
 207        test_must_fail git merge-recursive \
 208                --find-renames=$th0 --find-renames $tail &&
 209        $check_50
 210'
 211
 212test_expect_success 'assumption for further tests: trivial merge succeeds' '
 213        git read-tree --reset -u HEAD &&
 214        git merge-recursive HEAD -- HEAD HEAD &&
 215        git diff --quiet --cached &&
 216        git merge-recursive --find-renames=$th0 HEAD -- HEAD HEAD &&
 217        git diff --quiet --cached &&
 218        git merge-recursive --find-renames=$th2 HEAD -- HEAD HEAD &&
 219        git diff --quiet --cached &&
 220        git merge-recursive --find-renames=100% HEAD -- HEAD HEAD &&
 221        git diff --quiet --cached
 222'
 223
 224test_expect_success '--find-renames rejects negative argument' '
 225        git read-tree --reset -u HEAD &&
 226        test_must_fail git merge-recursive --find-renames=-25 \
 227                HEAD -- HEAD HEAD &&
 228        git diff --quiet --cached
 229'
 230
 231test_expect_success '--find-renames rejects non-numbers' '
 232        git read-tree --reset -u HEAD &&
 233        test_must_fail git merge-recursive --find-renames=0xf \
 234                HEAD -- HEAD HEAD &&
 235        git diff --quiet --cached
 236'
 237
 238test_done