Skip to content

Commit 8d36d49

Browse files
committed
Merge branch 'kn/ref-filter-branch-list' into pu
The code to list branches in "git branch" has been consolidated with the more generic ref-filter API. Rerolled. This also really needs review. * kn/ref-filter-branch-list: branch: implement '--format' option branch: use ref-filter printing APIs branch, tag: use porcelain output ref-filter: allow porcelain to translate messages in the output ref-filter: add `:dir` and `:base` options for ref printing atoms ref-filter: make remote_ref_atom_parser() use refname_atom_parser_internal() ref-filter: introduce symref_atom_parser() and refname_atom_parser() ref-filter: introduce refname_atom_parser_internal() ref-filter: make "%(symref)" atom work with the ':short' modifier ref-filter: add support for %(upstream:track,nobracket) ref-filter: make %(upstream:track) prints "[gone]" for invalid upstreams ref-filter: introduce format_ref_array_item() ref-filter: move get_head_description() from branch.c ref-filter: modify "%(objectname:short)" to take length ref-filter: implement %(if:equals=<string>) and %(if:notequals=<string>) ref-filter: include reference to 'used_atom' within 'atom_value' ref-filter: implement %(if), %(then), and %(else) atoms
2 parents f262dfd + 53c2525 commit 8d36d49

File tree

10 files changed

+728
-295
lines changed

10 files changed

+728
-295
lines changed

Documentation/git-branch.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SYNOPSIS
1212
[--list] [-v [--abbrev=<length> | --no-abbrev]]
1313
[--column[=<options>] | --no-column]
1414
[(--merged | --no-merged | --contains) [<commit>]] [--sort=<key>]
15-
[--points-at <object>] [<pattern>...]
15+
[--points-at <object>] [--format=<format>] [<pattern>...]
1616
'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
1717
'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
1818
'git branch' --unset-upstream [<branchname>]
@@ -246,6 +246,11 @@ start-point is either a local or remote-tracking branch.
246246
--points-at <object>::
247247
Only list branches of the given object.
248248

249+
--format <format>::
250+
A string that interpolates `%(fieldname)` from the object
251+
pointed at by a ref being shown. The format is the same as
252+
that of linkgit:git-for-each-ref[1].
253+
249254
Examples
250255
--------
251256

Documentation/git-for-each-ref.txt

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ refname::
9696
slash-separated path components from the front of the refname
9797
(e.g., `%(refname:strip=2)` turns `refs/tags/foo` into `foo`.
9898
`<N>` must be a positive integer. If a displayed ref has fewer
99-
components than `<N>`, the command aborts with an error.
99+
components than `<N>`, the command aborts with an error. For the base
100+
directory of the ref (i.e. foo in refs/foo/bar/boz) append
101+
`:base`. For the entire directory path append `:dir`.
100102

101103
objecttype::
102104
The type of the object (`blob`, `tree`, `commit`, `tag`).
@@ -107,21 +109,30 @@ objectsize::
107109
objectname::
108110
The object name (aka SHA-1).
109111
For a non-ambiguous abbreviation of the object name append `:short`.
112+
For an abbreviation of the object name with desired length append
113+
`:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
114+
length may be exceeded to ensure unique object names.
115+
110116

111117
upstream::
112118
The name of a local ref which can be considered ``upstream''
113-
from the displayed ref. Respects `:short` in the same way as
114-
`refname` above. Additionally respects `:track` to show
115-
"[ahead N, behind M]" and `:trackshort` to show the terse
116-
version: ">" (ahead), "<" (behind), "<>" (ahead and behind),
117-
or "=" (in sync). Has no effect if the ref does not have
118-
tracking information associated with it.
119+
from the displayed ref. Respects `:short`, `:strip`, `:base`
120+
and `:dir` in the same way as `refname` above. Additionally
121+
respects `:track` to show "[ahead N, behind M]" and
122+
`:trackshort` to show the terse version: ">" (ahead), "<"
123+
(behind), "<>" (ahead and behind), or "=" (in sync). `:track`
124+
also prints "[gone]" whenever unknown upstream ref is
125+
encountered. Append `:track,nobracket` to show tracking
126+
information without brackets (i.e "ahead N, behind M"). Has
127+
no effect if the ref does not have tracking information
128+
associated with it.
119129

120130
push::
121-
The name of a local ref which represents the `@{push}` location
122-
for the displayed ref. Respects `:short`, `:track`, and
123-
`:trackshort` options as `upstream` does. Produces an empty
124-
string if no `@{push}` ref is configured.
131+
The name of a local ref which represents the `@{push}`
132+
location for the displayed ref. Respects `:short`, `:strip`,
133+
`:track`, `:trackshort`, `:base` and `:dir` options as
134+
`upstream` does. Produces an empty string if no `@{push}` ref
135+
is configured.
125136

126137
HEAD::
127138
'*' if HEAD matches current ref (the checked out branch), ' '
@@ -141,10 +152,26 @@ align::
141152
"width=" and/or "position=" prefixes may be omitted, and bare
142153
<width> and <position> used instead. For instance,
143154
`%(align:<width>,<position>)`. If the contents length is more
144-
than the width then no alignment is performed. If used with
145-
`--quote` everything in between %(align:...) and %(end) is
146-
quoted, but if nested then only the topmost level performs
147-
quoting.
155+
than the width then no alignment is performed.
156+
157+
if::
158+
Used as %(if)...%(then)...(%end) or
159+
%(if)...%(then)...%(else)...%(end). If there is an atom with
160+
value or string literal after the %(if) then everything after
161+
the %(then) is printed, else if the %(else) atom is used, then
162+
everything after %(else) is printed. We ignore space when
163+
evaluating the string before %(then), this is useful when we
164+
use the %(HEAD) atom which prints either "*" or " " and we
165+
want to apply the 'if' condition only on the 'HEAD' ref.
166+
Append ":equals=<string>" or ":notequals=<string>" to compare
167+
the value between the %(if:...) and %(then) atoms with the
168+
given string.
169+
170+
symref::
171+
The ref which the given symbolic ref refers to. If not a
172+
symbolic ref, nothing is printed. Respects the `:short`,
173+
`:strip`, `:base` and `:dir` options in the same way as
174+
`refname` above.
148175

149176
In addition to the above, for commit and tag objects, the header
150177
field names (`tree`, `parent`, `object`, `type`, and `tag`) can
@@ -181,6 +208,20 @@ As a special case for the date-type fields, you may specify a format for
181208
the date by adding `:` followed by date format name (see the
182209
values the `--date` option to linkgit:git-rev-list[1] takes).
183210

211+
Some atoms like %(align) and %(if) always require a matching %(end).
212+
We call them "opening atoms" and sometimes denote them as %($open).
213+
214+
When a scripting language specific quoting is in effect (i.e. one of
215+
`--shell`, `--perl`, `--python`, `--tcl` is used), except for opening
216+
atoms, replacement from every %(atom) is quoted when and only when it
217+
appears at the top-level (that is, when it appears outside
218+
%($open)...%(end)).
219+
220+
When a scripting language specific quoting is in effect, everything
221+
between a top-level opening atom and its matching %(end) is evaluated
222+
according to the semantics of the opening atom and its result is
223+
quoted.
224+
184225

185226
EXAMPLES
186227
--------
@@ -268,6 +309,22 @@ eval=`git for-each-ref --shell --format="$fmt" \
268309
eval "$eval"
269310
------------
270311

312+
313+
An example to show the usage of %(if)...%(then)...%(else)...%(end).
314+
This prefixes the current branch with a star.
315+
316+
------------
317+
git for-each-ref --format="%(if)%(HEAD)%(then)* %(else) %(end)%(refname:short)" refs/heads/
318+
------------
319+
320+
321+
An example to show the usage of %(if)...%(then)...%(end).
322+
This prints the authorname, if present.
323+
324+
------------
325+
git for-each-ref --format="%(refname)%(if)%(authorname)%(then) %(color:red)Authored by: %(authorname)%(end)"
326+
------------
327+
271328
SEE ALSO
272329
--------
273330
linkgit:git-show-ref[1]

0 commit comments

Comments
 (0)