interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / git-interpret-trailers.adoc

456 lines · 15090 bytes Raw Blame History
1git-interpret-trailers(1)
2=========================
3
4NAME
5----
6git-interpret-trailers - Add or parse metadata in commit messages
7
8SYNOPSIS
9--------
10[synopsis]
11git interpret-trailers [--in-place] [--trim-empty]
12 [(--trailer (<key>|<key-alias>)[(=|:)<value>])...]
13 [--parse] [<file>...]
14
15DESCRIPTION
16-----------
17Add or parse trailer metadata at the end of the otherwise
18free-form part of a commit message, or any other kind of text.
19
20A _trailer_ in its simplest form is a key-value pair with a colon as a
21separator. The _key_ consists of ASCII alphanumeric characters and
22hyphens (`-`). A _trailer block_ consists of one or more trailers. The
23trailer block needs to be preceded by a blank line, where a _blank line_
24is either an empty or a whitespace-only line. For example, in the
25following commit message
26
27------------------------------------------------
28subject
29
30Lorem ipsum dolor sit amet, consectetur adipiscing elit.
31
32Signed-off-by: Alice <alice@example.com>
33Signed-off-by: Bob <bob@example.com>
34------------------------------------------------
35
36the last two lines starting with `Signed-off-by` are trailers.
37
38This command reads commit messages from either the
39_<file>_ arguments or the standard input if no _<file>_ is specified.
40If `--parse` is specified, the output consists of the parsed trailers
41coming from the input, without influencing them with any command line
42options or configuration variables.
43
44Otherwise, this command applies `trailer.<key-alias>` configuration
45variables (which could potentially add new trailers, as well as
46reposition them), as well as any command line arguments that can
47override configuration variables (such as `--trailer=...` which could
48also add new trailers), to each input file. The result is emitted on the
49standard output.
50
51This command can also operate on the output of linkgit:git-format-patch[1],
52which is more elaborate than a plain commit message. Namely, such output
53includes a commit message (as above), a `---` divider line, and a patch part.
54For these inputs, the divider and patch parts are not modified by
55this command and are emitted as is on the output, unless
56`--no-divider` is specified.
57
58Some configuration variables control the way the `--trailer` arguments
59are applied to each input and the way any existing trailer in
60the input is changed. They also make it possible to
61automatically add some trailers.
62
63Let's consider new trailers added with `--trailer`.
64By default, the new trailer will appear at the end of the trailer block.
65Also by default, this new trailer will only be added
66if the last trailer is different to it.
67A trailer block will be created with only that trailer if a trailer
68block does not already exist. Recall that a trailer block needs to be
69preceded by a blank line, so a blank line will be inserted before the
70new trailer block in that case.
71
72This is how the new trailer is added: a `<key>=<value>` or
73`<key>:<value>` argument given using `--trailer` will be appended after
74the existing trailers. The _<key>_ and _<value>_ parts will be trimmed
75to remove starting and trailing whitespace, and the resulting trimmed
76_<key>_ and _<value>_ will appear in the output like this:
77
78------------------------------------------------
79key: value
80------------------------------------------------
81
82This means that the trimmed _<key>_ and _<value>_ will be separated by
83"`:`{nbsp}" (one colon followed by one space).
84
85Existing trailers are extracted from the input by looking for the
86trailer block. A trailer block is a group of one or more lines that (i)
87is all trailers, or (ii) contains at least one Git-generated or
88user-configured trailer and consists of at
89least 25% trailers.
90The trailer block is by definition at the end of the commit message.
91The message in turn is either (i) at the end of the input, or (ii) the
92last non-whitespace lines before a line that starts with `---` (followed
93by a space or the end of the line).
94
95For convenience, a _<key-alias>_ can be configured to make using `--trailer`
96shorter to type on the command line. This can be configured using the
97`trailer.<key-alias>.key` configuration variable. The _<key-alias>_ must be a prefix
98of the full _<key>_ string, although case sensitivity does not matter. For
99example, if you have
100
101------------------------------------------------
102trailer.sign.key "Signed-off-by: "
103------------------------------------------------
104
105in your configuration, you only need to specify `--trailer="sign: foo"`
106on the command line instead of `--trailer="Signed-off-by: foo"`.
107
108When reading trailers, there can be no whitespace before or inside the
109_<key>_, but any number of regular space and tab characters are allowed
110between the _<key>_ and the separator. There can be whitespaces before,
111inside or after the _<value>_. The _<value>_ may be split over multiple lines
112with each subsequent line starting with at least one whitespace, like
113the "folding" in RFC 822. Example:
114
115------------------------------------------------
116key: This is a very long value, with spaces and
117 newlines in it.
118------------------------------------------------
119
120OTHER RULES
121-----------
122
123What was covered in the previous section are the rules that are relevant
124for regular use. The following points are included for completeness.
125
126--
127* This command ignores comment lines (see `core.commentString` in
128 linkgit:git-config[1]). This is for use with the `prepare-commit-msg`
129 and `commit-msg` hooks.
130
131* Candidate trailer lines that have `:` as the separator, that have no
132 whitespace before the value part, and that start with `//` are not
133 recognized as trailers. This is to avoid accidentally interpreting
134 URLs as trailers (e.g. lines that start with `https://`).
135--
136
137OPTIONS
138-------
139`--in-place`::
140`--no-in-place`::
141 Edit the files in place. The default is `--no-in-place`.
142
143`--trim-empty`::
144`--no-trim-empty`::
145 If the _<value>_ part of any trailer contains only whitespace,
146 the whole trailer will be removed from the output.
147 This applies to existing trailers as well as new trailers.
148+
149The default is `--no-trim-empty`.
150
151`--trailer=<key>[(=|:)<value>]`::
152`--no-trailer`::
153 Specify a (_<key>_, _<value>_) pair that should be applied as a
154 trailer to the inputs. See the description of this command. Can
155 be given multiple times.
156+
157Use `--no-trailer` to reset the list.
158
159`--where=<placement>`::
160`--no-where`::
161 Specify where all new trailers will be added. A setting
162 provided with `--where` overrides the `trailer.where` and any
163 applicable `trailer.<key-alias>.where` configuration variables
164 and applies to all `--trailer` options until the next occurrence of
165 `--where` or `--no-where`. Possible placements are `after`,
166 `before`, `end` or `start`.
167+
168Use `--no-where` to clear the effect of any previous use of `--where`,
169such that the relevant configuration variables are no longer overridden.
170
171`--if-exists=<action>`::
172`--no-if-exists`::
173 Specify what action will be performed when there is already at
174 least one trailer with the same _<key>_ in the input. A setting
175 provided with `--if-exists` overrides the `trailer.ifExists` and any
176 applicable `trailer.<key-alias>.ifExists` configuration variables
177 and applies to all `--trailer` options until the next occurrence of
178 `--if-exists` or `--no-if-exists`. Possible actions are `addIfDifferent`,
179 `addIfDifferentNeighbor`, `add`, `replace` and `doNothing`.
180+
181Use `--no-if-exists` to clear the effect of any previous use of
182`--if-exists`, such that the relevant configuration variables are no
183longer overridden.
184
185`--if-missing=<action>`::
186`--no-if-missing`::
187 Specify what action will be performed when there is no other
188 trailer with the same _<key>_ in the input. A setting
189 provided with `--if-missing` overrides the `trailer.ifMissing` and any
190 applicable `trailer.<key-alias>.ifMissing` configuration variables
191 and applies to all `--trailer` options until the next occurrence of
192 `--if-missing` or `--no-if-missing`. Possible actions are
193 `doNothing` or `add`.
194+
195Use `--no-if-missing` to clear the effect of any previous use of
196`--if-missing`, such that the relevant configuration variables are no
197longer overridden.
198
199`--only-trailers`::
200`--no-only-trailers`::
201 Output only the trailers, not any other parts of the
202 input. The default is `--no-only-trailers`.
203
204`--only-input`::
205`--no-only-input`::
206 Output only trailers that exist in the input; do not add any
207 from the command-line or by applying `trailer.<key-alias>` configuration
208 variables. The default is `--no-only-input`.
209
210`--unfold`::
211`--no-unfold`::
212 If a trailer has a value that runs over multiple lines (aka "folded"),
213 reformat the value into a single line. The default is `--no-unfold`.
214
215`--parse`::
216 A convenience alias for `--only-trailers --only-input
217 --unfold`. This makes it easier to only see the trailers coming from the
218 input without influencing them with any command line options or
219 configuration variables, while also making the output machine-friendly with
220 `--unfold`.
221+
222There is no convenience alias to negate this alias.
223
224`--divider`::
225`--no-divider`::
226 Treat `---` as the end of the commit message. This is the default.
227 Use `--no-divider` when you know your input contains just the
228 commit message itself (and not an email or the output of
229 linkgit:git-format-patch[1]).
230
231CONFIGURATION VARIABLES
232-----------------------
233
234include::includes/cmd-config-section-all.adoc[]
235
236include::config/trailer.adoc[]
237
238EXAMPLES
239--------
240
241* Configure a `sign` trailer with a `Signed-off-by` key, and then
242 add two of these trailers to a commit message file:
243+
244------------
245$ git config trailer.sign.key "Signed-off-by"
246$ cat msg.txt
247subject
248
249body text
250$ git interpret-trailers --trailer 'sign: Alice <alice@example.com>' --trailer 'sign: Bob <bob@example.com>' <msg.txt
251subject
252
253body text
254
255Signed-off-by: Alice <alice@example.com>
256Signed-off-by: Bob <bob@example.com>
257------------
258
259* Use the `--in-place` option to edit a commit message file in place:
260+
261------------
262$ cat msg.txt
263subject
264
265body text
266
267Signed-off-by: Bob <bob@example.com>
268$ git interpret-trailers --trailer 'Acked-by: Alice <alice@example.com>' --in-place msg.txt
269$ cat msg.txt
270subject
271
272body text
273
274Signed-off-by: Bob <bob@example.com>
275Acked-by: Alice <alice@example.com>
276------------
277
278* Extract the last commit as a patch, and add a `Cc` and a
279 `Reviewed-by` trailer to it:
280+
281------------
282$ git format-patch -1
2830001-foo.patch
284$ git interpret-trailers --trailer 'Cc: Alice <alice@example.com>' --trailer 'Reviewed-by: Bob <bob@example.com>' 0001-foo.patch >0001-bar.patch
285------------
286
287* Configure a `sign` trailer with a command to automatically add a
288 "`Signed-off-by:`{nbsp}" with the author information only if there is no
289 "`Signed-off-by:`{nbsp}" already, and show how it works:
290+
291------------
292$ cat msg1.txt
293subject
294
295body text
296$ git config trailer.sign.key "Signed-off-by: "
297$ git config trailer.sign.ifmissing add
298$ git config trailer.sign.ifexists doNothing
299$ git config trailer.sign.cmd 'echo "$(git config user.name) <$(git config user.email)>"'
300$ git interpret-trailers --trailer sign <msg1.txt
301subject
302
303body text
304
305Signed-off-by: Bob <bob@example.com>
306$ cat msg2.txt
307subject
308
309body text
310
311Signed-off-by: Alice <alice@example.com>
312$ git interpret-trailers --trailer sign <msg2.txt
313subject
314
315body text
316
317Signed-off-by: Alice <alice@example.com>
318------------
319
320* Configure a `fix` trailer with a key that contains a `#` and no
321 space after this character, and show how it works:
322+
323------------
324$ git config trailer.separators ":#"
325$ git config trailer.fix.key "Fix #"
326$ echo "subject" | git interpret-trailers --trailer fix=42
327subject
328
329Fix #42
330------------
331
332* Configure a `help` trailer with a command that searches for an author
333 identity and show how it works:
334+
335------------
336$ cat ~/bin/glog-find-author
337#!/bin/sh
338test -n "$1" && git log --author="$1" --pretty="%an <%ae>" -1 || true
339$ cat msg.txt
340subject
341
342body text
343$ git config trailer.help.key "Helped-by: "
344$ git config trailer.help.ifExists "addIfDifferentNeighbor"
345$ git config trailer.help.cmd "~/bin/glog-find-author"
346$ git interpret-trailers --trailer="help:Junio" --trailer="help:Couder" <msg.txt
347subject
348
349body text
350
351Helped-by: Junio C Hamano <gitster@pobox.com>
352Helped-by: Christian Couder <christian.couder@gmail.com>
353------------
354
355* Configure a `ref` trailer with a command that searches for the last
356 relevant commit and show how it works:
357+
358------------
359$ cat ~/bin/glog-grep
360#!/bin/sh
361test -n "$1" && git log --grep "$1" --pretty=reference -1 || true
362$ cat msg.txt
363subject
364
365body text
366$ git config trailer.ref.key "Reference-to: "
367$ git config trailer.ref.ifExists "replace"
368$ git config trailer.ref.cmd "~/bin/glog-grep"
369$ git interpret-trailers --trailer="ref:Add copyright notices." <msg.txt
370subject
371
372body text
373
374Reference-to: 8bc9a0c769 (Add copyright notices., 2005-04-07)
375------------
376
377* Configure a `see` trailer with a command to show the subject of a
378 commit that is related, and show how it works:
379+
380------------
381$ cat msg.txt
382subject
383
384body text
385
386see: HEAD~2
387$ cat ~/bin/glog-ref
388#!/bin/sh
389git log -1 --oneline --format="%h (%s)" --abbrev-commit --abbrev=14
390$ git config trailer.see.key "See-also: "
391$ git config trailer.see.ifExists "replace"
392$ git config trailer.see.ifMissing "doNothing"
393$ git config trailer.see.cmd "glog-ref"
394$ git interpret-trailers --trailer=see <msg.txt
395subject
396
397body text
398
399See-also: fe3187489d69c4 (subject of related commit)
400------------
401
402* Configure a commit template with some trailers with empty values
403 (using sed to show and keep the trailing spaces at the end of the
404 trailers), then configure a commit-msg hook that uses
405 git-interpret-trailers(1) to remove trailers with empty values and to
406 add a `git-version` trailer:
407+
408------------
409$ cat temp.txt
410***subject***
411
412***message***
413
414Fixes: Z
415Cc: Z
416Reviewed-by: Z
417Signed-off-by: Z
418$ sed -e 's/ Z$/ /' temp.txt > commit_template.txt
419$ git config commit.template commit_template.txt
420$ cat .git/hooks/commit-msg
421#!/bin/sh
422git interpret-trailers --trim-empty --trailer "git-version: \$(git describe)" "\$1" > "\$1.new"
423mv "\$1.new" "\$1"
424$ chmod +x .git/hooks/commit-msg
425------------
426
427* Here we try to use three different trailer keys. But it fails because
428 two of them are not recognized as trailer keys.
429+
430----
431$ cat msg.txt
432subject
433
434Skapad-på: some-branch
435Hash-in-v6.11: 45c12d3269fe48f22834320c782ffe86c3560f2c
436Reviewed-by: Alice <alice@example.com>
437$ git interpret-trailers --only-trailers <msg.txt
438$
439----
440+
441Recall that a trailer key has to consist of only ASCII alphanumeric
442characters and hyphens, and this does not hold for the two first
443supposed trailer keys. And now none are recognized as trailers because
444the candidate trailer block has at least one non-trailer line, even
445though `Reviewed-by` is a valid trailer key. Recall that a trailer block
446has to either (i) be all trailers, or (ii) consist of at least one
447Git-generated or user-configured trailer (and some other conditions).
448And (ii) is not satisfied since we have not configured any trailer keys.
449
450SEE ALSO
451--------
452linkgit:git-commit[1], linkgit:git-format-patch[1], linkgit:git-config[1]
453
454GIT
455---
456Part of the linkgit:git[1] suite