refs/reftable: track last log record name via strbuf
[alt-git.git] / Documentation / git-for-each-ref.txt
blob3a9ad91b7af89a66cdd460f5ce73eaf533dad719
1 git-for-each-ref(1)
2 ===================
4 NAME
5 ----
6 git-for-each-ref - Output information on each ref
8 SYNOPSIS
9 --------
10 [verse]
11 'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
12                    [(--sort=<key>)...] [--format=<format>]
13                    [ --stdin | <pattern>... ]
14                    [--points-at=<object>]
15                    [--merged[=<object>]] [--no-merged[=<object>]]
16                    [--contains[=<object>]] [--no-contains[=<object>]]
17                    [--exclude=<pattern> ...]
19 DESCRIPTION
20 -----------
22 Iterate over all refs that match `<pattern>` and show them
23 according to the given `<format>`, after sorting them according
24 to the given set of `<key>`.  If `<count>` is given, stop after
25 showing that many refs.  The interpolated values in `<format>`
26 can optionally be quoted as string literals in the specified
27 host language allowing their direct evaluation in that language.
29 OPTIONS
30 -------
31 <pattern>...::
32         If one or more patterns are given, only refs are shown that
33         match against at least one pattern, either using fnmatch(3) or
34         literally, in the latter case matching completely or from the
35         beginning up to a slash.
37 --stdin::
38         If `--stdin` is supplied, then the list of patterns is read from
39         standard input instead of from the argument list.
41 --count=<count>::
42         By default the command shows all refs that match
43         `<pattern>`.  This option makes it stop after showing
44         that many refs.
46 --sort=<key>::
47         A field name to sort on.  Prefix `-` to sort in
48         descending order of the value.  When unspecified,
49         `refname` is used.  You may use the --sort=<key> option
50         multiple times, in which case the last key becomes the primary
51         key.
53 --format=<format>::
54         A string that interpolates `%(fieldname)` from a ref being shown and
55         the object it points at. In addition, the string literal `%%`
56         renders as `%` and `%xx` - where `xx` are hex digits - renders as
57         the character with hex code `xx`. For example, `%00` interpolates to
58         `\0` (NUL), `%09` to `\t` (TAB), and `%0a` to `\n` (LF).
60 When unspecified, `<format>` defaults to `%(objectname) SPC %(objecttype)
61 TAB %(refname)`.
63 --color[=<when>]::
64         Respect any colors specified in the `--format` option. The
65         `<when>` field must be one of `always`, `never`, or `auto` (if
66         `<when>` is absent, behave as if `always` was given).
68 --shell::
69 --perl::
70 --python::
71 --tcl::
72         If given, strings that substitute `%(fieldname)`
73         placeholders are quoted as string literals suitable for
74         the specified host language.  This is meant to produce
75         a scriptlet that can directly be `eval`ed.
77 --points-at=<object>::
78         Only list refs which points at the given object.
80 --merged[=<object>]::
81         Only list refs whose tips are reachable from the
82         specified commit (HEAD if not specified).
84 --no-merged[=<object>]::
85         Only list refs whose tips are not reachable from the
86         specified commit (HEAD if not specified).
88 --contains[=<object>]::
89         Only list refs which contain the specified commit (HEAD if not
90         specified).
92 --no-contains[=<object>]::
93         Only list refs which don't contain the specified commit (HEAD
94         if not specified).
96 --ignore-case::
97         Sorting and filtering refs are case insensitive.
99 --omit-empty::
100         Do not print a newline after formatted refs where the format expands
101         to the empty string.
103 --exclude=<pattern>::
104         If one or more patterns are given, only refs which do not match
105         any excluded pattern(s) are shown. Matching is done using the
106         same rules as `<pattern>` above.
108 FIELD NAMES
109 -----------
111 Various values from structured fields in referenced objects can
112 be used to interpolate into the resulting output, or as sort
113 keys.
115 For all objects, the following names can be used:
117 refname::
118         The name of the ref (the part after $GIT_DIR/).
119         For a non-ambiguous short name of the ref append `:short`.
120         The option core.warnAmbiguousRefs is used to select the strict
121         abbreviation mode. If `lstrip=<N>` (`rstrip=<N>`) is appended, strips `<N>`
122         slash-separated path components from the front (back) of the refname
123         (e.g. `%(refname:lstrip=2)` turns `refs/tags/foo` into `foo` and
124         `%(refname:rstrip=2)` turns `refs/tags/foo` into `refs`).
125         If `<N>` is a negative number, strip as many path components as
126         necessary from the specified end to leave `-<N>` path components
127         (e.g. `%(refname:lstrip=-2)` turns
128         `refs/tags/foo` into `tags/foo` and `%(refname:rstrip=-1)`
129         turns `refs/tags/foo` into `refs`). When the ref does not have
130         enough components, the result becomes an empty string if
131         stripping with positive <N>, or it becomes the full refname if
132         stripping with negative <N>.  Neither is an error.
134 `strip` can be used as a synonym to `lstrip`.
136 objecttype::
137         The type of the object (`blob`, `tree`, `commit`, `tag`).
139 objectsize::
140         The size of the object (the same as 'git cat-file -s' reports).
141         Append `:disk` to get the size, in bytes, that the object takes up on
142         disk. See the note about on-disk sizes in the `CAVEATS` section below.
143 objectname::
144         The object name (aka SHA-1).
145         For a non-ambiguous abbreviation of the object name append `:short`.
146         For an abbreviation of the object name with desired length append
147         `:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
148         length may be exceeded to ensure unique object names.
149 deltabase::
150         This expands to the object name of the delta base for the
151         given object, if it is stored as a delta.  Otherwise it
152         expands to the null object name (all zeroes).
154 upstream::
155         The name of a local ref which can be considered ``upstream''
156         from the displayed ref. Respects `:short`, `:lstrip` and
157         `:rstrip` in the same way as `refname` above.  Additionally
158         respects `:track` to show "[ahead N, behind M]" and
159         `:trackshort` to show the terse version: ">" (ahead), "<"
160         (behind), "<>" (ahead and behind), or "=" (in sync). `:track`
161         also prints "[gone]" whenever unknown upstream ref is
162         encountered. Append `:track,nobracket` to show tracking
163         information without brackets (i.e "ahead N, behind M").
165 For any remote-tracking branch `%(upstream)`, `%(upstream:remotename)`
166 and `%(upstream:remoteref)` refer to the name of the remote and the
167 name of the tracked remote ref, respectively. In other words, the
168 remote-tracking branch can be updated explicitly and individually by
169 using the refspec `%(upstream:remoteref):%(upstream)` to fetch from
170 `%(upstream:remotename)`.
172 Has no effect if the ref does not have tracking information associated
173 with it.  All the options apart from `nobracket` are mutually exclusive,
174 but if used together the last option is selected.
176 push::
177         The name of a local ref which represents the `@{push}`
178         location for the displayed ref. Respects `:short`, `:lstrip`,
179         `:rstrip`, `:track`, `:trackshort`, `:remotename`, and `:remoteref`
180         options as `upstream` does. Produces an empty string if no `@{push}`
181         ref is configured.
183 HEAD::
184         '*' if HEAD matches current ref (the checked out branch), ' '
185         otherwise.
187 color::
188         Change output color. Followed by `:<colorname>`, where color
189         names are described under Values in the "CONFIGURATION FILE"
190         section of linkgit:git-config[1].  For example,
191         `%(color:bold red)`.
193 align::
194         Left-, middle-, or right-align the content between
195         %(align:...) and %(end). The "align:" is followed by
196         `width=<width>` and `position=<position>` in any order
197         separated by a comma, where the `<position>` is either left,
198         right or middle, default being left and `<width>` is the total
199         length of the content with alignment. For brevity, the
200         "width=" and/or "position=" prefixes may be omitted, and bare
201         <width> and <position> used instead.  For instance,
202         `%(align:<width>,<position>)`. If the contents length is more
203         than the width then no alignment is performed. If used with
204         `--quote` everything in between %(align:...) and %(end) is
205         quoted, but if nested then only the topmost level performs
206         quoting.
208 if::
209         Used as %(if)...%(then)...%(end) or
210         %(if)...%(then)...%(else)...%(end).  If there is an atom with
211         value or string literal after the %(if) then everything after
212         the %(then) is printed, else if the %(else) atom is used, then
213         everything after %(else) is printed. We ignore space when
214         evaluating the string before %(then), this is useful when we
215         use the %(HEAD) atom which prints either "*" or " " and we
216         want to apply the 'if' condition only on the 'HEAD' ref.
217         Append ":equals=<string>" or ":notequals=<string>" to compare
218         the value between the %(if:...) and %(then) atoms with the
219         given string.
221 symref::
222         The ref which the given symbolic ref refers to. If not a
223         symbolic ref, nothing is printed. Respects the `:short`,
224         `:lstrip` and `:rstrip` options in the same way as `refname`
225         above.
227 signature::
228         The GPG signature of a commit.
230 signature:grade::
231         Show "G" for a good (valid) signature, "B" for a bad
232         signature, "U" for a good signature with unknown validity, "X"
233         for a good signature that has expired, "Y" for a good
234         signature made by an expired key, "R" for a good signature
235         made by a revoked key, "E" if the signature cannot be
236         checked (e.g. missing key) and "N" for no signature.
238 signature:signer::
239         The signer of the GPG signature of a commit.
241 signature:key::
242         The key of the GPG signature of a commit.
244 signature:fingerprint::
245         The fingerprint of the GPG signature of a commit.
247 signature:primarykeyfingerprint::
248         The primary key fingerprint of the GPG signature of a commit.
250 signature:trustlevel::
251         The trust level of the GPG signature of a commit. Possible
252         outputs are `ultimate`, `fully`, `marginal`, `never` and `undefined`.
254 worktreepath::
255         The absolute path to the worktree in which the ref is checked
256         out, if it is checked out in any linked worktree. Empty string
257         otherwise.
259 ahead-behind:<committish>::
260         Two integers, separated by a space, demonstrating the number of
261         commits ahead and behind, respectively, when comparing the output
262         ref to the `<committish>` specified in the format.
264 describe[:options]::
265         A human-readable name, like linkgit:git-describe[1];
266         empty string for undescribable commits. The `describe` string may
267         be followed by a colon and one or more comma-separated options.
270 tags=<bool-value>;;
271         Instead of only considering annotated tags, consider
272         lightweight tags as well; see the corresponding option in
273         linkgit:git-describe[1] for details.
274 abbrev=<number>;;
275         Use at least <number> hexadecimal digits; see the corresponding
276         option in linkgit:git-describe[1] for details.
277 match=<pattern>;;
278         Only consider tags matching the given `glob(7)` pattern,
279         excluding the "refs/tags/" prefix; see the corresponding option
280         in linkgit:git-describe[1] for details.
281 exclude=<pattern>;;
282         Do not consider tags matching the given `glob(7)` pattern,
283         excluding the "refs/tags/" prefix; see the corresponding option
284         in linkgit:git-describe[1] for details.
287 In addition to the above, for commit and tag objects, the header
288 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
289 be used to specify the value in the header field.
290 Fields `tree` and `parent` can also be used with modifier `:short` and
291 `:short=<length>` just like `objectname`.
293 For commit and tag objects, the special `creatordate` and `creator`
294 fields will correspond to the appropriate date or name-email-date tuple
295 from the `committer` or `tagger` fields depending on the object type.
296 These are intended for working on a mix of annotated and lightweight tags.
298 For tag objects, a `fieldname` prefixed with an asterisk (`*`) expands to
299 the `fieldname` value of the peeled object, rather than that of the tag
300 object itself.
302 Fields that have name-email-date tuple as its value (`author`,
303 `committer`, and `tagger`) can be suffixed with `name`, `email`,
304 and `date` to extract the named component.  For email fields (`authoremail`,
305 `committeremail` and `taggeremail`), `:trim` can be appended to get the email
306 without angle brackets, and `:localpart` to get the part before the `@` symbol
307 out of the trimmed email. In addition to these, the `:mailmap` option and the
308 corresponding `:mailmap,trim` and `:mailmap,localpart` can be used (order does
309 not matter) to get values of the name and email according to the .mailmap file
310 or according to the file set in the mailmap.file or mailmap.blob configuration
311 variable (see linkgit:gitmailmap[5]).
313 The raw data in an object is `raw`.
315 raw:size::
316         The raw data size of the object.
318 Note that `--format=%(raw)` can not be used with `--python`, `--shell`, `--tcl`,
319 because such language may not support arbitrary binary data in their string
320 variable type.
322 The message in a commit or a tag object is `contents`, from which
323 `contents:<part>` can be used to extract various parts out of:
325 contents:size::
326         The size in bytes of the commit or tag message.
328 contents:subject::
329         The first paragraph of the message, which typically is a
330         single line, is taken as the "subject" of the commit or the
331         tag message.
332         Instead of `contents:subject`, field `subject` can also be used to
333         obtain same results. `:sanitize` can be appended to `subject` for
334         subject line suitable for filename.
336 contents:body::
337         The remainder of the commit or the tag message that follows
338         the "subject".
340 contents:signature::
341         The optional GPG signature of the tag.
343 contents:lines=N::
344         The first `N` lines of the message.
346 Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
347 are obtained as `trailers[:options]` (or by using the historical alias
348 `contents:trailers[:options]`). For valid [:option] values see `trailers`
349 section of linkgit:git-log[1].
351 For sorting purposes, fields with numeric values sort in numeric order
352 (`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
353 All other fields are used to sort in their byte-value order.
355 There is also an option to sort by versions, this can be done by using
356 the fieldname `version:refname` or its alias `v:refname`.
358 In any case, a field name that refers to a field inapplicable to
359 the object referred by the ref does not cause an error.  It
360 returns an empty string instead.
362 As a special case for the date-type fields, you may specify a format for the
363 date by adding `:` followed by date format name (see the values the `--date`
364 option to linkgit:git-rev-list[1] takes). If this formatting is provided in
365 a `--sort` key, references will be sorted according to the byte-value of the
366 formatted string rather than the numeric value of the underlying timestamp.
368 Some atoms like %(align) and %(if) always require a matching %(end).
369 We call them "opening atoms" and sometimes denote them as %($open).
371 When a scripting language specific quoting is in effect, everything
372 between a top-level opening atom and its matching %(end) is evaluated
373 according to the semantics of the opening atom and only its result
374 from the top-level is quoted.
377 EXAMPLES
378 --------
380 An example directly producing formatted text.  Show the most recent
381 3 tagged commits:
383 ------------
384 #!/bin/sh
386 git for-each-ref --count=3 --sort='-*authordate' \
387 --format='From: %(*authorname) %(*authoremail)
388 Subject: %(*subject)
389 Date: %(*authordate)
390 Ref: %(*refname)
392 %(*body)
393 ' 'refs/tags'
394 ------------
397 A simple example showing the use of shell eval on the output,
398 demonstrating the use of --shell.  List the prefixes of all heads:
399 ------------
400 #!/bin/sh
402 git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
403 while read entry
405         eval "$entry"
406         echo `dirname $ref`
407 done
408 ------------
411 A bit more elaborate report on tags, demonstrating that the format
412 may be an entire script:
413 ------------
414 #!/bin/sh
416 fmt='
417         r=%(refname)
418         t=%(*objecttype)
419         T=${r#refs/tags/}
421         o=%(*objectname)
422         n=%(*authorname)
423         e=%(*authoremail)
424         s=%(*subject)
425         d=%(*authordate)
426         b=%(*body)
428         kind=Tag
429         if test "z$t" = z
430         then
431                 # could be a lightweight tag
432                 t=%(objecttype)
433                 kind="Lightweight tag"
434                 o=%(objectname)
435                 n=%(authorname)
436                 e=%(authoremail)
437                 s=%(subject)
438                 d=%(authordate)
439                 b=%(body)
440         fi
441         echo "$kind $T points at a $t object $o"
442         if test "z$t" = zcommit
443         then
444                 echo "The commit was authored by $n $e
445 at $d, and titled
447     $s
449 Its message reads as:
451                 echo "$b" | sed -e "s/^/    /"
452                 echo
453         fi
456 eval=`git for-each-ref --shell --format="$fmt" \
457         --sort='*objecttype' \
458         --sort=-taggerdate \
459         refs/tags`
460 eval "$eval"
461 ------------
464 An example to show the usage of %(if)...%(then)...%(else)...%(end).
465 This prefixes the current branch with a star.
467 ------------
468 git for-each-ref --format="%(if)%(HEAD)%(then)* %(else)  %(end)%(refname:short)" refs/heads/
469 ------------
472 An example to show the usage of %(if)...%(then)...%(end).
473 This prints the authorname, if present.
475 ------------
476 git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"
477 ------------
479 CAVEATS
480 -------
482 Note that the sizes of objects on disk are reported accurately, but care
483 should be taken in drawing conclusions about which refs or objects are
484 responsible for disk usage. The size of a packed non-delta object may be
485 much larger than the size of objects which delta against it, but the
486 choice of which object is the base and which is the delta is arbitrary
487 and is subject to change during a repack.
489 Note also that multiple copies of an object may be present in the object
490 database; in this case, it is undefined which copy's size or delta base
491 will be reported.
493 NOTES
494 -----
496 include::ref-reachability-filters.txt[]
498 SEE ALSO
499 --------
500 linkgit:git-show-ref[1]
504 Part of the linkgit:git[1] suite