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"
27 static struct ref_msg
{
31 const char *ahead_behind
;
33 /* Untranslated plumbing messages: */
40 void setup_ref_filter_porcelain_msg(void)
42 msgs
.gone
= _("gone");
43 msgs
.ahead
= _("ahead %d");
44 msgs
.behind
= _("behind %d");
45 msgs
.ahead_behind
= _("ahead %d, behind %d");
48 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
49 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
50 typedef enum { SOURCE_NONE
= 0, SOURCE_OBJ
, SOURCE_OTHER
} info_source
;
58 cmp_status cmp_status
;
60 unsigned int then_atom_seen
: 1,
62 condition_satisfied
: 1;
66 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
70 static struct expand_data
{
72 enum object_type type
;
75 struct object_id delta_base_oid
;
78 struct object_info info
;
81 struct ref_to_worktree_entry
{
82 struct hashmap_entry ent
;
83 struct worktree
*wt
; /* key is wt->head_ref */
86 static int ref_to_worktree_map_cmpfnc(const void *unused_lookupdata
,
87 const struct hashmap_entry
*eptr
,
88 const struct hashmap_entry
*kptr
,
89 const void *keydata_aka_refname
)
91 const struct ref_to_worktree_entry
*e
, *k
;
93 e
= container_of(eptr
, const struct ref_to_worktree_entry
, ent
);
94 k
= container_of(kptr
, const struct ref_to_worktree_entry
, ent
);
96 return strcmp(e
->wt
->head_ref
,
97 keydata_aka_refname
? keydata_aka_refname
: k
->wt
->head_ref
);
100 static struct ref_to_worktree_map
{
102 struct worktree
**worktrees
;
103 } ref_to_worktree_map
;
106 * An atom is a valid field atom listed below, possibly prefixed with
107 * a "*" to denote deref_tag().
109 * We parse given format string and sort specifiers, and make a list
110 * of properties that we need to extract out of objects. ref_array_item
111 * structure will hold an array of values extracted that can be
112 * indexed with the "atom number", which is an index into this
115 static struct used_atom
{
120 char color
[COLOR_MAXLEN
];
124 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
126 struct refname_atom refname
;
127 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
130 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LENGTH
,
131 C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
132 struct process_trailer_options trailer_opts
;
136 cmp_status cmp_status
;
140 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
143 struct email_option
{
144 enum { EO_RAW
, EO_TRIM
, EO_LOCALPART
} option
;
146 struct refname_atom refname
;
150 static int used_atom_cnt
, need_tagged
, need_symref
;
153 * Expand string, append it to strbuf *sb, then return error code ret.
154 * Allow to save few lines of code.
156 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
160 strbuf_vaddf(sb
, fmt
, ap
);
165 static int color_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
166 const char *color_value
, struct strbuf
*err
)
169 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
170 if (color_parse(color_value
, atom
->u
.color
) < 0)
171 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
174 * We check this after we've parsed the color, which lets us complain
175 * about syntactically bogus color names even if they won't be used.
177 if (!want_color(format
->use_color
))
178 color_parse("", atom
->u
.color
);
182 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
183 const char *name
, struct strbuf
*err
)
186 atom
->option
= R_NORMAL
;
187 else if (!strcmp(arg
, "short"))
188 atom
->option
= R_SHORT
;
189 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
190 skip_prefix(arg
, "strip=", &arg
)) {
191 atom
->option
= R_LSTRIP
;
192 if (strtol_i(arg
, 10, &atom
->lstrip
))
193 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
194 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
195 atom
->option
= R_RSTRIP
;
196 if (strtol_i(arg
, 10, &atom
->rstrip
))
197 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
199 return strbuf_addf_ret(err
, -1, _("unrecognized %%(%s) argument: %s"), name
, arg
);
203 static int remote_ref_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
204 const char *arg
, struct strbuf
*err
)
206 struct string_list params
= STRING_LIST_INIT_DUP
;
209 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
210 atom
->u
.remote_ref
.push
= 1;
213 atom
->u
.remote_ref
.option
= RR_REF
;
214 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
215 arg
, atom
->name
, err
);
218 atom
->u
.remote_ref
.nobracket
= 0;
219 string_list_split(¶ms
, arg
, ',', -1);
221 for (i
= 0; i
< params
.nr
; i
++) {
222 const char *s
= params
.items
[i
].string
;
224 if (!strcmp(s
, "track"))
225 atom
->u
.remote_ref
.option
= RR_TRACK
;
226 else if (!strcmp(s
, "trackshort"))
227 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
228 else if (!strcmp(s
, "nobracket"))
229 atom
->u
.remote_ref
.nobracket
= 1;
230 else if (!strcmp(s
, "remotename")) {
231 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
232 atom
->u
.remote_ref
.push_remote
= 1;
233 } else if (!strcmp(s
, "remoteref")) {
234 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
235 atom
->u
.remote_ref
.push_remote
= 1;
237 atom
->u
.remote_ref
.option
= RR_REF
;
238 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
239 arg
, atom
->name
, err
)) {
240 string_list_clear(¶ms
, 0);
246 string_list_clear(¶ms
, 0);
250 static int objecttype_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
251 const char *arg
, struct strbuf
*err
)
254 return strbuf_addf_ret(err
, -1, _("%%(objecttype) does not take arguments"));
255 if (*atom
->name
== '*')
256 oi_deref
.info
.typep
= &oi_deref
.type
;
258 oi
.info
.typep
= &oi
.type
;
262 static int objectsize_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
263 const char *arg
, struct strbuf
*err
)
266 if (*atom
->name
== '*')
267 oi_deref
.info
.sizep
= &oi_deref
.size
;
269 oi
.info
.sizep
= &oi
.size
;
270 } else if (!strcmp(arg
, "disk")) {
271 if (*atom
->name
== '*')
272 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
274 oi
.info
.disk_sizep
= &oi
.disk_size
;
276 return strbuf_addf_ret(err
, -1, _("unrecognized %%(objectsize) argument: %s"), arg
);
280 static int deltabase_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
281 const char *arg
, struct strbuf
*err
)
284 return strbuf_addf_ret(err
, -1, _("%%(deltabase) does not take arguments"));
285 if (*atom
->name
== '*')
286 oi_deref
.info
.delta_base_oid
= &oi_deref
.delta_base_oid
;
288 oi
.info
.delta_base_oid
= &oi
.delta_base_oid
;
292 static int body_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
293 const char *arg
, struct strbuf
*err
)
296 return strbuf_addf_ret(err
, -1, _("%%(body) does not take arguments"));
297 atom
->u
.contents
.option
= C_BODY_DEP
;
301 static int subject_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
302 const char *arg
, struct strbuf
*err
)
305 return strbuf_addf_ret(err
, -1, _("%%(subject) does not take arguments"));
306 atom
->u
.contents
.option
= C_SUB
;
310 static int trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
311 const char *arg
, struct strbuf
*err
)
313 struct string_list params
= STRING_LIST_INIT_DUP
;
316 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
319 string_list_split(¶ms
, arg
, ',', -1);
320 for (i
= 0; i
< params
.nr
; i
++) {
321 const char *s
= params
.items
[i
].string
;
322 if (!strcmp(s
, "unfold"))
323 atom
->u
.contents
.trailer_opts
.unfold
= 1;
324 else if (!strcmp(s
, "only"))
325 atom
->u
.contents
.trailer_opts
.only_trailers
= 1;
327 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), s
);
328 string_list_clear(¶ms
, 0);
333 atom
->u
.contents
.option
= C_TRAILERS
;
334 string_list_clear(¶ms
, 0);
338 static int contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
339 const char *arg
, struct strbuf
*err
)
342 atom
->u
.contents
.option
= C_BARE
;
343 else if (!strcmp(arg
, "body"))
344 atom
->u
.contents
.option
= C_BODY
;
345 else if (!strcmp(arg
, "size"))
346 atom
->u
.contents
.option
= C_LENGTH
;
347 else if (!strcmp(arg
, "signature"))
348 atom
->u
.contents
.option
= C_SIG
;
349 else if (!strcmp(arg
, "subject"))
350 atom
->u
.contents
.option
= C_SUB
;
351 else if (skip_prefix(arg
, "trailers", &arg
)) {
352 skip_prefix(arg
, ":", &arg
);
353 if (trailers_atom_parser(format
, atom
, *arg
? arg
: NULL
, err
))
355 } else if (skip_prefix(arg
, "lines=", &arg
)) {
356 atom
->u
.contents
.option
= C_LINES
;
357 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
358 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
360 return strbuf_addf_ret(err
, -1, _("unrecognized %%(contents) argument: %s"), arg
);
364 static int objectname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
365 const char *arg
, struct strbuf
*err
)
368 atom
->u
.objectname
.option
= O_FULL
;
369 else if (!strcmp(arg
, "short"))
370 atom
->u
.objectname
.option
= O_SHORT
;
371 else if (skip_prefix(arg
, "short=", &arg
)) {
372 atom
->u
.objectname
.option
= O_LENGTH
;
373 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
374 atom
->u
.objectname
.length
== 0)
375 return strbuf_addf_ret(err
, -1, _("positive value expected objectname:short=%s"), arg
);
376 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
377 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
379 return strbuf_addf_ret(err
, -1, _("unrecognized %%(objectname) argument: %s"), arg
);
383 static int person_email_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
384 const char *arg
, struct strbuf
*err
)
387 atom
->u
.email_option
.option
= EO_RAW
;
388 else if (!strcmp(arg
, "trim"))
389 atom
->u
.email_option
.option
= EO_TRIM
;
390 else if (!strcmp(arg
, "localpart"))
391 atom
->u
.email_option
.option
= EO_LOCALPART
;
393 return strbuf_addf_ret(err
, -1, _("unrecognized email option: %s"), arg
);
397 static int refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
398 const char *arg
, struct strbuf
*err
)
400 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
403 static align_type
parse_align_position(const char *s
)
405 if (!strcmp(s
, "right"))
407 else if (!strcmp(s
, "middle"))
409 else if (!strcmp(s
, "left"))
414 static int align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
415 const char *arg
, struct strbuf
*err
)
417 struct align
*align
= &atom
->u
.align
;
418 struct string_list params
= STRING_LIST_INIT_DUP
;
420 unsigned int width
= ~0U;
423 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
425 align
->position
= ALIGN_LEFT
;
427 string_list_split(¶ms
, arg
, ',', -1);
428 for (i
= 0; i
< params
.nr
; i
++) {
429 const char *s
= params
.items
[i
].string
;
432 if (skip_prefix(s
, "position=", &s
)) {
433 position
= parse_align_position(s
);
435 strbuf_addf(err
, _("unrecognized position:%s"), s
);
436 string_list_clear(¶ms
, 0);
439 align
->position
= position
;
440 } else if (skip_prefix(s
, "width=", &s
)) {
441 if (strtoul_ui(s
, 10, &width
)) {
442 strbuf_addf(err
, _("unrecognized width:%s"), s
);
443 string_list_clear(¶ms
, 0);
446 } else if (!strtoul_ui(s
, 10, &width
))
448 else if ((position
= parse_align_position(s
)) >= 0)
449 align
->position
= position
;
451 strbuf_addf(err
, _("unrecognized %%(align) argument: %s"), s
);
452 string_list_clear(¶ms
, 0);
458 string_list_clear(¶ms
, 0);
459 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
461 align
->width
= width
;
462 string_list_clear(¶ms
, 0);
466 static int if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
467 const char *arg
, struct strbuf
*err
)
470 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
472 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
473 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
474 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
475 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
477 return strbuf_addf_ret(err
, -1, _("unrecognized %%(if) argument: %s"), arg
);
481 static int head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
482 const char *arg
, struct strbuf
*unused_err
)
484 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
492 int (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
,
493 const char *arg
, struct strbuf
*err
);
495 { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
496 { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
497 { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
498 { "objectname", SOURCE_OTHER
, FIELD_STR
, objectname_atom_parser
},
499 { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
500 { "tree", SOURCE_OBJ
},
501 { "parent", SOURCE_OBJ
},
502 { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
503 { "object", SOURCE_OBJ
},
504 { "type", SOURCE_OBJ
},
505 { "tag", SOURCE_OBJ
},
506 { "author", SOURCE_OBJ
},
507 { "authorname", SOURCE_OBJ
},
508 { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
509 { "authordate", SOURCE_OBJ
, FIELD_TIME
},
510 { "committer", SOURCE_OBJ
},
511 { "committername", SOURCE_OBJ
},
512 { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
513 { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
514 { "tagger", SOURCE_OBJ
},
515 { "taggername", SOURCE_OBJ
},
516 { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
517 { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
518 { "creator", SOURCE_OBJ
},
519 { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
520 { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
521 { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
522 { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
523 { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
524 { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
525 { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
526 { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
527 { "flag", SOURCE_NONE
},
528 { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
529 { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
530 { "worktreepath", SOURCE_NONE
},
531 { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
532 { "end", SOURCE_NONE
},
533 { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
534 { "then", SOURCE_NONE
},
535 { "else", SOURCE_NONE
},
537 * Please update $__git_ref_fieldlist in git-completion.bash
538 * when you add new atoms
542 #define REF_FORMATTING_STATE_INIT { 0, NULL }
544 struct ref_formatting_stack
{
545 struct ref_formatting_stack
*prev
;
546 struct strbuf output
;
547 void (*at_end
)(struct ref_formatting_stack
**stack
);
551 struct ref_formatting_state
{
553 struct ref_formatting_stack
*stack
;
558 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
560 uintmax_t value
; /* used for sorting when not FIELD_STR */
561 struct used_atom
*atom
;
565 * Used to parse format string and sort specifiers
567 static int parse_ref_filter_atom(const struct ref_format
*format
,
568 const char *atom
, const char *ep
,
576 if (*sp
== '*' && sp
< ep
)
579 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
580 (int)(ep
-atom
), atom
);
582 /* Do we have the atom already used elsewhere? */
583 for (i
= 0; i
< used_atom_cnt
; i
++) {
584 int len
= strlen(used_atom
[i
].name
);
585 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
590 * If the atom name has a colon, strip it and everything after
591 * it off - it specifies the format for this entry, and
592 * shouldn't be used for checking against the valid_atom
595 arg
= memchr(sp
, ':', ep
- sp
);
596 atom_len
= (arg
? arg
: ep
) - sp
;
598 /* Is the atom a valid one? */
599 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
600 int len
= strlen(valid_atom
[i
].name
);
601 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
605 if (ARRAY_SIZE(valid_atom
) <= i
)
606 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
607 (int)(ep
-atom
), atom
);
608 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
609 return strbuf_addf_ret(err
, -1,
610 _("not a git repository, but the field '%.*s' requires access to object data"),
611 (int)(ep
-atom
), atom
);
613 /* Add it in, including the deref prefix */
616 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
617 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
618 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
619 used_atom
[at
].source
= valid_atom
[i
].source
;
620 if (used_atom
[at
].source
== SOURCE_OBJ
) {
622 oi_deref
.info
.contentp
= &oi_deref
.content
;
624 oi
.info
.contentp
= &oi
.content
;
627 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
630 * Treat empty sub-arguments list as NULL (i.e.,
631 * "%(atom:)" is equivalent to "%(atom)").
636 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
637 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
641 if (!strcmp(valid_atom
[i
].name
, "symref"))
646 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
648 switch (quote_style
) {
650 strbuf_addstr(s
, str
);
653 sq_quote_buf(s
, str
);
656 perl_quote_buf(s
, str
);
659 python_quote_buf(s
, str
);
662 tcl_quote_buf(s
, str
);
667 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
668 struct strbuf
*unused_err
)
671 * Quote formatting is only done when the stack has a single
672 * element. Otherwise quote formatting is done on the
673 * element's entire output strbuf when the %(end) atom is
676 if (!state
->stack
->prev
)
677 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
679 strbuf_addstr(&state
->stack
->output
, v
->s
);
683 static void push_stack_element(struct ref_formatting_stack
**stack
)
685 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
687 strbuf_init(&s
->output
, 0);
692 static void pop_stack_element(struct ref_formatting_stack
**stack
)
694 struct ref_formatting_stack
*current
= *stack
;
695 struct ref_formatting_stack
*prev
= current
->prev
;
698 strbuf_addbuf(&prev
->output
, ¤t
->output
);
699 strbuf_release(¤t
->output
);
704 static void end_align_handler(struct ref_formatting_stack
**stack
)
706 struct ref_formatting_stack
*cur
= *stack
;
707 struct align
*align
= (struct align
*)cur
->at_end_data
;
708 struct strbuf s
= STRBUF_INIT
;
710 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
711 strbuf_swap(&cur
->output
, &s
);
715 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
716 struct strbuf
*unused_err
)
718 struct ref_formatting_stack
*new_stack
;
720 push_stack_element(&state
->stack
);
721 new_stack
= state
->stack
;
722 new_stack
->at_end
= end_align_handler
;
723 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
727 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
729 struct ref_formatting_stack
*cur
= *stack
;
730 struct ref_formatting_stack
*prev
= cur
->prev
;
731 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
733 if (!if_then_else
->then_atom_seen
)
734 die(_("format: %%(if) atom used without a %%(then) atom"));
736 if (if_then_else
->else_atom_seen
) {
738 * There is an %(else) atom: we need to drop one state from the
739 * stack, either the %(else) branch if the condition is satisfied, or
740 * the %(then) branch if it isn't.
742 if (if_then_else
->condition_satisfied
) {
743 strbuf_reset(&cur
->output
);
744 pop_stack_element(&cur
);
746 strbuf_swap(&cur
->output
, &prev
->output
);
747 strbuf_reset(&cur
->output
);
748 pop_stack_element(&cur
);
750 } else if (!if_then_else
->condition_satisfied
) {
752 * No %(else) atom: just drop the %(then) branch if the
753 * condition is not satisfied.
755 strbuf_reset(&cur
->output
);
762 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
763 struct strbuf
*unused_err
)
765 struct ref_formatting_stack
*new_stack
;
766 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
768 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
769 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
771 push_stack_element(&state
->stack
);
772 new_stack
= state
->stack
;
773 new_stack
->at_end
= if_then_else_handler
;
774 new_stack
->at_end_data
= if_then_else
;
778 static int is_empty(const char *s
)
788 static int then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
791 struct ref_formatting_stack
*cur
= state
->stack
;
792 struct if_then_else
*if_then_else
= NULL
;
794 if (cur
->at_end
== if_then_else_handler
)
795 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
797 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used without an %%(if) atom"));
798 if (if_then_else
->then_atom_seen
)
799 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
800 if (if_then_else
->else_atom_seen
)
801 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
802 if_then_else
->then_atom_seen
= 1;
804 * If the 'equals' or 'notequals' attribute is used then
805 * perform the required comparison. If not, only non-empty
806 * strings satisfy the 'if' condition.
808 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
809 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
810 if_then_else
->condition_satisfied
= 1;
811 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
812 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
813 if_then_else
->condition_satisfied
= 1;
814 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
815 if_then_else
->condition_satisfied
= 1;
816 strbuf_reset(&cur
->output
);
820 static int else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
823 struct ref_formatting_stack
*prev
= state
->stack
;
824 struct if_then_else
*if_then_else
= NULL
;
826 if (prev
->at_end
== if_then_else_handler
)
827 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
829 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without an %%(if) atom"));
830 if (!if_then_else
->then_atom_seen
)
831 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without a %%(then) atom"));
832 if (if_then_else
->else_atom_seen
)
833 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
834 if_then_else
->else_atom_seen
= 1;
835 push_stack_element(&state
->stack
);
836 state
->stack
->at_end_data
= prev
->at_end_data
;
837 state
->stack
->at_end
= prev
->at_end
;
841 static int end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
844 struct ref_formatting_stack
*current
= state
->stack
;
845 struct strbuf s
= STRBUF_INIT
;
847 if (!current
->at_end
)
848 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
849 current
->at_end(&state
->stack
);
851 /* Stack may have been popped within at_end(), hence reset the current pointer */
852 current
= state
->stack
;
855 * Perform quote formatting when the stack element is that of
856 * a supporting atom. If nested then perform quote formatting
857 * only on the topmost supporting atom.
859 if (!current
->prev
->prev
) {
860 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
861 strbuf_swap(¤t
->output
, &s
);
864 pop_stack_element(&state
->stack
);
869 * In a format string, find the next occurrence of %(atom).
871 static const char *find_next(const char *cp
)
876 * %( is the start of an atom;
877 * %% is a quoted per-cent.
881 else if (cp
[1] == '%')
882 cp
++; /* skip over two % */
883 /* otherwise this is a singleton, literal % */
891 * Make sure the format string is well formed, and parse out
894 int verify_ref_format(struct ref_format
*format
)
898 format
->need_color_reset_at_eol
= 0;
899 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
900 struct strbuf err
= STRBUF_INIT
;
901 const char *color
, *ep
= strchr(sp
, ')');
905 return error(_("malformed format string %s"), sp
);
906 /* sp points at "%(" and ep points at the closing ")" */
907 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
912 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
913 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
914 strbuf_release(&err
);
916 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
917 format
->need_color_reset_at_eol
= 0;
921 static int grab_objectname(const char *name
, const struct object_id
*oid
,
922 struct atom_value
*v
, struct used_atom
*atom
)
924 if (starts_with(name
, "objectname")) {
925 if (atom
->u
.objectname
.option
== O_SHORT
) {
926 v
->s
= xstrdup(find_unique_abbrev(oid
, DEFAULT_ABBREV
));
928 } else if (atom
->u
.objectname
.option
== O_FULL
) {
929 v
->s
= xstrdup(oid_to_hex(oid
));
931 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
932 v
->s
= xstrdup(find_unique_abbrev(oid
, atom
->u
.objectname
.length
));
935 BUG("unknown %%(objectname) option");
940 /* See grab_values */
941 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
945 for (i
= 0; i
< used_atom_cnt
; i
++) {
946 const char *name
= used_atom
[i
].name
;
947 struct atom_value
*v
= &val
[i
];
948 if (!!deref
!= (*name
== '*'))
952 if (!strcmp(name
, "objecttype"))
953 v
->s
= xstrdup(type_name(oi
->type
));
954 else if (!strcmp(name
, "objectsize:disk")) {
955 v
->value
= oi
->disk_size
;
956 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
957 } else if (!strcmp(name
, "objectsize")) {
959 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
960 } else if (!strcmp(name
, "deltabase"))
961 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
963 grab_objectname(name
, &oi
->oid
, v
, &used_atom
[i
]);
967 /* See grab_values */
968 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
971 struct tag
*tag
= (struct tag
*) obj
;
973 for (i
= 0; i
< used_atom_cnt
; i
++) {
974 const char *name
= used_atom
[i
].name
;
975 struct atom_value
*v
= &val
[i
];
976 if (!!deref
!= (*name
== '*'))
980 if (!strcmp(name
, "tag"))
981 v
->s
= xstrdup(tag
->tag
);
982 else if (!strcmp(name
, "type") && tag
->tagged
)
983 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
984 else if (!strcmp(name
, "object") && tag
->tagged
)
985 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
989 /* See grab_values */
990 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
993 struct commit
*commit
= (struct commit
*) obj
;
995 for (i
= 0; i
< used_atom_cnt
; i
++) {
996 const char *name
= used_atom
[i
].name
;
997 struct atom_value
*v
= &val
[i
];
998 if (!!deref
!= (*name
== '*'))
1002 if (!strcmp(name
, "tree")) {
1003 v
->s
= xstrdup(oid_to_hex(get_commit_tree_oid(commit
)));
1005 else if (!strcmp(name
, "numparent")) {
1006 v
->value
= commit_list_count(commit
->parents
);
1007 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1009 else if (!strcmp(name
, "parent")) {
1010 struct commit_list
*parents
;
1011 struct strbuf s
= STRBUF_INIT
;
1012 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1013 struct commit
*parent
= parents
->item
;
1014 if (parents
!= commit
->parents
)
1015 strbuf_addch(&s
, ' ');
1016 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
1018 v
->s
= strbuf_detach(&s
, NULL
);
1023 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1027 if (!strncmp(buf
, who
, wholen
) &&
1029 return buf
+ wholen
+ 1;
1030 eol
= strchr(buf
, '\n');
1035 return ""; /* end of header */
1041 static const char *copy_line(const char *buf
)
1043 const char *eol
= strchrnul(buf
, '\n');
1044 return xmemdupz(buf
, eol
- buf
);
1047 static const char *copy_name(const char *buf
)
1050 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1051 if (!strncmp(cp
, " <", 2))
1052 return xmemdupz(buf
, cp
- buf
);
1057 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1059 const char *email
= strchr(buf
, '<');
1060 const char *eoemail
;
1063 switch (atom
->u
.email_option
.option
) {
1065 eoemail
= strchr(email
, '>');
1071 eoemail
= strchr(email
, '>');
1075 eoemail
= strchr(email
, '@');
1077 eoemail
= strchr(email
, '>');
1080 BUG("unknown email option");
1085 return xmemdupz(email
, eoemail
- email
);
1088 static char *copy_subject(const char *buf
, unsigned long len
)
1090 char *r
= xmemdupz(buf
, len
);
1093 for (i
= 0; i
< len
; i
++)
1100 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1102 const char *eoemail
= strstr(buf
, "> ");
1104 timestamp_t timestamp
;
1106 struct date_mode date_mode
= { DATE_NORMAL
};
1107 const char *formatp
;
1110 * We got here because atomname ends in "date" or "date<something>";
1111 * it's not possible that <something> is not ":<format>" because
1112 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1113 * ":" means no format is specified, and use the default.
1115 formatp
= strchr(atomname
, ':');
1116 if (formatp
!= NULL
) {
1118 parse_date_format(formatp
, &date_mode
);
1123 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1124 if (timestamp
== TIME_MAX
)
1126 tz
= strtol(zone
, NULL
, 10);
1127 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1129 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1130 v
->value
= timestamp
;
1137 /* See grab_values */
1138 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1141 int wholen
= strlen(who
);
1142 const char *wholine
= NULL
;
1144 for (i
= 0; i
< used_atom_cnt
; i
++) {
1145 const char *name
= used_atom
[i
].name
;
1146 struct atom_value
*v
= &val
[i
];
1147 if (!!deref
!= (*name
== '*'))
1151 if (strncmp(who
, name
, wholen
))
1153 if (name
[wholen
] != 0 &&
1154 strcmp(name
+ wholen
, "name") &&
1155 !starts_with(name
+ wholen
, "email") &&
1156 !starts_with(name
+ wholen
, "date"))
1159 wholine
= find_wholine(who
, wholen
, buf
);
1161 return; /* no point looking for it */
1162 if (name
[wholen
] == 0)
1163 v
->s
= copy_line(wholine
);
1164 else if (!strcmp(name
+ wholen
, "name"))
1165 v
->s
= copy_name(wholine
);
1166 else if (starts_with(name
+ wholen
, "email"))
1167 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1168 else if (starts_with(name
+ wholen
, "date"))
1169 grab_date(wholine
, v
, name
);
1173 * For a tag or a commit object, if "creator" or "creatordate" is
1174 * requested, do something special.
1176 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1177 return; /* "author" for commit object is not wanted */
1179 wholine
= find_wholine(who
, wholen
, buf
);
1182 for (i
= 0; i
< used_atom_cnt
; i
++) {
1183 const char *name
= used_atom
[i
].name
;
1184 struct atom_value
*v
= &val
[i
];
1185 if (!!deref
!= (*name
== '*'))
1190 if (starts_with(name
, "creatordate"))
1191 grab_date(wholine
, v
, name
);
1192 else if (!strcmp(name
, "creator"))
1193 v
->s
= copy_line(wholine
);
1197 static void find_subpos(const char *buf
,
1198 const char **sub
, unsigned long *sublen
,
1199 const char **body
, unsigned long *bodylen
,
1200 unsigned long *nonsiglen
,
1201 const char **sig
, unsigned long *siglen
)
1204 /* skip past header until we hit empty line */
1205 while (*buf
&& *buf
!= '\n') {
1206 eol
= strchrnul(buf
, '\n');
1211 /* skip any empty lines */
1212 while (*buf
== '\n')
1215 /* parse signature first; we might not even have a subject line */
1216 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
1217 *siglen
= strlen(*sig
);
1219 /* subject is first non-empty line */
1221 /* subject goes to first empty line */
1222 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
1223 eol
= strchrnul(buf
, '\n');
1228 *sublen
= buf
- *sub
;
1229 /* drop trailing newline, if present */
1230 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1233 /* skip any empty lines */
1234 while (*buf
== '\n')
1237 *bodylen
= strlen(buf
);
1238 *nonsiglen
= *sig
- buf
;
1242 * If 'lines' is greater than 0, append that many lines from the given
1243 * 'buf' of length 'size' to the given strbuf.
1245 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1248 const char *sp
, *eol
;
1253 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1255 strbuf_addstr(out
, "\n ");
1256 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1257 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1258 strbuf_add(out
, sp
, len
);
1265 /* See grab_values */
1266 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, void *buf
)
1269 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1270 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1272 for (i
= 0; i
< used_atom_cnt
; i
++) {
1273 struct used_atom
*atom
= &used_atom
[i
];
1274 const char *name
= atom
->name
;
1275 struct atom_value
*v
= &val
[i
];
1276 if (!!deref
!= (*name
== '*'))
1280 if (strcmp(name
, "subject") &&
1281 strcmp(name
, "body") &&
1282 !starts_with(name
, "trailers") &&
1283 !starts_with(name
, "contents"))
1288 &bodypos
, &bodylen
, &nonsiglen
,
1291 if (atom
->u
.contents
.option
== C_SUB
)
1292 v
->s
= copy_subject(subpos
, sublen
);
1293 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1294 v
->s
= xmemdupz(bodypos
, bodylen
);
1295 else if (atom
->u
.contents
.option
== C_LENGTH
)
1296 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)strlen(subpos
));
1297 else if (atom
->u
.contents
.option
== C_BODY
)
1298 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1299 else if (atom
->u
.contents
.option
== C_SIG
)
1300 v
->s
= xmemdupz(sigpos
, siglen
);
1301 else if (atom
->u
.contents
.option
== C_LINES
) {
1302 struct strbuf s
= STRBUF_INIT
;
1303 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1305 /* Size is the length of the message after removing the signature */
1306 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1307 v
->s
= strbuf_detach(&s
, NULL
);
1308 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1309 struct strbuf s
= STRBUF_INIT
;
1311 /* Format the trailer info according to the trailer_opts given */
1312 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1314 v
->s
= strbuf_detach(&s
, NULL
);
1315 } else if (atom
->u
.contents
.option
== C_BARE
)
1316 v
->s
= xstrdup(subpos
);
1321 * We want to have empty print-string for field requests
1322 * that do not apply (e.g. "authordate" for a tag object)
1324 static void fill_missing_values(struct atom_value
*val
)
1327 for (i
= 0; i
< used_atom_cnt
; i
++) {
1328 struct atom_value
*v
= &val
[i
];
1335 * val is a list of atom_value to hold returned values. Extract
1336 * the values for atoms in used_atom array out of (obj, buf, sz).
1337 * when deref is false, (obj, buf, sz) is the object that is
1338 * pointed at by the ref itself; otherwise it is the object the
1339 * ref (which is a tag) refers to.
1341 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
)
1343 switch (obj
->type
) {
1345 grab_tag_values(val
, deref
, obj
);
1346 grab_sub_body_contents(val
, deref
, buf
);
1347 grab_person("tagger", val
, deref
, buf
);
1350 grab_commit_values(val
, deref
, obj
);
1351 grab_sub_body_contents(val
, deref
, buf
);
1352 grab_person("author", val
, deref
, buf
);
1353 grab_person("committer", val
, deref
, buf
);
1356 /* grab_tree_values(val, deref, obj, buf, sz); */
1359 /* grab_blob_values(val, deref, obj, buf, sz); */
1362 die("Eh? Object of type %d?", obj
->type
);
1366 static inline char *copy_advance(char *dst
, const char *src
)
1373 static const char *lstrip_ref_components(const char *refname
, int len
)
1375 long remaining
= len
;
1376 const char *start
= xstrdup(refname
);
1377 const char *to_free
= start
;
1381 const char *p
= refname
;
1383 /* Find total no of '/' separated path-components */
1384 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1387 * The number of components we need to strip is now
1388 * the total minus the components to be left (Plus one
1389 * because we count the number of '/', but the number
1390 * of components is one more than the no of '/').
1392 remaining
= i
+ len
+ 1;
1395 while (remaining
> 0) {
1398 free((char *)to_free
);
1406 start
= xstrdup(start
);
1407 free((char *)to_free
);
1411 static const char *rstrip_ref_components(const char *refname
, int len
)
1413 long remaining
= len
;
1414 const char *start
= xstrdup(refname
);
1415 const char *to_free
= start
;
1419 const char *p
= refname
;
1421 /* Find total no of '/' separated path-components */
1422 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1425 * The number of components we need to strip is now
1426 * the total minus the components to be left (Plus one
1427 * because we count the number of '/', but the number
1428 * of components is one more than the no of '/').
1430 remaining
= i
+ len
+ 1;
1433 while (remaining
-- > 0) {
1434 char *p
= strrchr(start
, '/');
1436 free((char *)to_free
);
1444 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1446 if (atom
->option
== R_SHORT
)
1447 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1448 else if (atom
->option
== R_LSTRIP
)
1449 return lstrip_ref_components(refname
, atom
->lstrip
);
1450 else if (atom
->option
== R_RSTRIP
)
1451 return rstrip_ref_components(refname
, atom
->rstrip
);
1453 return xstrdup(refname
);
1456 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1457 struct branch
*branch
, const char **s
)
1459 int num_ours
, num_theirs
;
1460 if (atom
->u
.remote_ref
.option
== RR_REF
)
1461 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1462 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1463 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1464 NULL
, atom
->u
.remote_ref
.push
,
1465 AHEAD_BEHIND_FULL
) < 0) {
1466 *s
= xstrdup(msgs
.gone
);
1467 } else if (!num_ours
&& !num_theirs
)
1470 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1471 else if (!num_theirs
)
1472 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1474 *s
= xstrfmt(msgs
.ahead_behind
,
1475 num_ours
, num_theirs
);
1476 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1477 const char *to_free
= *s
;
1478 *s
= xstrfmt("[%s]", *s
);
1479 free((void *)to_free
);
1481 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1482 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1483 NULL
, atom
->u
.remote_ref
.push
,
1484 AHEAD_BEHIND_FULL
) < 0) {
1488 if (!num_ours
&& !num_theirs
)
1492 else if (!num_theirs
)
1496 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1498 const char *remote
= atom
->u
.remote_ref
.push
?
1499 pushremote_for_branch(branch
, &explicit) :
1500 remote_for_branch(branch
, &explicit);
1501 *s
= xstrdup(explicit ? remote
: "");
1502 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1505 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
1506 *s
= xstrdup(merge
? merge
: "");
1508 BUG("unhandled RR_* enum");
1511 char *get_head_description(void)
1513 struct strbuf desc
= STRBUF_INIT
;
1514 struct wt_status_state state
;
1515 memset(&state
, 0, sizeof(state
));
1516 wt_status_get_state(the_repository
, &state
, 1);
1519 * The ( character must be hard-coded and not part of a localizable
1520 * string, since the description is used as a sort key and compared
1523 strbuf_addch(&desc
, '(');
1524 if (state
.rebase_in_progress
||
1525 state
.rebase_interactive_in_progress
) {
1527 strbuf_addf(&desc
, _("no branch, rebasing %s"),
1530 strbuf_addf(&desc
, _("no branch, rebasing detached HEAD %s"),
1531 state
.detached_from
);
1532 } else if (state
.bisect_in_progress
)
1533 strbuf_addf(&desc
, _("no branch, bisect started on %s"),
1535 else if (state
.detached_from
) {
1536 if (state
.detached_at
)
1537 strbuf_addstr(&desc
, HEAD_DETACHED_AT
);
1539 strbuf_addstr(&desc
, HEAD_DETACHED_FROM
);
1540 strbuf_addstr(&desc
, state
.detached_from
);
1543 strbuf_addstr(&desc
, _("no branch"));
1544 strbuf_addch(&desc
, ')');
1548 free(state
.detached_from
);
1549 return strbuf_detach(&desc
, NULL
);
1552 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1557 return show_ref(&atom
->u
.refname
, ref
->symref
);
1560 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1562 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1563 return get_head_description();
1564 return show_ref(&atom
->u
.refname
, ref
->refname
);
1567 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
1568 struct expand_data
*oi
, struct strbuf
*err
)
1570 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1572 if (oi
->info
.contentp
) {
1573 /* We need to know that to use parse_object_buffer properly */
1574 oi
->info
.sizep
= &oi
->size
;
1575 oi
->info
.typep
= &oi
->type
;
1577 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
1578 OBJECT_INFO_LOOKUP_REPLACE
))
1579 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1580 oid_to_hex(&oi
->oid
), ref
->refname
);
1581 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
1582 BUG("Object size is less than zero.");
1584 if (oi
->info
.contentp
) {
1585 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
1589 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1590 oid_to_hex(&oi
->oid
), ref
->refname
);
1592 grab_values(ref
->value
, deref
, *obj
, oi
->content
);
1595 grab_common_values(ref
->value
, deref
, oi
);
1601 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
1605 for (i
= 0; worktrees
[i
]; i
++) {
1606 if (worktrees
[i
]->head_ref
) {
1607 struct ref_to_worktree_entry
*entry
;
1608 entry
= xmalloc(sizeof(*entry
));
1609 entry
->wt
= worktrees
[i
];
1610 hashmap_entry_init(&entry
->ent
,
1611 strhash(worktrees
[i
]->head_ref
));
1613 hashmap_add(map
, &entry
->ent
);
1618 static void lazy_init_worktree_map(void)
1620 if (ref_to_worktree_map
.worktrees
)
1623 ref_to_worktree_map
.worktrees
= get_worktrees();
1624 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
1625 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
1628 static char *get_worktree_path(const struct used_atom
*atom
, const struct ref_array_item
*ref
)
1630 struct hashmap_entry entry
, *e
;
1631 struct ref_to_worktree_entry
*lookup_result
;
1633 lazy_init_worktree_map();
1635 hashmap_entry_init(&entry
, strhash(ref
->refname
));
1636 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
1641 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
1643 return xstrdup(lookup_result
->wt
->path
);
1647 * Parse the object referred by ref, and grab needed value.
1649 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
1653 struct object_info empty
= OBJECT_INFO_INIT
;
1655 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1657 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1658 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1661 ref
->symref
= xstrdup("");
1664 /* Fill in specials first */
1665 for (i
= 0; i
< used_atom_cnt
; i
++) {
1666 struct used_atom
*atom
= &used_atom
[i
];
1667 const char *name
= used_atom
[i
].name
;
1668 struct atom_value
*v
= &ref
->value
[i
];
1670 const char *refname
;
1671 struct branch
*branch
= NULL
;
1673 v
->handler
= append_atom
;
1681 if (starts_with(name
, "refname"))
1682 refname
= get_refname(atom
, ref
);
1683 else if (!strcmp(name
, "worktreepath")) {
1684 if (ref
->kind
== FILTER_REFS_BRANCHES
)
1685 v
->s
= get_worktree_path(atom
, ref
);
1690 else if (starts_with(name
, "symref"))
1691 refname
= get_symref(atom
, ref
);
1692 else if (starts_with(name
, "upstream")) {
1693 const char *branch_name
;
1694 /* only local branches may have an upstream */
1695 if (!skip_prefix(ref
->refname
, "refs/heads/",
1700 branch
= branch_get(branch_name
);
1702 refname
= branch_get_upstream(branch
, NULL
);
1704 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1708 } else if (atom
->u
.remote_ref
.push
) {
1709 const char *branch_name
;
1711 if (!skip_prefix(ref
->refname
, "refs/heads/",
1714 branch
= branch_get(branch_name
);
1716 if (atom
->u
.remote_ref
.push_remote
)
1719 refname
= branch_get_push(branch
, NULL
);
1723 /* We will definitely re-init v->s on the next line. */
1725 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1727 } else if (starts_with(name
, "color:")) {
1728 v
->s
= xstrdup(atom
->u
.color
);
1730 } else if (!strcmp(name
, "flag")) {
1731 char buf
[256], *cp
= buf
;
1732 if (ref
->flag
& REF_ISSYMREF
)
1733 cp
= copy_advance(cp
, ",symref");
1734 if (ref
->flag
& REF_ISPACKED
)
1735 cp
= copy_advance(cp
, ",packed");
1740 v
->s
= xstrdup(buf
+ 1);
1743 } else if (!deref
&& grab_objectname(name
, &ref
->objectname
, v
, atom
)) {
1745 } else if (!strcmp(name
, "HEAD")) {
1746 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1747 v
->s
= xstrdup("*");
1749 v
->s
= xstrdup(" ");
1751 } else if (starts_with(name
, "align")) {
1752 v
->handler
= align_atom_handler
;
1755 } else if (!strcmp(name
, "end")) {
1756 v
->handler
= end_atom_handler
;
1759 } else if (starts_with(name
, "if")) {
1761 if (skip_prefix(name
, "if:", &s
))
1765 v
->handler
= if_atom_handler
;
1767 } else if (!strcmp(name
, "then")) {
1768 v
->handler
= then_atom_handler
;
1771 } else if (!strcmp(name
, "else")) {
1772 v
->handler
= else_atom_handler
;
1779 v
->s
= xstrdup(refname
);
1781 v
->s
= xstrfmt("%s^{}", refname
);
1782 free((char *)refname
);
1785 for (i
= 0; i
< used_atom_cnt
; i
++) {
1786 struct atom_value
*v
= &ref
->value
[i
];
1787 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
1788 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1789 oid_to_hex(&ref
->objectname
), ref
->refname
);
1793 oi
.info
.contentp
= &oi
.content
;
1794 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
1795 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
1799 oi
.oid
= ref
->objectname
;
1800 if (get_object(ref
, 0, &obj
, &oi
, err
))
1804 * If there is no atom that wants to know about tagged
1805 * object, we are done.
1807 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1811 * If it is a tag object, see if we use a value that derefs
1812 * the object, and if we do grab the object it refers to.
1814 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
1817 * NEEDSWORK: This derefs tag only once, which
1818 * is good to deal with chains of trust, but
1819 * is not consistent with what deref_tag() does
1820 * which peels the onion to the core.
1822 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
1826 * Given a ref, return the value for the atom. This lazily gets value
1827 * out of the object by calling populate value.
1829 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
1830 struct atom_value
**v
, struct strbuf
*err
)
1833 if (populate_value(ref
, err
))
1835 fill_missing_values(ref
->value
);
1837 *v
= &ref
->value
[atom
];
1842 * Return 1 if the refname matches one of the patterns, otherwise 0.
1843 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1844 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1845 * matches "refs/heads/mas*", too).
1847 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1849 const char **patterns
= filter
->name_patterns
;
1852 if (filter
->ignore_case
)
1853 flags
|= WM_CASEFOLD
;
1856 * When no '--format' option is given we need to skip the prefix
1857 * for matching refs of tags and branches.
1859 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1860 skip_prefix(refname
, "refs/heads/", &refname
) ||
1861 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1862 skip_prefix(refname
, "refs/", &refname
));
1864 for (; *patterns
; patterns
++) {
1865 if (!wildmatch(*patterns
, refname
, flags
))
1872 * Return 1 if the refname matches one of the patterns, otherwise 0.
1873 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1874 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1875 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1877 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1879 const char **pattern
= filter
->name_patterns
;
1880 int namelen
= strlen(refname
);
1881 unsigned flags
= WM_PATHNAME
;
1883 if (filter
->ignore_case
)
1884 flags
|= WM_CASEFOLD
;
1886 for (; *pattern
; pattern
++) {
1887 const char *p
= *pattern
;
1888 int plen
= strlen(p
);
1890 if ((plen
<= namelen
) &&
1891 !strncmp(refname
, p
, plen
) &&
1892 (refname
[plen
] == '\0' ||
1893 refname
[plen
] == '/' ||
1896 if (!wildmatch(p
, refname
, flags
))
1902 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1903 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1905 if (!*filter
->name_patterns
)
1906 return 1; /* No pattern always matches */
1907 if (filter
->match_as_path
)
1908 return match_name_as_path(filter
, refname
);
1909 return match_pattern(filter
, refname
);
1912 static int qsort_strcmp(const void *va
, const void *vb
)
1914 const char *a
= *(const char **)va
;
1915 const char *b
= *(const char **)vb
;
1917 return strcmp(a
, b
);
1920 static void find_longest_prefixes_1(struct string_list
*out
,
1921 struct strbuf
*prefix
,
1922 const char **patterns
, size_t nr
)
1926 for (i
= 0; i
< nr
; i
++) {
1927 char c
= patterns
[i
][prefix
->len
];
1928 if (!c
|| is_glob_special(c
)) {
1929 string_list_append(out
, prefix
->buf
);
1939 * Set "end" to the index of the element _after_ the last one
1942 for (end
= i
+ 1; end
< nr
; end
++) {
1943 if (patterns
[i
][prefix
->len
] != patterns
[end
][prefix
->len
])
1947 strbuf_addch(prefix
, patterns
[i
][prefix
->len
]);
1948 find_longest_prefixes_1(out
, prefix
, patterns
+ i
, end
- i
);
1949 strbuf_setlen(prefix
, prefix
->len
- 1);
1955 static void find_longest_prefixes(struct string_list
*out
,
1956 const char **patterns
)
1958 struct strvec sorted
= STRVEC_INIT
;
1959 struct strbuf prefix
= STRBUF_INIT
;
1961 strvec_pushv(&sorted
, patterns
);
1962 QSORT(sorted
.v
, sorted
.nr
, qsort_strcmp
);
1964 find_longest_prefixes_1(out
, &prefix
, sorted
.v
, sorted
.nr
);
1966 strvec_clear(&sorted
);
1967 strbuf_release(&prefix
);
1971 * This is the same as for_each_fullref_in(), but it tries to iterate
1972 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1973 * pattern match, so the callback still has to match each ref individually.
1975 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1980 struct string_list prefixes
= STRING_LIST_INIT_DUP
;
1981 struct string_list_item
*prefix
;
1984 if (!filter
->match_as_path
) {
1986 * in this case, the patterns are applied after
1987 * prefixes like "refs/heads/" etc. are stripped off,
1988 * so we have to look at everything:
1990 return for_each_fullref_in("", cb
, cb_data
, broken
);
1993 if (filter
->ignore_case
) {
1995 * we can't handle case-insensitive comparisons,
1996 * so just return everything and let the caller
1999 return for_each_fullref_in("", cb
, cb_data
, broken
);
2002 if (!filter
->name_patterns
[0]) {
2003 /* no patterns; we have to look at everything */
2004 return for_each_fullref_in("", cb
, cb_data
, broken
);
2007 find_longest_prefixes(&prefixes
, filter
->name_patterns
);
2009 for_each_string_list_item(prefix
, &prefixes
) {
2010 ret
= for_each_fullref_in(prefix
->string
, cb
, cb_data
, broken
);
2015 string_list_clear(&prefixes
, 0);
2020 * Given a ref (oid, refname), check if the ref belongs to the array
2021 * of oids. If the given ref is a tag, check if the given tag points
2022 * at one of the oids in the given oid array.
2024 * 1. Only a single level of indirection is obtained, we might want to
2025 * change this to account for multiple levels (e.g. annotated tags
2026 * pointing to annotated tags pointing to a commit.)
2027 * 2. As the refs are cached we might know what refname peels to without
2028 * the need to parse the object via parse_object(). peel_ref() might be a
2029 * more efficient alternative to obtain the pointee.
2031 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
2032 const struct object_id
*oid
,
2033 const char *refname
)
2035 const struct object_id
*tagged_oid
= NULL
;
2038 if (oid_array_lookup(points_at
, oid
) >= 0)
2040 obj
= parse_object(the_repository
, oid
);
2042 die(_("malformed object at '%s'"), refname
);
2043 if (obj
->type
== OBJ_TAG
)
2044 tagged_oid
= get_tagged_oid((struct tag
*)obj
);
2045 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
2051 * Allocate space for a new ref_array_item and copy the name and oid to it.
2053 * Callers can then fill in other struct members at their leisure.
2055 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2056 const struct object_id
*oid
)
2058 struct ref_array_item
*ref
;
2060 FLEX_ALLOC_STR(ref
, refname
, refname
);
2061 oidcpy(&ref
->objectname
, oid
);
2066 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2067 const char *refname
,
2068 const struct object_id
*oid
)
2070 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2072 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2073 array
->items
[array
->nr
++] = ref
;
2078 static int ref_kind_from_refname(const char *refname
)
2086 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2087 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2088 { "refs/tags/", FILTER_REFS_TAGS
}
2091 if (!strcmp(refname
, "HEAD"))
2092 return FILTER_REFS_DETACHED_HEAD
;
2094 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2095 if (starts_with(refname
, ref_kind
[i
].prefix
))
2096 return ref_kind
[i
].kind
;
2099 return FILTER_REFS_OTHERS
;
2102 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2104 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2105 filter
->kind
== FILTER_REFS_REMOTES
||
2106 filter
->kind
== FILTER_REFS_TAGS
)
2107 return filter
->kind
;
2108 return ref_kind_from_refname(refname
);
2111 struct ref_filter_cbdata
{
2112 struct ref_array
*array
;
2113 struct ref_filter
*filter
;
2114 struct contains_cache contains_cache
;
2115 struct contains_cache no_contains_cache
;
2119 * A call-back given to for_each_ref(). Filter refs and keep them for
2120 * later object processing.
2122 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2124 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2125 struct ref_filter
*filter
= ref_cbdata
->filter
;
2126 struct ref_array_item
*ref
;
2127 struct commit
*commit
= NULL
;
2130 if (flag
& REF_BAD_NAME
) {
2131 warning(_("ignoring ref with broken name %s"), refname
);
2135 if (flag
& REF_ISBROKEN
) {
2136 warning(_("ignoring broken ref %s"), refname
);
2140 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2141 kind
= filter_ref_kind(filter
, refname
);
2142 if (!(kind
& filter
->kind
))
2145 if (!filter_pattern_match(filter
, refname
))
2148 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2152 * A merge filter is applied on refs pointing to commits. Hence
2153 * obtain the commit using the 'oid' available and discard all
2154 * non-commits early. The actual filtering is done later.
2156 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2157 commit
= lookup_commit_reference_gently(the_repository
, oid
,
2161 /* We perform the filtering for the '--contains' option... */
2162 if (filter
->with_commit
&&
2163 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2165 /* ...or for the `--no-contains' option */
2166 if (filter
->no_commit
&&
2167 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2172 * We do not open the object yet; sort may only need refname
2173 * to do its job and the resulting list may yet to be pruned
2174 * by maxcount logic.
2176 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2177 ref
->commit
= commit
;
2184 /* Free memory allocated for a ref_array_item */
2185 static void free_array_item(struct ref_array_item
*item
)
2187 free((char *)item
->symref
);
2190 for (i
= 0; i
< used_atom_cnt
; i
++)
2191 free((char *)item
->value
[i
].s
);
2197 /* Free all memory allocated for ref_array */
2198 void ref_array_clear(struct ref_array
*array
)
2202 for (i
= 0; i
< array
->nr
; i
++)
2203 free_array_item(array
->items
[i
]);
2204 FREE_AND_NULL(array
->items
);
2205 array
->nr
= array
->alloc
= 0;
2207 for (i
= 0; i
< used_atom_cnt
; i
++)
2208 free((char *)used_atom
[i
].name
);
2209 FREE_AND_NULL(used_atom
);
2212 if (ref_to_worktree_map
.worktrees
) {
2213 hashmap_free_entries(&(ref_to_worktree_map
.map
),
2214 struct ref_to_worktree_entry
, ent
);
2215 free_worktrees(ref_to_worktree_map
.worktrees
);
2216 ref_to_worktree_map
.worktrees
= NULL
;
2220 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
2222 struct rev_info revs
;
2224 struct ref_filter
*filter
= ref_cbdata
->filter
;
2225 struct ref_array
*array
= ref_cbdata
->array
;
2226 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
2228 repo_init_revisions(the_repository
, &revs
, NULL
);
2230 for (i
= 0; i
< array
->nr
; i
++) {
2231 struct ref_array_item
*item
= array
->items
[i
];
2232 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
2233 to_clear
[i
] = item
->commit
;
2236 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
2237 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
2240 if (prepare_revision_walk(&revs
))
2241 die(_("revision walk setup failed"));
2246 for (i
= 0; i
< old_nr
; i
++) {
2247 struct ref_array_item
*item
= array
->items
[i
];
2248 struct commit
*commit
= item
->commit
;
2250 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2252 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
2253 array
->items
[array
->nr
++] = array
->items
[i
];
2255 free_array_item(item
);
2258 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2259 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
2264 * API for filtering a set of refs. Based on the type of refs the user
2265 * has requested, we iterate through those refs and apply filters
2266 * as per the given ref_filter structure and finally store the
2267 * filtered refs in the ref_array structure.
2269 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2271 struct ref_filter_cbdata ref_cbdata
;
2273 unsigned int broken
= 0;
2275 ref_cbdata
.array
= array
;
2276 ref_cbdata
.filter
= filter
;
2278 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
2280 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2282 init_contains_cache(&ref_cbdata
.contains_cache
);
2283 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2285 /* Simple per-ref filtering */
2287 die("filter_refs: invalid type");
2290 * For common cases where we need only branches or remotes or tags,
2291 * we only iterate through those refs. If a mix of refs is needed,
2292 * we iterate over all refs and filter out required refs with the help
2293 * of filter_ref_kind().
2295 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2296 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
2297 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2298 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2299 else if (filter
->kind
== FILTER_REFS_TAGS
)
2300 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2301 else if (filter
->kind
& FILTER_REFS_ALL
)
2302 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2303 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2304 head_ref(ref_filter_handler
, &ref_cbdata
);
2307 clear_contains_cache(&ref_cbdata
.contains_cache
);
2308 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2310 /* Filters that need revision walking */
2311 if (filter
->merge_commit
)
2312 do_merge_filter(&ref_cbdata
);
2317 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2319 struct atom_value
*va
, *vb
;
2321 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2322 int (*cmp_fn
)(const char *, const char *);
2323 struct strbuf err
= STRBUF_INIT
;
2325 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2327 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2329 strbuf_release(&err
);
2330 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2332 cmp
= versioncmp(va
->s
, vb
->s
);
2333 else if (cmp_type
== FIELD_STR
)
2334 cmp
= cmp_fn(va
->s
, vb
->s
);
2336 if (va
->value
< vb
->value
)
2338 else if (va
->value
== vb
->value
)
2344 return (s
->reverse
) ? -cmp
: cmp
;
2347 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2349 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2350 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2351 struct ref_sorting
*s
;
2353 for (s
= ref_sorting
; s
; s
= s
->next
) {
2354 int cmp
= cmp_ref_sorting(s
, a
, b
);
2359 return s
&& s
->ignore_case
?
2360 strcasecmp(a
->refname
, b
->refname
) :
2361 strcmp(a
->refname
, b
->refname
);
2364 void ref_sorting_icase_all(struct ref_sorting
*sorting
, int flag
)
2366 for (; sorting
; sorting
= sorting
->next
)
2367 sorting
->ignore_case
= !!flag
;
2370 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2372 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2375 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2377 struct strbuf
*s
= &state
->stack
->output
;
2379 while (*cp
&& (!ep
|| cp
< ep
)) {
2384 int ch
= hex2chr(cp
+ 1);
2386 strbuf_addch(s
, ch
);
2392 strbuf_addch(s
, *cp
);
2397 int format_ref_array_item(struct ref_array_item
*info
,
2398 const struct ref_format
*format
,
2399 struct strbuf
*final_buf
,
2400 struct strbuf
*error_buf
)
2402 const char *cp
, *sp
, *ep
;
2403 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2405 state
.quote_style
= format
->quote_style
;
2406 push_stack_element(&state
.stack
);
2408 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2409 struct atom_value
*atomv
;
2412 ep
= strchr(sp
, ')');
2414 append_literal(cp
, sp
, &state
);
2415 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2416 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2417 atomv
->handler(atomv
, &state
, error_buf
)) {
2418 pop_stack_element(&state
.stack
);
2423 sp
= cp
+ strlen(cp
);
2424 append_literal(cp
, sp
, &state
);
2426 if (format
->need_color_reset_at_eol
) {
2427 struct atom_value resetv
;
2428 resetv
.s
= GIT_COLOR_RESET
;
2429 if (append_atom(&resetv
, &state
, error_buf
)) {
2430 pop_stack_element(&state
.stack
);
2434 if (state
.stack
->prev
) {
2435 pop_stack_element(&state
.stack
);
2436 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2438 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2439 pop_stack_element(&state
.stack
);
2443 void show_ref_array_item(struct ref_array_item
*info
,
2444 const struct ref_format
*format
)
2446 struct strbuf final_buf
= STRBUF_INIT
;
2447 struct strbuf error_buf
= STRBUF_INIT
;
2449 if (format_ref_array_item(info
, format
, &final_buf
, &error_buf
))
2450 die("%s", error_buf
.buf
);
2451 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2452 strbuf_release(&error_buf
);
2453 strbuf_release(&final_buf
);
2457 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2458 const struct ref_format
*format
)
2460 struct ref_array_item
*ref_item
;
2461 ref_item
= new_ref_array_item(name
, oid
);
2462 ref_item
->kind
= ref_kind_from_refname(name
);
2463 show_ref_array_item(ref_item
, format
);
2464 free_array_item(ref_item
);
2467 static int parse_sorting_atom(const char *atom
)
2470 * This parses an atom using a dummy ref_format, since we don't
2471 * actually care about the formatting details.
2473 struct ref_format dummy
= REF_FORMAT_INIT
;
2474 const char *end
= atom
+ strlen(atom
);
2475 struct strbuf err
= STRBUF_INIT
;
2476 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2479 strbuf_release(&err
);
2483 /* If no sorting option is given, use refname to sort as default */
2484 struct ref_sorting
*ref_default_sorting(void)
2486 static const char cstr_name
[] = "refname";
2488 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2490 sorting
->next
= NULL
;
2491 sorting
->atom
= parse_sorting_atom(cstr_name
);
2495 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2497 struct ref_sorting
*s
;
2499 s
= xcalloc(1, sizeof(*s
));
2500 s
->next
= *sorting_tail
;
2507 if (skip_prefix(arg
, "version:", &arg
) ||
2508 skip_prefix(arg
, "v:", &arg
))
2510 s
->atom
= parse_sorting_atom(arg
);
2513 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2516 * NEEDSWORK: We should probably clear the list in this case, but we've
2517 * already munged the global used_atoms list, which would need to be
2520 BUG_ON_OPT_NEG(unset
);
2522 parse_ref_sorting(opt
->value
, arg
);
2526 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2528 struct ref_filter
*rf
= opt
->value
;
2529 struct object_id oid
;
2530 int no_merged
= starts_with(opt
->long_name
, "no");
2532 BUG_ON_OPT_NEG(unset
);
2536 return error(_("option `%s' is incompatible with --merged"),
2539 return error(_("option `%s' is incompatible with --no-merged"),
2544 rf
->merge
= no_merged
2545 ? REF_FILTER_MERGED_OMIT
2546 : REF_FILTER_MERGED_INCLUDE
;
2548 if (get_oid(arg
, &oid
))
2549 die(_("malformed object name %s"), arg
);
2551 rf
->merge_commit
= lookup_commit_reference_gently(the_repository
,
2553 if (!rf
->merge_commit
)
2554 return error(_("option `%s' must point to a commit"), opt
->long_name
);