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 <adlternative@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
ZheNing Hu committed
Mar 23, 2021 at 13:55 UTC
2daae3d1d1bf513f1e1c00f1e4df75e1cb500e0f
3 files changed
+347
-1
Documentation/git-commit.txt
+13
-1
@@ -14,7 +14,8 @@ SYNOPSIS
14
[--allow-empty-message] [--no-verify] [-e] [--author=<author>]
15
[--date=<date>] [--cleanup=<mode>] [--[no-]status]
16
[-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
17
- [-S[<keyid>]] [--] [<pathspec>...]
17
+ [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]
18
+ [--] [<pathspec>...]
19
20
DESCRIPTION
21
-----------
@@ -166,6 +167,17 @@ The `-m` option is mutually exclusive with `-c`, `-C`, and `-F`.
167
168
include::signoff-option.txt[]
169
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
+ <committer@example.com>" --trailer "Helped-by:C O Mitter \
174
+ <committer@example.com>"` 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
+
181
-n::
182
--no-verify::
183
This option bypasses the pre-commit and commit-msg hooks.
builtin/commit.c
+22
@@ -113,6 +113,7 @@ static int config_commit_verbose = -1; /* unspecified */
113
static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
114
static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
115
static char *sign_commit, *pathspec_from_file;
116
+static struct strvec trailer_args = STRVEC_INIT;
117
118
/*
119
* The default commit message cleanup mode will remove the lines
@@ -131,6 +132,14 @@ static struct strbuf message = STRBUF_INIT;
132
133
static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
134
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
+
143
static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
144
{
145
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,
967
968
fclose(s->fp);
969
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
+
982
/*
983
* Reject an attempt to record a non-merge empty commit without
984
* 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)
1528
OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
1529
OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1530
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),
1532
OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
1533
OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1534
OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
t/t7502-commit-porcelain.sh
+312
@@ -38,6 +38,16 @@ check_summary_oneline() {
38
test_cmp exp act
39
}
40
41
+trailer_commit_base () {
42
+ echo "fun" >>file &&
43
+ git add file &&
44
+ git commit -s --trailer "Signed-off-by=C1 E1 " \
45
+ --trailer "Helped-by:C2 E2 " \
46
+ --trailer "Reported-by=C3 E3" \
47
+ --trailer "Mentored-by:C4 E4" \
48
+ -m "hello"
49
+}
50
+
51
test_expect_success 'output summary format' '
52
53
echo new >file1 &&
@@ -154,6 +164,308 @@ test_expect_success 'sign off' '
164
165
'
166
167
+test_expect_success 'commit --trailer with "="' '
168
+ trailer_commit_base &&
169
+ cat >expected <<-\EOF &&
170
+ hello
171
+
172
+ Signed-off-by: C O Mitter <committer@example.com>
173
+ Signed-off-by: C1 E1
174
+ Helped-by: C2 E2
175
+ Reported-by: C3 E3
176
+ Mentored-by: C4 E4
177
+ EOF
178
+ git cat-file commit HEAD >commit.msg &&
179
+ sed -e "1,/^\$/d" commit.msg >actual &&
180
+ test_cmp expected actual
181
+'
182
+
183
+test_expect_success 'commit --trailer with -c and "replace" as ifexists' '
184
+ trailer_commit_base &&
185
+ cat >expected <<-\EOF &&
186
+ hello
187
+
188
+ Signed-off-by: C O Mitter <committer@example.com>
189
+ Signed-off-by: C1 E1
190
+ Reported-by: C3 E3
191
+ Mentored-by: C4 E4
192
+ Helped-by: C3 E3
193
+ EOF
194
+ git -c trailer.ifexists="replace" \
195
+ commit --trailer "Mentored-by: C4 E4" \
196
+ --trailer "Helped-by: C3 E3" \
197
+ --amend &&
198
+ git cat-file commit HEAD >commit.msg &&
199
+ sed -e "1,/^\$/d" commit.msg >actual &&
200
+ test_cmp expected actual
201
+'
202
+
203
+test_expect_success 'commit --trailer with -c and "add" as ifexists' '
204
+ trailer_commit_base &&
205
+ cat >expected <<-\EOF &&
206
+ hello
207
+
208
+ Signed-off-by: C O Mitter <committer@example.com>
209
+ Signed-off-by: C1 E1
210
+ Helped-by: C2 E2
211
+ Reported-by: C3 E3
212
+ Mentored-by: C4 E4
213
+ Reported-by: C3 E3
214
+ Mentored-by: C4 E4
215
+ EOF
216
+ git -c trailer.ifexists="add" \
217
+ commit --trailer "Reported-by: C3 E3" \
218
+ --trailer "Mentored-by: C4 E4" \
219
+ --amend &&
220
+ git cat-file commit HEAD >commit.msg &&
221
+ sed -e "1,/^\$/d" commit.msg >actual &&
222
+ test_cmp expected actual
223
+'
224
+
225
+test_expect_success 'commit --trailer with -c and "donothing" as ifexists' '
226
+ trailer_commit_base &&
227
+ cat >expected <<-\EOF &&
228
+ hello
229
+
230
+ Signed-off-by: C O Mitter <committer@example.com>
231
+ Signed-off-by: C1 E1
232
+ Helped-by: C2 E2
233
+ Reported-by: C3 E3
234
+ Mentored-by: C4 E4
235
+ Reviewed-by: C6 E6
236
+ EOF
237
+ git -c trailer.ifexists="donothing" \
238
+ commit --trailer "Mentored-by: C5 E5" \
239
+ --trailer "Reviewed-by: C6 E6" \
240
+ --amend &&
241
+ git cat-file commit HEAD >commit.msg &&
242
+ sed -e "1,/^\$/d" commit.msg >actual &&
243
+ test_cmp expected actual
244
+'
245
+
246
+test_expect_success 'commit --trailer with -c and "addIfDifferent" as ifexists' '
247
+ trailer_commit_base &&
248
+ cat >expected <<-\EOF &&
249
+ hello
250
+
251
+ Signed-off-by: C O Mitter <committer@example.com>
252
+ Signed-off-by: C1 E1
253
+ Helped-by: C2 E2
254
+ Reported-by: C3 E3
255
+ Mentored-by: C4 E4
256
+ Mentored-by: C5 E5
257
+ EOF
258
+ git -c trailer.ifexists="addIfDifferent" \
259
+ commit --trailer "Reported-by: C3 E3" \
260
+ --trailer "Mentored-by: C5 E5" \
261
+ --amend &&
262
+ git cat-file commit HEAD >commit.msg &&
263
+ sed -e "1,/^\$/d" commit.msg >actual &&
264
+ test_cmp expected actual
265
+'
266
+
267
+test_expect_success 'commit --trailer with -c and "addIfDifferentNeighbor" as ifexists' '
268
+ trailer_commit_base &&
269
+ cat >expected <<-\EOF &&
270
+ hello
271
+
272
+ Signed-off-by: C O Mitter <committer@example.com>
273
+ Signed-off-by: C1 E1
274
+ Helped-by: C2 E2
275
+ Reported-by: C3 E3
276
+ Mentored-by: C4 E4
277
+ Reported-by: C3 E3
278
+ EOF
279
+ git -c trailer.ifexists="addIfDifferentNeighbor" \
280
+ commit --trailer "Mentored-by: C4 E4" \
281
+ --trailer "Reported-by: C3 E3" \
282
+ --amend &&
283
+ git cat-file commit HEAD >commit.msg &&
284
+ sed -e "1,/^\$/d" commit.msg >actual &&
285
+ test_cmp expected actual
286
+'
287
+
288
+test_expect_success 'commit --trailer with -c and "end" as where' '
289
+ trailer_commit_base &&
290
+ cat >expected <<-\EOF &&
291
+ hello
292
+
293
+ Signed-off-by: C O Mitter <committer@example.com>
294
+ Signed-off-by: C1 E1
295
+ Helped-by: C2 E2
296
+ Reported-by: C3 E3
297
+ Mentored-by: C4 E4
298
+ Reported-by: C3 E3
299
+ Mentored-by: C4 E4
300
+ EOF
301
+ git -c trailer.where="end" \
302
+ commit --trailer "Reported-by: C3 E3" \
303
+ --trailer "Mentored-by: C4 E4" \
304
+ --amend &&
305
+ git cat-file commit HEAD >commit.msg &&
306
+ sed -e "1,/^\$/d" commit.msg >actual &&
307
+ test_cmp expected actual
308
+'
309
+
310
+test_expect_success 'commit --trailer with -c and "start" as where' '
311
+ trailer_commit_base &&
312
+ cat >expected <<-\EOF &&
313
+ hello
314
+
315
+ Signed-off-by: C1 E1
316
+ Signed-off-by: C O Mitter <committer@example.com>
317
+ Signed-off-by: C1 E1
318
+ Helped-by: C2 E2
319
+ Reported-by: C3 E3
320
+ Mentored-by: C4 E4
321
+ EOF
322
+ git -c trailer.where="start" \
323
+ commit --trailer "Signed-off-by: C O Mitter <committer@example.com>" \
324
+ --trailer "Signed-off-by: C1 E1" \
325
+ --amend &&
326
+ git cat-file commit HEAD >commit.msg &&
327
+ sed -e "1,/^\$/d" commit.msg >actual &&
328
+ test_cmp expected actual
329
+'
330
+
331
+test_expect_success 'commit --trailer with -c and "after" as where' '
332
+ trailer_commit_base &&
333
+ cat >expected <<-\EOF &&
334
+ hello
335
+
336
+ Signed-off-by: C O Mitter <committer@example.com>
337
+ Signed-off-by: C1 E1
338
+ Helped-by: C2 E2
339
+ Reported-by: C3 E3
340
+ Mentored-by: C4 E4
341
+ Mentored-by: C5 E5
342
+ EOF
343
+ git -c trailer.where="after" \
344
+ commit --trailer "Mentored-by: C4 E4" \
345
+ --trailer "Mentored-by: C5 E5" \
346
+ --amend &&
347
+ git cat-file commit HEAD >commit.msg &&
348
+ sed -e "1,/^\$/d" commit.msg >actual &&
349
+ test_cmp expected actual
350
+'
351
+
352
+test_expect_success 'commit --trailer with -c and "before" as where' '
353
+ trailer_commit_base &&
354
+ cat >expected <<-\EOF &&
355
+ hello
356
+
357
+ Signed-off-by: C O Mitter <committer@example.com>
358
+ Signed-off-by: C1 E1
359
+ Helped-by: C2 E2
360
+ Reported-by: C3 E3
361
+ Mentored-by: C2 E2
362
+ Mentored-by: C3 E3
363
+ Mentored-by: C4 E4
364
+ EOF
365
+ git -c trailer.where="before" \
366
+ commit --trailer "Mentored-by: C3 E3" \
367
+ --trailer "Mentored-by: C2 E2" \
368
+ --amend &&
369
+ git cat-file commit HEAD >commit.msg &&
370
+ sed -e "1,/^\$/d" commit.msg >actual &&
371
+ test_cmp expected actual
372
+'
373
+
374
+test_expect_success 'commit --trailer with -c and "donothing" as ifmissing' '
375
+ trailer_commit_base &&
376
+ cat >expected <<-\EOF &&
377
+ hello
378
+
379
+ Signed-off-by: C O Mitter <committer@example.com>
380
+ Signed-off-by: C1 E1
381
+ Helped-by: C2 E2
382
+ Reported-by: C3 E3
383
+ Mentored-by: C4 E4
384
+ Helped-by: C5 E5
385
+ EOF
386
+ git -c trailer.ifmissing="donothing" \
387
+ commit --trailer "Helped-by: C5 E5" \
388
+ --trailer "Based-by: C6 E6" \
389
+ --amend &&
390
+ git cat-file commit HEAD >commit.msg &&
391
+ sed -e "1,/^\$/d" commit.msg >actual &&
392
+ test_cmp expected actual
393
+'
394
+
395
+test_expect_success 'commit --trailer with -c and "add" as ifmissing' '
396
+ trailer_commit_base &&
397
+ cat >expected <<-\EOF &&
398
+ hello
399
+
400
+ Signed-off-by: C O Mitter <committer@example.com>
401
+ Signed-off-by: C1 E1
402
+ Helped-by: C2 E2
403
+ Reported-by: C3 E3
404
+ Mentored-by: C4 E4
405
+ Helped-by: C5 E5
406
+ Based-by: C6 E6
407
+ EOF
408
+ git -c trailer.ifmissing="add" \
409
+ commit --trailer "Helped-by: C5 E5" \
410
+ --trailer "Based-by: C6 E6" \
411
+ --amend &&
412
+ git cat-file commit HEAD >commit.msg &&
413
+ sed -e "1,/^\$/d" commit.msg >actual &&
414
+ test_cmp expected actual
415
+'
416
+
417
+test_expect_success 'commit --trailer with -c ack.key ' '
418
+ echo "fun" >>file1 &&
419
+ git add file1 &&
420
+ cat >expected <<-\EOF &&
421
+ hello
422
+
423
+ Acked-by: Peff
424
+ EOF
425
+ git -c trailer.ack.key="Acked-by" \
426
+ commit --trailer "ack = Peff" -m "hello" &&
427
+ git cat-file commit HEAD >commit.msg &&
428
+ sed -e "1,/^\$/d" commit.msg >actual &&
429
+ test_cmp expected actual
430
+'
431
+
432
+test_expect_success 'commit --trailer with -c and ":=#" as separators' '
433
+ echo "fun" >>file1 &&
434
+ git add file1 &&
435
+ cat >expected <<-\EOF &&
436
+ I hate bug
437
+
438
+ Bug #42
439
+ EOF
440
+ git -c trailer.separators=":=#" \
441
+ -c trailer.bug.key="Bug #" \
442
+ commit --trailer "bug = 42" -m "I hate bug" &&
443
+ git cat-file commit HEAD >commit.msg &&
444
+ sed -e "1,/^\$/d" commit.msg >actual &&
445
+ test_cmp expected actual
446
+'
447
+
448
+test_expect_success 'commit --trailer with -c and command' '
449
+ trailer_commit_base &&
450
+ cat >expected <<-\EOF &&
451
+ hello
452
+
453
+ Signed-off-by: C O Mitter <committer@example.com>
454
+ Signed-off-by: C1 E1
455
+ Helped-by: C2 E2
456
+ Mentored-by: C4 E4
457
+ Reported-by: A U Thor <author@example.com>
458
+ EOF
459
+ git -c trailer.report.key="Reported-by: " \
460
+ -c trailer.report.ifexists="replace" \
461
+ -c trailer.report.command="NAME=\"\$ARG\"; test -n \"\$NAME\" && \
462
+ git log --author=\"\$NAME\" -1 --format=\"format:%aN <%aE>\" || true" \
463
+ commit --trailer "report = author" --amend &&
464
+ git cat-file commit HEAD >commit.msg &&
465
+ sed -e "1,/^\$/d" commit.msg >actual &&
466
+ test_cmp expected actual
467
+'
468
+
469
test_expect_success 'multiple -m' '
470
471
>negative &&