blob: 22ff3a603e03e3c9a1f0d76918fafa225989079d [file] [log] [blame]
Junio C Hamano4c43d652014-10-20 21:14:291git-interpret-trailers(1)
2=========================
3
4NAME
5----
Junio C Hamanob5513772019-04-22 03:38:396git-interpret-trailers - Add or parse structured information in commit messages
Junio C Hamano4c43d652014-10-20 21:14:297
8SYNOPSIS
9--------
10[verse]
Junio C Hamanocf9c7732022-10-28 18:56:0611'git interpret-trailers' [--in-place] [--trim-empty]
12[(--trailer <token>[(=|:)<value>])...]
13[--parse] [<file>...]
Junio C Hamano4c43d652014-10-20 21:14:2914
15DESCRIPTION
16-----------
Junio C Hamanofb1fdf12017-08-27 06:14:5917Help parsing or adding 'trailers' lines, that look similar to RFC 822 e-mail
Junio C Hamano4c43d652014-10-20 21:14:2918headers, at the end of the otherwise free-form part of a commit
19message.
20
21This command reads some patches or commit messages from either the
Junio C Hamanofb1fdf12017-08-27 06:14:5922<file> arguments or the standard input if no <file> is specified. If
23`--parse` is specified, the output consists of the parsed trailers.
24
25Otherwise, this command applies the arguments passed using the
26`--trailer` option, if any, to the commit message part of each input
27file. The result is emitted on the standard output.
Junio C Hamano4c43d652014-10-20 21:14:2928
29Some configuration variables control the way the `--trailer` arguments
30are applied to each commit message and the way any existing trailer in
31the commit message is changed. They also make it possible to
32automatically add some trailers.
33
34By default, a '<token>=<value>' or '<token>:<value>' argument given
35using `--trailer` will be appended after the existing trailers only if
36the last trailer has a different (<token>, <value>) pair (or if there
37is no existing trailer). The <token> and <value> parts will be trimmed
38to remove starting and trailing whitespace, and the resulting trimmed
39<token> and <value> will appear in the message like this:
40
41------------------------------------------------
42token: value
43------------------------------------------------
44
45This means that the trimmed <token> and <value> will be separated by
46`': '` (one colon followed by one space).
47
48By default the new trailer will appear at the end of all the existing
49trailers. If there is no existing trailer, the new trailer will appear
Junio C Hamanofb5ffde2014-11-04 22:38:5650after the commit message part of the output, and, if there is no line
Junio C Hamano4c43d652014-10-20 21:14:2951with only spaces at the end of the commit message part, one blank line
52will be added before the new trailer.
53
54Existing trailers are extracted from the input message by looking for
Junio C Hamano615c3b32018-02-28 23:40:2755a group of one or more lines that (i) is all trailers, or (ii) contains at
Junio C Hamanoca785492016-11-23 20:12:3056least one Git-generated or user-configured trailer and consists of at
57least 25% trailers.
Junio C Hamanof2f28b12016-10-31 21:41:5858The group must be preceded by one or more empty (or whitespace-only) lines.
Junio C Hamano4c43d652014-10-20 21:14:2959The group must either be at the end of the message or be the last
Junio C Hamano980e61e2018-09-17 22:45:5260non-whitespace lines before a line that starts with '---' (followed by a
61space or the end of the line). Such three minus signs start the patch
62part of the message. See also `--no-divider` below.
Junio C Hamano4c43d652014-10-20 21:14:2963
Junio C Hamano80d588f2022-09-09 22:08:2964When reading trailers, there can be no whitespace before or inside the
65token, but any number of regular space and tab characters are allowed
66between the token and the separator. There can be whitespaces before,
67inside or after the value. The value may be split over multiple lines
68with each subsequent line starting with at least one whitespace, like
69the "folding" in RFC 822.
Junio C Hamano4c43d652014-10-20 21:14:2970
71Note that 'trailers' do not follow and are not intended to follow many
Junio C Hamanof2f28b12016-10-31 21:41:5872rules for RFC 822 headers. For example they do not follow
73the encoding rules and probably many other rules.
Junio C Hamano4c43d652014-10-20 21:14:2974
75OPTIONS
76-------
Junio C Hamano7d098642016-01-30 00:05:2177--in-place::
78Edit the files in place.
79
Junio C Hamano4c43d652014-10-20 21:14:2980--trim-empty::
81If the <value> part of any trailer contains only whitespace,
82the whole trailer will be removed from the resulting message.
Junio C Hamanob0a1e832015-10-15 23:08:3183This applies to existing trailers as well as new trailers.
Junio C Hamano4c43d652014-10-20 21:14:2984
85--trailer <token>[(=|:)<value>]::
86Specify a (<token>, <value>) pair that should be applied as a
87trailer to the input messages. See the description of this
88command.
89
Junio C Hamanofb1fdf12017-08-27 06:14:5990--where <placement>::
91--no-where::
92Specify where all new trailers will be added. A setting
93provided with '--where' overrides all configuration variables
94and applies to all '--trailer' options until the next occurrence of
Junio C Hamanobfd91f42018-08-17 22:21:1695'--where' or '--no-where'. Possible values are `after`, `before`,
96`end` or `start`.
Junio C Hamanofb1fdf12017-08-27 06:14:5997
98--if-exists <action>::
99--no-if-exists::
100Specify what action will be performed when there is already at
101least one trailer with the same <token> in the message. A setting
102provided with '--if-exists' overrides all configuration variables
103and applies to all '--trailer' options until the next occurrence of
Junio C Hamanobfd91f42018-08-17 22:21:16104'--if-exists' or '--no-if-exists'. Possible actions are `addIfDifferent`,
105`addIfDifferentNeighbor`, `add`, `replace` and `doNothing`.
Junio C Hamanofb1fdf12017-08-27 06:14:59106
107--if-missing <action>::
108--no-if-missing::
109Specify what action will be performed when there is no other
110trailer with the same <token> in the message. A setting
111provided with '--if-missing' overrides all configuration variables
112and applies to all '--trailer' options until the next occurrence of
Junio C Hamanobfd91f42018-08-17 22:21:16113'--if-missing' or '--no-if-missing'. Possible actions are `doNothing`
114or `add`.
Junio C Hamanofb1fdf12017-08-27 06:14:59115
116--only-trailers::
117Output only the trailers, not any other parts of the input.
118
119--only-input::
120Output only trailers that exist in the input; do not add any
121from the command-line or by following configured `trailer.*`
122rules.
123
124--unfold::
125Remove any whitespace-continuation in trailers, so that each
126trailer appears on a line by itself with its full content.
127
128--parse::
129A convenience alias for `--only-trailers --only-input
130--unfold`.
131
Junio C Hamano980e61e2018-09-17 22:45:52132--no-divider::
133Do not treat `---` as the end of the commit message. Use this
134when you know your input contains just the commit message itself
135(and not an email or the output of `git format-patch`).
136
Junio C Hamano4c43d652014-10-20 21:14:29137CONFIGURATION VARIABLES
138-----------------------
139
140trailer.separators::
141This option tells which characters are recognized as trailer
142separators. By default only ':' is recognized as a trailer
143separator, except that '=' is always accepted on the command
144line for compatibility with other git commands.
145+
146The first character given by this option will be the default character
147used when another separator is not specified in the config for this
148trailer.
149+
150For example, if the value for this option is "%=$", then only lines
151using the format '<token><sep><value>' with <sep> containing '%', '='
152or '$' and then spaces will be considered trailers. And '%' will be
153the default separator used, so by default trailers will appear like:
154'<token>% <value>' (one percent sign and one space will appear between
155the token and the value).
156
157trailer.where::
158This option tells where a new trailer will be added.
159+
160This can be `end`, which is the default, `start`, `after` or `before`.
161+
162If it is `end`, then each new trailer will appear at the end of the
163existing trailers.
164+
165If it is `start`, then each new trailer will appear at the start,
166instead of the end, of the existing trailers.
167+
168If it is `after`, then each new trailer will appear just after the
169last trailer with the same <token>.
170+
171If it is `before`, then each new trailer will appear just before the
172first trailer with the same <token>.
173
174trailer.ifexists::
175This option makes it possible to choose what action will be
176performed when there is already at least one trailer with the
177same <token> in the message.
178+
179The valid values for this option are: `addIfDifferentNeighbor` (this
Junio C Hamano222ed822017-06-02 06:24:06180is the default), `addIfDifferent`, `add`, `replace` or `doNothing`.
Junio C Hamano4c43d652014-10-20 21:14:29181+
182With `addIfDifferentNeighbor`, a new trailer will be added only if no
183trailer with the same (<token>, <value>) pair is above or below the line
184where the new trailer will be added.
185+
186With `addIfDifferent`, a new trailer will be added only if no trailer
187with the same (<token>, <value>) pair is already in the message.
188+
189With `add`, a new trailer will be added, even if some trailers with
190the same (<token>, <value>) pair are already in the message.
191+
192With `replace`, an existing trailer with the same <token> will be
193deleted and the new trailer will be added. The deleted trailer will be
194the closest one (with the same <token>) to the place where the new one
195will be added.
196+
197With `doNothing`, nothing will be done; that is no new trailer will be
198added if there is already one with the same <token> in the message.
199
200trailer.ifmissing::
201This option makes it possible to choose what action will be
202performed when there is not yet any trailer with the same
203<token> in the message.
204+
205The valid values for this option are: `add` (this is the default) and
206`doNothing`.
207+
208With `add`, a new trailer will be added.
209+
210With `doNothing`, nothing will be done.
211
212trailer.<token>.key::
213This `key` will be used instead of <token> in the trailer. At
214the end of this key, a separator can appear and then some
215space characters. By default the only valid separator is ':',
216but this can be changed using the `trailer.separators` config
217variable.
218+
219If there is a separator, then the key will be used instead of both the
220<token> and the default separator when adding the trailer.
221
222trailer.<token>.where::
223This option takes the same values as the 'trailer.where'
224configuration variable and it overrides what is specified by
225that option for trailers with the specified <token>.
226
Junio C Hamanofb1fdf12017-08-27 06:14:59227trailer.<token>.ifexists::
228This option takes the same values as the 'trailer.ifexists'
Junio C Hamano4c43d652014-10-20 21:14:29229configuration variable and it overrides what is specified by
230that option for trailers with the specified <token>.
231
232trailer.<token>.ifmissing::
233This option takes the same values as the 'trailer.ifmissing'
234configuration variable and it overrides what is specified by
235that option for trailers with the specified <token>.
236
237trailer.<token>.command::
Junio C Hamano51a55bc2021-05-12 05:22:19238This option behaves in the same way as 'trailer.<token>.cmd', except
239that it doesn't pass anything as argument to the specified command.
240Instead the first occurrence of substring $ARG is replaced by the
241value that would be passed as argument.
Junio C Hamano4c43d652014-10-20 21:14:29242+
Junio C Hamano51a55bc2021-05-12 05:22:19243The 'trailer.<token>.command' option has been deprecated in favor of
244'trailer.<token>.cmd' due to the fact that $ARG in the user's command is
245only replaced once and that the original way of replacing $ARG is not safe.
Junio C Hamano4c43d652014-10-20 21:14:29246+
Junio C Hamano51a55bc2021-05-12 05:22:19247When both 'trailer.<token>.cmd' and 'trailer.<token>.command' are given
248for the same <token>, 'trailer.<token>.cmd' is used and
249'trailer.<token>.command' is ignored.
250
251trailer.<token>.cmd::
252This option can be used to specify a shell command that will be called:
253once to automatically add a trailer with the specified <token>, and then
254each time a '--trailer <token>=<value>' argument to modify the <value> of
255the trailer that this option would produce.
Junio C Hamano4c43d652014-10-20 21:14:29256+
Junio C Hamano51a55bc2021-05-12 05:22:19257When the specified command is first called to add a trailer
258with the specified <token>, the behavior is as if a special
259'--trailer <token>=<value>' argument was added at the beginning
260of the "git interpret-trailers" command, where <value>
261is taken to be the standard output of the command with any
262leading and trailing whitespace trimmed off.
263+
264If some '--trailer <token>=<value>' arguments are also passed
265on the command line, the command is called again once for each
266of these arguments with the same <token>. And the <value> part
267of these arguments, if any, will be passed to the command as its
268first argument. This way the command can produce a <value> computed
269from the <value> passed in the '--trailer <token>=<value>' argument.
Junio C Hamano4c43d652014-10-20 21:14:29270
271EXAMPLES
272--------
273
274* Configure a 'sign' trailer with a 'Signed-off-by' key, and then
275 add two of these trailers to a message:
276+
277------------
278$ git config trailer.sign.key "Signed-off-by"
279$ cat msg.txt
280subject
281
282message
283$ cat msg.txt | git interpret-trailers --trailer 'sign: Alice <alice@example.com>' --trailer 'sign: Bob <bob@example.com>'
284subject
285
286message
287
288Signed-off-by: Alice <alice@example.com>
289Signed-off-by: Bob <bob@example.com>
290------------
291
Junio C Hamano92d80372016-07-13 22:00:05292* Use the `--in-place` option to edit a message file in place:
Junio C Hamano7d098642016-01-30 00:05:21293+
294------------
295$ cat msg.txt
296subject
297
298message
299
300Signed-off-by: Bob <bob@example.com>
301$ git interpret-trailers --trailer 'Acked-by: Alice <alice@example.com>' --in-place msg.txt
302$ cat msg.txt
303subject
304
305message
306
307Signed-off-by: Bob <bob@example.com>
308Acked-by: Alice <alice@example.com>
309------------
310
Junio C Hamano4c43d652014-10-20 21:14:29311* Extract the last commit as a patch, and add a 'Cc' and a
312 'Reviewed-by' trailer to it:
313+
314------------
315$ git format-patch -1
3160001-foo.patch
317$ git interpret-trailers --trailer 'Cc: Alice <alice@example.com>' --trailer 'Reviewed-by: Bob <bob@example.com>' 0001-foo.patch >0001-bar.patch
318------------
319
320* Configure a 'sign' trailer with a command to automatically add a
321 'Signed-off-by: ' with the author information only if there is no
322 'Signed-off-by: ' already, and show how it works:
323+
324------------
325$ git config trailer.sign.key "Signed-off-by: "
326$ git config trailer.sign.ifmissing add
327$ git config trailer.sign.ifexists doNothing
328$ git config trailer.sign.command 'echo "$(git config user.name) <$(git config user.email)>"'
329$ git interpret-trailers <<EOF
330> EOF
331
332Signed-off-by: Bob <bob@example.com>
333$ git interpret-trailers <<EOF
334> Signed-off-by: Alice <alice@example.com>
335> EOF
336
337Signed-off-by: Alice <alice@example.com>
338------------
339
340* Configure a 'fix' trailer with a key that contains a '#' and no
341 space after this character, and show how it works:
342+
343------------
344$ git config trailer.separators ":#"
345$ git config trailer.fix.key "Fix #"
346$ echo "subject" | git interpret-trailers --trailer fix=42
347subject
348
349Fix #42
350------------
351
Junio C Hamano51a55bc2021-05-12 05:22:19352* Configure a 'help' trailer with a cmd use a script `glog-find-author`
353 which search specified author identity from git log in git repository
354 and show how it works:
355+
356------------
357$ cat ~/bin/glog-find-author
358#!/bin/sh
359test -n "$1" && git log --author="$1" --pretty="%an <%ae>" -1 || true
360$ git config trailer.help.key "Helped-by: "
361$ git config trailer.help.ifExists "addIfDifferentNeighbor"
362$ git config trailer.help.cmd "~/bin/glog-find-author"
363$ git interpret-trailers --trailer="help:Junio" --trailer="help:Couder" <<EOF
364> subject
365>
366> message
367>
368> EOF
369subject
370
371message
372
373Helped-by: Junio C Hamano <gitster@pobox.com>
374Helped-by: Christian Couder <christian.couder@gmail.com>
375------------
376
377* Configure a 'ref' trailer with a cmd use a script `glog-grep`
378 to grep last relevant commit from git log in the git repository
379 and show how it works:
380+
381------------
382$ cat ~/bin/glog-grep
383#!/bin/sh
384test -n "$1" && git log --grep "$1" --pretty=reference -1 || true
385$ git config trailer.ref.key "Reference-to: "
386$ git config trailer.ref.ifExists "replace"
387$ git config trailer.ref.cmd "~/bin/glog-grep"
388$ git interpret-trailers --trailer="ref:Add copyright notices." <<EOF
389> subject
390>
391> message
392>
393> EOF
394subject
395
396message
397
398Reference-to: 8bc9a0c769 (Add copyright notices., 2005-04-07)
399------------
400
Junio C Hamano4c43d652014-10-20 21:14:29401* Configure a 'see' trailer with a command to show the subject of a
402 commit that is related, and show how it works:
403+
404------------
405$ git config trailer.see.key "See-also: "
406$ git config trailer.see.ifExists "replace"
407$ git config trailer.see.ifMissing "doNothing"
408$ git config trailer.see.command "git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 \$ARG"
409$ git interpret-trailers <<EOF
410> subject
411>
412> message
413>
414> see: HEAD~2
415> EOF
416subject
417
418message
419
420See-also: fe3187489d69c4 (subject of related commit)
421------------
422
423* Configure a commit template with some trailers with empty values
424 (using sed to show and keep the trailing spaces at the end of the
425 trailers), then configure a commit-msg hook that uses
426 'git interpret-trailers' to remove trailers with empty values and
427 to add a 'git-version' trailer:
428+
429------------
430$ sed -e 's/ Z$/ /' >commit_template.txt <<EOF
431> ***subject***
432>
433> ***message***
434>
435> Fixes: Z
436> Cc: Z
437> Reviewed-by: Z
438> Signed-off-by: Z
439> EOF
440$ git config commit.template commit_template.txt
441$ cat >.git/hooks/commit-msg <<EOF
442> #!/bin/sh
443> git interpret-trailers --trim-empty --trailer "git-version: \$(git describe)" "\$1" > "\$1.new"
444> mv "\$1.new" "\$1"
445> EOF
446$ chmod +x .git/hooks/commit-msg
447------------
448
449SEE ALSO
450--------
451linkgit:git-commit[1], linkgit:git-format-patch[1], linkgit:git-config[1]
452
453GIT
454---
455Part of the linkgit:git[1] suite