All of lore.kernel.org
 help / color / mirror / Atom feed
From: Denton Liu <liu.denton@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Cc: Elijah Newren <newren@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Beat Bolli <dev+git@drbeat.li>, Pavel Roskin <plroskin@gmail.com>
Subject: [PATCH 3/5] notes: extract logic into set_display_notes()
Date: Mon, 9 Dec 2019 05:10:44 -0800	[thread overview]
Message-ID: <62543250c4ea0e0327f974cb90b294c60b525982.1575896661.git.liu.denton@gmail.com> (raw)
In-Reply-To: <cover.1575896661.git.liu.denton@gmail.com>

Instead of open coding the logic that tweaks the variables in
`struct display_notes_opt` within handle_revision_opt(), abstract away the
logic into set_display_notes() so that it can be reused.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 notes.c    | 24 ++++++++++++++++++++++++
 notes.h    | 10 ++++++++++
 revision.c | 20 ++++----------------
 3 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/notes.c b/notes.c
index 53d1e7767c..c93feff4ab 100644
--- a/notes.c
+++ b/notes.c
@@ -1045,6 +1045,30 @@ void init_display_notes(struct display_notes_opt *opt)
 	opt->use_default_notes = -1;
 }
 
+int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref)
+{
+	if (show_notes) {
+		if (opt_ref) {
+			struct strbuf buf = STRBUF_INIT;
+			strbuf_addstr(&buf, opt_ref);
+			expand_notes_ref(&buf);
+			string_list_append(&opt->extra_notes_refs,
+					   strbuf_detach(&buf, NULL));
+		} else {
+			opt->use_default_notes = 1;
+		}
+	} else {
+		opt->use_default_notes = -1;
+		/* we have been strdup'ing ourselves, so trick
+		 * string_list into free()ing strings */
+		opt->extra_notes_refs.strdup_strings = 1;
+		string_list_clear(&opt->extra_notes_refs, 0);
+		opt->extra_notes_refs.strdup_strings = 0;
+	}
+
+	return !!show_notes;
+}
+
 void load_display_notes(struct display_notes_opt *opt)
 {
 	char *display_ref_env;
diff --git a/notes.h b/notes.h
index c0b712371c..a476bfa066 100644
--- a/notes.h
+++ b/notes.h
@@ -265,6 +265,16 @@ struct display_notes_opt {
  */
 void init_display_notes(struct display_notes_opt *opt);
 
+/*
+ * Set a display_notes_opt to a given state. 'show_notes' is a boolean
+ * representing whether or not to show notes. 'opt_ref' points to a
+ * string for the notes ref, or is NULL if the default notes should be
+ * used.
+ *
+ * Return 'show_notes' normalized to 1 or 0.
+ */
+int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref);
+
 /*
  * Load the notes machinery for displaying several notes trees.
  *
diff --git a/revision.c b/revision.c
index 24ad974590..c2d8d24939 100644
--- a/revision.c
+++ b/revision.c
@@ -2172,9 +2172,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 			die("'%s': not a non-negative integer", arg);
 		revs->expand_tabs_in_log = val;
 	} else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
-		revs->show_notes = 1;
+		revs->show_notes = set_display_notes(&revs->notes_opt, 1, NULL);
 		revs->show_notes_given = 1;
-		revs->notes_opt.use_default_notes = 1;
 	} else if (!strcmp(arg, "--show-signature")) {
 		revs->show_signature = 1;
 	} else if (!strcmp(arg, "--no-show-signature")) {
@@ -2189,25 +2188,14 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		revs->track_first_time = 1;
 	} else if (skip_prefix(arg, "--show-notes=", &optarg) ||
 		   skip_prefix(arg, "--notes=", &optarg)) {
-		struct strbuf buf = STRBUF_INIT;
-		revs->show_notes = 1;
-		revs->show_notes_given = 1;
 		if (starts_with(arg, "--show-notes=") &&
 		    revs->notes_opt.use_default_notes < 0)
 			revs->notes_opt.use_default_notes = 1;
-		strbuf_addstr(&buf, optarg);
-		expand_notes_ref(&buf);
-		string_list_append(&revs->notes_opt.extra_notes_refs,
-				   strbuf_detach(&buf, NULL));
+		revs->show_notes = set_display_notes(&revs->notes_opt, 1, optarg);
+		revs->show_notes_given = 1;
 	} else if (!strcmp(arg, "--no-notes")) {
-		revs->show_notes = 0;
+		revs->show_notes = set_display_notes(&revs->notes_opt, 0, NULL);
 		revs->show_notes_given = 1;
-		revs->notes_opt.use_default_notes = -1;
-		/* we have been strdup'ing ourselves, so trick
-		 * string_list into free()ing strings */
-		revs->notes_opt.extra_notes_refs.strdup_strings = 1;
-		string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
-		revs->notes_opt.extra_notes_refs.strdup_strings = 0;
 	} else if (!strcmp(arg, "--standard-notes")) {
 		revs->show_notes_given = 1;
 		revs->notes_opt.use_default_notes = 1;
-- 
2.24.0.627.geba02921db


  parent reply	other threads:[~2019-12-09 13:10 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
2019-12-09 13:10 ` [PATCH 1/5] notes: rename to load_display_notes() Denton Liu
2019-12-09 13:10 ` [PATCH 2/5] notes: create init_display_notes() helper Denton Liu
2019-12-09 13:10 ` Denton Liu [this message]
2019-12-09 16:26   ` [PATCH 3/5] notes: extract logic into set_display_notes() Eric Sunshine
2019-12-09 19:19     ` Denton Liu
2019-12-10 11:22       ` Philip Oakley
2019-12-09 13:10 ` [PATCH 4/5] format-patch: use --notes behavior for format.notes Denton Liu
2019-12-09 13:10 ` [PATCH 5/5] format-patch: move git_config() before repo_init_revisions() Denton Liu
2019-12-09 21:36 ` [PATCH 0/5] format-patch: improve handling of `format.notes` Junio C Hamano
2019-12-09 21:42 ` Elijah Newren

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=62543250c4ea0e0327f974cb90b294c60b525982.1575896661.git.liu.denton@gmail.com \
    --to=liu.denton@gmail.com \
    --cc=dev+git@drbeat.li \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=newren@gmail.com \
    --cc=plroskin@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.