ref-filter: implement an `align` atom

Implement an `align` atom which left-, middle-, or right-aligns the content between %(align:...) and %(end). The "align:" is followed by `<width>` and `<position>` in any order separated by a comma, where the `<position>` is either left, right or middle, default being left and `<width>` is the total length of the content with alignment. If the contents length is more than the width then no alignment is performed. e.g. to align a refname atom to the middle with a total width of 40 we can do: --format="%(align:middle,40)%(refname)%(end)". We introduce an `at_end` function for each element of the stack which is to be called when the `end` atom is encountered. Using this we implement end_align_handler() for the `align` atom, this aligns the final strbuf by calling `strbuf_utf8_align()` from utf8.c. Ensure that quote formatting is performed on the whole of %(align:...)...%(end) rather than individual atoms inside. We skip quote formatting for individual atoms when the current stack element is handling an %(align:...) atom and perform quote formatting at the end when we encounter the %(end) atom of the second element of then stack. Add documentation and tests for the same. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Sep 11, 2015 at 20:33 UTC ce59208293f793ae822b5bfdf4040739f1c3ccb7
3 files changed +202 -1
Documentation/git-for-each-ref.txt
+11
@@ -127,6 +127,17 @@ color::
127 Change output color. Followed by `:<colorname>`, where names
128 are described in `color.branch.*`.
129
130 +align::
131 + Left-, middle-, or right-align the content between
132 + %(align:...) and %(end). The "align:" is followed by `<width>`
133 + and `<position>` in any order separated by a comma, where the
134 + `<position>` is either left, right or middle, default being
135 + left and `<width>` is the total length of the content with
136 + alignment. If the contents length is more than the width then
137 + no alignment is performed. If used with '--quote' everything
138 + in between %(align:...) and %(end) is quoted, but if nested
139 + then only the topmost level performs quoting.
140 +
141 In addition to the above, for commit and tag objects, the header
142 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
143 be used to specify the value in the header field.
ref-filter.c
+109 -1
@@ -10,6 +10,7 @@
10 #include "quote.h"
11 #include "ref-filter.h"
12 #include "revision.h"
13 +#include "utf8.h"
14
15 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
16
@@ -53,13 +54,22 @@ static struct {
54 { "flag" },
55 { "HEAD" },
56 { "color" },
57 + { "align" },
58 + { "end" },
59 };
60
61 #define REF_FORMATTING_STATE_INIT { 0, NULL }
62
63 +struct align {
64 + align_type position;
65 + unsigned int width;
66 +};
67 +
68 struct ref_formatting_stack {
69 struct ref_formatting_stack *prev;
70 struct strbuf output;
71 + void (*at_end)(struct ref_formatting_stack *stack);
72 + void *at_end_data;
73 };
74
75 struct ref_formatting_state {
@@ -69,6 +79,9 @@ struct ref_formatting_state {
79
80 struct atom_value {
81 const char *s;
82 + union {
83 + struct align align;
84 + } u;
85 void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
86 unsigned long ul; /* used for sorting when not FIELD_STR */
87 };
@@ -165,7 +178,16 @@ static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
178
179 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
180 {
168 - quote_formatting(&state->stack->output, v->s, state->quote_style);
181 + /*
182 + * Quote formatting is only done when the stack has a single
183 + * element. Otherwise quote formatting is done on the
184 + * element's entire output strbuf when the %(end) atom is
185 + * encountered.
186 + */
187 + if (!state->stack->prev)
188 + quote_formatting(&state->stack->output, v->s, state->quote_style);
189 + else
190 + strbuf_addstr(&state->stack->output, v->s);
191 }
192
193 static void push_stack_element(struct ref_formatting_stack **stack)
@@ -189,6 +211,48 @@ static void pop_stack_element(struct ref_formatting_stack **stack)
211 *stack = prev;
212 }
213
214 +static void end_align_handler(struct ref_formatting_stack *stack)
215 +{
216 + struct align *align = (struct align *)stack->at_end_data;
217 + struct strbuf s = STRBUF_INIT;
218 +
219 + strbuf_utf8_align(&s, align->position, align->width, stack->output.buf);
220 + strbuf_swap(&stack->output, &s);
221 + strbuf_release(&s);
222 +}
223 +
224 +static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
225 +{
226 + struct ref_formatting_stack *new;
227 +
228 + push_stack_element(&state->stack);
229 + new = state->stack;
230 + new->at_end = end_align_handler;
231 + new->at_end_data = &atomv->u.align;
232 +}
233 +
234 +static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
235 +{
236 + struct ref_formatting_stack *current = state->stack;
237 + struct strbuf s = STRBUF_INIT;
238 +
239 + if (!current->at_end)
240 + die(_("format: %%(end) atom used without corresponding atom"));
241 + current->at_end(current);
242 +
243 + /*
244 + * Perform quote formatting when the stack element is that of
245 + * a supporting atom. If nested then perform quote formatting
246 + * only on the topmost supporting atom.
247 + */
248 + if (!state->stack->prev->prev) {
249 + quote_formatting(&s, current->output.buf, state->quote_style);
250 + strbuf_swap(&current->output, &s);
251 + }
252 + strbuf_release(&s);
253 + pop_stack_element(&state->stack);
254 +}
255 +
256 static int match_atom_name(const char *name, const char *atom_name, const char **val)
257 {
258 const char *body;
@@ -773,6 +837,48 @@ static void populate_value(struct ref_array_item *ref)
837 else
838 v->s = " ";
839 continue;
840 + } else if (match_atom_name(name, "align", &valp)) {
841 + struct align *align = &v->u.align;
842 + struct strbuf **s, **to_free;
843 + int width = -1;
844 +
845 + if (!valp)
846 + die(_("expected format: %%(align:<width>,<position>)"));
847 +
848 + /*
849 + * TODO: Implement a function similar to strbuf_split_str()
850 + * which would omit the separator from the end of each value.
851 + */
852 + s = to_free = strbuf_split_str(valp, ',', 0);
853 +
854 + align->position = ALIGN_LEFT;
855 +
856 + while (*s) {
857 + /* Strip trailing comma */
858 + if (s[1])
859 + strbuf_setlen(s[0], s[0]->len - 1);
860 + if (!strtoul_ui(s[0]->buf, 10, (unsigned int *)&width))
861 + ;
862 + else if (!strcmp(s[0]->buf, "left"))
863 + align->position = ALIGN_LEFT;
864 + else if (!strcmp(s[0]->buf, "right"))
865 + align->position = ALIGN_RIGHT;
866 + else if (!strcmp(s[0]->buf, "middle"))
867 + align->position = ALIGN_MIDDLE;
868 + else
869 + die(_("improper format entered align:%s"), s[0]->buf);
870 + s++;
871 + }
872 +
873 + if (width < 0)
874 + die(_("positive width expected with the %%(align) atom"));
875 + align->width = width;
876 + strbuf_list_free(to_free);
877 + v->handler = align_atom_handler;
878 + continue;
879 + } else if (!strcmp(name, "end")) {
880 + v->handler = end_atom_handler;
881 + continue;
882 } else
883 continue;
884
@@ -1347,6 +1453,8 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
1453 resetv.s = color;
1454 append_atom(&resetv, &state);
1455 }
1456 + if (state.stack->prev)
1457 + die(_("format: %%(end) atom missing"));
1458 final_buf = &state.stack->output;
1459 fwrite(final_buf->buf, 1, final_buf->len, stdout);
1460 pop_stack_element(&state.stack);
t/t6302-for-each-ref-filter.sh
+82
@@ -85,4 +85,86 @@ test_expect_success '%(color) must fail' '
85 test_must_fail git for-each-ref --format="%(color)%(refname)"
86 '
87
88 +test_expect_success 'left alignment is default' '
89 + cat >expect <<-\EOF &&
90 + refname is refs/heads/master |refs/heads/master
91 + refname is refs/heads/side |refs/heads/side
92 + refname is refs/odd/spot |refs/odd/spot
93 + refname is refs/tags/double-tag|refs/tags/double-tag
94 + refname is refs/tags/four |refs/tags/four
95 + refname is refs/tags/one |refs/tags/one
96 + refname is refs/tags/signed-tag|refs/tags/signed-tag
97 + refname is refs/tags/three |refs/tags/three
98 + refname is refs/tags/two |refs/tags/two
99 + EOF
100 + git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
101 + test_cmp expect actual
102 +'
103 +
104 +test_expect_success 'middle alignment' '
105 + cat >expect <<-\EOF &&
106 + | refname is refs/heads/master |refs/heads/master
107 + | refname is refs/heads/side |refs/heads/side
108 + | refname is refs/odd/spot |refs/odd/spot
109 + |refname is refs/tags/double-tag|refs/tags/double-tag
110 + | refname is refs/tags/four |refs/tags/four
111 + | refname is refs/tags/one |refs/tags/one
112 + |refname is refs/tags/signed-tag|refs/tags/signed-tag
113 + | refname is refs/tags/three |refs/tags/three
114 + | refname is refs/tags/two |refs/tags/two
115 + EOF
116 + git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
117 + test_cmp expect actual
118 +'
119 +
120 +test_expect_success 'right alignment' '
121 + cat >expect <<-\EOF &&
122 + | refname is refs/heads/master|refs/heads/master
123 + | refname is refs/heads/side|refs/heads/side
124 + | refname is refs/odd/spot|refs/odd/spot
125 + |refname is refs/tags/double-tag|refs/tags/double-tag
126 + | refname is refs/tags/four|refs/tags/four
127 + | refname is refs/tags/one|refs/tags/one
128 + |refname is refs/tags/signed-tag|refs/tags/signed-tag
129 + | refname is refs/tags/three|refs/tags/three
130 + | refname is refs/tags/two|refs/tags/two
131 + EOF
132 + git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
133 + test_cmp expect actual
134 +'
135 +
136 +# Individual atoms inside %(align:...) and %(end) must not be quoted.
137 +
138 +test_expect_success 'alignment with format quote' "
139 + cat >expect <<-\EOF &&
140 + |' '\''master| A U Thor'\'' '|
141 + |' '\''side| A U Thor'\'' '|
142 + |' '\''odd/spot| A U Thor'\'' '|
143 + |' '\''double-tag| '\'' '|
144 + |' '\''four| A U Thor'\'' '|
145 + |' '\''one| A U Thor'\'' '|
146 + |' '\''signed-tag| '\'' '|
147 + |' '\''three| A U Thor'\'' '|
148 + |' '\''two| A U Thor'\'' '|
149 + EOF
150 + git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
151 + test_cmp expect actual
152 +"
153 +
154 +test_expect_success 'nested alignment with quote formatting' "
155 + cat >expect <<-\EOF &&
156 + |' master '|
157 + |' side '|
158 + |' odd/spot '|
159 + |' double-tag '|
160 + |' four '|
161 + |' one '|
162 + |' signed-tag '|
163 + |' three '|
164 + |' two '|
165 + EOF
166 + git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
167 + test_cmp expect actual
168 +"
169 +
170 test_done