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