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