2009-08-04 13:16:49 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Copyright (c) 2009 Giuseppe Bilotta
|
|
|
|
#
|
|
|
|
|
|
|
|
test_description='git-apply --ignore-whitespace.
|
|
|
|
|
|
|
|
'
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
# This primes main.c file that indents without using HT at all.
|
|
|
|
# Various patches with HT and other spaces are attempted in the test.
|
|
|
|
|
|
|
|
cat > patch1.patch <<\EOF
|
|
|
|
diff --git a/main.c b/main.c
|
|
|
|
new file mode 100644
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/main.c
|
|
|
|
@@ -0,0 +1,22 @@
|
|
|
|
+#include <stdio.h>
|
|
|
|
+
|
|
|
|
+void print_int(int num);
|
|
|
|
+int func(int num);
|
|
|
|
+
|
|
|
|
+int main() {
|
|
|
|
+ int i;
|
|
|
|
+
|
|
|
|
+ for (i = 0; i < 10; i++) {
|
|
|
|
+ print_int(func(i)); /* stuff */
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int func(int num) {
|
|
|
|
+ return num * num;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void print_int(int num) {
|
|
|
|
+ printf("%d", num);
|
|
|
|
+}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Since whitespace is very significant and we want to prevent whitespace
|
|
|
|
# mangling when creating this test from a patch, we protect 'fixable'
|
|
|
|
# whitespace by replacing spaces with Z and replacing them at patch
|
|
|
|
# creation time, hence the sed trick.
|
|
|
|
|
|
|
|
# This patch will fail unless whitespace differences are being ignored
|
|
|
|
|
|
|
|
sed -e 's/Z/ /g' > patch2.patch <<\EOF
|
|
|
|
diff --git a/main.c b/main.c
|
|
|
|
--- a/main.c
|
|
|
|
+++ b/main.c
|
|
|
|
@@ -10,6 +10,8 @@
|
|
|
|
Z print_int(func(i)); /* stuff */
|
|
|
|
Z }
|
|
|
|
Z
|
|
|
|
+ printf("\n");
|
|
|
|
+
|
|
|
|
Z return 0;
|
|
|
|
Z}
|
|
|
|
Z
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# This patch will fail even if whitespace differences are being ignored,
|
|
|
|
# because of the missing string at EOL. TODO: this testcase should be
|
|
|
|
# improved by creating a line that has the same hash with and without
|
|
|
|
# the final string.
|
|
|
|
|
|
|
|
sed -e 's/Z/ /g' > patch3.patch <<\EOF
|
|
|
|
diff --git a/main.c b/main.c
|
|
|
|
--- a/main.c
|
|
|
|
+++ b/main.c
|
|
|
|
@@ -10,3 +10,4 @@
|
|
|
|
Z for (i = 0; i < 10; i++) {
|
|
|
|
Z print_int(func(i));Z
|
|
|
|
+ /* stuff */
|
|
|
|
Z }
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# This patch will fail even if whitespace differences are being ignored,
|
|
|
|
# because of the missing EOL at EOF.
|
|
|
|
|
|
|
|
sed -e 's/Z/ /g' > patch4.patch <<\EOF
|
|
|
|
diff --git a/main.c b/main.c
|
|
|
|
--- a/main.c
|
|
|
|
+++ b/main.c
|
|
|
|
@@ -21,1 +21,1 @@
|
|
|
|
- };Z
|
|
|
|
\ No newline at end of file
|
|
|
|
+ };
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# This patch will fail unless whitespace differences are being ignored.
|
|
|
|
|
|
|
|
sed -e 's/Z/ /g' > patch5.patch <<\EOF
|
|
|
|
diff --git a/main.c b/main.c
|
|
|
|
--- a/main.c
|
|
|
|
+++ b/main.c
|
|
|
|
@@ -2,2 +2,3 @@
|
|
|
|
Z void print_int(int num);
|
|
|
|
+ /* a comment */
|
|
|
|
Z int func(int num);
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# And this is how the final output should be. Patches introduce
|
|
|
|
# HTs but the original SP indents are mostly kept.
|
|
|
|
|
|
|
|
sed -e 's/T/ /g' > main.c.final <<\EOF
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void print_int(int num);
|
|
|
|
int func(int num);
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
print_int(func(i)); /* stuff */
|
|
|
|
}
|
|
|
|
|
|
|
|
Tprintf("\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int func(int num) {
|
|
|
|
return num * num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_int(int num) {
|
|
|
|
printf("%d", num);
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success 'file creation' '
|
2009-11-28 19:38:55 +01:00
|
|
|
git apply patch1.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'patch2 fails (retab)' '
|
2009-11-28 19:38:55 +01:00
|
|
|
test_must_fail git apply patch2.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'patch2 applies with --ignore-whitespace' '
|
2009-11-28 19:38:55 +01:00
|
|
|
git apply --ignore-whitespace patch2.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'patch2 reverse applies with --ignore-space-change' '
|
2009-11-28 19:38:55 +01:00
|
|
|
git apply -R --ignore-space-change patch2.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
git config apply.ignorewhitespace change
|
|
|
|
|
|
|
|
test_expect_success 'patch2 applies (apply.ignorewhitespace = change)' '
|
apply --ignore-space-change: lines with and without leading whitespaces do not match
The fuzzy_matchlines() function is used when attempting to resurrect
a patch that is whitespace-damaged, or when applying a patch that
was produced against an old codebase to the codebase after
indentation change.
The patch may want to change a line "a_bc" ("_" is used throught
this description for a whitespace to make it stand out) in the
original into something else, and we may not find "a_bc" in the
current source, but there may be "a__bc" (two spaces instead of one
the whitespace-damaged patch claims to expect). By ignoring the
amount of whitespaces, it forces "git apply" to consider that "a_bc"
in the broken patch meant to refer to "a__bc" in reality.
However, the implementation special cases a run of whitespaces at
the beginning of a line and makes "abc" match "_abc", even though a
whitespace in the middle of string never matches a 0-width gap,
e.g. "a_bc" does not match "abc". A run of whitespace at the end of
one string does not match a 0-width end of line on the other line,
either, e.g. "abc_" does not match "abc".
Fix this inconsistency by making the code skip leading whitespaces
only when both strings begin with a whitespace. This makes the
option mean the same as the option of the same name in "diff" and
"git diff".
Note that I am not sure if anybody sane should use this option in
the first place. The fuzzy match logic may be able to find the
original line that the patch author may have meant to touch because
it does not fully trust what the original lines say (i.e. context
lines prefixed by " " and old lines prefixed by "-" does not have to
exactly match the contents the patch is applied to). There is no
reason for us to trust what the replacement lines (i.e. new lines
prefixed by "+") say, either, but with this option enabled, we end
up copying these new lines with suspicious whitespace distributions
literally into the patched result. But as long as we keep it, we
should make it do its insane thing consistently.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-03-26 21:42:06 +01:00
|
|
|
git apply patch2.patch &&
|
|
|
|
test_cmp main.c.final main.c
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'patch3 fails (missing string at EOL)' '
|
2009-11-28 19:38:55 +01:00
|
|
|
test_must_fail git apply patch3.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'patch4 fails (missing EOL at EOF)' '
|
2009-11-28 19:38:55 +01:00
|
|
|
test_must_fail git apply patch4.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
apply --ignore-space-change: lines with and without leading whitespaces do not match
The fuzzy_matchlines() function is used when attempting to resurrect
a patch that is whitespace-damaged, or when applying a patch that
was produced against an old codebase to the codebase after
indentation change.
The patch may want to change a line "a_bc" ("_" is used throught
this description for a whitespace to make it stand out) in the
original into something else, and we may not find "a_bc" in the
current source, but there may be "a__bc" (two spaces instead of one
the whitespace-damaged patch claims to expect). By ignoring the
amount of whitespaces, it forces "git apply" to consider that "a_bc"
in the broken patch meant to refer to "a__bc" in reality.
However, the implementation special cases a run of whitespaces at
the beginning of a line and makes "abc" match "_abc", even though a
whitespace in the middle of string never matches a 0-width gap,
e.g. "a_bc" does not match "abc". A run of whitespace at the end of
one string does not match a 0-width end of line on the other line,
either, e.g. "abc_" does not match "abc".
Fix this inconsistency by making the code skip leading whitespaces
only when both strings begin with a whitespace. This makes the
option mean the same as the option of the same name in "diff" and
"git diff".
Note that I am not sure if anybody sane should use this option in
the first place. The fuzzy match logic may be able to find the
original line that the patch author may have meant to touch because
it does not fully trust what the original lines say (i.e. context
lines prefixed by " " and old lines prefixed by "-" does not have to
exactly match the contents the patch is applied to). There is no
reason for us to trust what the replacement lines (i.e. new lines
prefixed by "+") say, either, but with this option enabled, we end
up copying these new lines with suspicious whitespace distributions
literally into the patched result. But as long as we keep it, we
should make it do its insane thing consistently.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-03-26 21:42:06 +01:00
|
|
|
test_expect_success 'patch5 fails (leading whitespace differences matter)' '
|
|
|
|
test_must_fail git apply patch5.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 're-create file (with --ignore-whitespace)' '
|
|
|
|
rm -f main.c &&
|
2009-11-28 19:38:55 +01:00
|
|
|
git apply patch1.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'patch5 fails (--no-ignore-whitespace)' '
|
2009-11-28 19:38:55 +01:00
|
|
|
test_must_fail git apply --no-ignore-whitespace patch5.patch
|
2009-08-04 13:16:49 +02:00
|
|
|
'
|
|
|
|
|
2017-11-16 19:50:31 +01:00
|
|
|
test_expect_success 'apply --ignore-space-change --inaccurate-eof' '
|
|
|
|
echo 1 >file &&
|
|
|
|
git apply --ignore-space-change --inaccurate-eof <<-\EOF &&
|
|
|
|
diff --git a/file b/file
|
|
|
|
--- a/file
|
|
|
|
+++ b/file
|
|
|
|
@@ -1 +1 @@
|
|
|
|
-1
|
|
|
|
+2
|
|
|
|
EOF
|
|
|
|
printf 2 >expect &&
|
|
|
|
test_cmp expect file
|
|
|
|
'
|
|
|
|
|
2009-08-04 13:16:49 +02:00
|
|
|
test_done
|