commit, tag: parse --trailer with OPT_STRVEC

Now that amend_file_with_trailers() expects raw trailer lines, do not store argv-style "--trailer=<trailer>" strings in git commit and git tag. Parse --trailer using OPT_STRVEC so trailer_args contains only the trailer value, and drop the temporary prefix stripping in amend_file_with_trailers(). Signed-off-by: Li Chen <me@linux.beauty> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Li Chen committed Mar 6, 2026 at 14:53 UTC 5e148696bf86f0173dfc91571d15ba833ec19ccd
4 files changed +8 -28
builtin/commit.c
+2 -1
@@ -1720,7 +1720,8 @@ int cmd_commit(int argc,
1720 OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1721 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1722 OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1723 - OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG),
1723 + OPT_STRVEC(0, "trailer", &trailer_args, N_("trailer"),
1724 + N_("add custom trailer(s)")),
1725 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
1726 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1727 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
builtin/tag.c
+2 -2
@@ -499,8 +499,8 @@ int cmd_tag(int argc,
499 OPT_CALLBACK_F('m', "message", &msg, N_("message"),
500 N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg),
501 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
502 - OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args, N_("trailer"),
503 - N_("add custom trailer(s)"), PARSE_OPT_NONEG),
502 + OPT_STRVEC(0, "trailer", &trailer_args, N_("trailer"),
503 + N_("add custom trailer(s)")),
504 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
505 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
506 OPT_CLEANUP(&cleanup_arg),
trailer.c
+2 -23
@@ -1341,46 +1341,25 @@ int amend_file_with_trailers(const char *path,
1341 const struct strvec *trailer_args)
1342 {
1343 struct strbuf buf = STRBUF_INIT;
1344 - struct strvec stripped_trailer_args = STRVEC_INIT;
1344 int ret = 0;
1346 - size_t i;
1345
1346 if (!trailer_args)
1347 BUG("amend_file_with_trailers called with NULL trailer_args");
1348 if (!trailer_args->nr)
1349 return 0;
1350
1353 - for (i = 0; i < trailer_args->nr; i++) {
1354 - const char *txt = trailer_args->v[i];
1355 -
1356 - /*
1357 - * Historically amend_file_with_trailers() passed its arguments
1358 - * to "git interpret-trailers", which expected argv entries in
1359 - * "--trailer=<trailer>" form. Continue to accept those for
1360 - * existing callers, but pass only the value portion to the
1361 - * in-process implementation.
1362 - */
1363 - skip_prefix(txt, "--trailer=", &txt);
1364 - if (!*txt) {
1365 - ret = error(_("empty --trailer argument"));
1366 - goto out;
1367 - }
1368 - strvec_push(&stripped_trailer_args, txt);
1369 - }
1370 -
1371 - if (validate_trailer_args(&stripped_trailer_args)) {
1351 + if (validate_trailer_args(trailer_args)) {
1352 ret = -1;
1353 goto out;
1354 }
1355 if (strbuf_read_file(&buf, path, 0) < 0)
1356 ret = error_errno(_("could not read '%s'"), path);
1357 else
1378 - amend_strbuf_with_trailers(&buf, &stripped_trailer_args);
1358 + amend_strbuf_with_trailers(&buf, trailer_args);
1359
1360 if (!ret)
1361 ret = write_file_in_place(path, &buf);
1362 out:
1383 - strvec_clear(&stripped_trailer_args);
1363 strbuf_release(&buf);
1364 return ret;
1365 }
trailer.h
+2 -2
@@ -209,8 +209,8 @@ int amend_strbuf_with_trailers(struct strbuf *buf,
209 /*
210 * Augment a file by appending trailers specified in trailer_args.
211 *
212 - * Each element of trailer_args should be an argv-style --trailer=<trailer>
213 - * option (i.e., including the --trailer= prefix).
212 + * Each element of trailer_args should be in the same format as the value
213 + * accepted by --trailer=<trailer> (i.e., without the --trailer= prefix).
214 *
215 * Returns 0 on success or a non-zero error code on failure.
216 */