threads / patch / 43447

patchRe: [PATCH] git-diff: don't add trailing blanks (i.e., do what GNU diff -u now does)

Subject: Re: [PATCH] git-diff: don't add trailing blanks (i.e., do what GNU diff -u now does)

## tl;dr

6 messages between Dec 17, 2006 and Dec 18, 2006. Diffs are folded; open one to read it.

replies: 5people: 5as markdown or json

Jim Meyering· Dec 17, 2006, 19:32 UTC · lore

[PATCH] git-diff: don't add trailing blanks (i.e., do what GNU diff -u now does)

You may recall that GNU diff -u changed recently so that it no longer outputs any trailing space unless the input data has it. This means that blank context lines are now blank also in diff -u output. Before, they would have a single trailing space.

Then, git was changed to allow that new diff output format. Now that git-diff generates output using its internal diff, its output is no longer identical to what you get when using GNU diff.

This patch makes the output of git-diff the same as GNU diff's.
Signed-off-by: Jim Meyering <jim@meyering.net>
---
 t/t4016-diff-trailing-space.sh |   31 +++++++++++++++++++++++++++++++
 xdiff/xutils.c                 |    3 +++
 2 files changed, 34 insertions(+), 0 deletions(-)
Show changes to 2 files +33 −1

t/t4016-diff-trailing-space.sh, xdiff/xutils.c

diff --git a/t/t4016-diff-trailing-space.sh b/t/t4016-diff-trailing-space.sh
new file mode 100755
index 0000000..95c4674
--- /dev/null
+++ b/t/t4016-diff-trailing-space.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+#
+# Copyright (c) Jim Meyering
+#
+test_description='diff does not add trailing spaces'
+
+. ./test-lib.sh
+
+cat <<\EOF > exp ||
+diff --git a/f b/f
+index 5f6a263..8cb8bae 100644
+--- a/f
++++ b/f
+@@ -1,2 +1,2 @@
+
+-x
++y
+EOF
+exit 1
+
+test_expect_success \
+    "$test_description" \
+    '(echo; echo x) > f &&
+     git-add f &&
+     git-commit -q -m. f &&
+     (echo; echo y) > f &&
+     git-diff f > actual &&
+     cmp exp actual
+     '
+
+test_done
diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index 1b899f3..8b7380a 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -51,6 +51,9 @@ int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
 	mb[0].size = psize;
 	mb[1].ptr = (char *) rec;
 	mb[1].size = size;
+	/* Don't emit a trailing space for an empty context line.  */
+	if (size == 1 && rec[0] == '\n' && psize == 1 && *pre == ' ')
+		mb[0].size = 0;
 	if (size > 0 && rec[size - 1] != '\n') {
 		mb[2].ptr = (char *) "\n\\ No newline at end of file\n";
 		mb[2].size = strlen(mb[2].ptr);
--
Linus Torvalds· Dec 17, 2006, 20:00 UTC · re: Jim Meyering · lore
On Sun, 17 Dec 2006, Jim Meyering wrote:
>
> You may recall that GNU diff -u changed recently so that it no
> longer outputs any trailing space unless the input data has it.
I still consider that to be a bug in GNU "diff -u".

We work around that bug when applying patches, but I don't think we should replicate it.

Junio C Hamano· Dec 17, 2006, 20:09 UTC · re: Linus Torvalds · lore
Linus Torvalds <torvalds@osdl.org> writes:
Show 9 quoted lines
> On Sun, 17 Dec 2006, Jim Meyering wrote:
>>
>> You may recall that GNU diff -u changed recently so that it no
>> longer outputs any trailing space unless the input data has it.
>
> I still consider that to be a bug in GNU "diff -u".
>
> We work around that bug when applying patches, but I don't think we should 
> replicate it.
Me neither.
Jim Meyering· Dec 17, 2006, 20:36 UTC · re: Junio C Hamano · lore
Junio C Hamano <junkio@cox.net> wrote:
Show 11 quoted lines
> Linus Torvalds <torvalds@osdl.org> writes:
>> On Sun, 17 Dec 2006, Jim Meyering wrote:
>>> You may recall that GNU diff -u changed recently so that it no
>>> longer outputs any trailing space unless the input data has it.
>>
>> I still consider that to be a bug in GNU "diff -u".
>>
>> We work around that bug when applying patches, but I don't think we should
>> replicate it.
>
> Me neither.
I can't say I didn't expect this.
Jakub Narebski· Dec 17, 2006, 20:54 UTC · re: Junio C Hamano · lore
Junio C Hamano wrote:
Show 13 quoted lines
> Linus Torvalds <torvalds@osdl.org> writes:
> 
>> On Sun, 17 Dec 2006, Jim Meyering wrote:
>>>
>>> You may recall that GNU diff -u changed recently so that it no
>>> longer outputs any trailing space unless the input data has it.
>>
>> I still consider that to be a bug in GNU "diff -u".
>>
>> We work around that bug when applying patches, but I don't think we should 
>> replicate it.
> 
> Me neither.
Perhaps with --gnu-diff-compatibility then? 
-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
Johannes Schindelin· Dec 18, 2006, 12:06 UTC · re: Jakub Narebski · lore
Hi,
On Sun, 17 Dec 2006, Jakub Narebski wrote:
Show 17 quoted lines
> Junio C Hamano wrote:
> 
> > Linus Torvalds <torvalds@osdl.org> writes:
> > 
> >> On Sun, 17 Dec 2006, Jim Meyering wrote:
> >>>
> >>> You may recall that GNU diff -u changed recently so that it no
> >>> longer outputs any trailing space unless the input data has it.
> >>
> >> I still consider that to be a bug in GNU "diff -u".
> >>
> >> We work around that bug when applying patches, but I don't think we should 
> >> replicate it.
> > 
> > Me neither.
> 
> Perhaps with --gnu-diff-compatibility then? 
Rather --braindead-gnu-bug-compatibility.

Ciao, Dscho

← back to recent threads