Documentation / git-for-each-ref.txton commit ref-filter.c: parse trailers arguments with %(contents) atom (7a5edbd)
   1git-for-each-ref(1)
   2===================
   3
   4NAME
   5----
   6git-for-each-ref - Output information on each ref
   7
   8SYNOPSIS
   9--------
  10[verse]
  11'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
  12                   [(--sort=<key>)...] [--format=<format>] [<pattern>...]
  13                   [--points-at=<object>]
  14                   (--merged[=<object>] | --no-merged[=<object>])
  15                   [--contains[=<object>]] [--no-contains[=<object>]]
  16
  17DESCRIPTION
  18-----------
  19
  20Iterate over all refs that match `<pattern>` and show them
  21according to the given `<format>`, after sorting them according
  22to the given set of `<key>`.  If `<count>` is given, stop after
  23showing that many refs.  The interpolated values in `<format>`
  24can optionally be quoted as string literals in the specified
  25host language allowing their direct evaluation in that language.
  26
  27OPTIONS
  28-------
  29<pattern>...::
  30        If one or more patterns are given, only refs are shown that
  31        match against at least one pattern, either using fnmatch(3) or
  32        literally, in the latter case matching completely or from the
  33        beginning up to a slash.
  34
  35--count=<count>::
  36        By default the command shows all refs that match
  37        `<pattern>`.  This option makes it stop after showing
  38        that many refs.
  39
  40--sort=<key>::
  41        A field name to sort on.  Prefix `-` to sort in
  42        descending order of the value.  When unspecified,
  43        `refname` is used.  You may use the --sort=<key> option
  44        multiple times, in which case the last key becomes the primary
  45        key.
  46
  47--format=<format>::
  48        A string that interpolates `%(fieldname)` from a ref being shown
  49        and the object it points at.  If `fieldname`
  50        is prefixed with an asterisk (`*`) and the ref points
  51        at a tag object, use the value for the field in the object
  52        which the tag object refers to (instead of the field in the tag object).
  53        When unspecified, `<format>` defaults to
  54        `%(objectname) SPC %(objecttype) TAB %(refname)`.
  55        It also interpolates `%%` to `%`, and `%xx` where `xx`
  56        are hex digits interpolates to character with hex code
  57        `xx`; for example `%00` interpolates to `\0` (NUL),
  58        `%09` to `\t` (TAB) and `%0a` to `\n` (LF).
  59
  60--shell::
  61--perl::
  62--python::
  63--tcl::
  64        If given, strings that substitute `%(fieldname)`
  65        placeholders are quoted as string literals suitable for
  66        the specified host language.  This is meant to produce
  67        a scriptlet that can directly be `eval`ed.
  68
  69--points-at=<object>::
  70        Only list refs which points at the given object.
  71
  72--merged[=<object>]::
  73        Only list refs whose tips are reachable from the
  74        specified commit (HEAD if not specified),
  75        incompatible with `--no-merged`.
  76
  77--no-merged[=<object>]::
  78        Only list refs whose tips are not reachable from the
  79        specified commit (HEAD if not specified),
  80        incompatible with `--merged`.
  81
  82--contains[=<object>]::
  83        Only list refs which contain the specified commit (HEAD if not
  84        specified).
  85
  86--no-contains[=<object>]::
  87        Only list refs which don't contain the specified commit (HEAD
  88        if not specified).
  89
  90--ignore-case::
  91        Sorting and filtering refs are case insensitive.
  92
  93FIELD NAMES
  94-----------
  95
  96Various values from structured fields in referenced objects can
  97be used to interpolate into the resulting output, or as sort
  98keys.
  99
 100For all objects, the following names can be used:
 101
 102refname::
 103        The name of the ref (the part after $GIT_DIR/).
 104        For a non-ambiguous short name of the ref append `:short`.
 105        The option core.warnAmbiguousRefs is used to select the strict
 106        abbreviation mode. If `lstrip=<N>` (`rstrip=<N>`) is appended, strips `<N>`
 107        slash-separated path components from the front (back) of the refname
 108        (e.g. `%(refname:lstrip=2)` turns `refs/tags/foo` into `foo` and
 109        `%(refname:rstrip=2)` turns `refs/tags/foo` into `refs`).
 110        If `<N>` is a negative number, strip as many path components as
 111        necessary from the specified end to leave `-<N>` path components
 112        (e.g. `%(refname:lstrip=-2)` turns
 113        `refs/tags/foo` into `tags/foo` and `%(refname:rstrip=-1)`
 114        turns `refs/tags/foo` into `refs`). When the ref does not have
 115        enough components, the result becomes an empty string if
 116        stripping with positive <N>, or it becomes the full refname if
 117        stripping with negative <N>.  Neither is an error.
 118+
 119`strip` can be used as a synomym to `lstrip`.
 120
 121objecttype::
 122        The type of the object (`blob`, `tree`, `commit`, `tag`).
 123
 124objectsize::
 125        The size of the object (the same as 'git cat-file -s' reports).
 126
 127objectname::
 128        The object name (aka SHA-1).
 129        For a non-ambiguous abbreviation of the object name append `:short`.
 130        For an abbreviation of the object name with desired length append
 131        `:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
 132        length may be exceeded to ensure unique object names.
 133
 134upstream::
 135        The name of a local ref which can be considered ``upstream''
 136        from the displayed ref. Respects `:short`, `:lstrip` and
 137        `:rstrip` in the same way as `refname` above.  Additionally
 138        respects `:track` to show "[ahead N, behind M]" and
 139        `:trackshort` to show the terse version: ">" (ahead), "<"
 140        (behind), "<>" (ahead and behind), or "=" (in sync). `:track`
 141        also prints "[gone]" whenever unknown upstream ref is
 142        encountered. Append `:track,nobracket` to show tracking
 143        information without brackets (i.e "ahead N, behind M").  Has
 144        no effect if the ref does not have tracking information
 145        associated with it.  All the options apart from `nobracket`
 146        are mutually exclusive, but if used together the last option
 147        is selected.
 148
 149push::
 150        The name of a local ref which represents the `@{push}`
 151        location for the displayed ref. Respects `:short`, `:lstrip`,
 152        `:rstrip`, `:track`, and `:trackshort` options as `upstream`
 153        does. Produces an empty string if no `@{push}` ref is
 154        configured.
 155
 156HEAD::
 157        '*' if HEAD matches current ref (the checked out branch), ' '
 158        otherwise.
 159
 160color::
 161        Change output color. Followed by `:<colorname>`, where color
 162        names are described under Values in the "CONFIGURATION FILE"
 163        section of linkgit:git-config[1].  For example,
 164        `%(color:bold red)`.
 165
 166align::
 167        Left-, middle-, or right-align the content between
 168        %(align:...) and %(end). The "align:" is followed by
 169        `width=<width>` and `position=<position>` in any order
 170        separated by a comma, where the `<position>` is either left,
 171        right or middle, default being left and `<width>` is the total
 172        length of the content with alignment. For brevity, the
 173        "width=" and/or "position=" prefixes may be omitted, and bare
 174        <width> and <position> used instead.  For instance,
 175        `%(align:<width>,<position>)`. If the contents length is more
 176        than the width then no alignment is performed. If used with
 177        `--quote` everything in between %(align:...) and %(end) is
 178        quoted, but if nested then only the topmost level performs
 179        quoting.
 180
 181if::
 182        Used as %(if)...%(then)...%(end) or
 183        %(if)...%(then)...%(else)...%(end).  If there is an atom with
 184        value or string literal after the %(if) then everything after
 185        the %(then) is printed, else if the %(else) atom is used, then
 186        everything after %(else) is printed. We ignore space when
 187        evaluating the string before %(then), this is useful when we
 188        use the %(HEAD) atom which prints either "*" or " " and we
 189        want to apply the 'if' condition only on the 'HEAD' ref.
 190        Append ":equals=<string>" or ":notequals=<string>" to compare
 191        the value between the %(if:...) and %(then) atoms with the
 192        given string.
 193
 194symref::
 195        The ref which the given symbolic ref refers to. If not a
 196        symbolic ref, nothing is printed. Respects the `:short`,
 197        `:lstrip` and `:rstrip` options in the same way as `refname`
 198        above.
 199
 200In addition to the above, for commit and tag objects, the header
 201field names (`tree`, `parent`, `object`, `type`, and `tag`) can
 202be used to specify the value in the header field.
 203
 204For commit and tag objects, the special `creatordate` and `creator`
 205fields will correspond to the appropriate date or name-email-date tuple
 206from the `committer` or `tagger` fields depending on the object type.
 207These are intended for working on a mix of annotated and lightweight tags.
 208
 209Fields that have name-email-date tuple as its value (`author`,
 210`committer`, and `tagger`) can be suffixed with `name`, `email`,
 211and `date` to extract the named component.
 212
 213The complete message in a commit and tag object is `contents`.
 214Its first line is `contents:subject`, where subject is the concatenation
 215of all lines of the commit message up to the first blank line.  The next
 216line is `contents:body`, where body is all of the lines after the first
 217blank line.  The optional GPG signature is `contents:signature`.  The
 218first `N` lines of the message is obtained using `contents:lines=N`.
 219Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
 220are obtained as `trailers` (or by using the historical alias
 221`contents:trailers`).  Non-trailer lines from the trailer block can be omitted
 222with `trailers:only`. Whitespace-continuations can be removed from trailers so
 223that each trailer appears on a line by itself with its full content with
 224`trailers:unfold`. Both can be used together as `trailers:unfold,only`.
 225
 226For sorting purposes, fields with numeric values sort in numeric order
 227(`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
 228All other fields are used to sort in their byte-value order.
 229
 230There is also an option to sort by versions, this can be done by using
 231the fieldname `version:refname` or its alias `v:refname`.
 232
 233In any case, a field name that refers to a field inapplicable to
 234the object referred by the ref does not cause an error.  It
 235returns an empty string instead.
 236
 237As a special case for the date-type fields, you may specify a format for
 238the date by adding `:` followed by date format name (see the
 239values the `--date` option to linkgit:git-rev-list[1] takes).
 240
 241Some atoms like %(align) and %(if) always require a matching %(end).
 242We call them "opening atoms" and sometimes denote them as %($open).
 243
 244When a scripting language specific quoting is in effect, everything
 245between a top-level opening atom and its matching %(end) is evaluated
 246according to the semantics of the opening atom and only its result
 247from the top-level is quoted.
 248
 249
 250EXAMPLES
 251--------
 252
 253An example directly producing formatted text.  Show the most recent
 2543 tagged commits:
 255
 256------------
 257#!/bin/sh
 258
 259git for-each-ref --count=3 --sort='-*authordate' \
 260--format='From: %(*authorname) %(*authoremail)
 261Subject: %(*subject)
 262Date: %(*authordate)
 263Ref: %(*refname)
 264
 265%(*body)
 266' 'refs/tags'
 267------------
 268
 269
 270A simple example showing the use of shell eval on the output,
 271demonstrating the use of --shell.  List the prefixes of all heads:
 272------------
 273#!/bin/sh
 274
 275git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
 276while read entry
 277do
 278        eval "$entry"
 279        echo `dirname $ref`
 280done
 281------------
 282
 283
 284A bit more elaborate report on tags, demonstrating that the format
 285may be an entire script:
 286------------
 287#!/bin/sh
 288
 289fmt='
 290        r=%(refname)
 291        t=%(*objecttype)
 292        T=${r#refs/tags/}
 293
 294        o=%(*objectname)
 295        n=%(*authorname)
 296        e=%(*authoremail)
 297        s=%(*subject)
 298        d=%(*authordate)
 299        b=%(*body)
 300
 301        kind=Tag
 302        if test "z$t" = z
 303        then
 304                # could be a lightweight tag
 305                t=%(objecttype)
 306                kind="Lightweight tag"
 307                o=%(objectname)
 308                n=%(authorname)
 309                e=%(authoremail)
 310                s=%(subject)
 311                d=%(authordate)
 312                b=%(body)
 313        fi
 314        echo "$kind $T points at a $t object $o"
 315        if test "z$t" = zcommit
 316        then
 317                echo "The commit was authored by $n $e
 318at $d, and titled
 319
 320    $s
 321
 322Its message reads as:
 323"
 324                echo "$b" | sed -e "s/^/    /"
 325                echo
 326        fi
 327'
 328
 329eval=`git for-each-ref --shell --format="$fmt" \
 330        --sort='*objecttype' \
 331        --sort=-taggerdate \
 332        refs/tags`
 333eval "$eval"
 334------------
 335
 336
 337An example to show the usage of %(if)...%(then)...%(else)...%(end).
 338This prefixes the current branch with a star.
 339
 340------------
 341git for-each-ref --format="%(if)%(HEAD)%(then)* %(else)  %(end)%(refname:short)" refs/heads/
 342------------
 343
 344
 345An example to show the usage of %(if)...%(then)...%(end).
 346This prints the authorname, if present.
 347
 348------------
 349git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"
 350------------
 351
 352SEE ALSO
 353--------
 354linkgit:git-show-ref[1]
 355
 356GIT
 357---
 358Part of the linkgit:git[1] suite