Skip to content

Commit 1d72975

Browse files
Denton-Lgitster
authored andcommitted
notes: break set_display_notes() into smaller functions
In 8164c96 (format-patch: use --notes behavior for format.notes, 2019-12-09), we introduced set_display_notes() which was a monolithic function with three mutually exclusive branches. Break the function up into three small and simple functions that each are only responsible for one task. This family of functions accepts an `int *show_notes` instead of returning a value suitable for assignment to `show_notes`. This is for two reasons. First of all, this guarantees that the external `show_notes` variable changes in lockstep with the `struct display_notes_opt`. Second, this prompts future developers to be careful about doing something meaningful with this value. In fact, a NULL check is intentionally omitted because causing a segfault here would tell the future developer that they are meant to use the value for something meaningful. One alternative was making the family of functions accept a `struct rev_info *` instead of the `struct display_notes_opt *`, since the former contains the `show_notes` field as well. This does not work because we have to call git_config() before repo_init_revisions(). However, if we had a `struct rev_info`, we'd need to initialize it before it gets assigned values from git_config(). As a result, we break the circular dependency by having standalone `int show_notes` and `struct display_notes_opt notes_opt` variables which temporarily hold values from git_config() until the values are copied over to `rev`. To implement this change, we need to get a pointer to `rev_info::show_notes`. Unfortunately, this is not possible with bitfields and only direct-assignment is possible. Change `rev_info::show_notes` to a non-bitfield int so that we can get its address. Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66f79ee commit 1d72975

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

builtin/log.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,12 @@ static int git_format_config(const char *var, const char *value, void *cb)
868868
}
869869
if (!strcmp(var, "format.notes")) {
870870
int b = git_parse_maybe_bool(value);
871-
show_notes = set_display_notes(&notes_opt, b, b < 0 ? value : NULL);
871+
if (b < 0)
872+
enable_ref_display_notes(&notes_opt, &show_notes, value);
873+
else if (b)
874+
enable_default_display_notes(&notes_opt, &show_notes);
875+
else
876+
disable_display_notes(&notes_opt, &show_notes);
872877
return 0;
873878
}
874879

notes.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,28 +1045,31 @@ void init_display_notes(struct display_notes_opt *opt)
10451045
opt->use_default_notes = -1;
10461046
}
10471047

1048-
int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref)
1048+
void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes)
10491049
{
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-
}
1050+
opt->use_default_notes = 1;
1051+
*show_notes = 1;
1052+
}
10681053

1069-
return !!show_notes;
1054+
void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
1055+
const char *ref) {
1056+
struct strbuf buf = STRBUF_INIT;
1057+
strbuf_addstr(&buf, ref);
1058+
expand_notes_ref(&buf);
1059+
string_list_append(&opt->extra_notes_refs,
1060+
strbuf_detach(&buf, NULL));
1061+
*show_notes = 1;
1062+
}
1063+
1064+
void disable_display_notes(struct display_notes_opt *opt, int *show_notes)
1065+
{
1066+
opt->use_default_notes = -1;
1067+
/* we have been strdup'ing ourselves, so trick
1068+
* string_list into free()ing strings */
1069+
opt->extra_notes_refs.strdup_strings = 1;
1070+
string_list_clear(&opt->extra_notes_refs, 0);
1071+
opt->extra_notes_refs.strdup_strings = 0;
1072+
*show_notes = 0;
10701073
}
10711074

10721075
void load_display_notes(struct display_notes_opt *opt)

notes.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,19 @@ struct display_notes_opt {
266266
void init_display_notes(struct display_notes_opt *opt);
267267

268268
/*
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.
269+
* This family of functions enables or disables the display of notes. In
270+
* particular, 'enable_default_display_notes' will display the default notes,
271+
* 'enable_default_display_notes' will display the notes ref 'ref' and
272+
* 'disable_display_notes' will disable notes, including those added by previous
273+
* invocations of the 'enable_*_display_notes' functions.
274+
*
275+
* 'show_notes' is a points to a boolean which will be set to 1 if notes are
276+
* displayed, else 0. It must not be NULL.
275277
*/
276-
int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref);
278+
void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes);
279+
void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
280+
const char *ref);
281+
void disable_display_notes(struct display_notes_opt *opt, int *show_notes);
277282

278283
/*
279284
* Load the notes machinery for displaying several notes trees.

revision.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
21722172
die("'%s': not a non-negative integer", arg);
21732173
revs->expand_tabs_in_log = val;
21742174
} else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2175-
revs->show_notes = set_display_notes(&revs->notes_opt, 1, NULL);
2175+
enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
21762176
revs->show_notes_given = 1;
21772177
} else if (!strcmp(arg, "--show-signature")) {
21782178
revs->show_signature = 1;
@@ -2191,10 +2191,10 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
21912191
if (starts_with(arg, "--show-notes=") &&
21922192
revs->notes_opt.use_default_notes < 0)
21932193
revs->notes_opt.use_default_notes = 1;
2194-
revs->show_notes = set_display_notes(&revs->notes_opt, 1, optarg);
2194+
enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
21952195
revs->show_notes_given = 1;
21962196
} else if (!strcmp(arg, "--no-notes")) {
2197-
revs->show_notes = set_display_notes(&revs->notes_opt, 0, NULL);
2197+
disable_display_notes(&revs->notes_opt, &revs->show_notes);
21982198
revs->show_notes_given = 1;
21992199
} else if (!strcmp(arg, "--standard-notes")) {
22002200
revs->show_notes_given = 1;

revision.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ struct rev_info {
177177
always_show_header:1;
178178

179179
/* Format info */
180+
int show_notes;
180181
unsigned int shown_one:1,
181182
shown_dashes:1,
182183
show_merge:1,
183-
show_notes:1,
184184
show_notes_given:1,
185185
show_signature:1,
186186
pretty_given:1,

0 commit comments

Comments
 (0)