]> git.ipfire.org Git - thirdparty/git.git/commitdiff
notes: extract logic into set_display_notes()
authorDenton Liu <liu.denton@gmail.com>
Mon, 9 Dec 2019 13:10:44 +0000 (05:10 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 9 Dec 2019 21:36:45 +0000 (13:36 -0800)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
notes.c
notes.h
revision.c

diff --git a/notes.c b/notes.c
index 53d1e7767ce459e53aa4ffeae129672d599d1859..c93feff4abd306bc9fdc59aa775e3a0c924cfd2a 100644 (file)
--- 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 c0b712371cc28df7aa45aa10b0af01bc2bdc3a42..a476bfa06659a9d6c8bc12538f150cde06898b86 100644 (file)
--- 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.
  *
index 24ad974590bf6609586d53be3b78cfab89470510..c2d8d24939dd7eb6b892accbd1680ec3cedf5ffb 100644 (file)
@@ -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;