ref-filter: implement an `align` atom
[git/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"
c95b7585
KN
14
15typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
16
17static struct {
18 const char *name;
19 cmp_type cmp_type;
20} valid_atom[] = {
21 { "refname" },
22 { "objecttype" },
23 { "objectsize", FIELD_ULONG },
24 { "objectname" },
25 { "tree" },
26 { "parent" },
27 { "numparent", FIELD_ULONG },
28 { "object" },
29 { "type" },
30 { "tag" },
31 { "author" },
32 { "authorname" },
33 { "authoremail" },
34 { "authordate", FIELD_TIME },
35 { "committer" },
36 { "committername" },
37 { "committeremail" },
38 { "committerdate", FIELD_TIME },
39 { "tagger" },
40 { "taggername" },
41 { "taggeremail" },
42 { "taggerdate", FIELD_TIME },
43 { "creator" },
44 { "creatordate", FIELD_TIME },
45 { "subject" },
46 { "body" },
47 { "contents" },
48 { "contents:subject" },
49 { "contents:body" },
50 { "contents:signature" },
51 { "upstream" },
52 { "push" },
53 { "symref" },
54 { "flag" },
55 { "HEAD" },
56 { "color" },
ce592082
KN
57 { "align" },
58 { "end" },
c95b7585
KN
59};
60
574e96a2
KN
61#define REF_FORMATTING_STATE_INIT { 0, NULL }
62
ce592082
KN
63struct align {
64 align_type position;
65 unsigned int width;
66};
67
574e96a2
KN
68struct ref_formatting_stack {
69 struct ref_formatting_stack *prev;
70 struct strbuf output;
ce592082
KN
71 void (*at_end)(struct ref_formatting_stack *stack);
72 void *at_end_data;
574e96a2
KN
73};
74
75struct ref_formatting_state {
76 int quote_style;
77 struct ref_formatting_stack *stack;
78};
79
3a25761a
KN
80struct atom_value {
81 const char *s;
ce592082
KN
82 union {
83 struct align align;
84 } u;
63d89fbc 85 void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
3a25761a
KN
86 unsigned long ul; /* used for sorting when not FIELD_STR */
87};
88
c95b7585
KN
89/*
90 * An atom is a valid field atom listed above, possibly prefixed with
91 * a "*" to denote deref_tag().
92 *
93 * We parse given format string and sort specifiers, and make a list
94 * of properties that we need to extract out of objects. ref_array_item
95 * structure will hold an array of values extracted that can be
96 * indexed with the "atom number", which is an index into this
97 * array.
98 */
99static const char **used_atom;
100static cmp_type *used_atom_type;
101static int used_atom_cnt, need_tagged, need_symref;
102static int need_color_reset_at_eol;
103
104/*
105 * Used to parse format string and sort specifiers
106 */
107int parse_ref_filter_atom(const char *atom, const char *ep)
108{
109 const char *sp;
110 int i, at;
111
112 sp = atom;
113 if (*sp == '*' && sp < ep)
114 sp++; /* deref */
115 if (ep <= sp)
116 die("malformed field name: %.*s", (int)(ep-atom), atom);
117
118 /* Do we have the atom already used elsewhere? */
119 for (i = 0; i < used_atom_cnt; i++) {
120 int len = strlen(used_atom[i]);
121 if (len == ep - atom && !memcmp(used_atom[i], atom, len))
122 return i;
123 }
124
125 /* Is the atom a valid one? */
126 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
127 int len = strlen(valid_atom[i].name);
128 /*
129 * If the atom name has a colon, strip it and everything after
130 * it off - it specifies the format for this entry, and
131 * shouldn't be used for checking against the valid_atom
132 * table.
133 */
134 const char *formatp = strchr(sp, ':');
135 if (!formatp || ep < formatp)
136 formatp = ep;
137 if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len))
138 break;
139 }
140
141 if (ARRAY_SIZE(valid_atom) <= i)
142 die("unknown field name: %.*s", (int)(ep-atom), atom);
143
144 /* Add it in, including the deref prefix */
145 at = used_atom_cnt;
146 used_atom_cnt++;
147 REALLOC_ARRAY(used_atom, used_atom_cnt);
148 REALLOC_ARRAY(used_atom_type, used_atom_cnt);
149 used_atom[at] = xmemdupz(atom, ep - atom);
150 used_atom_type[at] = valid_atom[i].cmp_type;
151 if (*atom == '*')
152 need_tagged = 1;
153 if (!strcmp(used_atom[at], "symref"))
154 need_symref = 1;
155 return at;
156}
157
63d89fbc
KN
158static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
159{
160 switch (quote_style) {
161 case QUOTE_NONE:
162 strbuf_addstr(s, str);
163 break;
164 case QUOTE_SHELL:
165 sq_quote_buf(s, str);
166 break;
167 case QUOTE_PERL:
168 perl_quote_buf(s, str);
169 break;
170 case QUOTE_PYTHON:
171 python_quote_buf(s, str);
172 break;
173 case QUOTE_TCL:
174 tcl_quote_buf(s, str);
175 break;
176 }
177}
178
179static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
180{
ce592082
KN
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);
63d89fbc
KN
191}
192
574e96a2
KN
193static void push_stack_element(struct ref_formatting_stack **stack)
194{
195 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
196
197 strbuf_init(&s->output, 0);
198 s->prev = *stack;
199 *stack = s;
200}
201
202static void pop_stack_element(struct ref_formatting_stack **stack)
203{
204 struct ref_formatting_stack *current = *stack;
205 struct ref_formatting_stack *prev = current->prev;
206
207 if (prev)
208 strbuf_addbuf(&prev->output, &current->output);
209 strbuf_release(&current->output);
210 free(current);
211 *stack = prev;
212}
213
ce592082
KN
214static 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
224static 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
234static 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
40a7551d
KN
256static int match_atom_name(const char *name, const char *atom_name, const char **val)
257{
258 const char *body;
259
260 if (!skip_prefix(name, atom_name, &body))
261 return 0; /* doesn't even begin with "atom_name" */
262 if (!body[0]) {
263 *val = NULL; /* %(atom_name) and no customization */
264 return 1;
265 }
266 if (body[0] != ':')
267 return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
268 *val = body + 1; /* "atom_name:val" */
269 return 1;
270}
271
c95b7585
KN
272/*
273 * In a format string, find the next occurrence of %(atom).
274 */
275static const char *find_next(const char *cp)
276{
277 while (*cp) {
278 if (*cp == '%') {
279 /*
280 * %( is the start of an atom;
281 * %% is a quoted per-cent.
282 */
283 if (cp[1] == '(')
284 return cp;
285 else if (cp[1] == '%')
286 cp++; /* skip over two % */
287 /* otherwise this is a singleton, literal % */
288 }
289 cp++;
290 }
291 return NULL;
292}
293
294/*
295 * Make sure the format string is well formed, and parse out
296 * the used atoms.
297 */
298int verify_ref_format(const char *format)
299{
300 const char *cp, *sp;
301
302 need_color_reset_at_eol = 0;
303 for (cp = format; *cp && (sp = find_next(cp)); ) {
304 const char *color, *ep = strchr(sp, ')');
305 int at;
306
307 if (!ep)
308 return error("malformed format string %s", sp);
309 /* sp points at "%(" and ep points at the closing ")" */
310 at = parse_ref_filter_atom(sp + 2, ep);
311 cp = ep + 1;
312
313 if (skip_prefix(used_atom[at], "color:", &color))
314 need_color_reset_at_eol = !!strcmp(color, "reset");
315 }
316 return 0;
317}
318
319/*
320 * Given an object name, read the object data and size, and return a
321 * "struct object". If the object data we are returning is also borrowed
322 * by the "struct object" representation, set *eaten as well---it is a
323 * signal from parse_object_buffer to us not to free the buffer.
324 */
325static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
326{
327 enum object_type type;
328 void *buf = read_sha1_file(sha1, &type, sz);
329
330 if (buf)
331 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
332 else
333 *obj = NULL;
334 return buf;
335}
336
337static int grab_objectname(const char *name, const unsigned char *sha1,
338 struct atom_value *v)
339{
340 if (!strcmp(name, "objectname")) {
341 char *s = xmalloc(41);
342 strcpy(s, sha1_to_hex(sha1));
343 v->s = s;
344 return 1;
345 }
346 if (!strcmp(name, "objectname:short")) {
347 v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
348 return 1;
349 }
350 return 0;
351}
352
353/* See grab_values */
354static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
355{
356 int i;
357
358 for (i = 0; i < used_atom_cnt; i++) {
359 const char *name = used_atom[i];
360 struct atom_value *v = &val[i];
361 if (!!deref != (*name == '*'))
362 continue;
363 if (deref)
364 name++;
365 if (!strcmp(name, "objecttype"))
366 v->s = typename(obj->type);
367 else if (!strcmp(name, "objectsize")) {
368 char *s = xmalloc(40);
369 sprintf(s, "%lu", sz);
370 v->ul = sz;
371 v->s = s;
372 }
373 else if (deref)
374 grab_objectname(name, obj->sha1, v);
375 }
376}
377
378/* See grab_values */
379static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
380{
381 int i;
382 struct tag *tag = (struct tag *) obj;
383
384 for (i = 0; i < used_atom_cnt; i++) {
385 const char *name = used_atom[i];
386 struct atom_value *v = &val[i];
387 if (!!deref != (*name == '*'))
388 continue;
389 if (deref)
390 name++;
391 if (!strcmp(name, "tag"))
392 v->s = tag->tag;
393 else if (!strcmp(name, "type") && tag->tagged)
394 v->s = typename(tag->tagged->type);
395 else if (!strcmp(name, "object") && tag->tagged) {
396 char *s = xmalloc(41);
397 strcpy(s, sha1_to_hex(tag->tagged->sha1));
398 v->s = s;
399 }
400 }
401}
402
403/* See grab_values */
404static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
405{
406 int i;
407 struct commit *commit = (struct commit *) obj;
408
409 for (i = 0; i < used_atom_cnt; i++) {
410 const char *name = used_atom[i];
411 struct atom_value *v = &val[i];
412 if (!!deref != (*name == '*'))
413 continue;
414 if (deref)
415 name++;
416 if (!strcmp(name, "tree")) {
417 char *s = xmalloc(41);
418 strcpy(s, sha1_to_hex(commit->tree->object.sha1));
419 v->s = s;
420 }
421 if (!strcmp(name, "numparent")) {
422 char *s = xmalloc(40);
423 v->ul = commit_list_count(commit->parents);
424 sprintf(s, "%lu", v->ul);
425 v->s = s;
426 }
427 else if (!strcmp(name, "parent")) {
428 int num = commit_list_count(commit->parents);
429 int i;
430 struct commit_list *parents;
431 char *s = xmalloc(41 * num + 1);
432 v->s = s;
433 for (i = 0, parents = commit->parents;
434 parents;
435 parents = parents->next, i = i + 41) {
436 struct commit *parent = parents->item;
437 strcpy(s+i, sha1_to_hex(parent->object.sha1));
438 if (parents->next)
439 s[i+40] = ' ';
440 }
441 if (!i)
442 *s = '\0';
443 }
444 }
445}
446
447static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
448{
449 const char *eol;
450 while (*buf) {
451 if (!strncmp(buf, who, wholen) &&
452 buf[wholen] == ' ')
453 return buf + wholen + 1;
454 eol = strchr(buf, '\n');
455 if (!eol)
456 return "";
457 eol++;
458 if (*eol == '\n')
459 return ""; /* end of header */
460 buf = eol;
461 }
462 return "";
463}
464
465static const char *copy_line(const char *buf)
466{
467 const char *eol = strchrnul(buf, '\n');
468 return xmemdupz(buf, eol - buf);
469}
470
471static const char *copy_name(const char *buf)
472{
473 const char *cp;
474 for (cp = buf; *cp && *cp != '\n'; cp++) {
475 if (!strncmp(cp, " <", 2))
476 return xmemdupz(buf, cp - buf);
477 }
478 return "";
479}
480
481static const char *copy_email(const char *buf)
482{
483 const char *email = strchr(buf, '<');
484 const char *eoemail;
485 if (!email)
486 return "";
487 eoemail = strchr(email, '>');
488 if (!eoemail)
489 return "";
490 return xmemdupz(email, eoemail + 1 - email);
491}
492
493static char *copy_subject(const char *buf, unsigned long len)
494{
495 char *r = xmemdupz(buf, len);
496 int i;
497
498 for (i = 0; i < len; i++)
499 if (r[i] == '\n')
500 r[i] = ' ';
501
502 return r;
503}
504
505static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
506{
507 const char *eoemail = strstr(buf, "> ");
508 char *zone;
509 unsigned long timestamp;
510 long tz;
d939af12 511 struct date_mode date_mode = { DATE_NORMAL };
c95b7585
KN
512 const char *formatp;
513
514 /*
515 * We got here because atomname ends in "date" or "date<something>";
516 * it's not possible that <something> is not ":<format>" because
517 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
518 * ":" means no format is specified, and use the default.
519 */
520 formatp = strchr(atomname, ':');
521 if (formatp != NULL) {
522 formatp++;
d939af12 523 parse_date_format(formatp, &date_mode);
c95b7585
KN
524 }
525
526 if (!eoemail)
527 goto bad;
528 timestamp = strtoul(eoemail + 2, &zone, 10);
529 if (timestamp == ULONG_MAX)
530 goto bad;
531 tz = strtol(zone, NULL, 10);
532 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
533 goto bad;
d939af12 534 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
c95b7585
KN
535 v->ul = timestamp;
536 return;
537 bad:
538 v->s = "";
539 v->ul = 0;
540}
541
542/* See grab_values */
543static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
544{
545 int i;
546 int wholen = strlen(who);
547 const char *wholine = NULL;
548
549 for (i = 0; i < used_atom_cnt; i++) {
550 const char *name = used_atom[i];
551 struct atom_value *v = &val[i];
552 if (!!deref != (*name == '*'))
553 continue;
554 if (deref)
555 name++;
556 if (strncmp(who, name, wholen))
557 continue;
558 if (name[wholen] != 0 &&
559 strcmp(name + wholen, "name") &&
560 strcmp(name + wholen, "email") &&
561 !starts_with(name + wholen, "date"))
562 continue;
563 if (!wholine)
564 wholine = find_wholine(who, wholen, buf, sz);
565 if (!wholine)
566 return; /* no point looking for it */
567 if (name[wholen] == 0)
568 v->s = copy_line(wholine);
569 else if (!strcmp(name + wholen, "name"))
570 v->s = copy_name(wholine);
571 else if (!strcmp(name + wholen, "email"))
572 v->s = copy_email(wholine);
573 else if (starts_with(name + wholen, "date"))
574 grab_date(wholine, v, name);
575 }
576
577 /*
578 * For a tag or a commit object, if "creator" or "creatordate" is
579 * requested, do something special.
580 */
581 if (strcmp(who, "tagger") && strcmp(who, "committer"))
582 return; /* "author" for commit object is not wanted */
583 if (!wholine)
584 wholine = find_wholine(who, wholen, buf, sz);
585 if (!wholine)
586 return;
587 for (i = 0; i < used_atom_cnt; i++) {
588 const char *name = used_atom[i];
589 struct atom_value *v = &val[i];
590 if (!!deref != (*name == '*'))
591 continue;
592 if (deref)
593 name++;
594
595 if (starts_with(name, "creatordate"))
596 grab_date(wholine, v, name);
597 else if (!strcmp(name, "creator"))
598 v->s = copy_line(wholine);
599 }
600}
601
602static void find_subpos(const char *buf, unsigned long sz,
603 const char **sub, unsigned long *sublen,
604 const char **body, unsigned long *bodylen,
605 unsigned long *nonsiglen,
606 const char **sig, unsigned long *siglen)
607{
608 const char *eol;
609 /* skip past header until we hit empty line */
610 while (*buf && *buf != '\n') {
611 eol = strchrnul(buf, '\n');
612 if (*eol)
613 eol++;
614 buf = eol;
615 }
616 /* skip any empty lines */
617 while (*buf == '\n')
618 buf++;
619
620 /* parse signature first; we might not even have a subject line */
621 *sig = buf + parse_signature(buf, strlen(buf));
622 *siglen = strlen(*sig);
623
624 /* subject is first non-empty line */
625 *sub = buf;
626 /* subject goes to first empty line */
627 while (buf < *sig && *buf && *buf != '\n') {
628 eol = strchrnul(buf, '\n');
629 if (*eol)
630 eol++;
631 buf = eol;
632 }
633 *sublen = buf - *sub;
634 /* drop trailing newline, if present */
635 if (*sublen && (*sub)[*sublen - 1] == '\n')
636 *sublen -= 1;
637
638 /* skip any empty lines */
639 while (*buf == '\n')
640 buf++;
641 *body = buf;
642 *bodylen = strlen(buf);
643 *nonsiglen = *sig - buf;
644}
645
646/* See grab_values */
647static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
648{
649 int i;
650 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
651 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
652
653 for (i = 0; i < used_atom_cnt; i++) {
654 const char *name = used_atom[i];
655 struct atom_value *v = &val[i];
656 if (!!deref != (*name == '*'))
657 continue;
658 if (deref)
659 name++;
660 if (strcmp(name, "subject") &&
661 strcmp(name, "body") &&
662 strcmp(name, "contents") &&
663 strcmp(name, "contents:subject") &&
664 strcmp(name, "contents:body") &&
665 strcmp(name, "contents:signature"))
666 continue;
667 if (!subpos)
668 find_subpos(buf, sz,
669 &subpos, &sublen,
670 &bodypos, &bodylen, &nonsiglen,
671 &sigpos, &siglen);
672
673 if (!strcmp(name, "subject"))
674 v->s = copy_subject(subpos, sublen);
675 else if (!strcmp(name, "contents:subject"))
676 v->s = copy_subject(subpos, sublen);
677 else if (!strcmp(name, "body"))
678 v->s = xmemdupz(bodypos, bodylen);
679 else if (!strcmp(name, "contents:body"))
680 v->s = xmemdupz(bodypos, nonsiglen);
681 else if (!strcmp(name, "contents:signature"))
682 v->s = xmemdupz(sigpos, siglen);
683 else if (!strcmp(name, "contents"))
684 v->s = xstrdup(subpos);
685 }
686}
687
688/*
689 * We want to have empty print-string for field requests
690 * that do not apply (e.g. "authordate" for a tag object)
691 */
692static void fill_missing_values(struct atom_value *val)
693{
694 int i;
695 for (i = 0; i < used_atom_cnt; i++) {
696 struct atom_value *v = &val[i];
697 if (v->s == NULL)
698 v->s = "";
699 }
700}
701
702/*
703 * val is a list of atom_value to hold returned values. Extract
704 * the values for atoms in used_atom array out of (obj, buf, sz).
705 * when deref is false, (obj, buf, sz) is the object that is
706 * pointed at by the ref itself; otherwise it is the object the
707 * ref (which is a tag) refers to.
708 */
709static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
710{
711 grab_common_values(val, deref, obj, buf, sz);
712 switch (obj->type) {
713 case OBJ_TAG:
714 grab_tag_values(val, deref, obj, buf, sz);
715 grab_sub_body_contents(val, deref, obj, buf, sz);
716 grab_person("tagger", val, deref, obj, buf, sz);
717 break;
718 case OBJ_COMMIT:
719 grab_commit_values(val, deref, obj, buf, sz);
720 grab_sub_body_contents(val, deref, obj, buf, sz);
721 grab_person("author", val, deref, obj, buf, sz);
722 grab_person("committer", val, deref, obj, buf, sz);
723 break;
724 case OBJ_TREE:
725 /* grab_tree_values(val, deref, obj, buf, sz); */
726 break;
727 case OBJ_BLOB:
728 /* grab_blob_values(val, deref, obj, buf, sz); */
729 break;
730 default:
731 die("Eh? Object of type %d?", obj->type);
732 }
733}
734
735static inline char *copy_advance(char *dst, const char *src)
736{
737 while (*src)
738 *dst++ = *src++;
739 return dst;
740}
741
742/*
743 * Parse the object referred by ref, and grab needed value.
744 */
745static void populate_value(struct ref_array_item *ref)
746{
747 void *buf;
748 struct object *obj;
749 int eaten, i;
750 unsigned long size;
751 const unsigned char *tagged;
752
753 ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
754
755 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
756 unsigned char unused1[20];
757 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
758 unused1, NULL);
759 if (!ref->symref)
760 ref->symref = "";
761 }
762
763 /* Fill in specials first */
764 for (i = 0; i < used_atom_cnt; i++) {
765 const char *name = used_atom[i];
766 struct atom_value *v = &ref->value[i];
767 int deref = 0;
768 const char *refname;
769 const char *formatp;
40a7551d 770 const char *valp;
c95b7585
KN
771 struct branch *branch = NULL;
772
63d89fbc
KN
773 v->handler = append_atom;
774
c95b7585
KN
775 if (*name == '*') {
776 deref = 1;
777 name++;
778 }
779
780 if (starts_with(name, "refname"))
781 refname = ref->refname;
782 else if (starts_with(name, "symref"))
783 refname = ref->symref ? ref->symref : "";
784 else if (starts_with(name, "upstream")) {
785 const char *branch_name;
786 /* only local branches may have an upstream */
787 if (!skip_prefix(ref->refname, "refs/heads/",
788 &branch_name))
789 continue;
790 branch = branch_get(branch_name);
791
792 refname = branch_get_upstream(branch, NULL);
793 if (!refname)
794 continue;
795 } else if (starts_with(name, "push")) {
796 const char *branch_name;
797 if (!skip_prefix(ref->refname, "refs/heads/",
798 &branch_name))
799 continue;
800 branch = branch_get(branch_name);
801
802 refname = branch_get_push(branch, NULL);
803 if (!refname)
804 continue;
40a7551d 805 } else if (match_atom_name(name, "color", &valp)) {
c95b7585
KN
806 char color[COLOR_MAXLEN] = "";
807
40a7551d
KN
808 if (!valp)
809 die(_("expected format: %%(color:<color>)"));
810 if (color_parse(valp, color) < 0)
c95b7585
KN
811 die(_("unable to parse format"));
812 v->s = xstrdup(color);
813 continue;
814 } else if (!strcmp(name, "flag")) {
815 char buf[256], *cp = buf;
816 if (ref->flag & REF_ISSYMREF)
817 cp = copy_advance(cp, ",symref");
818 if (ref->flag & REF_ISPACKED)
819 cp = copy_advance(cp, ",packed");
820 if (cp == buf)
821 v->s = "";
822 else {
823 *cp = '\0';
824 v->s = xstrdup(buf + 1);
825 }
826 continue;
827 } else if (!deref && grab_objectname(name, ref->objectname, v)) {
828 continue;
829 } else if (!strcmp(name, "HEAD")) {
830 const char *head;
831 unsigned char sha1[20];
832
833 head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
834 sha1, NULL);
835 if (!strcmp(ref->refname, head))
836 v->s = "*";
837 else
838 v->s = " ";
839 continue;
ce592082
KN
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;
c95b7585
KN
882 } else
883 continue;
884
885 formatp = strchr(name, ':');
886 if (formatp) {
887 int num_ours, num_theirs;
888
889 formatp++;
890 if (!strcmp(formatp, "short"))
891 refname = shorten_unambiguous_ref(refname,
892 warn_ambiguous_refs);
893 else if (!strcmp(formatp, "track") &&
894 (starts_with(name, "upstream") ||
895 starts_with(name, "push"))) {
896 char buf[40];
897
898 if (stat_tracking_info(branch, &num_ours,
899 &num_theirs, NULL))
900 continue;
901
902 if (!num_ours && !num_theirs)
903 v->s = "";
904 else if (!num_ours) {
905 sprintf(buf, "[behind %d]", num_theirs);
906 v->s = xstrdup(buf);
907 } else if (!num_theirs) {
908 sprintf(buf, "[ahead %d]", num_ours);
909 v->s = xstrdup(buf);
910 } else {
911 sprintf(buf, "[ahead %d, behind %d]",
912 num_ours, num_theirs);
913 v->s = xstrdup(buf);
914 }
915 continue;
916 } else if (!strcmp(formatp, "trackshort") &&
917 (starts_with(name, "upstream") ||
918 starts_with(name, "push"))) {
919 assert(branch);
920
921 if (stat_tracking_info(branch, &num_ours,
922 &num_theirs, NULL))
923 continue;
924
925 if (!num_ours && !num_theirs)
926 v->s = "=";
927 else if (!num_ours)
928 v->s = "<";
929 else if (!num_theirs)
930 v->s = ">";
931 else
932 v->s = "<>";
933 continue;
934 } else
935 die("unknown %.*s format %s",
936 (int)(formatp - name), name, formatp);
937 }
938
939 if (!deref)
940 v->s = refname;
941 else {
942 int len = strlen(refname);
943 char *s = xmalloc(len + 4);
944 sprintf(s, "%s^{}", refname);
945 v->s = s;
946 }
947 }
948
949 for (i = 0; i < used_atom_cnt; i++) {
950 struct atom_value *v = &ref->value[i];
951 if (v->s == NULL)
952 goto need_obj;
953 }
954 return;
955
956 need_obj:
957 buf = get_obj(ref->objectname, &obj, &size, &eaten);
958 if (!buf)
959 die("missing object %s for %s",
960 sha1_to_hex(ref->objectname), ref->refname);
961 if (!obj)
962 die("parse_object_buffer failed on %s for %s",
963 sha1_to_hex(ref->objectname), ref->refname);
964
965 grab_values(ref->value, 0, obj, buf, size);
966 if (!eaten)
967 free(buf);
968
969 /*
970 * If there is no atom that wants to know about tagged
971 * object, we are done.
972 */
973 if (!need_tagged || (obj->type != OBJ_TAG))
974 return;
975
976 /*
977 * If it is a tag object, see if we use a value that derefs
978 * the object, and if we do grab the object it refers to.
979 */
980 tagged = ((struct tag *)obj)->tagged->sha1;
981
982 /*
983 * NEEDSWORK: This derefs tag only once, which
984 * is good to deal with chains of trust, but
985 * is not consistent with what deref_tag() does
986 * which peels the onion to the core.
987 */
988 buf = get_obj(tagged, &obj, &size, &eaten);
989 if (!buf)
990 die("missing object %s for %s",
991 sha1_to_hex(tagged), ref->refname);
992 if (!obj)
993 die("parse_object_buffer failed on %s for %s",
994 sha1_to_hex(tagged), ref->refname);
995 grab_values(ref->value, 1, obj, buf, size);
996 if (!eaten)
997 free(buf);
998}
999
1000/*
1001 * Given a ref, return the value for the atom. This lazily gets value
1002 * out of the object by calling populate value.
1003 */
1004static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
1005{
1006 if (!ref->value) {
1007 populate_value(ref);
1008 fill_missing_values(ref->value);
1009 }
1010 *v = &ref->value[atom];
1011}
1012
ee2bd06b
KN
1013enum contains_result {
1014 CONTAINS_UNKNOWN = -1,
1015 CONTAINS_NO = 0,
1016 CONTAINS_YES = 1
1017};
1018
1019/*
1020 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1021 * overflows.
1022 *
1023 * At each recursion step, the stack items points to the commits whose
1024 * ancestors are to be inspected.
1025 */
1026struct contains_stack {
1027 int nr, alloc;
1028 struct contains_stack_entry {
1029 struct commit *commit;
1030 struct commit_list *parents;
1031 } *contains_stack;
1032};
1033
1034static int in_commit_list(const struct commit_list *want, struct commit *c)
1035{
1036 for (; want; want = want->next)
1037 if (!hashcmp(want->item->object.sha1, c->object.sha1))
1038 return 1;
1039 return 0;
1040}
1041
1042/*
1043 * Test whether the candidate or one of its parents is contained in the list.
1044 * Do not recurse to find out, though, but return -1 if inconclusive.
1045 */
1046static enum contains_result contains_test(struct commit *candidate,
1047 const struct commit_list *want)
1048{
1049 /* was it previously marked as containing a want commit? */
1050 if (candidate->object.flags & TMP_MARK)
1051 return 1;
1052 /* or marked as not possibly containing a want commit? */
1053 if (candidate->object.flags & UNINTERESTING)
1054 return 0;
1055 /* or are we it? */
1056 if (in_commit_list(want, candidate)) {
1057 candidate->object.flags |= TMP_MARK;
1058 return 1;
1059 }
1060
1061 if (parse_commit(candidate) < 0)
1062 return 0;
1063
1064 return -1;
1065}
1066
1067static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
1068{
1069 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
1070 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
1071 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
1072}
1073
1074static enum contains_result contains_tag_algo(struct commit *candidate,
1075 const struct commit_list *want)
1076{
1077 struct contains_stack contains_stack = { 0, 0, NULL };
1078 int result = contains_test(candidate, want);
1079
1080 if (result != CONTAINS_UNKNOWN)
1081 return result;
1082
1083 push_to_contains_stack(candidate, &contains_stack);
1084 while (contains_stack.nr) {
1085 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
1086 struct commit *commit = entry->commit;
1087 struct commit_list *parents = entry->parents;
1088
1089 if (!parents) {
1090 commit->object.flags |= UNINTERESTING;
1091 contains_stack.nr--;
1092 }
1093 /*
1094 * If we just popped the stack, parents->item has been marked,
1095 * therefore contains_test will return a meaningful 0 or 1.
1096 */
1097 else switch (contains_test(parents->item, want)) {
1098 case CONTAINS_YES:
1099 commit->object.flags |= TMP_MARK;
1100 contains_stack.nr--;
1101 break;
1102 case CONTAINS_NO:
1103 entry->parents = parents->next;
1104 break;
1105 case CONTAINS_UNKNOWN:
1106 push_to_contains_stack(parents->item, &contains_stack);
1107 break;
1108 }
1109 }
1110 free(contains_stack.contains_stack);
1111 return contains_test(candidate, want);
1112}
1113
1114static int commit_contains(struct ref_filter *filter, struct commit *commit)
1115{
1116 if (filter->with_commit_tag_algo)
1117 return contains_tag_algo(commit, filter->with_commit);
1118 return is_descendant_of(commit, filter->with_commit);
1119}
1120
c95b7585
KN
1121/*
1122 * Return 1 if the refname matches one of the patterns, otherwise 0.
1123 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1124 * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
1125 * matches "refs/heads/m*",too).
1126 */
1127static int match_name_as_path(const char **pattern, const char *refname)
1128{
1129 int namelen = strlen(refname);
1130 for (; *pattern; pattern++) {
1131 const char *p = *pattern;
1132 int plen = strlen(p);
1133
1134 if ((plen <= namelen) &&
1135 !strncmp(refname, p, plen) &&
1136 (refname[plen] == '\0' ||
1137 refname[plen] == '/' ||
1138 p[plen-1] == '/'))
1139 return 1;
1140 if (!wildmatch(p, refname, WM_PATHNAME, NULL))
1141 return 1;
1142 }
1143 return 0;
1144}
1145
68411046
KN
1146/*
1147 * Given a ref (sha1, refname), check if the ref belongs to the array
1148 * of sha1s. If the given ref is a tag, check if the given tag points
1149 * at one of the sha1s in the given sha1 array.
1150 * the given sha1_array.
1151 * NEEDSWORK:
1152 * 1. Only a single level of inderection is obtained, we might want to
1153 * change this to account for multiple levels (e.g. annotated tags
1154 * pointing to annotated tags pointing to a commit.)
1155 * 2. As the refs are cached we might know what refname peels to without
1156 * the need to parse the object via parse_object(). peel_ref() might be a
1157 * more efficient alternative to obtain the pointee.
1158 */
1159static const unsigned char *match_points_at(struct sha1_array *points_at,
1160 const unsigned char *sha1,
1161 const char *refname)
1162{
1163 const unsigned char *tagged_sha1 = NULL;
1164 struct object *obj;
1165
1166 if (sha1_array_lookup(points_at, sha1) >= 0)
1167 return sha1;
1168 obj = parse_object(sha1);
1169 if (!obj)
1170 die(_("malformed object at '%s'"), refname);
1171 if (obj->type == OBJ_TAG)
1172 tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
1173 if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
1174 return tagged_sha1;
1175 return NULL;
1176}
1177
c95b7585
KN
1178/* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1179static struct ref_array_item *new_ref_array_item(const char *refname,
1180 const unsigned char *objectname,
1181 int flag)
1182{
1958a6eb
KN
1183 size_t len = strlen(refname);
1184 struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
1185 memcpy(ref->refname, refname, len);
1186 ref->refname[len] = '\0';
c95b7585
KN
1187 hashcpy(ref->objectname, objectname);
1188 ref->flag = flag;
1189
1190 return ref;
1191}
1192
1193/*
1194 * A call-back given to for_each_ref(). Filter refs and keep them for
1195 * later object processing.
1196 */
14de7fba 1197static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
c95b7585
KN
1198{
1199 struct ref_filter_cbdata *ref_cbdata = cb_data;
14de7fba 1200 struct ref_filter *filter = ref_cbdata->filter;
c95b7585 1201 struct ref_array_item *ref;
35257aa0 1202 struct commit *commit = NULL;
c95b7585
KN
1203
1204 if (flag & REF_BAD_NAME) {
1205 warning("ignoring ref with broken name %s", refname);
1206 return 0;
1207 }
1208
7ebc8cbe
JH
1209 if (flag & REF_ISBROKEN) {
1210 warning("ignoring broken ref %s", refname);
1211 return 0;
1212 }
1213
c95b7585
KN
1214 if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
1215 return 0;
1216
68411046
KN
1217 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
1218 return 0;
1219
35257aa0
KN
1220 /*
1221 * A merge filter is applied on refs pointing to commits. Hence
1222 * obtain the commit using the 'oid' available and discard all
1223 * non-commits early. The actual filtering is done later.
1224 */
ee2bd06b 1225 if (filter->merge_commit || filter->with_commit) {
35257aa0
KN
1226 commit = lookup_commit_reference_gently(oid->hash, 1);
1227 if (!commit)
1228 return 0;
ee2bd06b
KN
1229 /* We perform the filtering for the '--contains' option */
1230 if (filter->with_commit &&
1231 !commit_contains(filter, commit))
1232 return 0;
35257aa0
KN
1233 }
1234
c95b7585
KN
1235 /*
1236 * We do not open the object yet; sort may only need refname
1237 * to do its job and the resulting list may yet to be pruned
1238 * by maxcount logic.
1239 */
1240 ref = new_ref_array_item(refname, oid->hash, flag);
35257aa0 1241 ref->commit = commit;
c95b7585 1242
14de7fba
KN
1243 REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1244 ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
c95b7585
KN
1245 return 0;
1246}
1247
1248/* Free memory allocated for a ref_array_item */
1249static void free_array_item(struct ref_array_item *item)
1250{
1251 free((char *)item->symref);
c95b7585
KN
1252 free(item);
1253}
1254
1255/* Free all memory allocated for ref_array */
1256void ref_array_clear(struct ref_array *array)
1257{
1258 int i;
1259
1260 for (i = 0; i < array->nr; i++)
1261 free_array_item(array->items[i]);
1262 free(array->items);
1263 array->items = NULL;
1264 array->nr = array->alloc = 0;
1265}
1266
35257aa0
KN
1267static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
1268{
1269 struct rev_info revs;
1270 int i, old_nr;
1271 struct ref_filter *filter = ref_cbdata->filter;
1272 struct ref_array *array = ref_cbdata->array;
1273 struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
1274
1275 init_revisions(&revs, NULL);
1276
1277 for (i = 0; i < array->nr; i++) {
1278 struct ref_array_item *item = array->items[i];
1279 add_pending_object(&revs, &item->commit->object, item->refname);
1280 to_clear[i] = item->commit;
1281 }
1282
1283 filter->merge_commit->object.flags |= UNINTERESTING;
1284 add_pending_object(&revs, &filter->merge_commit->object, "");
1285
1286 revs.limited = 1;
1287 if (prepare_revision_walk(&revs))
1288 die(_("revision walk setup failed"));
1289
1290 old_nr = array->nr;
1291 array->nr = 0;
1292
1293 for (i = 0; i < old_nr; i++) {
1294 struct ref_array_item *item = array->items[i];
1295 struct commit *commit = item->commit;
1296
1297 int is_merged = !!(commit->object.flags & UNINTERESTING);
1298
1299 if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
1300 array->items[array->nr++] = array->items[i];
1301 else
1302 free_array_item(item);
1303 }
1304
1305 for (i = 0; i < old_nr; i++)
1306 clear_commit_marks(to_clear[i], ALL_REV_FLAGS);
1307 clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
1308 free(to_clear);
1309}
1310
14de7fba
KN
1311/*
1312 * API for filtering a set of refs. Based on the type of refs the user
1313 * has requested, we iterate through those refs and apply filters
1314 * as per the given ref_filter structure and finally store the
1315 * filtered refs in the ref_array structure.
1316 */
1317int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
1318{
1319 struct ref_filter_cbdata ref_cbdata;
35257aa0 1320 int ret = 0;
14de7fba
KN
1321
1322 ref_cbdata.array = array;
1323 ref_cbdata.filter = filter;
1324
35257aa0 1325 /* Simple per-ref filtering */
14de7fba 1326 if (type & (FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN))
35257aa0 1327 ret = for_each_rawref(ref_filter_handler, &ref_cbdata);
14de7fba 1328 else if (type & FILTER_REFS_ALL)
35257aa0
KN
1329 ret = for_each_ref(ref_filter_handler, &ref_cbdata);
1330 else if (type)
14de7fba 1331 die("filter_refs: invalid type");
35257aa0
KN
1332
1333 /* Filters that need revision walking */
1334 if (filter->merge_commit)
1335 do_merge_filter(&ref_cbdata);
1336
1337 return ret;
14de7fba
KN
1338}
1339
c95b7585
KN
1340static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
1341{
1342 struct atom_value *va, *vb;
1343 int cmp;
1344 cmp_type cmp_type = used_atom_type[s->atom];
1345
1346 get_ref_atom_value(a, s->atom, &va);
1347 get_ref_atom_value(b, s->atom, &vb);
1348 switch (cmp_type) {
1349 case FIELD_STR:
1350 cmp = strcmp(va->s, vb->s);
1351 break;
1352 default:
1353 if (va->ul < vb->ul)
1354 cmp = -1;
1355 else if (va->ul == vb->ul)
1356 cmp = 0;
1357 else
1358 cmp = 1;
1359 break;
1360 }
1361 return (s->reverse) ? -cmp : cmp;
1362}
1363
1364static struct ref_sorting *ref_sorting;
1365static int compare_refs(const void *a_, const void *b_)
1366{
1367 struct ref_array_item *a = *((struct ref_array_item **)a_);
1368 struct ref_array_item *b = *((struct ref_array_item **)b_);
1369 struct ref_sorting *s;
1370
1371 for (s = ref_sorting; s; s = s->next) {
1372 int cmp = cmp_ref_sorting(s, a, b);
1373 if (cmp)
1374 return cmp;
1375 }
1376 return 0;
1377}
1378
1379void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1380{
1381 ref_sorting = sorting;
1382 qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
1383}
1384
c95b7585
KN
1385static int hex1(char ch)
1386{
1387 if ('0' <= ch && ch <= '9')
1388 return ch - '0';
1389 else if ('a' <= ch && ch <= 'f')
1390 return ch - 'a' + 10;
1391 else if ('A' <= ch && ch <= 'F')
1392 return ch - 'A' + 10;
1393 return -1;
1394}
1395static int hex2(const char *cp)
1396{
1397 if (cp[0] && cp[1])
1398 return (hex1(cp[0]) << 4) | hex1(cp[1]);
1399 else
1400 return -1;
1401}
1402
574e96a2 1403static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
c95b7585 1404{
574e96a2
KN
1405 struct strbuf *s = &state->stack->output;
1406
c95b7585
KN
1407 while (*cp && (!ep || cp < ep)) {
1408 if (*cp == '%') {
1409 if (cp[1] == '%')
1410 cp++;
1411 else {
1412 int ch = hex2(cp + 1);
1413 if (0 <= ch) {
574e96a2 1414 strbuf_addch(s, ch);
c95b7585
KN
1415 cp += 3;
1416 continue;
1417 }
1418 }
1419 }
574e96a2 1420 strbuf_addch(s, *cp);
c95b7585
KN
1421 cp++;
1422 }
1423}
1424
1425void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1426{
1427 const char *cp, *sp, *ep;
574e96a2
KN
1428 struct strbuf *final_buf;
1429 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
1430
1431 state.quote_style = quote_style;
1432 push_stack_element(&state.stack);
c95b7585
KN
1433
1434 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
1435 struct atom_value *atomv;
1436
1437 ep = strchr(sp, ')');
1438 if (cp < sp)
574e96a2 1439 append_literal(cp, sp, &state);
c95b7585 1440 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
63d89fbc 1441 atomv->handler(atomv, &state);
c95b7585
KN
1442 }
1443 if (*cp) {
1444 sp = cp + strlen(cp);
574e96a2 1445 append_literal(cp, sp, &state);
c95b7585
KN
1446 }
1447 if (need_color_reset_at_eol) {
1448 struct atom_value resetv;
1449 char color[COLOR_MAXLEN] = "";
1450
1451 if (color_parse("reset", color) < 0)
1452 die("BUG: couldn't parse 'reset' as a color");
1453 resetv.s = color;
574e96a2 1454 append_atom(&resetv, &state);
c95b7585 1455 }
ce592082
KN
1456 if (state.stack->prev)
1457 die(_("format: %%(end) atom missing"));
574e96a2
KN
1458 final_buf = &state.stack->output;
1459 fwrite(final_buf->buf, 1, final_buf->len, stdout);
1460 pop_stack_element(&state.stack);
c95b7585
KN
1461 putchar('\n');
1462}
1463
1464/* If no sorting option is given, use refname to sort as default */
1465struct ref_sorting *ref_default_sorting(void)
1466{
1467 static const char cstr_name[] = "refname";
1468
1469 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
1470
1471 sorting->next = NULL;
1472 sorting->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
1473 return sorting;
1474}
1475
1476int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1477{
1478 struct ref_sorting **sorting_tail = opt->value;
1479 struct ref_sorting *s;
1480 int len;
1481
1482 if (!arg) /* should --no-sort void the list ? */
1483 return -1;
1484
1485 s = xcalloc(1, sizeof(*s));
1486 s->next = *sorting_tail;
1487 *sorting_tail = s;
1488
1489 if (*arg == '-') {
1490 s->reverse = 1;
1491 arg++;
1492 }
1493 len = strlen(arg);
1494 s->atom = parse_ref_filter_atom(arg, arg+len);
1495 return 0;
1496}
5afcb905
KN
1497
1498int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
1499{
1500 struct ref_filter *rf = opt->value;
1501 unsigned char sha1[20];
1502
1503 rf->merge = starts_with(opt->long_name, "no")
1504 ? REF_FILTER_MERGED_OMIT
1505 : REF_FILTER_MERGED_INCLUDE;
1506
1507 if (get_sha1(arg, sha1))
1508 die(_("malformed object name %s"), arg);
1509
1510 rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
1511 if (!rf->merge_commit)
1512 return opterror(opt, "must point to a commit", 0);
1513
1514 return 0;
1515}