Skip to content

Commit 859f9c4

Browse files
dschoJunio C Hamano
authored andcommitted
teach diff machinery about --ignore-space-at-eol
`git diff --ignore-space-at-eol` will ignore whitespace at the line ends. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 44478d9 commit 859f9c4

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
20382038
options->xdl_opts |= XDF_IGNORE_WHITESPACE;
20392039
else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
20402040
options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2041+
else if (!strcmp(arg, "--ignore-space-at-eol"))
2042+
options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
20412043
else if (!strcmp(arg, "--color-words"))
20422044
options->color_diff = options->color_diff_words = 1;
20432045
else if (!strcmp(arg, "--no-renames"))

xdiff/xdiff.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ extern "C" {
3131
#define XDF_NEED_MINIMAL (1 << 1)
3232
#define XDF_IGNORE_WHITESPACE (1 << 2)
3333
#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3)
34-
#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)
34+
#define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 4)
35+
#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
3536

3637
#define XDL_PATCH_NORMAL '-'
3738
#define XDL_PATCH_REVERSE '+'

xdiff/xutils.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,21 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
215215
return 0;
216216
}
217217
return (i1 >= s1 && i2 >= s2);
218+
} else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL) {
219+
for (i1 = i2 = 0; i1 < s1 && i2 < s2; ) {
220+
if (l1[i1] != l2[i2]) {
221+
while (i1 < s1 && isspace(l1[i1]))
222+
i1++;
223+
while (i2 < s2 && isspace(l2[i2]))
224+
i2++;
225+
if (i1 < s1 || i2 < s2)
226+
return 0;
227+
return 1;
228+
}
229+
i1++;
230+
i2++;
231+
}
232+
return i1 >= s1 && i2 >= s2;
218233
} else
219234
return s1 == s2 && !memcmp(l1, l2, s1);
220235

@@ -227,6 +242,7 @@ unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
227242

228243
for (; ptr < top && *ptr != '\n'; ptr++) {
229244
if (isspace(*ptr) && (flags & XDF_WHITESPACE_FLAGS)) {
245+
const char *ptr2 = ptr;
230246
while (ptr + 1 < top && isspace(ptr[1])
231247
&& ptr[1] != '\n')
232248
ptr++;
@@ -235,6 +251,14 @@ unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
235251
ha += (ha << 5);
236252
ha ^= (unsigned long) ' ';
237253
}
254+
if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
255+
&& ptr[1] != '\n') {
256+
while (ptr2 != ptr + 1) {
257+
ha += (ha << 5);
258+
ha ^= (unsigned long) *ptr2;
259+
ptr2++;
260+
}
261+
}
238262
continue;
239263
}
240264
ha += (ha << 5);

0 commit comments

Comments
 (0)