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
, C_LINES
,
131 C_SIG
, C_SUB
, C_SUB_SANITIZE
, 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 atom
->u
.contents
.option
= C_SUB
;
306 else if (!strcmp(arg
, "sanitize"))
307 atom
->u
.contents
.option
= C_SUB_SANITIZE
;
309 return strbuf_addf_ret(err
, -1, _("unrecognized %%(subject) argument: %s"), arg
);
313 static int trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
314 const char *arg
, struct strbuf
*err
)
316 struct string_list params
= STRING_LIST_INIT_DUP
;
319 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
322 string_list_split(¶ms
, arg
, ',', -1);
323 for (i
= 0; i
< params
.nr
; i
++) {
324 const char *s
= params
.items
[i
].string
;
325 if (!strcmp(s
, "unfold"))
326 atom
->u
.contents
.trailer_opts
.unfold
= 1;
327 else if (!strcmp(s
, "only"))
328 atom
->u
.contents
.trailer_opts
.only_trailers
= 1;
330 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), s
);
331 string_list_clear(¶ms
, 0);
336 atom
->u
.contents
.option
= C_TRAILERS
;
337 string_list_clear(¶ms
, 0);
341 static int contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
342 const char *arg
, struct strbuf
*err
)
345 atom
->u
.contents
.option
= C_BARE
;
346 else if (!strcmp(arg
, "body"))
347 atom
->u
.contents
.option
= C_BODY
;
348 else if (!strcmp(arg
, "size"))
349 atom
->u
.contents
.option
= C_LENGTH
;
350 else if (!strcmp(arg
, "signature"))
351 atom
->u
.contents
.option
= C_SIG
;
352 else if (!strcmp(arg
, "subject"))
353 atom
->u
.contents
.option
= C_SUB
;
354 else if (!strcmp(arg
, "trailers")) {
355 if (trailers_atom_parser(format
, atom
, NULL
, err
))
357 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
358 if (trailers_atom_parser(format
, atom
, arg
, err
))
360 } else if (skip_prefix(arg
, "lines=", &arg
)) {
361 atom
->u
.contents
.option
= C_LINES
;
362 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
363 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
365 return strbuf_addf_ret(err
, -1, _("unrecognized %%(contents) argument: %s"), arg
);
369 static int oid_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
370 const char *arg
, struct strbuf
*err
)
373 atom
->u
.oid
.option
= O_FULL
;
374 else if (!strcmp(arg
, "short"))
375 atom
->u
.oid
.option
= O_SHORT
;
376 else if (skip_prefix(arg
, "short=", &arg
)) {
377 atom
->u
.oid
.option
= O_LENGTH
;
378 if (strtoul_ui(arg
, 10, &atom
->u
.oid
.length
) ||
379 atom
->u
.oid
.length
== 0)
380 return strbuf_addf_ret(err
, -1, _("positive value expected '%s' in %%(%s)"), arg
, atom
->name
);
381 if (atom
->u
.oid
.length
< MINIMUM_ABBREV
)
382 atom
->u
.oid
.length
= MINIMUM_ABBREV
;
384 return strbuf_addf_ret(err
, -1, _("unrecognized argument '%s' in %%(%s)"), arg
, atom
->name
);
388 static int person_email_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
389 const char *arg
, struct strbuf
*err
)
392 atom
->u
.email_option
.option
= EO_RAW
;
393 else if (!strcmp(arg
, "trim"))
394 atom
->u
.email_option
.option
= EO_TRIM
;
395 else if (!strcmp(arg
, "localpart"))
396 atom
->u
.email_option
.option
= EO_LOCALPART
;
398 return strbuf_addf_ret(err
, -1, _("unrecognized email option: %s"), arg
);
402 static int refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
403 const char *arg
, struct strbuf
*err
)
405 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
408 static align_type
parse_align_position(const char *s
)
410 if (!strcmp(s
, "right"))
412 else if (!strcmp(s
, "middle"))
414 else if (!strcmp(s
, "left"))
419 static int align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
420 const char *arg
, struct strbuf
*err
)
422 struct align
*align
= &atom
->u
.align
;
423 struct string_list params
= STRING_LIST_INIT_DUP
;
425 unsigned int width
= ~0U;
428 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
430 align
->position
= ALIGN_LEFT
;
432 string_list_split(¶ms
, arg
, ',', -1);
433 for (i
= 0; i
< params
.nr
; i
++) {
434 const char *s
= params
.items
[i
].string
;
437 if (skip_prefix(s
, "position=", &s
)) {
438 position
= parse_align_position(s
);
440 strbuf_addf(err
, _("unrecognized position:%s"), s
);
441 string_list_clear(¶ms
, 0);
444 align
->position
= position
;
445 } else if (skip_prefix(s
, "width=", &s
)) {
446 if (strtoul_ui(s
, 10, &width
)) {
447 strbuf_addf(err
, _("unrecognized width:%s"), s
);
448 string_list_clear(¶ms
, 0);
451 } else if (!strtoul_ui(s
, 10, &width
))
453 else if ((position
= parse_align_position(s
)) >= 0)
454 align
->position
= position
;
456 strbuf_addf(err
, _("unrecognized %%(align) argument: %s"), s
);
457 string_list_clear(¶ms
, 0);
463 string_list_clear(¶ms
, 0);
464 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
466 align
->width
= width
;
467 string_list_clear(¶ms
, 0);
471 static int if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
472 const char *arg
, struct strbuf
*err
)
475 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
477 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
478 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
479 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
480 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
482 return strbuf_addf_ret(err
, -1, _("unrecognized %%(if) argument: %s"), arg
);
486 static int head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
487 const char *arg
, struct strbuf
*unused_err
)
489 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
497 int (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
,
498 const char *arg
, struct strbuf
*err
);
500 { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
501 { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
502 { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
503 { "objectname", SOURCE_OTHER
, FIELD_STR
, oid_atom_parser
},
504 { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
505 { "tree", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
506 { "parent", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
507 { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
508 { "object", SOURCE_OBJ
},
509 { "type", SOURCE_OBJ
},
510 { "tag", SOURCE_OBJ
},
511 { "author", SOURCE_OBJ
},
512 { "authorname", SOURCE_OBJ
},
513 { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
514 { "authordate", SOURCE_OBJ
, FIELD_TIME
},
515 { "committer", SOURCE_OBJ
},
516 { "committername", SOURCE_OBJ
},
517 { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
518 { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
519 { "tagger", SOURCE_OBJ
},
520 { "taggername", SOURCE_OBJ
},
521 { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
522 { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
523 { "creator", SOURCE_OBJ
},
524 { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
525 { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
526 { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
527 { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
528 { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
529 { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
530 { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
531 { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
532 { "flag", SOURCE_NONE
},
533 { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
534 { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
535 { "worktreepath", SOURCE_NONE
},
536 { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
537 { "end", SOURCE_NONE
},
538 { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
539 { "then", SOURCE_NONE
},
540 { "else", SOURCE_NONE
},
542 * Please update $__git_ref_fieldlist in git-completion.bash
543 * when you add new atoms
547 #define REF_FORMATTING_STATE_INIT { 0, NULL }
549 struct ref_formatting_stack
{
550 struct ref_formatting_stack
*prev
;
551 struct strbuf output
;
552 void (*at_end
)(struct ref_formatting_stack
**stack
);
556 struct ref_formatting_state
{
558 struct ref_formatting_stack
*stack
;
563 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
565 uintmax_t value
; /* used for sorting when not FIELD_STR */
566 struct used_atom
*atom
;
570 * Used to parse format string and sort specifiers
572 static int parse_ref_filter_atom(const struct ref_format
*format
,
573 const char *atom
, const char *ep
,
581 if (*sp
== '*' && sp
< ep
)
584 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
585 (int)(ep
-atom
), atom
);
587 /* Do we have the atom already used elsewhere? */
588 for (i
= 0; i
< used_atom_cnt
; i
++) {
589 int len
= strlen(used_atom
[i
].name
);
590 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
595 * If the atom name has a colon, strip it and everything after
596 * it off - it specifies the format for this entry, and
597 * shouldn't be used for checking against the valid_atom
600 arg
= memchr(sp
, ':', ep
- sp
);
601 atom_len
= (arg
? arg
: ep
) - sp
;
603 /* Is the atom a valid one? */
604 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
605 int len
= strlen(valid_atom
[i
].name
);
606 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
610 if (ARRAY_SIZE(valid_atom
) <= i
)
611 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
612 (int)(ep
-atom
), atom
);
613 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
614 return strbuf_addf_ret(err
, -1,
615 _("not a git repository, but the field '%.*s' requires access to object data"),
616 (int)(ep
-atom
), atom
);
618 /* Add it in, including the deref prefix */
621 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
622 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
623 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
624 used_atom
[at
].source
= valid_atom
[i
].source
;
625 if (used_atom
[at
].source
== SOURCE_OBJ
) {
627 oi_deref
.info
.contentp
= &oi_deref
.content
;
629 oi
.info
.contentp
= &oi
.content
;
632 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
635 * Treat empty sub-arguments list as NULL (i.e.,
636 * "%(atom:)" is equivalent to "%(atom)").
641 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
642 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
646 if (!strcmp(valid_atom
[i
].name
, "symref"))
651 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
653 switch (quote_style
) {
655 strbuf_addstr(s
, str
);
658 sq_quote_buf(s
, str
);
661 perl_quote_buf(s
, str
);
664 python_quote_buf(s
, str
);
667 tcl_quote_buf(s
, str
);
672 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
673 struct strbuf
*unused_err
)
676 * Quote formatting is only done when the stack has a single
677 * element. Otherwise quote formatting is done on the
678 * element's entire output strbuf when the %(end) atom is
681 if (!state
->stack
->prev
)
682 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
684 strbuf_addstr(&state
->stack
->output
, v
->s
);
688 static void push_stack_element(struct ref_formatting_stack
**stack
)
690 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
692 strbuf_init(&s
->output
, 0);
697 static void pop_stack_element(struct ref_formatting_stack
**stack
)
699 struct ref_formatting_stack
*current
= *stack
;
700 struct ref_formatting_stack
*prev
= current
->prev
;
703 strbuf_addbuf(&prev
->output
, ¤t
->output
);
704 strbuf_release(¤t
->output
);
709 static void end_align_handler(struct ref_formatting_stack
**stack
)
711 struct ref_formatting_stack
*cur
= *stack
;
712 struct align
*align
= (struct align
*)cur
->at_end_data
;
713 struct strbuf s
= STRBUF_INIT
;
715 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
716 strbuf_swap(&cur
->output
, &s
);
720 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
721 struct strbuf
*unused_err
)
723 struct ref_formatting_stack
*new_stack
;
725 push_stack_element(&state
->stack
);
726 new_stack
= state
->stack
;
727 new_stack
->at_end
= end_align_handler
;
728 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
732 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
734 struct ref_formatting_stack
*cur
= *stack
;
735 struct ref_formatting_stack
*prev
= cur
->prev
;
736 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
738 if (!if_then_else
->then_atom_seen
)
739 die(_("format: %%(if) atom used without a %%(then) atom"));
741 if (if_then_else
->else_atom_seen
) {
743 * There is an %(else) atom: we need to drop one state from the
744 * stack, either the %(else) branch if the condition is satisfied, or
745 * the %(then) branch if it isn't.
747 if (if_then_else
->condition_satisfied
) {
748 strbuf_reset(&cur
->output
);
749 pop_stack_element(&cur
);
751 strbuf_swap(&cur
->output
, &prev
->output
);
752 strbuf_reset(&cur
->output
);
753 pop_stack_element(&cur
);
755 } else if (!if_then_else
->condition_satisfied
) {
757 * No %(else) atom: just drop the %(then) branch if the
758 * condition is not satisfied.
760 strbuf_reset(&cur
->output
);
767 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
768 struct strbuf
*unused_err
)
770 struct ref_formatting_stack
*new_stack
;
771 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
773 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
774 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
776 push_stack_element(&state
->stack
);
777 new_stack
= state
->stack
;
778 new_stack
->at_end
= if_then_else_handler
;
779 new_stack
->at_end_data
= if_then_else
;
783 static int is_empty(const char *s
)
793 static int then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
796 struct ref_formatting_stack
*cur
= state
->stack
;
797 struct if_then_else
*if_then_else
= NULL
;
799 if (cur
->at_end
== if_then_else_handler
)
800 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
802 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used without an %%(if) atom"));
803 if (if_then_else
->then_atom_seen
)
804 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
805 if (if_then_else
->else_atom_seen
)
806 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
807 if_then_else
->then_atom_seen
= 1;
809 * If the 'equals' or 'notequals' attribute is used then
810 * perform the required comparison. If not, only non-empty
811 * strings satisfy the 'if' condition.
813 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
814 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
815 if_then_else
->condition_satisfied
= 1;
816 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
817 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
818 if_then_else
->condition_satisfied
= 1;
819 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
820 if_then_else
->condition_satisfied
= 1;
821 strbuf_reset(&cur
->output
);
825 static int else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
828 struct ref_formatting_stack
*prev
= state
->stack
;
829 struct if_then_else
*if_then_else
= NULL
;
831 if (prev
->at_end
== if_then_else_handler
)
832 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
834 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without an %%(if) atom"));
835 if (!if_then_else
->then_atom_seen
)
836 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without a %%(then) atom"));
837 if (if_then_else
->else_atom_seen
)
838 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
839 if_then_else
->else_atom_seen
= 1;
840 push_stack_element(&state
->stack
);
841 state
->stack
->at_end_data
= prev
->at_end_data
;
842 state
->stack
->at_end
= prev
->at_end
;
846 static int end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
849 struct ref_formatting_stack
*current
= state
->stack
;
850 struct strbuf s
= STRBUF_INIT
;
852 if (!current
->at_end
)
853 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
854 current
->at_end(&state
->stack
);
856 /* Stack may have been popped within at_end(), hence reset the current pointer */
857 current
= state
->stack
;
860 * Perform quote formatting when the stack element is that of
861 * a supporting atom. If nested then perform quote formatting
862 * only on the topmost supporting atom.
864 if (!current
->prev
->prev
) {
865 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
866 strbuf_swap(¤t
->output
, &s
);
869 pop_stack_element(&state
->stack
);
874 * In a format string, find the next occurrence of %(atom).
876 static const char *find_next(const char *cp
)
881 * %( is the start of an atom;
882 * %% is a quoted per-cent.
886 else if (cp
[1] == '%')
887 cp
++; /* skip over two % */
888 /* otherwise this is a singleton, literal % */
896 * Make sure the format string is well formed, and parse out
899 int verify_ref_format(struct ref_format
*format
)
903 format
->need_color_reset_at_eol
= 0;
904 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
905 struct strbuf err
= STRBUF_INIT
;
906 const char *color
, *ep
= strchr(sp
, ')');
910 return error(_("malformed format string %s"), sp
);
911 /* sp points at "%(" and ep points at the closing ")" */
912 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
917 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
918 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
919 strbuf_release(&err
);
921 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
922 format
->need_color_reset_at_eol
= 0;
926 static const char *do_grab_oid(const char *field
, const struct object_id
*oid
,
927 struct used_atom
*atom
)
929 switch (atom
->u
.oid
.option
) {
931 return oid_to_hex(oid
);
933 return find_unique_abbrev(oid
, atom
->u
.oid
.length
);
935 return find_unique_abbrev(oid
, DEFAULT_ABBREV
);
937 BUG("unknown %%(%s) option", field
);
941 static int grab_oid(const char *name
, const char *field
, const struct object_id
*oid
,
942 struct atom_value
*v
, struct used_atom
*atom
)
944 if (starts_with(name
, field
)) {
945 v
->s
= xstrdup(do_grab_oid(field
, oid
, atom
));
951 /* See grab_values */
952 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
956 for (i
= 0; i
< used_atom_cnt
; i
++) {
957 const char *name
= used_atom
[i
].name
;
958 struct atom_value
*v
= &val
[i
];
959 if (!!deref
!= (*name
== '*'))
963 if (!strcmp(name
, "objecttype"))
964 v
->s
= xstrdup(type_name(oi
->type
));
965 else if (!strcmp(name
, "objectsize:disk")) {
966 v
->value
= oi
->disk_size
;
967 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
968 } else if (!strcmp(name
, "objectsize")) {
970 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
971 } else if (!strcmp(name
, "deltabase"))
972 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
974 grab_oid(name
, "objectname", &oi
->oid
, v
, &used_atom
[i
]);
978 /* See grab_values */
979 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
982 struct tag
*tag
= (struct tag
*) obj
;
984 for (i
= 0; i
< used_atom_cnt
; i
++) {
985 const char *name
= used_atom
[i
].name
;
986 struct atom_value
*v
= &val
[i
];
987 if (!!deref
!= (*name
== '*'))
991 if (!strcmp(name
, "tag"))
992 v
->s
= xstrdup(tag
->tag
);
993 else if (!strcmp(name
, "type") && tag
->tagged
)
994 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
995 else if (!strcmp(name
, "object") && tag
->tagged
)
996 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
1000 /* See grab_values */
1001 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1004 struct commit
*commit
= (struct commit
*) obj
;
1006 for (i
= 0; i
< used_atom_cnt
; i
++) {
1007 const char *name
= used_atom
[i
].name
;
1008 struct atom_value
*v
= &val
[i
];
1009 if (!!deref
!= (*name
== '*'))
1013 if (grab_oid(name
, "tree", get_commit_tree_oid(commit
), v
, &used_atom
[i
]))
1015 if (!strcmp(name
, "numparent")) {
1016 v
->value
= commit_list_count(commit
->parents
);
1017 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1019 else if (starts_with(name
, "parent")) {
1020 struct commit_list
*parents
;
1021 struct strbuf s
= STRBUF_INIT
;
1022 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1023 struct object_id
*oid
= &parents
->item
->object
.oid
;
1024 if (parents
!= commit
->parents
)
1025 strbuf_addch(&s
, ' ');
1026 strbuf_addstr(&s
, do_grab_oid("parent", oid
, &used_atom
[i
]));
1028 v
->s
= strbuf_detach(&s
, NULL
);
1033 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1037 if (!strncmp(buf
, who
, wholen
) &&
1039 return buf
+ wholen
+ 1;
1040 eol
= strchr(buf
, '\n');
1045 return ""; /* end of header */
1051 static const char *copy_line(const char *buf
)
1053 const char *eol
= strchrnul(buf
, '\n');
1054 return xmemdupz(buf
, eol
- buf
);
1057 static const char *copy_name(const char *buf
)
1060 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1061 if (!strncmp(cp
, " <", 2))
1062 return xmemdupz(buf
, cp
- buf
);
1067 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1069 const char *email
= strchr(buf
, '<');
1070 const char *eoemail
;
1073 switch (atom
->u
.email_option
.option
) {
1075 eoemail
= strchr(email
, '>');
1081 eoemail
= strchr(email
, '>');
1085 eoemail
= strchr(email
, '@');
1087 eoemail
= strchr(email
, '>');
1090 BUG("unknown email option");
1095 return xmemdupz(email
, eoemail
- email
);
1098 static char *copy_subject(const char *buf
, unsigned long len
)
1100 struct strbuf sb
= STRBUF_INIT
;
1103 for (i
= 0; i
< len
; i
++) {
1104 if (buf
[i
] == '\r' && i
+ 1 < len
&& buf
[i
+ 1] == '\n')
1105 continue; /* ignore CR in CRLF */
1108 strbuf_addch(&sb
, ' ');
1110 strbuf_addch(&sb
, buf
[i
]);
1112 return strbuf_detach(&sb
, NULL
);
1115 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1117 const char *eoemail
= strstr(buf
, "> ");
1119 timestamp_t timestamp
;
1121 struct date_mode date_mode
= { DATE_NORMAL
};
1122 const char *formatp
;
1125 * We got here because atomname ends in "date" or "date<something>";
1126 * it's not possible that <something> is not ":<format>" because
1127 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1128 * ":" means no format is specified, and use the default.
1130 formatp
= strchr(atomname
, ':');
1131 if (formatp
!= NULL
) {
1133 parse_date_format(formatp
, &date_mode
);
1138 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1139 if (timestamp
== TIME_MAX
)
1141 tz
= strtol(zone
, NULL
, 10);
1142 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1144 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1145 v
->value
= timestamp
;
1152 /* See grab_values */
1153 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1156 int wholen
= strlen(who
);
1157 const char *wholine
= NULL
;
1159 for (i
= 0; i
< used_atom_cnt
; i
++) {
1160 const char *name
= used_atom
[i
].name
;
1161 struct atom_value
*v
= &val
[i
];
1162 if (!!deref
!= (*name
== '*'))
1166 if (strncmp(who
, name
, wholen
))
1168 if (name
[wholen
] != 0 &&
1169 strcmp(name
+ wholen
, "name") &&
1170 !starts_with(name
+ wholen
, "email") &&
1171 !starts_with(name
+ wholen
, "date"))
1174 wholine
= find_wholine(who
, wholen
, buf
);
1176 return; /* no point looking for it */
1177 if (name
[wholen
] == 0)
1178 v
->s
= copy_line(wholine
);
1179 else if (!strcmp(name
+ wholen
, "name"))
1180 v
->s
= copy_name(wholine
);
1181 else if (starts_with(name
+ wholen
, "email"))
1182 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1183 else if (starts_with(name
+ wholen
, "date"))
1184 grab_date(wholine
, v
, name
);
1188 * For a tag or a commit object, if "creator" or "creatordate" is
1189 * requested, do something special.
1191 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1192 return; /* "author" for commit object is not wanted */
1194 wholine
= find_wholine(who
, wholen
, buf
);
1197 for (i
= 0; i
< used_atom_cnt
; i
++) {
1198 const char *name
= used_atom
[i
].name
;
1199 struct atom_value
*v
= &val
[i
];
1200 if (!!deref
!= (*name
== '*'))
1205 if (starts_with(name
, "creatordate"))
1206 grab_date(wholine
, v
, name
);
1207 else if (!strcmp(name
, "creator"))
1208 v
->s
= copy_line(wholine
);
1212 static void find_subpos(const char *buf
,
1213 const char **sub
, size_t *sublen
,
1214 const char **body
, size_t *bodylen
,
1216 const char **sig
, size_t *siglen
)
1218 struct strbuf payload
= STRBUF_INIT
;
1219 struct strbuf signature
= STRBUF_INIT
;
1221 const char *end
= buf
+ strlen(buf
);
1222 const char *sigstart
;
1225 /* skip past header until we hit empty line */
1226 while (*buf
&& *buf
!= '\n') {
1227 eol
= strchrnul(buf
, '\n');
1232 /* skip any empty lines */
1233 while (*buf
== '\n')
1236 /* parse signature first; we might not even have a subject line */
1237 parse_signature(buf
, end
- buf
, &payload
, &signature
);
1238 *sig
= strbuf_detach(&signature
, siglen
);
1239 sigstart
= buf
+ parse_signed_buffer(buf
, strlen(buf
));
1241 /* subject is first non-empty line */
1243 /* subject goes to first empty line before signature begins */
1244 if ((eol
= strstr(*sub
, "\n\n"))) {
1245 eol
= eol
< sigstart
? eol
: sigstart
;
1246 /* check if message uses CRLF */
1247 } else if (! (eol
= strstr(*sub
, "\r\n\r\n"))) {
1248 /* treat whole message as subject */
1249 eol
= strrchr(*sub
, '\0');
1252 *sublen
= buf
- *sub
;
1253 /* drop trailing newline, if present */
1254 while (*sublen
&& ((*sub
)[*sublen
- 1] == '\n' ||
1255 (*sub
)[*sublen
- 1] == '\r'))
1258 /* skip any empty lines */
1259 while (*buf
== '\n' || *buf
== '\r')
1262 *bodylen
= strlen(buf
);
1263 *nonsiglen
= sigstart
- buf
;
1267 * If 'lines' is greater than 0, append that many lines from the given
1268 * 'buf' of length 'size' to the given strbuf.
1270 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1273 const char *sp
, *eol
;
1278 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1280 strbuf_addstr(out
, "\n ");
1281 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1282 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1283 strbuf_add(out
, sp
, len
);
1290 /* See grab_values */
1291 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, void *buf
)
1294 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1295 size_t sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1297 for (i
= 0; i
< used_atom_cnt
; i
++) {
1298 struct used_atom
*atom
= &used_atom
[i
];
1299 const char *name
= atom
->name
;
1300 struct atom_value
*v
= &val
[i
];
1302 if (!!deref
!= (*name
== '*'))
1306 if (strcmp(name
, "body") &&
1307 !starts_with(name
, "subject") &&
1308 !starts_with(name
, "trailers") &&
1309 !starts_with(name
, "contents"))
1314 &bodypos
, &bodylen
, &nonsiglen
,
1317 if (atom
->u
.contents
.option
== C_SUB
)
1318 v
->s
= copy_subject(subpos
, sublen
);
1319 else if (atom
->u
.contents
.option
== C_SUB_SANITIZE
) {
1320 struct strbuf sb
= STRBUF_INIT
;
1321 format_sanitized_subject(&sb
, subpos
, sublen
);
1322 v
->s
= strbuf_detach(&sb
, NULL
);
1323 } else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1324 v
->s
= xmemdupz(bodypos
, bodylen
);
1325 else if (atom
->u
.contents
.option
== C_LENGTH
)
1326 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)strlen(subpos
));
1327 else if (atom
->u
.contents
.option
== C_BODY
)
1328 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1329 else if (atom
->u
.contents
.option
== C_SIG
)
1330 v
->s
= xmemdupz(sigpos
, siglen
);
1331 else if (atom
->u
.contents
.option
== C_LINES
) {
1332 struct strbuf s
= STRBUF_INIT
;
1333 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1335 /* Size is the length of the message after removing the signature */
1336 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1337 v
->s
= strbuf_detach(&s
, NULL
);
1338 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1339 struct strbuf s
= STRBUF_INIT
;
1341 /* Format the trailer info according to the trailer_opts given */
1342 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1344 v
->s
= strbuf_detach(&s
, NULL
);
1345 } else if (atom
->u
.contents
.option
== C_BARE
)
1346 v
->s
= xstrdup(subpos
);
1349 free((void *)sigpos
);
1353 * We want to have empty print-string for field requests
1354 * that do not apply (e.g. "authordate" for a tag object)
1356 static void fill_missing_values(struct atom_value
*val
)
1359 for (i
= 0; i
< used_atom_cnt
; i
++) {
1360 struct atom_value
*v
= &val
[i
];
1367 * val is a list of atom_value to hold returned values. Extract
1368 * the values for atoms in used_atom array out of (obj, buf, sz).
1369 * when deref is false, (obj, buf, sz) is the object that is
1370 * pointed at by the ref itself; otherwise it is the object the
1371 * ref (which is a tag) refers to.
1373 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
)
1375 switch (obj
->type
) {
1377 grab_tag_values(val
, deref
, obj
);
1378 grab_sub_body_contents(val
, deref
, buf
);
1379 grab_person("tagger", val
, deref
, buf
);
1382 grab_commit_values(val
, deref
, obj
);
1383 grab_sub_body_contents(val
, deref
, buf
);
1384 grab_person("author", val
, deref
, buf
);
1385 grab_person("committer", val
, deref
, buf
);
1388 /* grab_tree_values(val, deref, obj, buf, sz); */
1391 /* grab_blob_values(val, deref, obj, buf, sz); */
1394 die("Eh? Object of type %d?", obj
->type
);
1398 static inline char *copy_advance(char *dst
, const char *src
)
1405 static const char *lstrip_ref_components(const char *refname
, int len
)
1407 long remaining
= len
;
1408 const char *start
= xstrdup(refname
);
1409 const char *to_free
= start
;
1413 const char *p
= refname
;
1415 /* Find total no of '/' separated path-components */
1416 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1419 * The number of components we need to strip is now
1420 * the total minus the components to be left (Plus one
1421 * because we count the number of '/', but the number
1422 * of components is one more than the no of '/').
1424 remaining
= i
+ len
+ 1;
1427 while (remaining
> 0) {
1430 free((char *)to_free
);
1438 start
= xstrdup(start
);
1439 free((char *)to_free
);
1443 static const char *rstrip_ref_components(const char *refname
, int len
)
1445 long remaining
= len
;
1446 const char *start
= xstrdup(refname
);
1447 const char *to_free
= start
;
1451 const char *p
= refname
;
1453 /* Find total no of '/' separated path-components */
1454 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1457 * The number of components we need to strip is now
1458 * the total minus the components to be left (Plus one
1459 * because we count the number of '/', but the number
1460 * of components is one more than the no of '/').
1462 remaining
= i
+ len
+ 1;
1465 while (remaining
-- > 0) {
1466 char *p
= strrchr(start
, '/');
1468 free((char *)to_free
);
1476 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1478 if (atom
->option
== R_SHORT
)
1479 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1480 else if (atom
->option
== R_LSTRIP
)
1481 return lstrip_ref_components(refname
, atom
->lstrip
);
1482 else if (atom
->option
== R_RSTRIP
)
1483 return rstrip_ref_components(refname
, atom
->rstrip
);
1485 return xstrdup(refname
);
1488 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1489 struct branch
*branch
, const char **s
)
1491 int num_ours
, num_theirs
;
1492 if (atom
->u
.remote_ref
.option
== RR_REF
)
1493 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1494 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1495 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1496 NULL
, atom
->u
.remote_ref
.push
,
1497 AHEAD_BEHIND_FULL
) < 0) {
1498 *s
= xstrdup(msgs
.gone
);
1499 } else if (!num_ours
&& !num_theirs
)
1502 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1503 else if (!num_theirs
)
1504 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1506 *s
= xstrfmt(msgs
.ahead_behind
,
1507 num_ours
, num_theirs
);
1508 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1509 const char *to_free
= *s
;
1510 *s
= xstrfmt("[%s]", *s
);
1511 free((void *)to_free
);
1513 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1514 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1515 NULL
, atom
->u
.remote_ref
.push
,
1516 AHEAD_BEHIND_FULL
) < 0) {
1520 if (!num_ours
&& !num_theirs
)
1524 else if (!num_theirs
)
1528 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1530 const char *remote
= atom
->u
.remote_ref
.push
?
1531 pushremote_for_branch(branch
, &explicit) :
1532 remote_for_branch(branch
, &explicit);
1533 *s
= xstrdup(explicit ? remote
: "");
1534 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1537 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
1538 *s
= xstrdup(merge
? merge
: "");
1540 BUG("unhandled RR_* enum");
1543 char *get_head_description(void)
1545 struct strbuf desc
= STRBUF_INIT
;
1546 struct wt_status_state state
;
1547 memset(&state
, 0, sizeof(state
));
1548 wt_status_get_state(the_repository
, &state
, 1);
1551 * The ( character must be hard-coded and not part of a localizable
1552 * string, since the description is used as a sort key and compared
1555 strbuf_addch(&desc
, '(');
1556 if (state
.rebase_in_progress
||
1557 state
.rebase_interactive_in_progress
) {
1559 strbuf_addf(&desc
, _("no branch, rebasing %s"),
1562 strbuf_addf(&desc
, _("no branch, rebasing detached HEAD %s"),
1563 state
.detached_from
);
1564 } else if (state
.bisect_in_progress
)
1565 strbuf_addf(&desc
, _("no branch, bisect started on %s"),
1567 else if (state
.detached_from
) {
1568 if (state
.detached_at
)
1569 strbuf_addstr(&desc
, HEAD_DETACHED_AT
);
1571 strbuf_addstr(&desc
, HEAD_DETACHED_FROM
);
1572 strbuf_addstr(&desc
, state
.detached_from
);
1575 strbuf_addstr(&desc
, _("no branch"));
1576 strbuf_addch(&desc
, ')');
1578 wt_status_state_free_buffers(&state
);
1579 return strbuf_detach(&desc
, NULL
);
1582 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1587 return show_ref(&atom
->u
.refname
, ref
->symref
);
1590 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1592 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1593 return get_head_description();
1594 return show_ref(&atom
->u
.refname
, ref
->refname
);
1597 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
1598 struct expand_data
*oi
, struct strbuf
*err
)
1600 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1602 if (oi
->info
.contentp
) {
1603 /* We need to know that to use parse_object_buffer properly */
1604 oi
->info
.sizep
= &oi
->size
;
1605 oi
->info
.typep
= &oi
->type
;
1607 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
1608 OBJECT_INFO_LOOKUP_REPLACE
))
1609 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1610 oid_to_hex(&oi
->oid
), ref
->refname
);
1611 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
1612 BUG("Object size is less than zero.");
1614 if (oi
->info
.contentp
) {
1615 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
1619 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1620 oid_to_hex(&oi
->oid
), ref
->refname
);
1622 grab_values(ref
->value
, deref
, *obj
, oi
->content
);
1625 grab_common_values(ref
->value
, deref
, oi
);
1631 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
1635 for (i
= 0; worktrees
[i
]; i
++) {
1636 if (worktrees
[i
]->head_ref
) {
1637 struct ref_to_worktree_entry
*entry
;
1638 entry
= xmalloc(sizeof(*entry
));
1639 entry
->wt
= worktrees
[i
];
1640 hashmap_entry_init(&entry
->ent
,
1641 strhash(worktrees
[i
]->head_ref
));
1643 hashmap_add(map
, &entry
->ent
);
1648 static void lazy_init_worktree_map(void)
1650 if (ref_to_worktree_map
.worktrees
)
1653 ref_to_worktree_map
.worktrees
= get_worktrees();
1654 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
1655 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
1658 static char *get_worktree_path(const struct used_atom
*atom
, const struct ref_array_item
*ref
)
1660 struct hashmap_entry entry
, *e
;
1661 struct ref_to_worktree_entry
*lookup_result
;
1663 lazy_init_worktree_map();
1665 hashmap_entry_init(&entry
, strhash(ref
->refname
));
1666 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
1671 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
1673 return xstrdup(lookup_result
->wt
->path
);
1677 * Parse the object referred by ref, and grab needed value.
1679 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
1683 struct object_info empty
= OBJECT_INFO_INIT
;
1685 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1687 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1688 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1691 ref
->symref
= xstrdup("");
1694 /* Fill in specials first */
1695 for (i
= 0; i
< used_atom_cnt
; i
++) {
1696 struct used_atom
*atom
= &used_atom
[i
];
1697 const char *name
= used_atom
[i
].name
;
1698 struct atom_value
*v
= &ref
->value
[i
];
1700 const char *refname
;
1701 struct branch
*branch
= NULL
;
1703 v
->handler
= append_atom
;
1711 if (starts_with(name
, "refname"))
1712 refname
= get_refname(atom
, ref
);
1713 else if (!strcmp(name
, "worktreepath")) {
1714 if (ref
->kind
== FILTER_REFS_BRANCHES
)
1715 v
->s
= get_worktree_path(atom
, ref
);
1720 else if (starts_with(name
, "symref"))
1721 refname
= get_symref(atom
, ref
);
1722 else if (starts_with(name
, "upstream")) {
1723 const char *branch_name
;
1724 /* only local branches may have an upstream */
1725 if (!skip_prefix(ref
->refname
, "refs/heads/",
1730 branch
= branch_get(branch_name
);
1732 refname
= branch_get_upstream(branch
, NULL
);
1734 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1738 } else if (atom
->u
.remote_ref
.push
) {
1739 const char *branch_name
;
1741 if (!skip_prefix(ref
->refname
, "refs/heads/",
1744 branch
= branch_get(branch_name
);
1746 if (atom
->u
.remote_ref
.push_remote
)
1749 refname
= branch_get_push(branch
, NULL
);
1753 /* We will definitely re-init v->s on the next line. */
1755 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1757 } else if (starts_with(name
, "color:")) {
1758 v
->s
= xstrdup(atom
->u
.color
);
1760 } else if (!strcmp(name
, "flag")) {
1761 char buf
[256], *cp
= buf
;
1762 if (ref
->flag
& REF_ISSYMREF
)
1763 cp
= copy_advance(cp
, ",symref");
1764 if (ref
->flag
& REF_ISPACKED
)
1765 cp
= copy_advance(cp
, ",packed");
1770 v
->s
= xstrdup(buf
+ 1);
1773 } else if (!deref
&& grab_oid(name
, "objectname", &ref
->objectname
, v
, atom
)) {
1775 } else if (!strcmp(name
, "HEAD")) {
1776 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1777 v
->s
= xstrdup("*");
1779 v
->s
= xstrdup(" ");
1781 } else if (starts_with(name
, "align")) {
1782 v
->handler
= align_atom_handler
;
1785 } else if (!strcmp(name
, "end")) {
1786 v
->handler
= end_atom_handler
;
1789 } else if (starts_with(name
, "if")) {
1791 if (skip_prefix(name
, "if:", &s
))
1795 v
->handler
= if_atom_handler
;
1797 } else if (!strcmp(name
, "then")) {
1798 v
->handler
= then_atom_handler
;
1801 } else if (!strcmp(name
, "else")) {
1802 v
->handler
= else_atom_handler
;
1809 v
->s
= xstrdup(refname
);
1811 v
->s
= xstrfmt("%s^{}", refname
);
1812 free((char *)refname
);
1815 for (i
= 0; i
< used_atom_cnt
; i
++) {
1816 struct atom_value
*v
= &ref
->value
[i
];
1817 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
1818 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1819 oid_to_hex(&ref
->objectname
), ref
->refname
);
1823 oi
.info
.contentp
= &oi
.content
;
1824 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
1825 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
1829 oi
.oid
= ref
->objectname
;
1830 if (get_object(ref
, 0, &obj
, &oi
, err
))
1834 * If there is no atom that wants to know about tagged
1835 * object, we are done.
1837 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1841 * If it is a tag object, see if we use a value that derefs
1842 * the object, and if we do grab the object it refers to.
1844 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
1847 * NEEDSWORK: This derefs tag only once, which
1848 * is good to deal with chains of trust, but
1849 * is not consistent with what deref_tag() does
1850 * which peels the onion to the core.
1852 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
1856 * Given a ref, return the value for the atom. This lazily gets value
1857 * out of the object by calling populate value.
1859 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
1860 struct atom_value
**v
, struct strbuf
*err
)
1863 if (populate_value(ref
, err
))
1865 fill_missing_values(ref
->value
);
1867 *v
= &ref
->value
[atom
];
1872 * Return 1 if the refname matches one of the patterns, otherwise 0.
1873 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1874 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1875 * matches "refs/heads/mas*", too).
1877 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1879 const char **patterns
= filter
->name_patterns
;
1882 if (filter
->ignore_case
)
1883 flags
|= WM_CASEFOLD
;
1886 * When no '--format' option is given we need to skip the prefix
1887 * for matching refs of tags and branches.
1889 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1890 skip_prefix(refname
, "refs/heads/", &refname
) ||
1891 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1892 skip_prefix(refname
, "refs/", &refname
));
1894 for (; *patterns
; patterns
++) {
1895 if (!wildmatch(*patterns
, refname
, flags
))
1902 * Return 1 if the refname matches one of the patterns, otherwise 0.
1903 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1904 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1905 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1907 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1909 const char **pattern
= filter
->name_patterns
;
1910 int namelen
= strlen(refname
);
1911 unsigned flags
= WM_PATHNAME
;
1913 if (filter
->ignore_case
)
1914 flags
|= WM_CASEFOLD
;
1916 for (; *pattern
; pattern
++) {
1917 const char *p
= *pattern
;
1918 int plen
= strlen(p
);
1920 if ((plen
<= namelen
) &&
1921 !strncmp(refname
, p
, plen
) &&
1922 (refname
[plen
] == '\0' ||
1923 refname
[plen
] == '/' ||
1926 if (!wildmatch(p
, refname
, flags
))
1932 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1933 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1935 if (!*filter
->name_patterns
)
1936 return 1; /* No pattern always matches */
1937 if (filter
->match_as_path
)
1938 return match_name_as_path(filter
, refname
);
1939 return match_pattern(filter
, refname
);
1942 static int qsort_strcmp(const void *va
, const void *vb
)
1944 const char *a
= *(const char **)va
;
1945 const char *b
= *(const char **)vb
;
1947 return strcmp(a
, b
);
1950 static void find_longest_prefixes_1(struct string_list
*out
,
1951 struct strbuf
*prefix
,
1952 const char **patterns
, size_t nr
)
1956 for (i
= 0; i
< nr
; i
++) {
1957 char c
= patterns
[i
][prefix
->len
];
1958 if (!c
|| is_glob_special(c
)) {
1959 string_list_append(out
, prefix
->buf
);
1969 * Set "end" to the index of the element _after_ the last one
1972 for (end
= i
+ 1; end
< nr
; end
++) {
1973 if (patterns
[i
][prefix
->len
] != patterns
[end
][prefix
->len
])
1977 strbuf_addch(prefix
, patterns
[i
][prefix
->len
]);
1978 find_longest_prefixes_1(out
, prefix
, patterns
+ i
, end
- i
);
1979 strbuf_setlen(prefix
, prefix
->len
- 1);
1985 static void find_longest_prefixes(struct string_list
*out
,
1986 const char **patterns
)
1988 struct strvec sorted
= STRVEC_INIT
;
1989 struct strbuf prefix
= STRBUF_INIT
;
1991 strvec_pushv(&sorted
, patterns
);
1992 QSORT(sorted
.v
, sorted
.nr
, qsort_strcmp
);
1994 find_longest_prefixes_1(out
, &prefix
, sorted
.v
, sorted
.nr
);
1996 strvec_clear(&sorted
);
1997 strbuf_release(&prefix
);
2001 * This is the same as for_each_fullref_in(), but it tries to iterate
2002 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2003 * pattern match, so the callback still has to match each ref individually.
2005 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
2010 struct string_list prefixes
= STRING_LIST_INIT_DUP
;
2011 struct string_list_item
*prefix
;
2014 if (!filter
->match_as_path
) {
2016 * in this case, the patterns are applied after
2017 * prefixes like "refs/heads/" etc. are stripped off,
2018 * so we have to look at everything:
2020 return for_each_fullref_in("", cb
, cb_data
, broken
);
2023 if (filter
->ignore_case
) {
2025 * we can't handle case-insensitive comparisons,
2026 * so just return everything and let the caller
2029 return for_each_fullref_in("", cb
, cb_data
, broken
);
2032 if (!filter
->name_patterns
[0]) {
2033 /* no patterns; we have to look at everything */
2034 return for_each_fullref_in("", cb
, cb_data
, broken
);
2037 find_longest_prefixes(&prefixes
, filter
->name_patterns
);
2039 for_each_string_list_item(prefix
, &prefixes
) {
2040 ret
= for_each_fullref_in(prefix
->string
, cb
, cb_data
, broken
);
2045 string_list_clear(&prefixes
, 0);
2050 * Given a ref (oid, refname), check if the ref belongs to the array
2051 * of oids. If the given ref is a tag, check if the given tag points
2052 * at one of the oids in the given oid array.
2054 * 1. Only a single level of indirection is obtained, we might want to
2055 * change this to account for multiple levels (e.g. annotated tags
2056 * pointing to annotated tags pointing to a commit.)
2057 * 2. As the refs are cached we might know what refname peels to without
2058 * the need to parse the object via parse_object(). peel_ref() might be a
2059 * more efficient alternative to obtain the pointee.
2061 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
2062 const struct object_id
*oid
,
2063 const char *refname
)
2065 const struct object_id
*tagged_oid
= NULL
;
2068 if (oid_array_lookup(points_at
, oid
) >= 0)
2070 obj
= parse_object(the_repository
, oid
);
2072 die(_("malformed object at '%s'"), refname
);
2073 if (obj
->type
== OBJ_TAG
)
2074 tagged_oid
= get_tagged_oid((struct tag
*)obj
);
2075 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
2081 * Allocate space for a new ref_array_item and copy the name and oid to it.
2083 * Callers can then fill in other struct members at their leisure.
2085 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2086 const struct object_id
*oid
)
2088 struct ref_array_item
*ref
;
2090 FLEX_ALLOC_STR(ref
, refname
, refname
);
2091 oidcpy(&ref
->objectname
, oid
);
2096 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2097 const char *refname
,
2098 const struct object_id
*oid
)
2100 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2102 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2103 array
->items
[array
->nr
++] = ref
;
2108 static int ref_kind_from_refname(const char *refname
)
2116 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2117 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2118 { "refs/tags/", FILTER_REFS_TAGS
}
2121 if (!strcmp(refname
, "HEAD"))
2122 return FILTER_REFS_DETACHED_HEAD
;
2124 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2125 if (starts_with(refname
, ref_kind
[i
].prefix
))
2126 return ref_kind
[i
].kind
;
2129 return FILTER_REFS_OTHERS
;
2132 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2134 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2135 filter
->kind
== FILTER_REFS_REMOTES
||
2136 filter
->kind
== FILTER_REFS_TAGS
)
2137 return filter
->kind
;
2138 return ref_kind_from_refname(refname
);
2141 struct ref_filter_cbdata
{
2142 struct ref_array
*array
;
2143 struct ref_filter
*filter
;
2144 struct contains_cache contains_cache
;
2145 struct contains_cache no_contains_cache
;
2149 * A call-back given to for_each_ref(). Filter refs and keep them for
2150 * later object processing.
2152 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2154 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2155 struct ref_filter
*filter
= ref_cbdata
->filter
;
2156 struct ref_array_item
*ref
;
2157 struct commit
*commit
= NULL
;
2160 if (flag
& REF_BAD_NAME
) {
2161 warning(_("ignoring ref with broken name %s"), refname
);
2165 if (flag
& REF_ISBROKEN
) {
2166 warning(_("ignoring broken ref %s"), refname
);
2170 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2171 kind
= filter_ref_kind(filter
, refname
);
2172 if (!(kind
& filter
->kind
))
2175 if (!filter_pattern_match(filter
, refname
))
2178 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2182 * A merge filter is applied on refs pointing to commits. Hence
2183 * obtain the commit using the 'oid' available and discard all
2184 * non-commits early. The actual filtering is done later.
2186 if (filter
->reachable_from
|| filter
->unreachable_from
||
2187 filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2188 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
2191 /* We perform the filtering for the '--contains' option... */
2192 if (filter
->with_commit
&&
2193 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2195 /* ...or for the `--no-contains' option */
2196 if (filter
->no_commit
&&
2197 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2202 * We do not open the object yet; sort may only need refname
2203 * to do its job and the resulting list may yet to be pruned
2204 * by maxcount logic.
2206 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2207 ref
->commit
= commit
;
2214 /* Free memory allocated for a ref_array_item */
2215 static void free_array_item(struct ref_array_item
*item
)
2217 free((char *)item
->symref
);
2220 for (i
= 0; i
< used_atom_cnt
; i
++)
2221 free((char *)item
->value
[i
].s
);
2227 /* Free all memory allocated for ref_array */
2228 void ref_array_clear(struct ref_array
*array
)
2232 for (i
= 0; i
< array
->nr
; i
++)
2233 free_array_item(array
->items
[i
]);
2234 FREE_AND_NULL(array
->items
);
2235 array
->nr
= array
->alloc
= 0;
2237 for (i
= 0; i
< used_atom_cnt
; i
++)
2238 free((char *)used_atom
[i
].name
);
2239 FREE_AND_NULL(used_atom
);
2242 if (ref_to_worktree_map
.worktrees
) {
2243 hashmap_clear_and_free(&(ref_to_worktree_map
.map
),
2244 struct ref_to_worktree_entry
, ent
);
2245 free_worktrees(ref_to_worktree_map
.worktrees
);
2246 ref_to_worktree_map
.worktrees
= NULL
;
2250 #define EXCLUDE_REACHED 0
2251 #define INCLUDE_REACHED 1
2252 static void reach_filter(struct ref_array
*array
,
2253 struct commit_list
*check_reachable
,
2254 int include_reached
)
2256 struct rev_info revs
;
2258 struct commit
**to_clear
;
2259 struct commit_list
*cr
;
2261 if (!check_reachable
)
2264 to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
2266 repo_init_revisions(the_repository
, &revs
, NULL
);
2268 for (i
= 0; i
< array
->nr
; i
++) {
2269 struct ref_array_item
*item
= array
->items
[i
];
2270 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
2271 to_clear
[i
] = item
->commit
;
2274 for (cr
= check_reachable
; cr
; cr
= cr
->next
) {
2275 struct commit
*merge_commit
= cr
->item
;
2276 merge_commit
->object
.flags
|= UNINTERESTING
;
2277 add_pending_object(&revs
, &merge_commit
->object
, "");
2281 if (prepare_revision_walk(&revs
))
2282 die(_("revision walk setup failed"));
2287 for (i
= 0; i
< old_nr
; i
++) {
2288 struct ref_array_item
*item
= array
->items
[i
];
2289 struct commit
*commit
= item
->commit
;
2291 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2293 if (is_merged
== include_reached
)
2294 array
->items
[array
->nr
++] = array
->items
[i
];
2296 free_array_item(item
);
2299 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2301 while (check_reachable
) {
2302 struct commit
*merge_commit
= pop_commit(&check_reachable
);
2303 clear_commit_marks(merge_commit
, ALL_REV_FLAGS
);
2310 * API for filtering a set of refs. Based on the type of refs the user
2311 * has requested, we iterate through those refs and apply filters
2312 * as per the given ref_filter structure and finally store the
2313 * filtered refs in the ref_array structure.
2315 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2317 struct ref_filter_cbdata ref_cbdata
;
2319 unsigned int broken
= 0;
2321 ref_cbdata
.array
= array
;
2322 ref_cbdata
.filter
= filter
;
2324 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
2326 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2328 init_contains_cache(&ref_cbdata
.contains_cache
);
2329 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2331 /* Simple per-ref filtering */
2333 die("filter_refs: invalid type");
2336 * For common cases where we need only branches or remotes or tags,
2337 * we only iterate through those refs. If a mix of refs is needed,
2338 * we iterate over all refs and filter out required refs with the help
2339 * of filter_ref_kind().
2341 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2342 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
2343 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2344 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2345 else if (filter
->kind
== FILTER_REFS_TAGS
)
2346 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2347 else if (filter
->kind
& FILTER_REFS_ALL
)
2348 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2349 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2350 head_ref(ref_filter_handler
, &ref_cbdata
);
2353 clear_contains_cache(&ref_cbdata
.contains_cache
);
2354 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2356 /* Filters that need revision walking */
2357 reach_filter(array
, filter
->reachable_from
, INCLUDE_REACHED
);
2358 reach_filter(array
, filter
->unreachable_from
, EXCLUDE_REACHED
);
2363 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2365 struct atom_value
*va
, *vb
;
2367 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2368 int (*cmp_fn
)(const char *, const char *);
2369 struct strbuf err
= STRBUF_INIT
;
2371 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2373 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2375 strbuf_release(&err
);
2376 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2378 cmp
= versioncmp(va
->s
, vb
->s
);
2379 else if (cmp_type
== FIELD_STR
)
2380 cmp
= cmp_fn(va
->s
, vb
->s
);
2382 if (va
->value
< vb
->value
)
2384 else if (va
->value
== vb
->value
)
2390 return (s
->reverse
) ? -cmp
: cmp
;
2393 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2395 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2396 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2397 struct ref_sorting
*s
;
2399 for (s
= ref_sorting
; s
; s
= s
->next
) {
2400 int cmp
= cmp_ref_sorting(s
, a
, b
);
2405 return s
&& s
->ignore_case
?
2406 strcasecmp(a
->refname
, b
->refname
) :
2407 strcmp(a
->refname
, b
->refname
);
2410 void ref_sorting_icase_all(struct ref_sorting
*sorting
, int flag
)
2412 for (; sorting
; sorting
= sorting
->next
)
2413 sorting
->ignore_case
= !!flag
;
2416 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2418 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2421 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2423 struct strbuf
*s
= &state
->stack
->output
;
2425 while (*cp
&& (!ep
|| cp
< ep
)) {
2430 int ch
= hex2chr(cp
+ 1);
2432 strbuf_addch(s
, ch
);
2438 strbuf_addch(s
, *cp
);
2443 int format_ref_array_item(struct ref_array_item
*info
,
2444 const struct ref_format
*format
,
2445 struct strbuf
*final_buf
,
2446 struct strbuf
*error_buf
)
2448 const char *cp
, *sp
, *ep
;
2449 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2451 state
.quote_style
= format
->quote_style
;
2452 push_stack_element(&state
.stack
);
2454 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2455 struct atom_value
*atomv
;
2458 ep
= strchr(sp
, ')');
2460 append_literal(cp
, sp
, &state
);
2461 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2462 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2463 atomv
->handler(atomv
, &state
, error_buf
)) {
2464 pop_stack_element(&state
.stack
);
2469 sp
= cp
+ strlen(cp
);
2470 append_literal(cp
, sp
, &state
);
2472 if (format
->need_color_reset_at_eol
) {
2473 struct atom_value resetv
;
2474 resetv
.s
= GIT_COLOR_RESET
;
2475 if (append_atom(&resetv
, &state
, error_buf
)) {
2476 pop_stack_element(&state
.stack
);
2480 if (state
.stack
->prev
) {
2481 pop_stack_element(&state
.stack
);
2482 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2484 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2485 pop_stack_element(&state
.stack
);
2489 void show_ref_array_item(struct ref_array_item
*info
,
2490 const struct ref_format
*format
)
2492 struct strbuf final_buf
= STRBUF_INIT
;
2493 struct strbuf error_buf
= STRBUF_INIT
;
2495 if (format_ref_array_item(info
, format
, &final_buf
, &error_buf
))
2496 die("%s", error_buf
.buf
);
2497 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2498 strbuf_release(&error_buf
);
2499 strbuf_release(&final_buf
);
2503 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2504 const struct ref_format
*format
)
2506 struct ref_array_item
*ref_item
;
2507 ref_item
= new_ref_array_item(name
, oid
);
2508 ref_item
->kind
= ref_kind_from_refname(name
);
2509 show_ref_array_item(ref_item
, format
);
2510 free_array_item(ref_item
);
2513 static int parse_sorting_atom(const char *atom
)
2516 * This parses an atom using a dummy ref_format, since we don't
2517 * actually care about the formatting details.
2519 struct ref_format dummy
= REF_FORMAT_INIT
;
2520 const char *end
= atom
+ strlen(atom
);
2521 struct strbuf err
= STRBUF_INIT
;
2522 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2525 strbuf_release(&err
);
2529 /* If no sorting option is given, use refname to sort as default */
2530 struct ref_sorting
*ref_default_sorting(void)
2532 static const char cstr_name
[] = "refname";
2534 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2536 sorting
->next
= NULL
;
2537 sorting
->atom
= parse_sorting_atom(cstr_name
);
2541 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2543 struct ref_sorting
*s
;
2545 s
= xcalloc(1, sizeof(*s
));
2546 s
->next
= *sorting_tail
;
2553 if (skip_prefix(arg
, "version:", &arg
) ||
2554 skip_prefix(arg
, "v:", &arg
))
2556 s
->atom
= parse_sorting_atom(arg
);
2559 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2562 * NEEDSWORK: We should probably clear the list in this case, but we've
2563 * already munged the global used_atoms list, which would need to be
2566 BUG_ON_OPT_NEG(unset
);
2568 parse_ref_sorting(opt
->value
, arg
);
2572 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2574 struct ref_filter
*rf
= opt
->value
;
2575 struct object_id oid
;
2576 struct commit
*merge_commit
;
2578 BUG_ON_OPT_NEG(unset
);
2580 if (get_oid(arg
, &oid
))
2581 die(_("malformed object name %s"), arg
);
2583 merge_commit
= lookup_commit_reference_gently(the_repository
, &oid
, 0);
2586 return error(_("option `%s' must point to a commit"), opt
->long_name
);
2588 if (starts_with(opt
->long_name
, "no"))
2589 commit_list_insert(merge_commit
, &rf
->unreachable_from
);
2591 commit_list_insert(merge_commit
, &rf
->reachable_from
);