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
;
88 cmp_status cmp_status
;
92 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
95 struct refname_atom refname
;
99 static int used_atom_cnt
, need_tagged
, need_symref
;
101 static void color_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *color_value
)
104 die(_("expected format: %%(color:<color>)"));
105 if (color_parse(color_value
, atom
->u
.color
) < 0)
106 die(_("unrecognized color: %%(color:%s)"), color_value
);
108 * We check this after we've parsed the color, which lets us complain
109 * about syntactically bogus color names even if they won't be used.
111 if (!want_color(format
->use_color
))
112 color_parse("", atom
->u
.color
);
115 static void refname_atom_parser_internal(struct refname_atom
*atom
,
116 const char *arg
, const char *name
)
119 atom
->option
= R_NORMAL
;
120 else if (!strcmp(arg
, "short"))
121 atom
->option
= R_SHORT
;
122 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
123 skip_prefix(arg
, "strip=", &arg
)) {
124 atom
->option
= R_LSTRIP
;
125 if (strtol_i(arg
, 10, &atom
->lstrip
))
126 die(_("Integer value expected refname:lstrip=%s"), arg
);
127 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
128 atom
->option
= R_RSTRIP
;
129 if (strtol_i(arg
, 10, &atom
->rstrip
))
130 die(_("Integer value expected refname:rstrip=%s"), arg
);
132 die(_("unrecognized %%(%s) argument: %s"), name
, arg
);
135 static void remote_ref_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
137 struct string_list params
= STRING_LIST_INIT_DUP
;
141 atom
->u
.remote_ref
.option
= RR_REF
;
142 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
147 atom
->u
.remote_ref
.nobracket
= 0;
148 string_list_split(¶ms
, arg
, ',', -1);
150 for (i
= 0; i
< params
.nr
; i
++) {
151 const char *s
= params
.items
[i
].string
;
153 if (!strcmp(s
, "track"))
154 atom
->u
.remote_ref
.option
= RR_TRACK
;
155 else if (!strcmp(s
, "trackshort"))
156 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
157 else if (!strcmp(s
, "nobracket"))
158 atom
->u
.remote_ref
.nobracket
= 1;
160 atom
->u
.remote_ref
.option
= RR_REF
;
161 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
166 string_list_clear(¶ms
, 0);
169 static void body_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
172 die(_("%%(body) does not take arguments"));
173 atom
->u
.contents
.option
= C_BODY_DEP
;
176 static void subject_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
179 die(_("%%(subject) does not take arguments"));
180 atom
->u
.contents
.option
= C_SUB
;
183 static void trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
186 die(_("%%(trailers) does not take arguments"));
187 atom
->u
.contents
.option
= C_TRAILERS
;
190 static void contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
193 atom
->u
.contents
.option
= C_BARE
;
194 else if (!strcmp(arg
, "body"))
195 atom
->u
.contents
.option
= C_BODY
;
196 else if (!strcmp(arg
, "signature"))
197 atom
->u
.contents
.option
= C_SIG
;
198 else if (!strcmp(arg
, "subject"))
199 atom
->u
.contents
.option
= C_SUB
;
200 else if (!strcmp(arg
, "trailers"))
201 atom
->u
.contents
.option
= C_TRAILERS
;
202 else if (skip_prefix(arg
, "lines=", &arg
)) {
203 atom
->u
.contents
.option
= C_LINES
;
204 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
205 die(_("positive value expected contents:lines=%s"), arg
);
207 die(_("unrecognized %%(contents) argument: %s"), arg
);
210 static void objectname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
213 atom
->u
.objectname
.option
= O_FULL
;
214 else if (!strcmp(arg
, "short"))
215 atom
->u
.objectname
.option
= O_SHORT
;
216 else if (skip_prefix(arg
, "short=", &arg
)) {
217 atom
->u
.objectname
.option
= O_LENGTH
;
218 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
219 atom
->u
.objectname
.length
== 0)
220 die(_("positive value expected objectname:short=%s"), arg
);
221 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
222 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
224 die(_("unrecognized %%(objectname) argument: %s"), arg
);
227 static void refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
229 refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
);
232 static align_type
parse_align_position(const char *s
)
234 if (!strcmp(s
, "right"))
236 else if (!strcmp(s
, "middle"))
238 else if (!strcmp(s
, "left"))
243 static void align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
245 struct align
*align
= &atom
->u
.align
;
246 struct string_list params
= STRING_LIST_INIT_DUP
;
248 unsigned int width
= ~0U;
251 die(_("expected format: %%(align:<width>,<position>)"));
253 align
->position
= ALIGN_LEFT
;
255 string_list_split(¶ms
, arg
, ',', -1);
256 for (i
= 0; i
< params
.nr
; i
++) {
257 const char *s
= params
.items
[i
].string
;
260 if (skip_prefix(s
, "position=", &s
)) {
261 position
= parse_align_position(s
);
263 die(_("unrecognized position:%s"), s
);
264 align
->position
= position
;
265 } else if (skip_prefix(s
, "width=", &s
)) {
266 if (strtoul_ui(s
, 10, &width
))
267 die(_("unrecognized width:%s"), s
);
268 } else if (!strtoul_ui(s
, 10, &width
))
270 else if ((position
= parse_align_position(s
)) >= 0)
271 align
->position
= position
;
273 die(_("unrecognized %%(align) argument: %s"), s
);
277 die(_("positive width expected with the %%(align) atom"));
278 align
->width
= width
;
279 string_list_clear(¶ms
, 0);
282 static void if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
285 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
287 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
288 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
289 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
290 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
292 die(_("unrecognized %%(if) argument: %s"), arg
);
296 static void head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
)
298 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
304 void (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
, const char *arg
);
306 { "refname" , FIELD_STR
, refname_atom_parser
},
308 { "objectsize", FIELD_ULONG
},
309 { "objectname", FIELD_STR
, objectname_atom_parser
},
312 { "numparent", FIELD_ULONG
},
319 { "authordate", FIELD_TIME
},
322 { "committeremail" },
323 { "committerdate", FIELD_TIME
},
327 { "taggerdate", FIELD_TIME
},
329 { "creatordate", FIELD_TIME
},
330 { "subject", FIELD_STR
, subject_atom_parser
},
331 { "body", FIELD_STR
, body_atom_parser
},
332 { "trailers", FIELD_STR
, trailers_atom_parser
},
333 { "contents", FIELD_STR
, contents_atom_parser
},
334 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
335 { "push", FIELD_STR
, remote_ref_atom_parser
},
336 { "symref", FIELD_STR
, refname_atom_parser
},
338 { "HEAD", FIELD_STR
, head_atom_parser
},
339 { "color", FIELD_STR
, color_atom_parser
},
340 { "align", FIELD_STR
, align_atom_parser
},
342 { "if", FIELD_STR
, if_atom_parser
},
347 #define REF_FORMATTING_STATE_INIT { 0, NULL }
349 struct ref_formatting_stack
{
350 struct ref_formatting_stack
*prev
;
351 struct strbuf output
;
352 void (*at_end
)(struct ref_formatting_stack
**stack
);
356 struct ref_formatting_state
{
358 struct ref_formatting_stack
*stack
;
363 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
364 uintmax_t value
; /* used for sorting when not FIELD_STR */
365 struct used_atom
*atom
;
369 * Used to parse format string and sort specifiers
371 static int parse_ref_filter_atom(const struct ref_format
*format
,
372 const char *atom
, const char *ep
)
379 if (*sp
== '*' && sp
< ep
)
382 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
384 /* Do we have the atom already used elsewhere? */
385 for (i
= 0; i
< used_atom_cnt
; i
++) {
386 int len
= strlen(used_atom
[i
].name
);
387 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
392 * If the atom name has a colon, strip it and everything after
393 * it off - it specifies the format for this entry, and
394 * shouldn't be used for checking against the valid_atom
397 arg
= memchr(sp
, ':', ep
- sp
);
398 atom_len
= (arg
? arg
: ep
) - sp
;
400 /* Is the atom a valid one? */
401 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
402 int len
= strlen(valid_atom
[i
].name
);
403 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
407 if (ARRAY_SIZE(valid_atom
) <= i
)
408 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
410 /* Add it in, including the deref prefix */
413 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
414 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
415 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
417 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
420 * Treat empty sub-arguments list as NULL (i.e.,
421 * "%(atom:)" is equivalent to "%(atom)").
426 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
427 if (valid_atom
[i
].parser
)
428 valid_atom
[i
].parser(format
, &used_atom
[at
], arg
);
431 if (!strcmp(valid_atom
[i
].name
, "symref"))
436 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
438 switch (quote_style
) {
440 strbuf_addstr(s
, str
);
443 sq_quote_buf(s
, str
);
446 perl_quote_buf(s
, str
);
449 python_quote_buf(s
, str
);
452 tcl_quote_buf(s
, str
);
457 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
460 * Quote formatting is only done when the stack has a single
461 * element. Otherwise quote formatting is done on the
462 * element's entire output strbuf when the %(end) atom is
465 if (!state
->stack
->prev
)
466 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
468 strbuf_addstr(&state
->stack
->output
, v
->s
);
471 static void push_stack_element(struct ref_formatting_stack
**stack
)
473 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
475 strbuf_init(&s
->output
, 0);
480 static void pop_stack_element(struct ref_formatting_stack
**stack
)
482 struct ref_formatting_stack
*current
= *stack
;
483 struct ref_formatting_stack
*prev
= current
->prev
;
486 strbuf_addbuf(&prev
->output
, ¤t
->output
);
487 strbuf_release(¤t
->output
);
492 static void end_align_handler(struct ref_formatting_stack
**stack
)
494 struct ref_formatting_stack
*cur
= *stack
;
495 struct align
*align
= (struct align
*)cur
->at_end_data
;
496 struct strbuf s
= STRBUF_INIT
;
498 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
499 strbuf_swap(&cur
->output
, &s
);
503 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
505 struct ref_formatting_stack
*new;
507 push_stack_element(&state
->stack
);
509 new->at_end
= end_align_handler
;
510 new->at_end_data
= &atomv
->atom
->u
.align
;
513 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
515 struct ref_formatting_stack
*cur
= *stack
;
516 struct ref_formatting_stack
*prev
= cur
->prev
;
517 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
519 if (!if_then_else
->then_atom_seen
)
520 die(_("format: %%(if) atom used without a %%(then) atom"));
522 if (if_then_else
->else_atom_seen
) {
524 * There is an %(else) atom: we need to drop one state from the
525 * stack, either the %(else) branch if the condition is satisfied, or
526 * the %(then) branch if it isn't.
528 if (if_then_else
->condition_satisfied
) {
529 strbuf_reset(&cur
->output
);
530 pop_stack_element(&cur
);
532 strbuf_swap(&cur
->output
, &prev
->output
);
533 strbuf_reset(&cur
->output
);
534 pop_stack_element(&cur
);
536 } else if (!if_then_else
->condition_satisfied
) {
538 * No %(else) atom: just drop the %(then) branch if the
539 * condition is not satisfied.
541 strbuf_reset(&cur
->output
);
548 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
550 struct ref_formatting_stack
*new;
551 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
553 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
554 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
556 push_stack_element(&state
->stack
);
558 new->at_end
= if_then_else_handler
;
559 new->at_end_data
= if_then_else
;
562 static int is_empty(const char *s
)
572 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
574 struct ref_formatting_stack
*cur
= state
->stack
;
575 struct if_then_else
*if_then_else
= NULL
;
577 if (cur
->at_end
== if_then_else_handler
)
578 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
580 die(_("format: %%(then) atom used without an %%(if) atom"));
581 if (if_then_else
->then_atom_seen
)
582 die(_("format: %%(then) atom used more than once"));
583 if (if_then_else
->else_atom_seen
)
584 die(_("format: %%(then) atom used after %%(else)"));
585 if_then_else
->then_atom_seen
= 1;
587 * If the 'equals' or 'notequals' attribute is used then
588 * perform the required comparison. If not, only non-empty
589 * strings satisfy the 'if' condition.
591 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
592 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
593 if_then_else
->condition_satisfied
= 1;
594 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
595 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
596 if_then_else
->condition_satisfied
= 1;
597 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
598 if_then_else
->condition_satisfied
= 1;
599 strbuf_reset(&cur
->output
);
602 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
604 struct ref_formatting_stack
*prev
= state
->stack
;
605 struct if_then_else
*if_then_else
= NULL
;
607 if (prev
->at_end
== if_then_else_handler
)
608 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
610 die(_("format: %%(else) atom used without an %%(if) atom"));
611 if (!if_then_else
->then_atom_seen
)
612 die(_("format: %%(else) atom used without a %%(then) atom"));
613 if (if_then_else
->else_atom_seen
)
614 die(_("format: %%(else) atom used more than once"));
615 if_then_else
->else_atom_seen
= 1;
616 push_stack_element(&state
->stack
);
617 state
->stack
->at_end_data
= prev
->at_end_data
;
618 state
->stack
->at_end
= prev
->at_end
;
621 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
623 struct ref_formatting_stack
*current
= state
->stack
;
624 struct strbuf s
= STRBUF_INIT
;
626 if (!current
->at_end
)
627 die(_("format: %%(end) atom used without corresponding atom"));
628 current
->at_end(&state
->stack
);
630 /* Stack may have been popped within at_end(), hence reset the current pointer */
631 current
= state
->stack
;
634 * Perform quote formatting when the stack element is that of
635 * a supporting atom. If nested then perform quote formatting
636 * only on the topmost supporting atom.
638 if (!current
->prev
->prev
) {
639 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
640 strbuf_swap(¤t
->output
, &s
);
643 pop_stack_element(&state
->stack
);
647 * In a format string, find the next occurrence of %(atom).
649 static const char *find_next(const char *cp
)
654 * %( is the start of an atom;
655 * %% is a quoted per-cent.
659 else if (cp
[1] == '%')
660 cp
++; /* skip over two % */
661 /* otherwise this is a singleton, literal % */
669 * Make sure the format string is well formed, and parse out
672 int verify_ref_format(struct ref_format
*format
)
676 format
->need_color_reset_at_eol
= 0;
677 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
678 const char *color
, *ep
= strchr(sp
, ')');
682 return error(_("malformed format string %s"), sp
);
683 /* sp points at "%(" and ep points at the closing ")" */
684 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
);
687 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
688 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
690 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
691 format
->need_color_reset_at_eol
= 0;
696 * Given an object name, read the object data and size, and return a
697 * "struct object". If the object data we are returning is also borrowed
698 * by the "struct object" representation, set *eaten as well---it is a
699 * signal from parse_object_buffer to us not to free the buffer.
701 static void *get_obj(const struct object_id
*oid
, struct object
**obj
, unsigned long *sz
, int *eaten
)
703 enum object_type type
;
704 void *buf
= read_sha1_file(oid
->hash
, &type
, sz
);
707 *obj
= parse_object_buffer(oid
, type
, *sz
, buf
, eaten
);
713 static int grab_objectname(const char *name
, const unsigned char *sha1
,
714 struct atom_value
*v
, struct used_atom
*atom
)
716 if (starts_with(name
, "objectname")) {
717 if (atom
->u
.objectname
.option
== O_SHORT
) {
718 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
720 } else if (atom
->u
.objectname
.option
== O_FULL
) {
721 v
->s
= xstrdup(sha1_to_hex(sha1
));
723 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
724 v
->s
= xstrdup(find_unique_abbrev(sha1
, atom
->u
.objectname
.length
));
727 die("BUG: unknown %%(objectname) option");
732 /* See grab_values */
733 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
737 for (i
= 0; i
< used_atom_cnt
; i
++) {
738 const char *name
= used_atom
[i
].name
;
739 struct atom_value
*v
= &val
[i
];
740 if (!!deref
!= (*name
== '*'))
744 if (!strcmp(name
, "objecttype"))
745 v
->s
= typename(obj
->type
);
746 else if (!strcmp(name
, "objectsize")) {
748 v
->s
= xstrfmt("%lu", sz
);
751 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
755 /* See grab_values */
756 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
759 struct tag
*tag
= (struct tag
*) obj
;
761 for (i
= 0; i
< used_atom_cnt
; i
++) {
762 const char *name
= used_atom
[i
].name
;
763 struct atom_value
*v
= &val
[i
];
764 if (!!deref
!= (*name
== '*'))
768 if (!strcmp(name
, "tag"))
770 else if (!strcmp(name
, "type") && tag
->tagged
)
771 v
->s
= typename(tag
->tagged
->type
);
772 else if (!strcmp(name
, "object") && tag
->tagged
)
773 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
777 /* See grab_values */
778 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
781 struct commit
*commit
= (struct commit
*) obj
;
783 for (i
= 0; i
< used_atom_cnt
; i
++) {
784 const char *name
= used_atom
[i
].name
;
785 struct atom_value
*v
= &val
[i
];
786 if (!!deref
!= (*name
== '*'))
790 if (!strcmp(name
, "tree")) {
791 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
793 else if (!strcmp(name
, "numparent")) {
794 v
->value
= commit_list_count(commit
->parents
);
795 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
797 else if (!strcmp(name
, "parent")) {
798 struct commit_list
*parents
;
799 struct strbuf s
= STRBUF_INIT
;
800 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
801 struct commit
*parent
= parents
->item
;
802 if (parents
!= commit
->parents
)
803 strbuf_addch(&s
, ' ');
804 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
806 v
->s
= strbuf_detach(&s
, NULL
);
811 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
815 if (!strncmp(buf
, who
, wholen
) &&
817 return buf
+ wholen
+ 1;
818 eol
= strchr(buf
, '\n');
823 return ""; /* end of header */
829 static const char *copy_line(const char *buf
)
831 const char *eol
= strchrnul(buf
, '\n');
832 return xmemdupz(buf
, eol
- buf
);
835 static const char *copy_name(const char *buf
)
838 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
839 if (!strncmp(cp
, " <", 2))
840 return xmemdupz(buf
, cp
- buf
);
845 static const char *copy_email(const char *buf
)
847 const char *email
= strchr(buf
, '<');
851 eoemail
= strchr(email
, '>');
854 return xmemdupz(email
, eoemail
+ 1 - email
);
857 static char *copy_subject(const char *buf
, unsigned long len
)
859 char *r
= xmemdupz(buf
, len
);
862 for (i
= 0; i
< len
; i
++)
869 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
871 const char *eoemail
= strstr(buf
, "> ");
873 timestamp_t timestamp
;
875 struct date_mode date_mode
= { DATE_NORMAL
};
879 * We got here because atomname ends in "date" or "date<something>";
880 * it's not possible that <something> is not ":<format>" because
881 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
882 * ":" means no format is specified, and use the default.
884 formatp
= strchr(atomname
, ':');
885 if (formatp
!= NULL
) {
887 parse_date_format(formatp
, &date_mode
);
892 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
893 if (timestamp
== TIME_MAX
)
895 tz
= strtol(zone
, NULL
, 10);
896 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
898 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
899 v
->value
= timestamp
;
906 /* See grab_values */
907 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
910 int wholen
= strlen(who
);
911 const char *wholine
= NULL
;
913 for (i
= 0; i
< used_atom_cnt
; i
++) {
914 const char *name
= used_atom
[i
].name
;
915 struct atom_value
*v
= &val
[i
];
916 if (!!deref
!= (*name
== '*'))
920 if (strncmp(who
, name
, wholen
))
922 if (name
[wholen
] != 0 &&
923 strcmp(name
+ wholen
, "name") &&
924 strcmp(name
+ wholen
, "email") &&
925 !starts_with(name
+ wholen
, "date"))
928 wholine
= find_wholine(who
, wholen
, buf
, sz
);
930 return; /* no point looking for it */
931 if (name
[wholen
] == 0)
932 v
->s
= copy_line(wholine
);
933 else if (!strcmp(name
+ wholen
, "name"))
934 v
->s
= copy_name(wholine
);
935 else if (!strcmp(name
+ wholen
, "email"))
936 v
->s
= copy_email(wholine
);
937 else if (starts_with(name
+ wholen
, "date"))
938 grab_date(wholine
, v
, name
);
942 * For a tag or a commit object, if "creator" or "creatordate" is
943 * requested, do something special.
945 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
946 return; /* "author" for commit object is not wanted */
948 wholine
= find_wholine(who
, wholen
, buf
, sz
);
951 for (i
= 0; i
< used_atom_cnt
; i
++) {
952 const char *name
= used_atom
[i
].name
;
953 struct atom_value
*v
= &val
[i
];
954 if (!!deref
!= (*name
== '*'))
959 if (starts_with(name
, "creatordate"))
960 grab_date(wholine
, v
, name
);
961 else if (!strcmp(name
, "creator"))
962 v
->s
= copy_line(wholine
);
966 static void find_subpos(const char *buf
, unsigned long sz
,
967 const char **sub
, unsigned long *sublen
,
968 const char **body
, unsigned long *bodylen
,
969 unsigned long *nonsiglen
,
970 const char **sig
, unsigned long *siglen
)
973 /* skip past header until we hit empty line */
974 while (*buf
&& *buf
!= '\n') {
975 eol
= strchrnul(buf
, '\n');
980 /* skip any empty lines */
984 /* parse signature first; we might not even have a subject line */
985 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
986 *siglen
= strlen(*sig
);
988 /* subject is first non-empty line */
990 /* subject goes to first empty line */
991 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
992 eol
= strchrnul(buf
, '\n');
997 *sublen
= buf
- *sub
;
998 /* drop trailing newline, if present */
999 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1002 /* skip any empty lines */
1003 while (*buf
== '\n')
1006 *bodylen
= strlen(buf
);
1007 *nonsiglen
= *sig
- buf
;
1011 * If 'lines' is greater than 0, append that many lines from the given
1012 * 'buf' of length 'size' to the given strbuf.
1014 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1017 const char *sp
, *eol
;
1022 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1024 strbuf_addstr(out
, "\n ");
1025 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1026 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1027 strbuf_add(out
, sp
, len
);
1034 /* See grab_values */
1035 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1038 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1039 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1041 for (i
= 0; i
< used_atom_cnt
; i
++) {
1042 struct used_atom
*atom
= &used_atom
[i
];
1043 const char *name
= atom
->name
;
1044 struct atom_value
*v
= &val
[i
];
1045 if (!!deref
!= (*name
== '*'))
1049 if (strcmp(name
, "subject") &&
1050 strcmp(name
, "body") &&
1051 strcmp(name
, "trailers") &&
1052 !starts_with(name
, "contents"))
1055 find_subpos(buf
, sz
,
1057 &bodypos
, &bodylen
, &nonsiglen
,
1060 if (atom
->u
.contents
.option
== C_SUB
)
1061 v
->s
= copy_subject(subpos
, sublen
);
1062 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1063 v
->s
= xmemdupz(bodypos
, bodylen
);
1064 else if (atom
->u
.contents
.option
== C_BODY
)
1065 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1066 else if (atom
->u
.contents
.option
== C_SIG
)
1067 v
->s
= xmemdupz(sigpos
, siglen
);
1068 else if (atom
->u
.contents
.option
== C_LINES
) {
1069 struct strbuf s
= STRBUF_INIT
;
1070 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1072 /* Size is the length of the message after removing the signature */
1073 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1074 v
->s
= strbuf_detach(&s
, NULL
);
1075 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1076 struct trailer_info info
;
1078 /* Search for trailer info */
1079 trailer_info_get(&info
, subpos
);
1080 v
->s
= xmemdupz(info
.trailer_start
,
1081 info
.trailer_end
- info
.trailer_start
);
1082 trailer_info_release(&info
);
1083 } else if (atom
->u
.contents
.option
== C_BARE
)
1084 v
->s
= xstrdup(subpos
);
1089 * We want to have empty print-string for field requests
1090 * that do not apply (e.g. "authordate" for a tag object)
1092 static void fill_missing_values(struct atom_value
*val
)
1095 for (i
= 0; i
< used_atom_cnt
; i
++) {
1096 struct atom_value
*v
= &val
[i
];
1103 * val is a list of atom_value to hold returned values. Extract
1104 * the values for atoms in used_atom array out of (obj, buf, sz).
1105 * when deref is false, (obj, buf, sz) is the object that is
1106 * pointed at by the ref itself; otherwise it is the object the
1107 * ref (which is a tag) refers to.
1109 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1111 grab_common_values(val
, deref
, obj
, buf
, sz
);
1112 switch (obj
->type
) {
1114 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1115 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1116 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1119 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1120 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1121 grab_person("author", val
, deref
, obj
, buf
, sz
);
1122 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1125 /* grab_tree_values(val, deref, obj, buf, sz); */
1128 /* grab_blob_values(val, deref, obj, buf, sz); */
1131 die("Eh? Object of type %d?", obj
->type
);
1135 static inline char *copy_advance(char *dst
, const char *src
)
1142 static const char *lstrip_ref_components(const char *refname
, int len
)
1144 long remaining
= len
;
1145 const char *start
= refname
;
1149 const char *p
= refname
;
1151 /* Find total no of '/' separated path-components */
1152 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1155 * The number of components we need to strip is now
1156 * the total minus the components to be left (Plus one
1157 * because we count the number of '/', but the number
1158 * of components is one more than the no of '/').
1160 remaining
= i
+ len
+ 1;
1163 while (remaining
> 0) {
1176 static const char *rstrip_ref_components(const char *refname
, int len
)
1178 long remaining
= len
;
1179 char *start
= xstrdup(refname
);
1183 const char *p
= refname
;
1185 /* Find total no of '/' separated path-components */
1186 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1189 * The number of components we need to strip is now
1190 * the total minus the components to be left (Plus one
1191 * because we count the number of '/', but the number
1192 * of components is one more than the no of '/').
1194 remaining
= i
+ len
+ 1;
1197 while (remaining
-- > 0) {
1198 char *p
= strrchr(start
, '/');
1207 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1209 if (atom
->option
== R_SHORT
)
1210 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1211 else if (atom
->option
== R_LSTRIP
)
1212 return lstrip_ref_components(refname
, atom
->lstrip
);
1213 else if (atom
->option
== R_RSTRIP
)
1214 return rstrip_ref_components(refname
, atom
->rstrip
);
1219 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1220 struct branch
*branch
, const char **s
)
1222 int num_ours
, num_theirs
;
1223 if (atom
->u
.remote_ref
.option
== RR_REF
)
1224 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1225 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1226 if (stat_tracking_info(branch
, &num_ours
,
1227 &num_theirs
, NULL
)) {
1228 *s
= xstrdup(msgs
.gone
);
1229 } else if (!num_ours
&& !num_theirs
)
1232 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1233 else if (!num_theirs
)
1234 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1236 *s
= xstrfmt(msgs
.ahead_behind
,
1237 num_ours
, num_theirs
);
1238 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1239 const char *to_free
= *s
;
1240 *s
= xstrfmt("[%s]", *s
);
1241 free((void *)to_free
);
1243 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1244 if (stat_tracking_info(branch
, &num_ours
,
1248 if (!num_ours
&& !num_theirs
)
1252 else if (!num_theirs
)
1257 die("BUG: unhandled RR_* enum");
1260 char *get_head_description(void)
1262 struct strbuf desc
= STRBUF_INIT
;
1263 struct wt_status_state state
;
1264 memset(&state
, 0, sizeof(state
));
1265 wt_status_get_state(&state
, 1);
1266 if (state
.rebase_in_progress
||
1267 state
.rebase_interactive_in_progress
)
1268 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1270 else if (state
.bisect_in_progress
)
1271 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1273 else if (state
.detached_from
) {
1274 if (state
.detached_at
)
1276 * TRANSLATORS: make sure this matches "HEAD
1277 * detached at " in wt-status.c
1279 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1280 state
.detached_from
);
1283 * TRANSLATORS: make sure this matches "HEAD
1284 * detached from " in wt-status.c
1286 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1287 state
.detached_from
);
1290 strbuf_addstr(&desc
, _("(no branch)"));
1293 free(state
.detached_from
);
1294 return strbuf_detach(&desc
, NULL
);
1297 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1302 return show_ref(&atom
->u
.refname
, ref
->symref
);
1305 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1307 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1308 return get_head_description();
1309 return show_ref(&atom
->u
.refname
, ref
->refname
);
1313 * Parse the object referred by ref, and grab needed value.
1315 static void populate_value(struct ref_array_item
*ref
)
1321 const struct object_id
*tagged
;
1323 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1325 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1326 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1332 /* Fill in specials first */
1333 for (i
= 0; i
< used_atom_cnt
; i
++) {
1334 struct used_atom
*atom
= &used_atom
[i
];
1335 const char *name
= used_atom
[i
].name
;
1336 struct atom_value
*v
= &ref
->value
[i
];
1338 const char *refname
;
1339 struct branch
*branch
= NULL
;
1341 v
->handler
= append_atom
;
1349 if (starts_with(name
, "refname"))
1350 refname
= get_refname(atom
, ref
);
1351 else if (starts_with(name
, "symref"))
1352 refname
= get_symref(atom
, ref
);
1353 else if (starts_with(name
, "upstream")) {
1354 const char *branch_name
;
1355 /* only local branches may have an upstream */
1356 if (!skip_prefix(ref
->refname
, "refs/heads/",
1359 branch
= branch_get(branch_name
);
1361 refname
= branch_get_upstream(branch
, NULL
);
1363 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1365 } else if (starts_with(name
, "push")) {
1366 const char *branch_name
;
1367 if (!skip_prefix(ref
->refname
, "refs/heads/",
1370 branch
= branch_get(branch_name
);
1372 refname
= branch_get_push(branch
, NULL
);
1375 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1377 } else if (starts_with(name
, "color:")) {
1378 v
->s
= atom
->u
.color
;
1380 } else if (!strcmp(name
, "flag")) {
1381 char buf
[256], *cp
= buf
;
1382 if (ref
->flag
& REF_ISSYMREF
)
1383 cp
= copy_advance(cp
, ",symref");
1384 if (ref
->flag
& REF_ISPACKED
)
1385 cp
= copy_advance(cp
, ",packed");
1390 v
->s
= xstrdup(buf
+ 1);
1393 } else if (!deref
&& grab_objectname(name
, ref
->objectname
.hash
, v
, atom
)) {
1395 } else if (!strcmp(name
, "HEAD")) {
1396 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1401 } else if (starts_with(name
, "align")) {
1402 v
->handler
= align_atom_handler
;
1404 } else if (!strcmp(name
, "end")) {
1405 v
->handler
= end_atom_handler
;
1407 } else if (starts_with(name
, "if")) {
1410 if (skip_prefix(name
, "if:", &s
))
1412 v
->handler
= if_atom_handler
;
1414 } else if (!strcmp(name
, "then")) {
1415 v
->handler
= then_atom_handler
;
1417 } else if (!strcmp(name
, "else")) {
1418 v
->handler
= else_atom_handler
;
1426 v
->s
= xstrfmt("%s^{}", refname
);
1429 for (i
= 0; i
< used_atom_cnt
; i
++) {
1430 struct atom_value
*v
= &ref
->value
[i
];
1437 buf
= get_obj(&ref
->objectname
, &obj
, &size
, &eaten
);
1439 die(_("missing object %s for %s"),
1440 oid_to_hex(&ref
->objectname
), ref
->refname
);
1442 die(_("parse_object_buffer failed on %s for %s"),
1443 oid_to_hex(&ref
->objectname
), ref
->refname
);
1445 grab_values(ref
->value
, 0, obj
, buf
, size
);
1450 * If there is no atom that wants to know about tagged
1451 * object, we are done.
1453 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1457 * If it is a tag object, see if we use a value that derefs
1458 * the object, and if we do grab the object it refers to.
1460 tagged
= &((struct tag
*)obj
)->tagged
->oid
;
1463 * NEEDSWORK: This derefs tag only once, which
1464 * is good to deal with chains of trust, but
1465 * is not consistent with what deref_tag() does
1466 * which peels the onion to the core.
1468 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1470 die(_("missing object %s for %s"),
1471 oid_to_hex(tagged
), ref
->refname
);
1473 die(_("parse_object_buffer failed on %s for %s"),
1474 oid_to_hex(tagged
), ref
->refname
);
1475 grab_values(ref
->value
, 1, obj
, buf
, size
);
1481 * Given a ref, return the value for the atom. This lazily gets value
1482 * out of the object by calling populate value.
1484 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1487 populate_value(ref
);
1488 fill_missing_values(ref
->value
);
1490 *v
= &ref
->value
[atom
];
1494 * Unknown has to be "0" here, because that's the default value for
1495 * contains_cache slab entries that have not yet been assigned.
1497 enum contains_result
{
1498 CONTAINS_UNKNOWN
= 0,
1503 define_commit_slab(contains_cache
, enum contains_result
);
1505 struct ref_filter_cbdata
{
1506 struct ref_array
*array
;
1507 struct ref_filter
*filter
;
1508 struct contains_cache contains_cache
;
1509 struct contains_cache no_contains_cache
;
1513 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1516 * At each recursion step, the stack items points to the commits whose
1517 * ancestors are to be inspected.
1519 struct contains_stack
{
1521 struct contains_stack_entry
{
1522 struct commit
*commit
;
1523 struct commit_list
*parents
;
1527 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1529 for (; want
; want
= want
->next
)
1530 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1536 * Test whether the candidate or one of its parents is contained in the list.
1537 * Do not recurse to find out, though, but return -1 if inconclusive.
1539 static enum contains_result
contains_test(struct commit
*candidate
,
1540 const struct commit_list
*want
,
1541 struct contains_cache
*cache
)
1543 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
1545 /* If we already have the answer cached, return that. */
1550 if (in_commit_list(want
, candidate
)) {
1551 *cached
= CONTAINS_YES
;
1552 return CONTAINS_YES
;
1555 /* Otherwise, we don't know; prepare to recurse */
1556 parse_commit_or_die(candidate
);
1557 return CONTAINS_UNKNOWN
;
1560 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1562 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1563 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1564 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1567 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1568 const struct commit_list
*want
,
1569 struct contains_cache
*cache
)
1571 struct contains_stack contains_stack
= { 0, 0, NULL
};
1572 enum contains_result result
= contains_test(candidate
, want
, cache
);
1574 if (result
!= CONTAINS_UNKNOWN
)
1577 push_to_contains_stack(candidate
, &contains_stack
);
1578 while (contains_stack
.nr
) {
1579 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1580 struct commit
*commit
= entry
->commit
;
1581 struct commit_list
*parents
= entry
->parents
;
1584 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
1585 contains_stack
.nr
--;
1588 * If we just popped the stack, parents->item has been marked,
1589 * therefore contains_test will return a meaningful yes/no.
1591 else switch (contains_test(parents
->item
, want
, cache
)) {
1593 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
1594 contains_stack
.nr
--;
1597 entry
->parents
= parents
->next
;
1599 case CONTAINS_UNKNOWN
:
1600 push_to_contains_stack(parents
->item
, &contains_stack
);
1604 free(contains_stack
.contains_stack
);
1605 return contains_test(candidate
, want
, cache
);
1608 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
1609 struct commit_list
*list
, struct contains_cache
*cache
)
1611 if (filter
->with_commit_tag_algo
)
1612 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
1613 return is_descendant_of(commit
, list
);
1617 * Return 1 if the refname matches one of the patterns, otherwise 0.
1618 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1619 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1620 * matches "refs/heads/mas*", too).
1622 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1624 const char **patterns
= filter
->name_patterns
;
1627 if (filter
->ignore_case
)
1628 flags
|= WM_CASEFOLD
;
1631 * When no '--format' option is given we need to skip the prefix
1632 * for matching refs of tags and branches.
1634 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1635 skip_prefix(refname
, "refs/heads/", &refname
) ||
1636 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1637 skip_prefix(refname
, "refs/", &refname
));
1639 for (; *patterns
; patterns
++) {
1640 if (!wildmatch(*patterns
, refname
, flags
))
1647 * Return 1 if the refname matches one of the patterns, otherwise 0.
1648 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1649 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1650 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1652 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1654 const char **pattern
= filter
->name_patterns
;
1655 int namelen
= strlen(refname
);
1656 unsigned flags
= WM_PATHNAME
;
1658 if (filter
->ignore_case
)
1659 flags
|= WM_CASEFOLD
;
1661 for (; *pattern
; pattern
++) {
1662 const char *p
= *pattern
;
1663 int plen
= strlen(p
);
1665 if ((plen
<= namelen
) &&
1666 !strncmp(refname
, p
, plen
) &&
1667 (refname
[plen
] == '\0' ||
1668 refname
[plen
] == '/' ||
1671 if (!wildmatch(p
, refname
, WM_PATHNAME
))
1677 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1678 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1680 if (!*filter
->name_patterns
)
1681 return 1; /* No pattern always matches */
1682 if (filter
->match_as_path
)
1683 return match_name_as_path(filter
, refname
);
1684 return match_pattern(filter
, refname
);
1688 * Find the longest prefix of pattern we can pass to
1689 * `for_each_fullref_in()`, namely the part of pattern preceding the
1690 * first glob character. (Note that `for_each_fullref_in()` is
1691 * perfectly happy working with a prefix that doesn't end at a
1692 * pathname component boundary.)
1694 static void find_longest_prefix(struct strbuf
*out
, const char *pattern
)
1698 for (p
= pattern
; *p
&& !is_glob_special(*p
); p
++)
1701 strbuf_add(out
, pattern
, p
- pattern
);
1705 * This is the same as for_each_fullref_in(), but it tries to iterate
1706 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1707 * pattern match, so the callback still has to match each ref individually.
1709 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1714 struct strbuf prefix
= STRBUF_INIT
;
1717 if (!filter
->match_as_path
) {
1719 * in this case, the patterns are applied after
1720 * prefixes like "refs/heads/" etc. are stripped off,
1721 * so we have to look at everything:
1723 return for_each_fullref_in("", cb
, cb_data
, broken
);
1726 if (!filter
->name_patterns
[0]) {
1727 /* no patterns; we have to look at everything */
1728 return for_each_fullref_in("", cb
, cb_data
, broken
);
1731 if (filter
->name_patterns
[1]) {
1733 * multiple patterns; in theory this could still work as long
1734 * as the patterns are disjoint. We'd just make multiple calls
1735 * to for_each_ref(). But if they're not disjoint, we'd end up
1736 * reporting the same ref multiple times. So let's punt on that
1739 return for_each_fullref_in("", cb
, cb_data
, broken
);
1742 find_longest_prefix(&prefix
, filter
->name_patterns
[0]);
1744 ret
= for_each_fullref_in(prefix
.buf
, cb
, cb_data
, broken
);
1745 strbuf_release(&prefix
);
1750 * Given a ref (sha1, refname), check if the ref belongs to the array
1751 * of sha1s. If the given ref is a tag, check if the given tag points
1752 * at one of the sha1s in the given sha1 array.
1753 * the given sha1_array.
1755 * 1. Only a single level of inderection is obtained, we might want to
1756 * change this to account for multiple levels (e.g. annotated tags
1757 * pointing to annotated tags pointing to a commit.)
1758 * 2. As the refs are cached we might know what refname peels to without
1759 * the need to parse the object via parse_object(). peel_ref() might be a
1760 * more efficient alternative to obtain the pointee.
1762 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1763 const struct object_id
*oid
,
1764 const char *refname
)
1766 const struct object_id
*tagged_oid
= NULL
;
1769 if (oid_array_lookup(points_at
, oid
) >= 0)
1771 obj
= parse_object(oid
);
1773 die(_("malformed object at '%s'"), refname
);
1774 if (obj
->type
== OBJ_TAG
)
1775 tagged_oid
= &((struct tag
*)obj
)->tagged
->oid
;
1776 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
1781 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1782 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1783 const unsigned char *objectname
,
1786 struct ref_array_item
*ref
;
1787 FLEX_ALLOC_STR(ref
, refname
, refname
);
1788 hashcpy(ref
->objectname
.hash
, objectname
);
1794 static int ref_kind_from_refname(const char *refname
)
1802 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1803 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1804 { "refs/tags/", FILTER_REFS_TAGS
}
1807 if (!strcmp(refname
, "HEAD"))
1808 return FILTER_REFS_DETACHED_HEAD
;
1810 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1811 if (starts_with(refname
, ref_kind
[i
].prefix
))
1812 return ref_kind
[i
].kind
;
1815 return FILTER_REFS_OTHERS
;
1818 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1820 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1821 filter
->kind
== FILTER_REFS_REMOTES
||
1822 filter
->kind
== FILTER_REFS_TAGS
)
1823 return filter
->kind
;
1824 return ref_kind_from_refname(refname
);
1828 * A call-back given to for_each_ref(). Filter refs and keep them for
1829 * later object processing.
1831 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1833 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1834 struct ref_filter
*filter
= ref_cbdata
->filter
;
1835 struct ref_array_item
*ref
;
1836 struct commit
*commit
= NULL
;
1839 if (flag
& REF_BAD_NAME
) {
1840 warning(_("ignoring ref with broken name %s"), refname
);
1844 if (flag
& REF_ISBROKEN
) {
1845 warning(_("ignoring broken ref %s"), refname
);
1849 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1850 kind
= filter_ref_kind(filter
, refname
);
1851 if (!(kind
& filter
->kind
))
1854 if (!filter_pattern_match(filter
, refname
))
1857 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
1861 * A merge filter is applied on refs pointing to commits. Hence
1862 * obtain the commit using the 'oid' available and discard all
1863 * non-commits early. The actual filtering is done later.
1865 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
1866 commit
= lookup_commit_reference_gently(oid
, 1);
1869 /* We perform the filtering for the '--contains' option... */
1870 if (filter
->with_commit
&&
1871 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
1873 /* ...or for the `--no-contains' option */
1874 if (filter
->no_commit
&&
1875 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
1880 * We do not open the object yet; sort may only need refname
1881 * to do its job and the resulting list may yet to be pruned
1882 * by maxcount logic.
1884 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1885 ref
->commit
= commit
;
1887 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1888 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1893 /* Free memory allocated for a ref_array_item */
1894 static void free_array_item(struct ref_array_item
*item
)
1896 free((char *)item
->symref
);
1900 /* Free all memory allocated for ref_array */
1901 void ref_array_clear(struct ref_array
*array
)
1905 for (i
= 0; i
< array
->nr
; i
++)
1906 free_array_item(array
->items
[i
]);
1907 FREE_AND_NULL(array
->items
);
1908 array
->nr
= array
->alloc
= 0;
1911 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1913 struct rev_info revs
;
1915 struct ref_filter
*filter
= ref_cbdata
->filter
;
1916 struct ref_array
*array
= ref_cbdata
->array
;
1917 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1919 init_revisions(&revs
, NULL
);
1921 for (i
= 0; i
< array
->nr
; i
++) {
1922 struct ref_array_item
*item
= array
->items
[i
];
1923 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1924 to_clear
[i
] = item
->commit
;
1927 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1928 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1931 if (prepare_revision_walk(&revs
))
1932 die(_("revision walk setup failed"));
1937 for (i
= 0; i
< old_nr
; i
++) {
1938 struct ref_array_item
*item
= array
->items
[i
];
1939 struct commit
*commit
= item
->commit
;
1941 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1943 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1944 array
->items
[array
->nr
++] = array
->items
[i
];
1946 free_array_item(item
);
1949 for (i
= 0; i
< old_nr
; i
++)
1950 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1951 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1956 * API for filtering a set of refs. Based on the type of refs the user
1957 * has requested, we iterate through those refs and apply filters
1958 * as per the given ref_filter structure and finally store the
1959 * filtered refs in the ref_array structure.
1961 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1963 struct ref_filter_cbdata ref_cbdata
;
1965 unsigned int broken
= 0;
1967 ref_cbdata
.array
= array
;
1968 ref_cbdata
.filter
= filter
;
1970 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1972 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1974 init_contains_cache(&ref_cbdata
.contains_cache
);
1975 init_contains_cache(&ref_cbdata
.no_contains_cache
);
1977 /* Simple per-ref filtering */
1979 die("filter_refs: invalid type");
1982 * For common cases where we need only branches or remotes or tags,
1983 * we only iterate through those refs. If a mix of refs is needed,
1984 * we iterate over all refs and filter out required refs with the help
1985 * of filter_ref_kind().
1987 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1988 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1989 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1990 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1991 else if (filter
->kind
== FILTER_REFS_TAGS
)
1992 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1993 else if (filter
->kind
& FILTER_REFS_ALL
)
1994 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
1995 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1996 head_ref(ref_filter_handler
, &ref_cbdata
);
1999 clear_contains_cache(&ref_cbdata
.contains_cache
);
2000 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2002 /* Filters that need revision walking */
2003 if (filter
->merge_commit
)
2004 do_merge_filter(&ref_cbdata
);
2009 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2011 struct atom_value
*va
, *vb
;
2013 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2014 int (*cmp_fn
)(const char *, const char *);
2016 get_ref_atom_value(a
, s
->atom
, &va
);
2017 get_ref_atom_value(b
, s
->atom
, &vb
);
2018 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2020 cmp
= versioncmp(va
->s
, vb
->s
);
2021 else if (cmp_type
== FIELD_STR
)
2022 cmp
= cmp_fn(va
->s
, vb
->s
);
2024 if (va
->value
< vb
->value
)
2026 else if (va
->value
== vb
->value
)
2027 cmp
= cmp_fn(a
->refname
, b
->refname
);
2032 return (s
->reverse
) ? -cmp
: cmp
;
2035 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2037 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2038 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2039 struct ref_sorting
*s
;
2041 for (s
= ref_sorting
; s
; s
= s
->next
) {
2042 int cmp
= cmp_ref_sorting(s
, a
, b
);
2049 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2051 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2054 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2056 struct strbuf
*s
= &state
->stack
->output
;
2058 while (*cp
&& (!ep
|| cp
< ep
)) {
2063 int ch
= hex2chr(cp
+ 1);
2065 strbuf_addch(s
, ch
);
2071 strbuf_addch(s
, *cp
);
2076 void format_ref_array_item(struct ref_array_item
*info
,
2077 const struct ref_format
*format
,
2078 struct strbuf
*final_buf
)
2080 const char *cp
, *sp
, *ep
;
2081 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2083 state
.quote_style
= format
->quote_style
;
2084 push_stack_element(&state
.stack
);
2086 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2087 struct atom_value
*atomv
;
2089 ep
= strchr(sp
, ')');
2091 append_literal(cp
, sp
, &state
);
2092 get_ref_atom_value(info
,
2093 parse_ref_filter_atom(format
, sp
+ 2, ep
),
2095 atomv
->handler(atomv
, &state
);
2098 sp
= cp
+ strlen(cp
);
2099 append_literal(cp
, sp
, &state
);
2101 if (format
->need_color_reset_at_eol
) {
2102 struct atom_value resetv
;
2103 resetv
.s
= GIT_COLOR_RESET
;
2104 append_atom(&resetv
, &state
);
2106 if (state
.stack
->prev
)
2107 die(_("format: %%(end) atom missing"));
2108 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2109 pop_stack_element(&state
.stack
);
2112 void show_ref_array_item(struct ref_array_item
*info
,
2113 const struct ref_format
*format
)
2115 struct strbuf final_buf
= STRBUF_INIT
;
2117 format_ref_array_item(info
, format
, &final_buf
);
2118 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2119 strbuf_release(&final_buf
);
2123 void pretty_print_ref(const char *name
, const unsigned char *sha1
,
2124 const struct ref_format
*format
)
2126 struct ref_array_item
*ref_item
;
2127 ref_item
= new_ref_array_item(name
, sha1
, 0);
2128 ref_item
->kind
= ref_kind_from_refname(name
);
2129 show_ref_array_item(ref_item
, format
);
2130 free_array_item(ref_item
);
2133 static int parse_sorting_atom(const char *atom
)
2136 * This parses an atom using a dummy ref_format, since we don't
2137 * actually care about the formatting details.
2139 struct ref_format dummy
= REF_FORMAT_INIT
;
2140 const char *end
= atom
+ strlen(atom
);
2141 return parse_ref_filter_atom(&dummy
, atom
, end
);
2144 /* If no sorting option is given, use refname to sort as default */
2145 struct ref_sorting
*ref_default_sorting(void)
2147 static const char cstr_name
[] = "refname";
2149 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2151 sorting
->next
= NULL
;
2152 sorting
->atom
= parse_sorting_atom(cstr_name
);
2156 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2158 struct ref_sorting
*s
;
2160 s
= xcalloc(1, sizeof(*s
));
2161 s
->next
= *sorting_tail
;
2168 if (skip_prefix(arg
, "version:", &arg
) ||
2169 skip_prefix(arg
, "v:", &arg
))
2171 s
->atom
= parse_sorting_atom(arg
);
2174 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2176 if (!arg
) /* should --no-sort void the list ? */
2178 parse_ref_sorting(opt
->value
, arg
);
2182 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2184 struct ref_filter
*rf
= opt
->value
;
2185 struct object_id oid
;
2186 int no_merged
= starts_with(opt
->long_name
, "no");
2190 return opterror(opt
, "is incompatible with --merged", 0);
2192 return opterror(opt
, "is incompatible with --no-merged", 0);
2196 rf
->merge
= no_merged
2197 ? REF_FILTER_MERGED_OMIT
2198 : REF_FILTER_MERGED_INCLUDE
;
2200 if (get_oid(arg
, &oid
))
2201 die(_("malformed object name %s"), arg
);
2203 rf
->merge_commit
= lookup_commit_reference_gently(&oid
, 0);
2204 if (!rf
->merge_commit
)
2205 return opterror(opt
, "must point to a commit", 0);