3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
17 #include "wt-status.h"
18 #include "commit-slab.h"
20 static struct ref_msg
{
24 const char *ahead_behind
;
26 /* Untranslated plumbing messages: */
33 void setup_ref_filter_porcelain_msg(void)
35 msgs
.gone
= _("gone");
36 msgs
.ahead
= _("ahead %d");
37 msgs
.behind
= _("behind %d");
38 msgs
.ahead_behind
= _("ahead %d, behind %d");
41 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
42 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
50 cmp_status cmp_status
;
52 unsigned int then_atom_seen
: 1,
54 condition_satisfied
: 1;
58 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
63 * An atom is a valid field atom listed below, possibly prefixed with
64 * a "*" to denote deref_tag().
66 * We parse given format string and sort specifiers, and make a list
67 * of properties that we need to extract out of objects. ref_array_item
68 * structure will hold an array of values extracted that can be
69 * indexed with the "atom number", which is an index into this
72 static struct used_atom
{
76 char color
[COLOR_MAXLEN
];
79 enum { RR_REF
, RR_TRACK
, RR_TRACKSHORT
} option
;
80 struct refname_atom refname
;
81 unsigned int nobracket
: 1;
84 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
85 struct process_trailer_options trailer_opts
;
89 cmp_status cmp_status
;
93 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
96 struct refname_atom refname
;
100 static int used_atom_cnt
, need_tagged
, need_symref
;
102 static void color_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *color_value
)
105 die(_("expected format: %%(color:<color>)"));
106 if (color_parse(color_value
, atom
->u
.color
) < 0)
107 die(_("unrecognized color: %%(color:%s)"), color_value
);
109 * We check this after we've parsed the color, which lets us complain
110 * about syntactically bogus color names even if they won't be used.
112 if (!want_color(format
->use_color
))
113 color_parse("", atom
->u
.color
);
116 static void refname_atom_parser_internal(struct refname_atom
*atom
,
117 const char *arg
, const char *name
)
120 atom
->option
= R_NORMAL
;
121 else if (!strcmp(arg
, "short"))
122 atom
->option
= R_SHORT
;
123 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
124 skip_prefix(arg
, "strip=", &arg
)) {
125 atom
->option
= R_LSTRIP
;
126 if (strtol_i(arg
, 10, &atom
->lstrip
))
127 die(_("Integer value expected refname:lstrip=%s"), arg
);
128 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
129 atom
->option
= R_RSTRIP
;
130 if (strtol_i(arg
, 10, &atom
->rstrip
))
131 die(_("Integer value expected refname:rstrip=%s"), arg
);
133 die(_("unrecognized %%(%s) argument: %s"), name
, arg
);
136 static void remote_ref_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
138 struct string_list params
= STRING_LIST_INIT_DUP
;
142 atom
->u
.remote_ref
.option
= RR_REF
;
143 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
148 atom
->u
.remote_ref
.nobracket
= 0;
149 string_list_split(¶ms
, arg
, ',', -1);
151 for (i
= 0; i
< params
.nr
; i
++) {
152 const char *s
= params
.items
[i
].string
;
154 if (!strcmp(s
, "track"))
155 atom
->u
.remote_ref
.option
= RR_TRACK
;
156 else if (!strcmp(s
, "trackshort"))
157 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
158 else if (!strcmp(s
, "nobracket"))
159 atom
->u
.remote_ref
.nobracket
= 1;
161 atom
->u
.remote_ref
.option
= RR_REF
;
162 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
167 string_list_clear(¶ms
, 0);
170 static void body_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
173 die(_("%%(body) does not take arguments"));
174 atom
->u
.contents
.option
= C_BODY_DEP
;
177 static void subject_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
180 die(_("%%(subject) does not take arguments"));
181 atom
->u
.contents
.option
= C_SUB
;
184 static void trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
186 struct string_list params
= STRING_LIST_INIT_DUP
;
190 string_list_split(¶ms
, arg
, ',', -1);
191 for (i
= 0; i
< params
.nr
; i
++) {
192 const char *s
= params
.items
[i
].string
;
193 if (!strcmp(s
, "unfold"))
194 atom
->u
.contents
.trailer_opts
.unfold
= 1;
195 else if (!strcmp(s
, "only"))
196 atom
->u
.contents
.trailer_opts
.only_trailers
= 1;
198 die(_("unknown %%(trailers) argument: %s"), s
);
201 atom
->u
.contents
.option
= C_TRAILERS
;
202 string_list_clear(¶ms
, 0);
205 static void contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
208 atom
->u
.contents
.option
= C_BARE
;
209 else if (!strcmp(arg
, "body"))
210 atom
->u
.contents
.option
= C_BODY
;
211 else if (!strcmp(arg
, "signature"))
212 atom
->u
.contents
.option
= C_SIG
;
213 else if (!strcmp(arg
, "subject"))
214 atom
->u
.contents
.option
= C_SUB
;
215 else if (skip_prefix(arg
, "trailers", &arg
)) {
216 skip_prefix(arg
, ":", &arg
);
217 trailers_atom_parser(format
, atom
, *arg
? arg
: NULL
);
218 } else if (skip_prefix(arg
, "lines=", &arg
)) {
219 atom
->u
.contents
.option
= C_LINES
;
220 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
221 die(_("positive value expected contents:lines=%s"), arg
);
223 die(_("unrecognized %%(contents) argument: %s"), arg
);
226 static void objectname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
229 atom
->u
.objectname
.option
= O_FULL
;
230 else if (!strcmp(arg
, "short"))
231 atom
->u
.objectname
.option
= O_SHORT
;
232 else if (skip_prefix(arg
, "short=", &arg
)) {
233 atom
->u
.objectname
.option
= O_LENGTH
;
234 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
235 atom
->u
.objectname
.length
== 0)
236 die(_("positive value expected objectname:short=%s"), arg
);
237 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
238 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
240 die(_("unrecognized %%(objectname) argument: %s"), arg
);
243 static void refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
245 refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
);
248 static align_type
parse_align_position(const char *s
)
250 if (!strcmp(s
, "right"))
252 else if (!strcmp(s
, "middle"))
254 else if (!strcmp(s
, "left"))
259 static void align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
261 struct align
*align
= &atom
->u
.align
;
262 struct string_list params
= STRING_LIST_INIT_DUP
;
264 unsigned int width
= ~0U;
267 die(_("expected format: %%(align:<width>,<position>)"));
269 align
->position
= ALIGN_LEFT
;
271 string_list_split(¶ms
, arg
, ',', -1);
272 for (i
= 0; i
< params
.nr
; i
++) {
273 const char *s
= params
.items
[i
].string
;
276 if (skip_prefix(s
, "position=", &s
)) {
277 position
= parse_align_position(s
);
279 die(_("unrecognized position:%s"), s
);
280 align
->position
= position
;
281 } else if (skip_prefix(s
, "width=", &s
)) {
282 if (strtoul_ui(s
, 10, &width
))
283 die(_("unrecognized width:%s"), s
);
284 } else if (!strtoul_ui(s
, 10, &width
))
286 else if ((position
= parse_align_position(s
)) >= 0)
287 align
->position
= position
;
289 die(_("unrecognized %%(align) argument: %s"), s
);
293 die(_("positive width expected with the %%(align) atom"));
294 align
->width
= width
;
295 string_list_clear(¶ms
, 0);
298 static void if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
301 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
303 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
304 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
305 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
306 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
308 die(_("unrecognized %%(if) argument: %s"), arg
);
312 static void head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
314 struct object_id unused
;
316 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, unused
.hash
, NULL
);
322 void (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
);
324 { "refname" , FIELD_STR
, refname_atom_parser
},
326 { "objectsize", FIELD_ULONG
},
327 { "objectname", FIELD_STR
, objectname_atom_parser
},
330 { "numparent", FIELD_ULONG
},
337 { "authordate", FIELD_TIME
},
340 { "committeremail" },
341 { "committerdate", FIELD_TIME
},
345 { "taggerdate", FIELD_TIME
},
347 { "creatordate", FIELD_TIME
},
348 { "subject", FIELD_STR
, subject_atom_parser
},
349 { "body", FIELD_STR
, body_atom_parser
},
350 { "trailers", FIELD_STR
, trailers_atom_parser
},
351 { "contents", FIELD_STR
, contents_atom_parser
},
352 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
353 { "push", FIELD_STR
, remote_ref_atom_parser
},
354 { "symref", FIELD_STR
, refname_atom_parser
},
356 { "HEAD", FIELD_STR
, head_atom_parser
},
357 { "color", FIELD_STR
, color_atom_parser
},
358 { "align", FIELD_STR
, align_atom_parser
},
360 { "if", FIELD_STR
, if_atom_parser
},
365 #define REF_FORMATTING_STATE_INIT { 0, NULL }
367 struct ref_formatting_stack
{
368 struct ref_formatting_stack
*prev
;
369 struct strbuf output
;
370 void (*at_end
)(struct ref_formatting_stack
**stack
);
374 struct ref_formatting_state
{
376 struct ref_formatting_stack
*stack
;
381 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
382 uintmax_t value
; /* used for sorting when not FIELD_STR */
383 struct used_atom
*atom
;
387 * Used to parse format string and sort specifiers
389 static int parse_ref_filter_atom(const struct ref_format
*format
,
390 const char *atom
, const char *ep
)
397 if (*sp
== '*' && sp
< ep
)
400 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
402 /* Do we have the atom already used elsewhere? */
403 for (i
= 0; i
< used_atom_cnt
; i
++) {
404 int len
= strlen(used_atom
[i
].name
);
405 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
410 * If the atom name has a colon, strip it and everything after
411 * it off - it specifies the format for this entry, and
412 * shouldn't be used for checking against the valid_atom
415 arg
= memchr(sp
, ':', ep
- sp
);
416 atom_len
= (arg
? arg
: ep
) - sp
;
418 /* Is the atom a valid one? */
419 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
420 int len
= strlen(valid_atom
[i
].name
);
421 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
425 if (ARRAY_SIZE(valid_atom
) <= i
)
426 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
428 /* Add it in, including the deref prefix */
431 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
432 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
433 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
435 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
436 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
437 if (valid_atom
[i
].parser
)
438 valid_atom
[i
].parser(format
, &used_atom
[at
], arg
);
441 if (!strcmp(valid_atom
[i
].name
, "symref"))
446 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
448 switch (quote_style
) {
450 strbuf_addstr(s
, str
);
453 sq_quote_buf(s
, str
);
456 perl_quote_buf(s
, str
);
459 python_quote_buf(s
, str
);
462 tcl_quote_buf(s
, str
);
467 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
470 * Quote formatting is only done when the stack has a single
471 * element. Otherwise quote formatting is done on the
472 * element's entire output strbuf when the %(end) atom is
475 if (!state
->stack
->prev
)
476 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
478 strbuf_addstr(&state
->stack
->output
, v
->s
);
481 static void push_stack_element(struct ref_formatting_stack
**stack
)
483 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
485 strbuf_init(&s
->output
, 0);
490 static void pop_stack_element(struct ref_formatting_stack
**stack
)
492 struct ref_formatting_stack
*current
= *stack
;
493 struct ref_formatting_stack
*prev
= current
->prev
;
496 strbuf_addbuf(&prev
->output
, ¤t
->output
);
497 strbuf_release(¤t
->output
);
502 static void end_align_handler(struct ref_formatting_stack
**stack
)
504 struct ref_formatting_stack
*cur
= *stack
;
505 struct align
*align
= (struct align
*)cur
->at_end_data
;
506 struct strbuf s
= STRBUF_INIT
;
508 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
509 strbuf_swap(&cur
->output
, &s
);
513 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
515 struct ref_formatting_stack
*new;
517 push_stack_element(&state
->stack
);
519 new->at_end
= end_align_handler
;
520 new->at_end_data
= &atomv
->atom
->u
.align
;
523 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
525 struct ref_formatting_stack
*cur
= *stack
;
526 struct ref_formatting_stack
*prev
= cur
->prev
;
527 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
529 if (!if_then_else
->then_atom_seen
)
530 die(_("format: %%(if) atom used without a %%(then) atom"));
532 if (if_then_else
->else_atom_seen
) {
534 * There is an %(else) atom: we need to drop one state from the
535 * stack, either the %(else) branch if the condition is satisfied, or
536 * the %(then) branch if it isn't.
538 if (if_then_else
->condition_satisfied
) {
539 strbuf_reset(&cur
->output
);
540 pop_stack_element(&cur
);
542 strbuf_swap(&cur
->output
, &prev
->output
);
543 strbuf_reset(&cur
->output
);
544 pop_stack_element(&cur
);
546 } else if (!if_then_else
->condition_satisfied
) {
548 * No %(else) atom: just drop the %(then) branch if the
549 * condition is not satisfied.
551 strbuf_reset(&cur
->output
);
558 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
560 struct ref_formatting_stack
*new;
561 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
563 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
564 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
566 push_stack_element(&state
->stack
);
568 new->at_end
= if_then_else_handler
;
569 new->at_end_data
= if_then_else
;
572 static int is_empty(const char *s
)
582 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
584 struct ref_formatting_stack
*cur
= state
->stack
;
585 struct if_then_else
*if_then_else
= NULL
;
587 if (cur
->at_end
== if_then_else_handler
)
588 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
590 die(_("format: %%(then) atom used without an %%(if) atom"));
591 if (if_then_else
->then_atom_seen
)
592 die(_("format: %%(then) atom used more than once"));
593 if (if_then_else
->else_atom_seen
)
594 die(_("format: %%(then) atom used after %%(else)"));
595 if_then_else
->then_atom_seen
= 1;
597 * If the 'equals' or 'notequals' attribute is used then
598 * perform the required comparison. If not, only non-empty
599 * strings satisfy the 'if' condition.
601 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
602 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
603 if_then_else
->condition_satisfied
= 1;
604 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
605 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
606 if_then_else
->condition_satisfied
= 1;
607 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
608 if_then_else
->condition_satisfied
= 1;
609 strbuf_reset(&cur
->output
);
612 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
614 struct ref_formatting_stack
*prev
= state
->stack
;
615 struct if_then_else
*if_then_else
= NULL
;
617 if (prev
->at_end
== if_then_else_handler
)
618 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
620 die(_("format: %%(else) atom used without an %%(if) atom"));
621 if (!if_then_else
->then_atom_seen
)
622 die(_("format: %%(else) atom used without a %%(then) atom"));
623 if (if_then_else
->else_atom_seen
)
624 die(_("format: %%(else) atom used more than once"));
625 if_then_else
->else_atom_seen
= 1;
626 push_stack_element(&state
->stack
);
627 state
->stack
->at_end_data
= prev
->at_end_data
;
628 state
->stack
->at_end
= prev
->at_end
;
631 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
633 struct ref_formatting_stack
*current
= state
->stack
;
634 struct strbuf s
= STRBUF_INIT
;
636 if (!current
->at_end
)
637 die(_("format: %%(end) atom used without corresponding atom"));
638 current
->at_end(&state
->stack
);
640 /* Stack may have been popped within at_end(), hence reset the current pointer */
641 current
= state
->stack
;
644 * Perform quote formatting when the stack element is that of
645 * a supporting atom. If nested then perform quote formatting
646 * only on the topmost supporting atom.
648 if (!current
->prev
->prev
) {
649 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
650 strbuf_swap(¤t
->output
, &s
);
653 pop_stack_element(&state
->stack
);
657 * In a format string, find the next occurrence of %(atom).
659 static const char *find_next(const char *cp
)
664 * %( is the start of an atom;
665 * %% is a quoted per-cent.
669 else if (cp
[1] == '%')
670 cp
++; /* skip over two % */
671 /* otherwise this is a singleton, literal % */
679 * Make sure the format string is well formed, and parse out
682 int verify_ref_format(struct ref_format
*format
)
686 format
->need_color_reset_at_eol
= 0;
687 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
688 const char *color
, *ep
= strchr(sp
, ')');
692 return error(_("malformed format string %s"), sp
);
693 /* sp points at "%(" and ep points at the closing ")" */
694 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
);
697 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
698 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
700 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
701 format
->need_color_reset_at_eol
= 0;
706 * Given an object name, read the object data and size, and return a
707 * "struct object". If the object data we are returning is also borrowed
708 * by the "struct object" representation, set *eaten as well---it is a
709 * signal from parse_object_buffer to us not to free the buffer.
711 static void *get_obj(const struct object_id
*oid
, struct object
**obj
, unsigned long *sz
, int *eaten
)
713 enum object_type type
;
714 void *buf
= read_sha1_file(oid
->hash
, &type
, sz
);
717 *obj
= parse_object_buffer(oid
, type
, *sz
, buf
, eaten
);
723 static int grab_objectname(const char *name
, const unsigned char *sha1
,
724 struct atom_value
*v
, struct used_atom
*atom
)
726 if (starts_with(name
, "objectname")) {
727 if (atom
->u
.objectname
.option
== O_SHORT
) {
728 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
730 } else if (atom
->u
.objectname
.option
== O_FULL
) {
731 v
->s
= xstrdup(sha1_to_hex(sha1
));
733 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
734 v
->s
= xstrdup(find_unique_abbrev(sha1
, atom
->u
.objectname
.length
));
737 die("BUG: unknown %%(objectname) option");
742 /* See grab_values */
743 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
747 for (i
= 0; i
< used_atom_cnt
; i
++) {
748 const char *name
= used_atom
[i
].name
;
749 struct atom_value
*v
= &val
[i
];
750 if (!!deref
!= (*name
== '*'))
754 if (!strcmp(name
, "objecttype"))
755 v
->s
= typename(obj
->type
);
756 else if (!strcmp(name
, "objectsize")) {
758 v
->s
= xstrfmt("%lu", sz
);
761 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
765 /* See grab_values */
766 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
769 struct tag
*tag
= (struct tag
*) obj
;
771 for (i
= 0; i
< used_atom_cnt
; i
++) {
772 const char *name
= used_atom
[i
].name
;
773 struct atom_value
*v
= &val
[i
];
774 if (!!deref
!= (*name
== '*'))
778 if (!strcmp(name
, "tag"))
780 else if (!strcmp(name
, "type") && tag
->tagged
)
781 v
->s
= typename(tag
->tagged
->type
);
782 else if (!strcmp(name
, "object") && tag
->tagged
)
783 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
787 /* See grab_values */
788 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
791 struct commit
*commit
= (struct commit
*) obj
;
793 for (i
= 0; i
< used_atom_cnt
; i
++) {
794 const char *name
= used_atom
[i
].name
;
795 struct atom_value
*v
= &val
[i
];
796 if (!!deref
!= (*name
== '*'))
800 if (!strcmp(name
, "tree")) {
801 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
803 else if (!strcmp(name
, "numparent")) {
804 v
->value
= commit_list_count(commit
->parents
);
805 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
807 else if (!strcmp(name
, "parent")) {
808 struct commit_list
*parents
;
809 struct strbuf s
= STRBUF_INIT
;
810 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
811 struct commit
*parent
= parents
->item
;
812 if (parents
!= commit
->parents
)
813 strbuf_addch(&s
, ' ');
814 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
816 v
->s
= strbuf_detach(&s
, NULL
);
821 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
825 if (!strncmp(buf
, who
, wholen
) &&
827 return buf
+ wholen
+ 1;
828 eol
= strchr(buf
, '\n');
833 return ""; /* end of header */
839 static const char *copy_line(const char *buf
)
841 const char *eol
= strchrnul(buf
, '\n');
842 return xmemdupz(buf
, eol
- buf
);
845 static const char *copy_name(const char *buf
)
848 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
849 if (!strncmp(cp
, " <", 2))
850 return xmemdupz(buf
, cp
- buf
);
855 static const char *copy_email(const char *buf
)
857 const char *email
= strchr(buf
, '<');
861 eoemail
= strchr(email
, '>');
864 return xmemdupz(email
, eoemail
+ 1 - email
);
867 static char *copy_subject(const char *buf
, unsigned long len
)
869 char *r
= xmemdupz(buf
, len
);
872 for (i
= 0; i
< len
; i
++)
879 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
881 const char *eoemail
= strstr(buf
, "> ");
883 timestamp_t timestamp
;
885 struct date_mode date_mode
= { DATE_NORMAL
};
889 * We got here because atomname ends in "date" or "date<something>";
890 * it's not possible that <something> is not ":<format>" because
891 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
892 * ":" means no format is specified, and use the default.
894 formatp
= strchr(atomname
, ':');
895 if (formatp
!= NULL
) {
897 parse_date_format(formatp
, &date_mode
);
902 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
903 if (timestamp
== TIME_MAX
)
905 tz
= strtol(zone
, NULL
, 10);
906 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
908 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
909 v
->value
= timestamp
;
916 /* See grab_values */
917 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
920 int wholen
= strlen(who
);
921 const char *wholine
= NULL
;
923 for (i
= 0; i
< used_atom_cnt
; i
++) {
924 const char *name
= used_atom
[i
].name
;
925 struct atom_value
*v
= &val
[i
];
926 if (!!deref
!= (*name
== '*'))
930 if (strncmp(who
, name
, wholen
))
932 if (name
[wholen
] != 0 &&
933 strcmp(name
+ wholen
, "name") &&
934 strcmp(name
+ wholen
, "email") &&
935 !starts_with(name
+ wholen
, "date"))
938 wholine
= find_wholine(who
, wholen
, buf
, sz
);
940 return; /* no point looking for it */
941 if (name
[wholen
] == 0)
942 v
->s
= copy_line(wholine
);
943 else if (!strcmp(name
+ wholen
, "name"))
944 v
->s
= copy_name(wholine
);
945 else if (!strcmp(name
+ wholen
, "email"))
946 v
->s
= copy_email(wholine
);
947 else if (starts_with(name
+ wholen
, "date"))
948 grab_date(wholine
, v
, name
);
952 * For a tag or a commit object, if "creator" or "creatordate" is
953 * requested, do something special.
955 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
956 return; /* "author" for commit object is not wanted */
958 wholine
= find_wholine(who
, wholen
, buf
, sz
);
961 for (i
= 0; i
< used_atom_cnt
; i
++) {
962 const char *name
= used_atom
[i
].name
;
963 struct atom_value
*v
= &val
[i
];
964 if (!!deref
!= (*name
== '*'))
969 if (starts_with(name
, "creatordate"))
970 grab_date(wholine
, v
, name
);
971 else if (!strcmp(name
, "creator"))
972 v
->s
= copy_line(wholine
);
976 static void find_subpos(const char *buf
, unsigned long sz
,
977 const char **sub
, unsigned long *sublen
,
978 const char **body
, unsigned long *bodylen
,
979 unsigned long *nonsiglen
,
980 const char **sig
, unsigned long *siglen
)
983 /* skip past header until we hit empty line */
984 while (*buf
&& *buf
!= '\n') {
985 eol
= strchrnul(buf
, '\n');
990 /* skip any empty lines */
994 /* parse signature first; we might not even have a subject line */
995 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
996 *siglen
= strlen(*sig
);
998 /* subject is first non-empty line */
1000 /* subject goes to first empty line */
1001 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
1002 eol
= strchrnul(buf
, '\n');
1007 *sublen
= buf
- *sub
;
1008 /* drop trailing newline, if present */
1009 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1012 /* skip any empty lines */
1013 while (*buf
== '\n')
1016 *bodylen
= strlen(buf
);
1017 *nonsiglen
= *sig
- buf
;
1021 * If 'lines' is greater than 0, append that many lines from the given
1022 * 'buf' of length 'size' to the given strbuf.
1024 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1027 const char *sp
, *eol
;
1032 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1034 strbuf_addstr(out
, "\n ");
1035 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1036 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1037 strbuf_add(out
, sp
, len
);
1044 /* See grab_values */
1045 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1048 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1049 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1051 for (i
= 0; i
< used_atom_cnt
; i
++) {
1052 struct used_atom
*atom
= &used_atom
[i
];
1053 const char *name
= atom
->name
;
1054 struct atom_value
*v
= &val
[i
];
1055 if (!!deref
!= (*name
== '*'))
1059 if (strcmp(name
, "subject") &&
1060 strcmp(name
, "body") &&
1061 !starts_with(name
, "trailers") &&
1062 !starts_with(name
, "contents"))
1065 find_subpos(buf
, sz
,
1067 &bodypos
, &bodylen
, &nonsiglen
,
1070 if (atom
->u
.contents
.option
== C_SUB
)
1071 v
->s
= copy_subject(subpos
, sublen
);
1072 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1073 v
->s
= xmemdupz(bodypos
, bodylen
);
1074 else if (atom
->u
.contents
.option
== C_BODY
)
1075 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1076 else if (atom
->u
.contents
.option
== C_SIG
)
1077 v
->s
= xmemdupz(sigpos
, siglen
);
1078 else if (atom
->u
.contents
.option
== C_LINES
) {
1079 struct strbuf s
= STRBUF_INIT
;
1080 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1082 /* Size is the length of the message after removing the signature */
1083 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1084 v
->s
= strbuf_detach(&s
, NULL
);
1085 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1086 struct strbuf s
= STRBUF_INIT
;
1088 /* Format the trailer info according to the trailer_opts given */
1089 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1091 v
->s
= strbuf_detach(&s
, NULL
);
1092 } else if (atom
->u
.contents
.option
== C_BARE
)
1093 v
->s
= xstrdup(subpos
);
1098 * We want to have empty print-string for field requests
1099 * that do not apply (e.g. "authordate" for a tag object)
1101 static void fill_missing_values(struct atom_value
*val
)
1104 for (i
= 0; i
< used_atom_cnt
; i
++) {
1105 struct atom_value
*v
= &val
[i
];
1112 * val is a list of atom_value to hold returned values. Extract
1113 * the values for atoms in used_atom array out of (obj, buf, sz).
1114 * when deref is false, (obj, buf, sz) is the object that is
1115 * pointed at by the ref itself; otherwise it is the object the
1116 * ref (which is a tag) refers to.
1118 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1120 grab_common_values(val
, deref
, obj
, buf
, sz
);
1121 switch (obj
->type
) {
1123 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1124 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1125 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1128 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1129 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1130 grab_person("author", val
, deref
, obj
, buf
, sz
);
1131 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1134 /* grab_tree_values(val, deref, obj, buf, sz); */
1137 /* grab_blob_values(val, deref, obj, buf, sz); */
1140 die("Eh? Object of type %d?", obj
->type
);
1144 static inline char *copy_advance(char *dst
, const char *src
)
1151 static const char *lstrip_ref_components(const char *refname
, int len
)
1153 long remaining
= len
;
1154 const char *start
= refname
;
1158 const char *p
= refname
;
1160 /* Find total no of '/' separated path-components */
1161 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1164 * The number of components we need to strip is now
1165 * the total minus the components to be left (Plus one
1166 * because we count the number of '/', but the number
1167 * of components is one more than the no of '/').
1169 remaining
= i
+ len
+ 1;
1172 while (remaining
> 0) {
1185 static const char *rstrip_ref_components(const char *refname
, int len
)
1187 long remaining
= len
;
1188 char *start
= xstrdup(refname
);
1192 const char *p
= refname
;
1194 /* Find total no of '/' separated path-components */
1195 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1198 * The number of components we need to strip is now
1199 * the total minus the components to be left (Plus one
1200 * because we count the number of '/', but the number
1201 * of components is one more than the no of '/').
1203 remaining
= i
+ len
+ 1;
1206 while (remaining
-- > 0) {
1207 char *p
= strrchr(start
, '/');
1216 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1218 if (atom
->option
== R_SHORT
)
1219 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1220 else if (atom
->option
== R_LSTRIP
)
1221 return lstrip_ref_components(refname
, atom
->lstrip
);
1222 else if (atom
->option
== R_RSTRIP
)
1223 return rstrip_ref_components(refname
, atom
->rstrip
);
1228 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1229 struct branch
*branch
, const char **s
)
1231 int num_ours
, num_theirs
;
1232 if (atom
->u
.remote_ref
.option
== RR_REF
)
1233 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1234 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1235 if (stat_tracking_info(branch
, &num_ours
,
1236 &num_theirs
, NULL
)) {
1237 *s
= xstrdup(msgs
.gone
);
1238 } else if (!num_ours
&& !num_theirs
)
1241 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1242 else if (!num_theirs
)
1243 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1245 *s
= xstrfmt(msgs
.ahead_behind
,
1246 num_ours
, num_theirs
);
1247 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1248 const char *to_free
= *s
;
1249 *s
= xstrfmt("[%s]", *s
);
1250 free((void *)to_free
);
1252 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1253 if (stat_tracking_info(branch
, &num_ours
,
1257 if (!num_ours
&& !num_theirs
)
1261 else if (!num_theirs
)
1266 die("BUG: unhandled RR_* enum");
1269 char *get_head_description(void)
1271 struct strbuf desc
= STRBUF_INIT
;
1272 struct wt_status_state state
;
1273 memset(&state
, 0, sizeof(state
));
1274 wt_status_get_state(&state
, 1);
1275 if (state
.rebase_in_progress
||
1276 state
.rebase_interactive_in_progress
)
1277 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1279 else if (state
.bisect_in_progress
)
1280 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1282 else if (state
.detached_from
) {
1283 if (state
.detached_at
)
1285 * TRANSLATORS: make sure this matches "HEAD
1286 * detached at " in wt-status.c
1288 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1289 state
.detached_from
);
1292 * TRANSLATORS: make sure this matches "HEAD
1293 * detached from " in wt-status.c
1295 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1296 state
.detached_from
);
1299 strbuf_addstr(&desc
, _("(no branch)"));
1302 free(state
.detached_from
);
1303 return strbuf_detach(&desc
, NULL
);
1306 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1311 return show_ref(&atom
->u
.refname
, ref
->symref
);
1314 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1316 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1317 return get_head_description();
1318 return show_ref(&atom
->u
.refname
, ref
->refname
);
1322 * Parse the object referred by ref, and grab needed value.
1324 static void populate_value(struct ref_array_item
*ref
)
1330 const struct object_id
*tagged
;
1332 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1334 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1335 struct object_id unused1
;
1336 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1337 unused1
.hash
, NULL
);
1342 /* Fill in specials first */
1343 for (i
= 0; i
< used_atom_cnt
; i
++) {
1344 struct used_atom
*atom
= &used_atom
[i
];
1345 const char *name
= used_atom
[i
].name
;
1346 struct atom_value
*v
= &ref
->value
[i
];
1348 const char *refname
;
1349 struct branch
*branch
= NULL
;
1351 v
->handler
= append_atom
;
1359 if (starts_with(name
, "refname"))
1360 refname
= get_refname(atom
, ref
);
1361 else if (starts_with(name
, "symref"))
1362 refname
= get_symref(atom
, ref
);
1363 else if (starts_with(name
, "upstream")) {
1364 const char *branch_name
;
1365 /* only local branches may have an upstream */
1366 if (!skip_prefix(ref
->refname
, "refs/heads/",
1369 branch
= branch_get(branch_name
);
1371 refname
= branch_get_upstream(branch
, NULL
);
1373 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1375 } else if (starts_with(name
, "push")) {
1376 const char *branch_name
;
1377 if (!skip_prefix(ref
->refname
, "refs/heads/",
1380 branch
= branch_get(branch_name
);
1382 refname
= branch_get_push(branch
, NULL
);
1385 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1387 } else if (starts_with(name
, "color:")) {
1388 v
->s
= atom
->u
.color
;
1390 } else if (!strcmp(name
, "flag")) {
1391 char buf
[256], *cp
= buf
;
1392 if (ref
->flag
& REF_ISSYMREF
)
1393 cp
= copy_advance(cp
, ",symref");
1394 if (ref
->flag
& REF_ISPACKED
)
1395 cp
= copy_advance(cp
, ",packed");
1400 v
->s
= xstrdup(buf
+ 1);
1403 } else if (!deref
&& grab_objectname(name
, ref
->objectname
.hash
, v
, atom
)) {
1405 } else if (!strcmp(name
, "HEAD")) {
1406 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1411 } else if (starts_with(name
, "align")) {
1412 v
->handler
= align_atom_handler
;
1414 } else if (!strcmp(name
, "end")) {
1415 v
->handler
= end_atom_handler
;
1417 } else if (starts_with(name
, "if")) {
1420 if (skip_prefix(name
, "if:", &s
))
1422 v
->handler
= if_atom_handler
;
1424 } else if (!strcmp(name
, "then")) {
1425 v
->handler
= then_atom_handler
;
1427 } else if (!strcmp(name
, "else")) {
1428 v
->handler
= else_atom_handler
;
1436 v
->s
= xstrfmt("%s^{}", refname
);
1439 for (i
= 0; i
< used_atom_cnt
; i
++) {
1440 struct atom_value
*v
= &ref
->value
[i
];
1447 buf
= get_obj(&ref
->objectname
, &obj
, &size
, &eaten
);
1449 die(_("missing object %s for %s"),
1450 oid_to_hex(&ref
->objectname
), ref
->refname
);
1452 die(_("parse_object_buffer failed on %s for %s"),
1453 oid_to_hex(&ref
->objectname
), ref
->refname
);
1455 grab_values(ref
->value
, 0, obj
, buf
, size
);
1460 * If there is no atom that wants to know about tagged
1461 * object, we are done.
1463 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1467 * If it is a tag object, see if we use a value that derefs
1468 * the object, and if we do grab the object it refers to.
1470 tagged
= &((struct tag
*)obj
)->tagged
->oid
;
1473 * NEEDSWORK: This derefs tag only once, which
1474 * is good to deal with chains of trust, but
1475 * is not consistent with what deref_tag() does
1476 * which peels the onion to the core.
1478 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1480 die(_("missing object %s for %s"),
1481 oid_to_hex(tagged
), ref
->refname
);
1483 die(_("parse_object_buffer failed on %s for %s"),
1484 oid_to_hex(tagged
), ref
->refname
);
1485 grab_values(ref
->value
, 1, obj
, buf
, size
);
1491 * Given a ref, return the value for the atom. This lazily gets value
1492 * out of the object by calling populate value.
1494 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1497 populate_value(ref
);
1498 fill_missing_values(ref
->value
);
1500 *v
= &ref
->value
[atom
];
1504 * Unknown has to be "0" here, because that's the default value for
1505 * contains_cache slab entries that have not yet been assigned.
1507 enum contains_result
{
1508 CONTAINS_UNKNOWN
= 0,
1513 define_commit_slab(contains_cache
, enum contains_result
);
1515 struct ref_filter_cbdata
{
1516 struct ref_array
*array
;
1517 struct ref_filter
*filter
;
1518 struct contains_cache contains_cache
;
1519 struct contains_cache no_contains_cache
;
1523 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1526 * At each recursion step, the stack items points to the commits whose
1527 * ancestors are to be inspected.
1529 struct contains_stack
{
1531 struct contains_stack_entry
{
1532 struct commit
*commit
;
1533 struct commit_list
*parents
;
1537 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1539 for (; want
; want
= want
->next
)
1540 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1546 * Test whether the candidate or one of its parents is contained in the list.
1547 * Do not recurse to find out, though, but return -1 if inconclusive.
1549 static enum contains_result
contains_test(struct commit
*candidate
,
1550 const struct commit_list
*want
,
1551 struct contains_cache
*cache
)
1553 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
1555 /* If we already have the answer cached, return that. */
1560 if (in_commit_list(want
, candidate
)) {
1561 *cached
= CONTAINS_YES
;
1562 return CONTAINS_YES
;
1565 /* Otherwise, we don't know; prepare to recurse */
1566 parse_commit_or_die(candidate
);
1567 return CONTAINS_UNKNOWN
;
1570 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1572 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1573 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1574 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1577 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1578 const struct commit_list
*want
,
1579 struct contains_cache
*cache
)
1581 struct contains_stack contains_stack
= { 0, 0, NULL
};
1582 enum contains_result result
= contains_test(candidate
, want
, cache
);
1584 if (result
!= CONTAINS_UNKNOWN
)
1587 push_to_contains_stack(candidate
, &contains_stack
);
1588 while (contains_stack
.nr
) {
1589 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1590 struct commit
*commit
= entry
->commit
;
1591 struct commit_list
*parents
= entry
->parents
;
1594 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
1595 contains_stack
.nr
--;
1598 * If we just popped the stack, parents->item has been marked,
1599 * therefore contains_test will return a meaningful yes/no.
1601 else switch (contains_test(parents
->item
, want
, cache
)) {
1603 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
1604 contains_stack
.nr
--;
1607 entry
->parents
= parents
->next
;
1609 case CONTAINS_UNKNOWN
:
1610 push_to_contains_stack(parents
->item
, &contains_stack
);
1614 free(contains_stack
.contains_stack
);
1615 return contains_test(candidate
, want
, cache
);
1618 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
1619 struct commit_list
*list
, struct contains_cache
*cache
)
1621 if (filter
->with_commit_tag_algo
)
1622 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
1623 return is_descendant_of(commit
, list
);
1627 * Return 1 if the refname matches one of the patterns, otherwise 0.
1628 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1629 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1630 * matches "refs/heads/mas*", too).
1632 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1634 const char **patterns
= filter
->name_patterns
;
1637 if (filter
->ignore_case
)
1638 flags
|= WM_CASEFOLD
;
1641 * When no '--format' option is given we need to skip the prefix
1642 * for matching refs of tags and branches.
1644 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1645 skip_prefix(refname
, "refs/heads/", &refname
) ||
1646 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1647 skip_prefix(refname
, "refs/", &refname
));
1649 for (; *patterns
; patterns
++) {
1650 if (!wildmatch(*patterns
, refname
, flags
))
1657 * Return 1 if the refname matches one of the patterns, otherwise 0.
1658 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1659 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1660 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1662 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1664 const char **pattern
= filter
->name_patterns
;
1665 int namelen
= strlen(refname
);
1666 unsigned flags
= WM_PATHNAME
;
1668 if (filter
->ignore_case
)
1669 flags
|= WM_CASEFOLD
;
1671 for (; *pattern
; pattern
++) {
1672 const char *p
= *pattern
;
1673 int plen
= strlen(p
);
1675 if ((plen
<= namelen
) &&
1676 !strncmp(refname
, p
, plen
) &&
1677 (refname
[plen
] == '\0' ||
1678 refname
[plen
] == '/' ||
1681 if (!wildmatch(p
, refname
, WM_PATHNAME
))
1687 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1688 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1690 if (!*filter
->name_patterns
)
1691 return 1; /* No pattern always matches */
1692 if (filter
->match_as_path
)
1693 return match_name_as_path(filter
, refname
);
1694 return match_pattern(filter
, refname
);
1698 * Find the longest prefix of pattern we can pass to
1699 * `for_each_fullref_in()`, namely the part of pattern preceding the
1700 * first glob character. (Note that `for_each_fullref_in()` is
1701 * perfectly happy working with a prefix that doesn't end at a
1702 * pathname component boundary.)
1704 static void find_longest_prefix(struct strbuf
*out
, const char *pattern
)
1708 for (p
= pattern
; *p
&& !is_glob_special(*p
); p
++)
1711 strbuf_add(out
, pattern
, p
- pattern
);
1715 * This is the same as for_each_fullref_in(), but it tries to iterate
1716 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1717 * pattern match, so the callback still has to match each ref individually.
1719 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1724 struct strbuf prefix
= STRBUF_INIT
;
1727 if (!filter
->match_as_path
) {
1729 * in this case, the patterns are applied after
1730 * prefixes like "refs/heads/" etc. are stripped off,
1731 * so we have to look at everything:
1733 return for_each_fullref_in("", cb
, cb_data
, broken
);
1736 if (!filter
->name_patterns
[0]) {
1737 /* no patterns; we have to look at everything */
1738 return for_each_fullref_in("", cb
, cb_data
, broken
);
1741 if (filter
->name_patterns
[1]) {
1743 * multiple patterns; in theory this could still work as long
1744 * as the patterns are disjoint. We'd just make multiple calls
1745 * to for_each_ref(). But if they're not disjoint, we'd end up
1746 * reporting the same ref multiple times. So let's punt on that
1749 return for_each_fullref_in("", cb
, cb_data
, broken
);
1752 find_longest_prefix(&prefix
, filter
->name_patterns
[0]);
1754 ret
= for_each_fullref_in(prefix
.buf
, cb
, cb_data
, broken
);
1755 strbuf_release(&prefix
);
1760 * Given a ref (sha1, refname), check if the ref belongs to the array
1761 * of sha1s. If the given ref is a tag, check if the given tag points
1762 * at one of the sha1s in the given sha1 array.
1763 * the given sha1_array.
1765 * 1. Only a single level of inderection is obtained, we might want to
1766 * change this to account for multiple levels (e.g. annotated tags
1767 * pointing to annotated tags pointing to a commit.)
1768 * 2. As the refs are cached we might know what refname peels to without
1769 * the need to parse the object via parse_object(). peel_ref() might be a
1770 * more efficient alternative to obtain the pointee.
1772 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1773 const struct object_id
*oid
,
1774 const char *refname
)
1776 const struct object_id
*tagged_oid
= NULL
;
1779 if (oid_array_lookup(points_at
, oid
) >= 0)
1781 obj
= parse_object(oid
);
1783 die(_("malformed object at '%s'"), refname
);
1784 if (obj
->type
== OBJ_TAG
)
1785 tagged_oid
= &((struct tag
*)obj
)->tagged
->oid
;
1786 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
1791 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1792 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1793 const unsigned char *objectname
,
1796 struct ref_array_item
*ref
;
1797 FLEX_ALLOC_STR(ref
, refname
, refname
);
1798 hashcpy(ref
->objectname
.hash
, objectname
);
1804 static int ref_kind_from_refname(const char *refname
)
1812 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1813 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1814 { "refs/tags/", FILTER_REFS_TAGS
}
1817 if (!strcmp(refname
, "HEAD"))
1818 return FILTER_REFS_DETACHED_HEAD
;
1820 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1821 if (starts_with(refname
, ref_kind
[i
].prefix
))
1822 return ref_kind
[i
].kind
;
1825 return FILTER_REFS_OTHERS
;
1828 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1830 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1831 filter
->kind
== FILTER_REFS_REMOTES
||
1832 filter
->kind
== FILTER_REFS_TAGS
)
1833 return filter
->kind
;
1834 return ref_kind_from_refname(refname
);
1838 * A call-back given to for_each_ref(). Filter refs and keep them for
1839 * later object processing.
1841 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1843 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1844 struct ref_filter
*filter
= ref_cbdata
->filter
;
1845 struct ref_array_item
*ref
;
1846 struct commit
*commit
= NULL
;
1849 if (flag
& REF_BAD_NAME
) {
1850 warning(_("ignoring ref with broken name %s"), refname
);
1854 if (flag
& REF_ISBROKEN
) {
1855 warning(_("ignoring broken ref %s"), refname
);
1859 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1860 kind
= filter_ref_kind(filter
, refname
);
1861 if (!(kind
& filter
->kind
))
1864 if (!filter_pattern_match(filter
, refname
))
1867 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
1871 * A merge filter is applied on refs pointing to commits. Hence
1872 * obtain the commit using the 'oid' available and discard all
1873 * non-commits early. The actual filtering is done later.
1875 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
1876 commit
= lookup_commit_reference_gently(oid
, 1);
1879 /* We perform the filtering for the '--contains' option... */
1880 if (filter
->with_commit
&&
1881 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
1883 /* ...or for the `--no-contains' option */
1884 if (filter
->no_commit
&&
1885 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
1890 * We do not open the object yet; sort may only need refname
1891 * to do its job and the resulting list may yet to be pruned
1892 * by maxcount logic.
1894 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1895 ref
->commit
= commit
;
1897 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1898 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1903 /* Free memory allocated for a ref_array_item */
1904 static void free_array_item(struct ref_array_item
*item
)
1906 free((char *)item
->symref
);
1910 /* Free all memory allocated for ref_array */
1911 void ref_array_clear(struct ref_array
*array
)
1915 for (i
= 0; i
< array
->nr
; i
++)
1916 free_array_item(array
->items
[i
]);
1917 FREE_AND_NULL(array
->items
);
1918 array
->nr
= array
->alloc
= 0;
1921 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1923 struct rev_info revs
;
1925 struct ref_filter
*filter
= ref_cbdata
->filter
;
1926 struct ref_array
*array
= ref_cbdata
->array
;
1927 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1929 init_revisions(&revs
, NULL
);
1931 for (i
= 0; i
< array
->nr
; i
++) {
1932 struct ref_array_item
*item
= array
->items
[i
];
1933 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1934 to_clear
[i
] = item
->commit
;
1937 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1938 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1941 if (prepare_revision_walk(&revs
))
1942 die(_("revision walk setup failed"));
1947 for (i
= 0; i
< old_nr
; i
++) {
1948 struct ref_array_item
*item
= array
->items
[i
];
1949 struct commit
*commit
= item
->commit
;
1951 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1953 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1954 array
->items
[array
->nr
++] = array
->items
[i
];
1956 free_array_item(item
);
1959 for (i
= 0; i
< old_nr
; i
++)
1960 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1961 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1966 * API for filtering a set of refs. Based on the type of refs the user
1967 * has requested, we iterate through those refs and apply filters
1968 * as per the given ref_filter structure and finally store the
1969 * filtered refs in the ref_array structure.
1971 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1973 struct ref_filter_cbdata ref_cbdata
;
1975 unsigned int broken
= 0;
1977 ref_cbdata
.array
= array
;
1978 ref_cbdata
.filter
= filter
;
1980 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1982 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1984 init_contains_cache(&ref_cbdata
.contains_cache
);
1985 init_contains_cache(&ref_cbdata
.no_contains_cache
);
1987 /* Simple per-ref filtering */
1989 die("filter_refs: invalid type");
1992 * For common cases where we need only branches or remotes or tags,
1993 * we only iterate through those refs. If a mix of refs is needed,
1994 * we iterate over all refs and filter out required refs with the help
1995 * of filter_ref_kind().
1997 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1998 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1999 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2000 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2001 else if (filter
->kind
== FILTER_REFS_TAGS
)
2002 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2003 else if (filter
->kind
& FILTER_REFS_ALL
)
2004 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2005 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2006 head_ref(ref_filter_handler
, &ref_cbdata
);
2009 clear_contains_cache(&ref_cbdata
.contains_cache
);
2010 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2012 /* Filters that need revision walking */
2013 if (filter
->merge_commit
)
2014 do_merge_filter(&ref_cbdata
);
2019 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2021 struct atom_value
*va
, *vb
;
2023 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2024 int (*cmp_fn
)(const char *, const char *);
2026 get_ref_atom_value(a
, s
->atom
, &va
);
2027 get_ref_atom_value(b
, s
->atom
, &vb
);
2028 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2030 cmp
= versioncmp(va
->s
, vb
->s
);
2031 else if (cmp_type
== FIELD_STR
)
2032 cmp
= cmp_fn(va
->s
, vb
->s
);
2034 if (va
->value
< vb
->value
)
2036 else if (va
->value
== vb
->value
)
2037 cmp
= cmp_fn(a
->refname
, b
->refname
);
2042 return (s
->reverse
) ? -cmp
: cmp
;
2045 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2047 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2048 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2049 struct ref_sorting
*s
;
2051 for (s
= ref_sorting
; s
; s
= s
->next
) {
2052 int cmp
= cmp_ref_sorting(s
, a
, b
);
2059 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2061 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2064 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2066 struct strbuf
*s
= &state
->stack
->output
;
2068 while (*cp
&& (!ep
|| cp
< ep
)) {
2073 int ch
= hex2chr(cp
+ 1);
2075 strbuf_addch(s
, ch
);
2081 strbuf_addch(s
, *cp
);
2086 void format_ref_array_item(struct ref_array_item
*info
,
2087 const struct ref_format
*format
,
2088 struct strbuf
*final_buf
)
2090 const char *cp
, *sp
, *ep
;
2091 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2093 state
.quote_style
= format
->quote_style
;
2094 push_stack_element(&state
.stack
);
2096 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2097 struct atom_value
*atomv
;
2099 ep
= strchr(sp
, ')');
2101 append_literal(cp
, sp
, &state
);
2102 get_ref_atom_value(info
,
2103 parse_ref_filter_atom(format
, sp
+ 2, ep
),
2105 atomv
->handler(atomv
, &state
);
2108 sp
= cp
+ strlen(cp
);
2109 append_literal(cp
, sp
, &state
);
2111 if (format
->need_color_reset_at_eol
) {
2112 struct atom_value resetv
;
2113 resetv
.s
= GIT_COLOR_RESET
;
2114 append_atom(&resetv
, &state
);
2116 if (state
.stack
->prev
)
2117 die(_("format: %%(end) atom missing"));
2118 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2119 pop_stack_element(&state
.stack
);
2122 void show_ref_array_item(struct ref_array_item
*info
,
2123 const struct ref_format
*format
)
2125 struct strbuf final_buf
= STRBUF_INIT
;
2127 format_ref_array_item(info
, format
, &final_buf
);
2128 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2129 strbuf_release(&final_buf
);
2133 void pretty_print_ref(const char *name
, const unsigned char *sha1
,
2134 const struct ref_format
*format
)
2136 struct ref_array_item
*ref_item
;
2137 ref_item
= new_ref_array_item(name
, sha1
, 0);
2138 ref_item
->kind
= ref_kind_from_refname(name
);
2139 show_ref_array_item(ref_item
, format
);
2140 free_array_item(ref_item
);
2143 static int parse_sorting_atom(const char *atom
)
2146 * This parses an atom using a dummy ref_format, since we don't
2147 * actually care about the formatting details.
2149 struct ref_format dummy
= REF_FORMAT_INIT
;
2150 const char *end
= atom
+ strlen(atom
);
2151 return parse_ref_filter_atom(&dummy
, atom
, end
);
2154 /* If no sorting option is given, use refname to sort as default */
2155 struct ref_sorting
*ref_default_sorting(void)
2157 static const char cstr_name
[] = "refname";
2159 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2161 sorting
->next
= NULL
;
2162 sorting
->atom
= parse_sorting_atom(cstr_name
);
2166 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2168 struct ref_sorting
*s
;
2170 s
= xcalloc(1, sizeof(*s
));
2171 s
->next
= *sorting_tail
;
2178 if (skip_prefix(arg
, "version:", &arg
) ||
2179 skip_prefix(arg
, "v:", &arg
))
2181 s
->atom
= parse_sorting_atom(arg
);
2184 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2186 if (!arg
) /* should --no-sort void the list ? */
2188 parse_ref_sorting(opt
->value
, arg
);
2192 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2194 struct ref_filter
*rf
= opt
->value
;
2195 struct object_id oid
;
2196 int no_merged
= starts_with(opt
->long_name
, "no");
2200 return opterror(opt
, "is incompatible with --merged", 0);
2202 return opterror(opt
, "is incompatible with --no-merged", 0);
2206 rf
->merge
= no_merged
2207 ? REF_FILTER_MERGED_OMIT
2208 : REF_FILTER_MERGED_INCLUDE
;
2210 if (get_oid(arg
, &oid
))
2211 die(_("malformed object name %s"), arg
);
2213 rf
->merge_commit
= lookup_commit_reference_gently(&oid
, 0);
2214 if (!rf
->merge_commit
)
2215 return opterror(opt
, "must point to a commit", 0);