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 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
320 void (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
);
322 { "refname" , FIELD_STR
, refname_atom_parser
},
324 { "objectsize", FIELD_ULONG
},
325 { "objectname", FIELD_STR
, objectname_atom_parser
},
328 { "numparent", FIELD_ULONG
},
335 { "authordate", FIELD_TIME
},
338 { "committeremail" },
339 { "committerdate", FIELD_TIME
},
343 { "taggerdate", FIELD_TIME
},
345 { "creatordate", FIELD_TIME
},
346 { "subject", FIELD_STR
, subject_atom_parser
},
347 { "body", FIELD_STR
, body_atom_parser
},
348 { "trailers", FIELD_STR
, trailers_atom_parser
},
349 { "contents", FIELD_STR
, contents_atom_parser
},
350 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
351 { "push", FIELD_STR
, remote_ref_atom_parser
},
352 { "symref", FIELD_STR
, refname_atom_parser
},
354 { "HEAD", FIELD_STR
, head_atom_parser
},
355 { "color", FIELD_STR
, color_atom_parser
},
356 { "align", FIELD_STR
, align_atom_parser
},
358 { "if", FIELD_STR
, if_atom_parser
},
363 #define REF_FORMATTING_STATE_INIT { 0, NULL }
365 struct ref_formatting_stack
{
366 struct ref_formatting_stack
*prev
;
367 struct strbuf output
;
368 void (*at_end
)(struct ref_formatting_stack
**stack
);
372 struct ref_formatting_state
{
374 struct ref_formatting_stack
*stack
;
379 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
380 uintmax_t value
; /* used for sorting when not FIELD_STR */
381 struct used_atom
*atom
;
385 * Used to parse format string and sort specifiers
387 static int parse_ref_filter_atom(const struct ref_format
*format
,
388 const char *atom
, const char *ep
)
395 if (*sp
== '*' && sp
< ep
)
398 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
400 /* Do we have the atom already used elsewhere? */
401 for (i
= 0; i
< used_atom_cnt
; i
++) {
402 int len
= strlen(used_atom
[i
].name
);
403 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
408 * If the atom name has a colon, strip it and everything after
409 * it off - it specifies the format for this entry, and
410 * shouldn't be used for checking against the valid_atom
413 arg
= memchr(sp
, ':', ep
- sp
);
414 atom_len
= (arg
? arg
: ep
) - sp
;
416 /* Is the atom a valid one? */
417 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
418 int len
= strlen(valid_atom
[i
].name
);
419 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
423 if (ARRAY_SIZE(valid_atom
) <= i
)
424 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
426 /* Add it in, including the deref prefix */
429 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
430 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
431 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
433 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
436 * Treat empty sub-arguments list as NULL (i.e.,
437 * "%(atom:)" is equivalent to "%(atom)").
442 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
443 if (valid_atom
[i
].parser
)
444 valid_atom
[i
].parser(format
, &used_atom
[at
], arg
);
447 if (!strcmp(valid_atom
[i
].name
, "symref"))
452 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
454 switch (quote_style
) {
456 strbuf_addstr(s
, str
);
459 sq_quote_buf(s
, str
);
462 perl_quote_buf(s
, str
);
465 python_quote_buf(s
, str
);
468 tcl_quote_buf(s
, str
);
473 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
476 * Quote formatting is only done when the stack has a single
477 * element. Otherwise quote formatting is done on the
478 * element's entire output strbuf when the %(end) atom is
481 if (!state
->stack
->prev
)
482 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
484 strbuf_addstr(&state
->stack
->output
, v
->s
);
487 static void push_stack_element(struct ref_formatting_stack
**stack
)
489 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
491 strbuf_init(&s
->output
, 0);
496 static void pop_stack_element(struct ref_formatting_stack
**stack
)
498 struct ref_formatting_stack
*current
= *stack
;
499 struct ref_formatting_stack
*prev
= current
->prev
;
502 strbuf_addbuf(&prev
->output
, ¤t
->output
);
503 strbuf_release(¤t
->output
);
508 static void end_align_handler(struct ref_formatting_stack
**stack
)
510 struct ref_formatting_stack
*cur
= *stack
;
511 struct align
*align
= (struct align
*)cur
->at_end_data
;
512 struct strbuf s
= STRBUF_INIT
;
514 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
515 strbuf_swap(&cur
->output
, &s
);
519 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
521 struct ref_formatting_stack
*new;
523 push_stack_element(&state
->stack
);
525 new->at_end
= end_align_handler
;
526 new->at_end_data
= &atomv
->atom
->u
.align
;
529 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
531 struct ref_formatting_stack
*cur
= *stack
;
532 struct ref_formatting_stack
*prev
= cur
->prev
;
533 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
535 if (!if_then_else
->then_atom_seen
)
536 die(_("format: %%(if) atom used without a %%(then) atom"));
538 if (if_then_else
->else_atom_seen
) {
540 * There is an %(else) atom: we need to drop one state from the
541 * stack, either the %(else) branch if the condition is satisfied, or
542 * the %(then) branch if it isn't.
544 if (if_then_else
->condition_satisfied
) {
545 strbuf_reset(&cur
->output
);
546 pop_stack_element(&cur
);
548 strbuf_swap(&cur
->output
, &prev
->output
);
549 strbuf_reset(&cur
->output
);
550 pop_stack_element(&cur
);
552 } else if (!if_then_else
->condition_satisfied
) {
554 * No %(else) atom: just drop the %(then) branch if the
555 * condition is not satisfied.
557 strbuf_reset(&cur
->output
);
564 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
566 struct ref_formatting_stack
*new;
567 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
569 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
570 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
572 push_stack_element(&state
->stack
);
574 new->at_end
= if_then_else_handler
;
575 new->at_end_data
= if_then_else
;
578 static int is_empty(const char *s
)
588 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
590 struct ref_formatting_stack
*cur
= state
->stack
;
591 struct if_then_else
*if_then_else
= NULL
;
593 if (cur
->at_end
== if_then_else_handler
)
594 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
596 die(_("format: %%(then) atom used without an %%(if) atom"));
597 if (if_then_else
->then_atom_seen
)
598 die(_("format: %%(then) atom used more than once"));
599 if (if_then_else
->else_atom_seen
)
600 die(_("format: %%(then) atom used after %%(else)"));
601 if_then_else
->then_atom_seen
= 1;
603 * If the 'equals' or 'notequals' attribute is used then
604 * perform the required comparison. If not, only non-empty
605 * strings satisfy the 'if' condition.
607 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
608 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
609 if_then_else
->condition_satisfied
= 1;
610 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
611 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
612 if_then_else
->condition_satisfied
= 1;
613 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
614 if_then_else
->condition_satisfied
= 1;
615 strbuf_reset(&cur
->output
);
618 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
620 struct ref_formatting_stack
*prev
= state
->stack
;
621 struct if_then_else
*if_then_else
= NULL
;
623 if (prev
->at_end
== if_then_else_handler
)
624 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
626 die(_("format: %%(else) atom used without an %%(if) atom"));
627 if (!if_then_else
->then_atom_seen
)
628 die(_("format: %%(else) atom used without a %%(then) atom"));
629 if (if_then_else
->else_atom_seen
)
630 die(_("format: %%(else) atom used more than once"));
631 if_then_else
->else_atom_seen
= 1;
632 push_stack_element(&state
->stack
);
633 state
->stack
->at_end_data
= prev
->at_end_data
;
634 state
->stack
->at_end
= prev
->at_end
;
637 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
639 struct ref_formatting_stack
*current
= state
->stack
;
640 struct strbuf s
= STRBUF_INIT
;
642 if (!current
->at_end
)
643 die(_("format: %%(end) atom used without corresponding atom"));
644 current
->at_end(&state
->stack
);
646 /* Stack may have been popped within at_end(), hence reset the current pointer */
647 current
= state
->stack
;
650 * Perform quote formatting when the stack element is that of
651 * a supporting atom. If nested then perform quote formatting
652 * only on the topmost supporting atom.
654 if (!current
->prev
->prev
) {
655 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
656 strbuf_swap(¤t
->output
, &s
);
659 pop_stack_element(&state
->stack
);
663 * In a format string, find the next occurrence of %(atom).
665 static const char *find_next(const char *cp
)
670 * %( is the start of an atom;
671 * %% is a quoted per-cent.
675 else if (cp
[1] == '%')
676 cp
++; /* skip over two % */
677 /* otherwise this is a singleton, literal % */
685 * Make sure the format string is well formed, and parse out
688 int verify_ref_format(struct ref_format
*format
)
692 format
->need_color_reset_at_eol
= 0;
693 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
694 const char *color
, *ep
= strchr(sp
, ')');
698 return error(_("malformed format string %s"), sp
);
699 /* sp points at "%(" and ep points at the closing ")" */
700 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
);
703 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
704 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
706 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
707 format
->need_color_reset_at_eol
= 0;
712 * Given an object name, read the object data and size, and return a
713 * "struct object". If the object data we are returning is also borrowed
714 * by the "struct object" representation, set *eaten as well---it is a
715 * signal from parse_object_buffer to us not to free the buffer.
717 static void *get_obj(const struct object_id
*oid
, struct object
**obj
, unsigned long *sz
, int *eaten
)
719 enum object_type type
;
720 void *buf
= read_sha1_file(oid
->hash
, &type
, sz
);
723 *obj
= parse_object_buffer(oid
, type
, *sz
, buf
, eaten
);
729 static int grab_objectname(const char *name
, const unsigned char *sha1
,
730 struct atom_value
*v
, struct used_atom
*atom
)
732 if (starts_with(name
, "objectname")) {
733 if (atom
->u
.objectname
.option
== O_SHORT
) {
734 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
736 } else if (atom
->u
.objectname
.option
== O_FULL
) {
737 v
->s
= xstrdup(sha1_to_hex(sha1
));
739 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
740 v
->s
= xstrdup(find_unique_abbrev(sha1
, atom
->u
.objectname
.length
));
743 die("BUG: unknown %%(objectname) option");
748 /* See grab_values */
749 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
753 for (i
= 0; i
< used_atom_cnt
; i
++) {
754 const char *name
= used_atom
[i
].name
;
755 struct atom_value
*v
= &val
[i
];
756 if (!!deref
!= (*name
== '*'))
760 if (!strcmp(name
, "objecttype"))
761 v
->s
= typename(obj
->type
);
762 else if (!strcmp(name
, "objectsize")) {
764 v
->s
= xstrfmt("%lu", sz
);
767 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
771 /* See grab_values */
772 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
775 struct tag
*tag
= (struct tag
*) obj
;
777 for (i
= 0; i
< used_atom_cnt
; i
++) {
778 const char *name
= used_atom
[i
].name
;
779 struct atom_value
*v
= &val
[i
];
780 if (!!deref
!= (*name
== '*'))
784 if (!strcmp(name
, "tag"))
786 else if (!strcmp(name
, "type") && tag
->tagged
)
787 v
->s
= typename(tag
->tagged
->type
);
788 else if (!strcmp(name
, "object") && tag
->tagged
)
789 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
793 /* See grab_values */
794 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
797 struct commit
*commit
= (struct commit
*) obj
;
799 for (i
= 0; i
< used_atom_cnt
; i
++) {
800 const char *name
= used_atom
[i
].name
;
801 struct atom_value
*v
= &val
[i
];
802 if (!!deref
!= (*name
== '*'))
806 if (!strcmp(name
, "tree")) {
807 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
809 else if (!strcmp(name
, "numparent")) {
810 v
->value
= commit_list_count(commit
->parents
);
811 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
813 else if (!strcmp(name
, "parent")) {
814 struct commit_list
*parents
;
815 struct strbuf s
= STRBUF_INIT
;
816 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
817 struct commit
*parent
= parents
->item
;
818 if (parents
!= commit
->parents
)
819 strbuf_addch(&s
, ' ');
820 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
822 v
->s
= strbuf_detach(&s
, NULL
);
827 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
831 if (!strncmp(buf
, who
, wholen
) &&
833 return buf
+ wholen
+ 1;
834 eol
= strchr(buf
, '\n');
839 return ""; /* end of header */
845 static const char *copy_line(const char *buf
)
847 const char *eol
= strchrnul(buf
, '\n');
848 return xmemdupz(buf
, eol
- buf
);
851 static const char *copy_name(const char *buf
)
854 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
855 if (!strncmp(cp
, " <", 2))
856 return xmemdupz(buf
, cp
- buf
);
861 static const char *copy_email(const char *buf
)
863 const char *email
= strchr(buf
, '<');
867 eoemail
= strchr(email
, '>');
870 return xmemdupz(email
, eoemail
+ 1 - email
);
873 static char *copy_subject(const char *buf
, unsigned long len
)
875 char *r
= xmemdupz(buf
, len
);
878 for (i
= 0; i
< len
; i
++)
885 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
887 const char *eoemail
= strstr(buf
, "> ");
889 timestamp_t timestamp
;
891 struct date_mode date_mode
= { DATE_NORMAL
};
895 * We got here because atomname ends in "date" or "date<something>";
896 * it's not possible that <something> is not ":<format>" because
897 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
898 * ":" means no format is specified, and use the default.
900 formatp
= strchr(atomname
, ':');
901 if (formatp
!= NULL
) {
903 parse_date_format(formatp
, &date_mode
);
908 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
909 if (timestamp
== TIME_MAX
)
911 tz
= strtol(zone
, NULL
, 10);
912 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
914 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
915 v
->value
= timestamp
;
922 /* See grab_values */
923 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
926 int wholen
= strlen(who
);
927 const char *wholine
= NULL
;
929 for (i
= 0; i
< used_atom_cnt
; i
++) {
930 const char *name
= used_atom
[i
].name
;
931 struct atom_value
*v
= &val
[i
];
932 if (!!deref
!= (*name
== '*'))
936 if (strncmp(who
, name
, wholen
))
938 if (name
[wholen
] != 0 &&
939 strcmp(name
+ wholen
, "name") &&
940 strcmp(name
+ wholen
, "email") &&
941 !starts_with(name
+ wholen
, "date"))
944 wholine
= find_wholine(who
, wholen
, buf
, sz
);
946 return; /* no point looking for it */
947 if (name
[wholen
] == 0)
948 v
->s
= copy_line(wholine
);
949 else if (!strcmp(name
+ wholen
, "name"))
950 v
->s
= copy_name(wholine
);
951 else if (!strcmp(name
+ wholen
, "email"))
952 v
->s
= copy_email(wholine
);
953 else if (starts_with(name
+ wholen
, "date"))
954 grab_date(wholine
, v
, name
);
958 * For a tag or a commit object, if "creator" or "creatordate" is
959 * requested, do something special.
961 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
962 return; /* "author" for commit object is not wanted */
964 wholine
= find_wholine(who
, wholen
, buf
, sz
);
967 for (i
= 0; i
< used_atom_cnt
; i
++) {
968 const char *name
= used_atom
[i
].name
;
969 struct atom_value
*v
= &val
[i
];
970 if (!!deref
!= (*name
== '*'))
975 if (starts_with(name
, "creatordate"))
976 grab_date(wholine
, v
, name
);
977 else if (!strcmp(name
, "creator"))
978 v
->s
= copy_line(wholine
);
982 static void find_subpos(const char *buf
, unsigned long sz
,
983 const char **sub
, unsigned long *sublen
,
984 const char **body
, unsigned long *bodylen
,
985 unsigned long *nonsiglen
,
986 const char **sig
, unsigned long *siglen
)
989 /* skip past header until we hit empty line */
990 while (*buf
&& *buf
!= '\n') {
991 eol
= strchrnul(buf
, '\n');
996 /* skip any empty lines */
1000 /* parse signature first; we might not even have a subject line */
1001 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
1002 *siglen
= strlen(*sig
);
1004 /* subject is first non-empty line */
1006 /* subject goes to first empty line */
1007 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
1008 eol
= strchrnul(buf
, '\n');
1013 *sublen
= buf
- *sub
;
1014 /* drop trailing newline, if present */
1015 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1018 /* skip any empty lines */
1019 while (*buf
== '\n')
1022 *bodylen
= strlen(buf
);
1023 *nonsiglen
= *sig
- buf
;
1027 * If 'lines' is greater than 0, append that many lines from the given
1028 * 'buf' of length 'size' to the given strbuf.
1030 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1033 const char *sp
, *eol
;
1038 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1040 strbuf_addstr(out
, "\n ");
1041 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1042 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1043 strbuf_add(out
, sp
, len
);
1050 /* See grab_values */
1051 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1054 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1055 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1057 for (i
= 0; i
< used_atom_cnt
; i
++) {
1058 struct used_atom
*atom
= &used_atom
[i
];
1059 const char *name
= atom
->name
;
1060 struct atom_value
*v
= &val
[i
];
1061 if (!!deref
!= (*name
== '*'))
1065 if (strcmp(name
, "subject") &&
1066 strcmp(name
, "body") &&
1067 !starts_with(name
, "trailers") &&
1068 !starts_with(name
, "contents"))
1071 find_subpos(buf
, sz
,
1073 &bodypos
, &bodylen
, &nonsiglen
,
1076 if (atom
->u
.contents
.option
== C_SUB
)
1077 v
->s
= copy_subject(subpos
, sublen
);
1078 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1079 v
->s
= xmemdupz(bodypos
, bodylen
);
1080 else if (atom
->u
.contents
.option
== C_BODY
)
1081 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1082 else if (atom
->u
.contents
.option
== C_SIG
)
1083 v
->s
= xmemdupz(sigpos
, siglen
);
1084 else if (atom
->u
.contents
.option
== C_LINES
) {
1085 struct strbuf s
= STRBUF_INIT
;
1086 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1088 /* Size is the length of the message after removing the signature */
1089 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1090 v
->s
= strbuf_detach(&s
, NULL
);
1091 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1092 struct strbuf s
= STRBUF_INIT
;
1094 /* Format the trailer info according to the trailer_opts given */
1095 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1097 v
->s
= strbuf_detach(&s
, NULL
);
1098 } else if (atom
->u
.contents
.option
== C_BARE
)
1099 v
->s
= xstrdup(subpos
);
1104 * We want to have empty print-string for field requests
1105 * that do not apply (e.g. "authordate" for a tag object)
1107 static void fill_missing_values(struct atom_value
*val
)
1110 for (i
= 0; i
< used_atom_cnt
; i
++) {
1111 struct atom_value
*v
= &val
[i
];
1118 * val is a list of atom_value to hold returned values. Extract
1119 * the values for atoms in used_atom array out of (obj, buf, sz).
1120 * when deref is false, (obj, buf, sz) is the object that is
1121 * pointed at by the ref itself; otherwise it is the object the
1122 * ref (which is a tag) refers to.
1124 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1126 grab_common_values(val
, deref
, obj
, buf
, sz
);
1127 switch (obj
->type
) {
1129 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1130 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1131 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1134 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1135 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1136 grab_person("author", val
, deref
, obj
, buf
, sz
);
1137 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1140 /* grab_tree_values(val, deref, obj, buf, sz); */
1143 /* grab_blob_values(val, deref, obj, buf, sz); */
1146 die("Eh? Object of type %d?", obj
->type
);
1150 static inline char *copy_advance(char *dst
, const char *src
)
1157 static const char *lstrip_ref_components(const char *refname
, int len
)
1159 long remaining
= len
;
1160 const char *start
= refname
;
1164 const char *p
= refname
;
1166 /* Find total no of '/' separated path-components */
1167 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1170 * The number of components we need to strip is now
1171 * the total minus the components to be left (Plus one
1172 * because we count the number of '/', but the number
1173 * of components is one more than the no of '/').
1175 remaining
= i
+ len
+ 1;
1178 while (remaining
> 0) {
1191 static const char *rstrip_ref_components(const char *refname
, int len
)
1193 long remaining
= len
;
1194 char *start
= xstrdup(refname
);
1198 const char *p
= refname
;
1200 /* Find total no of '/' separated path-components */
1201 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1204 * The number of components we need to strip is now
1205 * the total minus the components to be left (Plus one
1206 * because we count the number of '/', but the number
1207 * of components is one more than the no of '/').
1209 remaining
= i
+ len
+ 1;
1212 while (remaining
-- > 0) {
1213 char *p
= strrchr(start
, '/');
1222 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1224 if (atom
->option
== R_SHORT
)
1225 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1226 else if (atom
->option
== R_LSTRIP
)
1227 return lstrip_ref_components(refname
, atom
->lstrip
);
1228 else if (atom
->option
== R_RSTRIP
)
1229 return rstrip_ref_components(refname
, atom
->rstrip
);
1234 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1235 struct branch
*branch
, const char **s
)
1237 int num_ours
, num_theirs
;
1238 if (atom
->u
.remote_ref
.option
== RR_REF
)
1239 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1240 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1241 if (stat_tracking_info(branch
, &num_ours
,
1242 &num_theirs
, NULL
)) {
1243 *s
= xstrdup(msgs
.gone
);
1244 } else if (!num_ours
&& !num_theirs
)
1247 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1248 else if (!num_theirs
)
1249 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1251 *s
= xstrfmt(msgs
.ahead_behind
,
1252 num_ours
, num_theirs
);
1253 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1254 const char *to_free
= *s
;
1255 *s
= xstrfmt("[%s]", *s
);
1256 free((void *)to_free
);
1258 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1259 if (stat_tracking_info(branch
, &num_ours
,
1263 if (!num_ours
&& !num_theirs
)
1267 else if (!num_theirs
)
1272 die("BUG: unhandled RR_* enum");
1275 char *get_head_description(void)
1277 struct strbuf desc
= STRBUF_INIT
;
1278 struct wt_status_state state
;
1279 memset(&state
, 0, sizeof(state
));
1280 wt_status_get_state(&state
, 1);
1281 if (state
.rebase_in_progress
||
1282 state
.rebase_interactive_in_progress
)
1283 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1285 else if (state
.bisect_in_progress
)
1286 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1288 else if (state
.detached_from
) {
1289 if (state
.detached_at
)
1291 * TRANSLATORS: make sure this matches "HEAD
1292 * detached at " in wt-status.c
1294 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1295 state
.detached_from
);
1298 * TRANSLATORS: make sure this matches "HEAD
1299 * detached from " in wt-status.c
1301 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1302 state
.detached_from
);
1305 strbuf_addstr(&desc
, _("(no branch)"));
1308 free(state
.detached_from
);
1309 return strbuf_detach(&desc
, NULL
);
1312 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1317 return show_ref(&atom
->u
.refname
, ref
->symref
);
1320 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1322 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1323 return get_head_description();
1324 return show_ref(&atom
->u
.refname
, ref
->refname
);
1328 * Parse the object referred by ref, and grab needed value.
1330 static void populate_value(struct ref_array_item
*ref
)
1336 const struct object_id
*tagged
;
1338 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1340 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1341 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1347 /* Fill in specials first */
1348 for (i
= 0; i
< used_atom_cnt
; i
++) {
1349 struct used_atom
*atom
= &used_atom
[i
];
1350 const char *name
= used_atom
[i
].name
;
1351 struct atom_value
*v
= &ref
->value
[i
];
1353 const char *refname
;
1354 struct branch
*branch
= NULL
;
1356 v
->handler
= append_atom
;
1364 if (starts_with(name
, "refname"))
1365 refname
= get_refname(atom
, ref
);
1366 else if (starts_with(name
, "symref"))
1367 refname
= get_symref(atom
, ref
);
1368 else if (starts_with(name
, "upstream")) {
1369 const char *branch_name
;
1370 /* only local branches may have an upstream */
1371 if (!skip_prefix(ref
->refname
, "refs/heads/",
1374 branch
= branch_get(branch_name
);
1376 refname
= branch_get_upstream(branch
, NULL
);
1378 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1380 } else if (starts_with(name
, "push")) {
1381 const char *branch_name
;
1382 if (!skip_prefix(ref
->refname
, "refs/heads/",
1385 branch
= branch_get(branch_name
);
1387 refname
= branch_get_push(branch
, NULL
);
1390 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1392 } else if (starts_with(name
, "color:")) {
1393 v
->s
= atom
->u
.color
;
1395 } else if (!strcmp(name
, "flag")) {
1396 char buf
[256], *cp
= buf
;
1397 if (ref
->flag
& REF_ISSYMREF
)
1398 cp
= copy_advance(cp
, ",symref");
1399 if (ref
->flag
& REF_ISPACKED
)
1400 cp
= copy_advance(cp
, ",packed");
1405 v
->s
= xstrdup(buf
+ 1);
1408 } else if (!deref
&& grab_objectname(name
, ref
->objectname
.hash
, v
, atom
)) {
1410 } else if (!strcmp(name
, "HEAD")) {
1411 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1416 } else if (starts_with(name
, "align")) {
1417 v
->handler
= align_atom_handler
;
1419 } else if (!strcmp(name
, "end")) {
1420 v
->handler
= end_atom_handler
;
1422 } else if (starts_with(name
, "if")) {
1425 if (skip_prefix(name
, "if:", &s
))
1427 v
->handler
= if_atom_handler
;
1429 } else if (!strcmp(name
, "then")) {
1430 v
->handler
= then_atom_handler
;
1432 } else if (!strcmp(name
, "else")) {
1433 v
->handler
= else_atom_handler
;
1441 v
->s
= xstrfmt("%s^{}", refname
);
1444 for (i
= 0; i
< used_atom_cnt
; i
++) {
1445 struct atom_value
*v
= &ref
->value
[i
];
1452 buf
= get_obj(&ref
->objectname
, &obj
, &size
, &eaten
);
1454 die(_("missing object %s for %s"),
1455 oid_to_hex(&ref
->objectname
), ref
->refname
);
1457 die(_("parse_object_buffer failed on %s for %s"),
1458 oid_to_hex(&ref
->objectname
), ref
->refname
);
1460 grab_values(ref
->value
, 0, obj
, buf
, size
);
1465 * If there is no atom that wants to know about tagged
1466 * object, we are done.
1468 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1472 * If it is a tag object, see if we use a value that derefs
1473 * the object, and if we do grab the object it refers to.
1475 tagged
= &((struct tag
*)obj
)->tagged
->oid
;
1478 * NEEDSWORK: This derefs tag only once, which
1479 * is good to deal with chains of trust, but
1480 * is not consistent with what deref_tag() does
1481 * which peels the onion to the core.
1483 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1485 die(_("missing object %s for %s"),
1486 oid_to_hex(tagged
), ref
->refname
);
1488 die(_("parse_object_buffer failed on %s for %s"),
1489 oid_to_hex(tagged
), ref
->refname
);
1490 grab_values(ref
->value
, 1, obj
, buf
, size
);
1496 * Given a ref, return the value for the atom. This lazily gets value
1497 * out of the object by calling populate value.
1499 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1502 populate_value(ref
);
1503 fill_missing_values(ref
->value
);
1505 *v
= &ref
->value
[atom
];
1509 * Unknown has to be "0" here, because that's the default value for
1510 * contains_cache slab entries that have not yet been assigned.
1512 enum contains_result
{
1513 CONTAINS_UNKNOWN
= 0,
1518 define_commit_slab(contains_cache
, enum contains_result
);
1520 struct ref_filter_cbdata
{
1521 struct ref_array
*array
;
1522 struct ref_filter
*filter
;
1523 struct contains_cache contains_cache
;
1524 struct contains_cache no_contains_cache
;
1528 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1531 * At each recursion step, the stack items points to the commits whose
1532 * ancestors are to be inspected.
1534 struct contains_stack
{
1536 struct contains_stack_entry
{
1537 struct commit
*commit
;
1538 struct commit_list
*parents
;
1542 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1544 for (; want
; want
= want
->next
)
1545 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1551 * Test whether the candidate or one of its parents is contained in the list.
1552 * Do not recurse to find out, though, but return -1 if inconclusive.
1554 static enum contains_result
contains_test(struct commit
*candidate
,
1555 const struct commit_list
*want
,
1556 struct contains_cache
*cache
)
1558 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
1560 /* If we already have the answer cached, return that. */
1565 if (in_commit_list(want
, candidate
)) {
1566 *cached
= CONTAINS_YES
;
1567 return CONTAINS_YES
;
1570 /* Otherwise, we don't know; prepare to recurse */
1571 parse_commit_or_die(candidate
);
1572 return CONTAINS_UNKNOWN
;
1575 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1577 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1578 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1579 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1582 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1583 const struct commit_list
*want
,
1584 struct contains_cache
*cache
)
1586 struct contains_stack contains_stack
= { 0, 0, NULL
};
1587 enum contains_result result
= contains_test(candidate
, want
, cache
);
1589 if (result
!= CONTAINS_UNKNOWN
)
1592 push_to_contains_stack(candidate
, &contains_stack
);
1593 while (contains_stack
.nr
) {
1594 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1595 struct commit
*commit
= entry
->commit
;
1596 struct commit_list
*parents
= entry
->parents
;
1599 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
1600 contains_stack
.nr
--;
1603 * If we just popped the stack, parents->item has been marked,
1604 * therefore contains_test will return a meaningful yes/no.
1606 else switch (contains_test(parents
->item
, want
, cache
)) {
1608 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
1609 contains_stack
.nr
--;
1612 entry
->parents
= parents
->next
;
1614 case CONTAINS_UNKNOWN
:
1615 push_to_contains_stack(parents
->item
, &contains_stack
);
1619 free(contains_stack
.contains_stack
);
1620 return contains_test(candidate
, want
, cache
);
1623 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
1624 struct commit_list
*list
, struct contains_cache
*cache
)
1626 if (filter
->with_commit_tag_algo
)
1627 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
1628 return is_descendant_of(commit
, list
);
1632 * Return 1 if the refname matches one of the patterns, otherwise 0.
1633 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1634 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1635 * matches "refs/heads/mas*", too).
1637 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1639 const char **patterns
= filter
->name_patterns
;
1642 if (filter
->ignore_case
)
1643 flags
|= WM_CASEFOLD
;
1646 * When no '--format' option is given we need to skip the prefix
1647 * for matching refs of tags and branches.
1649 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1650 skip_prefix(refname
, "refs/heads/", &refname
) ||
1651 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1652 skip_prefix(refname
, "refs/", &refname
));
1654 for (; *patterns
; patterns
++) {
1655 if (!wildmatch(*patterns
, refname
, flags
))
1662 * Return 1 if the refname matches one of the patterns, otherwise 0.
1663 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1664 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1665 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1667 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1669 const char **pattern
= filter
->name_patterns
;
1670 int namelen
= strlen(refname
);
1671 unsigned flags
= WM_PATHNAME
;
1673 if (filter
->ignore_case
)
1674 flags
|= WM_CASEFOLD
;
1676 for (; *pattern
; pattern
++) {
1677 const char *p
= *pattern
;
1678 int plen
= strlen(p
);
1680 if ((plen
<= namelen
) &&
1681 !strncmp(refname
, p
, plen
) &&
1682 (refname
[plen
] == '\0' ||
1683 refname
[plen
] == '/' ||
1686 if (!wildmatch(p
, refname
, WM_PATHNAME
))
1692 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1693 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1695 if (!*filter
->name_patterns
)
1696 return 1; /* No pattern always matches */
1697 if (filter
->match_as_path
)
1698 return match_name_as_path(filter
, refname
);
1699 return match_pattern(filter
, refname
);
1703 * Find the longest prefix of pattern we can pass to
1704 * `for_each_fullref_in()`, namely the part of pattern preceding the
1705 * first glob character. (Note that `for_each_fullref_in()` is
1706 * perfectly happy working with a prefix that doesn't end at a
1707 * pathname component boundary.)
1709 static void find_longest_prefix(struct strbuf
*out
, const char *pattern
)
1713 for (p
= pattern
; *p
&& !is_glob_special(*p
); p
++)
1716 strbuf_add(out
, pattern
, p
- pattern
);
1720 * This is the same as for_each_fullref_in(), but it tries to iterate
1721 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1722 * pattern match, so the callback still has to match each ref individually.
1724 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1729 struct strbuf prefix
= STRBUF_INIT
;
1732 if (!filter
->match_as_path
) {
1734 * in this case, the patterns are applied after
1735 * prefixes like "refs/heads/" etc. are stripped off,
1736 * so we have to look at everything:
1738 return for_each_fullref_in("", cb
, cb_data
, broken
);
1741 if (!filter
->name_patterns
[0]) {
1742 /* no patterns; we have to look at everything */
1743 return for_each_fullref_in("", cb
, cb_data
, broken
);
1746 if (filter
->name_patterns
[1]) {
1748 * multiple patterns; in theory this could still work as long
1749 * as the patterns are disjoint. We'd just make multiple calls
1750 * to for_each_ref(). But if they're not disjoint, we'd end up
1751 * reporting the same ref multiple times. So let's punt on that
1754 return for_each_fullref_in("", cb
, cb_data
, broken
);
1757 find_longest_prefix(&prefix
, filter
->name_patterns
[0]);
1759 ret
= for_each_fullref_in(prefix
.buf
, cb
, cb_data
, broken
);
1760 strbuf_release(&prefix
);
1765 * Given a ref (sha1, refname), check if the ref belongs to the array
1766 * of sha1s. If the given ref is a tag, check if the given tag points
1767 * at one of the sha1s in the given sha1 array.
1768 * the given sha1_array.
1770 * 1. Only a single level of inderection is obtained, we might want to
1771 * change this to account for multiple levels (e.g. annotated tags
1772 * pointing to annotated tags pointing to a commit.)
1773 * 2. As the refs are cached we might know what refname peels to without
1774 * the need to parse the object via parse_object(). peel_ref() might be a
1775 * more efficient alternative to obtain the pointee.
1777 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1778 const struct object_id
*oid
,
1779 const char *refname
)
1781 const struct object_id
*tagged_oid
= NULL
;
1784 if (oid_array_lookup(points_at
, oid
) >= 0)
1786 obj
= parse_object(oid
);
1788 die(_("malformed object at '%s'"), refname
);
1789 if (obj
->type
== OBJ_TAG
)
1790 tagged_oid
= &((struct tag
*)obj
)->tagged
->oid
;
1791 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
1796 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1797 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1798 const unsigned char *objectname
,
1801 struct ref_array_item
*ref
;
1802 FLEX_ALLOC_STR(ref
, refname
, refname
);
1803 hashcpy(ref
->objectname
.hash
, objectname
);
1809 static int ref_kind_from_refname(const char *refname
)
1817 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1818 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1819 { "refs/tags/", FILTER_REFS_TAGS
}
1822 if (!strcmp(refname
, "HEAD"))
1823 return FILTER_REFS_DETACHED_HEAD
;
1825 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1826 if (starts_with(refname
, ref_kind
[i
].prefix
))
1827 return ref_kind
[i
].kind
;
1830 return FILTER_REFS_OTHERS
;
1833 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1835 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1836 filter
->kind
== FILTER_REFS_REMOTES
||
1837 filter
->kind
== FILTER_REFS_TAGS
)
1838 return filter
->kind
;
1839 return ref_kind_from_refname(refname
);
1843 * A call-back given to for_each_ref(). Filter refs and keep them for
1844 * later object processing.
1846 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1848 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1849 struct ref_filter
*filter
= ref_cbdata
->filter
;
1850 struct ref_array_item
*ref
;
1851 struct commit
*commit
= NULL
;
1854 if (flag
& REF_BAD_NAME
) {
1855 warning(_("ignoring ref with broken name %s"), refname
);
1859 if (flag
& REF_ISBROKEN
) {
1860 warning(_("ignoring broken ref %s"), refname
);
1864 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1865 kind
= filter_ref_kind(filter
, refname
);
1866 if (!(kind
& filter
->kind
))
1869 if (!filter_pattern_match(filter
, refname
))
1872 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
1876 * A merge filter is applied on refs pointing to commits. Hence
1877 * obtain the commit using the 'oid' available and discard all
1878 * non-commits early. The actual filtering is done later.
1880 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
1881 commit
= lookup_commit_reference_gently(oid
, 1);
1884 /* We perform the filtering for the '--contains' option... */
1885 if (filter
->with_commit
&&
1886 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
1888 /* ...or for the `--no-contains' option */
1889 if (filter
->no_commit
&&
1890 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
1895 * We do not open the object yet; sort may only need refname
1896 * to do its job and the resulting list may yet to be pruned
1897 * by maxcount logic.
1899 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1900 ref
->commit
= commit
;
1902 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1903 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1908 /* Free memory allocated for a ref_array_item */
1909 static void free_array_item(struct ref_array_item
*item
)
1911 free((char *)item
->symref
);
1915 /* Free all memory allocated for ref_array */
1916 void ref_array_clear(struct ref_array
*array
)
1920 for (i
= 0; i
< array
->nr
; i
++)
1921 free_array_item(array
->items
[i
]);
1922 FREE_AND_NULL(array
->items
);
1923 array
->nr
= array
->alloc
= 0;
1926 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1928 struct rev_info revs
;
1930 struct ref_filter
*filter
= ref_cbdata
->filter
;
1931 struct ref_array
*array
= ref_cbdata
->array
;
1932 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1934 init_revisions(&revs
, NULL
);
1936 for (i
= 0; i
< array
->nr
; i
++) {
1937 struct ref_array_item
*item
= array
->items
[i
];
1938 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1939 to_clear
[i
] = item
->commit
;
1942 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1943 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1946 if (prepare_revision_walk(&revs
))
1947 die(_("revision walk setup failed"));
1952 for (i
= 0; i
< old_nr
; i
++) {
1953 struct ref_array_item
*item
= array
->items
[i
];
1954 struct commit
*commit
= item
->commit
;
1956 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1958 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1959 array
->items
[array
->nr
++] = array
->items
[i
];
1961 free_array_item(item
);
1964 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
1965 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1970 * API for filtering a set of refs. Based on the type of refs the user
1971 * has requested, we iterate through those refs and apply filters
1972 * as per the given ref_filter structure and finally store the
1973 * filtered refs in the ref_array structure.
1975 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1977 struct ref_filter_cbdata ref_cbdata
;
1979 unsigned int broken
= 0;
1981 ref_cbdata
.array
= array
;
1982 ref_cbdata
.filter
= filter
;
1984 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1986 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1988 init_contains_cache(&ref_cbdata
.contains_cache
);
1989 init_contains_cache(&ref_cbdata
.no_contains_cache
);
1991 /* Simple per-ref filtering */
1993 die("filter_refs: invalid type");
1996 * For common cases where we need only branches or remotes or tags,
1997 * we only iterate through those refs. If a mix of refs is needed,
1998 * we iterate over all refs and filter out required refs with the help
1999 * of filter_ref_kind().
2001 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2002 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
2003 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2004 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2005 else if (filter
->kind
== FILTER_REFS_TAGS
)
2006 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2007 else if (filter
->kind
& FILTER_REFS_ALL
)
2008 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2009 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2010 head_ref(ref_filter_handler
, &ref_cbdata
);
2013 clear_contains_cache(&ref_cbdata
.contains_cache
);
2014 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2016 /* Filters that need revision walking */
2017 if (filter
->merge_commit
)
2018 do_merge_filter(&ref_cbdata
);
2023 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2025 struct atom_value
*va
, *vb
;
2027 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2028 int (*cmp_fn
)(const char *, const char *);
2030 get_ref_atom_value(a
, s
->atom
, &va
);
2031 get_ref_atom_value(b
, s
->atom
, &vb
);
2032 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2034 cmp
= versioncmp(va
->s
, vb
->s
);
2035 else if (cmp_type
== FIELD_STR
)
2036 cmp
= cmp_fn(va
->s
, vb
->s
);
2038 if (va
->value
< vb
->value
)
2040 else if (va
->value
== vb
->value
)
2041 cmp
= cmp_fn(a
->refname
, b
->refname
);
2046 return (s
->reverse
) ? -cmp
: cmp
;
2049 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2051 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2052 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2053 struct ref_sorting
*s
;
2055 for (s
= ref_sorting
; s
; s
= s
->next
) {
2056 int cmp
= cmp_ref_sorting(s
, a
, b
);
2063 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2065 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2068 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2070 struct strbuf
*s
= &state
->stack
->output
;
2072 while (*cp
&& (!ep
|| cp
< ep
)) {
2077 int ch
= hex2chr(cp
+ 1);
2079 strbuf_addch(s
, ch
);
2085 strbuf_addch(s
, *cp
);
2090 void format_ref_array_item(struct ref_array_item
*info
,
2091 const struct ref_format
*format
,
2092 struct strbuf
*final_buf
)
2094 const char *cp
, *sp
, *ep
;
2095 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2097 state
.quote_style
= format
->quote_style
;
2098 push_stack_element(&state
.stack
);
2100 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2101 struct atom_value
*atomv
;
2103 ep
= strchr(sp
, ')');
2105 append_literal(cp
, sp
, &state
);
2106 get_ref_atom_value(info
,
2107 parse_ref_filter_atom(format
, sp
+ 2, ep
),
2109 atomv
->handler(atomv
, &state
);
2112 sp
= cp
+ strlen(cp
);
2113 append_literal(cp
, sp
, &state
);
2115 if (format
->need_color_reset_at_eol
) {
2116 struct atom_value resetv
;
2117 resetv
.s
= GIT_COLOR_RESET
;
2118 append_atom(&resetv
, &state
);
2120 if (state
.stack
->prev
)
2121 die(_("format: %%(end) atom missing"));
2122 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2123 pop_stack_element(&state
.stack
);
2126 void show_ref_array_item(struct ref_array_item
*info
,
2127 const struct ref_format
*format
)
2129 struct strbuf final_buf
= STRBUF_INIT
;
2131 format_ref_array_item(info
, format
, &final_buf
);
2132 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2133 strbuf_release(&final_buf
);
2137 void pretty_print_ref(const char *name
, const unsigned char *sha1
,
2138 const struct ref_format
*format
)
2140 struct ref_array_item
*ref_item
;
2141 ref_item
= new_ref_array_item(name
, sha1
, 0);
2142 ref_item
->kind
= ref_kind_from_refname(name
);
2143 show_ref_array_item(ref_item
, format
);
2144 free_array_item(ref_item
);
2147 static int parse_sorting_atom(const char *atom
)
2150 * This parses an atom using a dummy ref_format, since we don't
2151 * actually care about the formatting details.
2153 struct ref_format dummy
= REF_FORMAT_INIT
;
2154 const char *end
= atom
+ strlen(atom
);
2155 return parse_ref_filter_atom(&dummy
, atom
, end
);
2158 /* If no sorting option is given, use refname to sort as default */
2159 struct ref_sorting
*ref_default_sorting(void)
2161 static const char cstr_name
[] = "refname";
2163 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2165 sorting
->next
= NULL
;
2166 sorting
->atom
= parse_sorting_atom(cstr_name
);
2170 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2172 struct ref_sorting
*s
;
2174 s
= xcalloc(1, sizeof(*s
));
2175 s
->next
= *sorting_tail
;
2182 if (skip_prefix(arg
, "version:", &arg
) ||
2183 skip_prefix(arg
, "v:", &arg
))
2185 s
->atom
= parse_sorting_atom(arg
);
2188 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2190 if (!arg
) /* should --no-sort void the list ? */
2192 parse_ref_sorting(opt
->value
, arg
);
2196 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2198 struct ref_filter
*rf
= opt
->value
;
2199 struct object_id oid
;
2200 int no_merged
= starts_with(opt
->long_name
, "no");
2204 return opterror(opt
, "is incompatible with --merged", 0);
2206 return opterror(opt
, "is incompatible with --no-merged", 0);
2210 rf
->merge
= no_merged
2211 ? REF_FILTER_MERGED_OMIT
2212 : REF_FILTER_MERGED_INCLUDE
;
2214 if (get_oid(arg
, &oid
))
2215 die(_("malformed object name %s"), arg
);
2217 rf
->merge_commit
= lookup_commit_reference_gently(&oid
, 0);
2218 if (!rf
->merge_commit
)
2219 return opterror(opt
, "must point to a commit", 0);