trailer: allow non-trailers in trailer block
[git.git] / Documentation / git-interpret-trailers.txt
blobcf4c5ea45f03c59ea20f80531c3f97e5311aa30e
1 git-interpret-trailers(1)
2 =========================
4 NAME
5 ----
6 git-interpret-trailers - help add structured information into commit messages
8 SYNOPSIS
9 --------
10 [verse]
11 'git interpret-trailers' [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]
13 DESCRIPTION
14 -----------
15 Help adding 'trailers' lines, that look similar to RFC 822 e-mail
16 headers, at the end of the otherwise free-form part of a commit
17 message.
19 This command reads some patches or commit messages from either the
20 <file> arguments or the standard input if no <file> is specified. Then
21 this command applies the arguments passed using the `--trailer`
22 option, if any, to the commit message part of each input file. The
23 result is emitted on the standard output.
25 Some configuration variables control the way the `--trailer` arguments
26 are applied to each commit message and the way any existing trailer in
27 the commit message is changed. They also make it possible to
28 automatically add some trailers.
30 By default, a '<token>=<value>' or '<token>:<value>' argument given
31 using `--trailer` will be appended after the existing trailers only if
32 the last trailer has a different (<token>, <value>) pair (or if there
33 is no existing trailer). The <token> and <value> parts will be trimmed
34 to remove starting and trailing whitespace, and the resulting trimmed
35 <token> and <value> will appear in the message like this:
37 ------------------------------------------------
38 token: value
39 ------------------------------------------------
41 This means that the trimmed <token> and <value> will be separated by
42 `': '` (one colon followed by one space).
44 By default the new trailer will appear at the end of all the existing
45 trailers. If there is no existing trailer, the new trailer will appear
46 after the commit message part of the output, and, if there is no line
47 with only spaces at the end of the commit message part, one blank line
48 will be added before the new trailer.
50 Existing trailers are extracted from the input message by looking for
51 a group of one or more lines that (i) are all trailers, or (ii) contains at
52 least one Git-generated trailer and consists of at least 25% trailers.
53 The group must be preceded by one or more empty (or whitespace-only) lines.
54 The group must either be at the end of the message or be the last
55 non-whitespace lines before a line that starts with '---'. Such three
56 minus signs start the patch part of the message.
58 When reading trailers, there can be whitespaces before and after the
59 token, the separator and the value. There can also be whitespaces
60 inside the token and the value.
62 Note that 'trailers' do not follow and are not intended to follow many
63 rules for RFC 822 headers. For example they do not follow the line
64 folding rules, the encoding rules and probably many other rules.
66 OPTIONS
67 -------
68 --in-place::
69         Edit the files in place.
71 --trim-empty::
72         If the <value> part of any trailer contains only whitespace,
73         the whole trailer will be removed from the resulting message.
74         This applies to existing trailers as well as new trailers.
76 --trailer <token>[(=|:)<value>]::
77         Specify a (<token>, <value>) pair that should be applied as a
78         trailer to the input messages. See the description of this
79         command.
81 CONFIGURATION VARIABLES
82 -----------------------
84 trailer.separators::
85         This option tells which characters are recognized as trailer
86         separators. By default only ':' is recognized as a trailer
87         separator, except that '=' is always accepted on the command
88         line for compatibility with other git commands.
90 The first character given by this option will be the default character
91 used when another separator is not specified in the config for this
92 trailer.
94 For example, if the value for this option is "%=$", then only lines
95 using the format '<token><sep><value>' with <sep> containing '%', '='
96 or '$' and then spaces will be considered trailers. And '%' will be
97 the default separator used, so by default trailers will appear like:
98 '<token>% <value>' (one percent sign and one space will appear between
99 the token and the value).
101 trailer.where::
102         This option tells where a new trailer will be added.
104 This can be `end`, which is the default, `start`, `after` or `before`.
106 If it is `end`, then each new trailer will appear at the end of the
107 existing trailers.
109 If it is `start`, then each new trailer will appear at the start,
110 instead of the end, of the existing trailers.
112 If it is `after`, then each new trailer will appear just after the
113 last trailer with the same <token>.
115 If it is `before`, then each new trailer will appear just before the
116 first trailer with the same <token>.
118 trailer.ifexists::
119         This option makes it possible to choose what action will be
120         performed when there is already at least one trailer with the
121         same <token> in the message.
123 The valid values for this option are: `addIfDifferentNeighbor` (this
124 is the default), `addIfDifferent`, `add`, `overwrite` or `doNothing`.
126 With `addIfDifferentNeighbor`, a new trailer will be added only if no
127 trailer with the same (<token>, <value>) pair is above or below the line
128 where the new trailer will be added.
130 With `addIfDifferent`, a new trailer will be added only if no trailer
131 with the same (<token>, <value>) pair is already in the message.
133 With `add`, a new trailer will be added, even if some trailers with
134 the same (<token>, <value>) pair are already in the message.
136 With `replace`, an existing trailer with the same <token> will be
137 deleted and the new trailer will be added. The deleted trailer will be
138 the closest one (with the same <token>) to the place where the new one
139 will be added.
141 With `doNothing`, nothing will be done; that is no new trailer will be
142 added if there is already one with the same <token> in the message.
144 trailer.ifmissing::
145         This option makes it possible to choose what action will be
146         performed when there is not yet any trailer with the same
147         <token> in the message.
149 The valid values for this option are: `add` (this is the default) and
150 `doNothing`.
152 With `add`, a new trailer will be added.
154 With `doNothing`, nothing will be done.
156 trailer.<token>.key::
157         This `key` will be used instead of <token> in the trailer. At
158         the end of this key, a separator can appear and then some
159         space characters. By default the only valid separator is ':',
160         but this can be changed using the `trailer.separators` config
161         variable.
163 If there is a separator, then the key will be used instead of both the
164 <token> and the default separator when adding the trailer.
166 trailer.<token>.where::
167         This option takes the same values as the 'trailer.where'
168         configuration variable and it overrides what is specified by
169         that option for trailers with the specified <token>.
171 trailer.<token>.ifexist::
172         This option takes the same values as the 'trailer.ifexist'
173         configuration variable and it overrides what is specified by
174         that option for trailers with the specified <token>.
176 trailer.<token>.ifmissing::
177         This option takes the same values as the 'trailer.ifmissing'
178         configuration variable and it overrides what is specified by
179         that option for trailers with the specified <token>.
181 trailer.<token>.command::
182         This option can be used to specify a shell command that will
183         be called to automatically add or modify a trailer with the
184         specified <token>.
186 When this option is specified, the behavior is as if a special
187 '<token>=<value>' argument were added at the beginning of the command
188 line, where <value> is taken to be the standard output of the
189 specified command with any leading and trailing whitespace trimmed
190 off.
192 If the command contains the `$ARG` string, this string will be
193 replaced with the <value> part of an existing trailer with the same
194 <token>, if any, before the command is launched.
196 If some '<token>=<value>' arguments are also passed on the command
197 line, when a 'trailer.<token>.command' is configured, the command will
198 also be executed for each of these arguments. And the <value> part of
199 these arguments, if any, will be used to replace the `$ARG` string in
200 the command.
202 EXAMPLES
203 --------
205 * Configure a 'sign' trailer with a 'Signed-off-by' key, and then
206   add two of these trailers to a message:
208 ------------
209 $ git config trailer.sign.key "Signed-off-by"
210 $ cat msg.txt
211 subject
213 message
214 $ cat msg.txt | git interpret-trailers --trailer 'sign: Alice <alice@example.com>' --trailer 'sign: Bob <bob@example.com>'
215 subject
217 message
219 Signed-off-by: Alice <alice@example.com>
220 Signed-off-by: Bob <bob@example.com>
221 ------------
223 * Use the `--in-place` option to edit a message file in place:
225 ------------
226 $ cat msg.txt
227 subject
229 message
231 Signed-off-by: Bob <bob@example.com>
232 $ git interpret-trailers --trailer 'Acked-by: Alice <alice@example.com>' --in-place msg.txt
233 $ cat msg.txt
234 subject
236 message
238 Signed-off-by: Bob <bob@example.com>
239 Acked-by: Alice <alice@example.com>
240 ------------
242 * Extract the last commit as a patch, and add a 'Cc' and a
243   'Reviewed-by' trailer to it:
245 ------------
246 $ git format-patch -1
247 0001-foo.patch
248 $ git interpret-trailers --trailer 'Cc: Alice <alice@example.com>' --trailer 'Reviewed-by: Bob <bob@example.com>' 0001-foo.patch >0001-bar.patch
249 ------------
251 * Configure a 'sign' trailer with a command to automatically add a
252   'Signed-off-by: ' with the author information only if there is no
253   'Signed-off-by: ' already, and show how it works:
255 ------------
256 $ git config trailer.sign.key "Signed-off-by: "
257 $ git config trailer.sign.ifmissing add
258 $ git config trailer.sign.ifexists doNothing
259 $ git config trailer.sign.command 'echo "$(git config user.name) <$(git config user.email)>"'
260 $ git interpret-trailers <<EOF
261 > EOF
263 Signed-off-by: Bob <bob@example.com>
264 $ git interpret-trailers <<EOF
265 > Signed-off-by: Alice <alice@example.com>
266 > EOF
268 Signed-off-by: Alice <alice@example.com>
269 ------------
271 * Configure a 'fix' trailer with a key that contains a '#' and no
272   space after this character, and show how it works:
274 ------------
275 $ git config trailer.separators ":#"
276 $ git config trailer.fix.key "Fix #"
277 $ echo "subject" | git interpret-trailers --trailer fix=42
278 subject
280 Fix #42
281 ------------
283 * Configure a 'see' trailer with a command to show the subject of a
284   commit that is related, and show how it works:
286 ------------
287 $ git config trailer.see.key "See-also: "
288 $ git config trailer.see.ifExists "replace"
289 $ git config trailer.see.ifMissing "doNothing"
290 $ git config trailer.see.command "git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 \$ARG"
291 $ git interpret-trailers <<EOF
292 > subject
294 > message
296 > see: HEAD~2
297 > EOF
298 subject
300 message
302 See-also: fe3187489d69c4 (subject of related commit)
303 ------------
305 * Configure a commit template with some trailers with empty values
306   (using sed to show and keep the trailing spaces at the end of the
307   trailers), then configure a commit-msg hook that uses
308   'git interpret-trailers' to remove trailers with empty values and
309   to add a 'git-version' trailer:
311 ------------
312 $ sed -e 's/ Z$/ /' >commit_template.txt <<EOF
313 > ***subject***
315 > ***message***
317 > Fixes: Z
318 > Cc: Z
319 > Reviewed-by: Z
320 > Signed-off-by: Z
321 > EOF
322 $ git config commit.template commit_template.txt
323 $ cat >.git/hooks/commit-msg <<EOF
324 > #!/bin/sh
325 > git interpret-trailers --trim-empty --trailer "git-version: \$(git describe)" "\$1" > "\$1.new"
326 > mv "\$1.new" "\$1"
327 > EOF
328 $ chmod +x .git/hooks/commit-msg
329 ------------
331 SEE ALSO
332 --------
333 linkgit:git-commit[1], linkgit:git-format-patch[1], linkgit:git-config[1]
337 Part of the linkgit:git[1] suite