]>
Commit | Line | Data |
---|---|---|
9f613ddd JH |
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 | -------- | |
97925fde | 10 | [verse] |
b1889c36 | 11 | 'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl] |
0adda936 | 12 | [(--sort=<key>)...] [--format=<format>] [<pattern>...] |
3233d51d KD |
13 | [--points-at=<object>] |
14 | (--merged[=<object>] | --no-merged[=<object>]) | |
15 | [--contains[=<object>]] [--no-contains[=<object>]] | |
9f613ddd JH |
16 | |
17 | DESCRIPTION | |
18 | ----------- | |
19 | ||
20 | Iterate over all refs that match `<pattern>` and show them | |
21 | according to the given `<format>`, after sorting them according | |
d4040e0a | 22 | to the given set of `<key>`. If `<count>` is given, stop after |
23bfbb81 | 23 | showing that many refs. The interpolated values in `<format>` |
9f613ddd | 24 | can optionally be quoted as string literals in the specified |
1729fa98 | 25 | host language allowing their direct evaluation in that language. |
9f613ddd JH |
26 | |
27 | OPTIONS | |
28 | ------- | |
f7a32dd9 KD |
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>:: | |
9f613ddd JH |
36 | By default the command shows all refs that match |
37 | `<pattern>`. This option makes it stop after showing | |
38 | that many refs. | |
39 | ||
f7a32dd9 | 40 | --sort=<key>:: |
9f613ddd JH |
41 | A field name to sort on. Prefix `-` to sort in |
42 | descending order of the value. When unspecified, | |
c0f6dc9b LW |
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. | |
9f613ddd | 46 | |
f7a32dd9 | 47 | --format=<format>:: |
e4933cee MG |
48 | A string that interpolates `%(fieldname)` from a ref being shown |
49 | and the object it points at. If `fieldname` | |
9f613ddd | 50 | is prefixed with an asterisk (`*`) and the ref points |
794b7e16 MG |
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 | |
ba7545ad JN |
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). | |
9f613ddd | 59 | |
dd61cc1c | 60 | --color[=<when>]:: |
0c88bf50 JK |
61 | Respect any colors specified in the `--format` option. The |
62 | `<when>` field must be one of `always`, `never`, or `auto` (if | |
63 | `<when>` is absent, behave as if `always` was given). | |
64 | ||
3240240f SB |
65 | --shell:: |
66 | --perl:: | |
67 | --python:: | |
68 | --tcl:: | |
9f613ddd JH |
69 | If given, strings that substitute `%(fieldname)` |
70 | placeholders are quoted as string literals suitable for | |
71 | the specified host language. This is meant to produce | |
72 | a scriptlet that can directly be `eval`ed. | |
73 | ||
3233d51d | 74 | --points-at=<object>:: |
d325406e | 75 | Only list refs which points at the given object. |
9f613ddd | 76 | |
3233d51d | 77 | --merged[=<object>]:: |
7c328348 | 78 | Only list refs whose tips are reachable from the |
17d6c744 ÆAB |
79 | specified commit (HEAD if not specified), |
80 | incompatible with `--no-merged`. | |
7c328348 | 81 | |
3233d51d | 82 | --no-merged[=<object>]:: |
7c328348 | 83 | Only list refs whose tips are not reachable from the |
17d6c744 ÆAB |
84 | specified commit (HEAD if not specified), |
85 | incompatible with `--merged`. | |
7c328348 | 86 | |
3233d51d | 87 | --contains[=<object>]:: |
8b5a3e98 | 88 | Only list refs which contain the specified commit (HEAD if not |
4a71109a KN |
89 | specified). |
90 | ||
3233d51d | 91 | --no-contains[=<object>]:: |
ac3f5a34 ÆAB |
92 | Only list refs which don't contain the specified commit (HEAD |
93 | if not specified). | |
94 | ||
3bb16a8b NTND |
95 | --ignore-case:: |
96 | Sorting and filtering refs are case insensitive. | |
97 | ||
9f613ddd JH |
98 | FIELD NAMES |
99 | ----------- | |
100 | ||
101 | Various values from structured fields in referenced objects can | |
102 | be used to interpolate into the resulting output, or as sort | |
103 | keys. | |
104 | ||
105 | For all objects, the following names can be used: | |
106 | ||
107 | refname:: | |
69057cf3 | 108 | The name of the ref (the part after $GIT_DIR/). |
7d66f21a | 109 | For a non-ambiguous short name of the ref append `:short`. |
2bb98169 | 110 | The option core.warnAmbiguousRefs is used to select the strict |
1a34728e KN |
111 | abbreviation mode. If `lstrip=<N>` (`rstrip=<N>`) is appended, strips `<N>` |
112 | slash-separated path components from the front (back) of the refname | |
113 | (e.g. `%(refname:lstrip=2)` turns `refs/tags/foo` into `foo` and | |
114 | `%(refname:rstrip=2)` turns `refs/tags/foo` into `refs`). | |
1a0ca5e3 | 115 | If `<N>` is a negative number, strip as many path components as |
1a34728e | 116 | necessary from the specified end to leave `-<N>` path components |
1a0ca5e3 | 117 | (e.g. `%(refname:lstrip=-2)` turns |
1a34728e KN |
118 | `refs/tags/foo` into `tags/foo` and `%(refname:rstrip=-1)` |
119 | turns `refs/tags/foo` into `refs`). When the ref does not have | |
1a0ca5e3 KN |
120 | enough components, the result becomes an empty string if |
121 | stripping with positive <N>, or it becomes the full refname if | |
122 | stripping with negative <N>. Neither is an error. | |
44a6b6ce | 123 | + |
c30d4f1b | 124 | `strip` can be used as a synonym to `lstrip`. |
9f613ddd JH |
125 | |
126 | objecttype:: | |
127 | The type of the object (`blob`, `tree`, `commit`, `tag`). | |
128 | ||
129 | objectsize:: | |
0b444cdb | 130 | The size of the object (the same as 'git cat-file -s' reports). |
59012fe5 OT |
131 | Append `:disk` to get the size, in bytes, that the object takes up on |
132 | disk. See the note about on-disk sizes in the `CAVEATS` section below. | |
9f613ddd JH |
133 | objectname:: |
134 | The object name (aka SHA-1). | |
67687fea | 135 | For a non-ambiguous abbreviation of the object name append `:short`. |
42d0eb05 KN |
136 | For an abbreviation of the object name with desired length append |
137 | `:short=<length>`, where the minimum length is MINIMUM_ABBREV. The | |
138 | length may be exceeded to ensure unique object names. | |
59012fe5 OT |
139 | deltabase:: |
140 | This expands to the object name of the delta base for the | |
141 | given object, if it is stored as a delta. Otherwise it | |
142 | expands to the null object name (all zeroes). | |
9f613ddd | 143 | |
8cae19d9 JK |
144 | upstream:: |
145 | The name of a local ref which can be considered ``upstream'' | |
1a34728e KN |
146 | from the displayed ref. Respects `:short`, `:lstrip` and |
147 | `:rstrip` in the same way as `refname` above. Additionally | |
148 | respects `:track` to show "[ahead N, behind M]" and | |
149 | `:trackshort` to show the terse version: ">" (ahead), "<" | |
150 | (behind), "<>" (ahead and behind), or "=" (in sync). `:track` | |
151 | also prints "[gone]" whenever unknown upstream ref is | |
152 | encountered. Append `:track,nobracket` to show tracking | |
cc72385f JS |
153 | information without brackets (i.e "ahead N, behind M"). |
154 | + | |
9700fae5 W |
155 | For any remote-tracking branch `%(upstream)`, `%(upstream:remotename)` |
156 | and `%(upstream:remoteref)` refer to the name of the remote and the | |
157 | name of the tracked remote ref, respectively. In other words, the | |
158 | remote-tracking branch can be updated explicitly and individually by | |
159 | using the refspec `%(upstream:remoteref):%(upstream)` to fetch from | |
160 | `%(upstream:remotename)`. | |
cc72385f JS |
161 | + |
162 | Has no effect if the ref does not have tracking information associated | |
163 | with it. All the options apart from `nobracket` are mutually exclusive, | |
164 | but if used together the last option is selected. | |
8cae19d9 | 165 | |
29bc8850 | 166 | push:: |
3ba308cb | 167 | The name of a local ref which represents the `@{push}` |
17938f17 | 168 | location for the displayed ref. Respects `:short`, `:lstrip`, |
9700fae5 W |
169 | `:rstrip`, `:track`, `:trackshort`, `:remotename`, and `:remoteref` |
170 | options as `upstream` does. Produces an empty string if no `@{push}` | |
171 | ref is configured. | |
29bc8850 | 172 | |
7a48b832 RR |
173 | HEAD:: |
174 | '*' if HEAD matches current ref (the checked out branch), ' ' | |
175 | otherwise. | |
176 | ||
fddb74c9 | 177 | color:: |
5d3d0681 JK |
178 | Change output color. Followed by `:<colorname>`, where color |
179 | names are described under Values in the "CONFIGURATION FILE" | |
180 | section of linkgit:git-config[1]. For example, | |
181 | `%(color:bold red)`. | |
fddb74c9 | 182 | |
ce592082 KN |
183 | align:: |
184 | Left-, middle-, or right-align the content between | |
395fb8f9 KN |
185 | %(align:...) and %(end). The "align:" is followed by |
186 | `width=<width>` and `position=<position>` in any order | |
187 | separated by a comma, where the `<position>` is either left, | |
188 | right or middle, default being left and `<width>` is the total | |
189 | length of the content with alignment. For brevity, the | |
190 | "width=" and/or "position=" prefixes may be omitted, and bare | |
191 | <width> and <position> used instead. For instance, | |
192 | `%(align:<width>,<position>)`. If the contents length is more | |
193 | than the width then no alignment is performed. If used with | |
bcf9626a | 194 | `--quote` everything in between %(align:...) and %(end) is |
395fb8f9 KN |
195 | quoted, but if nested then only the topmost level performs |
196 | quoting. | |
ce592082 | 197 | |
c58492d4 KN |
198 | if:: |
199 | Used as %(if)...%(then)...%(end) or | |
200 | %(if)...%(then)...%(else)...%(end). If there is an atom with | |
201 | value or string literal after the %(if) then everything after | |
202 | the %(then) is printed, else if the %(else) atom is used, then | |
203 | everything after %(else) is printed. We ignore space when | |
204 | evaluating the string before %(then), this is useful when we | |
205 | use the %(HEAD) atom which prints either "*" or " " and we | |
206 | want to apply the 'if' condition only on the 'HEAD' ref. | |
4f3e3b37 KN |
207 | Append ":equals=<string>" or ":notequals=<string>" to compare |
208 | the value between the %(if:...) and %(then) atoms with the | |
209 | given string. | |
c58492d4 | 210 | |
a7984101 KN |
211 | symref:: |
212 | The ref which the given symbolic ref refers to. If not a | |
1a34728e KN |
213 | symbolic ref, nothing is printed. Respects the `:short`, |
214 | `:lstrip` and `:rstrip` options in the same way as `refname` | |
215 | above. | |
a7984101 | 216 | |
9f613ddd JH |
217 | In addition to the above, for commit and tag objects, the header |
218 | field names (`tree`, `parent`, `object`, `type`, and `tag`) can | |
219 | be used to specify the value in the header field. | |
220 | ||
e914ef0d EW |
221 | For commit and tag objects, the special `creatordate` and `creator` |
222 | fields will correspond to the appropriate date or name-email-date tuple | |
223 | from the `committer` or `tagger` fields depending on the object type. | |
224 | These are intended for working on a mix of annotated and lightweight tags. | |
225 | ||
9f613ddd JH |
226 | Fields that have name-email-date tuple as its value (`author`, |
227 | `committer`, and `tagger`) can be suffixed with `name`, `email`, | |
228 | and `date` to extract the named component. | |
229 | ||
e2b23972 | 230 | The complete message in a commit and tag object is `contents`. |
52ffe995 JW |
231 | Its first line is `contents:subject`, where subject is the concatenation |
232 | of all lines of the commit message up to the first blank line. The next | |
ced1f08b | 233 | line is `contents:body`, where body is all of the lines after the first |
1bb38e5a KN |
234 | blank line. The optional GPG signature is `contents:signature`. The |
235 | first `N` lines of the message is obtained using `contents:lines=N`. | |
b1d31c89 | 236 | Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1] |
ced1f08b | 237 | are obtained as `trailers` (or by using the historical alias |
67a20a00 TB |
238 | `contents:trailers`). Non-trailer lines from the trailer block can be omitted |
239 | with `trailers:only`. Whitespace-continuations can be removed from trailers so | |
240 | that each trailer appears on a line by itself with its full content with | |
241 | `trailers:unfold`. Both can be used together as `trailers:unfold,only`. | |
9f613ddd | 242 | |
e914ef0d EW |
243 | For sorting purposes, fields with numeric values sort in numeric order |
244 | (`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`). | |
9f613ddd JH |
245 | All other fields are used to sort in their byte-value order. |
246 | ||
90c00408 KN |
247 | There is also an option to sort by versions, this can be done by using |
248 | the fieldname `version:refname` or its alias `v:refname`. | |
249 | ||
9f613ddd JH |
250 | In any case, a field name that refers to a field inapplicable to |
251 | the object referred by the ref does not cause an error. It | |
252 | returns an empty string instead. | |
253 | ||
d392e712 | 254 | As a special case for the date-type fields, you may specify a format for |
8f50d263 | 255 | the date by adding `:` followed by date format name (see the |
1cca17df | 256 | values the `--date` option to linkgit:git-rev-list[1] takes). |
d392e712 | 257 | |
c58492d4 KN |
258 | Some atoms like %(align) and %(if) always require a matching %(end). |
259 | We call them "opening atoms" and sometimes denote them as %($open). | |
260 | ||
261 | When a scripting language specific quoting is in effect, everything | |
262 | between a top-level opening atom and its matching %(end) is evaluated | |
263 | according to the semantics of the opening atom and only its result | |
264 | from the top-level is quoted. | |
265 | ||
9f613ddd JH |
266 | |
267 | EXAMPLES | |
268 | -------- | |
269 | ||
1729fa98 | 270 | An example directly producing formatted text. Show the most recent |
22817b40 | 271 | 3 tagged commits: |
9f613ddd JH |
272 | |
273 | ------------ | |
274 | #!/bin/sh | |
275 | ||
b1889c36 | 276 | git for-each-ref --count=3 --sort='-*authordate' \ |
9f613ddd JH |
277 | --format='From: %(*authorname) %(*authoremail) |
278 | Subject: %(*subject) | |
279 | Date: %(*authordate) | |
280 | Ref: %(*refname) | |
281 | ||
282 | %(*body) | |
283 | ' 'refs/tags' | |
284 | ------------ | |
285 | ||
1729fa98 AW |
286 | |
287 | A simple example showing the use of shell eval on the output, | |
22817b40 | 288 | demonstrating the use of --shell. List the prefixes of all heads: |
1729fa98 AW |
289 | ------------ |
290 | #!/bin/sh | |
291 | ||
b1889c36 | 292 | git for-each-ref --shell --format="ref=%(refname)" refs/heads | \ |
1729fa98 AW |
293 | while read entry |
294 | do | |
295 | eval "$entry" | |
296 | echo `dirname $ref` | |
297 | done | |
298 | ------------ | |
299 | ||
300 | ||
301 | A bit more elaborate report on tags, demonstrating that the format | |
22817b40 | 302 | may be an entire script: |
9f613ddd JH |
303 | ------------ |
304 | #!/bin/sh | |
305 | ||
306 | fmt=' | |
307 | r=%(refname) | |
308 | t=%(*objecttype) | |
309 | T=${r#refs/tags/} | |
310 | ||
311 | o=%(*objectname) | |
312 | n=%(*authorname) | |
313 | e=%(*authoremail) | |
314 | s=%(*subject) | |
315 | d=%(*authordate) | |
316 | b=%(*body) | |
317 | ||
318 | kind=Tag | |
319 | if test "z$t" = z | |
320 | then | |
321 | # could be a lightweight tag | |
322 | t=%(objecttype) | |
323 | kind="Lightweight tag" | |
324 | o=%(objectname) | |
325 | n=%(authorname) | |
326 | e=%(authoremail) | |
327 | s=%(subject) | |
328 | d=%(authordate) | |
329 | b=%(body) | |
330 | fi | |
331 | echo "$kind $T points at a $t object $o" | |
332 | if test "z$t" = zcommit | |
333 | then | |
334 | echo "The commit was authored by $n $e | |
335 | at $d, and titled | |
336 | ||
337 | $s | |
338 | ||
339 | Its message reads as: | |
340 | " | |
341 | echo "$b" | sed -e "s/^/ /" | |
342 | echo | |
343 | fi | |
344 | ' | |
345 | ||
b1889c36 | 346 | eval=`git for-each-ref --shell --format="$fmt" \ |
9f613ddd JH |
347 | --sort='*objecttype' \ |
348 | --sort=-taggerdate \ | |
349 | refs/tags` | |
350 | eval "$eval" | |
351 | ------------ | |
621c39de | 352 | |
c58492d4 KN |
353 | |
354 | An example to show the usage of %(if)...%(then)...%(else)...%(end). | |
355 | This prefixes the current branch with a star. | |
356 | ||
357 | ------------ | |
358 | git for-each-ref --format="%(if)%(HEAD)%(then)* %(else) %(end)%(refname:short)" refs/heads/ | |
359 | ------------ | |
360 | ||
361 | ||
362 | An example to show the usage of %(if)...%(then)...%(end). | |
363 | This prints the authorname, if present. | |
364 | ||
365 | ------------ | |
366 | git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)" | |
367 | ------------ | |
368 | ||
59012fe5 OT |
369 | CAVEATS |
370 | ------- | |
371 | ||
372 | Note that the sizes of objects on disk are reported accurately, but care | |
373 | should be taken in drawing conclusions about which refs or objects are | |
374 | responsible for disk usage. The size of a packed non-delta object may be | |
375 | much larger than the size of objects which delta against it, but the | |
376 | choice of which object is the base and which is the delta is arbitrary | |
377 | and is subject to change during a repack. | |
378 | ||
379 | Note also that multiple copies of an object may be present in the object | |
380 | database; in this case, it is undefined which copy's size or delta base | |
381 | will be reported. | |
382 | ||
f21e1c5d MH |
383 | SEE ALSO |
384 | -------- | |
385 | linkgit:git-show-ref[1] | |
386 | ||
621c39de AS |
387 | GIT |
388 | --- | |
389 | Part of the linkgit:git[1] suite |