3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
17 #include "wt-status.h"
18 #include "commit-slab.h"
20 static struct ref_msg
{
24 const char *ahead_behind
;
26 /* Untranslated plumbing messages: */
33 void setup_ref_filter_porcelain_msg(void)
35 msgs
.gone
= _("gone");
36 msgs
.ahead
= _("ahead %d");
37 msgs
.behind
= _("behind %d");
38 msgs
.ahead_behind
= _("ahead %d, behind %d");
41 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
42 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
50 cmp_status cmp_status
;
52 unsigned int then_atom_seen
: 1,
54 condition_satisfied
: 1;
58 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
63 * An atom is a valid field atom listed below, possibly prefixed with
64 * a "*" to denote deref_tag().
66 * We parse given format string and sort specifiers, and make a list
67 * of properties that we need to extract out of objects. ref_array_item
68 * structure will hold an array of values extracted that can be
69 * indexed with the "atom number", which is an index into this
72 static struct used_atom
{
76 char color
[COLOR_MAXLEN
];
79 enum { RR_REF
, RR_TRACK
, RR_TRACKSHORT
} option
;
80 struct refname_atom refname
;
81 unsigned int nobracket
: 1;
84 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
88 cmp_status cmp_status
;
92 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
95 struct refname_atom refname
;
98 static int used_atom_cnt
, need_tagged
, need_symref
;
99 static int need_color_reset_at_eol
;
101 static void color_atom_parser(struct used_atom
*atom
, const char *color_value
)
104 die(_("expected format: %%(color:<color>)"));
105 if (color_parse(color_value
, atom
->u
.color
) < 0)
106 die(_("unrecognized color: %%(color:%s)"), color_value
);
109 static void refname_atom_parser_internal(struct refname_atom
*atom
,
110 const char *arg
, const char *name
)
113 atom
->option
= R_NORMAL
;
114 else if (!strcmp(arg
, "short"))
115 atom
->option
= R_SHORT
;
116 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
117 skip_prefix(arg
, "strip=", &arg
)) {
118 atom
->option
= R_LSTRIP
;
119 if (strtol_i(arg
, 10, &atom
->lstrip
))
120 die(_("Integer value expected refname:lstrip=%s"), arg
);
121 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
122 atom
->option
= R_RSTRIP
;
123 if (strtol_i(arg
, 10, &atom
->rstrip
))
124 die(_("Integer value expected refname:rstrip=%s"), arg
);
126 die(_("unrecognized %%(%s) argument: %s"), name
, arg
);
129 static void remote_ref_atom_parser(struct used_atom
*atom
, const char *arg
)
131 struct string_list params
= STRING_LIST_INIT_DUP
;
135 atom
->u
.remote_ref
.option
= RR_REF
;
136 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
141 atom
->u
.remote_ref
.nobracket
= 0;
142 string_list_split(¶ms
, arg
, ',', -1);
144 for (i
= 0; i
< params
.nr
; i
++) {
145 const char *s
= params
.items
[i
].string
;
147 if (!strcmp(s
, "track"))
148 atom
->u
.remote_ref
.option
= RR_TRACK
;
149 else if (!strcmp(s
, "trackshort"))
150 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
151 else if (!strcmp(s
, "nobracket"))
152 atom
->u
.remote_ref
.nobracket
= 1;
154 atom
->u
.remote_ref
.option
= RR_REF
;
155 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
160 string_list_clear(¶ms
, 0);
163 static void body_atom_parser(struct used_atom
*atom
, const char *arg
)
166 die(_("%%(body) does not take arguments"));
167 atom
->u
.contents
.option
= C_BODY_DEP
;
170 static void subject_atom_parser(struct used_atom
*atom
, const char *arg
)
173 die(_("%%(subject) does not take arguments"));
174 atom
->u
.contents
.option
= C_SUB
;
177 static void trailers_atom_parser(struct used_atom
*atom
, const char *arg
)
180 die(_("%%(trailers) does not take arguments"));
181 atom
->u
.contents
.option
= C_TRAILERS
;
184 static void contents_atom_parser(struct used_atom
*atom
, const char *arg
)
187 atom
->u
.contents
.option
= C_BARE
;
188 else if (!strcmp(arg
, "body"))
189 atom
->u
.contents
.option
= C_BODY
;
190 else if (!strcmp(arg
, "signature"))
191 atom
->u
.contents
.option
= C_SIG
;
192 else if (!strcmp(arg
, "subject"))
193 atom
->u
.contents
.option
= C_SUB
;
194 else if (!strcmp(arg
, "trailers"))
195 atom
->u
.contents
.option
= C_TRAILERS
;
196 else if (skip_prefix(arg
, "lines=", &arg
)) {
197 atom
->u
.contents
.option
= C_LINES
;
198 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
199 die(_("positive value expected contents:lines=%s"), arg
);
201 die(_("unrecognized %%(contents) argument: %s"), arg
);
204 static void objectname_atom_parser(struct used_atom
*atom
, const char *arg
)
207 atom
->u
.objectname
.option
= O_FULL
;
208 else if (!strcmp(arg
, "short"))
209 atom
->u
.objectname
.option
= O_SHORT
;
210 else if (skip_prefix(arg
, "short=", &arg
)) {
211 atom
->u
.objectname
.option
= O_LENGTH
;
212 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
213 atom
->u
.objectname
.length
== 0)
214 die(_("positive value expected objectname:short=%s"), arg
);
215 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
216 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
218 die(_("unrecognized %%(objectname) argument: %s"), arg
);
221 static void refname_atom_parser(struct used_atom
*atom
, const char *arg
)
223 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
);
226 static align_type
parse_align_position(const char *s
)
228 if (!strcmp(s
, "right"))
230 else if (!strcmp(s
, "middle"))
232 else if (!strcmp(s
, "left"))
237 static void align_atom_parser(struct used_atom
*atom
, const char *arg
)
239 struct align
*align
= &atom
->u
.align
;
240 struct string_list params
= STRING_LIST_INIT_DUP
;
242 unsigned int width
= ~0U;
245 die(_("expected format: %%(align:<width>,<position>)"));
247 align
->position
= ALIGN_LEFT
;
249 string_list_split(¶ms
, arg
, ',', -1);
250 for (i
= 0; i
< params
.nr
; i
++) {
251 const char *s
= params
.items
[i
].string
;
254 if (skip_prefix(s
, "position=", &s
)) {
255 position
= parse_align_position(s
);
257 die(_("unrecognized position:%s"), s
);
258 align
->position
= position
;
259 } else if (skip_prefix(s
, "width=", &s
)) {
260 if (strtoul_ui(s
, 10, &width
))
261 die(_("unrecognized width:%s"), s
);
262 } else if (!strtoul_ui(s
, 10, &width
))
264 else if ((position
= parse_align_position(s
)) >= 0)
265 align
->position
= position
;
267 die(_("unrecognized %%(align) argument: %s"), s
);
271 die(_("positive width expected with the %%(align) atom"));
272 align
->width
= width
;
273 string_list_clear(¶ms
, 0);
276 static void if_atom_parser(struct used_atom
*atom
, const char *arg
)
279 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
281 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
282 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
283 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
284 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
286 die(_("unrecognized %%(if) argument: %s"), arg
);
294 void (*parser
)(struct used_atom
*atom
, const char *arg
);
296 { "refname" , FIELD_STR
, refname_atom_parser
},
298 { "objectsize", FIELD_ULONG
},
299 { "objectname", FIELD_STR
, objectname_atom_parser
},
302 { "numparent", FIELD_ULONG
},
309 { "authordate", FIELD_TIME
},
312 { "committeremail" },
313 { "committerdate", FIELD_TIME
},
317 { "taggerdate", FIELD_TIME
},
319 { "creatordate", FIELD_TIME
},
320 { "subject", FIELD_STR
, subject_atom_parser
},
321 { "body", FIELD_STR
, body_atom_parser
},
322 { "trailers", FIELD_STR
, trailers_atom_parser
},
323 { "contents", FIELD_STR
, contents_atom_parser
},
324 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
325 { "push", FIELD_STR
, remote_ref_atom_parser
},
326 { "symref", FIELD_STR
, refname_atom_parser
},
329 { "color", FIELD_STR
, color_atom_parser
},
330 { "align", FIELD_STR
, align_atom_parser
},
332 { "if", FIELD_STR
, if_atom_parser
},
337 #define REF_FORMATTING_STATE_INIT { 0, NULL }
339 struct ref_formatting_stack
{
340 struct ref_formatting_stack
*prev
;
341 struct strbuf output
;
342 void (*at_end
)(struct ref_formatting_stack
**stack
);
346 struct ref_formatting_state
{
348 struct ref_formatting_stack
*stack
;
353 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
354 unsigned long ul
; /* used for sorting when not FIELD_STR */
355 struct used_atom
*atom
;
359 * Used to parse format string and sort specifiers
361 int parse_ref_filter_atom(const char *atom
, const char *ep
)
368 if (*sp
== '*' && sp
< ep
)
371 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
373 /* Do we have the atom already used elsewhere? */
374 for (i
= 0; i
< used_atom_cnt
; i
++) {
375 int len
= strlen(used_atom
[i
].name
);
376 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
381 * If the atom name has a colon, strip it and everything after
382 * it off - it specifies the format for this entry, and
383 * shouldn't be used for checking against the valid_atom
386 arg
= memchr(sp
, ':', ep
- sp
);
387 atom_len
= (arg
? arg
: ep
) - sp
;
389 /* Is the atom a valid one? */
390 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
391 int len
= strlen(valid_atom
[i
].name
);
392 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
396 if (ARRAY_SIZE(valid_atom
) <= i
)
397 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
399 /* Add it in, including the deref prefix */
402 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
403 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
404 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
406 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
407 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
408 if (valid_atom
[i
].parser
)
409 valid_atom
[i
].parser(&used_atom
[at
], arg
);
412 if (!strcmp(valid_atom
[i
].name
, "symref"))
417 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
419 switch (quote_style
) {
421 strbuf_addstr(s
, str
);
424 sq_quote_buf(s
, str
);
427 perl_quote_buf(s
, str
);
430 python_quote_buf(s
, str
);
433 tcl_quote_buf(s
, str
);
438 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
441 * Quote formatting is only done when the stack has a single
442 * element. Otherwise quote formatting is done on the
443 * element's entire output strbuf when the %(end) atom is
446 if (!state
->stack
->prev
)
447 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
449 strbuf_addstr(&state
->stack
->output
, v
->s
);
452 static void push_stack_element(struct ref_formatting_stack
**stack
)
454 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
456 strbuf_init(&s
->output
, 0);
461 static void pop_stack_element(struct ref_formatting_stack
**stack
)
463 struct ref_formatting_stack
*current
= *stack
;
464 struct ref_formatting_stack
*prev
= current
->prev
;
467 strbuf_addbuf(&prev
->output
, ¤t
->output
);
468 strbuf_release(¤t
->output
);
473 static void end_align_handler(struct ref_formatting_stack
**stack
)
475 struct ref_formatting_stack
*cur
= *stack
;
476 struct align
*align
= (struct align
*)cur
->at_end_data
;
477 struct strbuf s
= STRBUF_INIT
;
479 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
480 strbuf_swap(&cur
->output
, &s
);
484 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
486 struct ref_formatting_stack
*new;
488 push_stack_element(&state
->stack
);
490 new->at_end
= end_align_handler
;
491 new->at_end_data
= &atomv
->atom
->u
.align
;
494 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
496 struct ref_formatting_stack
*cur
= *stack
;
497 struct ref_formatting_stack
*prev
= cur
->prev
;
498 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
500 if (!if_then_else
->then_atom_seen
)
501 die(_("format: %%(if) atom used without a %%(then) atom"));
503 if (if_then_else
->else_atom_seen
) {
505 * There is an %(else) atom: we need to drop one state from the
506 * stack, either the %(else) branch if the condition is satisfied, or
507 * the %(then) branch if it isn't.
509 if (if_then_else
->condition_satisfied
) {
510 strbuf_reset(&cur
->output
);
511 pop_stack_element(&cur
);
513 strbuf_swap(&cur
->output
, &prev
->output
);
514 strbuf_reset(&cur
->output
);
515 pop_stack_element(&cur
);
517 } else if (!if_then_else
->condition_satisfied
) {
519 * No %(else) atom: just drop the %(then) branch if the
520 * condition is not satisfied.
522 strbuf_reset(&cur
->output
);
529 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
531 struct ref_formatting_stack
*new;
532 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
534 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
535 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
537 push_stack_element(&state
->stack
);
539 new->at_end
= if_then_else_handler
;
540 new->at_end_data
= if_then_else
;
543 static int is_empty(const char *s
)
553 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
555 struct ref_formatting_stack
*cur
= state
->stack
;
556 struct if_then_else
*if_then_else
= NULL
;
558 if (cur
->at_end
== if_then_else_handler
)
559 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
561 die(_("format: %%(then) atom used without an %%(if) atom"));
562 if (if_then_else
->then_atom_seen
)
563 die(_("format: %%(then) atom used more than once"));
564 if (if_then_else
->else_atom_seen
)
565 die(_("format: %%(then) atom used after %%(else)"));
566 if_then_else
->then_atom_seen
= 1;
568 * If the 'equals' or 'notequals' attribute is used then
569 * perform the required comparison. If not, only non-empty
570 * strings satisfy the 'if' condition.
572 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
573 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
574 if_then_else
->condition_satisfied
= 1;
575 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
576 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
577 if_then_else
->condition_satisfied
= 1;
578 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
579 if_then_else
->condition_satisfied
= 1;
580 strbuf_reset(&cur
->output
);
583 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
585 struct ref_formatting_stack
*prev
= state
->stack
;
586 struct if_then_else
*if_then_else
= NULL
;
588 if (prev
->at_end
== if_then_else_handler
)
589 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
591 die(_("format: %%(else) atom used without an %%(if) atom"));
592 if (!if_then_else
->then_atom_seen
)
593 die(_("format: %%(else) atom used without a %%(then) atom"));
594 if (if_then_else
->else_atom_seen
)
595 die(_("format: %%(else) atom used more than once"));
596 if_then_else
->else_atom_seen
= 1;
597 push_stack_element(&state
->stack
);
598 state
->stack
->at_end_data
= prev
->at_end_data
;
599 state
->stack
->at_end
= prev
->at_end
;
602 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
604 struct ref_formatting_stack
*current
= state
->stack
;
605 struct strbuf s
= STRBUF_INIT
;
607 if (!current
->at_end
)
608 die(_("format: %%(end) atom used without corresponding atom"));
609 current
->at_end(&state
->stack
);
611 /* Stack may have been popped within at_end(), hence reset the current pointer */
612 current
= state
->stack
;
615 * Perform quote formatting when the stack element is that of
616 * a supporting atom. If nested then perform quote formatting
617 * only on the topmost supporting atom.
619 if (!current
->prev
->prev
) {
620 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
621 strbuf_swap(¤t
->output
, &s
);
624 pop_stack_element(&state
->stack
);
628 * In a format string, find the next occurrence of %(atom).
630 static const char *find_next(const char *cp
)
635 * %( is the start of an atom;
636 * %% is a quoted per-cent.
640 else if (cp
[1] == '%')
641 cp
++; /* skip over two % */
642 /* otherwise this is a singleton, literal % */
650 * Make sure the format string is well formed, and parse out
653 int verify_ref_format(const char *format
)
657 need_color_reset_at_eol
= 0;
658 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
659 const char *color
, *ep
= strchr(sp
, ')');
663 return error(_("malformed format string %s"), sp
);
664 /* sp points at "%(" and ep points at the closing ")" */
665 at
= parse_ref_filter_atom(sp
+ 2, ep
);
668 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
669 need_color_reset_at_eol
= !!strcmp(color
, "reset");
675 * Given an object name, read the object data and size, and return a
676 * "struct object". If the object data we are returning is also borrowed
677 * by the "struct object" representation, set *eaten as well---it is a
678 * signal from parse_object_buffer to us not to free the buffer.
680 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
682 enum object_type type
;
683 void *buf
= read_sha1_file(sha1
, &type
, sz
);
686 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
692 static int grab_objectname(const char *name
, const unsigned char *sha1
,
693 struct atom_value
*v
, struct used_atom
*atom
)
695 if (starts_with(name
, "objectname")) {
696 if (atom
->u
.objectname
.option
== O_SHORT
) {
697 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
699 } else if (atom
->u
.objectname
.option
== O_FULL
) {
700 v
->s
= xstrdup(sha1_to_hex(sha1
));
702 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
703 v
->s
= xstrdup(find_unique_abbrev(sha1
, atom
->u
.objectname
.length
));
706 die("BUG: unknown %%(objectname) option");
711 /* See grab_values */
712 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
716 for (i
= 0; i
< used_atom_cnt
; i
++) {
717 const char *name
= used_atom
[i
].name
;
718 struct atom_value
*v
= &val
[i
];
719 if (!!deref
!= (*name
== '*'))
723 if (!strcmp(name
, "objecttype"))
724 v
->s
= typename(obj
->type
);
725 else if (!strcmp(name
, "objectsize")) {
727 v
->s
= xstrfmt("%lu", sz
);
730 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
734 /* See grab_values */
735 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
738 struct tag
*tag
= (struct tag
*) obj
;
740 for (i
= 0; i
< used_atom_cnt
; i
++) {
741 const char *name
= used_atom
[i
].name
;
742 struct atom_value
*v
= &val
[i
];
743 if (!!deref
!= (*name
== '*'))
747 if (!strcmp(name
, "tag"))
749 else if (!strcmp(name
, "type") && tag
->tagged
)
750 v
->s
= typename(tag
->tagged
->type
);
751 else if (!strcmp(name
, "object") && tag
->tagged
)
752 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
756 /* See grab_values */
757 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
760 struct commit
*commit
= (struct commit
*) obj
;
762 for (i
= 0; i
< used_atom_cnt
; i
++) {
763 const char *name
= used_atom
[i
].name
;
764 struct atom_value
*v
= &val
[i
];
765 if (!!deref
!= (*name
== '*'))
769 if (!strcmp(name
, "tree")) {
770 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
772 else if (!strcmp(name
, "numparent")) {
773 v
->ul
= commit_list_count(commit
->parents
);
774 v
->s
= xstrfmt("%lu", v
->ul
);
776 else if (!strcmp(name
, "parent")) {
777 struct commit_list
*parents
;
778 struct strbuf s
= STRBUF_INIT
;
779 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
780 struct commit
*parent
= parents
->item
;
781 if (parents
!= commit
->parents
)
782 strbuf_addch(&s
, ' ');
783 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
785 v
->s
= strbuf_detach(&s
, NULL
);
790 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
794 if (!strncmp(buf
, who
, wholen
) &&
796 return buf
+ wholen
+ 1;
797 eol
= strchr(buf
, '\n');
802 return ""; /* end of header */
808 static const char *copy_line(const char *buf
)
810 const char *eol
= strchrnul(buf
, '\n');
811 return xmemdupz(buf
, eol
- buf
);
814 static const char *copy_name(const char *buf
)
817 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
818 if (!strncmp(cp
, " <", 2))
819 return xmemdupz(buf
, cp
- buf
);
824 static const char *copy_email(const char *buf
)
826 const char *email
= strchr(buf
, '<');
830 eoemail
= strchr(email
, '>');
833 return xmemdupz(email
, eoemail
+ 1 - email
);
836 static char *copy_subject(const char *buf
, unsigned long len
)
838 char *r
= xmemdupz(buf
, len
);
841 for (i
= 0; i
< len
; i
++)
848 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
850 const char *eoemail
= strstr(buf
, "> ");
852 unsigned long timestamp
;
854 struct date_mode date_mode
= { DATE_NORMAL
};
858 * We got here because atomname ends in "date" or "date<something>";
859 * it's not possible that <something> is not ":<format>" because
860 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
861 * ":" means no format is specified, and use the default.
863 formatp
= strchr(atomname
, ':');
864 if (formatp
!= NULL
) {
866 parse_date_format(formatp
, &date_mode
);
871 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
872 if (timestamp
== ULONG_MAX
)
874 tz
= strtol(zone
, NULL
, 10);
875 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
877 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
885 /* See grab_values */
886 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
889 int wholen
= strlen(who
);
890 const char *wholine
= NULL
;
892 for (i
= 0; i
< used_atom_cnt
; i
++) {
893 const char *name
= used_atom
[i
].name
;
894 struct atom_value
*v
= &val
[i
];
895 if (!!deref
!= (*name
== '*'))
899 if (strncmp(who
, name
, wholen
))
901 if (name
[wholen
] != 0 &&
902 strcmp(name
+ wholen
, "name") &&
903 strcmp(name
+ wholen
, "email") &&
904 !starts_with(name
+ wholen
, "date"))
907 wholine
= find_wholine(who
, wholen
, buf
, sz
);
909 return; /* no point looking for it */
910 if (name
[wholen
] == 0)
911 v
->s
= copy_line(wholine
);
912 else if (!strcmp(name
+ wholen
, "name"))
913 v
->s
= copy_name(wholine
);
914 else if (!strcmp(name
+ wholen
, "email"))
915 v
->s
= copy_email(wholine
);
916 else if (starts_with(name
+ wholen
, "date"))
917 grab_date(wholine
, v
, name
);
921 * For a tag or a commit object, if "creator" or "creatordate" is
922 * requested, do something special.
924 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
925 return; /* "author" for commit object is not wanted */
927 wholine
= find_wholine(who
, wholen
, buf
, sz
);
930 for (i
= 0; i
< used_atom_cnt
; i
++) {
931 const char *name
= used_atom
[i
].name
;
932 struct atom_value
*v
= &val
[i
];
933 if (!!deref
!= (*name
== '*'))
938 if (starts_with(name
, "creatordate"))
939 grab_date(wholine
, v
, name
);
940 else if (!strcmp(name
, "creator"))
941 v
->s
= copy_line(wholine
);
945 static void find_subpos(const char *buf
, unsigned long sz
,
946 const char **sub
, unsigned long *sublen
,
947 const char **body
, unsigned long *bodylen
,
948 unsigned long *nonsiglen
,
949 const char **sig
, unsigned long *siglen
)
952 /* skip past header until we hit empty line */
953 while (*buf
&& *buf
!= '\n') {
954 eol
= strchrnul(buf
, '\n');
959 /* skip any empty lines */
963 /* parse signature first; we might not even have a subject line */
964 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
965 *siglen
= strlen(*sig
);
967 /* subject is first non-empty line */
969 /* subject goes to first empty line */
970 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
971 eol
= strchrnul(buf
, '\n');
976 *sublen
= buf
- *sub
;
977 /* drop trailing newline, if present */
978 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
981 /* skip any empty lines */
985 *bodylen
= strlen(buf
);
986 *nonsiglen
= *sig
- buf
;
990 * If 'lines' is greater than 0, append that many lines from the given
991 * 'buf' of length 'size' to the given strbuf.
993 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
996 const char *sp
, *eol
;
1001 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1003 strbuf_addstr(out
, "\n ");
1004 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1005 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1006 strbuf_add(out
, sp
, len
);
1013 /* See grab_values */
1014 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1017 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1018 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1020 for (i
= 0; i
< used_atom_cnt
; i
++) {
1021 struct used_atom
*atom
= &used_atom
[i
];
1022 const char *name
= atom
->name
;
1023 struct atom_value
*v
= &val
[i
];
1024 if (!!deref
!= (*name
== '*'))
1028 if (strcmp(name
, "subject") &&
1029 strcmp(name
, "body") &&
1030 strcmp(name
, "trailers") &&
1031 !starts_with(name
, "contents"))
1034 find_subpos(buf
, sz
,
1036 &bodypos
, &bodylen
, &nonsiglen
,
1039 if (atom
->u
.contents
.option
== C_SUB
)
1040 v
->s
= copy_subject(subpos
, sublen
);
1041 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1042 v
->s
= xmemdupz(bodypos
, bodylen
);
1043 else if (atom
->u
.contents
.option
== C_BODY
)
1044 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1045 else if (atom
->u
.contents
.option
== C_SIG
)
1046 v
->s
= xmemdupz(sigpos
, siglen
);
1047 else if (atom
->u
.contents
.option
== C_LINES
) {
1048 struct strbuf s
= STRBUF_INIT
;
1049 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1051 /* Size is the length of the message after removing the signature */
1052 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1053 v
->s
= strbuf_detach(&s
, NULL
);
1054 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1055 struct trailer_info info
;
1057 /* Search for trailer info */
1058 trailer_info_get(&info
, subpos
);
1059 v
->s
= xmemdupz(info
.trailer_start
,
1060 info
.trailer_end
- info
.trailer_start
);
1061 trailer_info_release(&info
);
1062 } else if (atom
->u
.contents
.option
== C_BARE
)
1063 v
->s
= xstrdup(subpos
);
1068 * We want to have empty print-string for field requests
1069 * that do not apply (e.g. "authordate" for a tag object)
1071 static void fill_missing_values(struct atom_value
*val
)
1074 for (i
= 0; i
< used_atom_cnt
; i
++) {
1075 struct atom_value
*v
= &val
[i
];
1082 * val is a list of atom_value to hold returned values. Extract
1083 * the values for atoms in used_atom array out of (obj, buf, sz).
1084 * when deref is false, (obj, buf, sz) is the object that is
1085 * pointed at by the ref itself; otherwise it is the object the
1086 * ref (which is a tag) refers to.
1088 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1090 grab_common_values(val
, deref
, obj
, buf
, sz
);
1091 switch (obj
->type
) {
1093 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1094 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1095 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1098 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1099 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1100 grab_person("author", val
, deref
, obj
, buf
, sz
);
1101 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1104 /* grab_tree_values(val, deref, obj, buf, sz); */
1107 /* grab_blob_values(val, deref, obj, buf, sz); */
1110 die("Eh? Object of type %d?", obj
->type
);
1114 static inline char *copy_advance(char *dst
, const char *src
)
1121 static const char *lstrip_ref_components(const char *refname
, int len
)
1123 long remaining
= len
;
1124 const char *start
= refname
;
1128 const char *p
= refname
;
1130 /* Find total no of '/' separated path-components */
1131 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1134 * The number of components we need to strip is now
1135 * the total minus the components to be left (Plus one
1136 * because we count the number of '/', but the number
1137 * of components is one more than the no of '/').
1139 remaining
= i
+ len
+ 1;
1142 while (remaining
> 0) {
1155 static const char *rstrip_ref_components(const char *refname
, int len
)
1157 long remaining
= len
;
1158 char *start
= xstrdup(refname
);
1162 const char *p
= refname
;
1164 /* Find total no of '/' separated path-components */
1165 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1168 * The number of components we need to strip is now
1169 * the total minus the components to be left (Plus one
1170 * because we count the number of '/', but the number
1171 * of components is one more than the no of '/').
1173 remaining
= i
+ len
+ 1;
1176 while (remaining
-- > 0) {
1177 char *p
= strrchr(start
, '/');
1186 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1188 if (atom
->option
== R_SHORT
)
1189 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1190 else if (atom
->option
== R_LSTRIP
)
1191 return lstrip_ref_components(refname
, atom
->lstrip
);
1192 else if (atom
->option
== R_RSTRIP
)
1193 return rstrip_ref_components(refname
, atom
->rstrip
);
1198 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1199 struct branch
*branch
, const char **s
)
1201 int num_ours
, num_theirs
;
1202 if (atom
->u
.remote_ref
.option
== RR_REF
)
1203 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1204 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1205 if (stat_tracking_info(branch
, &num_ours
,
1206 &num_theirs
, NULL
)) {
1207 *s
= xstrdup(msgs
.gone
);
1208 } else if (!num_ours
&& !num_theirs
)
1211 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1212 else if (!num_theirs
)
1213 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1215 *s
= xstrfmt(msgs
.ahead_behind
,
1216 num_ours
, num_theirs
);
1217 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1218 const char *to_free
= *s
;
1219 *s
= xstrfmt("[%s]", *s
);
1220 free((void *)to_free
);
1222 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1223 if (stat_tracking_info(branch
, &num_ours
,
1227 if (!num_ours
&& !num_theirs
)
1231 else if (!num_theirs
)
1236 die("BUG: unhandled RR_* enum");
1239 char *get_head_description(void)
1241 struct strbuf desc
= STRBUF_INIT
;
1242 struct wt_status_state state
;
1243 memset(&state
, 0, sizeof(state
));
1244 wt_status_get_state(&state
, 1);
1245 if (state
.rebase_in_progress
||
1246 state
.rebase_interactive_in_progress
)
1247 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1249 else if (state
.bisect_in_progress
)
1250 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1252 else if (state
.detached_from
) {
1253 if (state
.detached_at
)
1255 * TRANSLATORS: make sure this matches "HEAD
1256 * detached at " in wt-status.c
1258 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1259 state
.detached_from
);
1262 * TRANSLATORS: make sure this matches "HEAD
1263 * detached from " in wt-status.c
1265 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1266 state
.detached_from
);
1269 strbuf_addstr(&desc
, _("(no branch)"));
1272 free(state
.detached_from
);
1273 return strbuf_detach(&desc
, NULL
);
1276 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1281 return show_ref(&atom
->u
.refname
, ref
->symref
);
1284 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1286 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1287 return get_head_description();
1288 return show_ref(&atom
->u
.refname
, ref
->refname
);
1292 * Parse the object referred by ref, and grab needed value.
1294 static void populate_value(struct ref_array_item
*ref
)
1300 const unsigned char *tagged
;
1302 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1304 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1305 struct object_id unused1
;
1306 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1307 unused1
.hash
, NULL
);
1312 /* Fill in specials first */
1313 for (i
= 0; i
< used_atom_cnt
; i
++) {
1314 struct used_atom
*atom
= &used_atom
[i
];
1315 const char *name
= used_atom
[i
].name
;
1316 struct atom_value
*v
= &ref
->value
[i
];
1318 const char *refname
;
1319 struct branch
*branch
= NULL
;
1321 v
->handler
= append_atom
;
1329 if (starts_with(name
, "refname"))
1330 refname
= get_refname(atom
, ref
);
1331 else if (starts_with(name
, "symref"))
1332 refname
= get_symref(atom
, ref
);
1333 else if (starts_with(name
, "upstream")) {
1334 const char *branch_name
;
1335 /* only local branches may have an upstream */
1336 if (!skip_prefix(ref
->refname
, "refs/heads/",
1339 branch
= branch_get(branch_name
);
1341 refname
= branch_get_upstream(branch
, NULL
);
1343 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1345 } else if (starts_with(name
, "push")) {
1346 const char *branch_name
;
1347 if (!skip_prefix(ref
->refname
, "refs/heads/",
1350 branch
= branch_get(branch_name
);
1352 refname
= branch_get_push(branch
, NULL
);
1355 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1357 } else if (starts_with(name
, "color:")) {
1358 v
->s
= atom
->u
.color
;
1360 } else if (!strcmp(name
, "flag")) {
1361 char buf
[256], *cp
= buf
;
1362 if (ref
->flag
& REF_ISSYMREF
)
1363 cp
= copy_advance(cp
, ",symref");
1364 if (ref
->flag
& REF_ISPACKED
)
1365 cp
= copy_advance(cp
, ",packed");
1370 v
->s
= xstrdup(buf
+ 1);
1373 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
, atom
)) {
1375 } else if (!strcmp(name
, "HEAD")) {
1377 unsigned char sha1
[20];
1379 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1381 if (head
&& !strcmp(ref
->refname
, head
))
1386 } else if (starts_with(name
, "align")) {
1387 v
->handler
= align_atom_handler
;
1389 } else if (!strcmp(name
, "end")) {
1390 v
->handler
= end_atom_handler
;
1392 } else if (starts_with(name
, "if")) {
1395 if (skip_prefix(name
, "if:", &s
))
1397 v
->handler
= if_atom_handler
;
1399 } else if (!strcmp(name
, "then")) {
1400 v
->handler
= then_atom_handler
;
1402 } else if (!strcmp(name
, "else")) {
1403 v
->handler
= else_atom_handler
;
1411 v
->s
= xstrfmt("%s^{}", refname
);
1414 for (i
= 0; i
< used_atom_cnt
; i
++) {
1415 struct atom_value
*v
= &ref
->value
[i
];
1422 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
1424 die(_("missing object %s for %s"),
1425 sha1_to_hex(ref
->objectname
), ref
->refname
);
1427 die(_("parse_object_buffer failed on %s for %s"),
1428 sha1_to_hex(ref
->objectname
), ref
->refname
);
1430 grab_values(ref
->value
, 0, obj
, buf
, size
);
1435 * If there is no atom that wants to know about tagged
1436 * object, we are done.
1438 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1442 * If it is a tag object, see if we use a value that derefs
1443 * the object, and if we do grab the object it refers to.
1445 tagged
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1448 * NEEDSWORK: This derefs tag only once, which
1449 * is good to deal with chains of trust, but
1450 * is not consistent with what deref_tag() does
1451 * which peels the onion to the core.
1453 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1455 die(_("missing object %s for %s"),
1456 sha1_to_hex(tagged
), ref
->refname
);
1458 die(_("parse_object_buffer failed on %s for %s"),
1459 sha1_to_hex(tagged
), ref
->refname
);
1460 grab_values(ref
->value
, 1, obj
, buf
, size
);
1466 * Given a ref, return the value for the atom. This lazily gets value
1467 * out of the object by calling populate value.
1469 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1472 populate_value(ref
);
1473 fill_missing_values(ref
->value
);
1475 *v
= &ref
->value
[atom
];
1479 * Unknown has to be "0" here, because that's the default value for
1480 * contains_cache slab entries that have not yet been assigned.
1482 enum contains_result
{
1483 CONTAINS_UNKNOWN
= 0,
1488 define_commit_slab(contains_cache
, enum contains_result
);
1490 struct ref_filter_cbdata
{
1491 struct ref_array
*array
;
1492 struct ref_filter
*filter
;
1493 struct contains_cache contains_cache
;
1494 struct contains_cache no_contains_cache
;
1498 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1501 * At each recursion step, the stack items points to the commits whose
1502 * ancestors are to be inspected.
1504 struct contains_stack
{
1506 struct contains_stack_entry
{
1507 struct commit
*commit
;
1508 struct commit_list
*parents
;
1512 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1514 for (; want
; want
= want
->next
)
1515 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1521 * Test whether the candidate or one of its parents is contained in the list.
1522 * Do not recurse to find out, though, but return -1 if inconclusive.
1524 static enum contains_result
contains_test(struct commit
*candidate
,
1525 const struct commit_list
*want
,
1526 struct contains_cache
*cache
)
1528 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
1530 /* If we already have the answer cached, return that. */
1535 if (in_commit_list(want
, candidate
)) {
1536 *cached
= CONTAINS_YES
;
1537 return CONTAINS_YES
;
1540 /* Otherwise, we don't know; prepare to recurse */
1541 parse_commit_or_die(candidate
);
1542 return CONTAINS_UNKNOWN
;
1545 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1547 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1548 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1549 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1552 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1553 const struct commit_list
*want
,
1554 struct contains_cache
*cache
)
1556 struct contains_stack contains_stack
= { 0, 0, NULL
};
1557 enum contains_result result
= contains_test(candidate
, want
, cache
);
1559 if (result
!= CONTAINS_UNKNOWN
)
1562 push_to_contains_stack(candidate
, &contains_stack
);
1563 while (contains_stack
.nr
) {
1564 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1565 struct commit
*commit
= entry
->commit
;
1566 struct commit_list
*parents
= entry
->parents
;
1569 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
1570 contains_stack
.nr
--;
1573 * If we just popped the stack, parents->item has been marked,
1574 * therefore contains_test will return a meaningful yes/no.
1576 else switch (contains_test(parents
->item
, want
, cache
)) {
1578 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
1579 contains_stack
.nr
--;
1582 entry
->parents
= parents
->next
;
1584 case CONTAINS_UNKNOWN
:
1585 push_to_contains_stack(parents
->item
, &contains_stack
);
1589 free(contains_stack
.contains_stack
);
1590 return contains_test(candidate
, want
, cache
);
1593 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
1594 struct commit_list
*list
, struct contains_cache
*cache
)
1596 if (filter
->with_commit_tag_algo
)
1597 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
1598 return is_descendant_of(commit
, list
);
1602 * Return 1 if the refname matches one of the patterns, otherwise 0.
1603 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1604 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1605 * matches "refs/heads/mas*", too).
1607 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1609 const char **patterns
= filter
->name_patterns
;
1612 if (filter
->ignore_case
)
1613 flags
|= WM_CASEFOLD
;
1616 * When no '--format' option is given we need to skip the prefix
1617 * for matching refs of tags and branches.
1619 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1620 skip_prefix(refname
, "refs/heads/", &refname
) ||
1621 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1622 skip_prefix(refname
, "refs/", &refname
));
1624 for (; *patterns
; patterns
++) {
1625 if (!wildmatch(*patterns
, refname
, flags
, NULL
))
1632 * Return 1 if the refname matches one of the patterns, otherwise 0.
1633 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1634 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1635 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1637 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1639 const char **pattern
= filter
->name_patterns
;
1640 int namelen
= strlen(refname
);
1641 unsigned flags
= WM_PATHNAME
;
1643 if (filter
->ignore_case
)
1644 flags
|= WM_CASEFOLD
;
1646 for (; *pattern
; pattern
++) {
1647 const char *p
= *pattern
;
1648 int plen
= strlen(p
);
1650 if ((plen
<= namelen
) &&
1651 !strncmp(refname
, p
, plen
) &&
1652 (refname
[plen
] == '\0' ||
1653 refname
[plen
] == '/' ||
1656 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1662 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1663 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1665 if (!*filter
->name_patterns
)
1666 return 1; /* No pattern always matches */
1667 if (filter
->match_as_path
)
1668 return match_name_as_path(filter
, refname
);
1669 return match_pattern(filter
, refname
);
1673 * Given a ref (sha1, refname), check if the ref belongs to the array
1674 * of sha1s. If the given ref is a tag, check if the given tag points
1675 * at one of the sha1s in the given sha1 array.
1676 * the given sha1_array.
1678 * 1. Only a single level of inderection is obtained, we might want to
1679 * change this to account for multiple levels (e.g. annotated tags
1680 * pointing to annotated tags pointing to a commit.)
1681 * 2. As the refs are cached we might know what refname peels to without
1682 * the need to parse the object via parse_object(). peel_ref() might be a
1683 * more efficient alternative to obtain the pointee.
1685 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1686 const struct object_id
*oid
,
1687 const char *refname
)
1689 const struct object_id
*tagged_oid
= NULL
;
1692 if (oid_array_lookup(points_at
, oid
) >= 0)
1694 obj
= parse_object(oid
->hash
);
1696 die(_("malformed object at '%s'"), refname
);
1697 if (obj
->type
== OBJ_TAG
)
1698 tagged_oid
= &((struct tag
*)obj
)->tagged
->oid
;
1699 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
1704 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1705 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1706 const unsigned char *objectname
,
1709 struct ref_array_item
*ref
;
1710 FLEX_ALLOC_STR(ref
, refname
, refname
);
1711 hashcpy(ref
->objectname
, objectname
);
1717 static int ref_kind_from_refname(const char *refname
)
1725 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1726 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1727 { "refs/tags/", FILTER_REFS_TAGS
}
1730 if (!strcmp(refname
, "HEAD"))
1731 return FILTER_REFS_DETACHED_HEAD
;
1733 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1734 if (starts_with(refname
, ref_kind
[i
].prefix
))
1735 return ref_kind
[i
].kind
;
1738 return FILTER_REFS_OTHERS
;
1741 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1743 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1744 filter
->kind
== FILTER_REFS_REMOTES
||
1745 filter
->kind
== FILTER_REFS_TAGS
)
1746 return filter
->kind
;
1747 return ref_kind_from_refname(refname
);
1751 * A call-back given to for_each_ref(). Filter refs and keep them for
1752 * later object processing.
1754 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1756 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1757 struct ref_filter
*filter
= ref_cbdata
->filter
;
1758 struct ref_array_item
*ref
;
1759 struct commit
*commit
= NULL
;
1762 if (flag
& REF_BAD_NAME
) {
1763 warning(_("ignoring ref with broken name %s"), refname
);
1767 if (flag
& REF_ISBROKEN
) {
1768 warning(_("ignoring broken ref %s"), refname
);
1772 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1773 kind
= filter_ref_kind(filter
, refname
);
1774 if (!(kind
& filter
->kind
))
1777 if (!filter_pattern_match(filter
, refname
))
1780 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
1784 * A merge filter is applied on refs pointing to commits. Hence
1785 * obtain the commit using the 'oid' available and discard all
1786 * non-commits early. The actual filtering is done later.
1788 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
1789 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1792 /* We perform the filtering for the '--contains' option... */
1793 if (filter
->with_commit
&&
1794 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
1796 /* ...or for the `--no-contains' option */
1797 if (filter
->no_commit
&&
1798 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
1803 * We do not open the object yet; sort may only need refname
1804 * to do its job and the resulting list may yet to be pruned
1805 * by maxcount logic.
1807 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1808 ref
->commit
= commit
;
1810 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1811 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1816 /* Free memory allocated for a ref_array_item */
1817 static void free_array_item(struct ref_array_item
*item
)
1819 free((char *)item
->symref
);
1823 /* Free all memory allocated for ref_array */
1824 void ref_array_clear(struct ref_array
*array
)
1828 for (i
= 0; i
< array
->nr
; i
++)
1829 free_array_item(array
->items
[i
]);
1831 array
->items
= NULL
;
1832 array
->nr
= array
->alloc
= 0;
1835 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1837 struct rev_info revs
;
1839 struct ref_filter
*filter
= ref_cbdata
->filter
;
1840 struct ref_array
*array
= ref_cbdata
->array
;
1841 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1843 init_revisions(&revs
, NULL
);
1845 for (i
= 0; i
< array
->nr
; i
++) {
1846 struct ref_array_item
*item
= array
->items
[i
];
1847 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1848 to_clear
[i
] = item
->commit
;
1851 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1852 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1855 if (prepare_revision_walk(&revs
))
1856 die(_("revision walk setup failed"));
1861 for (i
= 0; i
< old_nr
; i
++) {
1862 struct ref_array_item
*item
= array
->items
[i
];
1863 struct commit
*commit
= item
->commit
;
1865 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1867 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1868 array
->items
[array
->nr
++] = array
->items
[i
];
1870 free_array_item(item
);
1873 for (i
= 0; i
< old_nr
; i
++)
1874 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1875 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1880 * API for filtering a set of refs. Based on the type of refs the user
1881 * has requested, we iterate through those refs and apply filters
1882 * as per the given ref_filter structure and finally store the
1883 * filtered refs in the ref_array structure.
1885 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1887 struct ref_filter_cbdata ref_cbdata
;
1889 unsigned int broken
= 0;
1891 ref_cbdata
.array
= array
;
1892 ref_cbdata
.filter
= filter
;
1894 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1896 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1898 init_contains_cache(&ref_cbdata
.contains_cache
);
1899 init_contains_cache(&ref_cbdata
.no_contains_cache
);
1901 /* Simple per-ref filtering */
1903 die("filter_refs: invalid type");
1906 * For common cases where we need only branches or remotes or tags,
1907 * we only iterate through those refs. If a mix of refs is needed,
1908 * we iterate over all refs and filter out required refs with the help
1909 * of filter_ref_kind().
1911 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1912 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1913 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1914 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1915 else if (filter
->kind
== FILTER_REFS_TAGS
)
1916 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1917 else if (filter
->kind
& FILTER_REFS_ALL
)
1918 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1919 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1920 head_ref(ref_filter_handler
, &ref_cbdata
);
1923 clear_contains_cache(&ref_cbdata
.contains_cache
);
1924 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
1926 /* Filters that need revision walking */
1927 if (filter
->merge_commit
)
1928 do_merge_filter(&ref_cbdata
);
1933 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1935 struct atom_value
*va
, *vb
;
1937 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
1938 int (*cmp_fn
)(const char *, const char *);
1940 get_ref_atom_value(a
, s
->atom
, &va
);
1941 get_ref_atom_value(b
, s
->atom
, &vb
);
1942 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
1944 cmp
= versioncmp(va
->s
, vb
->s
);
1945 else if (cmp_type
== FIELD_STR
)
1946 cmp
= cmp_fn(va
->s
, vb
->s
);
1948 if (va
->ul
< vb
->ul
)
1950 else if (va
->ul
== vb
->ul
)
1951 cmp
= cmp_fn(a
->refname
, b
->refname
);
1956 return (s
->reverse
) ? -cmp
: cmp
;
1959 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
1961 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1962 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1963 struct ref_sorting
*s
;
1965 for (s
= ref_sorting
; s
; s
= s
->next
) {
1966 int cmp
= cmp_ref_sorting(s
, a
, b
);
1973 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1975 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
1978 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1980 struct strbuf
*s
= &state
->stack
->output
;
1982 while (*cp
&& (!ep
|| cp
< ep
)) {
1987 int ch
= hex2chr(cp
+ 1);
1989 strbuf_addch(s
, ch
);
1995 strbuf_addch(s
, *cp
);
2000 void format_ref_array_item(struct ref_array_item
*info
, const char *format
,
2001 int quote_style
, struct strbuf
*final_buf
)
2003 const char *cp
, *sp
, *ep
;
2004 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2006 state
.quote_style
= quote_style
;
2007 push_stack_element(&state
.stack
);
2009 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2010 struct atom_value
*atomv
;
2012 ep
= strchr(sp
, ')');
2014 append_literal(cp
, sp
, &state
);
2015 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
2016 atomv
->handler(atomv
, &state
);
2019 sp
= cp
+ strlen(cp
);
2020 append_literal(cp
, sp
, &state
);
2022 if (need_color_reset_at_eol
) {
2023 struct atom_value resetv
;
2024 char color
[COLOR_MAXLEN
] = "";
2026 if (color_parse("reset", color
) < 0)
2027 die("BUG: couldn't parse 'reset' as a color");
2029 append_atom(&resetv
, &state
);
2031 if (state
.stack
->prev
)
2032 die(_("format: %%(end) atom missing"));
2033 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2034 pop_stack_element(&state
.stack
);
2037 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
2039 struct strbuf final_buf
= STRBUF_INIT
;
2041 format_ref_array_item(info
, format
, quote_style
, &final_buf
);
2042 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2043 strbuf_release(&final_buf
);
2047 void pretty_print_ref(const char *name
, const unsigned char *sha1
,
2050 struct ref_array_item
*ref_item
;
2051 ref_item
= new_ref_array_item(name
, sha1
, 0);
2052 ref_item
->kind
= ref_kind_from_refname(name
);
2053 show_ref_array_item(ref_item
, format
, 0);
2054 free_array_item(ref_item
);
2057 /* If no sorting option is given, use refname to sort as default */
2058 struct ref_sorting
*ref_default_sorting(void)
2060 static const char cstr_name
[] = "refname";
2062 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2064 sorting
->next
= NULL
;
2065 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
2069 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2071 struct ref_sorting
**sorting_tail
= opt
->value
;
2072 struct ref_sorting
*s
;
2075 if (!arg
) /* should --no-sort void the list ? */
2078 s
= xcalloc(1, sizeof(*s
));
2079 s
->next
= *sorting_tail
;
2086 if (skip_prefix(arg
, "version:", &arg
) ||
2087 skip_prefix(arg
, "v:", &arg
))
2090 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
2094 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2096 struct ref_filter
*rf
= opt
->value
;
2097 unsigned char sha1
[20];
2098 int no_merged
= starts_with(opt
->long_name
, "no");
2102 return opterror(opt
, "is incompatible with --merged", 0);
2104 return opterror(opt
, "is incompatible with --no-merged", 0);
2108 rf
->merge
= no_merged
2109 ? REF_FILTER_MERGED_OMIT
2110 : REF_FILTER_MERGED_INCLUDE
;
2112 if (get_sha1(arg
, sha1
))
2113 die(_("malformed object name %s"), arg
);
2115 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
2116 if (!rf
->merge_commit
)
2117 return opterror(opt
, "must point to a commit", 0);