3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
17 #include "wt-status.h"
19 static struct ref_msg
{
23 const char *ahead_behind
;
25 /* Untranslated plumbing messages: */
32 void setup_ref_filter_porcelain_msg(void)
34 msgs
.gone
= _("gone");
35 msgs
.ahead
= _("ahead %d");
36 msgs
.behind
= _("behind %d");
37 msgs
.ahead_behind
= _("ahead %d, behind %d");
40 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
41 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
49 cmp_status cmp_status
;
51 unsigned int then_atom_seen
: 1,
53 condition_satisfied
: 1;
57 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
62 * An atom is a valid field atom listed below, possibly prefixed with
63 * a "*" to denote deref_tag().
65 * We parse given format string and sort specifiers, and make a list
66 * of properties that we need to extract out of objects. ref_array_item
67 * structure will hold an array of values extracted that can be
68 * indexed with the "atom number", which is an index into this
71 static struct used_atom
{
75 char color
[COLOR_MAXLEN
];
78 enum { RR_REF
, RR_TRACK
, RR_TRACKSHORT
} option
;
79 struct refname_atom refname
;
80 unsigned int nobracket
: 1;
83 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
87 cmp_status cmp_status
;
91 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
94 struct refname_atom refname
;
97 static int used_atom_cnt
, need_tagged
, need_symref
;
98 static int need_color_reset_at_eol
;
100 static void color_atom_parser(struct used_atom
*atom
, const char *color_value
)
103 die(_("expected format: %%(color:<color>)"));
104 if (color_parse(color_value
, atom
->u
.color
) < 0)
105 die(_("unrecognized color: %%(color:%s)"), color_value
);
108 static void refname_atom_parser_internal(struct refname_atom
*atom
,
109 const char *arg
, const char *name
)
112 atom
->option
= R_NORMAL
;
113 else if (!strcmp(arg
, "short"))
114 atom
->option
= R_SHORT
;
115 else if (skip_prefix(arg
, "lstrip=", &arg
)) {
116 atom
->option
= R_LSTRIP
;
117 if (strtol_i(arg
, 10, &atom
->lstrip
))
118 die(_("Integer value expected refname:lstrip=%s"), arg
);
119 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
120 atom
->option
= R_RSTRIP
;
121 if (strtol_i(arg
, 10, &atom
->rstrip
))
122 die(_("Integer value expected refname:rstrip=%s"), arg
);
124 die(_("unrecognized %%(%s) argument: %s"), name
, arg
);
127 static void remote_ref_atom_parser(struct used_atom
*atom
, const char *arg
)
129 struct string_list params
= STRING_LIST_INIT_DUP
;
133 atom
->u
.remote_ref
.option
= RR_REF
;
134 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
139 atom
->u
.remote_ref
.nobracket
= 0;
140 string_list_split(¶ms
, arg
, ',', -1);
142 for (i
= 0; i
< params
.nr
; i
++) {
143 const char *s
= params
.items
[i
].string
;
145 if (!strcmp(s
, "track"))
146 atom
->u
.remote_ref
.option
= RR_TRACK
;
147 else if (!strcmp(s
, "trackshort"))
148 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
149 else if (!strcmp(s
, "nobracket"))
150 atom
->u
.remote_ref
.nobracket
= 1;
152 atom
->u
.remote_ref
.option
= RR_REF
;
153 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
158 string_list_clear(¶ms
, 0);
161 static void body_atom_parser(struct used_atom
*atom
, const char *arg
)
164 die(_("%%(body) does not take arguments"));
165 atom
->u
.contents
.option
= C_BODY_DEP
;
168 static void subject_atom_parser(struct used_atom
*atom
, const char *arg
)
171 die(_("%%(subject) does not take arguments"));
172 atom
->u
.contents
.option
= C_SUB
;
175 static void trailers_atom_parser(struct used_atom
*atom
, const char *arg
)
178 die(_("%%(trailers) does not take arguments"));
179 atom
->u
.contents
.option
= C_TRAILERS
;
182 static void contents_atom_parser(struct used_atom
*atom
, const char *arg
)
185 atom
->u
.contents
.option
= C_BARE
;
186 else if (!strcmp(arg
, "body"))
187 atom
->u
.contents
.option
= C_BODY
;
188 else if (!strcmp(arg
, "signature"))
189 atom
->u
.contents
.option
= C_SIG
;
190 else if (!strcmp(arg
, "subject"))
191 atom
->u
.contents
.option
= C_SUB
;
192 else if (!strcmp(arg
, "trailers"))
193 atom
->u
.contents
.option
= C_TRAILERS
;
194 else if (skip_prefix(arg
, "lines=", &arg
)) {
195 atom
->u
.contents
.option
= C_LINES
;
196 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
197 die(_("positive value expected contents:lines=%s"), arg
);
199 die(_("unrecognized %%(contents) argument: %s"), arg
);
202 static void objectname_atom_parser(struct used_atom
*atom
, const char *arg
)
205 atom
->u
.objectname
.option
= O_FULL
;
206 else if (!strcmp(arg
, "short"))
207 atom
->u
.objectname
.option
= O_SHORT
;
208 else if (skip_prefix(arg
, "short=", &arg
)) {
209 atom
->u
.objectname
.option
= O_LENGTH
;
210 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
211 atom
->u
.objectname
.length
== 0)
212 die(_("positive value expected objectname:short=%s"), arg
);
213 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
214 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
216 die(_("unrecognized %%(objectname) argument: %s"), arg
);
219 static void refname_atom_parser(struct used_atom
*atom
, const char *arg
)
221 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
);
224 static align_type
parse_align_position(const char *s
)
226 if (!strcmp(s
, "right"))
228 else if (!strcmp(s
, "middle"))
230 else if (!strcmp(s
, "left"))
235 static void align_atom_parser(struct used_atom
*atom
, const char *arg
)
237 struct align
*align
= &atom
->u
.align
;
238 struct string_list params
= STRING_LIST_INIT_DUP
;
240 unsigned int width
= ~0U;
243 die(_("expected format: %%(align:<width>,<position>)"));
245 align
->position
= ALIGN_LEFT
;
247 string_list_split(¶ms
, arg
, ',', -1);
248 for (i
= 0; i
< params
.nr
; i
++) {
249 const char *s
= params
.items
[i
].string
;
252 if (skip_prefix(s
, "position=", &s
)) {
253 position
= parse_align_position(s
);
255 die(_("unrecognized position:%s"), s
);
256 align
->position
= position
;
257 } else if (skip_prefix(s
, "width=", &s
)) {
258 if (strtoul_ui(s
, 10, &width
))
259 die(_("unrecognized width:%s"), s
);
260 } else if (!strtoul_ui(s
, 10, &width
))
262 else if ((position
= parse_align_position(s
)) >= 0)
263 align
->position
= position
;
265 die(_("unrecognized %%(align) argument: %s"), s
);
269 die(_("positive width expected with the %%(align) atom"));
270 align
->width
= width
;
271 string_list_clear(¶ms
, 0);
274 static void if_atom_parser(struct used_atom
*atom
, const char *arg
)
277 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
279 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
280 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
281 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
282 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
284 die(_("unrecognized %%(if) argument: %s"), arg
);
292 void (*parser
)(struct used_atom
*atom
, const char *arg
);
294 { "refname" , FIELD_STR
, refname_atom_parser
},
296 { "objectsize", FIELD_ULONG
},
297 { "objectname", FIELD_STR
, objectname_atom_parser
},
300 { "numparent", FIELD_ULONG
},
307 { "authordate", FIELD_TIME
},
310 { "committeremail" },
311 { "committerdate", FIELD_TIME
},
315 { "taggerdate", FIELD_TIME
},
317 { "creatordate", FIELD_TIME
},
318 { "subject", FIELD_STR
, subject_atom_parser
},
319 { "body", FIELD_STR
, body_atom_parser
},
320 { "trailers", FIELD_STR
, trailers_atom_parser
},
321 { "contents", FIELD_STR
, contents_atom_parser
},
322 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
323 { "push", FIELD_STR
, remote_ref_atom_parser
},
324 { "symref", FIELD_STR
, refname_atom_parser
},
327 { "color", FIELD_STR
, color_atom_parser
},
328 { "align", FIELD_STR
, align_atom_parser
},
330 { "if", FIELD_STR
, if_atom_parser
},
335 #define REF_FORMATTING_STATE_INIT { 0, NULL }
337 struct ref_formatting_stack
{
338 struct ref_formatting_stack
*prev
;
339 struct strbuf output
;
340 void (*at_end
)(struct ref_formatting_stack
**stack
);
344 struct ref_formatting_state
{
346 struct ref_formatting_stack
*stack
;
351 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
352 unsigned long ul
; /* used for sorting when not FIELD_STR */
353 struct used_atom
*atom
;
357 * Used to parse format string and sort specifiers
359 int parse_ref_filter_atom(const char *atom
, const char *ep
)
366 if (*sp
== '*' && sp
< ep
)
369 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
371 /* Do we have the atom already used elsewhere? */
372 for (i
= 0; i
< used_atom_cnt
; i
++) {
373 int len
= strlen(used_atom
[i
].name
);
374 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
379 * If the atom name has a colon, strip it and everything after
380 * it off - it specifies the format for this entry, and
381 * shouldn't be used for checking against the valid_atom
384 arg
= memchr(sp
, ':', ep
- sp
);
385 atom_len
= (arg ? arg
: ep
) - sp
;
387 /* Is the atom a valid one? */
388 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
389 int len
= strlen(valid_atom
[i
].name
);
390 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
394 if (ARRAY_SIZE(valid_atom
) <= i
)
395 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
397 /* Add it in, including the deref prefix */
400 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
401 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
402 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
404 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
405 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
406 if (valid_atom
[i
].parser
)
407 valid_atom
[i
].parser(&used_atom
[at
], arg
);
410 if (!strcmp(valid_atom
[i
].name
, "symref"))
415 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
417 switch (quote_style
) {
419 strbuf_addstr(s
, str
);
422 sq_quote_buf(s
, str
);
425 perl_quote_buf(s
, str
);
428 python_quote_buf(s
, str
);
431 tcl_quote_buf(s
, str
);
436 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
439 * Quote formatting is only done when the stack has a single
440 * element. Otherwise quote formatting is done on the
441 * element's entire output strbuf when the %(end) atom is
444 if (!state
->stack
->prev
)
445 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
447 strbuf_addstr(&state
->stack
->output
, v
->s
);
450 static void push_stack_element(struct ref_formatting_stack
**stack
)
452 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
454 strbuf_init(&s
->output
, 0);
459 static void pop_stack_element(struct ref_formatting_stack
**stack
)
461 struct ref_formatting_stack
*current
= *stack
;
462 struct ref_formatting_stack
*prev
= current
->prev
;
465 strbuf_addbuf(&prev
->output
, ¤t
->output
);
466 strbuf_release(¤t
->output
);
471 static void end_align_handler(struct ref_formatting_stack
**stack
)
473 struct ref_formatting_stack
*cur
= *stack
;
474 struct align
*align
= (struct align
*)cur
->at_end_data
;
475 struct strbuf s
= STRBUF_INIT
;
477 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
478 strbuf_swap(&cur
->output
, &s
);
482 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
484 struct ref_formatting_stack
*new;
486 push_stack_element(&state
->stack
);
488 new->at_end
= end_align_handler
;
489 new->at_end_data
= &atomv
->atom
->u
.align
;
492 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
494 struct ref_formatting_stack
*cur
= *stack
;
495 struct ref_formatting_stack
*prev
= cur
->prev
;
496 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
498 if (!if_then_else
->then_atom_seen
)
499 die(_("format: %%(if) atom used without a %%(then) atom"));
501 if (if_then_else
->else_atom_seen
) {
503 * There is an %(else) atom: we need to drop one state from the
504 * stack, either the %(else) branch if the condition is satisfied, or
505 * the %(then) branch if it isn't.
507 if (if_then_else
->condition_satisfied
) {
508 strbuf_reset(&cur
->output
);
509 pop_stack_element(&cur
);
511 strbuf_swap(&cur
->output
, &prev
->output
);
512 strbuf_reset(&cur
->output
);
513 pop_stack_element(&cur
);
515 } else if (!if_then_else
->condition_satisfied
) {
517 * No %(else) atom: just drop the %(then) branch if the
518 * condition is not satisfied.
520 strbuf_reset(&cur
->output
);
527 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
529 struct ref_formatting_stack
*new;
530 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
532 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
533 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
535 push_stack_element(&state
->stack
);
537 new->at_end
= if_then_else_handler
;
538 new->at_end_data
= if_then_else
;
541 static int is_empty(const char *s
)
551 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
553 struct ref_formatting_stack
*cur
= state
->stack
;
554 struct if_then_else
*if_then_else
= NULL
;
556 if (cur
->at_end
== if_then_else_handler
)
557 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
559 die(_("format: %%(then) atom used without an %%(if) atom"));
560 if (if_then_else
->then_atom_seen
)
561 die(_("format: %%(then) atom used more than once"));
562 if (if_then_else
->else_atom_seen
)
563 die(_("format: %%(then) atom used after %%(else)"));
564 if_then_else
->then_atom_seen
= 1;
566 * If the 'equals' or 'notequals' attribute is used then
567 * perform the required comparison. If not, only non-empty
568 * strings satisfy the 'if' condition.
570 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
571 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
572 if_then_else
->condition_satisfied
= 1;
573 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
574 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
575 if_then_else
->condition_satisfied
= 1;
576 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
577 if_then_else
->condition_satisfied
= 1;
578 strbuf_reset(&cur
->output
);
581 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
583 struct ref_formatting_stack
*prev
= state
->stack
;
584 struct if_then_else
*if_then_else
= NULL
;
586 if (prev
->at_end
== if_then_else_handler
)
587 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
589 die(_("format: %%(else) atom used without an %%(if) atom"));
590 if (!if_then_else
->then_atom_seen
)
591 die(_("format: %%(else) atom used without a %%(then) atom"));
592 if (if_then_else
->else_atom_seen
)
593 die(_("format: %%(else) atom used more than once"));
594 if_then_else
->else_atom_seen
= 1;
595 push_stack_element(&state
->stack
);
596 state
->stack
->at_end_data
= prev
->at_end_data
;
597 state
->stack
->at_end
= prev
->at_end
;
600 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
602 struct ref_formatting_stack
*current
= state
->stack
;
603 struct strbuf s
= STRBUF_INIT
;
605 if (!current
->at_end
)
606 die(_("format: %%(end) atom used without corresponding atom"));
607 current
->at_end(&state
->stack
);
609 /* Stack may have been popped within at_end(), hence reset the current pointer */
610 current
= state
->stack
;
613 * Perform quote formatting when the stack element is that of
614 * a supporting atom. If nested then perform quote formatting
615 * only on the topmost supporting atom.
617 if (!current
->prev
->prev
) {
618 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
619 strbuf_swap(¤t
->output
, &s
);
622 pop_stack_element(&state
->stack
);
626 * In a format string, find the next occurrence of %(atom).
628 static const char *find_next(const char *cp
)
633 * %( is the start of an atom;
634 * %% is a quoted per-cent.
638 else if (cp
[1] == '%')
639 cp
++; /* skip over two % */
640 /* otherwise this is a singleton, literal % */
648 * Make sure the format string is well formed, and parse out
651 int verify_ref_format(const char *format
)
655 need_color_reset_at_eol
= 0;
656 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
657 const char *color
, *ep
= strchr(sp
, ')');
661 return error(_("malformed format string %s"), sp
);
662 /* sp points at "%(" and ep points at the closing ")" */
663 at
= parse_ref_filter_atom(sp
+ 2, ep
);
666 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
667 need_color_reset_at_eol
= !!strcmp(color
, "reset");
673 * Given an object name, read the object data and size, and return a
674 * "struct object". If the object data we are returning is also borrowed
675 * by the "struct object" representation, set *eaten as well---it is a
676 * signal from parse_object_buffer to us not to free the buffer.
678 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
680 enum object_type type
;
681 void *buf
= read_sha1_file(sha1
, &type
, sz
);
684 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
690 static int grab_objectname(const char *name
, const unsigned char *sha1
,
691 struct atom_value
*v
, struct used_atom
*atom
)
693 if (starts_with(name
, "objectname")) {
694 if (atom
->u
.objectname
.option
== O_SHORT
) {
695 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
697 } else if (atom
->u
.objectname
.option
== O_FULL
) {
698 v
->s
= xstrdup(sha1_to_hex(sha1
));
700 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
701 v
->s
= xstrdup(find_unique_abbrev(sha1
, atom
->u
.objectname
.length
));
704 die("BUG: unknown %%(objectname) option");
709 /* See grab_values */
710 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
714 for (i
= 0; i
< used_atom_cnt
; i
++) {
715 const char *name
= used_atom
[i
].name
;
716 struct atom_value
*v
= &val
[i
];
717 if (!!deref
!= (*name
== '*'))
721 if (!strcmp(name
, "objecttype"))
722 v
->s
= typename(obj
->type
);
723 else if (!strcmp(name
, "objectsize")) {
725 v
->s
= xstrfmt("%lu", sz
);
728 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
732 /* See grab_values */
733 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
736 struct tag
*tag
= (struct tag
*) obj
;
738 for (i
= 0; i
< used_atom_cnt
; i
++) {
739 const char *name
= used_atom
[i
].name
;
740 struct atom_value
*v
= &val
[i
];
741 if (!!deref
!= (*name
== '*'))
745 if (!strcmp(name
, "tag"))
747 else if (!strcmp(name
, "type") && tag
->tagged
)
748 v
->s
= typename(tag
->tagged
->type
);
749 else if (!strcmp(name
, "object") && tag
->tagged
)
750 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
754 /* See grab_values */
755 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
758 struct commit
*commit
= (struct commit
*) obj
;
760 for (i
= 0; i
< used_atom_cnt
; i
++) {
761 const char *name
= used_atom
[i
].name
;
762 struct atom_value
*v
= &val
[i
];
763 if (!!deref
!= (*name
== '*'))
767 if (!strcmp(name
, "tree")) {
768 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
770 else if (!strcmp(name
, "numparent")) {
771 v
->ul
= commit_list_count(commit
->parents
);
772 v
->s
= xstrfmt("%lu", v
->ul
);
774 else if (!strcmp(name
, "parent")) {
775 struct commit_list
*parents
;
776 struct strbuf s
= STRBUF_INIT
;
777 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
778 struct commit
*parent
= parents
->item
;
779 if (parents
!= commit
->parents
)
780 strbuf_addch(&s
, ' ');
781 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
783 v
->s
= strbuf_detach(&s
, NULL
);
788 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
792 if (!strncmp(buf
, who
, wholen
) &&
794 return buf
+ wholen
+ 1;
795 eol
= strchr(buf
, '\n');
800 return ""; /* end of header */
806 static const char *copy_line(const char *buf
)
808 const char *eol
= strchrnul(buf
, '\n');
809 return xmemdupz(buf
, eol
- buf
);
812 static const char *copy_name(const char *buf
)
815 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
816 if (!strncmp(cp
, " <", 2))
817 return xmemdupz(buf
, cp
- buf
);
822 static const char *copy_email(const char *buf
)
824 const char *email
= strchr(buf
, '<');
828 eoemail
= strchr(email
, '>');
831 return xmemdupz(email
, eoemail
+ 1 - email
);
834 static char *copy_subject(const char *buf
, unsigned long len
)
836 char *r
= xmemdupz(buf
, len
);
839 for (i
= 0; i
< len
; i
++)
846 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
848 const char *eoemail
= strstr(buf
, "> ");
850 unsigned long timestamp
;
852 struct date_mode date_mode
= { DATE_NORMAL
};
856 * We got here because atomname ends in "date" or "date<something>";
857 * it's not possible that <something> is not ":<format>" because
858 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
859 * ":" means no format is specified, and use the default.
861 formatp
= strchr(atomname
, ':');
862 if (formatp
!= NULL
) {
864 parse_date_format(formatp
, &date_mode
);
869 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
870 if (timestamp
== ULONG_MAX
)
872 tz
= strtol(zone
, NULL
, 10);
873 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
875 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
883 /* See grab_values */
884 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
887 int wholen
= strlen(who
);
888 const char *wholine
= NULL
;
890 for (i
= 0; i
< used_atom_cnt
; i
++) {
891 const char *name
= used_atom
[i
].name
;
892 struct atom_value
*v
= &val
[i
];
893 if (!!deref
!= (*name
== '*'))
897 if (strncmp(who
, name
, wholen
))
899 if (name
[wholen
] != 0 &&
900 strcmp(name
+ wholen
, "name") &&
901 strcmp(name
+ wholen
, "email") &&
902 !starts_with(name
+ wholen
, "date"))
905 wholine
= find_wholine(who
, wholen
, buf
, sz
);
907 return; /* no point looking for it */
908 if (name
[wholen
] == 0)
909 v
->s
= copy_line(wholine
);
910 else if (!strcmp(name
+ wholen
, "name"))
911 v
->s
= copy_name(wholine
);
912 else if (!strcmp(name
+ wholen
, "email"))
913 v
->s
= copy_email(wholine
);
914 else if (starts_with(name
+ wholen
, "date"))
915 grab_date(wholine
, v
, name
);
919 * For a tag or a commit object, if "creator" or "creatordate" is
920 * requested, do something special.
922 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
923 return; /* "author" for commit object is not wanted */
925 wholine
= find_wholine(who
, wholen
, buf
, sz
);
928 for (i
= 0; i
< used_atom_cnt
; i
++) {
929 const char *name
= used_atom
[i
].name
;
930 struct atom_value
*v
= &val
[i
];
931 if (!!deref
!= (*name
== '*'))
936 if (starts_with(name
, "creatordate"))
937 grab_date(wholine
, v
, name
);
938 else if (!strcmp(name
, "creator"))
939 v
->s
= copy_line(wholine
);
943 static void find_subpos(const char *buf
, unsigned long sz
,
944 const char **sub
, unsigned long *sublen
,
945 const char **body
, unsigned long *bodylen
,
946 unsigned long *nonsiglen
,
947 const char **sig
, unsigned long *siglen
)
950 /* skip past header until we hit empty line */
951 while (*buf
&& *buf
!= '\n') {
952 eol
= strchrnul(buf
, '\n');
957 /* skip any empty lines */
961 /* parse signature first; we might not even have a subject line */
962 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
963 *siglen
= strlen(*sig
);
965 /* subject is first non-empty line */
967 /* subject goes to first empty line */
968 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
969 eol
= strchrnul(buf
, '\n');
974 *sublen
= buf
- *sub
;
975 /* drop trailing newline, if present */
976 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
979 /* skip any empty lines */
983 *bodylen
= strlen(buf
);
984 *nonsiglen
= *sig
- buf
;
988 * If 'lines' is greater than 0, append that many lines from the given
989 * 'buf' of length 'size' to the given strbuf.
991 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
994 const char *sp
, *eol
;
999 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1001 strbuf_addstr(out
, "\n ");
1002 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1003 len
= eol ? eol
- sp
: size
- (sp
- buf
);
1004 strbuf_add(out
, sp
, len
);
1011 /* See grab_values */
1012 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1015 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1016 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1018 for (i
= 0; i
< used_atom_cnt
; i
++) {
1019 struct used_atom
*atom
= &used_atom
[i
];
1020 const char *name
= atom
->name
;
1021 struct atom_value
*v
= &val
[i
];
1022 if (!!deref
!= (*name
== '*'))
1026 if (strcmp(name
, "subject") &&
1027 strcmp(name
, "body") &&
1028 strcmp(name
, "trailers") &&
1029 !starts_with(name
, "contents"))
1032 find_subpos(buf
, sz
,
1034 &bodypos
, &bodylen
, &nonsiglen
,
1037 if (atom
->u
.contents
.option
== C_SUB
)
1038 v
->s
= copy_subject(subpos
, sublen
);
1039 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1040 v
->s
= xmemdupz(bodypos
, bodylen
);
1041 else if (atom
->u
.contents
.option
== C_BODY
)
1042 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1043 else if (atom
->u
.contents
.option
== C_SIG
)
1044 v
->s
= xmemdupz(sigpos
, siglen
);
1045 else if (atom
->u
.contents
.option
== C_LINES
) {
1046 struct strbuf s
= STRBUF_INIT
;
1047 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1049 /* Size is the length of the message after removing the signature */
1050 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1051 v
->s
= strbuf_detach(&s
, NULL
);
1052 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1053 struct trailer_info info
;
1055 /* Search for trailer info */
1056 trailer_info_get(&info
, subpos
);
1057 v
->s
= xmemdupz(info
.trailer_start
,
1058 info
.trailer_end
- info
.trailer_start
);
1059 trailer_info_release(&info
);
1060 } else if (atom
->u
.contents
.option
== C_BARE
)
1061 v
->s
= xstrdup(subpos
);
1066 * We want to have empty print-string for field requests
1067 * that do not apply (e.g. "authordate" for a tag object)
1069 static void fill_missing_values(struct atom_value
*val
)
1072 for (i
= 0; i
< used_atom_cnt
; i
++) {
1073 struct atom_value
*v
= &val
[i
];
1080 * val is a list of atom_value to hold returned values. Extract
1081 * the values for atoms in used_atom array out of (obj, buf, sz).
1082 * when deref is false, (obj, buf, sz) is the object that is
1083 * pointed at by the ref itself; otherwise it is the object the
1084 * ref (which is a tag) refers to.
1086 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1088 grab_common_values(val
, deref
, obj
, buf
, sz
);
1089 switch (obj
->type
) {
1091 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1092 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1093 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1096 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1097 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1098 grab_person("author", val
, deref
, obj
, buf
, sz
);
1099 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1102 /* grab_tree_values(val, deref, obj, buf, sz); */
1105 /* grab_blob_values(val, deref, obj, buf, sz); */
1108 die("Eh? Object of type %d?", obj
->type
);
1112 static inline char *copy_advance(char *dst
, const char *src
)
1119 static const char *lstrip_ref_components(const char *refname
, int len
)
1121 long remaining
= len
;
1122 const char *start
= refname
;
1126 const char *p
= refname
;
1128 /* Find total no of '/' separated path-components */
1129 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1132 * The number of components we need to strip is now
1133 * the total minus the components to be left (Plus one
1134 * because we count the number of '/', but the number
1135 * of components is one more than the no of '/').
1137 remaining
= i
+ len
+ 1;
1140 while (remaining
> 0) {
1153 static const char *rstrip_ref_components(const char *refname
, int len
)
1155 long remaining
= len
;
1156 char *start
= xstrdup(refname
);
1160 const char *p
= refname
;
1162 /* Find total no of '/' separated path-components */
1163 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1166 * The number of components we need to strip is now
1167 * the total minus the components to be left (Plus one
1168 * because we count the number of '/', but the number
1169 * of components is one more than the no of '/').
1171 remaining
= i
+ len
+ 1;
1174 while (remaining
-- > 0) {
1175 char *p
= strrchr(start
, '/');
1184 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1186 if (atom
->option
== R_SHORT
)
1187 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1188 else if (atom
->option
== R_LSTRIP
)
1189 return lstrip_ref_components(refname
, atom
->lstrip
);
1190 else if (atom
->option
== R_RSTRIP
)
1191 return rstrip_ref_components(refname
, atom
->rstrip
);
1196 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1197 struct branch
*branch
, const char **s
)
1199 int num_ours
, num_theirs
;
1200 if (atom
->u
.remote_ref
.option
== RR_REF
)
1201 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1202 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1203 if (stat_tracking_info(branch
, &num_ours
,
1204 &num_theirs
, NULL
)) {
1205 *s
= xstrdup(msgs
.gone
);
1206 } else if (!num_ours
&& !num_theirs
)
1209 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1210 else if (!num_theirs
)
1211 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1213 *s
= xstrfmt(msgs
.ahead_behind
,
1214 num_ours
, num_theirs
);
1215 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1216 const char *to_free
= *s
;
1217 *s
= xstrfmt("[%s]", *s
);
1218 free((void *)to_free
);
1220 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1221 if (stat_tracking_info(branch
, &num_ours
,
1225 if (!num_ours
&& !num_theirs
)
1229 else if (!num_theirs
)
1234 die("BUG: unhandled RR_* enum");
1237 char *get_head_description(void)
1239 struct strbuf desc
= STRBUF_INIT
;
1240 struct wt_status_state state
;
1241 memset(&state
, 0, sizeof(state
));
1242 wt_status_get_state(&state
, 1);
1243 if (state
.rebase_in_progress
||
1244 state
.rebase_interactive_in_progress
)
1245 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1247 else if (state
.bisect_in_progress
)
1248 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1250 else if (state
.detached_from
) {
1251 /* TRANSLATORS: make sure these match _("HEAD detached at ")
1252 and _("HEAD detached from ") in wt-status.c */
1253 if (state
.detached_at
)
1254 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1255 state
.detached_from
);
1257 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1258 state
.detached_from
);
1261 strbuf_addstr(&desc
, _("(no branch)"));
1264 free(state
.detached_from
);
1265 return strbuf_detach(&desc
, NULL
);
1268 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1273 return show_ref(&atom
->u
.refname
, ref
->symref
);
1276 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1278 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1279 return get_head_description();
1280 return show_ref(&atom
->u
.refname
, ref
->refname
);
1284 * Parse the object referred by ref, and grab needed value.
1286 static void populate_value(struct ref_array_item
*ref
)
1292 const unsigned char *tagged
;
1294 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1296 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1297 unsigned char unused1
[20];
1298 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1304 /* Fill in specials first */
1305 for (i
= 0; i
< used_atom_cnt
; i
++) {
1306 struct used_atom
*atom
= &used_atom
[i
];
1307 const char *name
= used_atom
[i
].name
;
1308 struct atom_value
*v
= &ref
->value
[i
];
1310 const char *refname
;
1311 struct branch
*branch
= NULL
;
1313 v
->handler
= append_atom
;
1321 if (starts_with(name
, "refname"))
1322 refname
= get_refname(atom
, ref
);
1323 else if (starts_with(name
, "symref"))
1324 refname
= get_symref(atom
, ref
);
1325 else if (starts_with(name
, "upstream")) {
1326 const char *branch_name
;
1327 /* only local branches may have an upstream */
1328 if (!skip_prefix(ref
->refname
, "refs/heads/",
1331 branch
= branch_get(branch_name
);
1333 refname
= branch_get_upstream(branch
, NULL
);
1335 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1337 } else if (starts_with(name
, "push")) {
1338 const char *branch_name
;
1339 if (!skip_prefix(ref
->refname
, "refs/heads/",
1342 branch
= branch_get(branch_name
);
1344 refname
= branch_get_push(branch
, NULL
);
1347 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1349 } else if (starts_with(name
, "color:")) {
1350 v
->s
= atom
->u
.color
;
1352 } else if (!strcmp(name
, "flag")) {
1353 char buf
[256], *cp
= buf
;
1354 if (ref
->flag
& REF_ISSYMREF
)
1355 cp
= copy_advance(cp
, ",symref");
1356 if (ref
->flag
& REF_ISPACKED
)
1357 cp
= copy_advance(cp
, ",packed");
1362 v
->s
= xstrdup(buf
+ 1);
1365 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
, atom
)) {
1367 } else if (!strcmp(name
, "HEAD")) {
1369 unsigned char sha1
[20];
1371 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1373 if (head
&& !strcmp(ref
->refname
, head
))
1378 } else if (starts_with(name
, "align")) {
1379 v
->handler
= align_atom_handler
;
1381 } else if (!strcmp(name
, "end")) {
1382 v
->handler
= end_atom_handler
;
1384 } else if (starts_with(name
, "if")) {
1387 if (skip_prefix(name
, "if:", &s
))
1389 v
->handler
= if_atom_handler
;
1391 } else if (!strcmp(name
, "then")) {
1392 v
->handler
= then_atom_handler
;
1394 } else if (!strcmp(name
, "else")) {
1395 v
->handler
= else_atom_handler
;
1403 v
->s
= xstrfmt("%s^{}", refname
);
1406 for (i
= 0; i
< used_atom_cnt
; i
++) {
1407 struct atom_value
*v
= &ref
->value
[i
];
1414 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
1416 die(_("missing object %s for %s"),
1417 sha1_to_hex(ref
->objectname
), ref
->refname
);
1419 die(_("parse_object_buffer failed on %s for %s"),
1420 sha1_to_hex(ref
->objectname
), ref
->refname
);
1422 grab_values(ref
->value
, 0, obj
, buf
, size
);
1427 * If there is no atom that wants to know about tagged
1428 * object, we are done.
1430 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1434 * If it is a tag object, see if we use a value that derefs
1435 * the object, and if we do grab the object it refers to.
1437 tagged
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1440 * NEEDSWORK: This derefs tag only once, which
1441 * is good to deal with chains of trust, but
1442 * is not consistent with what deref_tag() does
1443 * which peels the onion to the core.
1445 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1447 die(_("missing object %s for %s"),
1448 sha1_to_hex(tagged
), ref
->refname
);
1450 die(_("parse_object_buffer failed on %s for %s"),
1451 sha1_to_hex(tagged
), ref
->refname
);
1452 grab_values(ref
->value
, 1, obj
, buf
, size
);
1458 * Given a ref, return the value for the atom. This lazily gets value
1459 * out of the object by calling populate value.
1461 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1464 populate_value(ref
);
1465 fill_missing_values(ref
->value
);
1467 *v
= &ref
->value
[atom
];
1470 enum contains_result
{
1471 CONTAINS_UNKNOWN
= -1,
1477 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1480 * At each recursion step, the stack items points to the commits whose
1481 * ancestors are to be inspected.
1483 struct contains_stack
{
1485 struct contains_stack_entry
{
1486 struct commit
*commit
;
1487 struct commit_list
*parents
;
1491 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1493 for (; want
; want
= want
->next
)
1494 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1500 * Test whether the candidate or one of its parents is contained in the list.
1501 * Do not recurse to find out, though, but return -1 if inconclusive.
1503 static enum contains_result
contains_test(struct commit
*candidate
,
1504 const struct commit_list
*want
)
1506 /* was it previously marked as containing a want commit? */
1507 if (candidate
->object
.flags
& TMP_MARK
)
1509 /* or marked as not possibly containing a want commit? */
1510 if (candidate
->object
.flags
& UNINTERESTING
)
1513 if (in_commit_list(want
, candidate
)) {
1514 candidate
->object
.flags
|= TMP_MARK
;
1518 if (parse_commit(candidate
) < 0)
1524 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1526 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1527 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1528 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1531 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1532 const struct commit_list
*want
)
1534 struct contains_stack contains_stack
= { 0, 0, NULL
};
1535 int result
= contains_test(candidate
, want
);
1537 if (result
!= CONTAINS_UNKNOWN
)
1540 push_to_contains_stack(candidate
, &contains_stack
);
1541 while (contains_stack
.nr
) {
1542 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1543 struct commit
*commit
= entry
->commit
;
1544 struct commit_list
*parents
= entry
->parents
;
1547 commit
->object
.flags
|= UNINTERESTING
;
1548 contains_stack
.nr
--;
1551 * If we just popped the stack, parents->item has been marked,
1552 * therefore contains_test will return a meaningful 0 or 1.
1554 else switch (contains_test(parents
->item
, want
)) {
1556 commit
->object
.flags
|= TMP_MARK
;
1557 contains_stack
.nr
--;
1560 entry
->parents
= parents
->next
;
1562 case CONTAINS_UNKNOWN
:
1563 push_to_contains_stack(parents
->item
, &contains_stack
);
1567 free(contains_stack
.contains_stack
);
1568 return contains_test(candidate
, want
);
1571 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1573 if (filter
->with_commit_tag_algo
)
1574 return contains_tag_algo(commit
, filter
->with_commit
);
1575 return is_descendant_of(commit
, filter
->with_commit
);
1579 * Return 1 if the refname matches one of the patterns, otherwise 0.
1580 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1581 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1582 * matches "refs/heads/mas*", too).
1584 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1586 const char **patterns
= filter
->name_patterns
;
1589 if (filter
->ignore_case
)
1590 flags
|= WM_CASEFOLD
;
1593 * When no '--format' option is given we need to skip the prefix
1594 * for matching refs of tags and branches.
1596 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1597 skip_prefix(refname
, "refs/heads/", &refname
) ||
1598 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1599 skip_prefix(refname
, "refs/", &refname
));
1601 for (; *patterns
; patterns
++) {
1602 if (!wildmatch(*patterns
, refname
, flags
, NULL
))
1609 * Return 1 if the refname matches one of the patterns, otherwise 0.
1610 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1611 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1612 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1614 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1616 const char **pattern
= filter
->name_patterns
;
1617 int namelen
= strlen(refname
);
1618 unsigned flags
= WM_PATHNAME
;
1620 if (filter
->ignore_case
)
1621 flags
|= WM_CASEFOLD
;
1623 for (; *pattern
; pattern
++) {
1624 const char *p
= *pattern
;
1625 int plen
= strlen(p
);
1627 if ((plen
<= namelen
) &&
1628 !strncmp(refname
, p
, plen
) &&
1629 (refname
[plen
] == '\0' ||
1630 refname
[plen
] == '/' ||
1633 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1639 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1640 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1642 if (!*filter
->name_patterns
)
1643 return 1; /* No pattern always matches */
1644 if (filter
->match_as_path
)
1645 return match_name_as_path(filter
, refname
);
1646 return match_pattern(filter
, refname
);
1650 * Given a ref (sha1, refname), check if the ref belongs to the array
1651 * of sha1s. If the given ref is a tag, check if the given tag points
1652 * at one of the sha1s in the given sha1 array.
1653 * the given sha1_array.
1655 * 1. Only a single level of inderection is obtained, we might want to
1656 * change this to account for multiple levels (e.g. annotated tags
1657 * pointing to annotated tags pointing to a commit.)
1658 * 2. As the refs are cached we might know what refname peels to without
1659 * the need to parse the object via parse_object(). peel_ref() might be a
1660 * more efficient alternative to obtain the pointee.
1662 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1663 const unsigned char *sha1
,
1664 const char *refname
)
1666 const unsigned char *tagged_sha1
= NULL
;
1669 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1671 obj
= parse_object(sha1
);
1673 die(_("malformed object at '%s'"), refname
);
1674 if (obj
->type
== OBJ_TAG
)
1675 tagged_sha1
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1676 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1681 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1682 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1683 const unsigned char *objectname
,
1686 struct ref_array_item
*ref
;
1687 FLEX_ALLOC_STR(ref
, refname
, refname
);
1688 hashcpy(ref
->objectname
, objectname
);
1694 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1702 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1703 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1704 { "refs/tags/", FILTER_REFS_TAGS
}
1707 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1708 filter
->kind
== FILTER_REFS_REMOTES
||
1709 filter
->kind
== FILTER_REFS_TAGS
)
1710 return filter
->kind
;
1711 else if (!strcmp(refname
, "HEAD"))
1712 return FILTER_REFS_DETACHED_HEAD
;
1714 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1715 if (starts_with(refname
, ref_kind
[i
].prefix
))
1716 return ref_kind
[i
].kind
;
1719 return FILTER_REFS_OTHERS
;
1723 * A call-back given to for_each_ref(). Filter refs and keep them for
1724 * later object processing.
1726 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1728 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1729 struct ref_filter
*filter
= ref_cbdata
->filter
;
1730 struct ref_array_item
*ref
;
1731 struct commit
*commit
= NULL
;
1734 if (flag
& REF_BAD_NAME
) {
1735 warning(_("ignoring ref with broken name %s"), refname
);
1739 if (flag
& REF_ISBROKEN
) {
1740 warning(_("ignoring broken ref %s"), refname
);
1744 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1745 kind
= filter_ref_kind(filter
, refname
);
1746 if (!(kind
& filter
->kind
))
1749 if (!filter_pattern_match(filter
, refname
))
1752 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1756 * A merge filter is applied on refs pointing to commits. Hence
1757 * obtain the commit using the 'oid' available and discard all
1758 * non-commits early. The actual filtering is done later.
1760 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->verbose
) {
1761 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1764 /* We perform the filtering for the '--contains' option */
1765 if (filter
->with_commit
&&
1766 !commit_contains(filter
, commit
))
1771 * We do not open the object yet; sort may only need refname
1772 * to do its job and the resulting list may yet to be pruned
1773 * by maxcount logic.
1775 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1776 ref
->commit
= commit
;
1778 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1779 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1784 /* Free memory allocated for a ref_array_item */
1785 static void free_array_item(struct ref_array_item
*item
)
1787 free((char *)item
->symref
);
1791 /* Free all memory allocated for ref_array */
1792 void ref_array_clear(struct ref_array
*array
)
1796 for (i
= 0; i
< array
->nr
; i
++)
1797 free_array_item(array
->items
[i
]);
1799 array
->items
= NULL
;
1800 array
->nr
= array
->alloc
= 0;
1803 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1805 struct rev_info revs
;
1807 struct ref_filter
*filter
= ref_cbdata
->filter
;
1808 struct ref_array
*array
= ref_cbdata
->array
;
1809 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1811 init_revisions(&revs
, NULL
);
1813 for (i
= 0; i
< array
->nr
; i
++) {
1814 struct ref_array_item
*item
= array
->items
[i
];
1815 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1816 to_clear
[i
] = item
->commit
;
1819 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1820 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1823 if (prepare_revision_walk(&revs
))
1824 die(_("revision walk setup failed"));
1829 for (i
= 0; i
< old_nr
; i
++) {
1830 struct ref_array_item
*item
= array
->items
[i
];
1831 struct commit
*commit
= item
->commit
;
1833 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1835 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1836 array
->items
[array
->nr
++] = array
->items
[i
];
1838 free_array_item(item
);
1841 for (i
= 0; i
< old_nr
; i
++)
1842 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1843 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1848 * API for filtering a set of refs. Based on the type of refs the user
1849 * has requested, we iterate through those refs and apply filters
1850 * as per the given ref_filter structure and finally store the
1851 * filtered refs in the ref_array structure.
1853 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1855 struct ref_filter_cbdata ref_cbdata
;
1857 unsigned int broken
= 0;
1859 ref_cbdata
.array
= array
;
1860 ref_cbdata
.filter
= filter
;
1862 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1864 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1866 /* Simple per-ref filtering */
1868 die("filter_refs: invalid type");
1871 * For common cases where we need only branches or remotes or tags,
1872 * we only iterate through those refs. If a mix of refs is needed,
1873 * we iterate over all refs and filter out required refs with the help
1874 * of filter_ref_kind().
1876 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1877 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1878 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1879 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1880 else if (filter
->kind
== FILTER_REFS_TAGS
)
1881 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1882 else if (filter
->kind
& FILTER_REFS_ALL
)
1883 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1884 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1885 head_ref(ref_filter_handler
, &ref_cbdata
);
1889 /* Filters that need revision walking */
1890 if (filter
->merge_commit
)
1891 do_merge_filter(&ref_cbdata
);
1896 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1898 struct atom_value
*va
, *vb
;
1900 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
1901 int (*cmp_fn
)(const char *, const char *);
1903 get_ref_atom_value(a
, s
->atom
, &va
);
1904 get_ref_atom_value(b
, s
->atom
, &vb
);
1905 cmp_fn
= s
->ignore_case ? strcasecmp
: strcmp
;
1907 cmp
= versioncmp(va
->s
, vb
->s
);
1908 else if (cmp_type
== FIELD_STR
)
1909 cmp
= cmp_fn(va
->s
, vb
->s
);
1911 if (va
->ul
< vb
->ul
)
1913 else if (va
->ul
== vb
->ul
)
1914 cmp
= cmp_fn(a
->refname
, b
->refname
);
1919 return (s
->reverse
) ?
-cmp
: cmp
;
1922 static struct ref_sorting
*ref_sorting
;
1923 static int compare_refs(const void *a_
, const void *b_
)
1925 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1926 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1927 struct ref_sorting
*s
;
1929 for (s
= ref_sorting
; s
; s
= s
->next
) {
1930 int cmp
= cmp_ref_sorting(s
, a
, b
);
1937 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1939 ref_sorting
= sorting
;
1940 QSORT(array
->items
, array
->nr
, compare_refs
);
1943 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1945 struct strbuf
*s
= &state
->stack
->output
;
1947 while (*cp
&& (!ep
|| cp
< ep
)) {
1952 int ch
= hex2chr(cp
+ 1);
1954 strbuf_addch(s
, ch
);
1960 strbuf_addch(s
, *cp
);
1965 void format_ref_array_item(struct ref_array_item
*info
, const char *format
,
1966 int quote_style
, struct strbuf
*final_buf
)
1968 const char *cp
, *sp
, *ep
;
1969 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1971 state
.quote_style
= quote_style
;
1972 push_stack_element(&state
.stack
);
1974 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1975 struct atom_value
*atomv
;
1977 ep
= strchr(sp
, ')');
1979 append_literal(cp
, sp
, &state
);
1980 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1981 atomv
->handler(atomv
, &state
);
1984 sp
= cp
+ strlen(cp
);
1985 append_literal(cp
, sp
, &state
);
1987 if (need_color_reset_at_eol
) {
1988 struct atom_value resetv
;
1989 char color
[COLOR_MAXLEN
] = "";
1991 if (color_parse("reset", color
) < 0)
1992 die("BUG: couldn't parse 'reset' as a color");
1994 append_atom(&resetv
, &state
);
1996 if (state
.stack
->prev
)
1997 die(_("format: %%(end) atom missing"));
1998 strbuf_addbuf(final_buf
, &state
.stack
->output
);
1999 pop_stack_element(&state
.stack
);
2002 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
2004 struct strbuf final_buf
= STRBUF_INIT
;
2006 format_ref_array_item(info
, format
, quote_style
, &final_buf
);
2007 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2008 strbuf_release(&final_buf
);
2012 /* If no sorting option is given, use refname to sort as default */
2013 struct ref_sorting
*ref_default_sorting(void)
2015 static const char cstr_name
[] = "refname";
2017 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2019 sorting
->next
= NULL
;
2020 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
2024 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2026 struct ref_sorting
**sorting_tail
= opt
->value
;
2027 struct ref_sorting
*s
;
2030 if (!arg
) /* should --no-sort void the list ? */
2033 s
= xcalloc(1, sizeof(*s
));
2034 s
->next
= *sorting_tail
;
2041 if (skip_prefix(arg
, "version:", &arg
) ||
2042 skip_prefix(arg
, "v:", &arg
))
2045 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
2049 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2051 struct ref_filter
*rf
= opt
->value
;
2052 unsigned char sha1
[20];
2054 rf
->merge
= starts_with(opt
->long_name
, "no")
2055 ? REF_FILTER_MERGED_OMIT
2056 : REF_FILTER_MERGED_INCLUDE
;
2058 if (get_sha1(arg
, sha1
))
2059 die(_("malformed object name %s"), arg
);
2061 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
2062 if (!rf
->merge_commit
)
2063 return opterror(opt
, "must point to a commit", 0);