Skip to content

Commit c122c9a

Browse files
committed
Merge branch 'jc/apply-ignore-whitespace' into maint
"--ignore-space-change" option of "git apply" ignored the spaces at the beginning of line too aggressively, which is inconsistent with the option of the same name "diff" and "git diff" have. * jc/apply-ignore-whitespace: apply --ignore-space-change: lines with and without leading whitespaces do not match
2 parents ff7e96b + 14d3bb4 commit c122c9a

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)