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

Re: RFC: Using '--no-output-indicator-old' to only show new state

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Mar 11, 2022, 00:01 UTC
Message-ID
<CAHk-=wj0ZfmTEhc4iPJSbn_FxzU94qZfK9WcgujKUcZK9a2UvQ@mail.gmail.com>
In-Reply-To
<CAHk-=whRbuJJ1LzYN9F48JaS7EjuP3FkppHJXi1wAO_qLJQ2xw@mail.gmail.com>

On Thu, Mar 10, 2022 at 2:13 PM Linus Torvalds <torvalds@linux-foundation.org> wrote:

>
> Here's the stupid patch that "works" but doesn't allow the shortened
> version. Maybe somebody can point out what silly thing I did wrong.

I just created a short alias to do this. Maybe there's some smarter option, but this seems to work.

I've updated the commit message - I kept the --no-output-indicator-xyz form since it really logically ends up being exactly that, but I guess those changes could also be dropped.

Hmm?
              Linus
From 1d207585369ec1708da5549176a79bb22c472ee2 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <torvalds@linux-foundation.org>
Date: Thu, 10 Mar 2022 11:00:43 -0800
Subject: [PATCH] Allow '--new-only/--old-only' to only show the new/old parts
 of a diff

This is particularly useful if you want to just see the end result of a diff, without the original lines that have been removed (or, less commonly, the reverse).

This a fairly useful model in various GUI diff viewers, with 'gitk' for example having the option to show the diff, the old state, or the new state. It's not unusual to want to just see "what is the end result of my changes" without seeing (a) everything that didn't change and (b) the old removed state.

The option name can also be abbreviated to just "--new" and "--old".
Example:
    git show --new 04bf052eef

to see just the end result of the changes in commit 04bf052eef ("grep: simplify config parsing and option parsing").

Internally this is simply implemented as setting the output indicator for old and new lines, and you can in fact also do this with that fairly cumbersome interface (ie "--no-output-indicator-old" disables the old lines, and thus acts as "--new").

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---
 diff.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/diff.c b/diff.c
index 2bd5e0d817..f37f0b383a 100644
--- a/diff.c
+++ b/diff.c
@@ -1254,6 +1254,8 @@ static void emit_line_ws_markup(struct diff_options *o,
 	const char *ws = NULL;
 	int sign = o->output_indicators[sign_index];
 
+	if (!sign)
+		return;
 	if (o->ws_error_highlight & ws_rule) {
 		ws = diff_get_color_opt(o, DIFF_WHITESPACE);
 		if (!*ws)
@@ -4986,6 +4988,10 @@ static int diff_opt_char(const struct option *opt,
 {
 	char *value = opt->value;
 
+	if (unset) {
+		*value = 0;
+		return 0;
+	}
 	BUG_ON_OPT_NEG(unset);
 	if (arg[1])
 		return error(_("%s expects a character, got '%s'"),
@@ -4994,6 +5000,17 @@ static int diff_opt_char(const struct option *opt,
 	return 0;
 }
 
+static int diff_opt_no_char(const struct option *opt,
+			 const char *arg, int unset)
+{
+	char *value = opt->value;
+
+	BUG_ON_OPT_NEG(unset);
+	BUG_ON_OPT_ARG(arg);
+	*value = 0;
+	return 0;
+}
+
 static int diff_opt_color_moved(const struct option *opt,
 				const char *arg, int unset)
 {
@@ -5476,17 +5493,27 @@ static void prep_parse_options(struct diff_options *options)
 			       &options->output_indicators[OUTPUT_INDICATOR_NEW],
 			       N_("<char>"),
 			       N_("specify the character to indicate a new line instead of '+'"),
-			       PARSE_OPT_NONEG, diff_opt_char),
+			       0, diff_opt_char),
 		OPT_CALLBACK_F(0, "output-indicator-old",
 			       &options->output_indicators[OUTPUT_INDICATOR_OLD],
 			       N_("<char>"),
 			       N_("specify the character to indicate an old line instead of '-'"),
-			       PARSE_OPT_NONEG, diff_opt_char),
+			       0, diff_opt_char),
 		OPT_CALLBACK_F(0, "output-indicator-context",
 			       &options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
 			       N_("<char>"),
 			       N_("specify the character to indicate a context instead of ' '"),
 			       PARSE_OPT_NONEG, diff_opt_char),
+		OPT_CALLBACK_F(0, "new-only",
+			       &options->output_indicators[OUTPUT_INDICATOR_OLD], NULL,
+			       N_("show only new lines in diff"),
+			       PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_char),
+		OPT_CALLBACK_F(0, "old-only",
+			       &options->output_indicators[OUTPUT_INDICATOR_NEW], NULL,
+			       N_("show only old lines in diff"),
+			       PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_char),
+		OPT_ALIAS(0, "new", "new-only"),
+		OPT_ALIAS(0, "old", "old-only"),
 
 		OPT_GROUP(N_("Diff rename options")),
 		OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
-- 
2.35.1.459.g1d20758536
Previous: Linus TorvaldsNext: Ævar Arnfjörð Bjarmason
Message 6 of 11 in “RFC: Using '--no-output-indicator-old' to only show new state”
  1. Linus TorvaldsMar 10, 2022
  2. Junio C HamanoMar 10, 2022
  3. Linus TorvaldsMar 10, 2022
  4. Junio C HamanoMar 10, 2022
  5. Linus TorvaldsMar 10, 2022
  6. Linus TorvaldsMar 11, 2022
  7. Ævar Arnfjörð BjarmasonMar 11, 2022
  8. parse-options: add per-option flag to stop abbreviationÆvar Arnfjörð Bjarmason, Mar 11, 2022
  9. Linus TorvaldsMar 11, 2022
  10. Linus TorvaldsMar 11, 2022
  11. Konstantin RyabitsevMar 11, 2022

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.