3 #include "parse-options.h"
6 #include "object-store.h"
7 #include "repository.h"
13 #include "ref-filter.h"
16 #include "git-compat-util.h"
19 #include "wt-status.h"
20 #include "commit-slab.h"
21 #include "commit-graph.h"
22 #include "commit-reach.h"
24 static struct ref_msg
{
28 const char *ahead_behind
;
30 /* Untranslated plumbing messages: */
37 void setup_ref_filter_porcelain_msg(void)
39 msgs
.gone
= _("gone");
40 msgs
.ahead
= _("ahead %d");
41 msgs
.behind
= _("behind %d");
42 msgs
.ahead_behind
= _("ahead %d, behind %d");
45 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
46 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
54 cmp_status cmp_status
;
56 unsigned int then_atom_seen
: 1,
58 condition_satisfied
: 1;
62 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
67 * An atom is a valid field atom listed below, possibly prefixed with
68 * a "*" to denote deref_tag().
70 * We parse given format string and sort specifiers, and make a list
71 * of properties that we need to extract out of objects. ref_array_item
72 * structure will hold an array of values extracted that can be
73 * indexed with the "atom number", which is an index into this
76 static struct used_atom
{
80 char color
[COLOR_MAXLEN
];
84 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
86 struct refname_atom refname
;
87 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
90 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
91 struct process_trailer_options trailer_opts
;
95 cmp_status cmp_status
;
99 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
102 struct refname_atom refname
;
106 static int used_atom_cnt
, need_tagged
, need_symref
;
109 * Expand string, append it to strbuf *sb, then return error code ret.
110 * Allow to save few lines of code.
112 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
116 strbuf_vaddf(sb
, fmt
, ap
);
121 static int color_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
122 const char *color_value
, struct strbuf
*err
)
125 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
126 if (color_parse(color_value
, atom
->u
.color
) < 0)
127 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
130 * We check this after we've parsed the color, which lets us complain
131 * about syntactically bogus color names even if they won't be used.
133 if (!want_color(format
->use_color
))
134 color_parse("", atom
->u
.color
);
138 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
139 const char *name
, struct strbuf
*err
)
142 atom
->option
= R_NORMAL
;
143 else if (!strcmp(arg
, "short"))
144 atom
->option
= R_SHORT
;
145 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
146 skip_prefix(arg
, "strip=", &arg
)) {
147 atom
->option
= R_LSTRIP
;
148 if (strtol_i(arg
, 10, &atom
->lstrip
))
149 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
150 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
151 atom
->option
= R_RSTRIP
;
152 if (strtol_i(arg
, 10, &atom
->rstrip
))
153 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
155 return strbuf_addf_ret(err
, -1, _("unrecognized %%(%s) argument: %s"), name
, arg
);
159 static int remote_ref_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
160 const char *arg
, struct strbuf
*err
)
162 struct string_list params
= STRING_LIST_INIT_DUP
;
165 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
166 atom
->u
.remote_ref
.push
= 1;
169 atom
->u
.remote_ref
.option
= RR_REF
;
170 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
171 arg
, atom
->name
, err
);
174 atom
->u
.remote_ref
.nobracket
= 0;
175 string_list_split(¶ms
, arg
, ',', -1);
177 for (i
= 0; i
< params
.nr
; i
++) {
178 const char *s
= params
.items
[i
].string
;
180 if (!strcmp(s
, "track"))
181 atom
->u
.remote_ref
.option
= RR_TRACK
;
182 else if (!strcmp(s
, "trackshort"))
183 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
184 else if (!strcmp(s
, "nobracket"))
185 atom
->u
.remote_ref
.nobracket
= 1;
186 else if (!strcmp(s
, "remotename")) {
187 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
188 atom
->u
.remote_ref
.push_remote
= 1;
189 } else if (!strcmp(s
, "remoteref")) {
190 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
191 atom
->u
.remote_ref
.push_remote
= 1;
193 atom
->u
.remote_ref
.option
= RR_REF
;
194 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
195 arg
, atom
->name
, err
)) {
196 string_list_clear(¶ms
, 0);
202 string_list_clear(¶ms
, 0);
206 static int body_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
207 const char *arg
, struct strbuf
*err
)
210 return strbuf_addf_ret(err
, -1, _("%%(body) does not take arguments"));
211 atom
->u
.contents
.option
= C_BODY_DEP
;
215 static int subject_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
216 const char *arg
, struct strbuf
*err
)
219 return strbuf_addf_ret(err
, -1, _("%%(subject) does not take arguments"));
220 atom
->u
.contents
.option
= C_SUB
;
224 static int trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
225 const char *arg
, struct strbuf
*err
)
227 struct string_list params
= STRING_LIST_INIT_DUP
;
231 string_list_split(¶ms
, arg
, ',', -1);
232 for (i
= 0; i
< params
.nr
; i
++) {
233 const char *s
= params
.items
[i
].string
;
234 if (!strcmp(s
, "unfold"))
235 atom
->u
.contents
.trailer_opts
.unfold
= 1;
236 else if (!strcmp(s
, "only"))
237 atom
->u
.contents
.trailer_opts
.only_trailers
= 1;
239 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), s
);
240 string_list_clear(¶ms
, 0);
245 atom
->u
.contents
.option
= C_TRAILERS
;
246 string_list_clear(¶ms
, 0);
250 static int contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
251 const char *arg
, struct strbuf
*err
)
254 atom
->u
.contents
.option
= C_BARE
;
255 else if (!strcmp(arg
, "body"))
256 atom
->u
.contents
.option
= C_BODY
;
257 else if (!strcmp(arg
, "signature"))
258 atom
->u
.contents
.option
= C_SIG
;
259 else if (!strcmp(arg
, "subject"))
260 atom
->u
.contents
.option
= C_SUB
;
261 else if (skip_prefix(arg
, "trailers", &arg
)) {
262 skip_prefix(arg
, ":", &arg
);
263 if (trailers_atom_parser(format
, atom
, *arg
? arg
: NULL
, err
))
265 } else if (skip_prefix(arg
, "lines=", &arg
)) {
266 atom
->u
.contents
.option
= C_LINES
;
267 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
268 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
270 return strbuf_addf_ret(err
, -1, _("unrecognized %%(contents) argument: %s"), arg
);
274 static int objectname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
275 const char *arg
, struct strbuf
*err
)
278 atom
->u
.objectname
.option
= O_FULL
;
279 else if (!strcmp(arg
, "short"))
280 atom
->u
.objectname
.option
= O_SHORT
;
281 else if (skip_prefix(arg
, "short=", &arg
)) {
282 atom
->u
.objectname
.option
= O_LENGTH
;
283 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
284 atom
->u
.objectname
.length
== 0)
285 return strbuf_addf_ret(err
, -1, _("positive value expected objectname:short=%s"), arg
);
286 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
287 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
289 return strbuf_addf_ret(err
, -1, _("unrecognized %%(objectname) argument: %s"), arg
);
293 static int refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
294 const char *arg
, struct strbuf
*err
)
296 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
299 static align_type
parse_align_position(const char *s
)
301 if (!strcmp(s
, "right"))
303 else if (!strcmp(s
, "middle"))
305 else if (!strcmp(s
, "left"))
310 static int align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
311 const char *arg
, struct strbuf
*err
)
313 struct align
*align
= &atom
->u
.align
;
314 struct string_list params
= STRING_LIST_INIT_DUP
;
316 unsigned int width
= ~0U;
319 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
321 align
->position
= ALIGN_LEFT
;
323 string_list_split(¶ms
, arg
, ',', -1);
324 for (i
= 0; i
< params
.nr
; i
++) {
325 const char *s
= params
.items
[i
].string
;
328 if (skip_prefix(s
, "position=", &s
)) {
329 position
= parse_align_position(s
);
331 strbuf_addf(err
, _("unrecognized position:%s"), s
);
332 string_list_clear(¶ms
, 0);
335 align
->position
= position
;
336 } else if (skip_prefix(s
, "width=", &s
)) {
337 if (strtoul_ui(s
, 10, &width
)) {
338 strbuf_addf(err
, _("unrecognized width:%s"), s
);
339 string_list_clear(¶ms
, 0);
342 } else if (!strtoul_ui(s
, 10, &width
))
344 else if ((position
= parse_align_position(s
)) >= 0)
345 align
->position
= position
;
347 strbuf_addf(err
, _("unrecognized %%(align) argument: %s"), s
);
348 string_list_clear(¶ms
, 0);
354 string_list_clear(¶ms
, 0);
355 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
357 align
->width
= width
;
358 string_list_clear(¶ms
, 0);
362 static int if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
363 const char *arg
, struct strbuf
*err
)
366 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
368 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
369 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
370 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
371 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
373 return strbuf_addf_ret(err
, -1, _("unrecognized %%(if) argument: %s"), arg
);
377 static int head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
378 const char *arg
, struct strbuf
*unused_err
)
380 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
387 int (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
,
388 const char *arg
, struct strbuf
*err
);
390 { "refname" , FIELD_STR
, refname_atom_parser
},
392 { "objectsize", FIELD_ULONG
},
393 { "objectname", FIELD_STR
, objectname_atom_parser
},
396 { "numparent", FIELD_ULONG
},
403 { "authordate", FIELD_TIME
},
406 { "committeremail" },
407 { "committerdate", FIELD_TIME
},
411 { "taggerdate", FIELD_TIME
},
413 { "creatordate", FIELD_TIME
},
414 { "subject", FIELD_STR
, subject_atom_parser
},
415 { "body", FIELD_STR
, body_atom_parser
},
416 { "trailers", FIELD_STR
, trailers_atom_parser
},
417 { "contents", FIELD_STR
, contents_atom_parser
},
418 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
419 { "push", FIELD_STR
, remote_ref_atom_parser
},
420 { "symref", FIELD_STR
, refname_atom_parser
},
422 { "HEAD", FIELD_STR
, head_atom_parser
},
423 { "color", FIELD_STR
, color_atom_parser
},
424 { "align", FIELD_STR
, align_atom_parser
},
426 { "if", FIELD_STR
, if_atom_parser
},
431 #define REF_FORMATTING_STATE_INIT { 0, NULL }
433 struct ref_formatting_stack
{
434 struct ref_formatting_stack
*prev
;
435 struct strbuf output
;
436 void (*at_end
)(struct ref_formatting_stack
**stack
);
440 struct ref_formatting_state
{
442 struct ref_formatting_stack
*stack
;
447 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
449 uintmax_t value
; /* used for sorting when not FIELD_STR */
450 struct used_atom
*atom
;
454 * Used to parse format string and sort specifiers
456 static int parse_ref_filter_atom(const struct ref_format
*format
,
457 const char *atom
, const char *ep
,
465 if (*sp
== '*' && sp
< ep
)
468 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
469 (int)(ep
-atom
), atom
);
471 /* Do we have the atom already used elsewhere? */
472 for (i
= 0; i
< used_atom_cnt
; i
++) {
473 int len
= strlen(used_atom
[i
].name
);
474 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
479 * If the atom name has a colon, strip it and everything after
480 * it off - it specifies the format for this entry, and
481 * shouldn't be used for checking against the valid_atom
484 arg
= memchr(sp
, ':', ep
- sp
);
485 atom_len
= (arg
? arg
: ep
) - sp
;
487 /* Is the atom a valid one? */
488 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
489 int len
= strlen(valid_atom
[i
].name
);
490 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
494 if (ARRAY_SIZE(valid_atom
) <= i
)
495 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
496 (int)(ep
-atom
), atom
);
498 /* Add it in, including the deref prefix */
501 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
502 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
503 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
505 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
508 * Treat empty sub-arguments list as NULL (i.e.,
509 * "%(atom:)" is equivalent to "%(atom)").
514 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
515 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
519 if (!strcmp(valid_atom
[i
].name
, "symref"))
524 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
526 switch (quote_style
) {
528 strbuf_addstr(s
, str
);
531 sq_quote_buf(s
, str
);
534 perl_quote_buf(s
, str
);
537 python_quote_buf(s
, str
);
540 tcl_quote_buf(s
, str
);
545 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
546 struct strbuf
*unused_err
)
549 * Quote formatting is only done when the stack has a single
550 * element. Otherwise quote formatting is done on the
551 * element's entire output strbuf when the %(end) atom is
554 if (!state
->stack
->prev
)
555 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
557 strbuf_addstr(&state
->stack
->output
, v
->s
);
561 static void push_stack_element(struct ref_formatting_stack
**stack
)
563 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
565 strbuf_init(&s
->output
, 0);
570 static void pop_stack_element(struct ref_formatting_stack
**stack
)
572 struct ref_formatting_stack
*current
= *stack
;
573 struct ref_formatting_stack
*prev
= current
->prev
;
576 strbuf_addbuf(&prev
->output
, ¤t
->output
);
577 strbuf_release(¤t
->output
);
582 static void end_align_handler(struct ref_formatting_stack
**stack
)
584 struct ref_formatting_stack
*cur
= *stack
;
585 struct align
*align
= (struct align
*)cur
->at_end_data
;
586 struct strbuf s
= STRBUF_INIT
;
588 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
589 strbuf_swap(&cur
->output
, &s
);
593 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
594 struct strbuf
*unused_err
)
596 struct ref_formatting_stack
*new_stack
;
598 push_stack_element(&state
->stack
);
599 new_stack
= state
->stack
;
600 new_stack
->at_end
= end_align_handler
;
601 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
605 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
607 struct ref_formatting_stack
*cur
= *stack
;
608 struct ref_formatting_stack
*prev
= cur
->prev
;
609 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
611 if (!if_then_else
->then_atom_seen
)
612 die(_("format: %%(if) atom used without a %%(then) atom"));
614 if (if_then_else
->else_atom_seen
) {
616 * There is an %(else) atom: we need to drop one state from the
617 * stack, either the %(else) branch if the condition is satisfied, or
618 * the %(then) branch if it isn't.
620 if (if_then_else
->condition_satisfied
) {
621 strbuf_reset(&cur
->output
);
622 pop_stack_element(&cur
);
624 strbuf_swap(&cur
->output
, &prev
->output
);
625 strbuf_reset(&cur
->output
);
626 pop_stack_element(&cur
);
628 } else if (!if_then_else
->condition_satisfied
) {
630 * No %(else) atom: just drop the %(then) branch if the
631 * condition is not satisfied.
633 strbuf_reset(&cur
->output
);
640 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
641 struct strbuf
*unused_err
)
643 struct ref_formatting_stack
*new_stack
;
644 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
646 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
647 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
649 push_stack_element(&state
->stack
);
650 new_stack
= state
->stack
;
651 new_stack
->at_end
= if_then_else_handler
;
652 new_stack
->at_end_data
= if_then_else
;
656 static int is_empty(const char *s
)
666 static int then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
669 struct ref_formatting_stack
*cur
= state
->stack
;
670 struct if_then_else
*if_then_else
= NULL
;
672 if (cur
->at_end
== if_then_else_handler
)
673 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
675 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used without an %%(if) atom"));
676 if (if_then_else
->then_atom_seen
)
677 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
678 if (if_then_else
->else_atom_seen
)
679 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
680 if_then_else
->then_atom_seen
= 1;
682 * If the 'equals' or 'notequals' attribute is used then
683 * perform the required comparison. If not, only non-empty
684 * strings satisfy the 'if' condition.
686 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
687 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
688 if_then_else
->condition_satisfied
= 1;
689 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
690 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
691 if_then_else
->condition_satisfied
= 1;
692 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
693 if_then_else
->condition_satisfied
= 1;
694 strbuf_reset(&cur
->output
);
698 static int else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
701 struct ref_formatting_stack
*prev
= state
->stack
;
702 struct if_then_else
*if_then_else
= NULL
;
704 if (prev
->at_end
== if_then_else_handler
)
705 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
707 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without an %%(if) atom"));
708 if (!if_then_else
->then_atom_seen
)
709 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without a %%(then) atom"));
710 if (if_then_else
->else_atom_seen
)
711 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
712 if_then_else
->else_atom_seen
= 1;
713 push_stack_element(&state
->stack
);
714 state
->stack
->at_end_data
= prev
->at_end_data
;
715 state
->stack
->at_end
= prev
->at_end
;
719 static int end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
722 struct ref_formatting_stack
*current
= state
->stack
;
723 struct strbuf s
= STRBUF_INIT
;
725 if (!current
->at_end
)
726 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
727 current
->at_end(&state
->stack
);
729 /* Stack may have been popped within at_end(), hence reset the current pointer */
730 current
= state
->stack
;
733 * Perform quote formatting when the stack element is that of
734 * a supporting atom. If nested then perform quote formatting
735 * only on the topmost supporting atom.
737 if (!current
->prev
->prev
) {
738 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
739 strbuf_swap(¤t
->output
, &s
);
742 pop_stack_element(&state
->stack
);
747 * In a format string, find the next occurrence of %(atom).
749 static const char *find_next(const char *cp
)
754 * %( is the start of an atom;
755 * %% is a quoted per-cent.
759 else if (cp
[1] == '%')
760 cp
++; /* skip over two % */
761 /* otherwise this is a singleton, literal % */
769 * Make sure the format string is well formed, and parse out
772 int verify_ref_format(struct ref_format
*format
)
776 format
->need_color_reset_at_eol
= 0;
777 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
778 struct strbuf err
= STRBUF_INIT
;
779 const char *color
, *ep
= strchr(sp
, ')');
783 return error(_("malformed format string %s"), sp
);
784 /* sp points at "%(" and ep points at the closing ")" */
785 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
790 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
791 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
792 strbuf_release(&err
);
794 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
795 format
->need_color_reset_at_eol
= 0;
800 * Given an object name, read the object data and size, and return a
801 * "struct object". If the object data we are returning is also borrowed
802 * by the "struct object" representation, set *eaten as well---it is a
803 * signal from parse_object_buffer to us not to free the buffer.
805 static void *get_obj(const struct object_id
*oid
, struct object
**obj
, unsigned long *sz
, int *eaten
)
807 enum object_type type
;
808 void *buf
= read_object_file(oid
, &type
, sz
);
811 *obj
= parse_object_buffer(the_repository
, oid
, type
, *sz
,
818 static int grab_objectname(const char *name
, const struct object_id
*oid
,
819 struct atom_value
*v
, struct used_atom
*atom
)
821 if (starts_with(name
, "objectname")) {
822 if (atom
->u
.objectname
.option
== O_SHORT
) {
823 v
->s
= xstrdup(find_unique_abbrev(oid
, DEFAULT_ABBREV
));
825 } else if (atom
->u
.objectname
.option
== O_FULL
) {
826 v
->s
= xstrdup(oid_to_hex(oid
));
828 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
829 v
->s
= xstrdup(find_unique_abbrev(oid
, atom
->u
.objectname
.length
));
832 BUG("unknown %%(objectname) option");
837 /* See grab_values */
838 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
842 for (i
= 0; i
< used_atom_cnt
; i
++) {
843 const char *name
= used_atom
[i
].name
;
844 struct atom_value
*v
= &val
[i
];
845 if (!!deref
!= (*name
== '*'))
849 if (!strcmp(name
, "objecttype"))
850 v
->s
= type_name(obj
->type
);
851 else if (!strcmp(name
, "objectsize")) {
853 v
->s
= xstrfmt("%lu", sz
);
856 grab_objectname(name
, &obj
->oid
, v
, &used_atom
[i
]);
860 /* See grab_values */
861 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
864 struct tag
*tag
= (struct tag
*) obj
;
866 for (i
= 0; i
< used_atom_cnt
; i
++) {
867 const char *name
= used_atom
[i
].name
;
868 struct atom_value
*v
= &val
[i
];
869 if (!!deref
!= (*name
== '*'))
873 if (!strcmp(name
, "tag"))
875 else if (!strcmp(name
, "type") && tag
->tagged
)
876 v
->s
= type_name(tag
->tagged
->type
);
877 else if (!strcmp(name
, "object") && tag
->tagged
)
878 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
882 /* See grab_values */
883 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
886 struct commit
*commit
= (struct commit
*) obj
;
888 for (i
= 0; i
< used_atom_cnt
; i
++) {
889 const char *name
= used_atom
[i
].name
;
890 struct atom_value
*v
= &val
[i
];
891 if (!!deref
!= (*name
== '*'))
895 if (!strcmp(name
, "tree")) {
896 v
->s
= xstrdup(oid_to_hex(get_commit_tree_oid(commit
)));
898 else if (!strcmp(name
, "numparent")) {
899 v
->value
= commit_list_count(commit
->parents
);
900 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
902 else if (!strcmp(name
, "parent")) {
903 struct commit_list
*parents
;
904 struct strbuf s
= STRBUF_INIT
;
905 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
906 struct commit
*parent
= parents
->item
;
907 if (parents
!= commit
->parents
)
908 strbuf_addch(&s
, ' ');
909 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
911 v
->s
= strbuf_detach(&s
, NULL
);
916 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
920 if (!strncmp(buf
, who
, wholen
) &&
922 return buf
+ wholen
+ 1;
923 eol
= strchr(buf
, '\n');
928 return ""; /* end of header */
934 static const char *copy_line(const char *buf
)
936 const char *eol
= strchrnul(buf
, '\n');
937 return xmemdupz(buf
, eol
- buf
);
940 static const char *copy_name(const char *buf
)
943 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
944 if (!strncmp(cp
, " <", 2))
945 return xmemdupz(buf
, cp
- buf
);
950 static const char *copy_email(const char *buf
)
952 const char *email
= strchr(buf
, '<');
956 eoemail
= strchr(email
, '>');
959 return xmemdupz(email
, eoemail
+ 1 - email
);
962 static char *copy_subject(const char *buf
, unsigned long len
)
964 char *r
= xmemdupz(buf
, len
);
967 for (i
= 0; i
< len
; i
++)
974 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
976 const char *eoemail
= strstr(buf
, "> ");
978 timestamp_t timestamp
;
980 struct date_mode date_mode
= { DATE_NORMAL
};
984 * We got here because atomname ends in "date" or "date<something>";
985 * it's not possible that <something> is not ":<format>" because
986 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
987 * ":" means no format is specified, and use the default.
989 formatp
= strchr(atomname
, ':');
990 if (formatp
!= NULL
) {
992 parse_date_format(formatp
, &date_mode
);
997 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
998 if (timestamp
== TIME_MAX
)
1000 tz
= strtol(zone
, NULL
, 10);
1001 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1003 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1004 v
->value
= timestamp
;
1011 /* See grab_values */
1012 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1015 int wholen
= strlen(who
);
1016 const char *wholine
= NULL
;
1018 for (i
= 0; i
< used_atom_cnt
; i
++) {
1019 const char *name
= used_atom
[i
].name
;
1020 struct atom_value
*v
= &val
[i
];
1021 if (!!deref
!= (*name
== '*'))
1025 if (strncmp(who
, name
, wholen
))
1027 if (name
[wholen
] != 0 &&
1028 strcmp(name
+ wholen
, "name") &&
1029 strcmp(name
+ wholen
, "email") &&
1030 !starts_with(name
+ wholen
, "date"))
1033 wholine
= find_wholine(who
, wholen
, buf
, sz
);
1035 return; /* no point looking for it */
1036 if (name
[wholen
] == 0)
1037 v
->s
= copy_line(wholine
);
1038 else if (!strcmp(name
+ wholen
, "name"))
1039 v
->s
= copy_name(wholine
);
1040 else if (!strcmp(name
+ wholen
, "email"))
1041 v
->s
= copy_email(wholine
);
1042 else if (starts_with(name
+ wholen
, "date"))
1043 grab_date(wholine
, v
, name
);
1047 * For a tag or a commit object, if "creator" or "creatordate" is
1048 * requested, do something special.
1050 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1051 return; /* "author" for commit object is not wanted */
1053 wholine
= find_wholine(who
, wholen
, buf
, sz
);
1056 for (i
= 0; i
< used_atom_cnt
; i
++) {
1057 const char *name
= used_atom
[i
].name
;
1058 struct atom_value
*v
= &val
[i
];
1059 if (!!deref
!= (*name
== '*'))
1064 if (starts_with(name
, "creatordate"))
1065 grab_date(wholine
, v
, name
);
1066 else if (!strcmp(name
, "creator"))
1067 v
->s
= copy_line(wholine
);
1071 static void find_subpos(const char *buf
, unsigned long sz
,
1072 const char **sub
, unsigned long *sublen
,
1073 const char **body
, unsigned long *bodylen
,
1074 unsigned long *nonsiglen
,
1075 const char **sig
, unsigned long *siglen
)
1078 /* skip past header until we hit empty line */
1079 while (*buf
&& *buf
!= '\n') {
1080 eol
= strchrnul(buf
, '\n');
1085 /* skip any empty lines */
1086 while (*buf
== '\n')
1089 /* parse signature first; we might not even have a subject line */
1090 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
1091 *siglen
= strlen(*sig
);
1093 /* subject is first non-empty line */
1095 /* subject goes to first empty line */
1096 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
1097 eol
= strchrnul(buf
, '\n');
1102 *sublen
= buf
- *sub
;
1103 /* drop trailing newline, if present */
1104 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1107 /* skip any empty lines */
1108 while (*buf
== '\n')
1111 *bodylen
= strlen(buf
);
1112 *nonsiglen
= *sig
- buf
;
1116 * If 'lines' is greater than 0, append that many lines from the given
1117 * 'buf' of length 'size' to the given strbuf.
1119 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1122 const char *sp
, *eol
;
1127 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1129 strbuf_addstr(out
, "\n ");
1130 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1131 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1132 strbuf_add(out
, sp
, len
);
1139 /* See grab_values */
1140 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1143 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1144 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1146 for (i
= 0; i
< used_atom_cnt
; i
++) {
1147 struct used_atom
*atom
= &used_atom
[i
];
1148 const char *name
= atom
->name
;
1149 struct atom_value
*v
= &val
[i
];
1150 if (!!deref
!= (*name
== '*'))
1154 if (strcmp(name
, "subject") &&
1155 strcmp(name
, "body") &&
1156 !starts_with(name
, "trailers") &&
1157 !starts_with(name
, "contents"))
1160 find_subpos(buf
, sz
,
1162 &bodypos
, &bodylen
, &nonsiglen
,
1165 if (atom
->u
.contents
.option
== C_SUB
)
1166 v
->s
= copy_subject(subpos
, sublen
);
1167 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1168 v
->s
= xmemdupz(bodypos
, bodylen
);
1169 else if (atom
->u
.contents
.option
== C_BODY
)
1170 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1171 else if (atom
->u
.contents
.option
== C_SIG
)
1172 v
->s
= xmemdupz(sigpos
, siglen
);
1173 else if (atom
->u
.contents
.option
== C_LINES
) {
1174 struct strbuf s
= STRBUF_INIT
;
1175 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1177 /* Size is the length of the message after removing the signature */
1178 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1179 v
->s
= strbuf_detach(&s
, NULL
);
1180 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1181 struct strbuf s
= STRBUF_INIT
;
1183 /* Format the trailer info according to the trailer_opts given */
1184 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1186 v
->s
= strbuf_detach(&s
, NULL
);
1187 } else if (atom
->u
.contents
.option
== C_BARE
)
1188 v
->s
= xstrdup(subpos
);
1193 * We want to have empty print-string for field requests
1194 * that do not apply (e.g. "authordate" for a tag object)
1196 static void fill_missing_values(struct atom_value
*val
)
1199 for (i
= 0; i
< used_atom_cnt
; i
++) {
1200 struct atom_value
*v
= &val
[i
];
1207 * val is a list of atom_value to hold returned values. Extract
1208 * the values for atoms in used_atom array out of (obj, buf, sz).
1209 * when deref is false, (obj, buf, sz) is the object that is
1210 * pointed at by the ref itself; otherwise it is the object the
1211 * ref (which is a tag) refers to.
1213 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1215 grab_common_values(val
, deref
, obj
, buf
, sz
);
1216 switch (obj
->type
) {
1218 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1219 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1220 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1223 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1224 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1225 grab_person("author", val
, deref
, obj
, buf
, sz
);
1226 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1229 /* grab_tree_values(val, deref, obj, buf, sz); */
1232 /* grab_blob_values(val, deref, obj, buf, sz); */
1235 die("Eh? Object of type %d?", obj
->type
);
1239 static inline char *copy_advance(char *dst
, const char *src
)
1246 static const char *lstrip_ref_components(const char *refname
, int len
)
1248 long remaining
= len
;
1249 const char *start
= refname
;
1253 const char *p
= refname
;
1255 /* Find total no of '/' separated path-components */
1256 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1259 * The number of components we need to strip is now
1260 * the total minus the components to be left (Plus one
1261 * because we count the number of '/', but the number
1262 * of components is one more than the no of '/').
1264 remaining
= i
+ len
+ 1;
1267 while (remaining
> 0) {
1280 static const char *rstrip_ref_components(const char *refname
, int len
)
1282 long remaining
= len
;
1283 char *start
= xstrdup(refname
);
1287 const char *p
= refname
;
1289 /* Find total no of '/' separated path-components */
1290 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1293 * The number of components we need to strip is now
1294 * the total minus the components to be left (Plus one
1295 * because we count the number of '/', but the number
1296 * of components is one more than the no of '/').
1298 remaining
= i
+ len
+ 1;
1301 while (remaining
-- > 0) {
1302 char *p
= strrchr(start
, '/');
1311 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1313 if (atom
->option
== R_SHORT
)
1314 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1315 else if (atom
->option
== R_LSTRIP
)
1316 return lstrip_ref_components(refname
, atom
->lstrip
);
1317 else if (atom
->option
== R_RSTRIP
)
1318 return rstrip_ref_components(refname
, atom
->rstrip
);
1323 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1324 struct branch
*branch
, const char **s
)
1326 int num_ours
, num_theirs
;
1327 if (atom
->u
.remote_ref
.option
== RR_REF
)
1328 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1329 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1330 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1331 NULL
, AHEAD_BEHIND_FULL
) < 0) {
1332 *s
= xstrdup(msgs
.gone
);
1333 } else if (!num_ours
&& !num_theirs
)
1336 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1337 else if (!num_theirs
)
1338 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1340 *s
= xstrfmt(msgs
.ahead_behind
,
1341 num_ours
, num_theirs
);
1342 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1343 const char *to_free
= *s
;
1344 *s
= xstrfmt("[%s]", *s
);
1345 free((void *)to_free
);
1347 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1348 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1349 NULL
, AHEAD_BEHIND_FULL
) < 0)
1352 if (!num_ours
&& !num_theirs
)
1356 else if (!num_theirs
)
1360 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1362 const char *remote
= atom
->u
.remote_ref
.push
?
1363 pushremote_for_branch(branch
, &explicit) :
1364 remote_for_branch(branch
, &explicit);
1366 *s
= xstrdup(remote
);
1369 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1373 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
,
1376 *s
= xstrdup(merge
);
1380 BUG("unhandled RR_* enum");
1383 char *get_head_description(void)
1385 struct strbuf desc
= STRBUF_INIT
;
1386 struct wt_status_state state
;
1387 memset(&state
, 0, sizeof(state
));
1388 wt_status_get_state(&state
, 1);
1389 if (state
.rebase_in_progress
||
1390 state
.rebase_interactive_in_progress
) {
1392 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1395 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
1396 state
.detached_from
);
1397 } else if (state
.bisect_in_progress
)
1398 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1400 else if (state
.detached_from
) {
1401 if (state
.detached_at
)
1403 * TRANSLATORS: make sure this matches "HEAD
1404 * detached at " in wt-status.c
1406 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1407 state
.detached_from
);
1410 * TRANSLATORS: make sure this matches "HEAD
1411 * detached from " in wt-status.c
1413 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1414 state
.detached_from
);
1417 strbuf_addstr(&desc
, _("(no branch)"));
1420 free(state
.detached_from
);
1421 return strbuf_detach(&desc
, NULL
);
1424 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1429 return show_ref(&atom
->u
.refname
, ref
->symref
);
1432 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1434 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1435 return get_head_description();
1436 return show_ref(&atom
->u
.refname
, ref
->refname
);
1439 static int get_object(struct ref_array_item
*ref
, const struct object_id
*oid
,
1440 int deref
, struct object
**obj
, struct strbuf
*err
)
1445 void *buf
= get_obj(oid
, obj
, &size
, &eaten
);
1447 ret
= strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1448 oid_to_hex(oid
), ref
->refname
);
1450 ret
= strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1451 oid_to_hex(oid
), ref
->refname
);
1453 grab_values(ref
->value
, deref
, *obj
, buf
, size
);
1460 * Parse the object referred by ref, and grab needed value.
1462 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
1466 const struct object_id
*tagged
;
1468 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1470 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1471 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1477 /* Fill in specials first */
1478 for (i
= 0; i
< used_atom_cnt
; i
++) {
1479 struct used_atom
*atom
= &used_atom
[i
];
1480 const char *name
= used_atom
[i
].name
;
1481 struct atom_value
*v
= &ref
->value
[i
];
1483 const char *refname
;
1484 struct branch
*branch
= NULL
;
1486 v
->handler
= append_atom
;
1494 if (starts_with(name
, "refname"))
1495 refname
= get_refname(atom
, ref
);
1496 else if (starts_with(name
, "symref"))
1497 refname
= get_symref(atom
, ref
);
1498 else if (starts_with(name
, "upstream")) {
1499 const char *branch_name
;
1500 /* only local branches may have an upstream */
1501 if (!skip_prefix(ref
->refname
, "refs/heads/",
1504 branch
= branch_get(branch_name
);
1506 refname
= branch_get_upstream(branch
, NULL
);
1508 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1510 } else if (atom
->u
.remote_ref
.push
) {
1511 const char *branch_name
;
1512 if (!skip_prefix(ref
->refname
, "refs/heads/",
1515 branch
= branch_get(branch_name
);
1517 if (atom
->u
.remote_ref
.push_remote
)
1520 refname
= branch_get_push(branch
, NULL
);
1524 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1526 } else if (starts_with(name
, "color:")) {
1527 v
->s
= atom
->u
.color
;
1529 } else if (!strcmp(name
, "flag")) {
1530 char buf
[256], *cp
= buf
;
1531 if (ref
->flag
& REF_ISSYMREF
)
1532 cp
= copy_advance(cp
, ",symref");
1533 if (ref
->flag
& REF_ISPACKED
)
1534 cp
= copy_advance(cp
, ",packed");
1539 v
->s
= xstrdup(buf
+ 1);
1542 } else if (!deref
&& grab_objectname(name
, &ref
->objectname
, v
, atom
)) {
1544 } else if (!strcmp(name
, "HEAD")) {
1545 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1550 } else if (starts_with(name
, "align")) {
1551 v
->handler
= align_atom_handler
;
1553 } else if (!strcmp(name
, "end")) {
1554 v
->handler
= end_atom_handler
;
1556 } else if (starts_with(name
, "if")) {
1559 if (skip_prefix(name
, "if:", &s
))
1561 v
->handler
= if_atom_handler
;
1563 } else if (!strcmp(name
, "then")) {
1564 v
->handler
= then_atom_handler
;
1566 } else if (!strcmp(name
, "else")) {
1567 v
->handler
= else_atom_handler
;
1575 v
->s
= xstrfmt("%s^{}", refname
);
1578 for (i
= 0; i
< used_atom_cnt
; i
++) {
1579 struct atom_value
*v
= &ref
->value
[i
];
1583 if (used_atom_cnt
<= i
)
1586 if (get_object(ref
, &ref
->objectname
, 0, &obj
, err
))
1590 * If there is no atom that wants to know about tagged
1591 * object, we are done.
1593 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1597 * If it is a tag object, see if we use a value that derefs
1598 * the object, and if we do grab the object it refers to.
1600 tagged
= &((struct tag
*)obj
)->tagged
->oid
;
1603 * NEEDSWORK: This derefs tag only once, which
1604 * is good to deal with chains of trust, but
1605 * is not consistent with what deref_tag() does
1606 * which peels the onion to the core.
1608 return get_object(ref
, tagged
, 1, &obj
, err
);
1612 * Given a ref, return the value for the atom. This lazily gets value
1613 * out of the object by calling populate value.
1615 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
1616 struct atom_value
**v
, struct strbuf
*err
)
1619 if (populate_value(ref
, err
))
1621 fill_missing_values(ref
->value
);
1623 *v
= &ref
->value
[atom
];
1628 * Return 1 if the refname matches one of the patterns, otherwise 0.
1629 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1630 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1631 * matches "refs/heads/mas*", too).
1633 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1635 const char **patterns
= filter
->name_patterns
;
1638 if (filter
->ignore_case
)
1639 flags
|= WM_CASEFOLD
;
1642 * When no '--format' option is given we need to skip the prefix
1643 * for matching refs of tags and branches.
1645 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1646 skip_prefix(refname
, "refs/heads/", &refname
) ||
1647 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1648 skip_prefix(refname
, "refs/", &refname
));
1650 for (; *patterns
; patterns
++) {
1651 if (!wildmatch(*patterns
, refname
, flags
))
1658 * Return 1 if the refname matches one of the patterns, otherwise 0.
1659 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1660 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1661 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1663 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1665 const char **pattern
= filter
->name_patterns
;
1666 int namelen
= strlen(refname
);
1667 unsigned flags
= WM_PATHNAME
;
1669 if (filter
->ignore_case
)
1670 flags
|= WM_CASEFOLD
;
1672 for (; *pattern
; pattern
++) {
1673 const char *p
= *pattern
;
1674 int plen
= strlen(p
);
1676 if ((plen
<= namelen
) &&
1677 !strncmp(refname
, p
, plen
) &&
1678 (refname
[plen
] == '\0' ||
1679 refname
[plen
] == '/' ||
1682 if (!wildmatch(p
, refname
, WM_PATHNAME
))
1688 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1689 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1691 if (!*filter
->name_patterns
)
1692 return 1; /* No pattern always matches */
1693 if (filter
->match_as_path
)
1694 return match_name_as_path(filter
, refname
);
1695 return match_pattern(filter
, refname
);
1699 * Find the longest prefix of pattern we can pass to
1700 * `for_each_fullref_in()`, namely the part of pattern preceding the
1701 * first glob character. (Note that `for_each_fullref_in()` is
1702 * perfectly happy working with a prefix that doesn't end at a
1703 * pathname component boundary.)
1705 static void find_longest_prefix(struct strbuf
*out
, const char *pattern
)
1709 for (p
= pattern
; *p
&& !is_glob_special(*p
); p
++)
1712 strbuf_add(out
, pattern
, p
- pattern
);
1716 * This is the same as for_each_fullref_in(), but it tries to iterate
1717 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1718 * pattern match, so the callback still has to match each ref individually.
1720 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1725 struct strbuf prefix
= STRBUF_INIT
;
1728 if (!filter
->match_as_path
) {
1730 * in this case, the patterns are applied after
1731 * prefixes like "refs/heads/" etc. are stripped off,
1732 * so we have to look at everything:
1734 return for_each_fullref_in("", cb
, cb_data
, broken
);
1737 if (!filter
->name_patterns
[0]) {
1738 /* no patterns; we have to look at everything */
1739 return for_each_fullref_in("", cb
, cb_data
, broken
);
1742 if (filter
->name_patterns
[1]) {
1744 * multiple patterns; in theory this could still work as long
1745 * as the patterns are disjoint. We'd just make multiple calls
1746 * to for_each_ref(). But if they're not disjoint, we'd end up
1747 * reporting the same ref multiple times. So let's punt on that
1750 return for_each_fullref_in("", cb
, cb_data
, broken
);
1753 find_longest_prefix(&prefix
, filter
->name_patterns
[0]);
1755 ret
= for_each_fullref_in(prefix
.buf
, cb
, cb_data
, broken
);
1756 strbuf_release(&prefix
);
1761 * Given a ref (sha1, refname), check if the ref belongs to the array
1762 * of sha1s. If the given ref is a tag, check if the given tag points
1763 * at one of the sha1s in the given sha1 array.
1764 * the given sha1_array.
1766 * 1. Only a single level of inderection is obtained, we might want to
1767 * change this to account for multiple levels (e.g. annotated tags
1768 * pointing to annotated tags pointing to a commit.)
1769 * 2. As the refs are cached we might know what refname peels to without
1770 * the need to parse the object via parse_object(). peel_ref() might be a
1771 * more efficient alternative to obtain the pointee.
1773 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1774 const struct object_id
*oid
,
1775 const char *refname
)
1777 const struct object_id
*tagged_oid
= NULL
;
1780 if (oid_array_lookup(points_at
, oid
) >= 0)
1782 obj
= parse_object(the_repository
, oid
);
1784 die(_("malformed object at '%s'"), refname
);
1785 if (obj
->type
== OBJ_TAG
)
1786 tagged_oid
= &((struct tag
*)obj
)->tagged
->oid
;
1787 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
1793 * Allocate space for a new ref_array_item and copy the name and oid to it.
1795 * Callers can then fill in other struct members at their leisure.
1797 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1798 const struct object_id
*oid
)
1800 struct ref_array_item
*ref
;
1802 FLEX_ALLOC_STR(ref
, refname
, refname
);
1803 oidcpy(&ref
->objectname
, oid
);
1808 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
1809 const char *refname
,
1810 const struct object_id
*oid
)
1812 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
1814 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
1815 array
->items
[array
->nr
++] = ref
;
1820 static int ref_kind_from_refname(const char *refname
)
1828 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1829 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1830 { "refs/tags/", FILTER_REFS_TAGS
}
1833 if (!strcmp(refname
, "HEAD"))
1834 return FILTER_REFS_DETACHED_HEAD
;
1836 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1837 if (starts_with(refname
, ref_kind
[i
].prefix
))
1838 return ref_kind
[i
].kind
;
1841 return FILTER_REFS_OTHERS
;
1844 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1846 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1847 filter
->kind
== FILTER_REFS_REMOTES
||
1848 filter
->kind
== FILTER_REFS_TAGS
)
1849 return filter
->kind
;
1850 return ref_kind_from_refname(refname
);
1853 struct ref_filter_cbdata
{
1854 struct ref_array
*array
;
1855 struct ref_filter
*filter
;
1856 struct contains_cache contains_cache
;
1857 struct contains_cache no_contains_cache
;
1861 * A call-back given to for_each_ref(). Filter refs and keep them for
1862 * later object processing.
1864 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1866 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1867 struct ref_filter
*filter
= ref_cbdata
->filter
;
1868 struct ref_array_item
*ref
;
1869 struct commit
*commit
= NULL
;
1872 if (flag
& REF_BAD_NAME
) {
1873 warning(_("ignoring ref with broken name %s"), refname
);
1877 if (flag
& REF_ISBROKEN
) {
1878 warning(_("ignoring broken ref %s"), refname
);
1882 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1883 kind
= filter_ref_kind(filter
, refname
);
1884 if (!(kind
& filter
->kind
))
1887 if (!filter_pattern_match(filter
, refname
))
1890 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
1894 * A merge filter is applied on refs pointing to commits. Hence
1895 * obtain the commit using the 'oid' available and discard all
1896 * non-commits early. The actual filtering is done later.
1898 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
1899 commit
= lookup_commit_reference_gently(the_repository
, oid
,
1903 /* We perform the filtering for the '--contains' option... */
1904 if (filter
->with_commit
&&
1905 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
1907 /* ...or for the `--no-contains' option */
1908 if (filter
->no_commit
&&
1909 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
1914 * We do not open the object yet; sort may only need refname
1915 * to do its job and the resulting list may yet to be pruned
1916 * by maxcount logic.
1918 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
1919 ref
->commit
= commit
;
1926 /* Free memory allocated for a ref_array_item */
1927 static void free_array_item(struct ref_array_item
*item
)
1929 free((char *)item
->symref
);
1933 /* Free all memory allocated for ref_array */
1934 void ref_array_clear(struct ref_array
*array
)
1938 for (i
= 0; i
< array
->nr
; i
++)
1939 free_array_item(array
->items
[i
]);
1940 FREE_AND_NULL(array
->items
);
1941 array
->nr
= array
->alloc
= 0;
1944 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1946 struct rev_info revs
;
1948 struct ref_filter
*filter
= ref_cbdata
->filter
;
1949 struct ref_array
*array
= ref_cbdata
->array
;
1950 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1952 init_revisions(&revs
, NULL
);
1954 for (i
= 0; i
< array
->nr
; i
++) {
1955 struct ref_array_item
*item
= array
->items
[i
];
1956 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1957 to_clear
[i
] = item
->commit
;
1960 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1961 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1964 if (prepare_revision_walk(&revs
))
1965 die(_("revision walk setup failed"));
1970 for (i
= 0; i
< old_nr
; i
++) {
1971 struct ref_array_item
*item
= array
->items
[i
];
1972 struct commit
*commit
= item
->commit
;
1974 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1976 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1977 array
->items
[array
->nr
++] = array
->items
[i
];
1979 free_array_item(item
);
1982 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
1983 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1988 * API for filtering a set of refs. Based on the type of refs the user
1989 * has requested, we iterate through those refs and apply filters
1990 * as per the given ref_filter structure and finally store the
1991 * filtered refs in the ref_array structure.
1993 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1995 struct ref_filter_cbdata ref_cbdata
;
1997 unsigned int broken
= 0;
1999 ref_cbdata
.array
= array
;
2000 ref_cbdata
.filter
= filter
;
2002 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
2004 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2006 init_contains_cache(&ref_cbdata
.contains_cache
);
2007 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2009 /* Simple per-ref filtering */
2011 die("filter_refs: invalid type");
2014 * For common cases where we need only branches or remotes or tags,
2015 * we only iterate through those refs. If a mix of refs is needed,
2016 * we iterate over all refs and filter out required refs with the help
2017 * of filter_ref_kind().
2019 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2020 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
2021 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2022 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2023 else if (filter
->kind
== FILTER_REFS_TAGS
)
2024 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2025 else if (filter
->kind
& FILTER_REFS_ALL
)
2026 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2027 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2028 head_ref(ref_filter_handler
, &ref_cbdata
);
2031 clear_contains_cache(&ref_cbdata
.contains_cache
);
2032 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2034 /* Filters that need revision walking */
2035 if (filter
->merge_commit
)
2036 do_merge_filter(&ref_cbdata
);
2041 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2043 struct atom_value
*va
, *vb
;
2045 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2046 int (*cmp_fn
)(const char *, const char *);
2047 struct strbuf err
= STRBUF_INIT
;
2049 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2051 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2053 strbuf_release(&err
);
2054 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2056 cmp
= versioncmp(va
->s
, vb
->s
);
2057 else if (cmp_type
== FIELD_STR
)
2058 cmp
= cmp_fn(va
->s
, vb
->s
);
2060 if (va
->value
< vb
->value
)
2062 else if (va
->value
== vb
->value
)
2063 cmp
= cmp_fn(a
->refname
, b
->refname
);
2068 return (s
->reverse
) ? -cmp
: cmp
;
2071 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2073 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2074 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2075 struct ref_sorting
*s
;
2077 for (s
= ref_sorting
; s
; s
= s
->next
) {
2078 int cmp
= cmp_ref_sorting(s
, a
, b
);
2085 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2087 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2090 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2092 struct strbuf
*s
= &state
->stack
->output
;
2094 while (*cp
&& (!ep
|| cp
< ep
)) {
2099 int ch
= hex2chr(cp
+ 1);
2101 strbuf_addch(s
, ch
);
2107 strbuf_addch(s
, *cp
);
2112 int format_ref_array_item(struct ref_array_item
*info
,
2113 const struct ref_format
*format
,
2114 struct strbuf
*final_buf
,
2115 struct strbuf
*error_buf
)
2117 const char *cp
, *sp
, *ep
;
2118 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2120 state
.quote_style
= format
->quote_style
;
2121 push_stack_element(&state
.stack
);
2123 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2124 struct atom_value
*atomv
;
2127 ep
= strchr(sp
, ')');
2129 append_literal(cp
, sp
, &state
);
2130 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2131 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2132 atomv
->handler(atomv
, &state
, error_buf
)) {
2133 pop_stack_element(&state
.stack
);
2138 sp
= cp
+ strlen(cp
);
2139 append_literal(cp
, sp
, &state
);
2141 if (format
->need_color_reset_at_eol
) {
2142 struct atom_value resetv
;
2143 resetv
.s
= GIT_COLOR_RESET
;
2144 if (append_atom(&resetv
, &state
, error_buf
)) {
2145 pop_stack_element(&state
.stack
);
2149 if (state
.stack
->prev
) {
2150 pop_stack_element(&state
.stack
);
2151 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2153 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2154 pop_stack_element(&state
.stack
);
2158 void show_ref_array_item(struct ref_array_item
*info
,
2159 const struct ref_format
*format
)
2161 struct strbuf final_buf
= STRBUF_INIT
;
2162 struct strbuf error_buf
= STRBUF_INIT
;
2164 if (format_ref_array_item(info
, format
, &final_buf
, &error_buf
))
2165 die("%s", error_buf
.buf
);
2166 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2167 strbuf_release(&error_buf
);
2168 strbuf_release(&final_buf
);
2172 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2173 const struct ref_format
*format
)
2175 struct ref_array_item
*ref_item
;
2176 ref_item
= new_ref_array_item(name
, oid
);
2177 ref_item
->kind
= ref_kind_from_refname(name
);
2178 show_ref_array_item(ref_item
, format
);
2179 free_array_item(ref_item
);
2182 static int parse_sorting_atom(const char *atom
)
2185 * This parses an atom using a dummy ref_format, since we don't
2186 * actually care about the formatting details.
2188 struct ref_format dummy
= REF_FORMAT_INIT
;
2189 const char *end
= atom
+ strlen(atom
);
2190 struct strbuf err
= STRBUF_INIT
;
2191 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2194 strbuf_release(&err
);
2198 /* If no sorting option is given, use refname to sort as default */
2199 struct ref_sorting
*ref_default_sorting(void)
2201 static const char cstr_name
[] = "refname";
2203 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2205 sorting
->next
= NULL
;
2206 sorting
->atom
= parse_sorting_atom(cstr_name
);
2210 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2212 struct ref_sorting
*s
;
2214 s
= xcalloc(1, sizeof(*s
));
2215 s
->next
= *sorting_tail
;
2222 if (skip_prefix(arg
, "version:", &arg
) ||
2223 skip_prefix(arg
, "v:", &arg
))
2225 s
->atom
= parse_sorting_atom(arg
);
2228 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2230 if (!arg
) /* should --no-sort void the list ? */
2232 parse_ref_sorting(opt
->value
, arg
);
2236 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2238 struct ref_filter
*rf
= opt
->value
;
2239 struct object_id oid
;
2240 int no_merged
= starts_with(opt
->long_name
, "no");
2244 return opterror(opt
, "is incompatible with --merged", 0);
2246 return opterror(opt
, "is incompatible with --no-merged", 0);
2250 rf
->merge
= no_merged
2251 ? REF_FILTER_MERGED_OMIT
2252 : REF_FILTER_MERGED_INCLUDE
;
2254 if (get_oid(arg
, &oid
))
2255 die(_("malformed object name %s"), arg
);
2257 rf
->merge_commit
= lookup_commit_reference_gently(the_repository
,
2259 if (!rf
->merge_commit
)
2260 return opterror(opt
, "must point to a commit", 0);