Skip to content

Commit 395fb8f

Browse files
KarthikNayakgitster
authored andcommitted
ref-filter: align: introduce long-form syntax
Introduce optional prefixes "width=" and "position=" for the align atom so that the atom can be used as "%(align:width=<width>,position=<position>)". Add Documentation and tests for the same. Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Reviewed-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bd881d commit 395fb8f

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,18 @@ color::
133133

134134
align::
135135
Left-, middle-, or right-align the content between
136-
%(align:...) and %(end). The "align:" is followed by `<width>`
137-
and `<position>` in any order separated by a comma, where the
138-
`<position>` is either left, right or middle, default being
139-
left and `<width>` is the total length of the content with
140-
alignment. If the contents length is more than the width then
141-
no alignment is performed. If used with '--quote' everything
142-
in between %(align:...) and %(end) is quoted, but if nested
143-
then only the topmost level performs quoting.
136+
%(align:...) and %(end). The "align:" is followed by
137+
`width=<width>` and `position=<position>` in any order
138+
separated by a comma, where the `<position>` is either left,
139+
right or middle, default being left and `<width>` is the total
140+
length of the content with alignment. For brevity, the
141+
"width=" and/or "position=" prefixes may be omitted, and bare
142+
<width> and <position> used instead. For instance,
143+
`%(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.
144148

145149
In addition to the above, for commit and tag objects, the header
146150
field names (`tree`, `parent`, `object`, `type`, and `tag`) can

ref-filter.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ static void align_atom_parser(struct used_atom *atom, const char *arg)
7878
const char *s = params.items[i].string;
7979
int position;
8080

81-
if (!strtoul_ui(s, 10, &width))
81+
if (skip_prefix(s, "position=", &s)) {
82+
position = parse_align_position(s);
83+
if (position < 0)
84+
die(_("unrecognized position:%s"), s);
85+
align->position = position;
86+
} else if (skip_prefix(s, "width=", &s)) {
87+
if (strtoul_ui(s, 10, &width))
88+
die(_("unrecognized width:%s"), s);
89+
} else if (!strtoul_ui(s, 10, &width))
8290
;
8391
else if ((position = parse_align_position(s)) >= 0)
8492
align->position = position;

t/t6302-for-each-ref-filter.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,48 @@ test_expect_success 'right alignment' '
133133
test_cmp expect actual
134134
'
135135

136+
cat >expect <<-\EOF
137+
| refname is refs/heads/master |refs/heads/master
138+
| refname is refs/heads/side |refs/heads/side
139+
| refname is refs/odd/spot |refs/odd/spot
140+
| refname is refs/tags/double-tag |refs/tags/double-tag
141+
| refname is refs/tags/four |refs/tags/four
142+
| refname is refs/tags/one |refs/tags/one
143+
| refname is refs/tags/signed-tag |refs/tags/signed-tag
144+
| refname is refs/tags/three |refs/tags/three
145+
| refname is refs/tags/two |refs/tags/two
146+
EOF
147+
148+
test_align_permutations() {
149+
while read -r option
150+
do
151+
test_expect_success "align:$option" '
152+
git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
153+
test_cmp expect actual
154+
'
155+
done
156+
}
157+
158+
test_align_permutations <<-\EOF
159+
middle,42
160+
42,middle
161+
position=middle,42
162+
42,position=middle
163+
middle,width=42
164+
width=42,middle
165+
position=middle,width=42
166+
width=42,position=middle
167+
EOF
168+
169+
# Last one wins (silently) when multiple arguments of the same type are given
170+
171+
test_align_permutations <<-\EOF
172+
32,width=42,middle
173+
width=30,42,middle
174+
width=42,position=right,middle
175+
42,right,position=middle
176+
EOF
177+
136178
# Individual atoms inside %(align:...) and %(end) must not be quoted.
137179

138180
test_expect_success 'alignment with format quote' "

0 commit comments

Comments
 (0)