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 <sunshine@sunshineco.com> Signed-off-by: Karthik Nayak <Karthik.188@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Karthik Nayak committed
Feb 17, 2016 at 23:36 UTC
395fb8f9f4e13f0ca30b93c7b419c22a4625e5c4
3 files changed
+63
-9
Documentation/git-for-each-ref.txt
+12
-8
@@ -133,14 +133,18 @@ color::
133
134
align::
135
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.
148
149
In addition to the above, for commit and tag objects, the header
150
field names (`tree`, `parent`, `object`, `type`, and `tag`) can
ref-filter.c
+9
-1
@@ -78,7 +78,15 @@ static void align_atom_parser(struct used_atom *atom, const char *arg)
78
const char *s = params.items[i].string;
79
int position;
80
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))
90
;
91
else if ((position = parse_align_position(s)) >= 0)
92
align->position = position;
t/t6302-for-each-ref-filter.sh
+42
@@ -133,6 +133,48 @@ test_expect_success 'right alignment' '
133
test_cmp expect actual
134
'
135
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
+
178
# Individual atoms inside %(align:...) and %(end) must not be quoted.
179
180
test_expect_success 'alignment with format quote' "