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
;
247 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
248 unsigned long ul
; /* used for sorting when not FIELD_STR */
252 * Used to parse format string and sort specifiers
254 int parse_ref_filter_atom(const char *atom
, const char *ep
)
261 if (*sp
== '*' && sp
< ep
)
264 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
266 /* Do we have the atom already used elsewhere? */
267 for (i
= 0; i
< used_atom_cnt
; i
++) {
268 int len
= strlen(used_atom
[i
].name
);
269 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
274 * If the atom name has a colon, strip it and everything after
275 * it off - it specifies the format for this entry, and
276 * shouldn't be used for checking against the valid_atom
279 arg
= memchr(sp
, ':', ep
- sp
);
280 atom_len
= (arg
? arg
: ep
) - sp
;
282 /* Is the atom a valid one? */
283 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
284 int len
= strlen(valid_atom
[i
].name
);
285 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
289 if (ARRAY_SIZE(valid_atom
) <= i
)
290 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
292 /* Add it in, including the deref prefix */
295 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
296 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
297 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
299 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
300 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
301 if (valid_atom
[i
].parser
)
302 valid_atom
[i
].parser(&used_atom
[at
], arg
);
305 if (!strcmp(used_atom
[at
].name
, "symref"))
310 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
312 switch (quote_style
) {
314 strbuf_addstr(s
, str
);
317 sq_quote_buf(s
, str
);
320 perl_quote_buf(s
, str
);
323 python_quote_buf(s
, str
);
326 tcl_quote_buf(s
, str
);
331 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
334 * Quote formatting is only done when the stack has a single
335 * element. Otherwise quote formatting is done on the
336 * element's entire output strbuf when the %(end) atom is
339 if (!state
->stack
->prev
)
340 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
342 strbuf_addstr(&state
->stack
->output
, v
->s
);
345 static void push_stack_element(struct ref_formatting_stack
**stack
)
347 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
349 strbuf_init(&s
->output
, 0);
354 static void pop_stack_element(struct ref_formatting_stack
**stack
)
356 struct ref_formatting_stack
*current
= *stack
;
357 struct ref_formatting_stack
*prev
= current
->prev
;
360 strbuf_addbuf(&prev
->output
, ¤t
->output
);
361 strbuf_release(¤t
->output
);
366 static void end_align_handler(struct ref_formatting_stack
**stack
)
368 struct ref_formatting_stack
*cur
= *stack
;
369 struct align
*align
= (struct align
*)cur
->at_end_data
;
370 struct strbuf s
= STRBUF_INIT
;
372 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
373 strbuf_swap(&cur
->output
, &s
);
377 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
379 struct ref_formatting_stack
*new;
381 push_stack_element(&state
->stack
);
383 new->at_end
= end_align_handler
;
384 new->at_end_data
= &atomv
->u
.align
;
387 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
389 struct ref_formatting_stack
*cur
= *stack
;
390 struct ref_formatting_stack
*prev
= cur
->prev
;
391 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
393 if (!if_then_else
->then_atom_seen
)
394 die(_("format: %%(if) atom used without a %%(then) atom"));
396 if (if_then_else
->else_atom_seen
) {
398 * There is an %(else) atom: we need to drop one state from the
399 * stack, either the %(else) branch if the condition is satisfied, or
400 * the %(then) branch if it isn't.
402 if (if_then_else
->condition_satisfied
) {
403 strbuf_reset(&cur
->output
);
404 pop_stack_element(&cur
);
406 strbuf_swap(&cur
->output
, &prev
->output
);
407 strbuf_reset(&cur
->output
);
408 pop_stack_element(&cur
);
410 } else if (!if_then_else
->condition_satisfied
) {
412 * No %(else) atom: just drop the %(then) branch if the
413 * condition is not satisfied.
415 strbuf_reset(&cur
->output
);
422 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
424 struct ref_formatting_stack
*new;
425 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
427 push_stack_element(&state
->stack
);
429 new->at_end
= if_then_else_handler
;
430 new->at_end_data
= if_then_else
;
433 static int is_empty(const char *s
)
443 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
445 struct ref_formatting_stack
*cur
= state
->stack
;
446 struct if_then_else
*if_then_else
= NULL
;
448 if (cur
->at_end
== if_then_else_handler
)
449 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
451 die(_("format: %%(then) atom used without an %%(if) atom"));
452 if (if_then_else
->then_atom_seen
)
453 die(_("format: %%(then) atom used more than once"));
454 if (if_then_else
->else_atom_seen
)
455 die(_("format: %%(then) atom used after %%(else)"));
456 if_then_else
->then_atom_seen
= 1;
458 * If there exists non-empty string between the 'if' and
459 * 'then' atom then the 'if' condition is satisfied.
461 if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
462 if_then_else
->condition_satisfied
= 1;
463 strbuf_reset(&cur
->output
);
466 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
468 struct ref_formatting_stack
*prev
= state
->stack
;
469 struct if_then_else
*if_then_else
= NULL
;
471 if (prev
->at_end
== if_then_else_handler
)
472 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
474 die(_("format: %%(else) atom used without an %%(if) atom"));
475 if (!if_then_else
->then_atom_seen
)
476 die(_("format: %%(else) atom used without a %%(then) atom"));
477 if (if_then_else
->else_atom_seen
)
478 die(_("format: %%(else) atom used more than once"));
479 if_then_else
->else_atom_seen
= 1;
480 push_stack_element(&state
->stack
);
481 state
->stack
->at_end_data
= prev
->at_end_data
;
482 state
->stack
->at_end
= prev
->at_end
;
485 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
487 struct ref_formatting_stack
*current
= state
->stack
;
488 struct strbuf s
= STRBUF_INIT
;
490 if (!current
->at_end
)
491 die(_("format: %%(end) atom used without corresponding atom"));
492 current
->at_end(&state
->stack
);
494 /* Stack may have been popped within at_end(), hence reset the current pointer */
495 current
= state
->stack
;
498 * Perform quote formatting when the stack element is that of
499 * a supporting atom. If nested then perform quote formatting
500 * only on the topmost supporting atom.
502 if (!current
->prev
->prev
) {
503 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
504 strbuf_swap(¤t
->output
, &s
);
507 pop_stack_element(&state
->stack
);
511 * In a format string, find the next occurrence of %(atom).
513 static const char *find_next(const char *cp
)
518 * %( is the start of an atom;
519 * %% is a quoted per-cent.
523 else if (cp
[1] == '%')
524 cp
++; /* skip over two % */
525 /* otherwise this is a singleton, literal % */
533 * Make sure the format string is well formed, and parse out
536 int verify_ref_format(const char *format
)
540 need_color_reset_at_eol
= 0;
541 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
542 const char *color
, *ep
= strchr(sp
, ')');
546 return error(_("malformed format string %s"), sp
);
547 /* sp points at "%(" and ep points at the closing ")" */
548 at
= parse_ref_filter_atom(sp
+ 2, ep
);
551 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
552 need_color_reset_at_eol
= !!strcmp(color
, "reset");
558 * Given an object name, read the object data and size, and return a
559 * "struct object". If the object data we are returning is also borrowed
560 * by the "struct object" representation, set *eaten as well---it is a
561 * signal from parse_object_buffer to us not to free the buffer.
563 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
565 enum object_type type
;
566 void *buf
= read_sha1_file(sha1
, &type
, sz
);
569 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
575 static int grab_objectname(const char *name
, const unsigned char *sha1
,
576 struct atom_value
*v
, struct used_atom
*atom
)
578 if (starts_with(name
, "objectname")) {
579 if (atom
->u
.objectname
== O_SHORT
) {
580 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
582 } else if (atom
->u
.objectname
== O_FULL
) {
583 v
->s
= xstrdup(sha1_to_hex(sha1
));
586 die("BUG: unknown %%(objectname) option");
591 /* See grab_values */
592 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
596 for (i
= 0; i
< used_atom_cnt
; i
++) {
597 const char *name
= used_atom
[i
].name
;
598 struct atom_value
*v
= &val
[i
];
599 if (!!deref
!= (*name
== '*'))
603 if (!strcmp(name
, "objecttype"))
604 v
->s
= typename(obj
->type
);
605 else if (!strcmp(name
, "objectsize")) {
607 v
->s
= xstrfmt("%lu", sz
);
610 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
614 /* See grab_values */
615 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
618 struct tag
*tag
= (struct tag
*) obj
;
620 for (i
= 0; i
< used_atom_cnt
; i
++) {
621 const char *name
= used_atom
[i
].name
;
622 struct atom_value
*v
= &val
[i
];
623 if (!!deref
!= (*name
== '*'))
627 if (!strcmp(name
, "tag"))
629 else if (!strcmp(name
, "type") && tag
->tagged
)
630 v
->s
= typename(tag
->tagged
->type
);
631 else if (!strcmp(name
, "object") && tag
->tagged
)
632 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
636 /* See grab_values */
637 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
640 struct commit
*commit
= (struct commit
*) obj
;
642 for (i
= 0; i
< used_atom_cnt
; i
++) {
643 const char *name
= used_atom
[i
].name
;
644 struct atom_value
*v
= &val
[i
];
645 if (!!deref
!= (*name
== '*'))
649 if (!strcmp(name
, "tree")) {
650 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
652 else if (!strcmp(name
, "numparent")) {
653 v
->ul
= commit_list_count(commit
->parents
);
654 v
->s
= xstrfmt("%lu", v
->ul
);
656 else if (!strcmp(name
, "parent")) {
657 struct commit_list
*parents
;
658 struct strbuf s
= STRBUF_INIT
;
659 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
660 struct commit
*parent
= parents
->item
;
661 if (parents
!= commit
->parents
)
662 strbuf_addch(&s
, ' ');
663 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
665 v
->s
= strbuf_detach(&s
, NULL
);
670 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
674 if (!strncmp(buf
, who
, wholen
) &&
676 return buf
+ wholen
+ 1;
677 eol
= strchr(buf
, '\n');
682 return ""; /* end of header */
688 static const char *copy_line(const char *buf
)
690 const char *eol
= strchrnul(buf
, '\n');
691 return xmemdupz(buf
, eol
- buf
);
694 static const char *copy_name(const char *buf
)
697 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
698 if (!strncmp(cp
, " <", 2))
699 return xmemdupz(buf
, cp
- buf
);
704 static const char *copy_email(const char *buf
)
706 const char *email
= strchr(buf
, '<');
710 eoemail
= strchr(email
, '>');
713 return xmemdupz(email
, eoemail
+ 1 - email
);
716 static char *copy_subject(const char *buf
, unsigned long len
)
718 char *r
= xmemdupz(buf
, len
);
721 for (i
= 0; i
< len
; i
++)
728 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
730 const char *eoemail
= strstr(buf
, "> ");
732 unsigned long timestamp
;
734 struct date_mode date_mode
= { DATE_NORMAL
};
738 * We got here because atomname ends in "date" or "date<something>";
739 * it's not possible that <something> is not ":<format>" because
740 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
741 * ":" means no format is specified, and use the default.
743 formatp
= strchr(atomname
, ':');
744 if (formatp
!= NULL
) {
746 parse_date_format(formatp
, &date_mode
);
751 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
752 if (timestamp
== ULONG_MAX
)
754 tz
= strtol(zone
, NULL
, 10);
755 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
757 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
765 /* See grab_values */
766 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
769 int wholen
= strlen(who
);
770 const char *wholine
= NULL
;
772 for (i
= 0; i
< used_atom_cnt
; i
++) {
773 const char *name
= used_atom
[i
].name
;
774 struct atom_value
*v
= &val
[i
];
775 if (!!deref
!= (*name
== '*'))
779 if (strncmp(who
, name
, wholen
))
781 if (name
[wholen
] != 0 &&
782 strcmp(name
+ wholen
, "name") &&
783 strcmp(name
+ wholen
, "email") &&
784 !starts_with(name
+ wholen
, "date"))
787 wholine
= find_wholine(who
, wholen
, buf
, sz
);
789 return; /* no point looking for it */
790 if (name
[wholen
] == 0)
791 v
->s
= copy_line(wholine
);
792 else if (!strcmp(name
+ wholen
, "name"))
793 v
->s
= copy_name(wholine
);
794 else if (!strcmp(name
+ wholen
, "email"))
795 v
->s
= copy_email(wholine
);
796 else if (starts_with(name
+ wholen
, "date"))
797 grab_date(wholine
, v
, name
);
801 * For a tag or a commit object, if "creator" or "creatordate" is
802 * requested, do something special.
804 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
805 return; /* "author" for commit object is not wanted */
807 wholine
= find_wholine(who
, wholen
, buf
, sz
);
810 for (i
= 0; i
< used_atom_cnt
; i
++) {
811 const char *name
= used_atom
[i
].name
;
812 struct atom_value
*v
= &val
[i
];
813 if (!!deref
!= (*name
== '*'))
818 if (starts_with(name
, "creatordate"))
819 grab_date(wholine
, v
, name
);
820 else if (!strcmp(name
, "creator"))
821 v
->s
= copy_line(wholine
);
825 static void find_subpos(const char *buf
, unsigned long sz
,
826 const char **sub
, unsigned long *sublen
,
827 const char **body
, unsigned long *bodylen
,
828 unsigned long *nonsiglen
,
829 const char **sig
, unsigned long *siglen
)
832 /* skip past header until we hit empty line */
833 while (*buf
&& *buf
!= '\n') {
834 eol
= strchrnul(buf
, '\n');
839 /* skip any empty lines */
843 /* parse signature first; we might not even have a subject line */
844 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
845 *siglen
= strlen(*sig
);
847 /* subject is first non-empty line */
849 /* subject goes to first empty line */
850 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
851 eol
= strchrnul(buf
, '\n');
856 *sublen
= buf
- *sub
;
857 /* drop trailing newline, if present */
858 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
861 /* skip any empty lines */
865 *bodylen
= strlen(buf
);
866 *nonsiglen
= *sig
- buf
;
870 * If 'lines' is greater than 0, append that many lines from the given
871 * 'buf' of length 'size' to the given strbuf.
873 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
876 const char *sp
, *eol
;
881 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
883 strbuf_addstr(out
, "\n ");
884 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
885 len
= eol
? eol
- sp
: size
- (sp
- buf
);
886 strbuf_add(out
, sp
, len
);
893 /* See grab_values */
894 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
897 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
898 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
900 for (i
= 0; i
< used_atom_cnt
; i
++) {
901 struct used_atom
*atom
= &used_atom
[i
];
902 const char *name
= atom
->name
;
903 struct atom_value
*v
= &val
[i
];
904 if (!!deref
!= (*name
== '*'))
908 if (strcmp(name
, "subject") &&
909 strcmp(name
, "body") &&
910 strcmp(name
, "trailers") &&
911 !starts_with(name
, "contents"))
916 &bodypos
, &bodylen
, &nonsiglen
,
919 if (atom
->u
.contents
.option
== C_SUB
)
920 v
->s
= copy_subject(subpos
, sublen
);
921 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
922 v
->s
= xmemdupz(bodypos
, bodylen
);
923 else if (atom
->u
.contents
.option
== C_BODY
)
924 v
->s
= xmemdupz(bodypos
, nonsiglen
);
925 else if (atom
->u
.contents
.option
== C_SIG
)
926 v
->s
= xmemdupz(sigpos
, siglen
);
927 else if (atom
->u
.contents
.option
== C_LINES
) {
928 struct strbuf s
= STRBUF_INIT
;
929 const char *contents_end
= bodylen
+ bodypos
- siglen
;
931 /* Size is the length of the message after removing the signature */
932 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
933 v
->s
= strbuf_detach(&s
, NULL
);
934 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
935 struct trailer_info info
;
937 /* Search for trailer info */
938 trailer_info_get(&info
, subpos
);
939 v
->s
= xmemdupz(info
.trailer_start
,
940 info
.trailer_end
- info
.trailer_start
);
941 trailer_info_release(&info
);
942 } else if (atom
->u
.contents
.option
== C_BARE
)
943 v
->s
= xstrdup(subpos
);
948 * We want to have empty print-string for field requests
949 * that do not apply (e.g. "authordate" for a tag object)
951 static void fill_missing_values(struct atom_value
*val
)
954 for (i
= 0; i
< used_atom_cnt
; i
++) {
955 struct atom_value
*v
= &val
[i
];
962 * val is a list of atom_value to hold returned values. Extract
963 * the values for atoms in used_atom array out of (obj, buf, sz).
964 * when deref is false, (obj, buf, sz) is the object that is
965 * pointed at by the ref itself; otherwise it is the object the
966 * ref (which is a tag) refers to.
968 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
970 grab_common_values(val
, deref
, obj
, buf
, sz
);
973 grab_tag_values(val
, deref
, obj
, buf
, sz
);
974 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
975 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
978 grab_commit_values(val
, deref
, obj
, buf
, sz
);
979 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
980 grab_person("author", val
, deref
, obj
, buf
, sz
);
981 grab_person("committer", val
, deref
, obj
, buf
, sz
);
984 /* grab_tree_values(val, deref, obj, buf, sz); */
987 /* grab_blob_values(val, deref, obj, buf, sz); */
990 die("Eh? Object of type %d?", obj
->type
);
994 static inline char *copy_advance(char *dst
, const char *src
)
1001 static const char *strip_ref_components(const char *refname
, const char *nr_arg
)
1004 long nr
= strtol(nr_arg
, &end
, 10);
1005 long remaining
= nr
;
1006 const char *start
= refname
;
1008 if (nr
< 1 || *end
!= '\0')
1009 die(_(":strip= requires a positive integer argument"));
1014 die(_("ref '%s' does not have %ld components to :strip"),
1024 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1025 struct branch
*branch
, const char **s
)
1027 int num_ours
, num_theirs
;
1028 if (atom
->u
.remote_ref
== RR_SHORTEN
)
1029 *s
= shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1030 else if (atom
->u
.remote_ref
== RR_TRACK
) {
1031 if (stat_tracking_info(branch
, &num_ours
,
1035 if (!num_ours
&& !num_theirs
)
1038 *s
= xstrfmt("[behind %d]", num_theirs
);
1039 else if (!num_theirs
)
1040 *s
= xstrfmt("[ahead %d]", num_ours
);
1042 *s
= xstrfmt("[ahead %d, behind %d]",
1043 num_ours
, num_theirs
);
1044 } else if (atom
->u
.remote_ref
== RR_TRACKSHORT
) {
1045 if (stat_tracking_info(branch
, &num_ours
,
1049 if (!num_ours
&& !num_theirs
)
1053 else if (!num_theirs
)
1057 } else /* RR_NORMAL */
1062 * Parse the object referred by ref, and grab needed value.
1064 static void populate_value(struct ref_array_item
*ref
)
1070 const unsigned char *tagged
;
1072 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1074 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1075 unsigned char unused1
[20];
1076 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1082 /* Fill in specials first */
1083 for (i
= 0; i
< used_atom_cnt
; i
++) {
1084 struct used_atom
*atom
= &used_atom
[i
];
1085 const char *name
= used_atom
[i
].name
;
1086 struct atom_value
*v
= &ref
->value
[i
];
1088 const char *refname
;
1089 const char *formatp
;
1090 struct branch
*branch
= NULL
;
1092 v
->handler
= append_atom
;
1099 if (starts_with(name
, "refname"))
1100 refname
= ref
->refname
;
1101 else if (starts_with(name
, "symref"))
1102 refname
= ref
->symref
? ref
->symref
: "";
1103 else if (starts_with(name
, "upstream")) {
1104 const char *branch_name
;
1105 /* only local branches may have an upstream */
1106 if (!skip_prefix(ref
->refname
, "refs/heads/",
1109 branch
= branch_get(branch_name
);
1111 refname
= branch_get_upstream(branch
, NULL
);
1113 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1115 } else if (starts_with(name
, "push")) {
1116 const char *branch_name
;
1117 if (!skip_prefix(ref
->refname
, "refs/heads/",
1120 branch
= branch_get(branch_name
);
1122 refname
= branch_get_push(branch
, NULL
);
1125 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1127 } else if (starts_with(name
, "color:")) {
1128 v
->s
= atom
->u
.color
;
1130 } else if (!strcmp(name
, "flag")) {
1131 char buf
[256], *cp
= buf
;
1132 if (ref
->flag
& REF_ISSYMREF
)
1133 cp
= copy_advance(cp
, ",symref");
1134 if (ref
->flag
& REF_ISPACKED
)
1135 cp
= copy_advance(cp
, ",packed");
1140 v
->s
= xstrdup(buf
+ 1);
1143 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
, atom
)) {
1145 } else if (!strcmp(name
, "HEAD")) {
1147 unsigned char sha1
[20];
1149 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1151 if (head
&& !strcmp(ref
->refname
, head
))
1156 } else if (starts_with(name
, "align")) {
1157 v
->u
.align
= atom
->u
.align
;
1158 v
->handler
= align_atom_handler
;
1160 } else if (!strcmp(name
, "end")) {
1161 v
->handler
= end_atom_handler
;
1163 } else if (!strcmp(name
, "if")) {
1164 v
->handler
= if_atom_handler
;
1166 } else if (!strcmp(name
, "then")) {
1167 v
->handler
= then_atom_handler
;
1169 } else if (!strcmp(name
, "else")) {
1170 v
->handler
= else_atom_handler
;
1175 formatp
= strchr(name
, ':');
1180 if (!strcmp(formatp
, "short"))
1181 refname
= shorten_unambiguous_ref(refname
,
1182 warn_ambiguous_refs
);
1183 else if (skip_prefix(formatp
, "strip=", &arg
))
1184 refname
= strip_ref_components(refname
, arg
);
1186 die(_("unknown %.*s format %s"),
1187 (int)(formatp
- name
), name
, formatp
);
1193 v
->s
= xstrfmt("%s^{}", refname
);
1196 for (i
= 0; i
< used_atom_cnt
; i
++) {
1197 struct atom_value
*v
= &ref
->value
[i
];
1204 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
1206 die(_("missing object %s for %s"),
1207 sha1_to_hex(ref
->objectname
), ref
->refname
);
1209 die(_("parse_object_buffer failed on %s for %s"),
1210 sha1_to_hex(ref
->objectname
), ref
->refname
);
1212 grab_values(ref
->value
, 0, obj
, buf
, size
);
1217 * If there is no atom that wants to know about tagged
1218 * object, we are done.
1220 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1224 * If it is a tag object, see if we use a value that derefs
1225 * the object, and if we do grab the object it refers to.
1227 tagged
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1230 * NEEDSWORK: This derefs tag only once, which
1231 * is good to deal with chains of trust, but
1232 * is not consistent with what deref_tag() does
1233 * which peels the onion to the core.
1235 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1237 die(_("missing object %s for %s"),
1238 sha1_to_hex(tagged
), ref
->refname
);
1240 die(_("parse_object_buffer failed on %s for %s"),
1241 sha1_to_hex(tagged
), ref
->refname
);
1242 grab_values(ref
->value
, 1, obj
, buf
, size
);
1248 * Given a ref, return the value for the atom. This lazily gets value
1249 * out of the object by calling populate value.
1251 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1254 populate_value(ref
);
1255 fill_missing_values(ref
->value
);
1257 *v
= &ref
->value
[atom
];
1260 enum contains_result
{
1261 CONTAINS_UNKNOWN
= -1,
1267 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1270 * At each recursion step, the stack items points to the commits whose
1271 * ancestors are to be inspected.
1273 struct contains_stack
{
1275 struct contains_stack_entry
{
1276 struct commit
*commit
;
1277 struct commit_list
*parents
;
1281 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1283 for (; want
; want
= want
->next
)
1284 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1290 * Test whether the candidate or one of its parents is contained in the list.
1291 * Do not recurse to find out, though, but return -1 if inconclusive.
1293 static enum contains_result
contains_test(struct commit
*candidate
,
1294 const struct commit_list
*want
)
1296 /* was it previously marked as containing a want commit? */
1297 if (candidate
->object
.flags
& TMP_MARK
)
1299 /* or marked as not possibly containing a want commit? */
1300 if (candidate
->object
.flags
& UNINTERESTING
)
1303 if (in_commit_list(want
, candidate
)) {
1304 candidate
->object
.flags
|= TMP_MARK
;
1308 if (parse_commit(candidate
) < 0)
1314 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1316 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1317 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1318 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1321 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1322 const struct commit_list
*want
)
1324 struct contains_stack contains_stack
= { 0, 0, NULL
};
1325 int result
= contains_test(candidate
, want
);
1327 if (result
!= CONTAINS_UNKNOWN
)
1330 push_to_contains_stack(candidate
, &contains_stack
);
1331 while (contains_stack
.nr
) {
1332 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1333 struct commit
*commit
= entry
->commit
;
1334 struct commit_list
*parents
= entry
->parents
;
1337 commit
->object
.flags
|= UNINTERESTING
;
1338 contains_stack
.nr
--;
1341 * If we just popped the stack, parents->item has been marked,
1342 * therefore contains_test will return a meaningful 0 or 1.
1344 else switch (contains_test(parents
->item
, want
)) {
1346 commit
->object
.flags
|= TMP_MARK
;
1347 contains_stack
.nr
--;
1350 entry
->parents
= parents
->next
;
1352 case CONTAINS_UNKNOWN
:
1353 push_to_contains_stack(parents
->item
, &contains_stack
);
1357 free(contains_stack
.contains_stack
);
1358 return contains_test(candidate
, want
);
1361 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1363 if (filter
->with_commit_tag_algo
)
1364 return contains_tag_algo(commit
, filter
->with_commit
);
1365 return is_descendant_of(commit
, filter
->with_commit
);
1369 * Return 1 if the refname matches one of the patterns, otherwise 0.
1370 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1371 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1372 * matches "refs/heads/mas*", too).
1374 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1376 const char **patterns
= filter
->name_patterns
;
1379 if (filter
->ignore_case
)
1380 flags
|= WM_CASEFOLD
;
1383 * When no '--format' option is given we need to skip the prefix
1384 * for matching refs of tags and branches.
1386 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1387 skip_prefix(refname
, "refs/heads/", &refname
) ||
1388 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1389 skip_prefix(refname
, "refs/", &refname
));
1391 for (; *patterns
; patterns
++) {
1392 if (!wildmatch(*patterns
, refname
, flags
, NULL
))
1399 * Return 1 if the refname matches one of the patterns, otherwise 0.
1400 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1401 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1402 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1404 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1406 const char **pattern
= filter
->name_patterns
;
1407 int namelen
= strlen(refname
);
1408 unsigned flags
= WM_PATHNAME
;
1410 if (filter
->ignore_case
)
1411 flags
|= WM_CASEFOLD
;
1413 for (; *pattern
; pattern
++) {
1414 const char *p
= *pattern
;
1415 int plen
= strlen(p
);
1417 if ((plen
<= namelen
) &&
1418 !strncmp(refname
, p
, plen
) &&
1419 (refname
[plen
] == '\0' ||
1420 refname
[plen
] == '/' ||
1423 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1429 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1430 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1432 if (!*filter
->name_patterns
)
1433 return 1; /* No pattern always matches */
1434 if (filter
->match_as_path
)
1435 return match_name_as_path(filter
, refname
);
1436 return match_pattern(filter
, refname
);
1440 * Given a ref (sha1, refname), check if the ref belongs to the array
1441 * of sha1s. If the given ref is a tag, check if the given tag points
1442 * at one of the sha1s in the given sha1 array.
1443 * the given sha1_array.
1445 * 1. Only a single level of inderection is obtained, we might want to
1446 * change this to account for multiple levels (e.g. annotated tags
1447 * pointing to annotated tags pointing to a commit.)
1448 * 2. As the refs are cached we might know what refname peels to without
1449 * the need to parse the object via parse_object(). peel_ref() might be a
1450 * more efficient alternative to obtain the pointee.
1452 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1453 const unsigned char *sha1
,
1454 const char *refname
)
1456 const unsigned char *tagged_sha1
= NULL
;
1459 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1461 obj
= parse_object(sha1
);
1463 die(_("malformed object at '%s'"), refname
);
1464 if (obj
->type
== OBJ_TAG
)
1465 tagged_sha1
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1466 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1471 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1472 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1473 const unsigned char *objectname
,
1476 struct ref_array_item
*ref
;
1477 FLEX_ALLOC_STR(ref
, refname
, refname
);
1478 hashcpy(ref
->objectname
, objectname
);
1484 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1492 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1493 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1494 { "refs/tags/", FILTER_REFS_TAGS
}
1497 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1498 filter
->kind
== FILTER_REFS_REMOTES
||
1499 filter
->kind
== FILTER_REFS_TAGS
)
1500 return filter
->kind
;
1501 else if (!strcmp(refname
, "HEAD"))
1502 return FILTER_REFS_DETACHED_HEAD
;
1504 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1505 if (starts_with(refname
, ref_kind
[i
].prefix
))
1506 return ref_kind
[i
].kind
;
1509 return FILTER_REFS_OTHERS
;
1513 * A call-back given to for_each_ref(). Filter refs and keep them for
1514 * later object processing.
1516 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1518 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1519 struct ref_filter
*filter
= ref_cbdata
->filter
;
1520 struct ref_array_item
*ref
;
1521 struct commit
*commit
= NULL
;
1524 if (flag
& REF_BAD_NAME
) {
1525 warning(_("ignoring ref with broken name %s"), refname
);
1529 if (flag
& REF_ISBROKEN
) {
1530 warning(_("ignoring broken ref %s"), refname
);
1534 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1535 kind
= filter_ref_kind(filter
, refname
);
1536 if (!(kind
& filter
->kind
))
1539 if (!filter_pattern_match(filter
, refname
))
1542 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1546 * A merge filter is applied on refs pointing to commits. Hence
1547 * obtain the commit using the 'oid' available and discard all
1548 * non-commits early. The actual filtering is done later.
1550 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->verbose
) {
1551 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1554 /* We perform the filtering for the '--contains' option */
1555 if (filter
->with_commit
&&
1556 !commit_contains(filter
, commit
))
1561 * We do not open the object yet; sort may only need refname
1562 * to do its job and the resulting list may yet to be pruned
1563 * by maxcount logic.
1565 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1566 ref
->commit
= commit
;
1568 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1569 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1574 /* Free memory allocated for a ref_array_item */
1575 static void free_array_item(struct ref_array_item
*item
)
1577 free((char *)item
->symref
);
1581 /* Free all memory allocated for ref_array */
1582 void ref_array_clear(struct ref_array
*array
)
1586 for (i
= 0; i
< array
->nr
; i
++)
1587 free_array_item(array
->items
[i
]);
1589 array
->items
= NULL
;
1590 array
->nr
= array
->alloc
= 0;
1593 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1595 struct rev_info revs
;
1597 struct ref_filter
*filter
= ref_cbdata
->filter
;
1598 struct ref_array
*array
= ref_cbdata
->array
;
1599 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1601 init_revisions(&revs
, NULL
);
1603 for (i
= 0; i
< array
->nr
; i
++) {
1604 struct ref_array_item
*item
= array
->items
[i
];
1605 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1606 to_clear
[i
] = item
->commit
;
1609 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1610 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1613 if (prepare_revision_walk(&revs
))
1614 die(_("revision walk setup failed"));
1619 for (i
= 0; i
< old_nr
; i
++) {
1620 struct ref_array_item
*item
= array
->items
[i
];
1621 struct commit
*commit
= item
->commit
;
1623 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1625 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1626 array
->items
[array
->nr
++] = array
->items
[i
];
1628 free_array_item(item
);
1631 for (i
= 0; i
< old_nr
; i
++)
1632 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1633 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1638 * API for filtering a set of refs. Based on the type of refs the user
1639 * has requested, we iterate through those refs and apply filters
1640 * as per the given ref_filter structure and finally store the
1641 * filtered refs in the ref_array structure.
1643 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1645 struct ref_filter_cbdata ref_cbdata
;
1647 unsigned int broken
= 0;
1649 ref_cbdata
.array
= array
;
1650 ref_cbdata
.filter
= filter
;
1652 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1654 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1656 /* Simple per-ref filtering */
1658 die("filter_refs: invalid type");
1661 * For common cases where we need only branches or remotes or tags,
1662 * we only iterate through those refs. If a mix of refs is needed,
1663 * we iterate over all refs and filter out required refs with the help
1664 * of filter_ref_kind().
1666 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1667 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1668 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1669 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1670 else if (filter
->kind
== FILTER_REFS_TAGS
)
1671 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1672 else if (filter
->kind
& FILTER_REFS_ALL
)
1673 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1674 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1675 head_ref(ref_filter_handler
, &ref_cbdata
);
1679 /* Filters that need revision walking */
1680 if (filter
->merge_commit
)
1681 do_merge_filter(&ref_cbdata
);
1686 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1688 struct atom_value
*va
, *vb
;
1690 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
1691 int (*cmp_fn
)(const char *, const char *);
1693 get_ref_atom_value(a
, s
->atom
, &va
);
1694 get_ref_atom_value(b
, s
->atom
, &vb
);
1695 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
1697 cmp
= versioncmp(va
->s
, vb
->s
);
1698 else if (cmp_type
== FIELD_STR
)
1699 cmp
= cmp_fn(va
->s
, vb
->s
);
1701 if (va
->ul
< vb
->ul
)
1703 else if (va
->ul
== vb
->ul
)
1704 cmp
= cmp_fn(a
->refname
, b
->refname
);
1709 return (s
->reverse
) ? -cmp
: cmp
;
1712 static struct ref_sorting
*ref_sorting
;
1713 static int compare_refs(const void *a_
, const void *b_
)
1715 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1716 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1717 struct ref_sorting
*s
;
1719 for (s
= ref_sorting
; s
; s
= s
->next
) {
1720 int cmp
= cmp_ref_sorting(s
, a
, b
);
1727 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1729 ref_sorting
= sorting
;
1730 QSORT(array
->items
, array
->nr
, compare_refs
);
1733 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1735 struct strbuf
*s
= &state
->stack
->output
;
1737 while (*cp
&& (!ep
|| cp
< ep
)) {
1742 int ch
= hex2chr(cp
+ 1);
1744 strbuf_addch(s
, ch
);
1750 strbuf_addch(s
, *cp
);
1755 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1757 const char *cp
, *sp
, *ep
;
1758 struct strbuf
*final_buf
;
1759 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1761 state
.quote_style
= quote_style
;
1762 push_stack_element(&state
.stack
);
1764 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1765 struct atom_value
*atomv
;
1767 ep
= strchr(sp
, ')');
1769 append_literal(cp
, sp
, &state
);
1770 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1771 atomv
->handler(atomv
, &state
);
1774 sp
= cp
+ strlen(cp
);
1775 append_literal(cp
, sp
, &state
);
1777 if (need_color_reset_at_eol
) {
1778 struct atom_value resetv
;
1779 char color
[COLOR_MAXLEN
] = "";
1781 if (color_parse("reset", color
) < 0)
1782 die("BUG: couldn't parse 'reset' as a color");
1784 append_atom(&resetv
, &state
);
1786 if (state
.stack
->prev
)
1787 die(_("format: %%(end) atom missing"));
1788 final_buf
= &state
.stack
->output
;
1789 fwrite(final_buf
->buf
, 1, final_buf
->len
, stdout
);
1790 pop_stack_element(&state
.stack
);
1794 /* If no sorting option is given, use refname to sort as default */
1795 struct ref_sorting
*ref_default_sorting(void)
1797 static const char cstr_name
[] = "refname";
1799 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1801 sorting
->next
= NULL
;
1802 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1806 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1808 struct ref_sorting
**sorting_tail
= opt
->value
;
1809 struct ref_sorting
*s
;
1812 if (!arg
) /* should --no-sort void the list ? */
1815 s
= xcalloc(1, sizeof(*s
));
1816 s
->next
= *sorting_tail
;
1823 if (skip_prefix(arg
, "version:", &arg
) ||
1824 skip_prefix(arg
, "v:", &arg
))
1827 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1831 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1833 struct ref_filter
*rf
= opt
->value
;
1834 unsigned char sha1
[20];
1836 rf
->merge
= starts_with(opt
->long_name
, "no")
1837 ? REF_FILTER_MERGED_OMIT
1838 : REF_FILTER_MERGED_INCLUDE
;
1840 if (get_sha1(arg
, sha1
))
1841 die(_("malformed object name %s"), arg
);
1843 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1844 if (!rf
->merge_commit
)
1845 return opterror(opt
, "must point to a commit", 0);