git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Mark trailing whitespace error in del lines of diff
@ 2015-05-25 21:11 Christian Brabandt, Christian Brabandt
  2015-05-25 22:22 ` brian m. carlson
  2015-05-26  0:24 ` Mark trailing whitespace error in del lines of diff Junio C Hamano
  0 siblings, 2 replies; 30+ messages in thread
From: Christian Brabandt, Christian Brabandt @ 2015-05-25 21:11 UTC (permalink / raw)
  To: git

Currently git-diff only highlights trailing whitespace in the new lines
(prefixed with '+'), thus it is not visible in the deleted lines
(prefixed with '-').

Therefore introduce a new configuration variable for the core.whitespace
setting "blank-at-eol-old" (default off) that will highlight trailing
whitespace in those lines as well.

Signed-off-by: Christian Brabandt <cb@256bit.org>
---

Hi,
please be gentle, this is the first time I contribute to the git 
development.

Here is my use case: I have been working in a team repository, 
reformatting the source and wondered, why my reformatting did introduce 
some trailing whitespace. I suspected a bug in Vim and started to debug 
it, until I found out, that git-diff simply does not show trailing 
whitespace in the deleted lines. Therefore, I'd like to have an option, 
to also show trailing whitespace in the deleted lines of a diff. So here 
is the patch.

As far as I can see, this does not break any tests and also the 
behaviour of git-diff --check does not change. 

 Documentation/config.txt | 2 ++
 cache.h                  | 1 +
 diff.c                   | 8 +++++++-
 ws.c                     | 8 ++++++--
 4 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 0f668bb..f73f0f7 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -670,6 +670,8 @@ core.whitespace::
 +
 * `blank-at-eol` treats trailing whitespaces at the end of the line
   as an error (enabled by default).
+* `blank-at-eol-old` like `blank-at-eol`, but for the deleted lines
+  of a patch (i.e. those preceeded with a '-') (not enabled by default)
 * `space-before-tab` treats a space character that appears immediately
   before a tab character in the initial indent part of the line as an
   error (enabled by default).
diff --git a/cache.h b/cache.h
index 1f4226b..811b640 100644
--- a/cache.h
+++ b/cache.h
@@ -1618,6 +1618,7 @@ void shift_tree_by(const unsigned char *, const unsigned char *, unsigned char *
 #define WS_CR_AT_EOL           01000
 #define WS_BLANK_AT_EOF        02000
 #define WS_TAB_IN_INDENT       04000
+#define WS_BLANK_AT_EOL_OLD    010000
 #define WS_TRAILING_SPACE      (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
 #define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
 #define WS_TAB_WIDTH_MASK        077
diff --git a/diff.c b/diff.c
index 7500c55..4245956 100644
--- a/diff.c
+++ b/diff.c
@@ -1254,10 +1254,16 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 		const char *color =
 			diff_get_color(ecbdata->color_diff,
 				       line[0] == '-' ? DIFF_FILE_OLD : DIFF_PLAIN);
+		const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
+
 		ecbdata->lno_in_preimage++;
 		if (line[0] == ' ')
 			ecbdata->lno_in_postimage++;
-		emit_line(ecbdata->opt, color, reset, line, len);
+		if (*ws && ecbdata->ws_rule & WS_BLANK_AT_EOL_OLD)
+			ws_check_emit(line, len, ecbdata->ws_rule,
+				ecbdata->opt->file, color, reset, ws);
+		else
+			emit_line(ecbdata->opt, color, reset, line, len);
 	} else {
 		ecbdata->lno_in_postimage++;
 		emit_add_line(reset, ecbdata, line + 1, len - 1);
diff --git a/ws.c b/ws.c
index ea4b2b1..09e04f0 100644
--- a/ws.c
+++ b/ws.c
@@ -18,6 +18,7 @@ static struct whitespace_rule {
 	{ "indent-with-non-tab", WS_INDENT_WITH_NON_TAB, 0 },
 	{ "cr-at-eol", WS_CR_AT_EOL, 1 },
 	{ "blank-at-eol", WS_BLANK_AT_EOL, 0 },
+	{ "blank-at-eol-del", WS_BLANK_AT_EOL_OLD, 0, 1 },
 	{ "blank-at-eof", WS_BLANK_AT_EOF, 0 },
 	{ "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 },
 };
@@ -170,11 +171,14 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
 	}
 
 	/* Check for trailing whitespace. */
-	if (ws_rule & WS_BLANK_AT_EOL) {
+	if ((ws_rule & WS_BLANK_AT_EOL) || (ws_rule & WS_BLANK_AT_EOL_OLD)) {
 		for (i = len - 1; i >= 0; i--) {
 			if (isspace(line[i])) {
 				trailing_whitespace = i;
-				result |= WS_BLANK_AT_EOL;
+				if (ws_rule & WS_BLANK_AT_EOL)
+					result |= WS_BLANK_AT_EOL;
+				else
+					result |= WS_BLANK_AT_EOL_OLD;
 			}
 			else
 				break;

^ permalink raw reply related	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2015-05-27 20:53 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-25 21:11 Mark trailing whitespace error in del lines of diff Christian Brabandt, Christian Brabandt
2015-05-25 22:22 ` brian m. carlson
2015-05-25 23:27   ` Junio C Hamano
2015-05-25 23:52     ` brian m. carlson
2015-05-26 16:29     ` Christian Brabandt
2015-05-26 17:26       ` Junio C Hamano
2015-05-26 17:34         ` Junio C Hamano
2015-05-26 17:39           ` Christian Brabandt
2015-05-26 17:48             ` Junio C Hamano
2015-05-26 18:21               ` Christian Brabandt
2015-05-26 19:46               ` [PATCH v2 0/5] showing existing ws breakage Junio C Hamano
2015-05-26 19:46                 ` [PATCH v2 1/4] t4015: modernise style Junio C Hamano
2015-05-26 19:46                 ` [PATCH v2 2/4] t4015: separate common setup and per-test expectation Junio C Hamano
2015-05-26 19:46                 ` [PATCH v2 3/4] diff.c: add emit_del_line() and update callers of emit_line_0() Junio C Hamano
2015-05-26 19:46                 ` [PATCH v2 4/4] diff.c: --ws-check-deleted option Junio C Hamano
2015-05-27  6:30                 ` [PATCH v3 0/4] showing existing ws breakage Junio C Hamano
2015-05-27  6:30                   ` [PATCH v3 1/4] t4015: modernise style Junio C Hamano
2015-05-27  6:30                   ` [PATCH v3 2/4] t4015: separate common setup and per-test expectation Junio C Hamano
2015-05-27  6:30                   ` [PATCH v3 3/4] diff.c: add emit_del_line() and emit_context_line() Junio C Hamano
2015-05-27  6:30                   ` [PATCH v3 4/4] diff.c: --ws-error-highlight=<kind> option Junio C Hamano
2015-05-27  7:22                   ` [PATCH v3 0/4] showing existing ws breakage Jeff King
2015-05-27 18:57                     ` Junio C Hamano
2015-05-27 20:36                       ` Jeff King
2015-05-27 20:46                         ` Junio C Hamano
2015-05-27 20:48                           ` Jeff King
2015-05-27 20:53                             ` Junio C Hamano
2015-05-27 20:51                     ` Jeff King
2015-05-26  0:24 ` Mark trailing whitespace error in del lines of diff Junio C Hamano
2015-05-26 16:31   ` Christian Brabandt
2015-05-26 17:33     ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).