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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Mar 10, 2022 at 2:13 PM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> 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@xxxxxxxxxxxxxxxxxxxx>
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@xxxxxxxxxxxxxxxxxxxx>
---
 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


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux