git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Jim Meyering <jim@meyering.net>
Date
Dec 17, 2006, 19:32 UTC
Message-ID
<87y7p6nwsh.fsf@rho.meyering.net>

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(-)
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);
--
Next: Linus Torvalds
Message 1 of 6 in “git-diff: don't add trailing blanks (i.e., do what GNU diff -u now does)”
  1. git-diff: don't add trailing blanks (i.e., do what GNU diff -u now does)Jim Meyering, Dec 17, 2006
  2. Linus TorvaldsDec 17, 2006
  3. Junio C HamanoDec 17, 2006
  4. Jim MeyeringDec 17, 2006
  5. Jakub NarebskiDec 17, 2006
  6. Johannes SchindelinDec 18, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.