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