git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Christian Brabandt <cblists@256bit.org>, Christian Brabandt <cb@256bit.org>
To: git@vger.kernel.org
Subject: Mark trailing whitespace error in del lines of diff
Date: Mon, 25 May 2015 23:11:34 +0200	[thread overview]
Message-ID: <9b8e349e223dc9cd871fc5f7915e590548322932.1432585659.git.cb@256bit.org> (raw)

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;

             reply	other threads:[~2015-05-25 21:51 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-25 21:11 Christian Brabandt, Christian Brabandt [this message]
2015-05-25 22:22 ` Mark trailing whitespace error in del lines of diff 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9b8e349e223dc9cd871fc5f7915e590548322932.1432585659.git.cb@256bit.org \
    --to=cblists@256bit.org \
    --cc=cb@256bit.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).