add boolean diff.suppress-blank-empty config option
GNU diff's --suppress-blank-empty option makes it so that diff no longer outputs trailing white space unless the input data has it. With this option, empty context lines are now empty also in diff -u output. Before, they would have a single trailing space. * diff.c (diff_suppress_blank_empty): New global. (git_diff_basic_config): Set it. (fn_out_consume): Honor it. * t/t4029-diff-trailing-space.sh: New file. * Documentation/config.txt: Document it. Signed-off-by: Jim Meyering <meyering@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jim Meyering committed
Aug 15, 2008 at 13:39 UTC
a624eaa7820f4f9814e41e911c665a0aba2fce34
3 files changed
+56
Documentation/config.txt
+4
@@ -567,6 +567,10 @@ diff.autorefreshindex::
567
affects only 'git-diff' Porcelain, and not lower level
568
'diff' commands, such as 'git-diff-files'.
569
570
+diff.suppress-blank-empty::
571
+ A boolean to inhibit the standard behavior of printing a space
572
+ before each empty output line. Defaults to false.
573
+
574
diff.external::
575
If this config variable is set, diff generation is not
576
performed using the internal diff machinery, but using the
diff.c
+13
@@ -20,6 +20,7 @@
20
21
static int diff_detect_rename_default;
22
static int diff_rename_limit_default = 200;
23
+static int diff_suppress_blank_empty;
24
int diff_use_color_default = -1;
25
static const char *external_diff_cmd_cfg;
26
int diff_auto_refresh_index = 1;
@@ -176,6 +177,12 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
177
return 0;
178
}
179
180
+ /* like GNU diff's --suppress-blank-empty option */
181
+ if (!strcmp(var, "diff.suppress-blank-empty")) {
182
+ diff_suppress_blank_empty = git_config_bool(var, value);
183
+ return 0;
184
+ }
185
+
186
if (!prefixcmp(var, "diff.")) {
187
const char *ep = strrchr(var, '.');
188
if (ep != var + 4) {
@@ -580,6 +587,12 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
587
ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
588
}
589
590
+ if (diff_suppress_blank_empty
591
+ && len == 2 && line[0] == ' ' && line[1] == '\n') {
592
+ line[0] = '\n';
593
+ len = 1;
594
+ }
595
+
596
/* This is not really necessary for now because
597
* this codepath only deals with two-way diffs.
598
*/
t/t4029-diff-trailing-space.sh
new
+39
@@ -0,0 +1,39 @@
1
+#!/bin/bash
2
+#
3
+# Copyright (c) Jim Meyering
4
+#
5
+test_description='diff honors config option, diff.suppress-blank-empty'
6
+
7
+. ./test-lib.sh
8
+
9
+cat <<\EOF > exp ||
10
+diff --git a/f b/f
11
+index 5f6a263..8cb8bae 100644
12
+--- a/f
13
++++ b/f
14
+@@ -1,2 +1,2 @@
15
+
16
+-x
17
++y
18
+EOF
19
+exit 1
20
+
21
+test_expect_success \
22
+ "$test_description" \
23
+ 'printf "\nx\n" > f &&
24
+ git add f &&
25
+ git commit -q -m. f &&
26
+ printf "\ny\n" > f &&
27
+ git config --bool diff.suppress-blank-empty true &&
28
+ git diff f > actual &&
29
+ test_cmp exp actual &&
30
+ perl -i.bak -p -e "s/^\$/ /" exp &&
31
+ git config --bool diff.suppress-blank-empty false &&
32
+ git diff f > actual &&
33
+ test_cmp exp actual &&
34
+ git config --bool --unset diff.suppress-blank-empty &&
35
+ git diff f > actual &&
36
+ test_cmp exp actual
37
+ '
38
+
39
+test_done