git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git List Mailing <git@vger.kernel.org>
Subject: Re: RFC: Using '--no-output-indicator-old' to only show new state
Date: Thu, 10 Mar 2022 16:01:48 -0800	[thread overview]
Message-ID: <CAHk-=wj0ZfmTEhc4iPJSbn_FxzU94qZfK9WcgujKUcZK9a2UvQ@mail.gmail.com> (raw)
In-Reply-To: <CAHk-=whRbuJJ1LzYN9F48JaS7EjuP3FkppHJXi1wAO_qLJQ2xw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 537 bytes --]

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

[-- Attachment #2: 0001-Allow-new-only-old-only-to-only-show-the-new-old-par.patch --]
[-- Type: text/x-patch, Size: 3880 bytes --]

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


  reply	other threads:[~2022-03-11  0:02 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-10 19:40 RFC: Using '--no-output-indicator-old' to only show new state Linus Torvalds
2022-03-10 20:13 ` Junio C Hamano
2022-03-10 20:40   ` Linus Torvalds
2022-03-10 21:26     ` Junio C Hamano
2022-03-10 22:13       ` Linus Torvalds
2022-03-11  0:01         ` Linus Torvalds [this message]
2022-03-11  7:48           ` Ævar Arnfjörð Bjarmason
2022-03-11  9:45             ` [PATCH] parse-options: add per-option flag to stop abbreviation Ævar Arnfjörð Bjarmason
2022-03-11 18:44               ` Linus Torvalds
2022-03-11 19:15             ` RFC: Using '--no-output-indicator-old' to only show new state Linus Torvalds
2022-03-11 19:42               ` Konstantin Ryabitsev

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

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

  git send-email \
    --in-reply-to='CAHk-=wj0ZfmTEhc4iPJSbn_FxzU94qZfK9WcgujKUcZK9a2UvQ@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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.
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).