Skip to content

Commit 68e15e0

Browse files
committed
Merge branch 'zh/commit-trailer'
"git commit" learned "--trailer <key>[=<value>]" option; together with the interpret-trailers command, this will make it easier to support custom trailers. * zh/commit-trailer: commit: add --trailer option
2 parents a548f3e + 2daae3d commit 68e15e0

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
-----------
@@ -199,6 +200,17 @@ The `-m` option is mutually exclusive with `-c`, `-C`, and `-F`.
199200

200201
include::signoff-option.txt[]
201202

203+
--trailer <token>[(=|:)<value>]::
204+
Specify a (<token>, <value>) pair that should be applied as a
205+
trailer. (e.g. `git commit --trailer "Signed-off-by:C O Mitter \
206+
<[email protected]>" --trailer "Helped-by:C O Mitter \
207+
<[email protected]>"` will add the "Signed-off-by" trailer
208+
and the "Helped-by" trailer to the commit message.)
209+
The `trailer.*` configuration variables
210+
(linkgit:git-interpret-trailers[1]) can be used to define if
211+
a duplicated trailer is omitted, where in the run of trailers
212+
each trailer would appear, and other details.
213+
202214
-n::
203215
--no-verify::
204216
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
@@ -114,6 +114,7 @@ static int config_commit_verbose = -1; /* unspecified */
114114
static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
115115
static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
116116
static char *sign_commit, *pathspec_from_file;
117+
static struct strvec trailer_args = STRVEC_INIT;
117118

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

133134
static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
134135

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

9951004
fclose(s->fp);
9961005

1006+
if (trailer_args.nr) {
1007+
struct child_process run_trailer = CHILD_PROCESS_INIT;
1008+
1009+
strvec_pushl(&run_trailer.args, "interpret-trailers",
1010+
"--in-place", git_path_commit_editmsg(), NULL);
1011+
strvec_pushv(&run_trailer.args, trailer_args.v);
1012+
run_trailer.git_cmd = 1;
1013+
if (run_command(&run_trailer))
1014+
die(_("unable to pass trailers to --trailers"));
1015+
strvec_clear(&trailer_args);
1016+
}
1017+
9971018
/*
9981019
* Reject an attempt to record a non-merge empty commit without
9991020
* explicit --allow-empty. In the cherry-pick case, it may be
@@ -1596,6 +1617,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15961617
OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
15971618
OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
15981619
OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1620+
OPT_CALLBACK_F(0, "trailer", NULL, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer),
15991621
OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
16001622
OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
16011623
OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),

0 commit comments

Comments
 (0)