worktree: teach "add" to check out existing branches
[git.git] / Documentation / git-interpret-trailers.txt
blob9dd19a1dd9f126fbf54f1dc628b30fbe05445cf3
1 git-interpret-trailers(1)
2 =========================
4 NAME
5 ----
6 git-interpret-trailers - add or parse structured information in commit messages
8 SYNOPSIS
9 --------
10 [verse]
11 'git interpret-trailers' [options] [(--trailer <token>[(=|:)<value>])...] [<file>...]
12 'git interpret-trailers' [options] [--parse] [<file>...]
14 DESCRIPTION
15 -----------
16 Help parsing or adding 'trailers' lines, that look similar to RFC 822 e-mail
17 headers, at the end of the otherwise free-form part of a commit
18 message.
20 This command reads some patches or commit messages from either the
21 <file> arguments or the standard input if no <file> is specified. If
22 `--parse` is specified, the output consists of the parsed trailers.
24 Otherwise, this command applies the arguments passed using the
25 `--trailer` option, if any, to the commit message part of each input
26 file. The result is emitted on the standard output.
28 Some configuration variables control the way the `--trailer` arguments
29 are applied to each commit message and the way any existing trailer in
30 the commit message is changed. They also make it possible to
31 automatically add some trailers.
33 By default, a '<token>=<value>' or '<token>:<value>' argument given
34 using `--trailer` will be appended after the existing trailers only if
35 the last trailer has a different (<token>, <value>) pair (or if there
36 is no existing trailer). The <token> and <value> parts will be trimmed
37 to remove starting and trailing whitespace, and the resulting trimmed
38 <token> and <value> will appear in the message like this:
40 ------------------------------------------------
41 token: value
42 ------------------------------------------------
44 This means that the trimmed <token> and <value> will be separated by
45 `': '` (one colon followed by one space).
47 By default the new trailer will appear at the end of all the existing
48 trailers. If there is no existing trailer, the new trailer will appear
49 after the commit message part of the output, and, if there is no line
50 with only spaces at the end of the commit message part, one blank line
51 will be added before the new trailer.
53 Existing trailers are extracted from the input message by looking for
54 a group of one or more lines that (i) are all trailers, or (ii) contains at
55 least one Git-generated or user-configured trailer and consists of at
56 least 25% trailers.
57 The group must be preceded by one or more empty (or whitespace-only) lines.
58 The group must either be at the end of the message or be the last
59 non-whitespace lines before a line that starts with '---'. Such three
60 minus signs start the patch part of the message.
62 When reading trailers, there can be whitespaces after the
63 token, the separator and the value. There can also be whitespaces
64 inside the token and the value. The value may be split over multiple lines with
65 each subsequent line starting with whitespace, like the "folding" in RFC 822.
67 Note that 'trailers' do not follow and are not intended to follow many
68 rules for RFC 822 headers. For example they do not follow
69 the encoding rules and probably many other rules.
71 OPTIONS
72 -------
73 --in-place::
74         Edit the files in place.
76 --trim-empty::
77         If the <value> part of any trailer contains only whitespace,
78         the whole trailer will be removed from the resulting message.
79         This applies to existing trailers as well as new trailers.
81 --trailer <token>[(=|:)<value>]::
82         Specify a (<token>, <value>) pair that should be applied as a
83         trailer to the input messages. See the description of this
84         command.
86 --where <placement>::
87 --no-where::
88         Specify where all new trailers will be added.  A setting
89         provided with '--where' overrides all configuration variables
90         and applies to all '--trailer' options until the next occurrence of
91         '--where' or '--no-where'.
93 --if-exists <action>::
94 --no-if-exists::
95         Specify what action will be performed when there is already at
96         least one trailer with the same <token> in the message.  A setting
97         provided with '--if-exists' overrides all configuration variables
98         and applies to all '--trailer' options until the next occurrence of
99         '--if-exists' or '--no-if-exists'.
101 --if-missing <action>::
102 --no-if-missing::
103         Specify what action will be performed when there is no other
104         trailer with the same <token> in the message.  A setting
105         provided with '--if-missing' overrides all configuration variables
106         and applies to all '--trailer' options until the next occurrence of
107         '--if-missing' or '--no-if-missing'.
109 --only-trailers::
110         Output only the trailers, not any other parts of the input.
112 --only-input::
113         Output only trailers that exist in the input; do not add any
114         from the command-line or by following configured `trailer.*`
115         rules.
117 --unfold::
118         Remove any whitespace-continuation in trailers, so that each
119         trailer appears on a line by itself with its full content.
121 --parse::
122         A convenience alias for `--only-trailers --only-input
123         --unfold`.
125 CONFIGURATION VARIABLES
126 -----------------------
128 trailer.separators::
129         This option tells which characters are recognized as trailer
130         separators. By default only ':' is recognized as a trailer
131         separator, except that '=' is always accepted on the command
132         line for compatibility with other git commands.
134 The first character given by this option will be the default character
135 used when another separator is not specified in the config for this
136 trailer.
138 For example, if the value for this option is "%=$", then only lines
139 using the format '<token><sep><value>' with <sep> containing '%', '='
140 or '$' and then spaces will be considered trailers. And '%' will be
141 the default separator used, so by default trailers will appear like:
142 '<token>% <value>' (one percent sign and one space will appear between
143 the token and the value).
145 trailer.where::
146         This option tells where a new trailer will be added.
148 This can be `end`, which is the default, `start`, `after` or `before`.
150 If it is `end`, then each new trailer will appear at the end of the
151 existing trailers.
153 If it is `start`, then each new trailer will appear at the start,
154 instead of the end, of the existing trailers.
156 If it is `after`, then each new trailer will appear just after the
157 last trailer with the same <token>.
159 If it is `before`, then each new trailer will appear just before the
160 first trailer with the same <token>.
162 trailer.ifexists::
163         This option makes it possible to choose what action will be
164         performed when there is already at least one trailer with the
165         same <token> in the message.
167 The valid values for this option are: `addIfDifferentNeighbor` (this
168 is the default), `addIfDifferent`, `add`, `replace` or `doNothing`.
170 With `addIfDifferentNeighbor`, a new trailer will be added only if no
171 trailer with the same (<token>, <value>) pair is above or below the line
172 where the new trailer will be added.
174 With `addIfDifferent`, a new trailer will be added only if no trailer
175 with the same (<token>, <value>) pair is already in the message.
177 With `add`, a new trailer will be added, even if some trailers with
178 the same (<token>, <value>) pair are already in the message.
180 With `replace`, an existing trailer with the same <token> will be
181 deleted and the new trailer will be added. The deleted trailer will be
182 the closest one (with the same <token>) to the place where the new one
183 will be added.
185 With `doNothing`, nothing will be done; that is no new trailer will be
186 added if there is already one with the same <token> in the message.
188 trailer.ifmissing::
189         This option makes it possible to choose what action will be
190         performed when there is not yet any trailer with the same
191         <token> in the message.
193 The valid values for this option are: `add` (this is the default) and
194 `doNothing`.
196 With `add`, a new trailer will be added.
198 With `doNothing`, nothing will be done.
200 trailer.<token>.key::
201         This `key` will be used instead of <token> in the trailer. At
202         the end of this key, a separator can appear and then some
203         space characters. By default the only valid separator is ':',
204         but this can be changed using the `trailer.separators` config
205         variable.
207 If there is a separator, then the key will be used instead of both the
208 <token> and the default separator when adding the trailer.
210 trailer.<token>.where::
211         This option takes the same values as the 'trailer.where'
212         configuration variable and it overrides what is specified by
213         that option for trailers with the specified <token>.
215 trailer.<token>.ifexists::
216         This option takes the same values as the 'trailer.ifexists'
217         configuration variable and it overrides what is specified by
218         that option for trailers with the specified <token>.
220 trailer.<token>.ifmissing::
221         This option takes the same values as the 'trailer.ifmissing'
222         configuration variable and it overrides what is specified by
223         that option for trailers with the specified <token>.
225 trailer.<token>.command::
226         This option can be used to specify a shell command that will
227         be called to automatically add or modify a trailer with the
228         specified <token>.
230 When this option is specified, the behavior is as if a special
231 '<token>=<value>' argument were added at the beginning of the command
232 line, where <value> is taken to be the standard output of the
233 specified command with any leading and trailing whitespace trimmed
234 off.
236 If the command contains the `$ARG` string, this string will be
237 replaced with the <value> part of an existing trailer with the same
238 <token>, if any, before the command is launched.
240 If some '<token>=<value>' arguments are also passed on the command
241 line, when a 'trailer.<token>.command' is configured, the command will
242 also be executed for each of these arguments. And the <value> part of
243 these arguments, if any, will be used to replace the `$ARG` string in
244 the command.
246 EXAMPLES
247 --------
249 * Configure a 'sign' trailer with a 'Signed-off-by' key, and then
250   add two of these trailers to a message:
252 ------------
253 $ git config trailer.sign.key "Signed-off-by"
254 $ cat msg.txt
255 subject
257 message
258 $ cat msg.txt | git interpret-trailers --trailer 'sign: Alice <alice@example.com>' --trailer 'sign: Bob <bob@example.com>'
259 subject
261 message
263 Signed-off-by: Alice <alice@example.com>
264 Signed-off-by: Bob <bob@example.com>
265 ------------
267 * Use the `--in-place` option to edit a message file in place:
269 ------------
270 $ cat msg.txt
271 subject
273 message
275 Signed-off-by: Bob <bob@example.com>
276 $ git interpret-trailers --trailer 'Acked-by: Alice <alice@example.com>' --in-place msg.txt
277 $ cat msg.txt
278 subject
280 message
282 Signed-off-by: Bob <bob@example.com>
283 Acked-by: Alice <alice@example.com>
284 ------------
286 * Extract the last commit as a patch, and add a 'Cc' and a
287   'Reviewed-by' trailer to it:
289 ------------
290 $ git format-patch -1
291 0001-foo.patch
292 $ git interpret-trailers --trailer 'Cc: Alice <alice@example.com>' --trailer 'Reviewed-by: Bob <bob@example.com>' 0001-foo.patch >0001-bar.patch
293 ------------
295 * Configure a 'sign' trailer with a command to automatically add a
296   'Signed-off-by: ' with the author information only if there is no
297   'Signed-off-by: ' already, and show how it works:
299 ------------
300 $ git config trailer.sign.key "Signed-off-by: "
301 $ git config trailer.sign.ifmissing add
302 $ git config trailer.sign.ifexists doNothing
303 $ git config trailer.sign.command 'echo "$(git config user.name) <$(git config user.email)>"'
304 $ git interpret-trailers <<EOF
305 > EOF
307 Signed-off-by: Bob <bob@example.com>
308 $ git interpret-trailers <<EOF
309 > Signed-off-by: Alice <alice@example.com>
310 > EOF
312 Signed-off-by: Alice <alice@example.com>
313 ------------
315 * Configure a 'fix' trailer with a key that contains a '#' and no
316   space after this character, and show how it works:
318 ------------
319 $ git config trailer.separators ":#"
320 $ git config trailer.fix.key "Fix #"
321 $ echo "subject" | git interpret-trailers --trailer fix=42
322 subject
324 Fix #42
325 ------------
327 * Configure a 'see' trailer with a command to show the subject of a
328   commit that is related, and show how it works:
330 ------------
331 $ git config trailer.see.key "See-also: "
332 $ git config trailer.see.ifExists "replace"
333 $ git config trailer.see.ifMissing "doNothing"
334 $ git config trailer.see.command "git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 \$ARG"
335 $ git interpret-trailers <<EOF
336 > subject
338 > message
340 > see: HEAD~2
341 > EOF
342 subject
344 message
346 See-also: fe3187489d69c4 (subject of related commit)
347 ------------
349 * Configure a commit template with some trailers with empty values
350   (using sed to show and keep the trailing spaces at the end of the
351   trailers), then configure a commit-msg hook that uses
352   'git interpret-trailers' to remove trailers with empty values and
353   to add a 'git-version' trailer:
355 ------------
356 $ sed -e 's/ Z$/ /' >commit_template.txt <<EOF
357 > ***subject***
359 > ***message***
361 > Fixes: Z
362 > Cc: Z
363 > Reviewed-by: Z
364 > Signed-off-by: Z
365 > EOF
366 $ git config commit.template commit_template.txt
367 $ cat >.git/hooks/commit-msg <<EOF
368 > #!/bin/sh
369 > git interpret-trailers --trim-empty --trailer "git-version: \$(git describe)" "\$1" > "\$1.new"
370 > mv "\$1.new" "\$1"
371 > EOF
372 $ chmod +x .git/hooks/commit-msg
373 ------------
375 SEE ALSO
376 --------
377 linkgit:git-commit[1], linkgit:git-format-patch[1], linkgit:git-config[1]
381 Part of the linkgit:git[1] suite