git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Mark trailing whitespace error in del lines of diff

From
Junio C Hamano <gitster@pobox.com>
Date
May 26, 2015, 17:34 UTC
Message-ID
<xmqq617fkztj.fsf@gitster.dls.corp.google.com>
In-Reply-To
<xmqqegm3l06u.fsf@gitster.dls.corp.google.com>
Junio C Hamano <gitster@pobox.com> writes:
> I'll send out two patch series to do the painting part (I didn't
> want to touch "--check", as its utility is even more dubious
> compared to painting, at least to me).
And here is the second one.
-- >8 --
Subject: [PATCH 2/2] diff.c: --ws-check-deleted option

Traditionally, we only cared about whitespace breakages introduced in new lines. Some people want to paint whitespace breakages on old lines, too. When they see a whitespace breakage on a new line, they can spot the same kind of whitespace breakage on the corresponding old line and want to say "Ah, that breakage is there but was inherited from the original, so let's not fix that for now."

Enable such use with the new option, "--ws-check-deleted".
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/diff-options.txt |  7 +++++++
 diff.c                         | 21 ++++++++++++++++++++-
 diff.h                         |  1 +
 3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index b7c3afe..617cbc6 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -282,6 +282,13 @@ ifndef::git-format-patch[]
 	initial indent of the line are considered whitespace errors.
 	Exits with non-zero status if problems are found. Not compatible
 	with --exit-code.
+
+--ws-check-deleted::
+--no-ws-check-deleted::
+	Highlight whitespace errors in deleted lines in the color
+	specified by `color.diff.whitespace`, as well as in added
+	lines.
+
 endif::git-format-patch[]
 
 --full-index::
diff --git a/diff.c b/diff.c
index 93c1eb4..44cc234 100644
--- a/diff.c
+++ b/diff.c
@@ -503,8 +503,22 @@ static void emit_del_line(const char *reset,
 			  const char *line, int len)
 {
 	const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_OLD);
+	const char *ws = NULL;
 
-	emit_line_0(ecbdata->opt, set, reset, '-', line, len);
+	if (ecbdata->opt->ws_check_deleted) {
+		ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
+		if (!*ws)
+			ws = NULL;
+	}
+
+	if (!ws)
+		emit_line_0(ecbdata->opt, set, reset, '-', line, len);
+	else {
+		/* Emit just the prefix, then the rest. */
+		emit_line_0(ecbdata->opt, set, reset, '-', "", 0);
+		ws_check_emit(line, len, ecbdata->ws_rule,
+			      ecbdata->opt->file, set, reset, ws);
+	}
 }
 
 static void emit_hunk_header(struct emit_callback *ecbdata,
@@ -3819,6 +3833,11 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 	else if (skip_prefix(arg, "--submodule=", &arg))
 		return parse_submodule_opt(options, arg);
 
+	else if (!strcmp(arg, "--ws-check-deleted"))
+		options->ws_check_deleted = 1;
+	else if (!strcmp(arg, "--no-ws-check-deleted"))
+		options->ws_check_deleted = 0;
+
 	/* misc options */
 	else if (!strcmp(arg, "-z"))
 		options->line_termination = 0;
diff --git a/diff.h b/diff.h
index f6fdf49..baca5ec 100644
--- a/diff.h
+++ b/diff.h
@@ -138,6 +138,7 @@ struct diff_options {
 	int dirstat_permille;
 	int setup;
 	int abbrev;
+	int ws_check_deleted;
 	const char *prefix;
 	int prefix_length;
 	const char *stat_sep;
-- 
2.4.1-511-gc1146d5
Previous: Junio C HamanoNext: Christian Brabandt
Message 7 of 30 in “Mark trailing whitespace error in del lines of diff”
  1. Christian BrabandtMay 25, 2015
  2. brian m. carlsonMay 25, 2015
  3. Junio C HamanoMay 25, 2015
  4. brian m. carlsonMay 25, 2015
  5. Christian BrabandtMay 26, 2015
  6. Junio C HamanoMay 26, 2015
  7. Junio C HamanoMay 26, 2015
  8. Christian BrabandtMay 26, 2015
  9. Junio C HamanoMay 26, 2015
  10. Christian BrabandtMay 26, 2015
  11. 0/5 showing existing ws breakageJunio C Hamano, May 26, 2015
  12. 1/4 t4015: modernise styleJunio C Hamano, May 26, 2015
  13. 2/4 t4015: separate common setup and per-test expectationJunio C Hamano, May 26, 2015
  14. 3/4 diff.c: add emit_del_line() and update callers of emit_line_0()Junio C Hamano, May 26, 2015
  15. 4/4 diff.c: --ws-check-deleted optionJunio C Hamano, May 26, 2015
  16. 0/4 showing existing ws breakageJunio C Hamano, May 27, 2015
  17. 1/4 t4015: modernise styleJunio C Hamano, May 27, 2015
  18. 2/4 t4015: separate common setup and per-test expectationJunio C Hamano, May 27, 2015
  19. 3/4 diff.c: add emit_del_line() and emit_context_line()Junio C Hamano, May 27, 2015
  20. 4/4 diff.c: --ws-error-highlight=<kind> optionJunio C Hamano, May 27, 2015
  21. Jeff KingMay 27, 2015
  22. Junio C HamanoMay 27, 2015
  23. Jeff KingMay 27, 2015
  24. Junio C HamanoMay 27, 2015
  25. Jeff KingMay 27, 2015
  26. Junio C HamanoMay 27, 2015
  27. Jeff KingMay 27, 2015
  28. Junio C HamanoMay 26, 2015
  29. Christian BrabandtMay 26, 2015
  30. Junio C HamanoMay 26, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.