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