]> git.ipfire.org Git - thirdparty/git.git/blame - ref-filter.c
ref-filter: introduce refname_atom_parser()
[thirdparty/git.git] / ref-filter.c
CommitLineData
c95b7585
KN
1#include "builtin.h"
2#include "cache.h"
3#include "parse-options.h"
4#include "refs.h"
5#include "wildmatch.h"
6#include "commit.h"
7#include "remote.h"
8#include "color.h"
9#include "tag.h"
10#include "quote.h"
11#include "ref-filter.h"
35257aa0 12#include "revision.h"
ce592082 13#include "utf8.h"
90c00408
KN
14#include "git-compat-util.h"
15#include "version.h"
b1d31c89 16#include "trailer.h"
d4919bb2 17#include "wt-status.h"
c95b7585
KN
18
19typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
4f3e3b37 20typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
c95b7585 21
5bd881d9
KN
22struct align {
23 align_type position;
24 unsigned int width;
25};
26
c58492d4 27struct if_then_else {
4f3e3b37
KN
28 cmp_status cmp_status;
29 const char *str;
c58492d4
KN
30 unsigned int then_atom_seen : 1,
31 else_atom_seen : 1,
32 condition_satisfied : 1;
33};
34
b180e6fe
KN
35struct refname_atom {
36 enum { R_NORMAL, R_SHORT, R_STRIP } option;
37 unsigned int strip;
38};
39
50cd83dc
KN
40/*
41 * An atom is a valid field atom listed below, possibly prefixed with
42 * a "*" to denote deref_tag().
43 *
44 * We parse given format string and sort specifiers, and make a list
45 * of properties that we need to extract out of objects. ref_array_item
46 * structure will hold an array of values extracted that can be
47 * indexed with the "atom number", which is an index into this
48 * array.
49 */
b072add7
KN
50static struct used_atom {
51 const char *name;
52 cmp_type type;
fd935cc7
KN
53 union {
54 char color[COLOR_MAXLEN];
5bd881d9 55 struct align align;
7743fcca
KN
56 struct {
57 enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT } option;
58 unsigned int nobracket : 1;
59 } remote_ref;
452db397 60 struct {
b1d31c89 61 enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
452db397
KN
62 unsigned int nlines;
63 } contents;
4f3e3b37
KN
64 struct {
65 cmp_status cmp_status;
66 const char *str;
67 } if_then_else;
42d0eb05
KN
68 struct {
69 enum { O_FULL, O_LENGTH, O_SHORT } option;
70 unsigned int length;
71 } objectname;
b180e6fe 72 struct refname_atom refname;
fd935cc7 73 } u;
b072add7 74} *used_atom;
50cd83dc
KN
75static int used_atom_cnt, need_tagged, need_symref;
76static int need_color_reset_at_eol;
77
fd935cc7
KN
78static void color_atom_parser(struct used_atom *atom, const char *color_value)
79{
80 if (!color_value)
81 die(_("expected format: %%(color:<color>)"));
82 if (color_parse(color_value, atom->u.color) < 0)
83 die(_("unrecognized color: %%(color:%s)"), color_value);
84}
85
b180e6fe
KN
86static void refname_atom_parser_internal(struct refname_atom *atom,
87 const char *arg, const char *name)
88{
89 if (!arg)
90 atom->option = R_NORMAL;
91 else if (!strcmp(arg, "short"))
92 atom->option = R_SHORT;
93 else if (skip_prefix(arg, "strip=", &arg)) {
94 atom->option = R_STRIP;
95 if (strtoul_ui(arg, 10, &atom->strip) || atom->strip <= 0)
96 die(_("positive value expected refname:strip=%s"), arg);
97 } else
98 die(_("unrecognized %%(%s) argument: %s"), name, arg);
99}
100
5339bdad
KN
101static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
102{
7743fcca
KN
103 struct string_list params = STRING_LIST_INIT_DUP;
104 int i;
105
106 if (!arg) {
107 atom->u.remote_ref.option = RR_NORMAL;
108 return;
109 }
110
111 atom->u.remote_ref.nobracket = 0;
112 string_list_split(&params, arg, ',', -1);
113
114 for (i = 0; i < params.nr; i++) {
115 const char *s = params.items[i].string;
116
117 if (!strcmp(s, "short"))
118 atom->u.remote_ref.option = RR_SHORTEN;
119 else if (!strcmp(s, "track"))
120 atom->u.remote_ref.option = RR_TRACK;
121 else if (!strcmp(s, "trackshort"))
122 atom->u.remote_ref.option = RR_TRACKSHORT;
123 else if (!strcmp(s, "nobracket"))
124 atom->u.remote_ref.nobracket = 1;
125 else
126 die(_("unrecognized format: %%(%s)"), atom->name);
127 }
128
129 string_list_clear(&params, 0);
5339bdad
KN
130}
131
452db397
KN
132static void body_atom_parser(struct used_atom *atom, const char *arg)
133{
134 if (arg)
1823c619 135 die(_("%%(body) does not take arguments"));
452db397
KN
136 atom->u.contents.option = C_BODY_DEP;
137}
138
139static void subject_atom_parser(struct used_atom *atom, const char *arg)
140{
141 if (arg)
1823c619 142 die(_("%%(subject) does not take arguments"));
452db397
KN
143 atom->u.contents.option = C_SUB;
144}
145
b1d31c89
JK
146static void trailers_atom_parser(struct used_atom *atom, const char *arg)
147{
148 if (arg)
149 die(_("%%(trailers) does not take arguments"));
150 atom->u.contents.option = C_TRAILERS;
151}
152
452db397
KN
153static void contents_atom_parser(struct used_atom *atom, const char *arg)
154{
155 if (!arg)
156 atom->u.contents.option = C_BARE;
157 else if (!strcmp(arg, "body"))
158 atom->u.contents.option = C_BODY;
159 else if (!strcmp(arg, "signature"))
160 atom->u.contents.option = C_SIG;
161 else if (!strcmp(arg, "subject"))
162 atom->u.contents.option = C_SUB;
b1d31c89
JK
163 else if (!strcmp(arg, "trailers"))
164 atom->u.contents.option = C_TRAILERS;
452db397
KN
165 else if (skip_prefix(arg, "lines=", &arg)) {
166 atom->u.contents.option = C_LINES;
167 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
168 die(_("positive value expected contents:lines=%s"), arg);
169 } else
170 die(_("unrecognized %%(contents) argument: %s"), arg);
171}
172
fe63c4d1
KN
173static void objectname_atom_parser(struct used_atom *atom, const char *arg)
174{
175 if (!arg)
42d0eb05 176 atom->u.objectname.option = O_FULL;
fe63c4d1 177 else if (!strcmp(arg, "short"))
42d0eb05
KN
178 atom->u.objectname.option = O_SHORT;
179 else if (skip_prefix(arg, "short=", &arg)) {
180 atom->u.objectname.option = O_LENGTH;
181 if (strtoul_ui(arg, 10, &atom->u.objectname.length) ||
182 atom->u.objectname.length == 0)
183 die(_("positive value expected objectname:short=%s"), arg);
184 if (atom->u.objectname.length < MINIMUM_ABBREV)
185 atom->u.objectname.length = MINIMUM_ABBREV;
186 } else
fe63c4d1
KN
187 die(_("unrecognized %%(objectname) argument: %s"), arg);
188}
189
a7984101
KN
190static void refname_atom_parser(struct used_atom *atom, const char *arg)
191{
192 return refname_atom_parser_internal(&atom->u.refname, arg, atom->name);
193}
194
25a8d79e
KN
195static align_type parse_align_position(const char *s)
196{
197 if (!strcmp(s, "right"))
198 return ALIGN_RIGHT;
199 else if (!strcmp(s, "middle"))
200 return ALIGN_MIDDLE;
201 else if (!strcmp(s, "left"))
202 return ALIGN_LEFT;
203 return -1;
204}
205
5bd881d9
KN
206static void align_atom_parser(struct used_atom *atom, const char *arg)
207{
208 struct align *align = &atom->u.align;
209 struct string_list params = STRING_LIST_INIT_DUP;
210 int i;
211 unsigned int width = ~0U;
212
213 if (!arg)
214 die(_("expected format: %%(align:<width>,<position>)"));
215
216 align->position = ALIGN_LEFT;
217
218 string_list_split(&params, arg, ',', -1);
219 for (i = 0; i < params.nr; i++) {
220 const char *s = params.items[i].string;
221 int position;
222
395fb8f9
KN
223 if (skip_prefix(s, "position=", &s)) {
224 position = parse_align_position(s);
225 if (position < 0)
226 die(_("unrecognized position:%s"), s);
227 align->position = position;
228 } else if (skip_prefix(s, "width=", &s)) {
229 if (strtoul_ui(s, 10, &width))
230 die(_("unrecognized width:%s"), s);
231 } else if (!strtoul_ui(s, 10, &width))
5bd881d9
KN
232 ;
233 else if ((position = parse_align_position(s)) >= 0)
234 align->position = position;
235 else
236 die(_("unrecognized %%(align) argument: %s"), s);
237 }
238
239 if (width == ~0U)
240 die(_("positive width expected with the %%(align) atom"));
241 align->width = width;
242 string_list_clear(&params, 0);
243}
244
4f3e3b37
KN
245static void if_atom_parser(struct used_atom *atom, const char *arg)
246{
247 if (!arg) {
248 atom->u.if_then_else.cmp_status = COMPARE_NONE;
249 return;
250 } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
251 atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
252 } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
253 atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
254 } else {
255 die(_("unrecognized %%(if) argument: %s"), arg);
256 }
257}
258
259
c95b7585
KN
260static struct {
261 const char *name;
262 cmp_type cmp_type;
4de707ea 263 void (*parser)(struct used_atom *atom, const char *arg);
c95b7585 264} valid_atom[] = {
a7984101 265 { "refname" , FIELD_STR, refname_atom_parser },
c95b7585
KN
266 { "objecttype" },
267 { "objectsize", FIELD_ULONG },
fe63c4d1 268 { "objectname", FIELD_STR, objectname_atom_parser },
c95b7585
KN
269 { "tree" },
270 { "parent" },
271 { "numparent", FIELD_ULONG },
272 { "object" },
273 { "type" },
274 { "tag" },
275 { "author" },
276 { "authorname" },
277 { "authoremail" },
278 { "authordate", FIELD_TIME },
279 { "committer" },
280 { "committername" },
281 { "committeremail" },
282 { "committerdate", FIELD_TIME },
283 { "tagger" },
284 { "taggername" },
285 { "taggeremail" },
286 { "taggerdate", FIELD_TIME },
287 { "creator" },
288 { "creatordate", FIELD_TIME },
452db397
KN
289 { "subject", FIELD_STR, subject_atom_parser },
290 { "body", FIELD_STR, body_atom_parser },
b1d31c89 291 { "trailers", FIELD_STR, trailers_atom_parser },
452db397 292 { "contents", FIELD_STR, contents_atom_parser },
5339bdad
KN
293 { "upstream", FIELD_STR, remote_ref_atom_parser },
294 { "push", FIELD_STR, remote_ref_atom_parser },
a7984101 295 { "symref", FIELD_STR, refname_atom_parser },
c95b7585
KN
296 { "flag" },
297 { "HEAD" },
fd935cc7 298 { "color", FIELD_STR, color_atom_parser },
5bd881d9 299 { "align", FIELD_STR, align_atom_parser },
ce592082 300 { "end" },
4f3e3b37 301 { "if", FIELD_STR, if_atom_parser },
c58492d4
KN
302 { "then" },
303 { "else" },
c95b7585
KN
304};
305
574e96a2
KN
306#define REF_FORMATTING_STATE_INIT { 0, NULL }
307
308struct ref_formatting_stack {
309 struct ref_formatting_stack *prev;
310 struct strbuf output;
c58492d4 311 void (*at_end)(struct ref_formatting_stack **stack);
ce592082 312 void *at_end_data;
574e96a2
KN
313};
314
315struct ref_formatting_state {
316 int quote_style;
317 struct ref_formatting_stack *stack;
318};
319
3a25761a
KN
320struct atom_value {
321 const char *s;
63d89fbc 322 void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
3a25761a 323 unsigned long ul; /* used for sorting when not FIELD_STR */
c58fc856 324 struct used_atom *atom;
3a25761a
KN
325};
326
c95b7585
KN
327/*
328 * Used to parse format string and sort specifiers
329 */
330int parse_ref_filter_atom(const char *atom, const char *ep)
331{
332 const char *sp;
4de707ea 333 const char *arg;
e94ce139 334 int i, at, atom_len;
c95b7585
KN
335
336 sp = atom;
337 if (*sp == '*' && sp < ep)
338 sp++; /* deref */
339 if (ep <= sp)
1823c619 340 die(_("malformed field name: %.*s"), (int)(ep-atom), atom);
c95b7585
KN
341
342 /* Do we have the atom already used elsewhere? */
343 for (i = 0; i < used_atom_cnt; i++) {
b072add7
KN
344 int len = strlen(used_atom[i].name);
345 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
c95b7585
KN
346 return i;
347 }
348
e94ce139
SG
349 /*
350 * If the atom name has a colon, strip it and everything after
351 * it off - it specifies the format for this entry, and
352 * shouldn't be used for checking against the valid_atom
353 * table.
354 */
355 arg = memchr(sp, ':', ep - sp);
356 atom_len = (arg ? arg : ep) - sp;
357
c95b7585
KN
358 /* Is the atom a valid one? */
359 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
360 int len = strlen(valid_atom[i].name);
e94ce139 361 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
c95b7585
KN
362 break;
363 }
364
365 if (ARRAY_SIZE(valid_atom) <= i)
1823c619 366 die(_("unknown field name: %.*s"), (int)(ep-atom), atom);
c95b7585
KN
367
368 /* Add it in, including the deref prefix */
369 at = used_atom_cnt;
370 used_atom_cnt++;
371 REALLOC_ARRAY(used_atom, used_atom_cnt);
b072add7
KN
372 used_atom[at].name = xmemdupz(atom, ep - atom);
373 used_atom[at].type = valid_atom[i].cmp_type;
4de707ea
KN
374 if (arg)
375 arg = used_atom[at].name + (arg - atom) + 1;
fd935cc7 376 memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
4de707ea
KN
377 if (valid_atom[i].parser)
378 valid_atom[i].parser(&used_atom[at], arg);
c95b7585
KN
379 if (*atom == '*')
380 need_tagged = 1;
01f95825 381 if (!strcmp(valid_atom[i].name, "symref"))
c95b7585
KN
382 need_symref = 1;
383 return at;
384}
385
63d89fbc
KN
386static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
387{
388 switch (quote_style) {
389 case QUOTE_NONE:
390 strbuf_addstr(s, str);
391 break;
392 case QUOTE_SHELL:
393 sq_quote_buf(s, str);
394 break;
395 case QUOTE_PERL:
396 perl_quote_buf(s, str);
397 break;
398 case QUOTE_PYTHON:
399 python_quote_buf(s, str);
400 break;
401 case QUOTE_TCL:
402 tcl_quote_buf(s, str);
403 break;
404 }
405}
406
407static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
408{
ce592082
KN
409 /*
410 * Quote formatting is only done when the stack has a single
411 * element. Otherwise quote formatting is done on the
412 * element's entire output strbuf when the %(end) atom is
413 * encountered.
414 */
415 if (!state->stack->prev)
416 quote_formatting(&state->stack->output, v->s, state->quote_style);
417 else
418 strbuf_addstr(&state->stack->output, v->s);
63d89fbc
KN
419}
420
574e96a2
KN
421static void push_stack_element(struct ref_formatting_stack **stack)
422{
423 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
424
425 strbuf_init(&s->output, 0);
426 s->prev = *stack;
427 *stack = s;
428}
429
430static void pop_stack_element(struct ref_formatting_stack **stack)
431{
432 struct ref_formatting_stack *current = *stack;
433 struct ref_formatting_stack *prev = current->prev;
434
435 if (prev)
436 strbuf_addbuf(&prev->output, &current->output);
437 strbuf_release(&current->output);
438 free(current);
439 *stack = prev;
440}
441
c58492d4 442static void end_align_handler(struct ref_formatting_stack **stack)
ce592082 443{
c58492d4
KN
444 struct ref_formatting_stack *cur = *stack;
445 struct align *align = (struct align *)cur->at_end_data;
ce592082
KN
446 struct strbuf s = STRBUF_INIT;
447
c58492d4
KN
448 strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
449 strbuf_swap(&cur->output, &s);
ce592082
KN
450 strbuf_release(&s);
451}
452
453static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
454{
455 struct ref_formatting_stack *new;
456
457 push_stack_element(&state->stack);
458 new = state->stack;
459 new->at_end = end_align_handler;
c58fc856 460 new->at_end_data = &atomv->atom->u.align;
ce592082
KN
461}
462
c58492d4
KN
463static void if_then_else_handler(struct ref_formatting_stack **stack)
464{
465 struct ref_formatting_stack *cur = *stack;
466 struct ref_formatting_stack *prev = cur->prev;
467 struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
468
469 if (!if_then_else->then_atom_seen)
470 die(_("format: %%(if) atom used without a %%(then) atom"));
471
472 if (if_then_else->else_atom_seen) {
473 /*
474 * There is an %(else) atom: we need to drop one state from the
475 * stack, either the %(else) branch if the condition is satisfied, or
476 * the %(then) branch if it isn't.
477 */
478 if (if_then_else->condition_satisfied) {
479 strbuf_reset(&cur->output);
480 pop_stack_element(&cur);
481 } else {
482 strbuf_swap(&cur->output, &prev->output);
483 strbuf_reset(&cur->output);
484 pop_stack_element(&cur);
485 }
486 } else if (!if_then_else->condition_satisfied) {
487 /*
488 * No %(else) atom: just drop the %(then) branch if the
489 * condition is not satisfied.
490 */
491 strbuf_reset(&cur->output);
492 }
493
494 *stack = cur;
495 free(if_then_else);
496}
497
498static void if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
499{
500 struct ref_formatting_stack *new;
501 struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
502
4f3e3b37
KN
503 if_then_else->str = atomv->atom->u.if_then_else.str;
504 if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
505
c58492d4
KN
506 push_stack_element(&state->stack);
507 new = state->stack;
508 new->at_end = if_then_else_handler;
509 new->at_end_data = if_then_else;
510}
511
512static int is_empty(const char *s)
513{
514 while (*s != '\0') {
515 if (!isspace(*s))
516 return 0;
517 s++;
518 }
519 return 1;
520}
521
522static void then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
523{
524 struct ref_formatting_stack *cur = state->stack;
525 struct if_then_else *if_then_else = NULL;
526
527 if (cur->at_end == if_then_else_handler)
528 if_then_else = (struct if_then_else *)cur->at_end_data;
529 if (!if_then_else)
530 die(_("format: %%(then) atom used without an %%(if) atom"));
531 if (if_then_else->then_atom_seen)
532 die(_("format: %%(then) atom used more than once"));
533 if (if_then_else->else_atom_seen)
534 die(_("format: %%(then) atom used after %%(else)"));
535 if_then_else->then_atom_seen = 1;
536 /*
4f3e3b37
KN
537 * If the 'equals' or 'notequals' attribute is used then
538 * perform the required comparison. If not, only non-empty
539 * strings satisfy the 'if' condition.
c58492d4 540 */
4f3e3b37
KN
541 if (if_then_else->cmp_status == COMPARE_EQUAL) {
542 if (!strcmp(if_then_else->str, cur->output.buf))
543 if_then_else->condition_satisfied = 1;
544 } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
545 if (strcmp(if_then_else->str, cur->output.buf))
546 if_then_else->condition_satisfied = 1;
547 } else if (cur->output.len && !is_empty(cur->output.buf))
c58492d4
KN
548 if_then_else->condition_satisfied = 1;
549 strbuf_reset(&cur->output);
550}
551
552static void else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
553{
554 struct ref_formatting_stack *prev = state->stack;
555 struct if_then_else *if_then_else = NULL;
556
557 if (prev->at_end == if_then_else_handler)
558 if_then_else = (struct if_then_else *)prev->at_end_data;
559 if (!if_then_else)
560 die(_("format: %%(else) atom used without an %%(if) atom"));
561 if (!if_then_else->then_atom_seen)
562 die(_("format: %%(else) atom used without a %%(then) atom"));
563 if (if_then_else->else_atom_seen)
564 die(_("format: %%(else) atom used more than once"));
565 if_then_else->else_atom_seen = 1;
566 push_stack_element(&state->stack);
567 state->stack->at_end_data = prev->at_end_data;
568 state->stack->at_end = prev->at_end;
569}
570
ce592082
KN
571static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
572{
573 struct ref_formatting_stack *current = state->stack;
574 struct strbuf s = STRBUF_INIT;
575
576 if (!current->at_end)
577 die(_("format: %%(end) atom used without corresponding atom"));
c58492d4
KN
578 current->at_end(&state->stack);
579
580 /* Stack may have been popped within at_end(), hence reset the current pointer */
581 current = state->stack;
ce592082
KN
582
583 /*
584 * Perform quote formatting when the stack element is that of
585 * a supporting atom. If nested then perform quote formatting
586 * only on the topmost supporting atom.
587 */
c58492d4 588 if (!current->prev->prev) {
ce592082
KN
589 quote_formatting(&s, current->output.buf, state->quote_style);
590 strbuf_swap(&current->output, &s);
591 }
592 strbuf_release(&s);
593 pop_stack_element(&state->stack);
594}
595
c95b7585
KN
596/*
597 * In a format string, find the next occurrence of %(atom).
598 */
599static const char *find_next(const char *cp)
600{
601 while (*cp) {
602 if (*cp == '%') {
603 /*
604 * %( is the start of an atom;
605 * %% is a quoted per-cent.
606 */
607 if (cp[1] == '(')
608 return cp;
609 else if (cp[1] == '%')
610 cp++; /* skip over two % */
611 /* otherwise this is a singleton, literal % */
612 }
613 cp++;
614 }
615 return NULL;
616}
617
618/*
619 * Make sure the format string is well formed, and parse out
620 * the used atoms.
621 */
622int verify_ref_format(const char *format)
623{
624 const char *cp, *sp;
625
626 need_color_reset_at_eol = 0;
627 for (cp = format; *cp && (sp = find_next(cp)); ) {
628 const char *color, *ep = strchr(sp, ')');
629 int at;
630
631 if (!ep)
1823c619 632 return error(_("malformed format string %s"), sp);
c95b7585
KN
633 /* sp points at "%(" and ep points at the closing ")" */
634 at = parse_ref_filter_atom(sp + 2, ep);
635 cp = ep + 1;
636
b072add7 637 if (skip_prefix(used_atom[at].name, "color:", &color))
c95b7585
KN
638 need_color_reset_at_eol = !!strcmp(color, "reset");
639 }
640 return 0;
641}
642
643/*
644 * Given an object name, read the object data and size, and return a
645 * "struct object". If the object data we are returning is also borrowed
646 * by the "struct object" representation, set *eaten as well---it is a
647 * signal from parse_object_buffer to us not to free the buffer.
648 */
649static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
650{
651 enum object_type type;
652 void *buf = read_sha1_file(sha1, &type, sz);
653
654 if (buf)
655 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
656 else
657 *obj = NULL;
658 return buf;
659}
660
661static int grab_objectname(const char *name, const unsigned char *sha1,
fe63c4d1 662 struct atom_value *v, struct used_atom *atom)
c95b7585 663{
fe63c4d1 664 if (starts_with(name, "objectname")) {
42d0eb05 665 if (atom->u.objectname.option == O_SHORT) {
fe63c4d1
KN
666 v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
667 return 1;
42d0eb05 668 } else if (atom->u.objectname.option == O_FULL) {
fe63c4d1
KN
669 v->s = xstrdup(sha1_to_hex(sha1));
670 return 1;
42d0eb05
KN
671 } else if (atom->u.objectname.option == O_LENGTH) {
672 v->s = xstrdup(find_unique_abbrev(sha1, atom->u.objectname.length));
673 return 1;
fe63c4d1
KN
674 } else
675 die("BUG: unknown %%(objectname) option");
c95b7585
KN
676 }
677 return 0;
678}
679
680/* See grab_values */
681static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
682{
683 int i;
684
685 for (i = 0; i < used_atom_cnt; i++) {
b072add7 686 const char *name = used_atom[i].name;
c95b7585
KN
687 struct atom_value *v = &val[i];
688 if (!!deref != (*name == '*'))
689 continue;
690 if (deref)
691 name++;
692 if (!strcmp(name, "objecttype"))
693 v->s = typename(obj->type);
694 else if (!strcmp(name, "objectsize")) {
c95b7585 695 v->ul = sz;
a5e03bf5 696 v->s = xstrfmt("%lu", sz);
c95b7585
KN
697 }
698 else if (deref)
fe63c4d1 699 grab_objectname(name, obj->oid.hash, v, &used_atom[i]);
c95b7585
KN
700 }
701}
702
703/* See grab_values */
704static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
705{
706 int i;
707 struct tag *tag = (struct tag *) obj;
708
709 for (i = 0; i < used_atom_cnt; i++) {
b072add7 710 const char *name = used_atom[i].name;
c95b7585
KN
711 struct atom_value *v = &val[i];
712 if (!!deref != (*name == '*'))
713 continue;
714 if (deref)
715 name++;
716 if (!strcmp(name, "tag"))
717 v->s = tag->tag;
718 else if (!strcmp(name, "type") && tag->tagged)
719 v->s = typename(tag->tagged->type);
a5e03bf5 720 else if (!strcmp(name, "object") && tag->tagged)
f2fd0760 721 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
c95b7585
KN
722 }
723}
724
725/* See grab_values */
726static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
727{
728 int i;
729 struct commit *commit = (struct commit *) obj;
730
731 for (i = 0; i < used_atom_cnt; i++) {
b072add7 732 const char *name = used_atom[i].name;
c95b7585
KN
733 struct atom_value *v = &val[i];
734 if (!!deref != (*name == '*'))
735 continue;
736 if (deref)
737 name++;
738 if (!strcmp(name, "tree")) {
f2fd0760 739 v->s = xstrdup(oid_to_hex(&commit->tree->object.oid));
c95b7585 740 }
a5e03bf5 741 else if (!strcmp(name, "numparent")) {
c95b7585 742 v->ul = commit_list_count(commit->parents);
a5e03bf5 743 v->s = xstrfmt("%lu", v->ul);
c95b7585
KN
744 }
745 else if (!strcmp(name, "parent")) {
c95b7585 746 struct commit_list *parents;
a5e03bf5
JK
747 struct strbuf s = STRBUF_INIT;
748 for (parents = commit->parents; parents; parents = parents->next) {
c95b7585 749 struct commit *parent = parents->item;
a5e03bf5
JK
750 if (parents != commit->parents)
751 strbuf_addch(&s, ' ');
f2fd0760 752 strbuf_addstr(&s, oid_to_hex(&parent->object.oid));
c95b7585 753 }
a5e03bf5 754 v->s = strbuf_detach(&s, NULL);
c95b7585
KN
755 }
756 }
757}
758
759static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
760{
761 const char *eol;
762 while (*buf) {
763 if (!strncmp(buf, who, wholen) &&
764 buf[wholen] == ' ')
765 return buf + wholen + 1;
766 eol = strchr(buf, '\n');
767 if (!eol)
768 return "";
769 eol++;
770 if (*eol == '\n')
771 return ""; /* end of header */
772 buf = eol;
773 }
774 return "";
775}
776
777static const char *copy_line(const char *buf)
778{
779 const char *eol = strchrnul(buf, '\n');
780 return xmemdupz(buf, eol - buf);
781}
782
783static const char *copy_name(const char *buf)
784{
785 const char *cp;
786 for (cp = buf; *cp && *cp != '\n'; cp++) {
787 if (!strncmp(cp, " <", 2))
788 return xmemdupz(buf, cp - buf);
789 }
790 return "";
791}
792
793static const char *copy_email(const char *buf)
794{
795 const char *email = strchr(buf, '<');
796 const char *eoemail;
797 if (!email)
798 return "";
799 eoemail = strchr(email, '>');
800 if (!eoemail)
801 return "";
802 return xmemdupz(email, eoemail + 1 - email);
803}
804
805static char *copy_subject(const char *buf, unsigned long len)
806{
807 char *r = xmemdupz(buf, len);
808 int i;
809
810 for (i = 0; i < len; i++)
811 if (r[i] == '\n')
812 r[i] = ' ';
813
814 return r;
815}
816
817static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
818{
819 const char *eoemail = strstr(buf, "> ");
820 char *zone;
821 unsigned long timestamp;
822 long tz;
d939af12 823 struct date_mode date_mode = { DATE_NORMAL };
c95b7585
KN
824 const char *formatp;
825
826 /*
827 * We got here because atomname ends in "date" or "date<something>";
828 * it's not possible that <something> is not ":<format>" because
829 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
830 * ":" means no format is specified, and use the default.
831 */
832 formatp = strchr(atomname, ':');
833 if (formatp != NULL) {
834 formatp++;
d939af12 835 parse_date_format(formatp, &date_mode);
c95b7585
KN
836 }
837
838 if (!eoemail)
839 goto bad;
840 timestamp = strtoul(eoemail + 2, &zone, 10);
841 if (timestamp == ULONG_MAX)
842 goto bad;
843 tz = strtol(zone, NULL, 10);
844 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
845 goto bad;
d939af12 846 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
c95b7585
KN
847 v->ul = timestamp;
848 return;
849 bad:
850 v->s = "";
851 v->ul = 0;
852}
853
854/* See grab_values */
855static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
856{
857 int i;
858 int wholen = strlen(who);
859 const char *wholine = NULL;
860
861 for (i = 0; i < used_atom_cnt; i++) {
b072add7 862 const char *name = used_atom[i].name;
c95b7585
KN
863 struct atom_value *v = &val[i];
864 if (!!deref != (*name == '*'))
865 continue;
866 if (deref)
867 name++;
868 if (strncmp(who, name, wholen))
869 continue;
870 if (name[wholen] != 0 &&
871 strcmp(name + wholen, "name") &&
872 strcmp(name + wholen, "email") &&
873 !starts_with(name + wholen, "date"))
874 continue;
875 if (!wholine)
876 wholine = find_wholine(who, wholen, buf, sz);
877 if (!wholine)
878 return; /* no point looking for it */
879 if (name[wholen] == 0)
880 v->s = copy_line(wholine);
881 else if (!strcmp(name + wholen, "name"))
882 v->s = copy_name(wholine);
883 else if (!strcmp(name + wholen, "email"))
884 v->s = copy_email(wholine);
885 else if (starts_with(name + wholen, "date"))
886 grab_date(wholine, v, name);
887 }
888
889 /*
890 * For a tag or a commit object, if "creator" or "creatordate" is
891 * requested, do something special.
892 */
893 if (strcmp(who, "tagger") && strcmp(who, "committer"))
894 return; /* "author" for commit object is not wanted */
895 if (!wholine)
896 wholine = find_wholine(who, wholen, buf, sz);
897 if (!wholine)
898 return;
899 for (i = 0; i < used_atom_cnt; i++) {
b072add7 900 const char *name = used_atom[i].name;
c95b7585
KN
901 struct atom_value *v = &val[i];
902 if (!!deref != (*name == '*'))
903 continue;
904 if (deref)
905 name++;
906
907 if (starts_with(name, "creatordate"))
908 grab_date(wholine, v, name);
909 else if (!strcmp(name, "creator"))
910 v->s = copy_line(wholine);
911 }
912}
913
914static void find_subpos(const char *buf, unsigned long sz,
915 const char **sub, unsigned long *sublen,
916 const char **body, unsigned long *bodylen,
917 unsigned long *nonsiglen,
918 const char **sig, unsigned long *siglen)
919{
920 const char *eol;
921 /* skip past header until we hit empty line */
922 while (*buf && *buf != '\n') {
923 eol = strchrnul(buf, '\n');
924 if (*eol)
925 eol++;
926 buf = eol;
927 }
928 /* skip any empty lines */
929 while (*buf == '\n')
930 buf++;
931
932 /* parse signature first; we might not even have a subject line */
933 *sig = buf + parse_signature(buf, strlen(buf));
934 *siglen = strlen(*sig);
935
936 /* subject is first non-empty line */
937 *sub = buf;
938 /* subject goes to first empty line */
939 while (buf < *sig && *buf && *buf != '\n') {
940 eol = strchrnul(buf, '\n');
941 if (*eol)
942 eol++;
943 buf = eol;
944 }
945 *sublen = buf - *sub;
946 /* drop trailing newline, if present */
947 if (*sublen && (*sub)[*sublen - 1] == '\n')
948 *sublen -= 1;
949
950 /* skip any empty lines */
951 while (*buf == '\n')
952 buf++;
953 *body = buf;
954 *bodylen = strlen(buf);
955 *nonsiglen = *sig - buf;
956}
957
1bb38e5a
KN
958/*
959 * If 'lines' is greater than 0, append that many lines from the given
960 * 'buf' of length 'size' to the given strbuf.
961 */
962static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
963{
964 int i;
965 const char *sp, *eol;
966 size_t len;
967
968 sp = buf;
969
970 for (i = 0; i < lines && sp < buf + size; i++) {
971 if (i)
972 strbuf_addstr(out, "\n ");
973 eol = memchr(sp, '\n', size - (sp - buf));
974 len = eol ? eol - sp : size - (sp - buf);
975 strbuf_add(out, sp, len);
976 if (!eol)
977 break;
978 sp = eol + 1;
979 }
980}
981
c95b7585
KN
982/* See grab_values */
983static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
984{
985 int i;
986 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
987 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
988
989 for (i = 0; i < used_atom_cnt; i++) {
452db397
KN
990 struct used_atom *atom = &used_atom[i];
991 const char *name = atom->name;
c95b7585
KN
992 struct atom_value *v = &val[i];
993 if (!!deref != (*name == '*'))
994 continue;
995 if (deref)
996 name++;
997 if (strcmp(name, "subject") &&
998 strcmp(name, "body") &&
b1d31c89 999 strcmp(name, "trailers") &&
452db397 1000 !starts_with(name, "contents"))
c95b7585
KN
1001 continue;
1002 if (!subpos)
1003 find_subpos(buf, sz,
1004 &subpos, &sublen,
1005 &bodypos, &bodylen, &nonsiglen,
1006 &sigpos, &siglen);
1007
452db397 1008 if (atom->u.contents.option == C_SUB)
c95b7585 1009 v->s = copy_subject(subpos, sublen);
452db397 1010 else if (atom->u.contents.option == C_BODY_DEP)
c95b7585 1011 v->s = xmemdupz(bodypos, bodylen);
452db397 1012 else if (atom->u.contents.option == C_BODY)
c95b7585 1013 v->s = xmemdupz(bodypos, nonsiglen);
452db397 1014 else if (atom->u.contents.option == C_SIG)
c95b7585 1015 v->s = xmemdupz(sigpos, siglen);
452db397 1016 else if (atom->u.contents.option == C_LINES) {
1bb38e5a
KN
1017 struct strbuf s = STRBUF_INIT;
1018 const char *contents_end = bodylen + bodypos - siglen;
1019
1bb38e5a 1020 /* Size is the length of the message after removing the signature */
452db397 1021 append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
1bb38e5a 1022 v->s = strbuf_detach(&s, NULL);
b1d31c89
JK
1023 } else if (atom->u.contents.option == C_TRAILERS) {
1024 struct trailer_info info;
1025
1026 /* Search for trailer info */
1027 trailer_info_get(&info, subpos);
1028 v->s = xmemdupz(info.trailer_start,
1029 info.trailer_end - info.trailer_start);
1030 trailer_info_release(&info);
452db397
KN
1031 } else if (atom->u.contents.option == C_BARE)
1032 v->s = xstrdup(subpos);
c95b7585
KN
1033 }
1034}
1035
1036/*
1037 * We want to have empty print-string for field requests
1038 * that do not apply (e.g. "authordate" for a tag object)
1039 */
1040static void fill_missing_values(struct atom_value *val)
1041{
1042 int i;
1043 for (i = 0; i < used_atom_cnt; i++) {
1044 struct atom_value *v = &val[i];
1045 if (v->s == NULL)
1046 v->s = "";
1047 }
1048}
1049
1050/*
1051 * val is a list of atom_value to hold returned values. Extract
1052 * the values for atoms in used_atom array out of (obj, buf, sz).
1053 * when deref is false, (obj, buf, sz) is the object that is
1054 * pointed at by the ref itself; otherwise it is the object the
1055 * ref (which is a tag) refers to.
1056 */
1057static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
1058{
1059 grab_common_values(val, deref, obj, buf, sz);
1060 switch (obj->type) {
1061 case OBJ_TAG:
1062 grab_tag_values(val, deref, obj, buf, sz);
1063 grab_sub_body_contents(val, deref, obj, buf, sz);
1064 grab_person("tagger", val, deref, obj, buf, sz);
1065 break;
1066 case OBJ_COMMIT:
1067 grab_commit_values(val, deref, obj, buf, sz);
1068 grab_sub_body_contents(val, deref, obj, buf, sz);
1069 grab_person("author", val, deref, obj, buf, sz);
1070 grab_person("committer", val, deref, obj, buf, sz);
1071 break;
1072 case OBJ_TREE:
1073 /* grab_tree_values(val, deref, obj, buf, sz); */
1074 break;
1075 case OBJ_BLOB:
1076 /* grab_blob_values(val, deref, obj, buf, sz); */
1077 break;
1078 default:
1079 die("Eh? Object of type %d?", obj->type);
1080 }
1081}
1082
1083static inline char *copy_advance(char *dst, const char *src)
1084{
1085 while (*src)
1086 *dst++ = *src++;
1087 return dst;
1088}
1089
a7984101 1090static const char *strip_ref_components(const char *refname, unsigned int len)
0571979b 1091{
a7984101 1092 long remaining = len;
0571979b
JK
1093 const char *start = refname;
1094
0571979b
JK
1095 while (remaining) {
1096 switch (*start++) {
1097 case '\0':
a7984101
KN
1098 die(_("ref '%s' does not have %ud components to :strip"),
1099 refname, len);
0571979b
JK
1100 case '/':
1101 remaining--;
1102 break;
1103 }
1104 }
1105 return start;
1106}
1107
a7984101
KN
1108static const char *show_ref(struct refname_atom *atom, const char *refname)
1109{
1110 if (atom->option == R_SHORT)
1111 return shorten_unambiguous_ref(refname, warn_ambiguous_refs);
1112 else if (atom->option == R_STRIP)
1113 return strip_ref_components(refname, atom->strip);
1114 else
1115 return refname;
1116}
1117
5339bdad
KN
1118static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
1119 struct branch *branch, const char **s)
1120{
1121 int num_ours, num_theirs;
7743fcca 1122 if (atom->u.remote_ref.option == RR_SHORTEN)
5339bdad 1123 *s = shorten_unambiguous_ref(refname, warn_ambiguous_refs);
7743fcca 1124 else if (atom->u.remote_ref.option == RR_TRACK) {
5339bdad 1125 if (stat_tracking_info(branch, &num_ours,
ffd921d3 1126 &num_theirs, NULL)) {
7743fcca
KN
1127 *s = xstrdup("gone");
1128 } else if (!num_ours && !num_theirs)
5339bdad
KN
1129 *s = "";
1130 else if (!num_ours)
7743fcca 1131 *s = xstrfmt("behind %d", num_theirs);
5339bdad 1132 else if (!num_theirs)
7743fcca 1133 *s = xstrfmt("ahead %d", num_ours);
5339bdad 1134 else
7743fcca 1135 *s = xstrfmt("ahead %d, behind %d",
5339bdad 1136 num_ours, num_theirs);
7743fcca
KN
1137 if (!atom->u.remote_ref.nobracket && *s[0]) {
1138 const char *to_free = *s;
1139 *s = xstrfmt("[%s]", *s);
1140 free((void *)to_free);
1141 }
1142 } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
5339bdad
KN
1143 if (stat_tracking_info(branch, &num_ours,
1144 &num_theirs, NULL))
1145 return;
1146
1147 if (!num_ours && !num_theirs)
1148 *s = "=";
1149 else if (!num_ours)
1150 *s = "<";
1151 else if (!num_theirs)
1152 *s = ">";
1153 else
1154 *s = "<>";
1155 } else /* RR_NORMAL */
1156 *s = refname;
1157}
1158
d4919bb2
KN
1159char *get_head_description(void)
1160{
1161 struct strbuf desc = STRBUF_INIT;
1162 struct wt_status_state state;
1163 memset(&state, 0, sizeof(state));
1164 wt_status_get_state(&state, 1);
1165 if (state.rebase_in_progress ||
1166 state.rebase_interactive_in_progress)
1167 strbuf_addf(&desc, _("(no branch, rebasing %s)"),
1168 state.branch);
1169 else if (state.bisect_in_progress)
1170 strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
1171 state.branch);
1172 else if (state.detached_from) {
1173 /* TRANSLATORS: make sure these match _("HEAD detached at ")
1174 and _("HEAD detached from ") in wt-status.c */
1175 if (state.detached_at)
1176 strbuf_addf(&desc, _("(HEAD detached at %s)"),
1177 state.detached_from);
1178 else
1179 strbuf_addf(&desc, _("(HEAD detached from %s)"),
1180 state.detached_from);
1181 }
1182 else
1183 strbuf_addstr(&desc, _("(no branch)"));
1184 free(state.branch);
1185 free(state.onto);
1186 free(state.detached_from);
1187 return strbuf_detach(&desc, NULL);
1188}
1189
a7984101
KN
1190static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
1191{
1192 if (!ref->symref)
1193 return "";
1194 else
1195 return show_ref(&atom->u.refname, ref->symref);
1196}
1197
1198static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref)
1199{
1200 if (ref->kind & FILTER_REFS_DETACHED_HEAD)
1201 return get_head_description();
1202 return show_ref(&atom->u.refname, ref->refname);
1203}
1204
c95b7585
KN
1205/*
1206 * Parse the object referred by ref, and grab needed value.
1207 */
1208static void populate_value(struct ref_array_item *ref)
1209{
1210 void *buf;
1211 struct object *obj;
1212 int eaten, i;
1213 unsigned long size;
1214 const unsigned char *tagged;
1215
1216 ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
1217
1218 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
1219 unsigned char unused1[20];
1220 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
1221 unused1, NULL);
1222 if (!ref->symref)
1223 ref->symref = "";
1224 }
1225
1226 /* Fill in specials first */
1227 for (i = 0; i < used_atom_cnt; i++) {
fd935cc7 1228 struct used_atom *atom = &used_atom[i];
b072add7 1229 const char *name = used_atom[i].name;
c95b7585
KN
1230 struct atom_value *v = &ref->value[i];
1231 int deref = 0;
1232 const char *refname;
c95b7585
KN
1233 struct branch *branch = NULL;
1234
63d89fbc 1235 v->handler = append_atom;
c58fc856 1236 v->atom = atom;
63d89fbc 1237
c95b7585
KN
1238 if (*name == '*') {
1239 deref = 1;
1240 name++;
1241 }
1242
a7984101
KN
1243 if (starts_with(name, "refname"))
1244 refname = get_refname(atom, ref);
1245 else if (starts_with(name, "symref"))
1246 refname = get_symref(atom, ref);
c95b7585
KN
1247 else if (starts_with(name, "upstream")) {
1248 const char *branch_name;
1249 /* only local branches may have an upstream */
1250 if (!skip_prefix(ref->refname, "refs/heads/",
1251 &branch_name))
1252 continue;
1253 branch = branch_get(branch_name);
1254
1255 refname = branch_get_upstream(branch, NULL);
5339bdad
KN
1256 if (refname)
1257 fill_remote_ref_details(atom, refname, branch, &v->s);
1258 continue;
c95b7585
KN
1259 } else if (starts_with(name, "push")) {
1260 const char *branch_name;
1261 if (!skip_prefix(ref->refname, "refs/heads/",
1262 &branch_name))
1263 continue;
1264 branch = branch_get(branch_name);
1265
1266 refname = branch_get_push(branch, NULL);
1267 if (!refname)
1268 continue;
5339bdad
KN
1269 fill_remote_ref_details(atom, refname, branch, &v->s);
1270 continue;
fd935cc7
KN
1271 } else if (starts_with(name, "color:")) {
1272 v->s = atom->u.color;
c95b7585
KN
1273 continue;
1274 } else if (!strcmp(name, "flag")) {
1275 char buf[256], *cp = buf;
1276 if (ref->flag & REF_ISSYMREF)
1277 cp = copy_advance(cp, ",symref");
1278 if (ref->flag & REF_ISPACKED)
1279 cp = copy_advance(cp, ",packed");
1280 if (cp == buf)
1281 v->s = "";
1282 else {
1283 *cp = '\0';
1284 v->s = xstrdup(buf + 1);
1285 }
1286 continue;
fe63c4d1 1287 } else if (!deref && grab_objectname(name, ref->objectname, v, atom)) {
c95b7585
KN
1288 continue;
1289 } else if (!strcmp(name, "HEAD")) {
1290 const char *head;
1291 unsigned char sha1[20];
1292
1293 head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1294 sha1, NULL);
84679d47 1295 if (head && !strcmp(ref->refname, head))
c95b7585
KN
1296 v->s = "*";
1297 else
1298 v->s = " ";
1299 continue;
5bd881d9 1300 } else if (starts_with(name, "align")) {
ce592082
KN
1301 v->handler = align_atom_handler;
1302 continue;
1303 } else if (!strcmp(name, "end")) {
1304 v->handler = end_atom_handler;
1305 continue;
4f3e3b37
KN
1306 } else if (starts_with(name, "if")) {
1307 const char *s;
1308
1309 if (skip_prefix(name, "if:", &s))
1310 v->s = xstrdup(s);
c58492d4
KN
1311 v->handler = if_atom_handler;
1312 continue;
1313 } else if (!strcmp(name, "then")) {
1314 v->handler = then_atom_handler;
1315 continue;
1316 } else if (!strcmp(name, "else")) {
1317 v->handler = else_atom_handler;
1318 continue;
c95b7585
KN
1319 } else
1320 continue;
1321
c95b7585
KN
1322 if (!deref)
1323 v->s = refname;
a5e03bf5
JK
1324 else
1325 v->s = xstrfmt("%s^{}", refname);
c95b7585
KN
1326 }
1327
1328 for (i = 0; i < used_atom_cnt; i++) {
1329 struct atom_value *v = &ref->value[i];
1330 if (v->s == NULL)
1331 goto need_obj;
1332 }
1333 return;
1334
1335 need_obj:
1336 buf = get_obj(ref->objectname, &obj, &size, &eaten);
1337 if (!buf)
1823c619 1338 die(_("missing object %s for %s"),
c95b7585
KN
1339 sha1_to_hex(ref->objectname), ref->refname);
1340 if (!obj)
1823c619 1341 die(_("parse_object_buffer failed on %s for %s"),
c95b7585
KN
1342 sha1_to_hex(ref->objectname), ref->refname);
1343
1344 grab_values(ref->value, 0, obj, buf, size);
1345 if (!eaten)
1346 free(buf);
1347
1348 /*
1349 * If there is no atom that wants to know about tagged
1350 * object, we are done.
1351 */
1352 if (!need_tagged || (obj->type != OBJ_TAG))
1353 return;
1354
1355 /*
1356 * If it is a tag object, see if we use a value that derefs
1357 * the object, and if we do grab the object it refers to.
1358 */
ed1c9977 1359 tagged = ((struct tag *)obj)->tagged->oid.hash;
c95b7585
KN
1360
1361 /*
1362 * NEEDSWORK: This derefs tag only once, which
1363 * is good to deal with chains of trust, but
1364 * is not consistent with what deref_tag() does
1365 * which peels the onion to the core.
1366 */
1367 buf = get_obj(tagged, &obj, &size, &eaten);
1368 if (!buf)
1823c619 1369 die(_("missing object %s for %s"),
c95b7585
KN
1370 sha1_to_hex(tagged), ref->refname);
1371 if (!obj)
1823c619 1372 die(_("parse_object_buffer failed on %s for %s"),
c95b7585
KN
1373 sha1_to_hex(tagged), ref->refname);
1374 grab_values(ref->value, 1, obj, buf, size);
1375 if (!eaten)
1376 free(buf);
1377}
1378
1379/*
1380 * Given a ref, return the value for the atom. This lazily gets value
1381 * out of the object by calling populate value.
1382 */
1383static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
1384{
1385 if (!ref->value) {
1386 populate_value(ref);
1387 fill_missing_values(ref->value);
1388 }
1389 *v = &ref->value[atom];
1390}
1391
ee2bd06b
KN
1392enum contains_result {
1393 CONTAINS_UNKNOWN = -1,
1394 CONTAINS_NO = 0,
1395 CONTAINS_YES = 1
1396};
1397
1398/*
1399 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1400 * overflows.
1401 *
1402 * At each recursion step, the stack items points to the commits whose
1403 * ancestors are to be inspected.
1404 */
1405struct contains_stack {
1406 int nr, alloc;
1407 struct contains_stack_entry {
1408 struct commit *commit;
1409 struct commit_list *parents;
1410 } *contains_stack;
1411};
1412
1413static int in_commit_list(const struct commit_list *want, struct commit *c)
1414{
1415 for (; want; want = want->next)
f2fd0760 1416 if (!oidcmp(&want->item->object.oid, &c->object.oid))
ee2bd06b
KN
1417 return 1;
1418 return 0;
1419}
1420
1421/*
1422 * Test whether the candidate or one of its parents is contained in the list.
1423 * Do not recurse to find out, though, but return -1 if inconclusive.
1424 */
1425static enum contains_result contains_test(struct commit *candidate,
1426 const struct commit_list *want)
1427{
1428 /* was it previously marked as containing a want commit? */
1429 if (candidate->object.flags & TMP_MARK)
1430 return 1;
1431 /* or marked as not possibly containing a want commit? */
1432 if (candidate->object.flags & UNINTERESTING)
1433 return 0;
1434 /* or are we it? */
1435 if (in_commit_list(want, candidate)) {
1436 candidate->object.flags |= TMP_MARK;
1437 return 1;
1438 }
1439
1440 if (parse_commit(candidate) < 0)
1441 return 0;
1442
1443 return -1;
1444}
1445
1446static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
1447{
1448 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
1449 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
1450 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
1451}
1452
1453static enum contains_result contains_tag_algo(struct commit *candidate,
1454 const struct commit_list *want)
1455{
1456 struct contains_stack contains_stack = { 0, 0, NULL };
1457 int result = contains_test(candidate, want);
1458
1459 if (result != CONTAINS_UNKNOWN)
1460 return result;
1461
1462 push_to_contains_stack(candidate, &contains_stack);
1463 while (contains_stack.nr) {
1464 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
1465 struct commit *commit = entry->commit;
1466 struct commit_list *parents = entry->parents;
1467
1468 if (!parents) {
1469 commit->object.flags |= UNINTERESTING;
1470 contains_stack.nr--;
1471 }
1472 /*
1473 * If we just popped the stack, parents->item has been marked,
1474 * therefore contains_test will return a meaningful 0 or 1.
1475 */
1476 else switch (contains_test(parents->item, want)) {
1477 case CONTAINS_YES:
1478 commit->object.flags |= TMP_MARK;
1479 contains_stack.nr--;
1480 break;
1481 case CONTAINS_NO:
1482 entry->parents = parents->next;
1483 break;
1484 case CONTAINS_UNKNOWN:
1485 push_to_contains_stack(parents->item, &contains_stack);
1486 break;
1487 }
1488 }
1489 free(contains_stack.contains_stack);
1490 return contains_test(candidate, want);
1491}
1492
1493static int commit_contains(struct ref_filter *filter, struct commit *commit)
1494{
1495 if (filter->with_commit_tag_algo)
1496 return contains_tag_algo(commit, filter->with_commit);
1497 return is_descendant_of(commit, filter->with_commit);
1498}
1499
bef0e12b
KN
1500/*
1501 * Return 1 if the refname matches one of the patterns, otherwise 0.
1502 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1503 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1504 * matches "refs/heads/mas*", too).
1505 */
3bb16a8b 1506static int match_pattern(const struct ref_filter *filter, const char *refname)
bef0e12b 1507{
3bb16a8b
NTND
1508 const char **patterns = filter->name_patterns;
1509 unsigned flags = 0;
1510
1511 if (filter->ignore_case)
1512 flags |= WM_CASEFOLD;
1513
bef0e12b
KN
1514 /*
1515 * When no '--format' option is given we need to skip the prefix
1516 * for matching refs of tags and branches.
1517 */
1518 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1519 skip_prefix(refname, "refs/heads/", &refname) ||
1520 skip_prefix(refname, "refs/remotes/", &refname) ||
1521 skip_prefix(refname, "refs/", &refname));
1522
1523 for (; *patterns; patterns++) {
3bb16a8b 1524 if (!wildmatch(*patterns, refname, flags, NULL))
bef0e12b
KN
1525 return 1;
1526 }
1527 return 0;
1528}
1529
c95b7585
KN
1530/*
1531 * Return 1 if the refname matches one of the patterns, otherwise 0.
1532 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
bef0e12b
KN
1533 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1534 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
c95b7585 1535 */
3bb16a8b 1536static int match_name_as_path(const struct ref_filter *filter, const char *refname)
c95b7585 1537{
3bb16a8b 1538 const char **pattern = filter->name_patterns;
c95b7585 1539 int namelen = strlen(refname);
3bb16a8b
NTND
1540 unsigned flags = WM_PATHNAME;
1541
1542 if (filter->ignore_case)
1543 flags |= WM_CASEFOLD;
1544
c95b7585
KN
1545 for (; *pattern; pattern++) {
1546 const char *p = *pattern;
1547 int plen = strlen(p);
1548
1549 if ((plen <= namelen) &&
1550 !strncmp(refname, p, plen) &&
1551 (refname[plen] == '\0' ||
1552 refname[plen] == '/' ||
1553 p[plen-1] == '/'))
1554 return 1;
1555 if (!wildmatch(p, refname, WM_PATHNAME, NULL))
1556 return 1;
1557 }
1558 return 0;
1559}
1560
bef0e12b
KN
1561/* Return 1 if the refname matches one of the patterns, otherwise 0. */
1562static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1563{
1564 if (!*filter->name_patterns)
1565 return 1; /* No pattern always matches */
1566 if (filter->match_as_path)
3bb16a8b
NTND
1567 return match_name_as_path(filter, refname);
1568 return match_pattern(filter, refname);
bef0e12b
KN
1569}
1570
68411046
KN
1571/*
1572 * Given a ref (sha1, refname), check if the ref belongs to the array
1573 * of sha1s. If the given ref is a tag, check if the given tag points
1574 * at one of the sha1s in the given sha1 array.
1575 * the given sha1_array.
1576 * NEEDSWORK:
1577 * 1. Only a single level of inderection is obtained, we might want to
1578 * change this to account for multiple levels (e.g. annotated tags
1579 * pointing to annotated tags pointing to a commit.)
1580 * 2. As the refs are cached we might know what refname peels to without
1581 * the need to parse the object via parse_object(). peel_ref() might be a
1582 * more efficient alternative to obtain the pointee.
1583 */
1584static const unsigned char *match_points_at(struct sha1_array *points_at,
1585 const unsigned char *sha1,
1586 const char *refname)
1587{
1588 const unsigned char *tagged_sha1 = NULL;
1589 struct object *obj;
1590
1591 if (sha1_array_lookup(points_at, sha1) >= 0)
1592 return sha1;
1593 obj = parse_object(sha1);
1594 if (!obj)
1595 die(_("malformed object at '%s'"), refname);
1596 if (obj->type == OBJ_TAG)
ed1c9977 1597 tagged_sha1 = ((struct tag *)obj)->tagged->oid.hash;
68411046
KN
1598 if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
1599 return tagged_sha1;
1600 return NULL;
1601}
1602
c95b7585
KN
1603/* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1604static struct ref_array_item *new_ref_array_item(const char *refname,
1605 const unsigned char *objectname,
1606 int flag)
1607{
96ffc06f
JK
1608 struct ref_array_item *ref;
1609 FLEX_ALLOC_STR(ref, refname, refname);
c95b7585
KN
1610 hashcpy(ref->objectname, objectname);
1611 ref->flag = flag;
1612
1613 return ref;
1614}
1615
5b4f2851
KN
1616static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1617{
1618 unsigned int i;
1619
1620 static struct {
1621 const char *prefix;
1622 unsigned int kind;
1623 } ref_kind[] = {
1624 { "refs/heads/" , FILTER_REFS_BRANCHES },
1625 { "refs/remotes/" , FILTER_REFS_REMOTES },
1626 { "refs/tags/", FILTER_REFS_TAGS}
1627 };
1628
1629 if (filter->kind == FILTER_REFS_BRANCHES ||
1630 filter->kind == FILTER_REFS_REMOTES ||
1631 filter->kind == FILTER_REFS_TAGS)
1632 return filter->kind;
1633 else if (!strcmp(refname, "HEAD"))
1634 return FILTER_REFS_DETACHED_HEAD;
1635
1636 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
1637 if (starts_with(refname, ref_kind[i].prefix))
1638 return ref_kind[i].kind;
1639 }
1640
1641 return FILTER_REFS_OTHERS;
1642}
1643
c95b7585
KN
1644/*
1645 * A call-back given to for_each_ref(). Filter refs and keep them for
1646 * later object processing.
1647 */
14de7fba 1648static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
c95b7585
KN
1649{
1650 struct ref_filter_cbdata *ref_cbdata = cb_data;
14de7fba 1651 struct ref_filter *filter = ref_cbdata->filter;
c95b7585 1652 struct ref_array_item *ref;
35257aa0 1653 struct commit *commit = NULL;
5b4f2851 1654 unsigned int kind;
c95b7585
KN
1655
1656 if (flag & REF_BAD_NAME) {
1823c619 1657 warning(_("ignoring ref with broken name %s"), refname);
c95b7585
KN
1658 return 0;
1659 }
1660
7ebc8cbe 1661 if (flag & REF_ISBROKEN) {
1823c619 1662 warning(_("ignoring broken ref %s"), refname);
7ebc8cbe
JH
1663 return 0;
1664 }
1665
5b4f2851
KN
1666 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1667 kind = filter_ref_kind(filter, refname);
1668 if (!(kind & filter->kind))
1669 return 0;
1670
bef0e12b 1671 if (!filter_pattern_match(filter, refname))
c95b7585
KN
1672 return 0;
1673
68411046
KN
1674 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
1675 return 0;
1676
35257aa0
KN
1677 /*
1678 * A merge filter is applied on refs pointing to commits. Hence
1679 * obtain the commit using the 'oid' available and discard all
1680 * non-commits early. The actual filtering is done later.
1681 */
aedcb7dc 1682 if (filter->merge_commit || filter->with_commit || filter->verbose) {
35257aa0
KN
1683 commit = lookup_commit_reference_gently(oid->hash, 1);
1684 if (!commit)
1685 return 0;
ee2bd06b
KN
1686 /* We perform the filtering for the '--contains' option */
1687 if (filter->with_commit &&
1688 !commit_contains(filter, commit))
1689 return 0;
35257aa0
KN
1690 }
1691
c95b7585
KN
1692 /*
1693 * We do not open the object yet; sort may only need refname
1694 * to do its job and the resulting list may yet to be pruned
1695 * by maxcount logic.
1696 */
1697 ref = new_ref_array_item(refname, oid->hash, flag);
35257aa0 1698 ref->commit = commit;
c95b7585 1699
14de7fba
KN
1700 REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1701 ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
5b4f2851 1702 ref->kind = kind;
c95b7585
KN
1703 return 0;
1704}
1705
1706/* Free memory allocated for a ref_array_item */
1707static void free_array_item(struct ref_array_item *item)
1708{
1709 free((char *)item->symref);
c95b7585
KN
1710 free(item);
1711}
1712
1713/* Free all memory allocated for ref_array */
1714void ref_array_clear(struct ref_array *array)
1715{
1716 int i;
1717
1718 for (i = 0; i < array->nr; i++)
1719 free_array_item(array->items[i]);
1720 free(array->items);
1721 array->items = NULL;
1722 array->nr = array->alloc = 0;
1723}
1724
35257aa0
KN
1725static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
1726{
1727 struct rev_info revs;
1728 int i, old_nr;
1729 struct ref_filter *filter = ref_cbdata->filter;
1730 struct ref_array *array = ref_cbdata->array;
1731 struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
1732
1733 init_revisions(&revs, NULL);
1734
1735 for (i = 0; i < array->nr; i++) {
1736 struct ref_array_item *item = array->items[i];
1737 add_pending_object(&revs, &item->commit->object, item->refname);
1738 to_clear[i] = item->commit;
1739 }
1740
1741 filter->merge_commit->object.flags |= UNINTERESTING;
1742 add_pending_object(&revs, &filter->merge_commit->object, "");
1743
1744 revs.limited = 1;
1745 if (prepare_revision_walk(&revs))
1746 die(_("revision walk setup failed"));
1747
1748 old_nr = array->nr;
1749 array->nr = 0;
1750
1751 for (i = 0; i < old_nr; i++) {
1752 struct ref_array_item *item = array->items[i];
1753 struct commit *commit = item->commit;
1754
1755 int is_merged = !!(commit->object.flags & UNINTERESTING);
1756
1757 if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
1758 array->items[array->nr++] = array->items[i];
1759 else
1760 free_array_item(item);
1761 }
1762
1763 for (i = 0; i < old_nr; i++)
1764 clear_commit_marks(to_clear[i], ALL_REV_FLAGS);
1765 clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
1766 free(to_clear);
1767}
1768
14de7fba
KN
1769/*
1770 * API for filtering a set of refs. Based on the type of refs the user
1771 * has requested, we iterate through those refs and apply filters
1772 * as per the given ref_filter structure and finally store the
1773 * filtered refs in the ref_array structure.
1774 */
1775int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
1776{
1777 struct ref_filter_cbdata ref_cbdata;
35257aa0 1778 int ret = 0;
5b4f2851 1779 unsigned int broken = 0;
14de7fba
KN
1780
1781 ref_cbdata.array = array;
1782 ref_cbdata.filter = filter;
1783
5b4f2851
KN
1784 if (type & FILTER_REFS_INCLUDE_BROKEN)
1785 broken = 1;
1786 filter->kind = type & FILTER_REFS_KIND_MASK;
1787
35257aa0 1788 /* Simple per-ref filtering */
5b4f2851 1789 if (!filter->kind)
14de7fba 1790 die("filter_refs: invalid type");
5b4f2851
KN
1791 else {
1792 /*
1793 * For common cases where we need only branches or remotes or tags,
1794 * we only iterate through those refs. If a mix of refs is needed,
1795 * we iterate over all refs and filter out required refs with the help
1796 * of filter_ref_kind().
1797 */
1798 if (filter->kind == FILTER_REFS_BRANCHES)
1799 ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
1800 else if (filter->kind == FILTER_REFS_REMOTES)
1801 ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
1802 else if (filter->kind == FILTER_REFS_TAGS)
1803 ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
1804 else if (filter->kind & FILTER_REFS_ALL)
1805 ret = for_each_fullref_in("", ref_filter_handler, &ref_cbdata, broken);
1806 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
1807 head_ref(ref_filter_handler, &ref_cbdata);
1808 }
1809
35257aa0
KN
1810
1811 /* Filters that need revision walking */
1812 if (filter->merge_commit)
1813 do_merge_filter(&ref_cbdata);
1814
1815 return ret;
14de7fba
KN
1816}
1817
c95b7585
KN
1818static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
1819{
1820 struct atom_value *va, *vb;
1821 int cmp;
b072add7 1822 cmp_type cmp_type = used_atom[s->atom].type;
3bb16a8b 1823 int (*cmp_fn)(const char *, const char *);
c95b7585
KN
1824
1825 get_ref_atom_value(a, s->atom, &va);
1826 get_ref_atom_value(b, s->atom, &vb);
3bb16a8b 1827 cmp_fn = s->ignore_case ? strcasecmp : strcmp;
90c00408
KN
1828 if (s->version)
1829 cmp = versioncmp(va->s, vb->s);
1830 else if (cmp_type == FIELD_STR)
3bb16a8b 1831 cmp = cmp_fn(va->s, vb->s);
90c00408 1832 else {
c95b7585
KN
1833 if (va->ul < vb->ul)
1834 cmp = -1;
1835 else if (va->ul == vb->ul)
3bb16a8b 1836 cmp = cmp_fn(a->refname, b->refname);
c95b7585
KN
1837 else
1838 cmp = 1;
c95b7585 1839 }
90c00408 1840
c95b7585
KN
1841 return (s->reverse) ? -cmp : cmp;
1842}
1843
1844static struct ref_sorting *ref_sorting;
1845static int compare_refs(const void *a_, const void *b_)
1846{
1847 struct ref_array_item *a = *((struct ref_array_item **)a_);
1848 struct ref_array_item *b = *((struct ref_array_item **)b_);
1849 struct ref_sorting *s;
1850
1851 for (s = ref_sorting; s; s = s->next) {
1852 int cmp = cmp_ref_sorting(s, a, b);
1853 if (cmp)
1854 return cmp;
1855 }
1856 return 0;
1857}
1858
1859void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1860{
1861 ref_sorting = sorting;
9ed0d8d6 1862 QSORT(array->items, array->nr, compare_refs);
c95b7585
KN
1863}
1864
574e96a2 1865static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
c95b7585 1866{
574e96a2
KN
1867 struct strbuf *s = &state->stack->output;
1868
c95b7585
KN
1869 while (*cp && (!ep || cp < ep)) {
1870 if (*cp == '%') {
1871 if (cp[1] == '%')
1872 cp++;
1873 else {
d2330973 1874 int ch = hex2chr(cp + 1);
c95b7585 1875 if (0 <= ch) {
574e96a2 1876 strbuf_addch(s, ch);
c95b7585
KN
1877 cp += 3;
1878 continue;
1879 }
1880 }
1881 }
574e96a2 1882 strbuf_addch(s, *cp);
c95b7585
KN
1883 cp++;
1884 }
1885}
1886
99c6a71d
KN
1887void format_ref_array_item(struct ref_array_item *info, const char *format,
1888 int quote_style, struct strbuf *final_buf)
c95b7585
KN
1889{
1890 const char *cp, *sp, *ep;
574e96a2
KN
1891 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
1892
1893 state.quote_style = quote_style;
1894 push_stack_element(&state.stack);
c95b7585
KN
1895
1896 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
1897 struct atom_value *atomv;
1898
1899 ep = strchr(sp, ')');
1900 if (cp < sp)
574e96a2 1901 append_literal(cp, sp, &state);
c95b7585 1902 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
63d89fbc 1903 atomv->handler(atomv, &state);
c95b7585
KN
1904 }
1905 if (*cp) {
1906 sp = cp + strlen(cp);
574e96a2 1907 append_literal(cp, sp, &state);
c95b7585
KN
1908 }
1909 if (need_color_reset_at_eol) {
1910 struct atom_value resetv;
1911 char color[COLOR_MAXLEN] = "";
1912
1913 if (color_parse("reset", color) < 0)
1914 die("BUG: couldn't parse 'reset' as a color");
1915 resetv.s = color;
574e96a2 1916 append_atom(&resetv, &state);
c95b7585 1917 }
ce592082
KN
1918 if (state.stack->prev)
1919 die(_("format: %%(end) atom missing"));
99c6a71d 1920 strbuf_addbuf(final_buf, &state.stack->output);
574e96a2 1921 pop_stack_element(&state.stack);
99c6a71d
KN
1922}
1923
1924void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1925{
1926 struct strbuf final_buf = STRBUF_INIT;
1927
1928 format_ref_array_item(info, format, quote_style, &final_buf);
1929 fwrite(final_buf.buf, 1, final_buf.len, stdout);
1930 strbuf_release(&final_buf);
c95b7585
KN
1931 putchar('\n');
1932}
1933
1934/* If no sorting option is given, use refname to sort as default */
1935struct ref_sorting *ref_default_sorting(void)
1936{
1937 static const char cstr_name[] = "refname";
1938
1939 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
1940
1941 sorting->next = NULL;
1942 sorting->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
1943 return sorting;
1944}
1945
1946int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1947{
1948 struct ref_sorting **sorting_tail = opt->value;
1949 struct ref_sorting *s;
1950 int len;
1951
1952 if (!arg) /* should --no-sort void the list ? */
1953 return -1;
1954
1955 s = xcalloc(1, sizeof(*s));
1956 s->next = *sorting_tail;
1957 *sorting_tail = s;
1958
1959 if (*arg == '-') {
1960 s->reverse = 1;
1961 arg++;
1962 }
90c00408
KN
1963 if (skip_prefix(arg, "version:", &arg) ||
1964 skip_prefix(arg, "v:", &arg))
1965 s->version = 1;
c95b7585
KN
1966 len = strlen(arg);
1967 s->atom = parse_ref_filter_atom(arg, arg+len);
1968 return 0;
1969}
5afcb905
KN
1970
1971int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
1972{
1973 struct ref_filter *rf = opt->value;
1974 unsigned char sha1[20];
1975
1976 rf->merge = starts_with(opt->long_name, "no")
1977 ? REF_FILTER_MERGED_OMIT
1978 : REF_FILTER_MERGED_INCLUDE;
1979
1980 if (get_sha1(arg, sha1))
1981 die(_("malformed object name %s"), arg);
1982
1983 rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
1984 if (!rf->merge_commit)
1985 return opterror(opt, "must point to a commit", 0);
1986
1987 return 0;
1988}