t3034: add rename threshold tests
authorFelipe Gonçalves Assis <felipeg.assis@gmail.com>
Wed, 24 Feb 2016 01:41:29 +0000 (22:41 -0300)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Feb 2016 22:45:28 +0000 (14:45 -0800)
10ae752 (merge-recursive: option to specify rename threshold,
2010-09-27) introduced this feature but did not include any tests.

The tests use the new option --find-renames, which replaces the then
introduced and now deprecated option --rename-threshold.

Also update name and description of t3032 for consistency:
"merge-recursive options" -> "merge-recursive space options"

Signed-off-by: Felipe Gonçalves Assis <felipegassis@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t3032-merge-recursive-options.sh [deleted file]
t/t3032-merge-recursive-space-options.sh [new file with mode: 0755]
t/t3034-merge-recursive-rename-options.sh [new file with mode: 0755]
diff --git a/t/t3032-merge-recursive-options.sh b/t/t3032-merge-recursive-options.sh
deleted file mode 100755 (executable)
index 4029c9c..0000000
+++ /dev/null
@@ -1,207 +0,0 @@
-#!/bin/sh
-
-test_description='merge-recursive options
-
-* [master] Clarify
- ! [remote] Remove cruft
---
- + [remote] Remove cruft
-*  [master] Clarify
-*+ [remote^] Initial revision
-*   ok 1: setup
-'
-
-. ./test-lib.sh
-
-test_have_prereq SED_STRIPS_CR && SED_OPTIONS=-b
-if test_have_prereq GREP_STRIPS_CR
-then
-       GREP_OPTIONS=-U
-       export GREP_OPTIONS
-fi
-
-test_expect_success 'setup' '
-       conflict_hunks () {
-               sed $SED_OPTIONS -n -e "
-                       /^<<<</ b conflict
-                       b
-                       : conflict
-                       p
-                       /^>>>>/ b
-                       n
-                       b conflict
-               " "$@"
-       } &&
-
-       cat <<-\EOF >text.txt &&
-           Hope, he says, cherishes the soul of him who lives in
-           justice and holiness and is the nurse of his age and the
-           companion of his journey;--hope which is mightiest to sway
-           the restless soul of man.
-
-       How admirable are his words!  And the great blessing of riches, I do
-       not say to every man, but to a good man, is, that he has had no
-       occasion to deceive or to defraud others, either intentionally or
-       unintentionally; and when he departs to the world below he is not in
-       any apprehension about offerings due to the gods or debts which he owes
-       to men.  Now to this peace of mind the possession of wealth greatly
-       contributes; and therefore I say, that, setting one thing against
-       another, of the many advantages which wealth has to give, to a man of
-       sense this is in my opinion the greatest.
-
-       Well said, Cephalus, I replied; but as concerning justice, what is
-       it?--to speak the truth and to pay your debts--no more than this?  And
-       even to this are there not exceptions?  Suppose that a friend when in
-       his right mind has deposited arms with me and he asks for them when he
-       is not in his right mind, ought I to give them back to him?  No one
-       would say that I ought or that I should be right in doing so, any more
-       than they would say that I ought always to speak the truth to one who
-       is in his condition.
-
-       You are quite right, he replied.
-
-       But then, I said, speaking the truth and paying your debts is not a
-       correct definition of justice.
-
-       CEPHALUS - SOCRATES - POLEMARCHUS
-
-       Quite correct, Socrates, if Simonides is to be believed, said
-       Polemarchus interposing.
-
-       I fear, said Cephalus, that I must go now, for I have to look after the
-       sacrifices, and I hand over the argument to Polemarchus and the company.
-       EOF
-       git add text.txt &&
-       test_tick &&
-       git commit -m "Initial revision" &&
-
-       git checkout -b remote &&
-       sed -e "
-                       s/\.  /\. /g
-                       s/[?]  /? /g
-                       s/    / /g
-                       s/--/---/g
-                       s/but as concerning/but as con cerning/
-                       /CEPHALUS - SOCRATES - POLEMARCHUS/ d
-               " text.txt >text.txt+ &&
-       mv text.txt+ text.txt &&
-       git commit -a -m "Remove cruft" &&
-
-       git checkout master &&
-       sed -e "
-                       s/\(not in his right mind\),\(.*\)/\1;\2Q/
-                       s/Quite correct\(.*\)/It is too correct\1Q/
-                       s/unintentionally/un intentionally/
-                       /un intentionally/ s/$/Q/
-                       s/Polemarchus interposing./Polemarchus, interposing.Q/
-                       /justice and holiness/ s/$/Q/
-                       /pay your debts/ s/$/Q/
-               " text.txt | q_to_cr >text.txt+ &&
-       mv text.txt+ text.txt &&
-       git commit -a -m "Clarify" &&
-       git show-branch --all
-'
-
-test_expect_success 'naive merge fails' '
-       git read-tree --reset -u HEAD &&
-       test_must_fail git merge-recursive HEAD^ -- HEAD remote &&
-       test_must_fail git update-index --refresh &&
-       grep "<<<<<<" text.txt
-'
-
-test_expect_success '--ignore-space-change makes merge succeed' '
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote
-'
-
-test_expect_success 'naive cherry-pick fails' '
-       git read-tree --reset -u HEAD &&
-       test_must_fail git cherry-pick --no-commit remote &&
-       git read-tree --reset -u HEAD &&
-       test_must_fail git cherry-pick remote &&
-       test_must_fail git update-index --refresh &&
-       grep "<<<<<<" text.txt
-'
-
-test_expect_success '-Xignore-space-change makes cherry-pick succeed' '
-       git read-tree --reset -u HEAD &&
-       git cherry-pick --no-commit -Xignore-space-change remote
-'
-
-test_expect_success '--ignore-space-change: our w/s-only change wins' '
-       q_to_cr <<-\EOF >expected &&
-           justice and holiness and is the nurse of his age and theQ
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
-       grep "justice and holiness" text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_expect_success '--ignore-space-change: their real change wins over w/s' '
-       cat <<-\EOF >expected &&
-       it?---to speak the truth and to pay your debts---no more than this? And
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
-       grep "pay your debts" text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_expect_success '--ignore-space-change: does not ignore new spaces' '
-       cat <<-\EOF >expected1 &&
-       Well said, Cephalus, I replied; but as con cerning justice, what is
-       EOF
-       q_to_cr <<-\EOF >expected2 &&
-       un intentionally; and when he departs to the world below he is not inQ
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
-       grep "Well said" text.txt >actual1 &&
-       grep "when he departs" text.txt >actual2 &&
-       test_cmp expected1 actual1 &&
-       test_cmp expected2 actual2
-'
-
-test_expect_success '--ignore-all-space drops their new spaces' '
-       cat <<-\EOF >expected &&
-       Well said, Cephalus, I replied; but as concerning justice, what is
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
-       grep "Well said" text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_expect_success '--ignore-all-space keeps our new spaces' '
-       q_to_cr <<-\EOF >expected &&
-       un intentionally; and when he departs to the world below he is not inQ
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
-       grep "when he departs" text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_expect_success '--ignore-space-at-eol' '
-       q_to_cr <<-\EOF >expected &&
-       <<<<<<< HEAD
-       is not in his right mind; ought I to give them back to him?  No oneQ
-       =======
-       is not in his right mind, ought I to give them back to him? No one
-       >>>>>>> remote
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       test_must_fail git merge-recursive --ignore-space-at-eol \
-                                                HEAD^ -- HEAD remote &&
-       conflict_hunks text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_done
diff --git a/t/t3032-merge-recursive-space-options.sh b/t/t3032-merge-recursive-space-options.sh
new file mode 100755 (executable)
index 0000000..b56180e
--- /dev/null
@@ -0,0 +1,207 @@
+#!/bin/sh
+
+test_description='merge-recursive space options
+
+* [master] Clarify
+ ! [remote] Remove cruft
+--
+ + [remote] Remove cruft
+*  [master] Clarify
+*+ [remote^] Initial revision
+*   ok 1: setup
+'
+
+. ./test-lib.sh
+
+test_have_prereq SED_STRIPS_CR && SED_OPTIONS=-b
+if test_have_prereq GREP_STRIPS_CR
+then
+       GREP_OPTIONS=-U
+       export GREP_OPTIONS
+fi
+
+test_expect_success 'setup' '
+       conflict_hunks () {
+               sed $SED_OPTIONS -n -e "
+                       /^<<<</ b conflict
+                       b
+                       : conflict
+                       p
+                       /^>>>>/ b
+                       n
+                       b conflict
+               " "$@"
+       } &&
+
+       cat <<-\EOF >text.txt &&
+           Hope, he says, cherishes the soul of him who lives in
+           justice and holiness and is the nurse of his age and the
+           companion of his journey;--hope which is mightiest to sway
+           the restless soul of man.
+
+       How admirable are his words!  And the great blessing of riches, I do
+       not say to every man, but to a good man, is, that he has had no
+       occasion to deceive or to defraud others, either intentionally or
+       unintentionally; and when he departs to the world below he is not in
+       any apprehension about offerings due to the gods or debts which he owes
+       to men.  Now to this peace of mind the possession of wealth greatly
+       contributes; and therefore I say, that, setting one thing against
+       another, of the many advantages which wealth has to give, to a man of
+       sense this is in my opinion the greatest.
+
+       Well said, Cephalus, I replied; but as concerning justice, what is
+       it?--to speak the truth and to pay your debts--no more than this?  And
+       even to this are there not exceptions?  Suppose that a friend when in
+       his right mind has deposited arms with me and he asks for them when he
+       is not in his right mind, ought I to give them back to him?  No one
+       would say that I ought or that I should be right in doing so, any more
+       than they would say that I ought always to speak the truth to one who
+       is in his condition.
+
+       You are quite right, he replied.
+
+       But then, I said, speaking the truth and paying your debts is not a
+       correct definition of justice.
+
+       CEPHALUS - SOCRATES - POLEMARCHUS
+
+       Quite correct, Socrates, if Simonides is to be believed, said
+       Polemarchus interposing.
+
+       I fear, said Cephalus, that I must go now, for I have to look after the
+       sacrifices, and I hand over the argument to Polemarchus and the company.
+       EOF
+       git add text.txt &&
+       test_tick &&
+       git commit -m "Initial revision" &&
+
+       git checkout -b remote &&
+       sed -e "
+                       s/\.  /\. /g
+                       s/[?]  /? /g
+                       s/    / /g
+                       s/--/---/g
+                       s/but as concerning/but as con cerning/
+                       /CEPHALUS - SOCRATES - POLEMARCHUS/ d
+               " text.txt >text.txt+ &&
+       mv text.txt+ text.txt &&
+       git commit -a -m "Remove cruft" &&
+
+       git checkout master &&
+       sed -e "
+                       s/\(not in his right mind\),\(.*\)/\1;\2Q/
+                       s/Quite correct\(.*\)/It is too correct\1Q/
+                       s/unintentionally/un intentionally/
+                       /un intentionally/ s/$/Q/
+                       s/Polemarchus interposing./Polemarchus, interposing.Q/
+                       /justice and holiness/ s/$/Q/
+                       /pay your debts/ s/$/Q/
+               " text.txt | q_to_cr >text.txt+ &&
+       mv text.txt+ text.txt &&
+       git commit -a -m "Clarify" &&
+       git show-branch --all
+'
+
+test_expect_success 'naive merge fails' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive HEAD^ -- HEAD remote &&
+       test_must_fail git update-index --refresh &&
+       grep "<<<<<<" text.txt
+'
+
+test_expect_success '--ignore-space-change makes merge succeed' '
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote
+'
+
+test_expect_success 'naive cherry-pick fails' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git cherry-pick --no-commit remote &&
+       git read-tree --reset -u HEAD &&
+       test_must_fail git cherry-pick remote &&
+       test_must_fail git update-index --refresh &&
+       grep "<<<<<<" text.txt
+'
+
+test_expect_success '-Xignore-space-change makes cherry-pick succeed' '
+       git read-tree --reset -u HEAD &&
+       git cherry-pick --no-commit -Xignore-space-change remote
+'
+
+test_expect_success '--ignore-space-change: our w/s-only change wins' '
+       q_to_cr <<-\EOF >expected &&
+           justice and holiness and is the nurse of his age and theQ
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+       grep "justice and holiness" text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-change: their real change wins over w/s' '
+       cat <<-\EOF >expected &&
+       it?---to speak the truth and to pay your debts---no more than this? And
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+       grep "pay your debts" text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-change: does not ignore new spaces' '
+       cat <<-\EOF >expected1 &&
+       Well said, Cephalus, I replied; but as con cerning justice, what is
+       EOF
+       q_to_cr <<-\EOF >expected2 &&
+       un intentionally; and when he departs to the world below he is not inQ
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+       grep "Well said" text.txt >actual1 &&
+       grep "when he departs" text.txt >actual2 &&
+       test_cmp expected1 actual1 &&
+       test_cmp expected2 actual2
+'
+
+test_expect_success '--ignore-all-space drops their new spaces' '
+       cat <<-\EOF >expected &&
+       Well said, Cephalus, I replied; but as concerning justice, what is
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
+       grep "Well said" text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success '--ignore-all-space keeps our new spaces' '
+       q_to_cr <<-\EOF >expected &&
+       un intentionally; and when he departs to the world below he is not inQ
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
+       grep "when he departs" text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-at-eol' '
+       q_to_cr <<-\EOF >expected &&
+       <<<<<<< HEAD
+       is not in his right mind; ought I to give them back to him?  No oneQ
+       =======
+       is not in his right mind, ought I to give them back to him? No one
+       >>>>>>> remote
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --ignore-space-at-eol \
+                                                HEAD^ -- HEAD remote &&
+       conflict_hunks text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_done
diff --git a/t/t3034-merge-recursive-rename-options.sh b/t/t3034-merge-recursive-rename-options.sh
new file mode 100755 (executable)
index 0000000..51c2f87
--- /dev/null
@@ -0,0 +1,238 @@
+#!/bin/sh
+
+test_description='merge-recursive rename options
+
+Test rename detection by examining rename/delete conflicts.
+
+* (HEAD -> rename) rename
+| * (master) delete
+|/
+* base
+
+git diff --name-status base master
+D      0-old
+D      1-old
+D      2-old
+D      3-old
+
+git diff --name-status -M01 base rename
+R025    0-old   0-new
+R050    1-old   1-new
+R075    2-old   2-new
+R100    3-old   3-new
+
+Actual similarity indices are parsed from diff output. We rely on the fact that
+they are rounded down (see, e.g., Documentation/diff-generate-patch.txt, which
+mentions this in a different context).
+'
+
+. ./test-lib.sh
+
+get_expected_stages () {
+       git checkout rename -- $1-new &&
+       git ls-files --stage $1-new >expected-stages-undetected-$1 &&
+       sed "s/ 0       / 2     /" <expected-stages-undetected-$1 \
+               >expected-stages-detected-$1 &&
+       git read-tree -u --reset HEAD
+}
+
+rename_detected () {
+       git ls-files --stage $1-old $1-new >stages-actual-$1 &&
+       test_cmp expected-stages-detected-$1 stages-actual-$1
+}
+
+rename_undetected () {
+       git ls-files --stage $1-old $1-new >stages-actual-$1 &&
+       test_cmp expected-stages-undetected-$1 stages-actual-$1
+}
+
+check_common () {
+       git ls-files --stage >stages-actual &&
+       test_line_count = 4 stages-actual
+}
+
+check_threshold_0 () {
+       check_common &&
+       rename_detected 0 &&
+       rename_detected 1 &&
+       rename_detected 2 &&
+       rename_detected 3
+}
+
+check_threshold_1 () {
+       check_common &&
+       rename_undetected 0 &&
+       rename_detected 1 &&
+       rename_detected 2 &&
+       rename_detected 3
+}
+
+check_threshold_2 () {
+       check_common &&
+       rename_undetected 0 &&
+       rename_undetected 1 &&
+       rename_detected 2 &&
+       rename_detected 3
+}
+
+check_exact_renames () {
+       check_common &&
+       rename_undetected 0 &&
+       rename_undetected 1 &&
+       rename_undetected 2 &&
+       rename_detected 3
+}
+
+test_expect_success 'setup repo' '
+       cat <<-\EOF >3-old &&
+       33a
+       33b
+       33c
+       33d
+       EOF
+       sed s/33/22/ <3-old >2-old &&
+       sed s/33/11/ <3-old >1-old &&
+       sed s/33/00/ <3-old >0-old &&
+       git add [0-3]-old &&
+       git commit -m base &&
+       git rm [0-3]-old &&
+       git commit -m delete &&
+       git checkout -b rename HEAD^ &&
+       cp 3-old 3-new &&
+       sed 1,1s/./x/ <2-old >2-new &&
+       sed 1,2s/./x/ <1-old >1-new &&
+       sed 1,3s/./x/ <0-old >0-new &&
+       git add [0-3]-new &&
+       git rm [0-3]-old &&
+       git commit -m rename &&
+       get_expected_stages 0 &&
+       get_expected_stages 1 &&
+       get_expected_stages 2 &&
+       get_expected_stages 3 &&
+       check_50="false" &&
+       tail="HEAD^ -- HEAD master"
+'
+
+test_expect_success 'setup thresholds' '
+       git diff --name-status -M01 HEAD^ HEAD >diff-output &&
+       test_debug "cat diff-output" &&
+       test_line_count = 4 diff-output &&
+       grep "R[0-9][0-9][0-9]  \([0-3]\)-old   \1-new" diff-output \
+               >grep-output &&
+       test_cmp diff-output grep-output &&
+       th0=$(sed -n "s/R\(...\)        0-old   0-new/\1/p" <diff-output) &&
+       th1=$(sed -n "s/R\(...\)        1-old   1-new/\1/p" <diff-output) &&
+       th2=$(sed -n "s/R\(...\)        2-old   2-new/\1/p" <diff-output) &&
+       th3=$(sed -n "s/R\(...\)        3-old   3-new/\1/p" <diff-output) &&
+       test "$th0" -lt "$th1" &&
+       test "$th1" -lt "$th2" &&
+       test "$th2" -lt "$th3" &&
+       test "$th3" = 100 &&
+       if test 50 -le "$th0"
+       then
+               check_50=check_threshold_0
+       elif test 50 -le "$th1"
+       then
+               check_50=check_threshold_1
+       elif test 50 -le "$th2"
+       then
+               check_50=check_threshold_2
+       fi &&
+       th0="$th0%" &&
+       th1="$th1%" &&
+       th2="$th2%" &&
+       th3="$th3%"
+'
+
+test_expect_success 'assumption for tests: rename detection with diff' '
+       git diff --name-status -M$th0 --diff-filter=R HEAD^ HEAD \
+               >diff-output-0 &&
+       git diff --name-status -M$th1 --diff-filter=R HEAD^ HEAD \
+               >diff-output-1 &&
+       git diff --name-status -M$th2 --diff-filter=R HEAD^ HEAD \
+               >diff-output-2 &&
+       git diff --name-status -M100% --diff-filter=R HEAD^ HEAD \
+               >diff-output-3 &&
+       test_line_count = 4 diff-output-0 &&
+       test_line_count = 3 diff-output-1 &&
+       test_line_count = 2 diff-output-2 &&
+       test_line_count = 1 diff-output-3
+'
+
+test_expect_success 'default similarity threshold is 50%' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive $tail &&
+       $check_50
+'
+
+test_expect_success 'low rename threshold' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=$th0 $tail &&
+       check_threshold_0
+'
+
+test_expect_success 'medium rename threshold' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=$th1 $tail &&
+       check_threshold_1
+'
+
+test_expect_success 'high rename threshold' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=$th2 $tail &&
+       check_threshold_2
+'
+
+test_expect_success 'exact renames only' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=100% $tail &&
+       check_exact_renames
+'
+
+test_expect_success 'rename threshold is truncated' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=200% $tail &&
+       check_exact_renames
+'
+
+test_expect_success 'last wins in --find-renames=<m> --find-renames=<n>' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive \
+               --find-renames=$th0 --find-renames=$th2 $tail &&
+       check_threshold_2
+'
+
+test_expect_success '--find-renames resets threshold' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive \
+               --find-renames=$th0 --find-renames $tail &&
+       $check_50
+'
+
+test_expect_success 'assumption for further tests: trivial merge succeeds' '
+       git read-tree --reset -u HEAD &&
+       git merge-recursive HEAD -- HEAD HEAD &&
+       git diff --quiet --cached &&
+       git merge-recursive --find-renames=$th0 HEAD -- HEAD HEAD &&
+       git diff --quiet --cached &&
+       git merge-recursive --find-renames=$th2 HEAD -- HEAD HEAD &&
+       git diff --quiet --cached &&
+       git merge-recursive --find-renames=100% HEAD -- HEAD HEAD &&
+       git diff --quiet --cached
+'
+
+test_expect_success '--find-renames rejects negative argument' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=-25 \
+               HEAD -- HEAD HEAD &&
+       git diff --quiet --cached
+'
+
+test_expect_success '--find-renames rejects non-numbers' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=0xf \
+               HEAD -- HEAD HEAD &&
+       git diff --quiet --cached
+'
+
+test_done