[PATCH] git-diff: don't add trailing blanks (i.e., do what GNU diff -u now does)
From: Jim Meyering <hidden>
Date: 2016-08-11 19:43:41
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 <redacted> --- 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); --