Skip to content

Commit 116761b

Browse files
peffgitster
authored andcommitted
commit: avoid writing to global in option callback
The callback function for --trailer writes directly to the global trailer_args and ignores opt->value completely. This is OK, since that's where we expect to find the value. But it does mean the option declaration isn't as clear. E.g., we have: OPT_BOOL(0, "reset-author", &renew_authorship, ...), OPT_CALLBACK_F(0, "trailer", NULL, ..., opt_pass_trailer) In the first one we can see where the result will be stored, but in the second, we get only NULL, and you have to go read the callback. Let's pass &trailer_args, and use it in the callback. As a bonus, this silences a -Wunused-parameter warning. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7faba18 commit 116761b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static int opt_pass_trailer(const struct option *opt, const char *arg, int unset
139139
{
140140
BUG_ON_OPT_NEG(unset);
141141

142-
strvec_pushl(&trailer_args, "--trailer", arg, NULL);
142+
strvec_pushl(opt->value, "--trailer", arg, NULL);
143143
return 0;
144144
}
145145

@@ -1633,7 +1633,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16331633
OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
16341634
OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
16351635
OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1636-
OPT_CALLBACK_F(0, "trailer", NULL, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer),
1636+
OPT_CALLBACK_F(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer),
16371637
OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
16381638
OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
16391639
OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),

0 commit comments

Comments
 (0)