notes: extract logic into set_display_notes()
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>
Denton Liu committed
Dec 9, 2019 at 05:10 UTC
452538c3586a76939faf43019fb7c21b3147309b
3 files changed
+38
-16
notes.c
+24
@@ -1045,6 +1045,30 @@ void init_display_notes(struct display_notes_opt *opt)
1045
opt->use_default_notes = -1;
1046
}
1047
1048
+int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref)
1049
+{
1050
+ if (show_notes) {
1051
+ if (opt_ref) {
1052
+ struct strbuf buf = STRBUF_INIT;
1053
+ strbuf_addstr(&buf, opt_ref);
1054
+ expand_notes_ref(&buf);
1055
+ string_list_append(&opt->extra_notes_refs,
1056
+ strbuf_detach(&buf, NULL));
1057
+ } else {
1058
+ opt->use_default_notes = 1;
1059
+ }
1060
+ } else {
1061
+ opt->use_default_notes = -1;
1062
+ /* we have been strdup'ing ourselves, so trick
1063
+ * string_list into free()ing strings */
1064
+ opt->extra_notes_refs.strdup_strings = 1;
1065
+ string_list_clear(&opt->extra_notes_refs, 0);
1066
+ opt->extra_notes_refs.strdup_strings = 0;
1067
+ }
1068
+
1069
+ return !!show_notes;
1070
+}
1071
+
1072
void load_display_notes(struct display_notes_opt *opt)
1073
{
1074
char *display_ref_env;
notes.h
+10
@@ -265,6 +265,16 @@ struct display_notes_opt {
265
*/
266
void init_display_notes(struct display_notes_opt *opt);
267
268
+/*
269
+ * Set a display_notes_opt to a given state. 'show_notes' is a boolean
270
+ * representing whether or not to show notes. 'opt_ref' points to a
271
+ * string for the notes ref, or is NULL if the default notes should be
272
+ * used.
273
+ *
274
+ * Return 'show_notes' normalized to 1 or 0.
275
+ */
276
+int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref);
277
+
278
/*
279
* Load the notes machinery for displaying several notes trees.
280
*
revision.c
+4
-16
@@ -2172,9 +2172,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
2172
die("'%s': not a non-negative integer", arg);
2173
revs->expand_tabs_in_log = val;
2174
} else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2175
- revs->show_notes = 1;
2175
+ revs->show_notes = set_display_notes(&revs->notes_opt, 1, NULL);
2176
revs->show_notes_given = 1;
2177
- revs->notes_opt.use_default_notes = 1;
2177
} else if (!strcmp(arg, "--show-signature")) {
2178
revs->show_signature = 1;
2179
} 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
2188
revs->track_first_time = 1;
2189
} else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2190
skip_prefix(arg, "--notes=", &optarg)) {
2192
- struct strbuf buf = STRBUF_INIT;
2193
- revs->show_notes = 1;
2194
- revs->show_notes_given = 1;
2191
if (starts_with(arg, "--show-notes=") &&
2192
revs->notes_opt.use_default_notes < 0)
2193
revs->notes_opt.use_default_notes = 1;
2198
- strbuf_addstr(&buf, optarg);
2199
- expand_notes_ref(&buf);
2200
- string_list_append(&revs->notes_opt.extra_notes_refs,
2201
- strbuf_detach(&buf, NULL));
2194
+ revs->show_notes = set_display_notes(&revs->notes_opt, 1, optarg);
2195
+ revs->show_notes_given = 1;
2196
} else if (!strcmp(arg, "--no-notes")) {
2203
- revs->show_notes = 0;
2197
+ revs->show_notes = set_display_notes(&revs->notes_opt, 0, NULL);
2198
revs->show_notes_given = 1;
2205
- revs->notes_opt.use_default_notes = -1;
2206
- /* we have been strdup'ing ourselves, so trick
2207
- * string_list into free()ing strings */
2208
- revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2209
- string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2210
- revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2199
} else if (!strcmp(arg, "--standard-notes")) {
2200
revs->show_notes_given = 1;
2201
revs->notes_opt.use_default_notes = 1;