3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
18 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
26 unsigned int then_atom_seen : 1,
28 condition_satisfied : 1;
32 * An atom is a valid field atom listed below, possibly prefixed with
33 * a "*" to denote deref_tag().
35 * We parse given format string and sort specifiers, and make a list
36 * of properties that we need to extract out of objects. ref_array_item
37 * structure will hold an array of values extracted that can be
38 * indexed with the "atom number", which is an index into this
41 static struct used_atom {
45 char color[COLOR_MAXLEN];
47 enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT }
50 enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
53 enum { O_FULL, O_SHORT } objectname;
56 static int used_atom_cnt, need_tagged, need_symref;
57 static int need_color_reset_at_eol;
59 static void color_atom_parser(struct used_atom *atom, const char *color_value)
62 die(_("expected format: %%(color:<color>)"));
63 if (color_parse(color_value, atom->u.color) < 0)
64 die(_("unrecognized color: %%(color:%s)"), color_value);
67 static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
70 atom->u.remote_ref = RR_NORMAL;
71 else if (!strcmp(arg, "short"))
72 atom->u.remote_ref = RR_SHORTEN;
73 else if (!strcmp(arg, "track"))
74 atom->u.remote_ref = RR_TRACK;
75 else if (!strcmp(arg, "trackshort"))
76 atom->u.remote_ref = RR_TRACKSHORT;
78 die(_("unrecognized format: %%(%s)"), atom->name);
81 static void body_atom_parser(struct used_atom *atom, const char *arg)
84 die(_("%%(body) does not take arguments"));
85 atom->u.contents.option = C_BODY_DEP;
88 static void subject_atom_parser(struct used_atom *atom, const char *arg)
91 die(_("%%(subject) does not take arguments"));
92 atom->u.contents.option = C_SUB;
95 static void trailers_atom_parser(struct used_atom *atom, const char *arg)
98 die(_("%%(trailers) does not take arguments"));
99 atom->u.contents.option = C_TRAILERS;
102 static void contents_atom_parser(struct used_atom *atom, const char *arg)
105 atom->u.contents.option = C_BARE;
106 else if (!strcmp(arg, "body"))
107 atom->u.contents.option = C_BODY;
108 else if (!strcmp(arg, "signature"))
109 atom->u.contents.option = C_SIG;
110 else if (!strcmp(arg, "subject"))
111 atom->u.contents.option = C_SUB;
112 else if (!strcmp(arg, "trailers"))
113 atom->u.contents.option = C_TRAILERS;
114 else if (skip_prefix(arg, "lines=", &arg)) {
115 atom->u.contents.option = C_LINES;
116 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
117 die(_("positive value expected contents:lines=%s"), arg);
119 die(_("unrecognized %%(contents) argument: %s"), arg);
122 static void objectname_atom_parser(struct used_atom *atom, const char *arg)
125 atom->u.objectname = O_FULL;
126 else if (!strcmp(arg, "short"))
127 atom->u.objectname = O_SHORT;
129 die(_("unrecognized %%(objectname) argument: %s"), arg);
132 static align_type parse_align_position(const char *s)
134 if (!strcmp(s, "right"))
136 else if (!strcmp(s, "middle"))
138 else if (!strcmp(s, "left"))
143 static void align_atom_parser(struct used_atom *atom, const char *arg)
145 struct align *align = &atom->u.align;
146 struct string_list params = STRING_LIST_INIT_DUP;
148 unsigned int width = ~0U;
151 die(_("expected format: %%(align:<width>,<position>)"));
153 align->position = ALIGN_LEFT;
155 string_list_split(¶ms, arg, ',', -1);
156 for (i = 0; i < params.nr; i++) {
157 const char *s = params.items[i].string;
160 if (skip_prefix(s, "position=", &s)) {
161 position = parse_align_position(s);
163 die(_("unrecognized position:%s"), s);
164 align->position = position;
165 } else if (skip_prefix(s, "width=", &s)) {
166 if (strtoul_ui(s, 10, &width))
167 die(_("unrecognized width:%s"), s);
168 } else if (!strtoul_ui(s, 10, &width))
170 else if ((position = parse_align_position(s)) >= 0)
171 align->position = position;
173 die(_("unrecognized %%(align) argument: %s"), s);
177 die(_("positive width expected with the %%(align) atom"));
178 align->width = width;
179 string_list_clear(¶ms, 0);
185 void (*parser)(struct used_atom *atom, const char *arg);
189 { "objectsize", FIELD_ULONG },
190 { "objectname", FIELD_STR, objectname_atom_parser },
193 { "numparent", FIELD_ULONG },
200 { "authordate", FIELD_TIME },
203 { "committeremail" },
204 { "committerdate", FIELD_TIME },
208 { "taggerdate", FIELD_TIME },
210 { "creatordate", FIELD_TIME },
211 { "subject", FIELD_STR, subject_atom_parser },
212 { "body", FIELD_STR, body_atom_parser },
213 { "trailers", FIELD_STR, trailers_atom_parser },
214 { "contents", FIELD_STR, contents_atom_parser },
215 { "upstream", FIELD_STR, remote_ref_atom_parser },
216 { "push", FIELD_STR, remote_ref_atom_parser },
220 { "color", FIELD_STR, color_atom_parser },
221 { "align", FIELD_STR, align_atom_parser },
228 #define REF_FORMATTING_STATE_INIT { 0, NULL }
230 struct ref_formatting_stack {
231 struct ref_formatting_stack *prev;
232 struct strbuf output;
233 void (*at_end)(struct ref_formatting_stack **stack);
237 struct ref_formatting_state {
239 struct ref_formatting_stack *stack;
244 void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
245 unsigned long ul; /* used for sorting when not FIELD_STR */
246 struct used_atom *atom;
250 * Used to parse format string and sort specifiers
252 int parse_ref_filter_atom(const char *atom, const char *ep)
259 if (*sp == '*' && sp < ep)
262 die(_("malformed field name: %.*s"), (int)(ep-atom), atom);
264 /* Do we have the atom already used elsewhere? */
265 for (i = 0; i < used_atom_cnt; i++) {
266 int len = strlen(used_atom[i].name);
267 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
272 * If the atom name has a colon, strip it and everything after
273 * it off - it specifies the format for this entry, and
274 * shouldn't be used for checking against the valid_atom
277 arg = memchr(sp, ':', ep - sp);
278 atom_len = (arg ? arg : ep) - sp;
280 /* Is the atom a valid one? */
281 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
282 int len = strlen(valid_atom[i].name);
283 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
287 if (ARRAY_SIZE(valid_atom) <= i)
288 die(_("unknown field name: %.*s"), (int)(ep-atom), atom);
290 /* Add it in, including the deref prefix */
293 REALLOC_ARRAY(used_atom, used_atom_cnt);
294 used_atom[at].name = xmemdupz(atom, ep - atom);
295 used_atom[at].type = valid_atom[i].cmp_type;
297 arg = used_atom[at].name + (arg - atom) + 1;
298 memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
299 if (valid_atom[i].parser)
300 valid_atom[i].parser(&used_atom[at], arg);
303 if (!strcmp(used_atom[at].name, "symref"))
308 static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
310 switch (quote_style) {
312 strbuf_addstr(s, str);
315 sq_quote_buf(s, str);
318 perl_quote_buf(s, str);
321 python_quote_buf(s, str);
324 tcl_quote_buf(s, str);
329 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
332 * Quote formatting is only done when the stack has a single
333 * element. Otherwise quote formatting is done on the
334 * element's entire output strbuf when the %(end) atom is
337 if (!state->stack->prev)
338 quote_formatting(&state->stack->output, v->s, state->quote_style);
340 strbuf_addstr(&state->stack->output, v->s);
343 static void push_stack_element(struct ref_formatting_stack **stack)
345 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
347 strbuf_init(&s->output, 0);
352 static void pop_stack_element(struct ref_formatting_stack **stack)
354 struct ref_formatting_stack *current = *stack;
355 struct ref_formatting_stack *prev = current->prev;
358 strbuf_addbuf(&prev->output, ¤t->output);
359 strbuf_release(¤t->output);
364 static void end_align_handler(struct ref_formatting_stack **stack)
366 struct ref_formatting_stack *cur = *stack;
367 struct align *align = (struct align *)cur->at_end_data;
368 struct strbuf s = STRBUF_INIT;
370 strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
371 strbuf_swap(&cur->output, &s);
375 static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
377 struct ref_formatting_stack *new;
379 push_stack_element(&state->stack);
381 new->at_end = end_align_handler;
382 new->at_end_data = &atomv->atom->u.align;
385 static void if_then_else_handler(struct ref_formatting_stack **stack)
387 struct ref_formatting_stack *cur = *stack;
388 struct ref_formatting_stack *prev = cur->prev;
389 struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
391 if (!if_then_else->then_atom_seen)
392 die(_("format: %%(if) atom used without a %%(then) atom"));
394 if (if_then_else->else_atom_seen) {
396 * There is an %(else) atom: we need to drop one state from the
397 * stack, either the %(else) branch if the condition is satisfied, or
398 * the %(then) branch if it isn't.
400 if (if_then_else->condition_satisfied) {
401 strbuf_reset(&cur->output);
402 pop_stack_element(&cur);
404 strbuf_swap(&cur->output, &prev->output);
405 strbuf_reset(&cur->output);
406 pop_stack_element(&cur);
408 } else if (!if_then_else->condition_satisfied) {
410 * No %(else) atom: just drop the %(then) branch if the
411 * condition is not satisfied.
413 strbuf_reset(&cur->output);
420 static void if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
422 struct ref_formatting_stack *new;
423 struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
425 push_stack_element(&state->stack);
427 new->at_end = if_then_else_handler;
428 new->at_end_data = if_then_else;
431 static int is_empty(const char *s)
441 static void then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
443 struct ref_formatting_stack *cur = state->stack;
444 struct if_then_else *if_then_else = NULL;
446 if (cur->at_end == if_then_else_handler)
447 if_then_else = (struct if_then_else *)cur->at_end_data;
449 die(_("format: %%(then) atom used without an %%(if) atom"));
450 if (if_then_else->then_atom_seen)
451 die(_("format: %%(then) atom used more than once"));
452 if (if_then_else->else_atom_seen)
453 die(_("format: %%(then) atom used after %%(else)"));
454 if_then_else->then_atom_seen = 1;
456 * If there exists non-empty string between the 'if' and
457 * 'then' atom then the 'if' condition is satisfied.
459 if (cur->output.len && !is_empty(cur->output.buf))
460 if_then_else->condition_satisfied = 1;
461 strbuf_reset(&cur->output);
464 static void else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
466 struct ref_formatting_stack *prev = state->stack;
467 struct if_then_else *if_then_else = NULL;
469 if (prev->at_end == if_then_else_handler)
470 if_then_else = (struct if_then_else *)prev->at_end_data;
472 die(_("format: %%(else) atom used without an %%(if) atom"));
473 if (!if_then_else->then_atom_seen)
474 die(_("format: %%(else) atom used without a %%(then) atom"));
475 if (if_then_else->else_atom_seen)
476 die(_("format: %%(else) atom used more than once"));
477 if_then_else->else_atom_seen = 1;
478 push_stack_element(&state->stack);
479 state->stack->at_end_data = prev->at_end_data;
480 state->stack->at_end = prev->at_end;
483 static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
485 struct ref_formatting_stack *current = state->stack;
486 struct strbuf s = STRBUF_INIT;
488 if (!current->at_end)
489 die(_("format: %%(end) atom used without corresponding atom"));
490 current->at_end(&state->stack);
492 /* Stack may have been popped within at_end(), hence reset the current pointer */
493 current = state->stack;
496 * Perform quote formatting when the stack element is that of
497 * a supporting atom. If nested then perform quote formatting
498 * only on the topmost supporting atom.
500 if (!current->prev->prev) {
501 quote_formatting(&s, current->output.buf, state->quote_style);
502 strbuf_swap(¤t->output, &s);
505 pop_stack_element(&state->stack);
509 * In a format string, find the next occurrence of %(atom).
511 static const char *find_next(const char *cp)
516 * %( is the start of an atom;
517 * %% is a quoted per-cent.
521 else if (cp[1] == '%')
522 cp++; /* skip over two % */
523 /* otherwise this is a singleton, literal % */
531 * Make sure the format string is well formed, and parse out
534 int verify_ref_format(const char *format)
538 need_color_reset_at_eol = 0;
539 for (cp = format; *cp && (sp = find_next(cp)); ) {
540 const char *color, *ep = strchr(sp, ')');
544 return error(_("malformed format string %s"), sp);
545 /* sp points at "%(" and ep points at the closing ")" */
546 at = parse_ref_filter_atom(sp + 2, ep);
549 if (skip_prefix(used_atom[at].name, "color:", &color))
550 need_color_reset_at_eol = !!strcmp(color, "reset");
556 * Given an object name, read the object data and size, and return a
557 * "struct object". If the object data we are returning is also borrowed
558 * by the "struct object" representation, set *eaten as well---it is a
559 * signal from parse_object_buffer to us not to free the buffer.
561 static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
563 enum object_type type;
564 void *buf = read_sha1_file(sha1, &type, sz);
567 *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
573 static int grab_objectname(const char *name, const unsigned char *sha1,
574 struct atom_value *v, struct used_atom *atom)
576 if (starts_with(name, "objectname")) {
577 if (atom->u.objectname == O_SHORT) {
578 v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
580 } else if (atom->u.objectname == O_FULL) {
581 v->s = xstrdup(sha1_to_hex(sha1));
584 die("BUG: unknown %%(objectname) option");
589 /* See grab_values */
590 static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
594 for (i = 0; i < used_atom_cnt; i++) {
595 const char *name = used_atom[i].name;
596 struct atom_value *v = &val[i];
597 if (!!deref != (*name == '*'))
601 if (!strcmp(name, "objecttype"))
602 v->s = typename(obj->type);
603 else if (!strcmp(name, "objectsize")) {
605 v->s = xstrfmt("%lu", sz);
608 grab_objectname(name, obj->oid.hash, v, &used_atom[i]);
612 /* See grab_values */
613 static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
616 struct tag *tag = (struct tag *) obj;
618 for (i = 0; i < used_atom_cnt; i++) {
619 const char *name = used_atom[i].name;
620 struct atom_value *v = &val[i];
621 if (!!deref != (*name == '*'))
625 if (!strcmp(name, "tag"))
627 else if (!strcmp(name, "type") && tag->tagged)
628 v->s = typename(tag->tagged->type);
629 else if (!strcmp(name, "object") && tag->tagged)
630 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
634 /* See grab_values */
635 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
638 struct commit *commit = (struct commit *) obj;
640 for (i = 0; i < used_atom_cnt; i++) {
641 const char *name = used_atom[i].name;
642 struct atom_value *v = &val[i];
643 if (!!deref != (*name == '*'))
647 if (!strcmp(name, "tree")) {
648 v->s = xstrdup(oid_to_hex(&commit->tree->object.oid));
650 else if (!strcmp(name, "numparent")) {
651 v->ul = commit_list_count(commit->parents);
652 v->s = xstrfmt("%lu", v->ul);
654 else if (!strcmp(name, "parent")) {
655 struct commit_list *parents;
656 struct strbuf s = STRBUF_INIT;
657 for (parents = commit->parents; parents; parents = parents->next) {
658 struct commit *parent = parents->item;
659 if (parents != commit->parents)
660 strbuf_addch(&s, ' ');
661 strbuf_addstr(&s, oid_to_hex(&parent->object.oid));
663 v->s = strbuf_detach(&s, NULL);
668 static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
672 if (!strncmp(buf, who, wholen) &&
674 return buf + wholen + 1;
675 eol = strchr(buf, '\n');
680 return ""; /* end of header */
686 static const char *copy_line(const char *buf)
688 const char *eol = strchrnul(buf, '\n');
689 return xmemdupz(buf, eol - buf);
692 static const char *copy_name(const char *buf)
695 for (cp = buf; *cp && *cp != '\n'; cp++) {
696 if (!strncmp(cp, " <", 2))
697 return xmemdupz(buf, cp - buf);
702 static const char *copy_email(const char *buf)
704 const char *email = strchr(buf, '<');
708 eoemail = strchr(email, '>');
711 return xmemdupz(email, eoemail + 1 - email);
714 static char *copy_subject(const char *buf, unsigned long len)
716 char *r = xmemdupz(buf, len);
719 for (i = 0; i < len; i++)
726 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
728 const char *eoemail = strstr(buf, "> ");
730 unsigned long timestamp;
732 struct date_mode date_mode = { DATE_NORMAL };
736 * We got here because atomname ends in "date" or "date<something>";
737 * it's not possible that <something> is not ":<format>" because
738 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
739 * ":" means no format is specified, and use the default.
741 formatp = strchr(atomname, ':');
742 if (formatp != NULL) {
744 parse_date_format(formatp, &date_mode);
749 timestamp = strtoul(eoemail + 2, &zone, 10);
750 if (timestamp == ULONG_MAX)
752 tz = strtol(zone, NULL, 10);
753 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
755 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
763 /* See grab_values */
764 static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
767 int wholen = strlen(who);
768 const char *wholine = NULL;
770 for (i = 0; i < used_atom_cnt; i++) {
771 const char *name = used_atom[i].name;
772 struct atom_value *v = &val[i];
773 if (!!deref != (*name == '*'))
777 if (strncmp(who, name, wholen))
779 if (name[wholen] != 0 &&
780 strcmp(name + wholen, "name") &&
781 strcmp(name + wholen, "email") &&
782 !starts_with(name + wholen, "date"))
785 wholine = find_wholine(who, wholen, buf, sz);
787 return; /* no point looking for it */
788 if (name[wholen] == 0)
789 v->s = copy_line(wholine);
790 else if (!strcmp(name + wholen, "name"))
791 v->s = copy_name(wholine);
792 else if (!strcmp(name + wholen, "email"))
793 v->s = copy_email(wholine);
794 else if (starts_with(name + wholen, "date"))
795 grab_date(wholine, v, name);
799 * For a tag or a commit object, if "creator" or "creatordate" is
800 * requested, do something special.
802 if (strcmp(who, "tagger") && strcmp(who, "committer"))
803 return; /* "author" for commit object is not wanted */
805 wholine = find_wholine(who, wholen, buf, sz);
808 for (i = 0; i < used_atom_cnt; i++) {
809 const char *name = used_atom[i].name;
810 struct atom_value *v = &val[i];
811 if (!!deref != (*name == '*'))
816 if (starts_with(name, "creatordate"))
817 grab_date(wholine, v, name);
818 else if (!strcmp(name, "creator"))
819 v->s = copy_line(wholine);
823 static void find_subpos(const char *buf, unsigned long sz,
824 const char **sub, unsigned long *sublen,
825 const char **body, unsigned long *bodylen,
826 unsigned long *nonsiglen,
827 const char **sig, unsigned long *siglen)
830 /* skip past header until we hit empty line */
831 while (*buf && *buf != '\n') {
832 eol = strchrnul(buf, '\n');
837 /* skip any empty lines */
841 /* parse signature first; we might not even have a subject line */
842 *sig = buf + parse_signature(buf, strlen(buf));
843 *siglen = strlen(*sig);
845 /* subject is first non-empty line */
847 /* subject goes to first empty line */
848 while (buf < *sig && *buf && *buf != '\n') {
849 eol = strchrnul(buf, '\n');
854 *sublen = buf - *sub;
855 /* drop trailing newline, if present */
856 if (*sublen && (*sub)[*sublen - 1] == '\n')
859 /* skip any empty lines */
863 *bodylen = strlen(buf);
864 *nonsiglen = *sig - buf;
868 * If 'lines' is greater than 0, append that many lines from the given
869 * 'buf' of length 'size' to the given strbuf.
871 static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
874 const char *sp, *eol;
879 for (i = 0; i < lines && sp < buf + size; i++) {
881 strbuf_addstr(out, "\n ");
882 eol = memchr(sp, '\n', size - (sp - buf));
883 len = eol ? eol - sp : size - (sp - buf);
884 strbuf_add(out, sp, len);
891 /* See grab_values */
892 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
895 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
896 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
898 for (i = 0; i < used_atom_cnt; i++) {
899 struct used_atom *atom = &used_atom[i];
900 const char *name = atom->name;
901 struct atom_value *v = &val[i];
902 if (!!deref != (*name == '*'))
906 if (strcmp(name, "subject") &&
907 strcmp(name, "body") &&
908 strcmp(name, "trailers") &&
909 !starts_with(name, "contents"))
914 &bodypos, &bodylen, &nonsiglen,
917 if (atom->u.contents.option == C_SUB)
918 v->s = copy_subject(subpos, sublen);
919 else if (atom->u.contents.option == C_BODY_DEP)
920 v->s = xmemdupz(bodypos, bodylen);
921 else if (atom->u.contents.option == C_BODY)
922 v->s = xmemdupz(bodypos, nonsiglen);
923 else if (atom->u.contents.option == C_SIG)
924 v->s = xmemdupz(sigpos, siglen);
925 else if (atom->u.contents.option == C_LINES) {
926 struct strbuf s = STRBUF_INIT;
927 const char *contents_end = bodylen + bodypos - siglen;
929 /* Size is the length of the message after removing the signature */
930 append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
931 v->s = strbuf_detach(&s, NULL);
932 } else if (atom->u.contents.option == C_TRAILERS) {
933 struct trailer_info info;
935 /* Search for trailer info */
936 trailer_info_get(&info, subpos);
937 v->s = xmemdupz(info.trailer_start,
938 info.trailer_end - info.trailer_start);
939 trailer_info_release(&info);
940 } else if (atom->u.contents.option == C_BARE)
941 v->s = xstrdup(subpos);
946 * We want to have empty print-string for field requests
947 * that do not apply (e.g. "authordate" for a tag object)
949 static void fill_missing_values(struct atom_value *val)
952 for (i = 0; i < used_atom_cnt; i++) {
953 struct atom_value *v = &val[i];
960 * val is a list of atom_value to hold returned values. Extract
961 * the values for atoms in used_atom array out of (obj, buf, sz).
962 * when deref is false, (obj, buf, sz) is the object that is
963 * pointed at by the ref itself; otherwise it is the object the
964 * ref (which is a tag) refers to.
966 static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
968 grab_common_values(val, deref, obj, buf, sz);
971 grab_tag_values(val, deref, obj, buf, sz);
972 grab_sub_body_contents(val, deref, obj, buf, sz);
973 grab_person("tagger", val, deref, obj, buf, sz);
976 grab_commit_values(val, deref, obj, buf, sz);
977 grab_sub_body_contents(val, deref, obj, buf, sz);
978 grab_person("author", val, deref, obj, buf, sz);
979 grab_person("committer", val, deref, obj, buf, sz);
982 /* grab_tree_values(val, deref, obj, buf, sz); */
985 /* grab_blob_values(val, deref, obj, buf, sz); */
988 die("Eh? Object of type %d?", obj->type);
992 static inline char *copy_advance(char *dst, const char *src)
999 static const char *strip_ref_components(const char *refname, const char *nr_arg)
1002 long nr = strtol(nr_arg, &end, 10);
1003 long remaining = nr;
1004 const char *start = refname;
1006 if (nr < 1 || *end != '\0')
1007 die(_(":strip= requires a positive integer argument"));
1012 die(_("ref '%s' does not have %ld components to :strip"),
1022 static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
1023 struct branch *branch, const char **s)
1025 int num_ours, num_theirs;
1026 if (atom->u.remote_ref == RR_SHORTEN)
1027 *s = shorten_unambiguous_ref(refname, warn_ambiguous_refs);
1028 else if (atom->u.remote_ref == RR_TRACK) {
1029 if (stat_tracking_info(branch, &num_ours,
1033 if (!num_ours && !num_theirs)
1036 *s = xstrfmt("[behind %d]", num_theirs);
1037 else if (!num_theirs)
1038 *s = xstrfmt("[ahead %d]", num_ours);
1040 *s = xstrfmt("[ahead %d, behind %d]",
1041 num_ours, num_theirs);
1042 } else if (atom->u.remote_ref == RR_TRACKSHORT) {
1043 if (stat_tracking_info(branch, &num_ours,
1047 if (!num_ours && !num_theirs)
1051 else if (!num_theirs)
1055 } else /* RR_NORMAL */
1060 * Parse the object referred by ref, and grab needed value.
1062 static void populate_value(struct ref_array_item *ref)
1068 const unsigned char *tagged;
1070 ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
1072 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
1073 unsigned char unused1[20];
1074 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
1080 /* Fill in specials first */
1081 for (i = 0; i < used_atom_cnt; i++) {
1082 struct used_atom *atom = &used_atom[i];
1083 const char *name = used_atom[i].name;
1084 struct atom_value *v = &ref->value[i];
1086 const char *refname;
1087 const char *formatp;
1088 struct branch *branch = NULL;
1090 v->handler = append_atom;
1098 if (starts_with(name, "refname"))
1099 refname = ref->refname;
1100 else if (starts_with(name, "symref"))
1101 refname = ref->symref ? ref->symref : "";
1102 else if (starts_with(name, "upstream")) {
1103 const char *branch_name;
1104 /* only local branches may have an upstream */
1105 if (!skip_prefix(ref->refname, "refs/heads/",
1108 branch = branch_get(branch_name);
1110 refname = branch_get_upstream(branch, NULL);
1112 fill_remote_ref_details(atom, refname, branch, &v->s);
1114 } else if (starts_with(name, "push")) {
1115 const char *branch_name;
1116 if (!skip_prefix(ref->refname, "refs/heads/",
1119 branch = branch_get(branch_name);
1121 refname = branch_get_push(branch, NULL);
1124 fill_remote_ref_details(atom, refname, branch, &v->s);
1126 } else if (starts_with(name, "color:")) {
1127 v->s = atom->u.color;
1129 } else if (!strcmp(name, "flag")) {
1130 char buf[256], *cp = buf;
1131 if (ref->flag & REF_ISSYMREF)
1132 cp = copy_advance(cp, ",symref");
1133 if (ref->flag & REF_ISPACKED)
1134 cp = copy_advance(cp, ",packed");
1139 v->s = xstrdup(buf + 1);
1142 } else if (!deref && grab_objectname(name, ref->objectname, v, atom)) {
1144 } else if (!strcmp(name, "HEAD")) {
1146 unsigned char sha1[20];
1148 head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1150 if (head && !strcmp(ref->refname, head))
1155 } else if (starts_with(name, "align")) {
1156 v->handler = align_atom_handler;
1158 } else if (!strcmp(name, "end")) {
1159 v->handler = end_atom_handler;
1161 } else if (!strcmp(name, "if")) {
1162 v->handler = if_atom_handler;
1164 } else if (!strcmp(name, "then")) {
1165 v->handler = then_atom_handler;
1167 } else if (!strcmp(name, "else")) {
1168 v->handler = else_atom_handler;
1173 formatp = strchr(name, ':');
1178 if (!strcmp(formatp, "short"))
1179 refname = shorten_unambiguous_ref(refname,
1180 warn_ambiguous_refs);
1181 else if (skip_prefix(formatp, "strip=", &arg))
1182 refname = strip_ref_components(refname, arg);
1184 die(_("unknown %.*s format %s"),
1185 (int)(formatp - name), name, formatp);
1191 v->s = xstrfmt("%s^{}", refname);
1194 for (i = 0; i < used_atom_cnt; i++) {
1195 struct atom_value *v = &ref->value[i];
1202 buf = get_obj(ref->objectname, &obj, &size, &eaten);
1204 die(_("missing object %s for %s"),
1205 sha1_to_hex(ref->objectname), ref->refname);
1207 die(_("parse_object_buffer failed on %s for %s"),
1208 sha1_to_hex(ref->objectname), ref->refname);
1210 grab_values(ref->value, 0, obj, buf, size);
1215 * If there is no atom that wants to know about tagged
1216 * object, we are done.
1218 if (!need_tagged || (obj->type != OBJ_TAG))
1222 * If it is a tag object, see if we use a value that derefs
1223 * the object, and if we do grab the object it refers to.
1225 tagged = ((struct tag *)obj)->tagged->oid.hash;
1228 * NEEDSWORK: This derefs tag only once, which
1229 * is good to deal with chains of trust, but
1230 * is not consistent with what deref_tag() does
1231 * which peels the onion to the core.
1233 buf = get_obj(tagged, &obj, &size, &eaten);
1235 die(_("missing object %s for %s"),
1236 sha1_to_hex(tagged), ref->refname);
1238 die(_("parse_object_buffer failed on %s for %s"),
1239 sha1_to_hex(tagged), ref->refname);
1240 grab_values(ref->value, 1, obj, buf, size);
1246 * Given a ref, return the value for the atom. This lazily gets value
1247 * out of the object by calling populate value.
1249 static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
1252 populate_value(ref);
1253 fill_missing_values(ref->value);
1255 *v = &ref->value[atom];
1258 enum contains_result {
1259 CONTAINS_UNKNOWN = -1,
1265 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1268 * At each recursion step, the stack items points to the commits whose
1269 * ancestors are to be inspected.
1271 struct contains_stack {
1273 struct contains_stack_entry {
1274 struct commit *commit;
1275 struct commit_list *parents;
1279 static int in_commit_list(const struct commit_list *want, struct commit *c)
1281 for (; want; want = want->next)
1282 if (!oidcmp(&want->item->object.oid, &c->object.oid))
1288 * Test whether the candidate or one of its parents is contained in the list.
1289 * Do not recurse to find out, though, but return -1 if inconclusive.
1291 static enum contains_result contains_test(struct commit *candidate,
1292 const struct commit_list *want)
1294 /* was it previously marked as containing a want commit? */
1295 if (candidate->object.flags & TMP_MARK)
1297 /* or marked as not possibly containing a want commit? */
1298 if (candidate->object.flags & UNINTERESTING)
1301 if (in_commit_list(want, candidate)) {
1302 candidate->object.flags |= TMP_MARK;
1306 if (parse_commit(candidate) < 0)
1312 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
1314 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
1315 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
1316 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
1319 static enum contains_result contains_tag_algo(struct commit *candidate,
1320 const struct commit_list *want)
1322 struct contains_stack contains_stack = { 0, 0, NULL };
1323 int result = contains_test(candidate, want);
1325 if (result != CONTAINS_UNKNOWN)
1328 push_to_contains_stack(candidate, &contains_stack);
1329 while (contains_stack.nr) {
1330 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
1331 struct commit *commit = entry->commit;
1332 struct commit_list *parents = entry->parents;
1335 commit->object.flags |= UNINTERESTING;
1336 contains_stack.nr--;
1339 * If we just popped the stack, parents->item has been marked,
1340 * therefore contains_test will return a meaningful 0 or 1.
1342 else switch (contains_test(parents->item, want)) {
1344 commit->object.flags |= TMP_MARK;
1345 contains_stack.nr--;
1348 entry->parents = parents->next;
1350 case CONTAINS_UNKNOWN:
1351 push_to_contains_stack(parents->item, &contains_stack);
1355 free(contains_stack.contains_stack);
1356 return contains_test(candidate, want);
1359 static int commit_contains(struct ref_filter *filter, struct commit *commit)
1361 if (filter->with_commit_tag_algo)
1362 return contains_tag_algo(commit, filter->with_commit);
1363 return is_descendant_of(commit, filter->with_commit);
1367 * Return 1 if the refname matches one of the patterns, otherwise 0.
1368 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1369 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1370 * matches "refs/heads/mas*", too).
1372 static int match_pattern(const struct ref_filter *filter, const char *refname)
1374 const char **patterns = filter->name_patterns;
1377 if (filter->ignore_case)
1378 flags |= WM_CASEFOLD;
1381 * When no '--format' option is given we need to skip the prefix
1382 * for matching refs of tags and branches.
1384 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1385 skip_prefix(refname, "refs/heads/", &refname) ||
1386 skip_prefix(refname, "refs/remotes/", &refname) ||
1387 skip_prefix(refname, "refs/", &refname));
1389 for (; *patterns; patterns++) {
1390 if (!wildmatch(*patterns, refname, flags, NULL))
1397 * Return 1 if the refname matches one of the patterns, otherwise 0.
1398 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1399 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1400 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1402 static int match_name_as_path(const struct ref_filter *filter, const char *refname)
1404 const char **pattern = filter->name_patterns;
1405 int namelen = strlen(refname);
1406 unsigned flags = WM_PATHNAME;
1408 if (filter->ignore_case)
1409 flags |= WM_CASEFOLD;
1411 for (; *pattern; pattern++) {
1412 const char *p = *pattern;
1413 int plen = strlen(p);
1415 if ((plen <= namelen) &&
1416 !strncmp(refname, p, plen) &&
1417 (refname[plen] == '\0' ||
1418 refname[plen] == '/' ||
1421 if (!wildmatch(p, refname, WM_PATHNAME, NULL))
1427 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1428 static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1430 if (!*filter->name_patterns)
1431 return 1; /* No pattern always matches */
1432 if (filter->match_as_path)
1433 return match_name_as_path(filter, refname);
1434 return match_pattern(filter, refname);
1438 * Given a ref (sha1, refname), check if the ref belongs to the array
1439 * of sha1s. If the given ref is a tag, check if the given tag points
1440 * at one of the sha1s in the given sha1 array.
1441 * the given sha1_array.
1443 * 1. Only a single level of inderection is obtained, we might want to
1444 * change this to account for multiple levels (e.g. annotated tags
1445 * pointing to annotated tags pointing to a commit.)
1446 * 2. As the refs are cached we might know what refname peels to without
1447 * the need to parse the object via parse_object(). peel_ref() might be a
1448 * more efficient alternative to obtain the pointee.
1450 static const unsigned char *match_points_at(struct sha1_array *points_at,
1451 const unsigned char *sha1,
1452 const char *refname)
1454 const unsigned char *tagged_sha1 = NULL;
1457 if (sha1_array_lookup(points_at, sha1) >= 0)
1459 obj = parse_object(sha1);
1461 die(_("malformed object at '%s'"), refname);
1462 if (obj->type == OBJ_TAG)
1463 tagged_sha1 = ((struct tag *)obj)->tagged->oid.hash;
1464 if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
1469 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1470 static struct ref_array_item *new_ref_array_item(const char *refname,
1471 const unsigned char *objectname,
1474 struct ref_array_item *ref;
1475 FLEX_ALLOC_STR(ref, refname, refname);
1476 hashcpy(ref->objectname, objectname);
1482 static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1490 { "refs/heads/" , FILTER_REFS_BRANCHES },
1491 { "refs/remotes/" , FILTER_REFS_REMOTES },
1492 { "refs/tags/", FILTER_REFS_TAGS}
1495 if (filter->kind == FILTER_REFS_BRANCHES ||
1496 filter->kind == FILTER_REFS_REMOTES ||
1497 filter->kind == FILTER_REFS_TAGS)
1498 return filter->kind;
1499 else if (!strcmp(refname, "HEAD"))
1500 return FILTER_REFS_DETACHED_HEAD;
1502 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
1503 if (starts_with(refname, ref_kind[i].prefix))
1504 return ref_kind[i].kind;
1507 return FILTER_REFS_OTHERS;
1511 * A call-back given to for_each_ref(). Filter refs and keep them for
1512 * later object processing.
1514 static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
1516 struct ref_filter_cbdata *ref_cbdata = cb_data;
1517 struct ref_filter *filter = ref_cbdata->filter;
1518 struct ref_array_item *ref;
1519 struct commit *commit = NULL;
1522 if (flag & REF_BAD_NAME) {
1523 warning(_("ignoring ref with broken name %s"), refname);
1527 if (flag & REF_ISBROKEN) {
1528 warning(_("ignoring broken ref %s"), refname);
1532 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1533 kind = filter_ref_kind(filter, refname);
1534 if (!(kind & filter->kind))
1537 if (!filter_pattern_match(filter, refname))
1540 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
1544 * A merge filter is applied on refs pointing to commits. Hence
1545 * obtain the commit using the 'oid' available and discard all
1546 * non-commits early. The actual filtering is done later.
1548 if (filter->merge_commit || filter->with_commit || filter->verbose) {
1549 commit = lookup_commit_reference_gently(oid->hash, 1);
1552 /* We perform the filtering for the '--contains' option */
1553 if (filter->with_commit &&
1554 !commit_contains(filter, commit))
1559 * We do not open the object yet; sort may only need refname
1560 * to do its job and the resulting list may yet to be pruned
1561 * by maxcount logic.
1563 ref = new_ref_array_item(refname, oid->hash, flag);
1564 ref->commit = commit;
1566 REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1567 ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
1572 /* Free memory allocated for a ref_array_item */
1573 static void free_array_item(struct ref_array_item *item)
1575 free((char *)item->symref);
1579 /* Free all memory allocated for ref_array */
1580 void ref_array_clear(struct ref_array *array)
1584 for (i = 0; i < array->nr; i++)
1585 free_array_item(array->items[i]);
1587 array->items = NULL;
1588 array->nr = array->alloc = 0;
1591 static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
1593 struct rev_info revs;
1595 struct ref_filter *filter = ref_cbdata->filter;
1596 struct ref_array *array = ref_cbdata->array;
1597 struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
1599 init_revisions(&revs, NULL);
1601 for (i = 0; i < array->nr; i++) {
1602 struct ref_array_item *item = array->items[i];
1603 add_pending_object(&revs, &item->commit->object, item->refname);
1604 to_clear[i] = item->commit;
1607 filter->merge_commit->object.flags |= UNINTERESTING;
1608 add_pending_object(&revs, &filter->merge_commit->object, "");
1611 if (prepare_revision_walk(&revs))
1612 die(_("revision walk setup failed"));
1617 for (i = 0; i < old_nr; i++) {
1618 struct ref_array_item *item = array->items[i];
1619 struct commit *commit = item->commit;
1621 int is_merged = !!(commit->object.flags & UNINTERESTING);
1623 if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
1624 array->items[array->nr++] = array->items[i];
1626 free_array_item(item);
1629 for (i = 0; i < old_nr; i++)
1630 clear_commit_marks(to_clear[i], ALL_REV_FLAGS);
1631 clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
1636 * API for filtering a set of refs. Based on the type of refs the user
1637 * has requested, we iterate through those refs and apply filters
1638 * as per the given ref_filter structure and finally store the
1639 * filtered refs in the ref_array structure.
1641 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
1643 struct ref_filter_cbdata ref_cbdata;
1645 unsigned int broken = 0;
1647 ref_cbdata.array = array;
1648 ref_cbdata.filter = filter;
1650 if (type & FILTER_REFS_INCLUDE_BROKEN)
1652 filter->kind = type & FILTER_REFS_KIND_MASK;
1654 /* Simple per-ref filtering */
1656 die("filter_refs: invalid type");
1659 * For common cases where we need only branches or remotes or tags,
1660 * we only iterate through those refs. If a mix of refs is needed,
1661 * we iterate over all refs and filter out required refs with the help
1662 * of filter_ref_kind().
1664 if (filter->kind == FILTER_REFS_BRANCHES)
1665 ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
1666 else if (filter->kind == FILTER_REFS_REMOTES)
1667 ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
1668 else if (filter->kind == FILTER_REFS_TAGS)
1669 ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
1670 else if (filter->kind & FILTER_REFS_ALL)
1671 ret = for_each_fullref_in("", ref_filter_handler, &ref_cbdata, broken);
1672 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
1673 head_ref(ref_filter_handler, &ref_cbdata);
1677 /* Filters that need revision walking */
1678 if (filter->merge_commit)
1679 do_merge_filter(&ref_cbdata);
1684 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
1686 struct atom_value *va, *vb;
1688 cmp_type cmp_type = used_atom[s->atom].type;
1689 int (*cmp_fn)(const char *, const char *);
1691 get_ref_atom_value(a, s->atom, &va);
1692 get_ref_atom_value(b, s->atom, &vb);
1693 cmp_fn = s->ignore_case ? strcasecmp : strcmp;
1695 cmp = versioncmp(va->s, vb->s);
1696 else if (cmp_type == FIELD_STR)
1697 cmp = cmp_fn(va->s, vb->s);
1699 if (va->ul < vb->ul)
1701 else if (va->ul == vb->ul)
1702 cmp = cmp_fn(a->refname, b->refname);
1707 return (s->reverse) ? -cmp : cmp;
1710 static struct ref_sorting *ref_sorting;
1711 static int compare_refs(const void *a_, const void *b_)
1713 struct ref_array_item *a = *((struct ref_array_item **)a_);
1714 struct ref_array_item *b = *((struct ref_array_item **)b_);
1715 struct ref_sorting *s;
1717 for (s = ref_sorting; s; s = s->next) {
1718 int cmp = cmp_ref_sorting(s, a, b);
1725 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1727 ref_sorting = sorting;
1728 QSORT(array->items, array->nr, compare_refs);
1731 static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
1733 struct strbuf *s = &state->stack->output;
1735 while (*cp && (!ep || cp < ep)) {
1740 int ch = hex2chr(cp + 1);
1742 strbuf_addch(s, ch);
1748 strbuf_addch(s, *cp);
1753 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1755 const char *cp, *sp, *ep;
1756 struct strbuf *final_buf;
1757 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
1759 state.quote_style = quote_style;
1760 push_stack_element(&state.stack);
1762 for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
1763 struct atom_value *atomv;
1765 ep = strchr(sp, ')');
1767 append_literal(cp, sp, &state);
1768 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
1769 atomv->handler(atomv, &state);
1772 sp = cp + strlen(cp);
1773 append_literal(cp, sp, &state);
1775 if (need_color_reset_at_eol) {
1776 struct atom_value resetv;
1777 char color[COLOR_MAXLEN] = "";
1779 if (color_parse("reset", color) < 0)
1780 die("BUG: couldn't parse 'reset' as a color");
1782 append_atom(&resetv, &state);
1784 if (state.stack->prev)
1785 die(_("format: %%(end) atom missing"));
1786 final_buf = &state.stack->output;
1787 fwrite(final_buf->buf, 1, final_buf->len, stdout);
1788 pop_stack_element(&state.stack);
1792 /* If no sorting option is given, use refname to sort as default */
1793 struct ref_sorting *ref_default_sorting(void)
1795 static const char cstr_name[] = "refname";
1797 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
1799 sorting->next = NULL;
1800 sorting->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
1804 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1806 struct ref_sorting **sorting_tail = opt->value;
1807 struct ref_sorting *s;
1810 if (!arg) /* should --no-sort void the list ? */
1813 s = xcalloc(1, sizeof(*s));
1814 s->next = *sorting_tail;
1821 if (skip_prefix(arg, "version:", &arg) ||
1822 skip_prefix(arg, "v:", &arg))
1825 s->atom = parse_ref_filter_atom(arg, arg+len);
1829 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
1831 struct ref_filter *rf = opt->value;
1832 unsigned char sha1[20];
1834 rf->merge = starts_with(opt->long_name, "no")
1835 ? REF_FILTER_MERGED_OMIT
1836 : REF_FILTER_MERGED_INCLUDE;
1838 if (get_sha1(arg, sha1))
1839 die(_("malformed object name %s"), arg);
1841 rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
1842 if (!rf->merge_commit)
1843 return opterror(opt, "must point to a commit", 0);