3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
17 #include "wt-status.h"
19 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
20 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
28 cmp_status cmp_status
;
30 unsigned int then_atom_seen
: 1,
32 condition_satisfied
: 1;
36 * An atom is a valid field atom listed below, possibly prefixed with
37 * a "*" to denote deref_tag().
39 * We parse given format string and sort specifiers, and make a list
40 * of properties that we need to extract out of objects. ref_array_item
41 * structure will hold an array of values extracted that can be
42 * indexed with the "atom number", which is an index into this
45 static struct used_atom
{
49 char color
[COLOR_MAXLEN
];
52 enum { RR_NORMAL
, RR_SHORTEN
, RR_TRACK
, RR_TRACKSHORT
} option
;
53 unsigned int nobracket
: 1;
56 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
60 cmp_status cmp_status
;
64 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
69 static int used_atom_cnt
, need_tagged
, need_symref
;
70 static int need_color_reset_at_eol
;
72 static void color_atom_parser(struct used_atom
*atom
, const char *color_value
)
75 die(_("expected format: %%(color:<color>)"));
76 if (color_parse(color_value
, atom
->u
.color
) < 0)
77 die(_("unrecognized color: %%(color:%s)"), color_value
);
80 static void remote_ref_atom_parser(struct used_atom
*atom
, const char *arg
)
82 struct string_list params
= STRING_LIST_INIT_DUP
;
86 atom
->u
.remote_ref
.option
= RR_NORMAL
;
90 atom
->u
.remote_ref
.nobracket
= 0;
91 string_list_split(¶ms
, arg
, ',', -1);
93 for (i
= 0; i
< params
.nr
; i
++) {
94 const char *s
= params
.items
[i
].string
;
96 if (!strcmp(s
, "short"))
97 atom
->u
.remote_ref
.option
= RR_SHORTEN
;
98 else if (!strcmp(s
, "track"))
99 atom
->u
.remote_ref
.option
= RR_TRACK
;
100 else if (!strcmp(s
, "trackshort"))
101 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
102 else if (!strcmp(s
, "nobracket"))
103 atom
->u
.remote_ref
.nobracket
= 1;
105 die(_("unrecognized format: %%(%s)"), atom
->name
);
108 string_list_clear(¶ms
, 0);
111 static void body_atom_parser(struct used_atom
*atom
, const char *arg
)
114 die(_("%%(body) does not take arguments"));
115 atom
->u
.contents
.option
= C_BODY_DEP
;
118 static void subject_atom_parser(struct used_atom
*atom
, const char *arg
)
121 die(_("%%(subject) does not take arguments"));
122 atom
->u
.contents
.option
= C_SUB
;
125 static void trailers_atom_parser(struct used_atom
*atom
, const char *arg
)
128 die(_("%%(trailers) does not take arguments"));
129 atom
->u
.contents
.option
= C_TRAILERS
;
132 static void contents_atom_parser(struct used_atom
*atom
, const char *arg
)
135 atom
->u
.contents
.option
= C_BARE
;
136 else if (!strcmp(arg
, "body"))
137 atom
->u
.contents
.option
= C_BODY
;
138 else if (!strcmp(arg
, "signature"))
139 atom
->u
.contents
.option
= C_SIG
;
140 else if (!strcmp(arg
, "subject"))
141 atom
->u
.contents
.option
= C_SUB
;
142 else if (!strcmp(arg
, "trailers"))
143 atom
->u
.contents
.option
= C_TRAILERS
;
144 else if (skip_prefix(arg
, "lines=", &arg
)) {
145 atom
->u
.contents
.option
= C_LINES
;
146 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
147 die(_("positive value expected contents:lines=%s"), arg
);
149 die(_("unrecognized %%(contents) argument: %s"), arg
);
152 static void objectname_atom_parser(struct used_atom
*atom
, const char *arg
)
155 atom
->u
.objectname
.option
= O_FULL
;
156 else if (!strcmp(arg
, "short"))
157 atom
->u
.objectname
.option
= O_SHORT
;
158 else if (skip_prefix(arg
, "short=", &arg
)) {
159 atom
->u
.objectname
.option
= O_LENGTH
;
160 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
161 atom
->u
.objectname
.length
== 0)
162 die(_("positive value expected objectname:short=%s"), arg
);
163 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
164 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
166 die(_("unrecognized %%(objectname) argument: %s"), arg
);
169 static align_type
parse_align_position(const char *s
)
171 if (!strcmp(s
, "right"))
173 else if (!strcmp(s
, "middle"))
175 else if (!strcmp(s
, "left"))
180 static void align_atom_parser(struct used_atom
*atom
, const char *arg
)
182 struct align
*align
= &atom
->u
.align
;
183 struct string_list params
= STRING_LIST_INIT_DUP
;
185 unsigned int width
= ~0U;
188 die(_("expected format: %%(align:<width>,<position>)"));
190 align
->position
= ALIGN_LEFT
;
192 string_list_split(¶ms
, arg
, ',', -1);
193 for (i
= 0; i
< params
.nr
; i
++) {
194 const char *s
= params
.items
[i
].string
;
197 if (skip_prefix(s
, "position=", &s
)) {
198 position
= parse_align_position(s
);
200 die(_("unrecognized position:%s"), s
);
201 align
->position
= position
;
202 } else if (skip_prefix(s
, "width=", &s
)) {
203 if (strtoul_ui(s
, 10, &width
))
204 die(_("unrecognized width:%s"), s
);
205 } else if (!strtoul_ui(s
, 10, &width
))
207 else if ((position
= parse_align_position(s
)) >= 0)
208 align
->position
= position
;
210 die(_("unrecognized %%(align) argument: %s"), s
);
214 die(_("positive width expected with the %%(align) atom"));
215 align
->width
= width
;
216 string_list_clear(¶ms
, 0);
219 static void if_atom_parser(struct used_atom
*atom
, const char *arg
)
222 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
224 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
225 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
226 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
227 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
229 die(_("unrecognized %%(if) argument: %s"), arg
);
237 void (*parser
)(struct used_atom
*atom
, const char *arg
);
241 { "objectsize", FIELD_ULONG
},
242 { "objectname", FIELD_STR
, objectname_atom_parser
},
245 { "numparent", FIELD_ULONG
},
252 { "authordate", FIELD_TIME
},
255 { "committeremail" },
256 { "committerdate", FIELD_TIME
},
260 { "taggerdate", FIELD_TIME
},
262 { "creatordate", FIELD_TIME
},
263 { "subject", FIELD_STR
, subject_atom_parser
},
264 { "body", FIELD_STR
, body_atom_parser
},
265 { "trailers", FIELD_STR
, trailers_atom_parser
},
266 { "contents", FIELD_STR
, contents_atom_parser
},
267 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
268 { "push", FIELD_STR
, remote_ref_atom_parser
},
272 { "color", FIELD_STR
, color_atom_parser
},
273 { "align", FIELD_STR
, align_atom_parser
},
275 { "if", FIELD_STR
, if_atom_parser
},
280 #define REF_FORMATTING_STATE_INIT { 0, NULL }
282 struct ref_formatting_stack
{
283 struct ref_formatting_stack
*prev
;
284 struct strbuf output
;
285 void (*at_end
)(struct ref_formatting_stack
**stack
);
289 struct ref_formatting_state
{
291 struct ref_formatting_stack
*stack
;
296 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
297 unsigned long ul
; /* used for sorting when not FIELD_STR */
298 struct used_atom
*atom
;
302 * Used to parse format string and sort specifiers
304 int parse_ref_filter_atom(const char *atom
, const char *ep
)
311 if (*sp
== '*' && sp
< ep
)
314 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
316 /* Do we have the atom already used elsewhere? */
317 for (i
= 0; i
< used_atom_cnt
; i
++) {
318 int len
= strlen(used_atom
[i
].name
);
319 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
324 * If the atom name has a colon, strip it and everything after
325 * it off - it specifies the format for this entry, and
326 * shouldn't be used for checking against the valid_atom
329 arg
= memchr(sp
, ':', ep
- sp
);
330 atom_len
= (arg
? arg
: ep
) - sp
;
332 /* Is the atom a valid one? */
333 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
334 int len
= strlen(valid_atom
[i
].name
);
335 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
339 if (ARRAY_SIZE(valid_atom
) <= i
)
340 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
342 /* Add it in, including the deref prefix */
345 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
346 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
347 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
349 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
350 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
351 if (valid_atom
[i
].parser
)
352 valid_atom
[i
].parser(&used_atom
[at
], arg
);
355 if (!strcmp(valid_atom
[i
].name
, "symref"))
360 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
362 switch (quote_style
) {
364 strbuf_addstr(s
, str
);
367 sq_quote_buf(s
, str
);
370 perl_quote_buf(s
, str
);
373 python_quote_buf(s
, str
);
376 tcl_quote_buf(s
, str
);
381 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
384 * Quote formatting is only done when the stack has a single
385 * element. Otherwise quote formatting is done on the
386 * element's entire output strbuf when the %(end) atom is
389 if (!state
->stack
->prev
)
390 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
392 strbuf_addstr(&state
->stack
->output
, v
->s
);
395 static void push_stack_element(struct ref_formatting_stack
**stack
)
397 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
399 strbuf_init(&s
->output
, 0);
404 static void pop_stack_element(struct ref_formatting_stack
**stack
)
406 struct ref_formatting_stack
*current
= *stack
;
407 struct ref_formatting_stack
*prev
= current
->prev
;
410 strbuf_addbuf(&prev
->output
, ¤t
->output
);
411 strbuf_release(¤t
->output
);
416 static void end_align_handler(struct ref_formatting_stack
**stack
)
418 struct ref_formatting_stack
*cur
= *stack
;
419 struct align
*align
= (struct align
*)cur
->at_end_data
;
420 struct strbuf s
= STRBUF_INIT
;
422 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
423 strbuf_swap(&cur
->output
, &s
);
427 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
429 struct ref_formatting_stack
*new;
431 push_stack_element(&state
->stack
);
433 new->at_end
= end_align_handler
;
434 new->at_end_data
= &atomv
->atom
->u
.align
;
437 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
439 struct ref_formatting_stack
*cur
= *stack
;
440 struct ref_formatting_stack
*prev
= cur
->prev
;
441 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
443 if (!if_then_else
->then_atom_seen
)
444 die(_("format: %%(if) atom used without a %%(then) atom"));
446 if (if_then_else
->else_atom_seen
) {
448 * There is an %(else) atom: we need to drop one state from the
449 * stack, either the %(else) branch if the condition is satisfied, or
450 * the %(then) branch if it isn't.
452 if (if_then_else
->condition_satisfied
) {
453 strbuf_reset(&cur
->output
);
454 pop_stack_element(&cur
);
456 strbuf_swap(&cur
->output
, &prev
->output
);
457 strbuf_reset(&cur
->output
);
458 pop_stack_element(&cur
);
460 } else if (!if_then_else
->condition_satisfied
) {
462 * No %(else) atom: just drop the %(then) branch if the
463 * condition is not satisfied.
465 strbuf_reset(&cur
->output
);
472 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
474 struct ref_formatting_stack
*new;
475 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
477 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
478 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
480 push_stack_element(&state
->stack
);
482 new->at_end
= if_then_else_handler
;
483 new->at_end_data
= if_then_else
;
486 static int is_empty(const char *s
)
496 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
498 struct ref_formatting_stack
*cur
= state
->stack
;
499 struct if_then_else
*if_then_else
= NULL
;
501 if (cur
->at_end
== if_then_else_handler
)
502 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
504 die(_("format: %%(then) atom used without an %%(if) atom"));
505 if (if_then_else
->then_atom_seen
)
506 die(_("format: %%(then) atom used more than once"));
507 if (if_then_else
->else_atom_seen
)
508 die(_("format: %%(then) atom used after %%(else)"));
509 if_then_else
->then_atom_seen
= 1;
511 * If the 'equals' or 'notequals' attribute is used then
512 * perform the required comparison. If not, only non-empty
513 * strings satisfy the 'if' condition.
515 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
516 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
517 if_then_else
->condition_satisfied
= 1;
518 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
519 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
520 if_then_else
->condition_satisfied
= 1;
521 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
522 if_then_else
->condition_satisfied
= 1;
523 strbuf_reset(&cur
->output
);
526 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
528 struct ref_formatting_stack
*prev
= state
->stack
;
529 struct if_then_else
*if_then_else
= NULL
;
531 if (prev
->at_end
== if_then_else_handler
)
532 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
534 die(_("format: %%(else) atom used without an %%(if) atom"));
535 if (!if_then_else
->then_atom_seen
)
536 die(_("format: %%(else) atom used without a %%(then) atom"));
537 if (if_then_else
->else_atom_seen
)
538 die(_("format: %%(else) atom used more than once"));
539 if_then_else
->else_atom_seen
= 1;
540 push_stack_element(&state
->stack
);
541 state
->stack
->at_end_data
= prev
->at_end_data
;
542 state
->stack
->at_end
= prev
->at_end
;
545 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
547 struct ref_formatting_stack
*current
= state
->stack
;
548 struct strbuf s
= STRBUF_INIT
;
550 if (!current
->at_end
)
551 die(_("format: %%(end) atom used without corresponding atom"));
552 current
->at_end(&state
->stack
);
554 /* Stack may have been popped within at_end(), hence reset the current pointer */
555 current
= state
->stack
;
558 * Perform quote formatting when the stack element is that of
559 * a supporting atom. If nested then perform quote formatting
560 * only on the topmost supporting atom.
562 if (!current
->prev
->prev
) {
563 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
564 strbuf_swap(¤t
->output
, &s
);
567 pop_stack_element(&state
->stack
);
571 * In a format string, find the next occurrence of %(atom).
573 static const char *find_next(const char *cp
)
578 * %( is the start of an atom;
579 * %% is a quoted per-cent.
583 else if (cp
[1] == '%')
584 cp
++; /* skip over two % */
585 /* otherwise this is a singleton, literal % */
593 * Make sure the format string is well formed, and parse out
596 int verify_ref_format(const char *format
)
600 need_color_reset_at_eol
= 0;
601 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
602 const char *color
, *ep
= strchr(sp
, ')');
606 return error(_("malformed format string %s"), sp
);
607 /* sp points at "%(" and ep points at the closing ")" */
608 at
= parse_ref_filter_atom(sp
+ 2, ep
);
611 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
612 need_color_reset_at_eol
= !!strcmp(color
, "reset");
618 * Given an object name, read the object data and size, and return a
619 * "struct object". If the object data we are returning is also borrowed
620 * by the "struct object" representation, set *eaten as well---it is a
621 * signal from parse_object_buffer to us not to free the buffer.
623 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
625 enum object_type type
;
626 void *buf
= read_sha1_file(sha1
, &type
, sz
);
629 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
635 static int grab_objectname(const char *name
, const unsigned char *sha1
,
636 struct atom_value
*v
, struct used_atom
*atom
)
638 if (starts_with(name
, "objectname")) {
639 if (atom
->u
.objectname
.option
== O_SHORT
) {
640 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
642 } else if (atom
->u
.objectname
.option
== O_FULL
) {
643 v
->s
= xstrdup(sha1_to_hex(sha1
));
645 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
646 v
->s
= xstrdup(find_unique_abbrev(sha1
, atom
->u
.objectname
.length
));
649 die("BUG: unknown %%(objectname) option");
654 /* See grab_values */
655 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
659 for (i
= 0; i
< used_atom_cnt
; i
++) {
660 const char *name
= used_atom
[i
].name
;
661 struct atom_value
*v
= &val
[i
];
662 if (!!deref
!= (*name
== '*'))
666 if (!strcmp(name
, "objecttype"))
667 v
->s
= typename(obj
->type
);
668 else if (!strcmp(name
, "objectsize")) {
670 v
->s
= xstrfmt("%lu", sz
);
673 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
677 /* See grab_values */
678 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
681 struct tag
*tag
= (struct tag
*) obj
;
683 for (i
= 0; i
< used_atom_cnt
; i
++) {
684 const char *name
= used_atom
[i
].name
;
685 struct atom_value
*v
= &val
[i
];
686 if (!!deref
!= (*name
== '*'))
690 if (!strcmp(name
, "tag"))
692 else if (!strcmp(name
, "type") && tag
->tagged
)
693 v
->s
= typename(tag
->tagged
->type
);
694 else if (!strcmp(name
, "object") && tag
->tagged
)
695 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
699 /* See grab_values */
700 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
703 struct commit
*commit
= (struct commit
*) obj
;
705 for (i
= 0; i
< used_atom_cnt
; i
++) {
706 const char *name
= used_atom
[i
].name
;
707 struct atom_value
*v
= &val
[i
];
708 if (!!deref
!= (*name
== '*'))
712 if (!strcmp(name
, "tree")) {
713 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
715 else if (!strcmp(name
, "numparent")) {
716 v
->ul
= commit_list_count(commit
->parents
);
717 v
->s
= xstrfmt("%lu", v
->ul
);
719 else if (!strcmp(name
, "parent")) {
720 struct commit_list
*parents
;
721 struct strbuf s
= STRBUF_INIT
;
722 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
723 struct commit
*parent
= parents
->item
;
724 if (parents
!= commit
->parents
)
725 strbuf_addch(&s
, ' ');
726 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
728 v
->s
= strbuf_detach(&s
, NULL
);
733 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
737 if (!strncmp(buf
, who
, wholen
) &&
739 return buf
+ wholen
+ 1;
740 eol
= strchr(buf
, '\n');
745 return ""; /* end of header */
751 static const char *copy_line(const char *buf
)
753 const char *eol
= strchrnul(buf
, '\n');
754 return xmemdupz(buf
, eol
- buf
);
757 static const char *copy_name(const char *buf
)
760 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
761 if (!strncmp(cp
, " <", 2))
762 return xmemdupz(buf
, cp
- buf
);
767 static const char *copy_email(const char *buf
)
769 const char *email
= strchr(buf
, '<');
773 eoemail
= strchr(email
, '>');
776 return xmemdupz(email
, eoemail
+ 1 - email
);
779 static char *copy_subject(const char *buf
, unsigned long len
)
781 char *r
= xmemdupz(buf
, len
);
784 for (i
= 0; i
< len
; i
++)
791 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
793 const char *eoemail
= strstr(buf
, "> ");
795 unsigned long timestamp
;
797 struct date_mode date_mode
= { DATE_NORMAL
};
801 * We got here because atomname ends in "date" or "date<something>";
802 * it's not possible that <something> is not ":<format>" because
803 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
804 * ":" means no format is specified, and use the default.
806 formatp
= strchr(atomname
, ':');
807 if (formatp
!= NULL
) {
809 parse_date_format(formatp
, &date_mode
);
814 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
815 if (timestamp
== ULONG_MAX
)
817 tz
= strtol(zone
, NULL
, 10);
818 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
820 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
828 /* See grab_values */
829 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
832 int wholen
= strlen(who
);
833 const char *wholine
= NULL
;
835 for (i
= 0; i
< used_atom_cnt
; i
++) {
836 const char *name
= used_atom
[i
].name
;
837 struct atom_value
*v
= &val
[i
];
838 if (!!deref
!= (*name
== '*'))
842 if (strncmp(who
, name
, wholen
))
844 if (name
[wholen
] != 0 &&
845 strcmp(name
+ wholen
, "name") &&
846 strcmp(name
+ wholen
, "email") &&
847 !starts_with(name
+ wholen
, "date"))
850 wholine
= find_wholine(who
, wholen
, buf
, sz
);
852 return; /* no point looking for it */
853 if (name
[wholen
] == 0)
854 v
->s
= copy_line(wholine
);
855 else if (!strcmp(name
+ wholen
, "name"))
856 v
->s
= copy_name(wholine
);
857 else if (!strcmp(name
+ wholen
, "email"))
858 v
->s
= copy_email(wholine
);
859 else if (starts_with(name
+ wholen
, "date"))
860 grab_date(wholine
, v
, name
);
864 * For a tag or a commit object, if "creator" or "creatordate" is
865 * requested, do something special.
867 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
868 return; /* "author" for commit object is not wanted */
870 wholine
= find_wholine(who
, wholen
, buf
, sz
);
873 for (i
= 0; i
< used_atom_cnt
; i
++) {
874 const char *name
= used_atom
[i
].name
;
875 struct atom_value
*v
= &val
[i
];
876 if (!!deref
!= (*name
== '*'))
881 if (starts_with(name
, "creatordate"))
882 grab_date(wholine
, v
, name
);
883 else if (!strcmp(name
, "creator"))
884 v
->s
= copy_line(wholine
);
888 static void find_subpos(const char *buf
, unsigned long sz
,
889 const char **sub
, unsigned long *sublen
,
890 const char **body
, unsigned long *bodylen
,
891 unsigned long *nonsiglen
,
892 const char **sig
, unsigned long *siglen
)
895 /* skip past header until we hit empty line */
896 while (*buf
&& *buf
!= '\n') {
897 eol
= strchrnul(buf
, '\n');
902 /* skip any empty lines */
906 /* parse signature first; we might not even have a subject line */
907 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
908 *siglen
= strlen(*sig
);
910 /* subject is first non-empty line */
912 /* subject goes to first empty line */
913 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
914 eol
= strchrnul(buf
, '\n');
919 *sublen
= buf
- *sub
;
920 /* drop trailing newline, if present */
921 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
924 /* skip any empty lines */
928 *bodylen
= strlen(buf
);
929 *nonsiglen
= *sig
- buf
;
933 * If 'lines' is greater than 0, append that many lines from the given
934 * 'buf' of length 'size' to the given strbuf.
936 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
939 const char *sp
, *eol
;
944 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
946 strbuf_addstr(out
, "\n ");
947 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
948 len
= eol
? eol
- sp
: size
- (sp
- buf
);
949 strbuf_add(out
, sp
, len
);
956 /* See grab_values */
957 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
960 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
961 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
963 for (i
= 0; i
< used_atom_cnt
; i
++) {
964 struct used_atom
*atom
= &used_atom
[i
];
965 const char *name
= atom
->name
;
966 struct atom_value
*v
= &val
[i
];
967 if (!!deref
!= (*name
== '*'))
971 if (strcmp(name
, "subject") &&
972 strcmp(name
, "body") &&
973 strcmp(name
, "trailers") &&
974 !starts_with(name
, "contents"))
979 &bodypos
, &bodylen
, &nonsiglen
,
982 if (atom
->u
.contents
.option
== C_SUB
)
983 v
->s
= copy_subject(subpos
, sublen
);
984 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
985 v
->s
= xmemdupz(bodypos
, bodylen
);
986 else if (atom
->u
.contents
.option
== C_BODY
)
987 v
->s
= xmemdupz(bodypos
, nonsiglen
);
988 else if (atom
->u
.contents
.option
== C_SIG
)
989 v
->s
= xmemdupz(sigpos
, siglen
);
990 else if (atom
->u
.contents
.option
== C_LINES
) {
991 struct strbuf s
= STRBUF_INIT
;
992 const char *contents_end
= bodylen
+ bodypos
- siglen
;
994 /* Size is the length of the message after removing the signature */
995 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
996 v
->s
= strbuf_detach(&s
, NULL
);
997 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
998 struct trailer_info info
;
1000 /* Search for trailer info */
1001 trailer_info_get(&info
, subpos
);
1002 v
->s
= xmemdupz(info
.trailer_start
,
1003 info
.trailer_end
- info
.trailer_start
);
1004 trailer_info_release(&info
);
1005 } else if (atom
->u
.contents
.option
== C_BARE
)
1006 v
->s
= xstrdup(subpos
);
1011 * We want to have empty print-string for field requests
1012 * that do not apply (e.g. "authordate" for a tag object)
1014 static void fill_missing_values(struct atom_value
*val
)
1017 for (i
= 0; i
< used_atom_cnt
; i
++) {
1018 struct atom_value
*v
= &val
[i
];
1025 * val is a list of atom_value to hold returned values. Extract
1026 * the values for atoms in used_atom array out of (obj, buf, sz).
1027 * when deref is false, (obj, buf, sz) is the object that is
1028 * pointed at by the ref itself; otherwise it is the object the
1029 * ref (which is a tag) refers to.
1031 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1033 grab_common_values(val
, deref
, obj
, buf
, sz
);
1034 switch (obj
->type
) {
1036 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1037 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1038 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1041 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1042 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1043 grab_person("author", val
, deref
, obj
, buf
, sz
);
1044 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1047 /* grab_tree_values(val, deref, obj, buf, sz); */
1050 /* grab_blob_values(val, deref, obj, buf, sz); */
1053 die("Eh? Object of type %d?", obj
->type
);
1057 static inline char *copy_advance(char *dst
, const char *src
)
1064 static const char *strip_ref_components(const char *refname
, const char *nr_arg
)
1067 long nr
= strtol(nr_arg
, &end
, 10);
1068 long remaining
= nr
;
1069 const char *start
= refname
;
1071 if (nr
< 1 || *end
!= '\0')
1072 die(_(":strip= requires a positive integer argument"));
1077 die(_("ref '%s' does not have %ld components to :strip"),
1087 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1088 struct branch
*branch
, const char **s
)
1090 int num_ours
, num_theirs
;
1091 if (atom
->u
.remote_ref
.option
== RR_SHORTEN
)
1092 *s
= shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1093 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1094 if (stat_tracking_info(branch
, &num_ours
,
1095 &num_theirs
, NULL
)) {
1096 *s
= xstrdup("gone");
1097 } else if (!num_ours
&& !num_theirs
)
1100 *s
= xstrfmt("behind %d", num_theirs
);
1101 else if (!num_theirs
)
1102 *s
= xstrfmt("ahead %d", num_ours
);
1104 *s
= xstrfmt("ahead %d, behind %d",
1105 num_ours
, num_theirs
);
1106 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1107 const char *to_free
= *s
;
1108 *s
= xstrfmt("[%s]", *s
);
1109 free((void *)to_free
);
1111 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1112 if (stat_tracking_info(branch
, &num_ours
,
1116 if (!num_ours
&& !num_theirs
)
1120 else if (!num_theirs
)
1124 } else /* RR_NORMAL */
1128 char *get_head_description(void)
1130 struct strbuf desc
= STRBUF_INIT
;
1131 struct wt_status_state state
;
1132 memset(&state
, 0, sizeof(state
));
1133 wt_status_get_state(&state
, 1);
1134 if (state
.rebase_in_progress
||
1135 state
.rebase_interactive_in_progress
)
1136 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1138 else if (state
.bisect_in_progress
)
1139 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1141 else if (state
.detached_from
) {
1142 /* TRANSLATORS: make sure these match _("HEAD detached at ")
1143 and _("HEAD detached from ") in wt-status.c */
1144 if (state
.detached_at
)
1145 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1146 state
.detached_from
);
1148 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1149 state
.detached_from
);
1152 strbuf_addstr(&desc
, _("(no branch)"));
1155 free(state
.detached_from
);
1156 return strbuf_detach(&desc
, NULL
);
1160 * Parse the object referred by ref, and grab needed value.
1162 static void populate_value(struct ref_array_item
*ref
)
1168 const unsigned char *tagged
;
1170 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1172 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1173 unsigned char unused1
[20];
1174 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1180 /* Fill in specials first */
1181 for (i
= 0; i
< used_atom_cnt
; i
++) {
1182 struct used_atom
*atom
= &used_atom
[i
];
1183 const char *name
= used_atom
[i
].name
;
1184 struct atom_value
*v
= &ref
->value
[i
];
1186 const char *refname
;
1187 const char *formatp
;
1188 struct branch
*branch
= NULL
;
1190 v
->handler
= append_atom
;
1198 if (starts_with(name
, "refname")) {
1199 refname
= ref
->refname
;
1200 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1201 refname
= get_head_description();
1202 } else if (starts_with(name
, "symref"))
1203 refname
= ref
->symref
? ref
->symref
: "";
1204 else if (starts_with(name
, "upstream")) {
1205 const char *branch_name
;
1206 /* only local branches may have an upstream */
1207 if (!skip_prefix(ref
->refname
, "refs/heads/",
1210 branch
= branch_get(branch_name
);
1212 refname
= branch_get_upstream(branch
, NULL
);
1214 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1216 } else if (starts_with(name
, "push")) {
1217 const char *branch_name
;
1218 if (!skip_prefix(ref
->refname
, "refs/heads/",
1221 branch
= branch_get(branch_name
);
1223 refname
= branch_get_push(branch
, NULL
);
1226 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1228 } else if (starts_with(name
, "color:")) {
1229 v
->s
= atom
->u
.color
;
1231 } else if (!strcmp(name
, "flag")) {
1232 char buf
[256], *cp
= buf
;
1233 if (ref
->flag
& REF_ISSYMREF
)
1234 cp
= copy_advance(cp
, ",symref");
1235 if (ref
->flag
& REF_ISPACKED
)
1236 cp
= copy_advance(cp
, ",packed");
1241 v
->s
= xstrdup(buf
+ 1);
1244 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
, atom
)) {
1246 } else if (!strcmp(name
, "HEAD")) {
1248 unsigned char sha1
[20];
1250 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1252 if (head
&& !strcmp(ref
->refname
, head
))
1257 } else if (starts_with(name
, "align")) {
1258 v
->handler
= align_atom_handler
;
1260 } else if (!strcmp(name
, "end")) {
1261 v
->handler
= end_atom_handler
;
1263 } else if (starts_with(name
, "if")) {
1266 if (skip_prefix(name
, "if:", &s
))
1268 v
->handler
= if_atom_handler
;
1270 } else if (!strcmp(name
, "then")) {
1271 v
->handler
= then_atom_handler
;
1273 } else if (!strcmp(name
, "else")) {
1274 v
->handler
= else_atom_handler
;
1279 formatp
= strchr(name
, ':');
1284 if (!strcmp(formatp
, "short"))
1285 refname
= shorten_unambiguous_ref(refname
,
1286 warn_ambiguous_refs
);
1287 else if (skip_prefix(formatp
, "strip=", &arg
))
1288 refname
= strip_ref_components(refname
, arg
);
1290 die(_("unknown %.*s format %s"),
1291 (int)(formatp
- name
), name
, formatp
);
1297 v
->s
= xstrfmt("%s^{}", refname
);
1300 for (i
= 0; i
< used_atom_cnt
; i
++) {
1301 struct atom_value
*v
= &ref
->value
[i
];
1308 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
1310 die(_("missing object %s for %s"),
1311 sha1_to_hex(ref
->objectname
), ref
->refname
);
1313 die(_("parse_object_buffer failed on %s for %s"),
1314 sha1_to_hex(ref
->objectname
), ref
->refname
);
1316 grab_values(ref
->value
, 0, obj
, buf
, size
);
1321 * If there is no atom that wants to know about tagged
1322 * object, we are done.
1324 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1328 * If it is a tag object, see if we use a value that derefs
1329 * the object, and if we do grab the object it refers to.
1331 tagged
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1334 * NEEDSWORK: This derefs tag only once, which
1335 * is good to deal with chains of trust, but
1336 * is not consistent with what deref_tag() does
1337 * which peels the onion to the core.
1339 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1341 die(_("missing object %s for %s"),
1342 sha1_to_hex(tagged
), ref
->refname
);
1344 die(_("parse_object_buffer failed on %s for %s"),
1345 sha1_to_hex(tagged
), ref
->refname
);
1346 grab_values(ref
->value
, 1, obj
, buf
, size
);
1352 * Given a ref, return the value for the atom. This lazily gets value
1353 * out of the object by calling populate value.
1355 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1358 populate_value(ref
);
1359 fill_missing_values(ref
->value
);
1361 *v
= &ref
->value
[atom
];
1364 enum contains_result
{
1365 CONTAINS_UNKNOWN
= -1,
1371 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1374 * At each recursion step, the stack items points to the commits whose
1375 * ancestors are to be inspected.
1377 struct contains_stack
{
1379 struct contains_stack_entry
{
1380 struct commit
*commit
;
1381 struct commit_list
*parents
;
1385 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1387 for (; want
; want
= want
->next
)
1388 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1394 * Test whether the candidate or one of its parents is contained in the list.
1395 * Do not recurse to find out, though, but return -1 if inconclusive.
1397 static enum contains_result
contains_test(struct commit
*candidate
,
1398 const struct commit_list
*want
)
1400 /* was it previously marked as containing a want commit? */
1401 if (candidate
->object
.flags
& TMP_MARK
)
1403 /* or marked as not possibly containing a want commit? */
1404 if (candidate
->object
.flags
& UNINTERESTING
)
1407 if (in_commit_list(want
, candidate
)) {
1408 candidate
->object
.flags
|= TMP_MARK
;
1412 if (parse_commit(candidate
) < 0)
1418 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1420 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1421 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1422 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1425 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1426 const struct commit_list
*want
)
1428 struct contains_stack contains_stack
= { 0, 0, NULL
};
1429 int result
= contains_test(candidate
, want
);
1431 if (result
!= CONTAINS_UNKNOWN
)
1434 push_to_contains_stack(candidate
, &contains_stack
);
1435 while (contains_stack
.nr
) {
1436 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1437 struct commit
*commit
= entry
->commit
;
1438 struct commit_list
*parents
= entry
->parents
;
1441 commit
->object
.flags
|= UNINTERESTING
;
1442 contains_stack
.nr
--;
1445 * If we just popped the stack, parents->item has been marked,
1446 * therefore contains_test will return a meaningful 0 or 1.
1448 else switch (contains_test(parents
->item
, want
)) {
1450 commit
->object
.flags
|= TMP_MARK
;
1451 contains_stack
.nr
--;
1454 entry
->parents
= parents
->next
;
1456 case CONTAINS_UNKNOWN
:
1457 push_to_contains_stack(parents
->item
, &contains_stack
);
1461 free(contains_stack
.contains_stack
);
1462 return contains_test(candidate
, want
);
1465 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1467 if (filter
->with_commit_tag_algo
)
1468 return contains_tag_algo(commit
, filter
->with_commit
);
1469 return is_descendant_of(commit
, filter
->with_commit
);
1473 * Return 1 if the refname matches one of the patterns, otherwise 0.
1474 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1475 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1476 * matches "refs/heads/mas*", too).
1478 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1480 const char **patterns
= filter
->name_patterns
;
1483 if (filter
->ignore_case
)
1484 flags
|= WM_CASEFOLD
;
1487 * When no '--format' option is given we need to skip the prefix
1488 * for matching refs of tags and branches.
1490 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1491 skip_prefix(refname
, "refs/heads/", &refname
) ||
1492 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1493 skip_prefix(refname
, "refs/", &refname
));
1495 for (; *patterns
; patterns
++) {
1496 if (!wildmatch(*patterns
, refname
, flags
, NULL
))
1503 * Return 1 if the refname matches one of the patterns, otherwise 0.
1504 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1505 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1506 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1508 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1510 const char **pattern
= filter
->name_patterns
;
1511 int namelen
= strlen(refname
);
1512 unsigned flags
= WM_PATHNAME
;
1514 if (filter
->ignore_case
)
1515 flags
|= WM_CASEFOLD
;
1517 for (; *pattern
; pattern
++) {
1518 const char *p
= *pattern
;
1519 int plen
= strlen(p
);
1521 if ((plen
<= namelen
) &&
1522 !strncmp(refname
, p
, plen
) &&
1523 (refname
[plen
] == '\0' ||
1524 refname
[plen
] == '/' ||
1527 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1533 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1534 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1536 if (!*filter
->name_patterns
)
1537 return 1; /* No pattern always matches */
1538 if (filter
->match_as_path
)
1539 return match_name_as_path(filter
, refname
);
1540 return match_pattern(filter
, refname
);
1544 * Given a ref (sha1, refname), check if the ref belongs to the array
1545 * of sha1s. If the given ref is a tag, check if the given tag points
1546 * at one of the sha1s in the given sha1 array.
1547 * the given sha1_array.
1549 * 1. Only a single level of inderection is obtained, we might want to
1550 * change this to account for multiple levels (e.g. annotated tags
1551 * pointing to annotated tags pointing to a commit.)
1552 * 2. As the refs are cached we might know what refname peels to without
1553 * the need to parse the object via parse_object(). peel_ref() might be a
1554 * more efficient alternative to obtain the pointee.
1556 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1557 const unsigned char *sha1
,
1558 const char *refname
)
1560 const unsigned char *tagged_sha1
= NULL
;
1563 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1565 obj
= parse_object(sha1
);
1567 die(_("malformed object at '%s'"), refname
);
1568 if (obj
->type
== OBJ_TAG
)
1569 tagged_sha1
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1570 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1575 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1576 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1577 const unsigned char *objectname
,
1580 struct ref_array_item
*ref
;
1581 FLEX_ALLOC_STR(ref
, refname
, refname
);
1582 hashcpy(ref
->objectname
, objectname
);
1588 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1596 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1597 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1598 { "refs/tags/", FILTER_REFS_TAGS
}
1601 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1602 filter
->kind
== FILTER_REFS_REMOTES
||
1603 filter
->kind
== FILTER_REFS_TAGS
)
1604 return filter
->kind
;
1605 else if (!strcmp(refname
, "HEAD"))
1606 return FILTER_REFS_DETACHED_HEAD
;
1608 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1609 if (starts_with(refname
, ref_kind
[i
].prefix
))
1610 return ref_kind
[i
].kind
;
1613 return FILTER_REFS_OTHERS
;
1617 * A call-back given to for_each_ref(). Filter refs and keep them for
1618 * later object processing.
1620 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1622 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1623 struct ref_filter
*filter
= ref_cbdata
->filter
;
1624 struct ref_array_item
*ref
;
1625 struct commit
*commit
= NULL
;
1628 if (flag
& REF_BAD_NAME
) {
1629 warning(_("ignoring ref with broken name %s"), refname
);
1633 if (flag
& REF_ISBROKEN
) {
1634 warning(_("ignoring broken ref %s"), refname
);
1638 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1639 kind
= filter_ref_kind(filter
, refname
);
1640 if (!(kind
& filter
->kind
))
1643 if (!filter_pattern_match(filter
, refname
))
1646 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1650 * A merge filter is applied on refs pointing to commits. Hence
1651 * obtain the commit using the 'oid' available and discard all
1652 * non-commits early. The actual filtering is done later.
1654 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->verbose
) {
1655 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1658 /* We perform the filtering for the '--contains' option */
1659 if (filter
->with_commit
&&
1660 !commit_contains(filter
, commit
))
1665 * We do not open the object yet; sort may only need refname
1666 * to do its job and the resulting list may yet to be pruned
1667 * by maxcount logic.
1669 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1670 ref
->commit
= commit
;
1672 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1673 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1678 /* Free memory allocated for a ref_array_item */
1679 static void free_array_item(struct ref_array_item
*item
)
1681 free((char *)item
->symref
);
1685 /* Free all memory allocated for ref_array */
1686 void ref_array_clear(struct ref_array
*array
)
1690 for (i
= 0; i
< array
->nr
; i
++)
1691 free_array_item(array
->items
[i
]);
1693 array
->items
= NULL
;
1694 array
->nr
= array
->alloc
= 0;
1697 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1699 struct rev_info revs
;
1701 struct ref_filter
*filter
= ref_cbdata
->filter
;
1702 struct ref_array
*array
= ref_cbdata
->array
;
1703 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1705 init_revisions(&revs
, NULL
);
1707 for (i
= 0; i
< array
->nr
; i
++) {
1708 struct ref_array_item
*item
= array
->items
[i
];
1709 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1710 to_clear
[i
] = item
->commit
;
1713 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1714 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1717 if (prepare_revision_walk(&revs
))
1718 die(_("revision walk setup failed"));
1723 for (i
= 0; i
< old_nr
; i
++) {
1724 struct ref_array_item
*item
= array
->items
[i
];
1725 struct commit
*commit
= item
->commit
;
1727 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1729 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1730 array
->items
[array
->nr
++] = array
->items
[i
];
1732 free_array_item(item
);
1735 for (i
= 0; i
< old_nr
; i
++)
1736 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1737 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1742 * API for filtering a set of refs. Based on the type of refs the user
1743 * has requested, we iterate through those refs and apply filters
1744 * as per the given ref_filter structure and finally store the
1745 * filtered refs in the ref_array structure.
1747 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1749 struct ref_filter_cbdata ref_cbdata
;
1751 unsigned int broken
= 0;
1753 ref_cbdata
.array
= array
;
1754 ref_cbdata
.filter
= filter
;
1756 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1758 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1760 /* Simple per-ref filtering */
1762 die("filter_refs: invalid type");
1765 * For common cases where we need only branches or remotes or tags,
1766 * we only iterate through those refs. If a mix of refs is needed,
1767 * we iterate over all refs and filter out required refs with the help
1768 * of filter_ref_kind().
1770 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1771 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1772 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1773 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1774 else if (filter
->kind
== FILTER_REFS_TAGS
)
1775 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1776 else if (filter
->kind
& FILTER_REFS_ALL
)
1777 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1778 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1779 head_ref(ref_filter_handler
, &ref_cbdata
);
1783 /* Filters that need revision walking */
1784 if (filter
->merge_commit
)
1785 do_merge_filter(&ref_cbdata
);
1790 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1792 struct atom_value
*va
, *vb
;
1794 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
1795 int (*cmp_fn
)(const char *, const char *);
1797 get_ref_atom_value(a
, s
->atom
, &va
);
1798 get_ref_atom_value(b
, s
->atom
, &vb
);
1799 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
1801 cmp
= versioncmp(va
->s
, vb
->s
);
1802 else if (cmp_type
== FIELD_STR
)
1803 cmp
= cmp_fn(va
->s
, vb
->s
);
1805 if (va
->ul
< vb
->ul
)
1807 else if (va
->ul
== vb
->ul
)
1808 cmp
= cmp_fn(a
->refname
, b
->refname
);
1813 return (s
->reverse
) ? -cmp
: cmp
;
1816 static struct ref_sorting
*ref_sorting
;
1817 static int compare_refs(const void *a_
, const void *b_
)
1819 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1820 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1821 struct ref_sorting
*s
;
1823 for (s
= ref_sorting
; s
; s
= s
->next
) {
1824 int cmp
= cmp_ref_sorting(s
, a
, b
);
1831 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1833 ref_sorting
= sorting
;
1834 QSORT(array
->items
, array
->nr
, compare_refs
);
1837 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1839 struct strbuf
*s
= &state
->stack
->output
;
1841 while (*cp
&& (!ep
|| cp
< ep
)) {
1846 int ch
= hex2chr(cp
+ 1);
1848 strbuf_addch(s
, ch
);
1854 strbuf_addch(s
, *cp
);
1859 void format_ref_array_item(struct ref_array_item
*info
, const char *format
,
1860 int quote_style
, struct strbuf
*final_buf
)
1862 const char *cp
, *sp
, *ep
;
1863 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1865 state
.quote_style
= quote_style
;
1866 push_stack_element(&state
.stack
);
1868 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1869 struct atom_value
*atomv
;
1871 ep
= strchr(sp
, ')');
1873 append_literal(cp
, sp
, &state
);
1874 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1875 atomv
->handler(atomv
, &state
);
1878 sp
= cp
+ strlen(cp
);
1879 append_literal(cp
, sp
, &state
);
1881 if (need_color_reset_at_eol
) {
1882 struct atom_value resetv
;
1883 char color
[COLOR_MAXLEN
] = "";
1885 if (color_parse("reset", color
) < 0)
1886 die("BUG: couldn't parse 'reset' as a color");
1888 append_atom(&resetv
, &state
);
1890 if (state
.stack
->prev
)
1891 die(_("format: %%(end) atom missing"));
1892 strbuf_addbuf(final_buf
, &state
.stack
->output
);
1893 pop_stack_element(&state
.stack
);
1896 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1898 struct strbuf final_buf
= STRBUF_INIT
;
1900 format_ref_array_item(info
, format
, quote_style
, &final_buf
);
1901 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
1902 strbuf_release(&final_buf
);
1906 /* If no sorting option is given, use refname to sort as default */
1907 struct ref_sorting
*ref_default_sorting(void)
1909 static const char cstr_name
[] = "refname";
1911 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1913 sorting
->next
= NULL
;
1914 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1918 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1920 struct ref_sorting
**sorting_tail
= opt
->value
;
1921 struct ref_sorting
*s
;
1924 if (!arg
) /* should --no-sort void the list ? */
1927 s
= xcalloc(1, sizeof(*s
));
1928 s
->next
= *sorting_tail
;
1935 if (skip_prefix(arg
, "version:", &arg
) ||
1936 skip_prefix(arg
, "v:", &arg
))
1939 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1943 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1945 struct ref_filter
*rf
= opt
->value
;
1946 unsigned char sha1
[20];
1948 rf
->merge
= starts_with(opt
->long_name
, "no")
1949 ? REF_FILTER_MERGED_OMIT
1950 : REF_FILTER_MERGED_INCLUDE
;
1952 if (get_sha1(arg
, sha1
))
1953 die(_("malformed object name %s"), arg
);
1955 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1956 if (!rf
->merge_commit
)
1957 return opterror(opt
, "must point to a commit", 0);