Skip to content

Commit 14d3bb4

Browse files
committed
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 <[email protected]>
1 parent 7bbc4e8 commit 14d3bb4

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

builtin/apply.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,13 @@ static int fuzzy_matchlines(const char *s1, size_t n1,
300300
while ((*last2 == '\r') || (*last2 == '\n'))
301301
last2--;
302302

303-
/* skip leading whitespace */
304-
while (isspace(*s1) && (s1 <= last1))
305-
s1++;
306-
while (isspace(*s2) && (s2 <= last2))
307-
s2++;
303+
/* skip leading whitespaces, if both begin with whitespace */
304+
if (s1 <= last1 && s2 <= last2 && isspace(*s1) && isspace(*s2)) {
305+
while (isspace(*s1) && (s1 <= last1))
306+
s1++;
307+
while (isspace(*s2) && (s2 <= last2))
308+
s2++;
309+
}
308310
/* early return if both lines are empty */
309311
if ((s1 > last1) && (s2 > last2))
310312
return 1;

t/t4107-apply-ignore-whitespace.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ sed -e 's/T/ /g' > main.c.final <<\EOF
111111
#include <stdio.h>
112112
113113
void print_int(int num);
114-
T/* a comment */
115114
int func(int num);
116115
117116
int main() {
@@ -154,7 +153,8 @@ test_expect_success 'patch2 reverse applies with --ignore-space-change' '
154153
git config apply.ignorewhitespace change
155154

156155
test_expect_success 'patch2 applies (apply.ignorewhitespace = change)' '
157-
git apply patch2.patch
156+
git apply patch2.patch &&
157+
test_cmp main.c.final main.c
158158
'
159159

160160
test_expect_success 'patch3 fails (missing string at EOL)' '
@@ -165,12 +165,8 @@ test_expect_success 'patch4 fails (missing EOL at EOF)' '
165165
test_must_fail git apply patch4.patch
166166
'
167167

168-
test_expect_success 'patch5 applies (leading whitespace)' '
169-
git apply patch5.patch
170-
'
171-
172-
test_expect_success 'patches do not mangle whitespace' '
173-
test_cmp main.c main.c.final
168+
test_expect_success 'patch5 fails (leading whitespace differences matter)' '
169+
test_must_fail git apply patch5.patch
174170
'
175171

176172
test_expect_success 're-create file (with --ignore-whitespace)' '

0 commit comments

Comments
 (0)