Skip to content

Commit

Permalink
add boolean diff.suppress-blank-empty config option
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jim Meyering authored and Junio C Hamano committed Aug 20, 2008
1 parent c6670b3 commit a624eaa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Documentation/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,10 @@ diff.autorefreshindex::
affects only 'git-diff' Porcelain, and not lower level
'diff' commands, such as 'git-diff-files'.

diff.suppress-blank-empty::
A boolean to inhibit the standard behavior of printing a space
before each empty output line. Defaults to false.

diff.external::
If this config variable is set, diff generation is not
performed using the internal diff machinery, but using the
Expand Down
13 changes: 13 additions & 0 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

static int diff_detect_rename_default;
static int diff_rename_limit_default = 200;
static int diff_suppress_blank_empty;
int diff_use_color_default = -1;
static const char *external_diff_cmd_cfg;
int diff_auto_refresh_index = 1;
Expand Down Expand Up @@ -176,6 +177,12 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
return 0;
}

/* like GNU diff's --suppress-blank-empty option */
if (!strcmp(var, "diff.suppress-blank-empty")) {
diff_suppress_blank_empty = git_config_bool(var, value);
return 0;
}

if (!prefixcmp(var, "diff.")) {
const char *ep = strrchr(var, '.');
if (ep != var + 4) {
Expand Down Expand Up @@ -580,6 +587,12 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
}

if (diff_suppress_blank_empty
&& len == 2 && line[0] == ' ' && line[1] == '\n') {
line[0] = '\n';
len = 1;
}

/* This is not really necessary for now because
* this codepath only deals with two-way diffs.
*/
Expand Down
39 changes: 39 additions & 0 deletions t/t4029-diff-trailing-space.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
#
# Copyright (c) Jim Meyering
#
test_description='diff honors config option, diff.suppress-blank-empty'

. ./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" \
'printf "\nx\n" > f &&
git add f &&
git commit -q -m. f &&
printf "\ny\n" > f &&
git config --bool diff.suppress-blank-empty true &&
git diff f > actual &&
test_cmp exp actual &&
perl -i.bak -p -e "s/^\$/ /" exp &&
git config --bool diff.suppress-blank-empty false &&
git diff f > actual &&
test_cmp exp actual &&
git config --bool --unset diff.suppress-blank-empty &&
git diff f > actual &&
test_cmp exp actual
'

test_done

0 comments on commit a624eaa

Please sign in to comment.