]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-for-each-ref.txt
ref-filter.c: use peeled tag for '*' format fields
[thirdparty/git.git] / Documentation / git-for-each-ref.txt
CommitLineData
9f613ddd
JH
1git-for-each-ref(1)
2===================
3
4NAME
5----
6git-for-each-ref - Output information on each ref
7
8SYNOPSIS
9--------
97925fde 10[verse]
b1889c36 11'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
b73dec55
DS
12 [(--sort=<key>)...] [--format=<format>]
13 [ --stdin | <pattern>... ]
3233d51d 14 [--points-at=<object>]
21bf9339 15 [--merged[=<object>]] [--no-merged[=<object>]]
3233d51d 16 [--contains[=<object>]] [--no-contains[=<object>]]
8255dd8a 17 [--exclude=<pattern> ...]
9f613ddd
JH
18
19DESCRIPTION
20-----------
21
22Iterate over all refs that match `<pattern>` and show them
23according to the given `<format>`, after sorting them according
d4040e0a 24to the given set of `<key>`. If `<count>` is given, stop after
23bfbb81 25showing that many refs. The interpolated values in `<format>`
9f613ddd 26can optionally be quoted as string literals in the specified
1729fa98 27host language allowing their direct evaluation in that language.
9f613ddd
JH
28
29OPTIONS
30-------
f7a32dd9
KD
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.
36
b73dec55
DS
37--stdin::
38 If `--stdin` is supplied, then the list of patterns is read from
39 standard input instead of from the argument list.
40
f7a32dd9 41--count=<count>::
9f613ddd
JH
42 By default the command shows all refs that match
43 `<pattern>`. This option makes it stop after showing
44 that many refs.
45
f7a32dd9 46--sort=<key>::
9f613ddd
JH
47 A field name to sort on. Prefix `-` to sort in
48 descending order of the value. When unspecified,
c0f6dc9b
LW
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.
9f613ddd 52
f7a32dd9 53--format=<format>::
d1dfe6e9
VD
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).
59+
60When unspecified, `<format>` defaults to `%(objectname) SPC %(objecttype)
61TAB %(refname)`.
9f613ddd 62
dd61cc1c 63--color[=<when>]::
0c88bf50
JK
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).
67
3240240f
SB
68--shell::
69--perl::
70--python::
71--tcl::
9f613ddd
JH
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.
76
3233d51d 77--points-at=<object>::
d325406e 78 Only list refs which points at the given object.
9f613ddd 79
3233d51d 80--merged[=<object>]::
7c328348 81 Only list refs whose tips are reachable from the
21bf9339 82 specified commit (HEAD if not specified).
7c328348 83
3233d51d 84--no-merged[=<object>]::
7c328348 85 Only list refs whose tips are not reachable from the
21bf9339 86 specified commit (HEAD if not specified).
7c328348 87
3233d51d 88--contains[=<object>]::
8b5a3e98 89 Only list refs which contain the specified commit (HEAD if not
4a71109a
KN
90 specified).
91
3233d51d 92--no-contains[=<object>]::
ac3f5a34
ÆAB
93 Only list refs which don't contain the specified commit (HEAD
94 if not specified).
95
3bb16a8b
NTND
96--ignore-case::
97 Sorting and filtering refs are case insensitive.
98
aabfdc95
ØW
99--omit-empty::
100 Do not print a newline after formatted refs where the format expands
101 to the empty string.
102
8255dd8a
TB
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.
107
9f613ddd
JH
108FIELD NAMES
109-----------
110
111Various values from structured fields in referenced objects can
112be used to interpolate into the resulting output, or as sort
113keys.
114
115For all objects, the following names can be used:
116
117refname::
69057cf3 118 The name of the ref (the part after $GIT_DIR/).
7d66f21a 119 For a non-ambiguous short name of the ref append `:short`.
2bb98169 120 The option core.warnAmbiguousRefs is used to select the strict
1a34728e
KN
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`).
1a0ca5e3 125 If `<N>` is a negative number, strip as many path components as
1a34728e 126 necessary from the specified end to leave `-<N>` path components
1a0ca5e3 127 (e.g. `%(refname:lstrip=-2)` turns
1a34728e
KN
128 `refs/tags/foo` into `tags/foo` and `%(refname:rstrip=-1)`
129 turns `refs/tags/foo` into `refs`). When the ref does not have
1a0ca5e3
KN
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.
44a6b6ce 133+
c30d4f1b 134`strip` can be used as a synonym to `lstrip`.
9f613ddd
JH
135
136objecttype::
137 The type of the object (`blob`, `tree`, `commit`, `tag`).
138
139objectsize::
0b444cdb 140 The size of the object (the same as 'git cat-file -s' reports).
59012fe5
OT
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.
9f613ddd
JH
143objectname::
144 The object name (aka SHA-1).
67687fea 145 For a non-ambiguous abbreviation of the object name append `:short`.
42d0eb05
KN
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.
59012fe5
OT
149deltabase::
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).
9f613ddd 153
8cae19d9
JK
154upstream::
155 The name of a local ref which can be considered ``upstream''
1a34728e
KN
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
cc72385f
JS
163 information without brackets (i.e "ahead N, behind M").
164+
9700fae5
W
165For any remote-tracking branch `%(upstream)`, `%(upstream:remotename)`
166and `%(upstream:remoteref)` refer to the name of the remote and the
167name of the tracked remote ref, respectively. In other words, the
168remote-tracking branch can be updated explicitly and individually by
169using the refspec `%(upstream:remoteref):%(upstream)` to fetch from
170`%(upstream:remotename)`.
cc72385f
JS
171+
172Has no effect if the ref does not have tracking information associated
173with it. All the options apart from `nobracket` are mutually exclusive,
174but if used together the last option is selected.
8cae19d9 175
29bc8850 176push::
3ba308cb 177 The name of a local ref which represents the `@{push}`
17938f17 178 location for the displayed ref. Respects `:short`, `:lstrip`,
9700fae5
W
179 `:rstrip`, `:track`, `:trackshort`, `:remotename`, and `:remoteref`
180 options as `upstream` does. Produces an empty string if no `@{push}`
181 ref is configured.
29bc8850 182
7a48b832
RR
183HEAD::
184 '*' if HEAD matches current ref (the checked out branch), ' '
185 otherwise.
186
fddb74c9 187color::
5d3d0681
JK
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)`.
fddb74c9 192
ce592082
KN
193align::
194 Left-, middle-, or right-align the content between
395fb8f9
KN
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
bcf9626a 204 `--quote` everything in between %(align:...) and %(end) is
395fb8f9
KN
205 quoted, but if nested then only the topmost level performs
206 quoting.
ce592082 207
c58492d4
KN
208if::
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.
4f3e3b37
KN
217 Append ":equals=<string>" or ":notequals=<string>" to compare
218 the value between the %(if:...) and %(then) atoms with the
219 given string.
c58492d4 220
a7984101
KN
221symref::
222 The ref which the given symbolic ref refers to. If not a
1a34728e
KN
223 symbolic ref, nothing is printed. Respects the `:short`,
224 `:lstrip` and `:rstrip` options in the same way as `refname`
225 above.
a7984101 226
26c9c03f
KS
227signature::
228 The GPG signature of a commit.
229
230signature: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.
237
238signature:signer::
239 The signer of the GPG signature of a commit.
240
241signature:key::
242 The key of the GPG signature of a commit.
243
244signature:fingerprint::
245 The fingerprint of the GPG signature of a commit.
246
247signature:primarykeyfingerprint::
248 The primary key fingerprint of the GPG signature of a commit.
249
250signature:trustlevel::
251 The trust level of the GPG signature of a commit. Possible
252 outputs are `ultimate`, `fully`, `marginal`, `never` and `undefined`.
253
2582083f
NB
254worktreepath::
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.
258
49abcd21
DS
259ahead-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.
263
f5d18f8c
KS
264describe[: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.
268+
269--
270tags=<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.
274abbrev=<number>;;
275 Use at least <number> hexadecimal digits; see the corresponding
276 option in linkgit:git-describe[1] for details.
277match=<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.
281exclude=<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.
285--
286
9f613ddd
JH
287In addition to the above, for commit and tag objects, the header
288field names (`tree`, `parent`, `object`, `type`, and `tag`) can
289be used to specify the value in the header field.
26bc0aaf 290Fields `tree` and `parent` can also be used with modifier `:short` and
837adb10 291`:short=<length>` just like `objectname`.
9f613ddd 292
e914ef0d
EW
293For commit and tag objects, the special `creatordate` and `creator`
294fields will correspond to the appropriate date or name-email-date tuple
295from the `committer` or `tagger` fields depending on the object type.
296These are intended for working on a mix of annotated and lightweight tags.
297
d1dfe6e9 298For tag objects, a `fieldname` prefixed with an asterisk (`*`) expands to
188782ec
VD
299the `fieldname` value of the peeled object, rather than that of the tag
300object itself.
d1dfe6e9 301
9f613ddd
JH
302Fields that have name-email-date tuple as its value (`author`,
303`committer`, and `tagger`) can be suffixed with `name`, `email`,
b82445dc
HV
304and `date` to extract the named component. For email fields (`authoremail`,
305`committeremail` and `taggeremail`), `:trim` can be appended to get the email
306without angle brackets, and `:localpart` to get the part before the `@` symbol
307out of the trimmed email.
9f613ddd 308
bd0708c7
ZH
309The raw data in an object is `raw`.
310
311raw:size::
312 The raw data size of the object.
313
314Note that `--format=%(raw)` can not be used with `--python`, `--shell`, `--tcl`,
7121c4d4
ZH
315because such language may not support arbitrary binary data in their string
316variable type.
bd0708c7 317
9fcc9caf
CC
318The message in a commit or a tag object is `contents`, from which
319`contents:<part>` can be used to extract various parts out of:
320
b6839fda
CC
321contents:size::
322 The size in bytes of the commit or tag message.
323
9fcc9caf
CC
324contents:subject::
325 The first paragraph of the message, which typically is a
326 single line, is taken as the "subject" of the commit or the
327 tag message.
905f0a4e
HV
328 Instead of `contents:subject`, field `subject` can also be used to
329 obtain same results. `:sanitize` can be appended to `subject` for
330 subject line suitable for filename.
9fcc9caf
CC
331
332contents:body::
333 The remainder of the commit or the tag message that follows
334 the "subject".
335
336contents:signature::
337 The optional GPG signature of the tag.
338
339contents:lines=N::
340 The first `N` lines of the message.
341
b1d31c89 342Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
ee82a487
HV
343are obtained as `trailers[:options]` (or by using the historical alias
344`contents:trailers[:options]`). For valid [:option] values see `trailers`
345section of linkgit:git-log[1].
9f613ddd 346
e914ef0d
EW
347For sorting purposes, fields with numeric values sort in numeric order
348(`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
9f613ddd
JH
349All other fields are used to sort in their byte-value order.
350
90c00408
KN
351There is also an option to sort by versions, this can be done by using
352the fieldname `version:refname` or its alias `v:refname`.
353
9f613ddd
JH
354In any case, a field name that refers to a field inapplicable to
355the object referred by the ref does not cause an error. It
356returns an empty string instead.
357
d392e712 358As a special case for the date-type fields, you may specify a format for
8f50d263 359the date by adding `:` followed by date format name (see the
1cca17df 360values the `--date` option to linkgit:git-rev-list[1] takes).
d392e712 361
c58492d4
KN
362Some atoms like %(align) and %(if) always require a matching %(end).
363We call them "opening atoms" and sometimes denote them as %($open).
364
365When a scripting language specific quoting is in effect, everything
366between a top-level opening atom and its matching %(end) is evaluated
367according to the semantics of the opening atom and only its result
368from the top-level is quoted.
369
9f613ddd
JH
370
371EXAMPLES
372--------
373
1729fa98 374An example directly producing formatted text. Show the most recent
22817b40 3753 tagged commits:
9f613ddd
JH
376
377------------
378#!/bin/sh
379
b1889c36 380git for-each-ref --count=3 --sort='-*authordate' \
9f613ddd
JH
381--format='From: %(*authorname) %(*authoremail)
382Subject: %(*subject)
383Date: %(*authordate)
384Ref: %(*refname)
385
386%(*body)
387' 'refs/tags'
388------------
389
1729fa98
AW
390
391A simple example showing the use of shell eval on the output,
22817b40 392demonstrating the use of --shell. List the prefixes of all heads:
1729fa98
AW
393------------
394#!/bin/sh
395
b1889c36 396git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
1729fa98
AW
397while read entry
398do
399 eval "$entry"
400 echo `dirname $ref`
401done
402------------
403
404
405A bit more elaborate report on tags, demonstrating that the format
22817b40 406may be an entire script:
9f613ddd
JH
407------------
408#!/bin/sh
409
410fmt='
411 r=%(refname)
412 t=%(*objecttype)
413 T=${r#refs/tags/}
414
415 o=%(*objectname)
416 n=%(*authorname)
417 e=%(*authoremail)
418 s=%(*subject)
419 d=%(*authordate)
420 b=%(*body)
421
422 kind=Tag
423 if test "z$t" = z
424 then
425 # could be a lightweight tag
426 t=%(objecttype)
427 kind="Lightweight tag"
428 o=%(objectname)
429 n=%(authorname)
430 e=%(authoremail)
431 s=%(subject)
432 d=%(authordate)
433 b=%(body)
434 fi
435 echo "$kind $T points at a $t object $o"
436 if test "z$t" = zcommit
437 then
438 echo "The commit was authored by $n $e
439at $d, and titled
440
441 $s
442
443Its message reads as:
444"
445 echo "$b" | sed -e "s/^/ /"
446 echo
447 fi
448'
449
b1889c36 450eval=`git for-each-ref --shell --format="$fmt" \
9f613ddd
JH
451 --sort='*objecttype' \
452 --sort=-taggerdate \
453 refs/tags`
454eval "$eval"
455------------
621c39de 456
c58492d4
KN
457
458An example to show the usage of %(if)...%(then)...%(else)...%(end).
459This prefixes the current branch with a star.
460
461------------
462git for-each-ref --format="%(if)%(HEAD)%(then)* %(else) %(end)%(refname:short)" refs/heads/
463------------
464
465
466An example to show the usage of %(if)...%(then)...%(end).
467This prints the authorname, if present.
468
469------------
470git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"
471------------
472
59012fe5
OT
473CAVEATS
474-------
475
476Note that the sizes of objects on disk are reported accurately, but care
477should be taken in drawing conclusions about which refs or objects are
478responsible for disk usage. The size of a packed non-delta object may be
479much larger than the size of objects which delta against it, but the
480choice of which object is the base and which is the delta is arbitrary
481and is subject to change during a repack.
482
483Note also that multiple copies of an object may be present in the object
484database; in this case, it is undefined which copy's size or delta base
485will be reported.
486
415af72b
AL
487NOTES
488-----
489
b59cdffd 490include::ref-reachability-filters.txt[]
415af72b 491
f21e1c5d
MH
492SEE ALSO
493--------
494linkgit:git-show-ref[1]
495
621c39de
AS
496GIT
497---
498Part of the linkgit:git[1] suite