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
];
80 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
82 struct refname_atom refname
;
83 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
86 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
87 struct process_trailer_options trailer_opts
;
91 cmp_status cmp_status
;
95 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
98 struct refname_atom refname
;
102 static int used_atom_cnt
, need_tagged
, need_symref
;
104 static void color_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *color_value
)
107 die(_("expected format: %%(color:<color>)"));
108 if (color_parse(color_value
, atom
->u
.color
) < 0)
109 die(_("unrecognized color: %%(color:%s)"), color_value
);
111 * We check this after we've parsed the color, which lets us complain
112 * about syntactically bogus color names even if they won't be used.
114 if (!want_color(format
->use_color
))
115 color_parse("", atom
->u
.color
);
118 static void refname_atom_parser_internal(struct refname_atom
*atom
,
119 const char *arg
, const char *name
)
122 atom
->option
= R_NORMAL
;
123 else if (!strcmp(arg
, "short"))
124 atom
->option
= R_SHORT
;
125 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
126 skip_prefix(arg
, "strip=", &arg
)) {
127 atom
->option
= R_LSTRIP
;
128 if (strtol_i(arg
, 10, &atom
->lstrip
))
129 die(_("Integer value expected refname:lstrip=%s"), arg
);
130 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
131 atom
->option
= R_RSTRIP
;
132 if (strtol_i(arg
, 10, &atom
->rstrip
))
133 die(_("Integer value expected refname:rstrip=%s"), arg
);
135 die(_("unrecognized %%(%s) argument: %s"), name
, arg
);
138 static void remote_ref_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
140 struct string_list params
= STRING_LIST_INIT_DUP
;
143 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
144 atom
->u
.remote_ref
.push
= 1;
147 atom
->u
.remote_ref
.option
= RR_REF
;
148 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
153 atom
->u
.remote_ref
.nobracket
= 0;
154 string_list_split(¶ms
, arg
, ',', -1);
156 for (i
= 0; i
< params
.nr
; i
++) {
157 const char *s
= params
.items
[i
].string
;
159 if (!strcmp(s
, "track"))
160 atom
->u
.remote_ref
.option
= RR_TRACK
;
161 else if (!strcmp(s
, "trackshort"))
162 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
163 else if (!strcmp(s
, "nobracket"))
164 atom
->u
.remote_ref
.nobracket
= 1;
165 else if (!strcmp(s
, "remotename")) {
166 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
167 atom
->u
.remote_ref
.push_remote
= 1;
168 } else if (!strcmp(s
, "remoteref")) {
169 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
170 atom
->u
.remote_ref
.push_remote
= 1;
172 atom
->u
.remote_ref
.option
= RR_REF
;
173 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
178 string_list_clear(¶ms
, 0);
181 static void body_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
184 die(_("%%(body) does not take arguments"));
185 atom
->u
.contents
.option
= C_BODY_DEP
;
188 static void subject_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
191 die(_("%%(subject) does not take arguments"));
192 atom
->u
.contents
.option
= C_SUB
;
195 static void trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
197 struct string_list params
= STRING_LIST_INIT_DUP
;
201 string_list_split(¶ms
, arg
, ',', -1);
202 for (i
= 0; i
< params
.nr
; i
++) {
203 const char *s
= params
.items
[i
].string
;
204 if (!strcmp(s
, "unfold"))
205 atom
->u
.contents
.trailer_opts
.unfold
= 1;
206 else if (!strcmp(s
, "only"))
207 atom
->u
.contents
.trailer_opts
.only_trailers
= 1;
209 die(_("unknown %%(trailers) argument: %s"), s
);
212 atom
->u
.contents
.option
= C_TRAILERS
;
213 string_list_clear(¶ms
, 0);
216 static void contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
219 atom
->u
.contents
.option
= C_BARE
;
220 else if (!strcmp(arg
, "body"))
221 atom
->u
.contents
.option
= C_BODY
;
222 else if (!strcmp(arg
, "signature"))
223 atom
->u
.contents
.option
= C_SIG
;
224 else if (!strcmp(arg
, "subject"))
225 atom
->u
.contents
.option
= C_SUB
;
226 else if (skip_prefix(arg
, "trailers", &arg
)) {
227 skip_prefix(arg
, ":", &arg
);
228 trailers_atom_parser(format
, atom
, *arg
? arg
: NULL
);
229 } else if (skip_prefix(arg
, "lines=", &arg
)) {
230 atom
->u
.contents
.option
= C_LINES
;
231 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
232 die(_("positive value expected contents:lines=%s"), arg
);
234 die(_("unrecognized %%(contents) argument: %s"), arg
);
237 static void objectname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
240 atom
->u
.objectname
.option
= O_FULL
;
241 else if (!strcmp(arg
, "short"))
242 atom
->u
.objectname
.option
= O_SHORT
;
243 else if (skip_prefix(arg
, "short=", &arg
)) {
244 atom
->u
.objectname
.option
= O_LENGTH
;
245 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
246 atom
->u
.objectname
.length
== 0)
247 die(_("positive value expected objectname:short=%s"), arg
);
248 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
249 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
251 die(_("unrecognized %%(objectname) argument: %s"), arg
);
254 static void refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
256 refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
);
259 static align_type
parse_align_position(const char *s
)
261 if (!strcmp(s
, "right"))
263 else if (!strcmp(s
, "middle"))
265 else if (!strcmp(s
, "left"))
270 static void align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
272 struct align
*align
= &atom
->u
.align
;
273 struct string_list params
= STRING_LIST_INIT_DUP
;
275 unsigned int width
= ~0U;
278 die(_("expected format: %%(align:<width>,<position>)"));
280 align
->position
= ALIGN_LEFT
;
282 string_list_split(¶ms
, arg
, ',', -1);
283 for (i
= 0; i
< params
.nr
; i
++) {
284 const char *s
= params
.items
[i
].string
;
287 if (skip_prefix(s
, "position=", &s
)) {
288 position
= parse_align_position(s
);
290 die(_("unrecognized position:%s"), s
);
291 align
->position
= position
;
292 } else if (skip_prefix(s
, "width=", &s
)) {
293 if (strtoul_ui(s
, 10, &width
))
294 die(_("unrecognized width:%s"), s
);
295 } else if (!strtoul_ui(s
, 10, &width
))
297 else if ((position
= parse_align_position(s
)) >= 0)
298 align
->position
= position
;
300 die(_("unrecognized %%(align) argument: %s"), s
);
304 die(_("positive width expected with the %%(align) atom"));
305 align
->width
= width
;
306 string_list_clear(¶ms
, 0);
309 static void if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
312 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
314 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
315 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
316 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
317 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
319 die(_("unrecognized %%(if) argument: %s"), arg
);
323 static void head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
325 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
331 void (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
);
333 { "refname" , FIELD_STR
, refname_atom_parser
},
335 { "objectsize", FIELD_ULONG
},
336 { "objectname", FIELD_STR
, objectname_atom_parser
},
339 { "numparent", FIELD_ULONG
},
346 { "authordate", FIELD_TIME
},
349 { "committeremail" },
350 { "committerdate", FIELD_TIME
},
354 { "taggerdate", FIELD_TIME
},
356 { "creatordate", FIELD_TIME
},
357 { "subject", FIELD_STR
, subject_atom_parser
},
358 { "body", FIELD_STR
, body_atom_parser
},
359 { "trailers", FIELD_STR
, trailers_atom_parser
},
360 { "contents", FIELD_STR
, contents_atom_parser
},
361 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
362 { "push", FIELD_STR
, remote_ref_atom_parser
},
363 { "symref", FIELD_STR
, refname_atom_parser
},
365 { "HEAD", FIELD_STR
, head_atom_parser
},
366 { "color", FIELD_STR
, color_atom_parser
},
367 { "align", FIELD_STR
, align_atom_parser
},
369 { "if", FIELD_STR
, if_atom_parser
},
374 #define REF_FORMATTING_STATE_INIT { 0, NULL }
376 struct ref_formatting_stack
{
377 struct ref_formatting_stack
*prev
;
378 struct strbuf output
;
379 void (*at_end
)(struct ref_formatting_stack
**stack
);
383 struct ref_formatting_state
{
385 struct ref_formatting_stack
*stack
;
390 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
391 uintmax_t value
; /* used for sorting when not FIELD_STR */
392 struct used_atom
*atom
;
396 * Used to parse format string and sort specifiers
398 static int parse_ref_filter_atom(const struct ref_format
*format
,
399 const char *atom
, const char *ep
)
406 if (*sp
== '*' && sp
< ep
)
409 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
411 /* Do we have the atom already used elsewhere? */
412 for (i
= 0; i
< used_atom_cnt
; i
++) {
413 int len
= strlen(used_atom
[i
].name
);
414 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
419 * If the atom name has a colon, strip it and everything after
420 * it off - it specifies the format for this entry, and
421 * shouldn't be used for checking against the valid_atom
424 arg
= memchr(sp
, ':', ep
- sp
);
425 atom_len
= (arg
? arg
: ep
) - sp
;
427 /* Is the atom a valid one? */
428 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
429 int len
= strlen(valid_atom
[i
].name
);
430 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
434 if (ARRAY_SIZE(valid_atom
) <= i
)
435 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
437 /* Add it in, including the deref prefix */
440 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
441 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
442 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
444 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
447 * Treat empty sub-arguments list as NULL (i.e.,
448 * "%(atom:)" is equivalent to "%(atom)").
453 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
454 if (valid_atom
[i
].parser
)
455 valid_atom
[i
].parser(format
, &used_atom
[at
], arg
);
458 if (!strcmp(valid_atom
[i
].name
, "symref"))
463 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
465 switch (quote_style
) {
467 strbuf_addstr(s
, str
);
470 sq_quote_buf(s
, str
);
473 perl_quote_buf(s
, str
);
476 python_quote_buf(s
, str
);
479 tcl_quote_buf(s
, str
);
484 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
487 * Quote formatting is only done when the stack has a single
488 * element. Otherwise quote formatting is done on the
489 * element's entire output strbuf when the %(end) atom is
492 if (!state
->stack
->prev
)
493 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
495 strbuf_addstr(&state
->stack
->output
, v
->s
);
498 static void push_stack_element(struct ref_formatting_stack
**stack
)
500 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
502 strbuf_init(&s
->output
, 0);
507 static void pop_stack_element(struct ref_formatting_stack
**stack
)
509 struct ref_formatting_stack
*current
= *stack
;
510 struct ref_formatting_stack
*prev
= current
->prev
;
513 strbuf_addbuf(&prev
->output
, ¤t
->output
);
514 strbuf_release(¤t
->output
);
519 static void end_align_handler(struct ref_formatting_stack
**stack
)
521 struct ref_formatting_stack
*cur
= *stack
;
522 struct align
*align
= (struct align
*)cur
->at_end_data
;
523 struct strbuf s
= STRBUF_INIT
;
525 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
526 strbuf_swap(&cur
->output
, &s
);
530 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
532 struct ref_formatting_stack
*new;
534 push_stack_element(&state
->stack
);
536 new->at_end
= end_align_handler
;
537 new->at_end_data
= &atomv
->atom
->u
.align
;
540 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
542 struct ref_formatting_stack
*cur
= *stack
;
543 struct ref_formatting_stack
*prev
= cur
->prev
;
544 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
546 if (!if_then_else
->then_atom_seen
)
547 die(_("format: %%(if) atom used without a %%(then) atom"));
549 if (if_then_else
->else_atom_seen
) {
551 * There is an %(else) atom: we need to drop one state from the
552 * stack, either the %(else) branch if the condition is satisfied, or
553 * the %(then) branch if it isn't.
555 if (if_then_else
->condition_satisfied
) {
556 strbuf_reset(&cur
->output
);
557 pop_stack_element(&cur
);
559 strbuf_swap(&cur
->output
, &prev
->output
);
560 strbuf_reset(&cur
->output
);
561 pop_stack_element(&cur
);
563 } else if (!if_then_else
->condition_satisfied
) {
565 * No %(else) atom: just drop the %(then) branch if the
566 * condition is not satisfied.
568 strbuf_reset(&cur
->output
);
575 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
577 struct ref_formatting_stack
*new;
578 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
580 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
581 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
583 push_stack_element(&state
->stack
);
585 new->at_end
= if_then_else_handler
;
586 new->at_end_data
= if_then_else
;
589 static int is_empty(const char *s
)
599 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
601 struct ref_formatting_stack
*cur
= state
->stack
;
602 struct if_then_else
*if_then_else
= NULL
;
604 if (cur
->at_end
== if_then_else_handler
)
605 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
607 die(_("format: %%(then) atom used without an %%(if) atom"));
608 if (if_then_else
->then_atom_seen
)
609 die(_("format: %%(then) atom used more than once"));
610 if (if_then_else
->else_atom_seen
)
611 die(_("format: %%(then) atom used after %%(else)"));
612 if_then_else
->then_atom_seen
= 1;
614 * If the 'equals' or 'notequals' attribute is used then
615 * perform the required comparison. If not, only non-empty
616 * strings satisfy the 'if' condition.
618 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
619 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
620 if_then_else
->condition_satisfied
= 1;
621 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
622 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
623 if_then_else
->condition_satisfied
= 1;
624 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
625 if_then_else
->condition_satisfied
= 1;
626 strbuf_reset(&cur
->output
);
629 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
631 struct ref_formatting_stack
*prev
= state
->stack
;
632 struct if_then_else
*if_then_else
= NULL
;
634 if (prev
->at_end
== if_then_else_handler
)
635 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
637 die(_("format: %%(else) atom used without an %%(if) atom"));
638 if (!if_then_else
->then_atom_seen
)
639 die(_("format: %%(else) atom used without a %%(then) atom"));
640 if (if_then_else
->else_atom_seen
)
641 die(_("format: %%(else) atom used more than once"));
642 if_then_else
->else_atom_seen
= 1;
643 push_stack_element(&state
->stack
);
644 state
->stack
->at_end_data
= prev
->at_end_data
;
645 state
->stack
->at_end
= prev
->at_end
;
648 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
650 struct ref_formatting_stack
*current
= state
->stack
;
651 struct strbuf s
= STRBUF_INIT
;
653 if (!current
->at_end
)
654 die(_("format: %%(end) atom used without corresponding atom"));
655 current
->at_end(&state
->stack
);
657 /* Stack may have been popped within at_end(), hence reset the current pointer */
658 current
= state
->stack
;
661 * Perform quote formatting when the stack element is that of
662 * a supporting atom. If nested then perform quote formatting
663 * only on the topmost supporting atom.
665 if (!current
->prev
->prev
) {
666 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
667 strbuf_swap(¤t
->output
, &s
);
670 pop_stack_element(&state
->stack
);
674 * In a format string, find the next occurrence of %(atom).
676 static const char *find_next(const char *cp
)
681 * %( is the start of an atom;
682 * %% is a quoted per-cent.
686 else if (cp
[1] == '%')
687 cp
++; /* skip over two % */
688 /* otherwise this is a singleton, literal % */
696 * Make sure the format string is well formed, and parse out
699 int verify_ref_format(struct ref_format
*format
)
703 format
->need_color_reset_at_eol
= 0;
704 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
705 const char *color
, *ep
= strchr(sp
, ')');
709 return error(_("malformed format string %s"), sp
);
710 /* sp points at "%(" and ep points at the closing ")" */
711 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
);
714 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
715 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
717 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
718 format
->need_color_reset_at_eol
= 0;
723 * Given an object name, read the object data and size, and return a
724 * "struct object". If the object data we are returning is also borrowed
725 * by the "struct object" representation, set *eaten as well---it is a
726 * signal from parse_object_buffer to us not to free the buffer.
728 static void *get_obj(const struct object_id
*oid
, struct object
**obj
, unsigned long *sz
, int *eaten
)
730 enum object_type type
;
731 void *buf
= read_sha1_file(oid
->hash
, &type
, sz
);
734 *obj
= parse_object_buffer(oid
, type
, *sz
, buf
, eaten
);
740 static int grab_objectname(const char *name
, const unsigned char *sha1
,
741 struct atom_value
*v
, struct used_atom
*atom
)
743 if (starts_with(name
, "objectname")) {
744 if (atom
->u
.objectname
.option
== O_SHORT
) {
745 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
747 } else if (atom
->u
.objectname
.option
== O_FULL
) {
748 v
->s
= xstrdup(sha1_to_hex(sha1
));
750 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
751 v
->s
= xstrdup(find_unique_abbrev(sha1
, atom
->u
.objectname
.length
));
754 die("BUG: unknown %%(objectname) option");
759 /* See grab_values */
760 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
764 for (i
= 0; i
< used_atom_cnt
; i
++) {
765 const char *name
= used_atom
[i
].name
;
766 struct atom_value
*v
= &val
[i
];
767 if (!!deref
!= (*name
== '*'))
771 if (!strcmp(name
, "objecttype"))
772 v
->s
= type_name(obj
->type
);
773 else if (!strcmp(name
, "objectsize")) {
775 v
->s
= xstrfmt("%lu", sz
);
778 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
782 /* See grab_values */
783 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
786 struct tag
*tag
= (struct tag
*) obj
;
788 for (i
= 0; i
< used_atom_cnt
; i
++) {
789 const char *name
= used_atom
[i
].name
;
790 struct atom_value
*v
= &val
[i
];
791 if (!!deref
!= (*name
== '*'))
795 if (!strcmp(name
, "tag"))
797 else if (!strcmp(name
, "type") && tag
->tagged
)
798 v
->s
= type_name(tag
->tagged
->type
);
799 else if (!strcmp(name
, "object") && tag
->tagged
)
800 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
804 /* See grab_values */
805 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
808 struct commit
*commit
= (struct commit
*) obj
;
810 for (i
= 0; i
< used_atom_cnt
; i
++) {
811 const char *name
= used_atom
[i
].name
;
812 struct atom_value
*v
= &val
[i
];
813 if (!!deref
!= (*name
== '*'))
817 if (!strcmp(name
, "tree")) {
818 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
820 else if (!strcmp(name
, "numparent")) {
821 v
->value
= commit_list_count(commit
->parents
);
822 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
824 else if (!strcmp(name
, "parent")) {
825 struct commit_list
*parents
;
826 struct strbuf s
= STRBUF_INIT
;
827 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
828 struct commit
*parent
= parents
->item
;
829 if (parents
!= commit
->parents
)
830 strbuf_addch(&s
, ' ');
831 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
833 v
->s
= strbuf_detach(&s
, NULL
);
838 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
842 if (!strncmp(buf
, who
, wholen
) &&
844 return buf
+ wholen
+ 1;
845 eol
= strchr(buf
, '\n');
850 return ""; /* end of header */
856 static const char *copy_line(const char *buf
)
858 const char *eol
= strchrnul(buf
, '\n');
859 return xmemdupz(buf
, eol
- buf
);
862 static const char *copy_name(const char *buf
)
865 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
866 if (!strncmp(cp
, " <", 2))
867 return xmemdupz(buf
, cp
- buf
);
872 static const char *copy_email(const char *buf
)
874 const char *email
= strchr(buf
, '<');
878 eoemail
= strchr(email
, '>');
881 return xmemdupz(email
, eoemail
+ 1 - email
);
884 static char *copy_subject(const char *buf
, unsigned long len
)
886 char *r
= xmemdupz(buf
, len
);
889 for (i
= 0; i
< len
; i
++)
896 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
898 const char *eoemail
= strstr(buf
, "> ");
900 timestamp_t timestamp
;
902 struct date_mode date_mode
= { DATE_NORMAL
};
906 * We got here because atomname ends in "date" or "date<something>";
907 * it's not possible that <something> is not ":<format>" because
908 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
909 * ":" means no format is specified, and use the default.
911 formatp
= strchr(atomname
, ':');
912 if (formatp
!= NULL
) {
914 parse_date_format(formatp
, &date_mode
);
919 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
920 if (timestamp
== TIME_MAX
)
922 tz
= strtol(zone
, NULL
, 10);
923 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
925 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
926 v
->value
= timestamp
;
933 /* See grab_values */
934 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
937 int wholen
= strlen(who
);
938 const char *wholine
= NULL
;
940 for (i
= 0; i
< used_atom_cnt
; i
++) {
941 const char *name
= used_atom
[i
].name
;
942 struct atom_value
*v
= &val
[i
];
943 if (!!deref
!= (*name
== '*'))
947 if (strncmp(who
, name
, wholen
))
949 if (name
[wholen
] != 0 &&
950 strcmp(name
+ wholen
, "name") &&
951 strcmp(name
+ wholen
, "email") &&
952 !starts_with(name
+ wholen
, "date"))
955 wholine
= find_wholine(who
, wholen
, buf
, sz
);
957 return; /* no point looking for it */
958 if (name
[wholen
] == 0)
959 v
->s
= copy_line(wholine
);
960 else if (!strcmp(name
+ wholen
, "name"))
961 v
->s
= copy_name(wholine
);
962 else if (!strcmp(name
+ wholen
, "email"))
963 v
->s
= copy_email(wholine
);
964 else if (starts_with(name
+ wholen
, "date"))
965 grab_date(wholine
, v
, name
);
969 * For a tag or a commit object, if "creator" or "creatordate" is
970 * requested, do something special.
972 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
973 return; /* "author" for commit object is not wanted */
975 wholine
= find_wholine(who
, wholen
, buf
, sz
);
978 for (i
= 0; i
< used_atom_cnt
; i
++) {
979 const char *name
= used_atom
[i
].name
;
980 struct atom_value
*v
= &val
[i
];
981 if (!!deref
!= (*name
== '*'))
986 if (starts_with(name
, "creatordate"))
987 grab_date(wholine
, v
, name
);
988 else if (!strcmp(name
, "creator"))
989 v
->s
= copy_line(wholine
);
993 static void find_subpos(const char *buf
, unsigned long sz
,
994 const char **sub
, unsigned long *sublen
,
995 const char **body
, unsigned long *bodylen
,
996 unsigned long *nonsiglen
,
997 const char **sig
, unsigned long *siglen
)
1000 /* skip past header until we hit empty line */
1001 while (*buf
&& *buf
!= '\n') {
1002 eol
= strchrnul(buf
, '\n');
1007 /* skip any empty lines */
1008 while (*buf
== '\n')
1011 /* parse signature first; we might not even have a subject line */
1012 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
1013 *siglen
= strlen(*sig
);
1015 /* subject is first non-empty line */
1017 /* subject goes to first empty line */
1018 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
1019 eol
= strchrnul(buf
, '\n');
1024 *sublen
= buf
- *sub
;
1025 /* drop trailing newline, if present */
1026 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1029 /* skip any empty lines */
1030 while (*buf
== '\n')
1033 *bodylen
= strlen(buf
);
1034 *nonsiglen
= *sig
- buf
;
1038 * If 'lines' is greater than 0, append that many lines from the given
1039 * 'buf' of length 'size' to the given strbuf.
1041 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1044 const char *sp
, *eol
;
1049 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1051 strbuf_addstr(out
, "\n ");
1052 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1053 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1054 strbuf_add(out
, sp
, len
);
1061 /* See grab_values */
1062 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1065 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1066 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1068 for (i
= 0; i
< used_atom_cnt
; i
++) {
1069 struct used_atom
*atom
= &used_atom
[i
];
1070 const char *name
= atom
->name
;
1071 struct atom_value
*v
= &val
[i
];
1072 if (!!deref
!= (*name
== '*'))
1076 if (strcmp(name
, "subject") &&
1077 strcmp(name
, "body") &&
1078 !starts_with(name
, "trailers") &&
1079 !starts_with(name
, "contents"))
1082 find_subpos(buf
, sz
,
1084 &bodypos
, &bodylen
, &nonsiglen
,
1087 if (atom
->u
.contents
.option
== C_SUB
)
1088 v
->s
= copy_subject(subpos
, sublen
);
1089 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1090 v
->s
= xmemdupz(bodypos
, bodylen
);
1091 else if (atom
->u
.contents
.option
== C_BODY
)
1092 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1093 else if (atom
->u
.contents
.option
== C_SIG
)
1094 v
->s
= xmemdupz(sigpos
, siglen
);
1095 else if (atom
->u
.contents
.option
== C_LINES
) {
1096 struct strbuf s
= STRBUF_INIT
;
1097 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1099 /* Size is the length of the message after removing the signature */
1100 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1101 v
->s
= strbuf_detach(&s
, NULL
);
1102 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1103 struct strbuf s
= STRBUF_INIT
;
1105 /* Format the trailer info according to the trailer_opts given */
1106 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1108 v
->s
= strbuf_detach(&s
, NULL
);
1109 } else if (atom
->u
.contents
.option
== C_BARE
)
1110 v
->s
= xstrdup(subpos
);
1115 * We want to have empty print-string for field requests
1116 * that do not apply (e.g. "authordate" for a tag object)
1118 static void fill_missing_values(struct atom_value
*val
)
1121 for (i
= 0; i
< used_atom_cnt
; i
++) {
1122 struct atom_value
*v
= &val
[i
];
1129 * val is a list of atom_value to hold returned values. Extract
1130 * the values for atoms in used_atom array out of (obj, buf, sz).
1131 * when deref is false, (obj, buf, sz) is the object that is
1132 * pointed at by the ref itself; otherwise it is the object the
1133 * ref (which is a tag) refers to.
1135 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1137 grab_common_values(val
, deref
, obj
, buf
, sz
);
1138 switch (obj
->type
) {
1140 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1141 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1142 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1145 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1146 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1147 grab_person("author", val
, deref
, obj
, buf
, sz
);
1148 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1151 /* grab_tree_values(val, deref, obj, buf, sz); */
1154 /* grab_blob_values(val, deref, obj, buf, sz); */
1157 die("Eh? Object of type %d?", obj
->type
);
1161 static inline char *copy_advance(char *dst
, const char *src
)
1168 static const char *lstrip_ref_components(const char *refname
, int len
)
1170 long remaining
= len
;
1171 const char *start
= refname
;
1175 const char *p
= refname
;
1177 /* Find total no of '/' separated path-components */
1178 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1181 * The number of components we need to strip is now
1182 * the total minus the components to be left (Plus one
1183 * because we count the number of '/', but the number
1184 * of components is one more than the no of '/').
1186 remaining
= i
+ len
+ 1;
1189 while (remaining
> 0) {
1202 static const char *rstrip_ref_components(const char *refname
, int len
)
1204 long remaining
= len
;
1205 char *start
= xstrdup(refname
);
1209 const char *p
= refname
;
1211 /* Find total no of '/' separated path-components */
1212 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1215 * The number of components we need to strip is now
1216 * the total minus the components to be left (Plus one
1217 * because we count the number of '/', but the number
1218 * of components is one more than the no of '/').
1220 remaining
= i
+ len
+ 1;
1223 while (remaining
-- > 0) {
1224 char *p
= strrchr(start
, '/');
1233 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1235 if (atom
->option
== R_SHORT
)
1236 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1237 else if (atom
->option
== R_LSTRIP
)
1238 return lstrip_ref_components(refname
, atom
->lstrip
);
1239 else if (atom
->option
== R_RSTRIP
)
1240 return rstrip_ref_components(refname
, atom
->rstrip
);
1245 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1246 struct branch
*branch
, const char **s
)
1248 int num_ours
, num_theirs
;
1249 if (atom
->u
.remote_ref
.option
== RR_REF
)
1250 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1251 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1252 if (stat_tracking_info(branch
, &num_ours
,
1253 &num_theirs
, NULL
)) {
1254 *s
= xstrdup(msgs
.gone
);
1255 } else if (!num_ours
&& !num_theirs
)
1258 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1259 else if (!num_theirs
)
1260 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1262 *s
= xstrfmt(msgs
.ahead_behind
,
1263 num_ours
, num_theirs
);
1264 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1265 const char *to_free
= *s
;
1266 *s
= xstrfmt("[%s]", *s
);
1267 free((void *)to_free
);
1269 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1270 if (stat_tracking_info(branch
, &num_ours
,
1274 if (!num_ours
&& !num_theirs
)
1278 else if (!num_theirs
)
1282 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1284 const char *remote
= atom
->u
.remote_ref
.push
?
1285 pushremote_for_branch(branch
, &explicit) :
1286 remote_for_branch(branch
, &explicit);
1288 *s
= xstrdup(remote
);
1291 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1295 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
,
1298 *s
= xstrdup(merge
);
1302 die("BUG: unhandled RR_* enum");
1305 char *get_head_description(void)
1307 struct strbuf desc
= STRBUF_INIT
;
1308 struct wt_status_state state
;
1309 memset(&state
, 0, sizeof(state
));
1310 wt_status_get_state(&state
, 1);
1311 if (state
.rebase_in_progress
||
1312 state
.rebase_interactive_in_progress
)
1313 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1315 else if (state
.bisect_in_progress
)
1316 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1318 else if (state
.detached_from
) {
1319 if (state
.detached_at
)
1321 * TRANSLATORS: make sure this matches "HEAD
1322 * detached at " in wt-status.c
1324 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1325 state
.detached_from
);
1328 * TRANSLATORS: make sure this matches "HEAD
1329 * detached from " in wt-status.c
1331 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1332 state
.detached_from
);
1335 strbuf_addstr(&desc
, _("(no branch)"));
1338 free(state
.detached_from
);
1339 return strbuf_detach(&desc
, NULL
);
1342 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1347 return show_ref(&atom
->u
.refname
, ref
->symref
);
1350 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1352 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1353 return get_head_description();
1354 return show_ref(&atom
->u
.refname
, ref
->refname
);
1358 * Parse the object referred by ref, and grab needed value.
1360 static void populate_value(struct ref_array_item
*ref
)
1366 const struct object_id
*tagged
;
1368 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1370 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1371 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1377 /* Fill in specials first */
1378 for (i
= 0; i
< used_atom_cnt
; i
++) {
1379 struct used_atom
*atom
= &used_atom
[i
];
1380 const char *name
= used_atom
[i
].name
;
1381 struct atom_value
*v
= &ref
->value
[i
];
1383 const char *refname
;
1384 struct branch
*branch
= NULL
;
1386 v
->handler
= append_atom
;
1394 if (starts_with(name
, "refname"))
1395 refname
= get_refname(atom
, ref
);
1396 else if (starts_with(name
, "symref"))
1397 refname
= get_symref(atom
, ref
);
1398 else if (starts_with(name
, "upstream")) {
1399 const char *branch_name
;
1400 /* only local branches may have an upstream */
1401 if (!skip_prefix(ref
->refname
, "refs/heads/",
1404 branch
= branch_get(branch_name
);
1406 refname
= branch_get_upstream(branch
, NULL
);
1408 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1410 } else if (atom
->u
.remote_ref
.push
) {
1411 const char *branch_name
;
1412 if (!skip_prefix(ref
->refname
, "refs/heads/",
1415 branch
= branch_get(branch_name
);
1417 if (atom
->u
.remote_ref
.push_remote
)
1420 refname
= branch_get_push(branch
, NULL
);
1424 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1426 } else if (starts_with(name
, "color:")) {
1427 v
->s
= atom
->u
.color
;
1429 } else if (!strcmp(name
, "flag")) {
1430 char buf
[256], *cp
= buf
;
1431 if (ref
->flag
& REF_ISSYMREF
)
1432 cp
= copy_advance(cp
, ",symref");
1433 if (ref
->flag
& REF_ISPACKED
)
1434 cp
= copy_advance(cp
, ",packed");
1439 v
->s
= xstrdup(buf
+ 1);
1442 } else if (!deref
&& grab_objectname(name
, ref
->objectname
.hash
, v
, atom
)) {
1444 } else if (!strcmp(name
, "HEAD")) {
1445 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1450 } else if (starts_with(name
, "align")) {
1451 v
->handler
= align_atom_handler
;
1453 } else if (!strcmp(name
, "end")) {
1454 v
->handler
= end_atom_handler
;
1456 } else if (starts_with(name
, "if")) {
1459 if (skip_prefix(name
, "if:", &s
))
1461 v
->handler
= if_atom_handler
;
1463 } else if (!strcmp(name
, "then")) {
1464 v
->handler
= then_atom_handler
;
1466 } else if (!strcmp(name
, "else")) {
1467 v
->handler
= else_atom_handler
;
1475 v
->s
= xstrfmt("%s^{}", refname
);
1478 for (i
= 0; i
< used_atom_cnt
; i
++) {
1479 struct atom_value
*v
= &ref
->value
[i
];
1486 buf
= get_obj(&ref
->objectname
, &obj
, &size
, &eaten
);
1488 die(_("missing object %s for %s"),
1489 oid_to_hex(&ref
->objectname
), ref
->refname
);
1491 die(_("parse_object_buffer failed on %s for %s"),
1492 oid_to_hex(&ref
->objectname
), ref
->refname
);
1494 grab_values(ref
->value
, 0, obj
, buf
, size
);
1499 * If there is no atom that wants to know about tagged
1500 * object, we are done.
1502 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1506 * If it is a tag object, see if we use a value that derefs
1507 * the object, and if we do grab the object it refers to.
1509 tagged
= &((struct tag
*)obj
)->tagged
->oid
;
1512 * NEEDSWORK: This derefs tag only once, which
1513 * is good to deal with chains of trust, but
1514 * is not consistent with what deref_tag() does
1515 * which peels the onion to the core.
1517 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1519 die(_("missing object %s for %s"),
1520 oid_to_hex(tagged
), ref
->refname
);
1522 die(_("parse_object_buffer failed on %s for %s"),
1523 oid_to_hex(tagged
), ref
->refname
);
1524 grab_values(ref
->value
, 1, obj
, buf
, size
);
1530 * Given a ref, return the value for the atom. This lazily gets value
1531 * out of the object by calling populate value.
1533 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1536 populate_value(ref
);
1537 fill_missing_values(ref
->value
);
1539 *v
= &ref
->value
[atom
];
1543 * Unknown has to be "0" here, because that's the default value for
1544 * contains_cache slab entries that have not yet been assigned.
1546 enum contains_result
{
1547 CONTAINS_UNKNOWN
= 0,
1552 define_commit_slab(contains_cache
, enum contains_result
);
1554 struct ref_filter_cbdata
{
1555 struct ref_array
*array
;
1556 struct ref_filter
*filter
;
1557 struct contains_cache contains_cache
;
1558 struct contains_cache no_contains_cache
;
1562 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1565 * At each recursion step, the stack items points to the commits whose
1566 * ancestors are to be inspected.
1568 struct contains_stack
{
1570 struct contains_stack_entry
{
1571 struct commit
*commit
;
1572 struct commit_list
*parents
;
1576 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1578 for (; want
; want
= want
->next
)
1579 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1585 * Test whether the candidate or one of its parents is contained in the list.
1586 * Do not recurse to find out, though, but return -1 if inconclusive.
1588 static enum contains_result
contains_test(struct commit
*candidate
,
1589 const struct commit_list
*want
,
1590 struct contains_cache
*cache
)
1592 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
1594 /* If we already have the answer cached, return that. */
1599 if (in_commit_list(want
, candidate
)) {
1600 *cached
= CONTAINS_YES
;
1601 return CONTAINS_YES
;
1604 /* Otherwise, we don't know; prepare to recurse */
1605 parse_commit_or_die(candidate
);
1606 return CONTAINS_UNKNOWN
;
1609 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1611 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1612 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1613 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1616 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1617 const struct commit_list
*want
,
1618 struct contains_cache
*cache
)
1620 struct contains_stack contains_stack
= { 0, 0, NULL
};
1621 enum contains_result result
= contains_test(candidate
, want
, cache
);
1623 if (result
!= CONTAINS_UNKNOWN
)
1626 push_to_contains_stack(candidate
, &contains_stack
);
1627 while (contains_stack
.nr
) {
1628 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1629 struct commit
*commit
= entry
->commit
;
1630 struct commit_list
*parents
= entry
->parents
;
1633 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
1634 contains_stack
.nr
--;
1637 * If we just popped the stack, parents->item has been marked,
1638 * therefore contains_test will return a meaningful yes/no.
1640 else switch (contains_test(parents
->item
, want
, cache
)) {
1642 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
1643 contains_stack
.nr
--;
1646 entry
->parents
= parents
->next
;
1648 case CONTAINS_UNKNOWN
:
1649 push_to_contains_stack(parents
->item
, &contains_stack
);
1653 free(contains_stack
.contains_stack
);
1654 return contains_test(candidate
, want
, cache
);
1657 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
1658 struct commit_list
*list
, struct contains_cache
*cache
)
1660 if (filter
->with_commit_tag_algo
)
1661 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
1662 return is_descendant_of(commit
, list
);
1666 * Return 1 if the refname matches one of the patterns, otherwise 0.
1667 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1668 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1669 * matches "refs/heads/mas*", too).
1671 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1673 const char **patterns
= filter
->name_patterns
;
1676 if (filter
->ignore_case
)
1677 flags
|= WM_CASEFOLD
;
1680 * When no '--format' option is given we need to skip the prefix
1681 * for matching refs of tags and branches.
1683 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1684 skip_prefix(refname
, "refs/heads/", &refname
) ||
1685 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1686 skip_prefix(refname
, "refs/", &refname
));
1688 for (; *patterns
; patterns
++) {
1689 if (!wildmatch(*patterns
, refname
, flags
))
1696 * Return 1 if the refname matches one of the patterns, otherwise 0.
1697 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1698 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1699 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1701 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1703 const char **pattern
= filter
->name_patterns
;
1704 int namelen
= strlen(refname
);
1705 unsigned flags
= WM_PATHNAME
;
1707 if (filter
->ignore_case
)
1708 flags
|= WM_CASEFOLD
;
1710 for (; *pattern
; pattern
++) {
1711 const char *p
= *pattern
;
1712 int plen
= strlen(p
);
1714 if ((plen
<= namelen
) &&
1715 !strncmp(refname
, p
, plen
) &&
1716 (refname
[plen
] == '\0' ||
1717 refname
[plen
] == '/' ||
1720 if (!wildmatch(p
, refname
, WM_PATHNAME
))
1726 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1727 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1729 if (!*filter
->name_patterns
)
1730 return 1; /* No pattern always matches */
1731 if (filter
->match_as_path
)
1732 return match_name_as_path(filter
, refname
);
1733 return match_pattern(filter
, refname
);
1737 * Find the longest prefix of pattern we can pass to
1738 * `for_each_fullref_in()`, namely the part of pattern preceding the
1739 * first glob character. (Note that `for_each_fullref_in()` is
1740 * perfectly happy working with a prefix that doesn't end at a
1741 * pathname component boundary.)
1743 static void find_longest_prefix(struct strbuf
*out
, const char *pattern
)
1747 for (p
= pattern
; *p
&& !is_glob_special(*p
); p
++)
1750 strbuf_add(out
, pattern
, p
- pattern
);
1754 * This is the same as for_each_fullref_in(), but it tries to iterate
1755 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1756 * pattern match, so the callback still has to match each ref individually.
1758 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1763 struct strbuf prefix
= STRBUF_INIT
;
1766 if (!filter
->match_as_path
) {
1768 * in this case, the patterns are applied after
1769 * prefixes like "refs/heads/" etc. are stripped off,
1770 * so we have to look at everything:
1772 return for_each_fullref_in("", cb
, cb_data
, broken
);
1775 if (!filter
->name_patterns
[0]) {
1776 /* no patterns; we have to look at everything */
1777 return for_each_fullref_in("", cb
, cb_data
, broken
);
1780 if (filter
->name_patterns
[1]) {
1782 * multiple patterns; in theory this could still work as long
1783 * as the patterns are disjoint. We'd just make multiple calls
1784 * to for_each_ref(). But if they're not disjoint, we'd end up
1785 * reporting the same ref multiple times. So let's punt on that
1788 return for_each_fullref_in("", cb
, cb_data
, broken
);
1791 find_longest_prefix(&prefix
, filter
->name_patterns
[0]);
1793 ret
= for_each_fullref_in(prefix
.buf
, cb
, cb_data
, broken
);
1794 strbuf_release(&prefix
);
1799 * Given a ref (sha1, refname), check if the ref belongs to the array
1800 * of sha1s. If the given ref is a tag, check if the given tag points
1801 * at one of the sha1s in the given sha1 array.
1802 * the given sha1_array.
1804 * 1. Only a single level of inderection is obtained, we might want to
1805 * change this to account for multiple levels (e.g. annotated tags
1806 * pointing to annotated tags pointing to a commit.)
1807 * 2. As the refs are cached we might know what refname peels to without
1808 * the need to parse the object via parse_object(). peel_ref() might be a
1809 * more efficient alternative to obtain the pointee.
1811 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1812 const struct object_id
*oid
,
1813 const char *refname
)
1815 const struct object_id
*tagged_oid
= NULL
;
1818 if (oid_array_lookup(points_at
, oid
) >= 0)
1820 obj
= parse_object(oid
);
1822 die(_("malformed object at '%s'"), refname
);
1823 if (obj
->type
== OBJ_TAG
)
1824 tagged_oid
= &((struct tag
*)obj
)->tagged
->oid
;
1825 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
1830 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1831 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1832 const unsigned char *objectname
,
1835 struct ref_array_item
*ref
;
1836 FLEX_ALLOC_STR(ref
, refname
, refname
);
1837 hashcpy(ref
->objectname
.hash
, objectname
);
1843 static int ref_kind_from_refname(const char *refname
)
1851 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1852 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1853 { "refs/tags/", FILTER_REFS_TAGS
}
1856 if (!strcmp(refname
, "HEAD"))
1857 return FILTER_REFS_DETACHED_HEAD
;
1859 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1860 if (starts_with(refname
, ref_kind
[i
].prefix
))
1861 return ref_kind
[i
].kind
;
1864 return FILTER_REFS_OTHERS
;
1867 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1869 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1870 filter
->kind
== FILTER_REFS_REMOTES
||
1871 filter
->kind
== FILTER_REFS_TAGS
)
1872 return filter
->kind
;
1873 return ref_kind_from_refname(refname
);
1877 * A call-back given to for_each_ref(). Filter refs and keep them for
1878 * later object processing.
1880 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1882 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1883 struct ref_filter
*filter
= ref_cbdata
->filter
;
1884 struct ref_array_item
*ref
;
1885 struct commit
*commit
= NULL
;
1888 if (flag
& REF_BAD_NAME
) {
1889 warning(_("ignoring ref with broken name %s"), refname
);
1893 if (flag
& REF_ISBROKEN
) {
1894 warning(_("ignoring broken ref %s"), refname
);
1898 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1899 kind
= filter_ref_kind(filter
, refname
);
1900 if (!(kind
& filter
->kind
))
1903 if (!filter_pattern_match(filter
, refname
))
1906 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
1910 * A merge filter is applied on refs pointing to commits. Hence
1911 * obtain the commit using the 'oid' available and discard all
1912 * non-commits early. The actual filtering is done later.
1914 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
1915 commit
= lookup_commit_reference_gently(oid
, 1);
1918 /* We perform the filtering for the '--contains' option... */
1919 if (filter
->with_commit
&&
1920 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
1922 /* ...or for the `--no-contains' option */
1923 if (filter
->no_commit
&&
1924 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
1929 * We do not open the object yet; sort may only need refname
1930 * to do its job and the resulting list may yet to be pruned
1931 * by maxcount logic.
1933 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1934 ref
->commit
= commit
;
1936 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1937 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1942 /* Free memory allocated for a ref_array_item */
1943 static void free_array_item(struct ref_array_item
*item
)
1945 free((char *)item
->symref
);
1949 /* Free all memory allocated for ref_array */
1950 void ref_array_clear(struct ref_array
*array
)
1954 for (i
= 0; i
< array
->nr
; i
++)
1955 free_array_item(array
->items
[i
]);
1956 FREE_AND_NULL(array
->items
);
1957 array
->nr
= array
->alloc
= 0;
1960 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1962 struct rev_info revs
;
1964 struct ref_filter
*filter
= ref_cbdata
->filter
;
1965 struct ref_array
*array
= ref_cbdata
->array
;
1966 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1968 init_revisions(&revs
, NULL
);
1970 for (i
= 0; i
< array
->nr
; i
++) {
1971 struct ref_array_item
*item
= array
->items
[i
];
1972 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1973 to_clear
[i
] = item
->commit
;
1976 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1977 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1980 if (prepare_revision_walk(&revs
))
1981 die(_("revision walk setup failed"));
1986 for (i
= 0; i
< old_nr
; i
++) {
1987 struct ref_array_item
*item
= array
->items
[i
];
1988 struct commit
*commit
= item
->commit
;
1990 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1992 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1993 array
->items
[array
->nr
++] = array
->items
[i
];
1995 free_array_item(item
);
1998 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
1999 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
2004 * API for filtering a set of refs. Based on the type of refs the user
2005 * has requested, we iterate through those refs and apply filters
2006 * as per the given ref_filter structure and finally store the
2007 * filtered refs in the ref_array structure.
2009 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2011 struct ref_filter_cbdata ref_cbdata
;
2013 unsigned int broken
= 0;
2015 ref_cbdata
.array
= array
;
2016 ref_cbdata
.filter
= filter
;
2018 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
2020 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2022 init_contains_cache(&ref_cbdata
.contains_cache
);
2023 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2025 /* Simple per-ref filtering */
2027 die("filter_refs: invalid type");
2030 * For common cases where we need only branches or remotes or tags,
2031 * we only iterate through those refs. If a mix of refs is needed,
2032 * we iterate over all refs and filter out required refs with the help
2033 * of filter_ref_kind().
2035 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2036 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
2037 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2038 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2039 else if (filter
->kind
== FILTER_REFS_TAGS
)
2040 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2041 else if (filter
->kind
& FILTER_REFS_ALL
)
2042 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2043 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2044 head_ref(ref_filter_handler
, &ref_cbdata
);
2047 clear_contains_cache(&ref_cbdata
.contains_cache
);
2048 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2050 /* Filters that need revision walking */
2051 if (filter
->merge_commit
)
2052 do_merge_filter(&ref_cbdata
);
2057 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2059 struct atom_value
*va
, *vb
;
2061 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2062 int (*cmp_fn
)(const char *, const char *);
2064 get_ref_atom_value(a
, s
->atom
, &va
);
2065 get_ref_atom_value(b
, s
->atom
, &vb
);
2066 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2068 cmp
= versioncmp(va
->s
, vb
->s
);
2069 else if (cmp_type
== FIELD_STR
)
2070 cmp
= cmp_fn(va
->s
, vb
->s
);
2072 if (va
->value
< vb
->value
)
2074 else if (va
->value
== vb
->value
)
2075 cmp
= cmp_fn(a
->refname
, b
->refname
);
2080 return (s
->reverse
) ? -cmp
: cmp
;
2083 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2085 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2086 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2087 struct ref_sorting
*s
;
2089 for (s
= ref_sorting
; s
; s
= s
->next
) {
2090 int cmp
= cmp_ref_sorting(s
, a
, b
);
2097 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2099 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2102 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2104 struct strbuf
*s
= &state
->stack
->output
;
2106 while (*cp
&& (!ep
|| cp
< ep
)) {
2111 int ch
= hex2chr(cp
+ 1);
2113 strbuf_addch(s
, ch
);
2119 strbuf_addch(s
, *cp
);
2124 void format_ref_array_item(struct ref_array_item
*info
,
2125 const struct ref_format
*format
,
2126 struct strbuf
*final_buf
)
2128 const char *cp
, *sp
, *ep
;
2129 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2131 state
.quote_style
= format
->quote_style
;
2132 push_stack_element(&state
.stack
);
2134 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2135 struct atom_value
*atomv
;
2137 ep
= strchr(sp
, ')');
2139 append_literal(cp
, sp
, &state
);
2140 get_ref_atom_value(info
,
2141 parse_ref_filter_atom(format
, sp
+ 2, ep
),
2143 atomv
->handler(atomv
, &state
);
2146 sp
= cp
+ strlen(cp
);
2147 append_literal(cp
, sp
, &state
);
2149 if (format
->need_color_reset_at_eol
) {
2150 struct atom_value resetv
;
2151 resetv
.s
= GIT_COLOR_RESET
;
2152 append_atom(&resetv
, &state
);
2154 if (state
.stack
->prev
)
2155 die(_("format: %%(end) atom missing"));
2156 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2157 pop_stack_element(&state
.stack
);
2160 void show_ref_array_item(struct ref_array_item
*info
,
2161 const struct ref_format
*format
)
2163 struct strbuf final_buf
= STRBUF_INIT
;
2165 format_ref_array_item(info
, format
, &final_buf
);
2166 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2167 strbuf_release(&final_buf
);
2171 void pretty_print_ref(const char *name
, const unsigned char *sha1
,
2172 const struct ref_format
*format
)
2174 struct ref_array_item
*ref_item
;
2175 ref_item
= new_ref_array_item(name
, sha1
, 0);
2176 ref_item
->kind
= ref_kind_from_refname(name
);
2177 show_ref_array_item(ref_item
, format
);
2178 free_array_item(ref_item
);
2181 static int parse_sorting_atom(const char *atom
)
2184 * This parses an atom using a dummy ref_format, since we don't
2185 * actually care about the formatting details.
2187 struct ref_format dummy
= REF_FORMAT_INIT
;
2188 const char *end
= atom
+ strlen(atom
);
2189 return parse_ref_filter_atom(&dummy
, atom
, end
);
2192 /* If no sorting option is given, use refname to sort as default */
2193 struct ref_sorting
*ref_default_sorting(void)
2195 static const char cstr_name
[] = "refname";
2197 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2199 sorting
->next
= NULL
;
2200 sorting
->atom
= parse_sorting_atom(cstr_name
);
2204 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2206 struct ref_sorting
*s
;
2208 s
= xcalloc(1, sizeof(*s
));
2209 s
->next
= *sorting_tail
;
2216 if (skip_prefix(arg
, "version:", &arg
) ||
2217 skip_prefix(arg
, "v:", &arg
))
2219 s
->atom
= parse_sorting_atom(arg
);
2222 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2224 if (!arg
) /* should --no-sort void the list ? */
2226 parse_ref_sorting(opt
->value
, arg
);
2230 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2232 struct ref_filter
*rf
= opt
->value
;
2233 struct object_id oid
;
2234 int no_merged
= starts_with(opt
->long_name
, "no");
2238 return opterror(opt
, "is incompatible with --merged", 0);
2240 return opterror(opt
, "is incompatible with --no-merged", 0);
2244 rf
->merge
= no_merged
2245 ? REF_FILTER_MERGED_OMIT
2246 : REF_FILTER_MERGED_INCLUDE
;
2248 if (get_oid(arg
, &oid
))
2249 die(_("malformed object name %s"), arg
);
2251 rf
->merge_commit
= lookup_commit_reference_gently(&oid
, 0);
2252 if (!rf
->merge_commit
)
2253 return opterror(opt
, "must point to a commit", 0);