Skip to content

Commit 2daae3d

Browse files
adlternativegitster
authored andcommitted
commit: add --trailer option
Historically, Git has supported the 'Signed-off-by' commit trailer using the '--signoff' and the '-s' option from the command line. But users may need to provide other trailer information from the command line such as "Helped-by", "Reported-by", "Mentored-by", Now implement a new `--trailer <token>[(=|:)<value>]` option to pass other trailers to `interpret-trailers` and insert them into commit messages. Signed-off-by: ZheNing Hu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1424303 commit 2daae3d

File tree

3 files changed

+347
-1
lines changed

3 files changed

+347
-1
lines changed

Documentation/git-commit.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ SYNOPSIS
1414
[--allow-empty-message] [--no-verify] [-e] [--author=<author>]
1515
[--date=<date>] [--cleanup=<mode>] [--[no-]status]
1616
[-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
17-
[-S[<keyid>]] [--] [<pathspec>...]
17+
[(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]
18+
[--] [<pathspec>...]
1819

1920
DESCRIPTION
2021
-----------
@@ -166,6 +167,17 @@ The `-m` option is mutually exclusive with `-c`, `-C`, and `-F`.
166167

167168
include::signoff-option.txt[]
168169

170+
--trailer <token>[(=|:)<value>]::
171+
Specify a (<token>, <value>) pair that should be applied as a
172+
trailer. (e.g. `git commit --trailer "Signed-off-by:C O Mitter \
173+
<[email protected]>" --trailer "Helped-by:C O Mitter \
174+
<[email protected]>"` will add the "Signed-off-by" trailer
175+
and the "Helped-by" trailer to the commit message.)
176+
The `trailer.*` configuration variables
177+
(linkgit:git-interpret-trailers[1]) can be used to define if
178+
a duplicated trailer is omitted, where in the run of trailers
179+
each trailer would appear, and other details.
180+
169181
-n::
170182
--no-verify::
171183
This option bypasses the pre-commit and commit-msg hooks.

builtin/commit.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static int config_commit_verbose = -1; /* unspecified */
113113
static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
114114
static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
115115
static char *sign_commit, *pathspec_from_file;
116+
static struct strvec trailer_args = STRVEC_INIT;
116117

117118
/*
118119
* The default commit message cleanup mode will remove the lines
@@ -131,6 +132,14 @@ static struct strbuf message = STRBUF_INIT;
131132

132133
static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
133134

135+
static int opt_pass_trailer(const struct option *opt, const char *arg, int unset)
136+
{
137+
BUG_ON_OPT_NEG(unset);
138+
139+
strvec_pushl(&trailer_args, "--trailer", arg, NULL);
140+
return 0;
141+
}
142+
134143
static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
135144
{
136145
enum wt_status_format *value = (enum wt_status_format *)opt->value;
@@ -958,6 +967,18 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
958967

959968
fclose(s->fp);
960969

970+
if (trailer_args.nr) {
971+
struct child_process run_trailer = CHILD_PROCESS_INIT;
972+
973+
strvec_pushl(&run_trailer.args, "interpret-trailers",
974+
"--in-place", git_path_commit_editmsg(), NULL);
975+
strvec_pushv(&run_trailer.args, trailer_args.v);
976+
run_trailer.git_cmd = 1;
977+
if (run_command(&run_trailer))
978+
die(_("unable to pass trailers to --trailers"));
979+
strvec_clear(&trailer_args);
980+
}
981+
961982
/*
962983
* Reject an attempt to record a non-merge empty commit without
963984
* explicit --allow-empty. In the cherry-pick case, it may be
@@ -1507,6 +1528,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15071528
OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
15081529
OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
15091530
OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1531+
OPT_CALLBACK_F(0, "trailer", NULL, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer),
15101532
OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
15111533
OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
15121534
OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),

0 commit comments

Comments
 (0)