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"
26 static struct ref_msg
{
30 const char *ahead_behind
;
32 /* Untranslated plumbing messages: */
39 void setup_ref_filter_porcelain_msg(void)
41 msgs
.gone
= _("gone");
42 msgs
.ahead
= _("ahead %d");
43 msgs
.behind
= _("behind %d");
44 msgs
.ahead_behind
= _("ahead %d, behind %d");
47 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
48 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
49 typedef enum { SOURCE_NONE
= 0, SOURCE_OBJ
, SOURCE_OTHER
} info_source
;
57 cmp_status cmp_status
;
59 unsigned int then_atom_seen
: 1,
61 condition_satisfied
: 1;
65 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
69 static struct expand_data
{
71 enum object_type type
;
74 struct object_id delta_base_oid
;
77 struct object_info info
;
80 struct ref_to_worktree_entry
{
81 struct hashmap_entry ent
; /* must be the first member! */
82 struct worktree
*wt
; /* key is wt->head_ref */
85 static int ref_to_worktree_map_cmpfnc(const void *unused_lookupdata
,
86 const void *existing_hashmap_entry_to_test
,
88 const void *keydata_aka_refname
)
90 const struct ref_to_worktree_entry
*e
= existing_hashmap_entry_to_test
;
91 const struct ref_to_worktree_entry
*k
= key
;
92 return strcmp(e
->wt
->head_ref
,
93 keydata_aka_refname
? keydata_aka_refname
: k
->wt
->head_ref
);
96 static struct ref_to_worktree_map
{
98 struct worktree
**worktrees
;
99 } ref_to_worktree_map
;
102 * An atom is a valid field atom listed below, possibly prefixed with
103 * a "*" to denote deref_tag().
105 * We parse given format string and sort specifiers, and make a list
106 * of properties that we need to extract out of objects. ref_array_item
107 * structure will hold an array of values extracted that can be
108 * indexed with the "atom number", which is an index into this
111 static struct used_atom
{
116 char color
[COLOR_MAXLEN
];
120 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
122 struct refname_atom refname
;
123 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
126 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
127 struct process_trailer_options trailer_opts
;
131 cmp_status cmp_status
;
135 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
138 struct refname_atom refname
;
142 static int used_atom_cnt
, need_tagged
, need_symref
;
145 * Expand string, append it to strbuf *sb, then return error code ret.
146 * Allow to save few lines of code.
148 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
152 strbuf_vaddf(sb
, fmt
, ap
);
157 static int color_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
158 const char *color_value
, struct strbuf
*err
)
161 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
162 if (color_parse(color_value
, atom
->u
.color
) < 0)
163 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
166 * We check this after we've parsed the color, which lets us complain
167 * about syntactically bogus color names even if they won't be used.
169 if (!want_color(format
->use_color
))
170 color_parse("", atom
->u
.color
);
174 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
175 const char *name
, struct strbuf
*err
)
178 atom
->option
= R_NORMAL
;
179 else if (!strcmp(arg
, "short"))
180 atom
->option
= R_SHORT
;
181 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
182 skip_prefix(arg
, "strip=", &arg
)) {
183 atom
->option
= R_LSTRIP
;
184 if (strtol_i(arg
, 10, &atom
->lstrip
))
185 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
186 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
187 atom
->option
= R_RSTRIP
;
188 if (strtol_i(arg
, 10, &atom
->rstrip
))
189 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
191 return strbuf_addf_ret(err
, -1, _("unrecognized %%(%s) argument: %s"), name
, arg
);
195 static int remote_ref_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
196 const char *arg
, struct strbuf
*err
)
198 struct string_list params
= STRING_LIST_INIT_DUP
;
201 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
202 atom
->u
.remote_ref
.push
= 1;
205 atom
->u
.remote_ref
.option
= RR_REF
;
206 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
207 arg
, atom
->name
, err
);
210 atom
->u
.remote_ref
.nobracket
= 0;
211 string_list_split(¶ms
, arg
, ',', -1);
213 for (i
= 0; i
< params
.nr
; i
++) {
214 const char *s
= params
.items
[i
].string
;
216 if (!strcmp(s
, "track"))
217 atom
->u
.remote_ref
.option
= RR_TRACK
;
218 else if (!strcmp(s
, "trackshort"))
219 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
220 else if (!strcmp(s
, "nobracket"))
221 atom
->u
.remote_ref
.nobracket
= 1;
222 else if (!strcmp(s
, "remotename")) {
223 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
224 atom
->u
.remote_ref
.push_remote
= 1;
225 } else if (!strcmp(s
, "remoteref")) {
226 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
227 atom
->u
.remote_ref
.push_remote
= 1;
229 atom
->u
.remote_ref
.option
= RR_REF
;
230 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
231 arg
, atom
->name
, err
)) {
232 string_list_clear(¶ms
, 0);
238 string_list_clear(¶ms
, 0);
242 static int objecttype_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
243 const char *arg
, struct strbuf
*err
)
246 return strbuf_addf_ret(err
, -1, _("%%(objecttype) does not take arguments"));
247 if (*atom
->name
== '*')
248 oi_deref
.info
.typep
= &oi_deref
.type
;
250 oi
.info
.typep
= &oi
.type
;
254 static int objectsize_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
255 const char *arg
, struct strbuf
*err
)
258 if (*atom
->name
== '*')
259 oi_deref
.info
.sizep
= &oi_deref
.size
;
261 oi
.info
.sizep
= &oi
.size
;
262 } else if (!strcmp(arg
, "disk")) {
263 if (*atom
->name
== '*')
264 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
266 oi
.info
.disk_sizep
= &oi
.disk_size
;
268 return strbuf_addf_ret(err
, -1, _("unrecognized %%(objectsize) argument: %s"), arg
);
272 static int deltabase_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
273 const char *arg
, struct strbuf
*err
)
276 return strbuf_addf_ret(err
, -1, _("%%(deltabase) does not take arguments"));
277 if (*atom
->name
== '*')
278 oi_deref
.info
.delta_base_sha1
= oi_deref
.delta_base_oid
.hash
;
280 oi
.info
.delta_base_sha1
= oi
.delta_base_oid
.hash
;
284 static int body_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
285 const char *arg
, struct strbuf
*err
)
288 return strbuf_addf_ret(err
, -1, _("%%(body) does not take arguments"));
289 atom
->u
.contents
.option
= C_BODY_DEP
;
293 static int subject_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
294 const char *arg
, struct strbuf
*err
)
297 return strbuf_addf_ret(err
, -1, _("%%(subject) does not take arguments"));
298 atom
->u
.contents
.option
= C_SUB
;
302 static int trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
303 const char *arg
, struct strbuf
*err
)
305 struct string_list params
= STRING_LIST_INIT_DUP
;
308 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
311 string_list_split(¶ms
, arg
, ',', -1);
312 for (i
= 0; i
< params
.nr
; i
++) {
313 const char *s
= params
.items
[i
].string
;
314 if (!strcmp(s
, "unfold"))
315 atom
->u
.contents
.trailer_opts
.unfold
= 1;
316 else if (!strcmp(s
, "only"))
317 atom
->u
.contents
.trailer_opts
.only_trailers
= 1;
319 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), s
);
320 string_list_clear(¶ms
, 0);
325 atom
->u
.contents
.option
= C_TRAILERS
;
326 string_list_clear(¶ms
, 0);
330 static int contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
331 const char *arg
, struct strbuf
*err
)
334 atom
->u
.contents
.option
= C_BARE
;
335 else if (!strcmp(arg
, "body"))
336 atom
->u
.contents
.option
= C_BODY
;
337 else if (!strcmp(arg
, "signature"))
338 atom
->u
.contents
.option
= C_SIG
;
339 else if (!strcmp(arg
, "subject"))
340 atom
->u
.contents
.option
= C_SUB
;
341 else if (skip_prefix(arg
, "trailers", &arg
)) {
342 skip_prefix(arg
, ":", &arg
);
343 if (trailers_atom_parser(format
, atom
, *arg
? arg
: NULL
, err
))
345 } else if (skip_prefix(arg
, "lines=", &arg
)) {
346 atom
->u
.contents
.option
= C_LINES
;
347 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
348 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
350 return strbuf_addf_ret(err
, -1, _("unrecognized %%(contents) argument: %s"), arg
);
354 static int objectname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
355 const char *arg
, struct strbuf
*err
)
358 atom
->u
.objectname
.option
= O_FULL
;
359 else if (!strcmp(arg
, "short"))
360 atom
->u
.objectname
.option
= O_SHORT
;
361 else if (skip_prefix(arg
, "short=", &arg
)) {
362 atom
->u
.objectname
.option
= O_LENGTH
;
363 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
364 atom
->u
.objectname
.length
== 0)
365 return strbuf_addf_ret(err
, -1, _("positive value expected objectname:short=%s"), arg
);
366 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
367 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
369 return strbuf_addf_ret(err
, -1, _("unrecognized %%(objectname) argument: %s"), arg
);
373 static int refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
374 const char *arg
, struct strbuf
*err
)
376 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
379 static align_type
parse_align_position(const char *s
)
381 if (!strcmp(s
, "right"))
383 else if (!strcmp(s
, "middle"))
385 else if (!strcmp(s
, "left"))
390 static int align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
391 const char *arg
, struct strbuf
*err
)
393 struct align
*align
= &atom
->u
.align
;
394 struct string_list params
= STRING_LIST_INIT_DUP
;
396 unsigned int width
= ~0U;
399 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
401 align
->position
= ALIGN_LEFT
;
403 string_list_split(¶ms
, arg
, ',', -1);
404 for (i
= 0; i
< params
.nr
; i
++) {
405 const char *s
= params
.items
[i
].string
;
408 if (skip_prefix(s
, "position=", &s
)) {
409 position
= parse_align_position(s
);
411 strbuf_addf(err
, _("unrecognized position:%s"), s
);
412 string_list_clear(¶ms
, 0);
415 align
->position
= position
;
416 } else if (skip_prefix(s
, "width=", &s
)) {
417 if (strtoul_ui(s
, 10, &width
)) {
418 strbuf_addf(err
, _("unrecognized width:%s"), s
);
419 string_list_clear(¶ms
, 0);
422 } else if (!strtoul_ui(s
, 10, &width
))
424 else if ((position
= parse_align_position(s
)) >= 0)
425 align
->position
= position
;
427 strbuf_addf(err
, _("unrecognized %%(align) argument: %s"), s
);
428 string_list_clear(¶ms
, 0);
434 string_list_clear(¶ms
, 0);
435 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
437 align
->width
= width
;
438 string_list_clear(¶ms
, 0);
442 static int if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
443 const char *arg
, struct strbuf
*err
)
446 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
448 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
449 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
450 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
451 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
453 return strbuf_addf_ret(err
, -1, _("unrecognized %%(if) argument: %s"), arg
);
457 static int head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
458 const char *arg
, struct strbuf
*unused_err
)
460 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
468 int (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
,
469 const char *arg
, struct strbuf
*err
);
471 { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
472 { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
473 { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
474 { "objectname", SOURCE_OTHER
, FIELD_STR
, objectname_atom_parser
},
475 { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
476 { "tree", SOURCE_OBJ
},
477 { "parent", SOURCE_OBJ
},
478 { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
479 { "object", SOURCE_OBJ
},
480 { "type", SOURCE_OBJ
},
481 { "tag", SOURCE_OBJ
},
482 { "author", SOURCE_OBJ
},
483 { "authorname", SOURCE_OBJ
},
484 { "authoremail", SOURCE_OBJ
},
485 { "authordate", SOURCE_OBJ
, FIELD_TIME
},
486 { "committer", SOURCE_OBJ
},
487 { "committername", SOURCE_OBJ
},
488 { "committeremail", SOURCE_OBJ
},
489 { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
490 { "tagger", SOURCE_OBJ
},
491 { "taggername", SOURCE_OBJ
},
492 { "taggeremail", SOURCE_OBJ
},
493 { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
494 { "creator", SOURCE_OBJ
},
495 { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
496 { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
497 { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
498 { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
499 { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
500 { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
501 { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
502 { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
503 { "flag", SOURCE_NONE
},
504 { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
505 { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
506 { "worktreepath", SOURCE_NONE
},
507 { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
508 { "end", SOURCE_NONE
},
509 { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
510 { "then", SOURCE_NONE
},
511 { "else", SOURCE_NONE
},
513 * Please update $__git_ref_fieldlist in git-completion.bash
514 * when you add new atoms
518 #define REF_FORMATTING_STATE_INIT { 0, NULL }
520 struct ref_formatting_stack
{
521 struct ref_formatting_stack
*prev
;
522 struct strbuf output
;
523 void (*at_end
)(struct ref_formatting_stack
**stack
);
527 struct ref_formatting_state
{
529 struct ref_formatting_stack
*stack
;
534 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
536 uintmax_t value
; /* used for sorting when not FIELD_STR */
537 struct used_atom
*atom
;
541 * Used to parse format string and sort specifiers
543 static int parse_ref_filter_atom(const struct ref_format
*format
,
544 const char *atom
, const char *ep
,
552 if (*sp
== '*' && sp
< ep
)
555 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
556 (int)(ep
-atom
), atom
);
558 /* Do we have the atom already used elsewhere? */
559 for (i
= 0; i
< used_atom_cnt
; i
++) {
560 int len
= strlen(used_atom
[i
].name
);
561 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
566 * If the atom name has a colon, strip it and everything after
567 * it off - it specifies the format for this entry, and
568 * shouldn't be used for checking against the valid_atom
571 arg
= memchr(sp
, ':', ep
- sp
);
572 atom_len
= (arg
? arg
: ep
) - sp
;
574 /* Is the atom a valid one? */
575 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
576 int len
= strlen(valid_atom
[i
].name
);
577 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
581 if (ARRAY_SIZE(valid_atom
) <= i
)
582 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
583 (int)(ep
-atom
), atom
);
584 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
585 return strbuf_addf_ret(err
, -1,
586 _("not a git repository, but the field '%.*s' requires access to object data"),
587 (int)(ep
-atom
), atom
);
589 /* Add it in, including the deref prefix */
592 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
593 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
594 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
595 used_atom
[at
].source
= valid_atom
[i
].source
;
596 if (used_atom
[at
].source
== SOURCE_OBJ
) {
598 oi_deref
.info
.contentp
= &oi_deref
.content
;
600 oi
.info
.contentp
= &oi
.content
;
603 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
606 * Treat empty sub-arguments list as NULL (i.e.,
607 * "%(atom:)" is equivalent to "%(atom)").
612 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
613 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
617 if (!strcmp(valid_atom
[i
].name
, "symref"))
622 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
624 switch (quote_style
) {
626 strbuf_addstr(s
, str
);
629 sq_quote_buf(s
, str
);
632 perl_quote_buf(s
, str
);
635 python_quote_buf(s
, str
);
638 tcl_quote_buf(s
, str
);
643 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
644 struct strbuf
*unused_err
)
647 * Quote formatting is only done when the stack has a single
648 * element. Otherwise quote formatting is done on the
649 * element's entire output strbuf when the %(end) atom is
652 if (!state
->stack
->prev
)
653 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
655 strbuf_addstr(&state
->stack
->output
, v
->s
);
659 static void push_stack_element(struct ref_formatting_stack
**stack
)
661 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
663 strbuf_init(&s
->output
, 0);
668 static void pop_stack_element(struct ref_formatting_stack
**stack
)
670 struct ref_formatting_stack
*current
= *stack
;
671 struct ref_formatting_stack
*prev
= current
->prev
;
674 strbuf_addbuf(&prev
->output
, ¤t
->output
);
675 strbuf_release(¤t
->output
);
680 static void end_align_handler(struct ref_formatting_stack
**stack
)
682 struct ref_formatting_stack
*cur
= *stack
;
683 struct align
*align
= (struct align
*)cur
->at_end_data
;
684 struct strbuf s
= STRBUF_INIT
;
686 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
687 strbuf_swap(&cur
->output
, &s
);
691 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
692 struct strbuf
*unused_err
)
694 struct ref_formatting_stack
*new_stack
;
696 push_stack_element(&state
->stack
);
697 new_stack
= state
->stack
;
698 new_stack
->at_end
= end_align_handler
;
699 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
703 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
705 struct ref_formatting_stack
*cur
= *stack
;
706 struct ref_formatting_stack
*prev
= cur
->prev
;
707 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
709 if (!if_then_else
->then_atom_seen
)
710 die(_("format: %%(if) atom used without a %%(then) atom"));
712 if (if_then_else
->else_atom_seen
) {
714 * There is an %(else) atom: we need to drop one state from the
715 * stack, either the %(else) branch if the condition is satisfied, or
716 * the %(then) branch if it isn't.
718 if (if_then_else
->condition_satisfied
) {
719 strbuf_reset(&cur
->output
);
720 pop_stack_element(&cur
);
722 strbuf_swap(&cur
->output
, &prev
->output
);
723 strbuf_reset(&cur
->output
);
724 pop_stack_element(&cur
);
726 } else if (!if_then_else
->condition_satisfied
) {
728 * No %(else) atom: just drop the %(then) branch if the
729 * condition is not satisfied.
731 strbuf_reset(&cur
->output
);
738 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
739 struct strbuf
*unused_err
)
741 struct ref_formatting_stack
*new_stack
;
742 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
744 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
745 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
747 push_stack_element(&state
->stack
);
748 new_stack
= state
->stack
;
749 new_stack
->at_end
= if_then_else_handler
;
750 new_stack
->at_end_data
= if_then_else
;
754 static int is_empty(const char *s
)
764 static int then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
767 struct ref_formatting_stack
*cur
= state
->stack
;
768 struct if_then_else
*if_then_else
= NULL
;
770 if (cur
->at_end
== if_then_else_handler
)
771 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
773 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used without an %%(if) atom"));
774 if (if_then_else
->then_atom_seen
)
775 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
776 if (if_then_else
->else_atom_seen
)
777 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
778 if_then_else
->then_atom_seen
= 1;
780 * If the 'equals' or 'notequals' attribute is used then
781 * perform the required comparison. If not, only non-empty
782 * strings satisfy the 'if' condition.
784 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
785 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
786 if_then_else
->condition_satisfied
= 1;
787 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
788 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
789 if_then_else
->condition_satisfied
= 1;
790 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
791 if_then_else
->condition_satisfied
= 1;
792 strbuf_reset(&cur
->output
);
796 static int else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
799 struct ref_formatting_stack
*prev
= state
->stack
;
800 struct if_then_else
*if_then_else
= NULL
;
802 if (prev
->at_end
== if_then_else_handler
)
803 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
805 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without an %%(if) atom"));
806 if (!if_then_else
->then_atom_seen
)
807 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without a %%(then) atom"));
808 if (if_then_else
->else_atom_seen
)
809 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
810 if_then_else
->else_atom_seen
= 1;
811 push_stack_element(&state
->stack
);
812 state
->stack
->at_end_data
= prev
->at_end_data
;
813 state
->stack
->at_end
= prev
->at_end
;
817 static int end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
820 struct ref_formatting_stack
*current
= state
->stack
;
821 struct strbuf s
= STRBUF_INIT
;
823 if (!current
->at_end
)
824 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
825 current
->at_end(&state
->stack
);
827 /* Stack may have been popped within at_end(), hence reset the current pointer */
828 current
= state
->stack
;
831 * Perform quote formatting when the stack element is that of
832 * a supporting atom. If nested then perform quote formatting
833 * only on the topmost supporting atom.
835 if (!current
->prev
->prev
) {
836 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
837 strbuf_swap(¤t
->output
, &s
);
840 pop_stack_element(&state
->stack
);
845 * In a format string, find the next occurrence of %(atom).
847 static const char *find_next(const char *cp
)
852 * %( is the start of an atom;
853 * %% is a quoted per-cent.
857 else if (cp
[1] == '%')
858 cp
++; /* skip over two % */
859 /* otherwise this is a singleton, literal % */
867 * Make sure the format string is well formed, and parse out
870 int verify_ref_format(struct ref_format
*format
)
874 format
->need_color_reset_at_eol
= 0;
875 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
876 struct strbuf err
= STRBUF_INIT
;
877 const char *color
, *ep
= strchr(sp
, ')');
881 return error(_("malformed format string %s"), sp
);
882 /* sp points at "%(" and ep points at the closing ")" */
883 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
888 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
889 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
890 strbuf_release(&err
);
892 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
893 format
->need_color_reset_at_eol
= 0;
897 static int grab_objectname(const char *name
, const struct object_id
*oid
,
898 struct atom_value
*v
, struct used_atom
*atom
)
900 if (starts_with(name
, "objectname")) {
901 if (atom
->u
.objectname
.option
== O_SHORT
) {
902 v
->s
= xstrdup(find_unique_abbrev(oid
, DEFAULT_ABBREV
));
904 } else if (atom
->u
.objectname
.option
== O_FULL
) {
905 v
->s
= xstrdup(oid_to_hex(oid
));
907 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
908 v
->s
= xstrdup(find_unique_abbrev(oid
, atom
->u
.objectname
.length
));
911 BUG("unknown %%(objectname) option");
916 /* See grab_values */
917 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
921 for (i
= 0; i
< used_atom_cnt
; i
++) {
922 const char *name
= used_atom
[i
].name
;
923 struct atom_value
*v
= &val
[i
];
924 if (!!deref
!= (*name
== '*'))
928 if (!strcmp(name
, "objecttype"))
929 v
->s
= xstrdup(type_name(oi
->type
));
930 else if (!strcmp(name
, "objectsize:disk")) {
931 v
->value
= oi
->disk_size
;
932 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
933 } else if (!strcmp(name
, "objectsize")) {
935 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
936 } else if (!strcmp(name
, "deltabase"))
937 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
939 grab_objectname(name
, &oi
->oid
, v
, &used_atom
[i
]);
943 /* See grab_values */
944 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
947 struct tag
*tag
= (struct tag
*) obj
;
949 for (i
= 0; i
< used_atom_cnt
; i
++) {
950 const char *name
= used_atom
[i
].name
;
951 struct atom_value
*v
= &val
[i
];
952 if (!!deref
!= (*name
== '*'))
956 if (!strcmp(name
, "tag"))
957 v
->s
= xstrdup(tag
->tag
);
958 else if (!strcmp(name
, "type") && tag
->tagged
)
959 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
960 else if (!strcmp(name
, "object") && tag
->tagged
)
961 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
965 /* See grab_values */
966 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
969 struct commit
*commit
= (struct commit
*) obj
;
971 for (i
= 0; i
< used_atom_cnt
; i
++) {
972 const char *name
= used_atom
[i
].name
;
973 struct atom_value
*v
= &val
[i
];
974 if (!!deref
!= (*name
== '*'))
978 if (!strcmp(name
, "tree")) {
979 v
->s
= xstrdup(oid_to_hex(get_commit_tree_oid(commit
)));
981 else if (!strcmp(name
, "numparent")) {
982 v
->value
= commit_list_count(commit
->parents
);
983 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
985 else if (!strcmp(name
, "parent")) {
986 struct commit_list
*parents
;
987 struct strbuf s
= STRBUF_INIT
;
988 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
989 struct commit
*parent
= parents
->item
;
990 if (parents
!= commit
->parents
)
991 strbuf_addch(&s
, ' ');
992 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
994 v
->s
= strbuf_detach(&s
, NULL
);
999 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1003 if (!strncmp(buf
, who
, wholen
) &&
1005 return buf
+ wholen
+ 1;
1006 eol
= strchr(buf
, '\n');
1011 return ""; /* end of header */
1017 static const char *copy_line(const char *buf
)
1019 const char *eol
= strchrnul(buf
, '\n');
1020 return xmemdupz(buf
, eol
- buf
);
1023 static const char *copy_name(const char *buf
)
1026 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1027 if (!strncmp(cp
, " <", 2))
1028 return xmemdupz(buf
, cp
- buf
);
1033 static const char *copy_email(const char *buf
)
1035 const char *email
= strchr(buf
, '<');
1036 const char *eoemail
;
1039 eoemail
= strchr(email
, '>');
1042 return xmemdupz(email
, eoemail
+ 1 - email
);
1045 static char *copy_subject(const char *buf
, unsigned long len
)
1047 char *r
= xmemdupz(buf
, len
);
1050 for (i
= 0; i
< len
; i
++)
1057 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1059 const char *eoemail
= strstr(buf
, "> ");
1061 timestamp_t timestamp
;
1063 struct date_mode date_mode
= { DATE_NORMAL
};
1064 const char *formatp
;
1067 * We got here because atomname ends in "date" or "date<something>";
1068 * it's not possible that <something> is not ":<format>" because
1069 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1070 * ":" means no format is specified, and use the default.
1072 formatp
= strchr(atomname
, ':');
1073 if (formatp
!= NULL
) {
1075 parse_date_format(formatp
, &date_mode
);
1080 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1081 if (timestamp
== TIME_MAX
)
1083 tz
= strtol(zone
, NULL
, 10);
1084 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1086 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1087 v
->value
= timestamp
;
1094 /* See grab_values */
1095 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1098 int wholen
= strlen(who
);
1099 const char *wholine
= NULL
;
1101 for (i
= 0; i
< used_atom_cnt
; i
++) {
1102 const char *name
= used_atom
[i
].name
;
1103 struct atom_value
*v
= &val
[i
];
1104 if (!!deref
!= (*name
== '*'))
1108 if (strncmp(who
, name
, wholen
))
1110 if (name
[wholen
] != 0 &&
1111 strcmp(name
+ wholen
, "name") &&
1112 strcmp(name
+ wholen
, "email") &&
1113 !starts_with(name
+ wholen
, "date"))
1116 wholine
= find_wholine(who
, wholen
, buf
);
1118 return; /* no point looking for it */
1119 if (name
[wholen
] == 0)
1120 v
->s
= copy_line(wholine
);
1121 else if (!strcmp(name
+ wholen
, "name"))
1122 v
->s
= copy_name(wholine
);
1123 else if (!strcmp(name
+ wholen
, "email"))
1124 v
->s
= copy_email(wholine
);
1125 else if (starts_with(name
+ wholen
, "date"))
1126 grab_date(wholine
, v
, name
);
1130 * For a tag or a commit object, if "creator" or "creatordate" is
1131 * requested, do something special.
1133 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1134 return; /* "author" for commit object is not wanted */
1136 wholine
= find_wholine(who
, wholen
, buf
);
1139 for (i
= 0; i
< used_atom_cnt
; i
++) {
1140 const char *name
= used_atom
[i
].name
;
1141 struct atom_value
*v
= &val
[i
];
1142 if (!!deref
!= (*name
== '*'))
1147 if (starts_with(name
, "creatordate"))
1148 grab_date(wholine
, v
, name
);
1149 else if (!strcmp(name
, "creator"))
1150 v
->s
= copy_line(wholine
);
1154 static void find_subpos(const char *buf
,
1155 const char **sub
, unsigned long *sublen
,
1156 const char **body
, unsigned long *bodylen
,
1157 unsigned long *nonsiglen
,
1158 const char **sig
, unsigned long *siglen
)
1161 /* skip past header until we hit empty line */
1162 while (*buf
&& *buf
!= '\n') {
1163 eol
= strchrnul(buf
, '\n');
1168 /* skip any empty lines */
1169 while (*buf
== '\n')
1172 /* parse signature first; we might not even have a subject line */
1173 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
1174 *siglen
= strlen(*sig
);
1176 /* subject is first non-empty line */
1178 /* subject goes to first empty line */
1179 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
1180 eol
= strchrnul(buf
, '\n');
1185 *sublen
= buf
- *sub
;
1186 /* drop trailing newline, if present */
1187 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1190 /* skip any empty lines */
1191 while (*buf
== '\n')
1194 *bodylen
= strlen(buf
);
1195 *nonsiglen
= *sig
- buf
;
1199 * If 'lines' is greater than 0, append that many lines from the given
1200 * 'buf' of length 'size' to the given strbuf.
1202 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1205 const char *sp
, *eol
;
1210 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1212 strbuf_addstr(out
, "\n ");
1213 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1214 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1215 strbuf_add(out
, sp
, len
);
1222 /* See grab_values */
1223 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, void *buf
)
1226 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1227 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1229 for (i
= 0; i
< used_atom_cnt
; i
++) {
1230 struct used_atom
*atom
= &used_atom
[i
];
1231 const char *name
= atom
->name
;
1232 struct atom_value
*v
= &val
[i
];
1233 if (!!deref
!= (*name
== '*'))
1237 if (strcmp(name
, "subject") &&
1238 strcmp(name
, "body") &&
1239 !starts_with(name
, "trailers") &&
1240 !starts_with(name
, "contents"))
1245 &bodypos
, &bodylen
, &nonsiglen
,
1248 if (atom
->u
.contents
.option
== C_SUB
)
1249 v
->s
= copy_subject(subpos
, sublen
);
1250 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1251 v
->s
= xmemdupz(bodypos
, bodylen
);
1252 else if (atom
->u
.contents
.option
== C_BODY
)
1253 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1254 else if (atom
->u
.contents
.option
== C_SIG
)
1255 v
->s
= xmemdupz(sigpos
, siglen
);
1256 else if (atom
->u
.contents
.option
== C_LINES
) {
1257 struct strbuf s
= STRBUF_INIT
;
1258 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1260 /* Size is the length of the message after removing the signature */
1261 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1262 v
->s
= strbuf_detach(&s
, NULL
);
1263 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1264 struct strbuf s
= STRBUF_INIT
;
1266 /* Format the trailer info according to the trailer_opts given */
1267 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1269 v
->s
= strbuf_detach(&s
, NULL
);
1270 } else if (atom
->u
.contents
.option
== C_BARE
)
1271 v
->s
= xstrdup(subpos
);
1276 * We want to have empty print-string for field requests
1277 * that do not apply (e.g. "authordate" for a tag object)
1279 static void fill_missing_values(struct atom_value
*val
)
1282 for (i
= 0; i
< used_atom_cnt
; i
++) {
1283 struct atom_value
*v
= &val
[i
];
1290 * val is a list of atom_value to hold returned values. Extract
1291 * the values for atoms in used_atom array out of (obj, buf, sz).
1292 * when deref is false, (obj, buf, sz) is the object that is
1293 * pointed at by the ref itself; otherwise it is the object the
1294 * ref (which is a tag) refers to.
1296 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
)
1298 switch (obj
->type
) {
1300 grab_tag_values(val
, deref
, obj
);
1301 grab_sub_body_contents(val
, deref
, buf
);
1302 grab_person("tagger", val
, deref
, buf
);
1305 grab_commit_values(val
, deref
, obj
);
1306 grab_sub_body_contents(val
, deref
, buf
);
1307 grab_person("author", val
, deref
, buf
);
1308 grab_person("committer", val
, deref
, buf
);
1311 /* grab_tree_values(val, deref, obj, buf, sz); */
1314 /* grab_blob_values(val, deref, obj, buf, sz); */
1317 die("Eh? Object of type %d?", obj
->type
);
1321 static inline char *copy_advance(char *dst
, const char *src
)
1328 static const char *lstrip_ref_components(const char *refname
, int len
)
1330 long remaining
= len
;
1331 const char *start
= xstrdup(refname
);
1332 const char *to_free
= start
;
1336 const char *p
= refname
;
1338 /* Find total no of '/' separated path-components */
1339 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1342 * The number of components we need to strip is now
1343 * the total minus the components to be left (Plus one
1344 * because we count the number of '/', but the number
1345 * of components is one more than the no of '/').
1347 remaining
= i
+ len
+ 1;
1350 while (remaining
> 0) {
1353 free((char *)to_free
);
1361 start
= xstrdup(start
);
1362 free((char *)to_free
);
1366 static const char *rstrip_ref_components(const char *refname
, int len
)
1368 long remaining
= len
;
1369 const char *start
= xstrdup(refname
);
1370 const char *to_free
= start
;
1374 const char *p
= refname
;
1376 /* Find total no of '/' separated path-components */
1377 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1380 * The number of components we need to strip is now
1381 * the total minus the components to be left (Plus one
1382 * because we count the number of '/', but the number
1383 * of components is one more than the no of '/').
1385 remaining
= i
+ len
+ 1;
1388 while (remaining
-- > 0) {
1389 char *p
= strrchr(start
, '/');
1391 free((char *)to_free
);
1399 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1401 if (atom
->option
== R_SHORT
)
1402 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1403 else if (atom
->option
== R_LSTRIP
)
1404 return lstrip_ref_components(refname
, atom
->lstrip
);
1405 else if (atom
->option
== R_RSTRIP
)
1406 return rstrip_ref_components(refname
, atom
->rstrip
);
1408 return xstrdup(refname
);
1411 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1412 struct branch
*branch
, const char **s
)
1414 int num_ours
, num_theirs
;
1415 if (atom
->u
.remote_ref
.option
== RR_REF
)
1416 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1417 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1418 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1419 NULL
, atom
->u
.remote_ref
.push
,
1420 AHEAD_BEHIND_FULL
) < 0) {
1421 *s
= xstrdup(msgs
.gone
);
1422 } else if (!num_ours
&& !num_theirs
)
1425 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1426 else if (!num_theirs
)
1427 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1429 *s
= xstrfmt(msgs
.ahead_behind
,
1430 num_ours
, num_theirs
);
1431 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1432 const char *to_free
= *s
;
1433 *s
= xstrfmt("[%s]", *s
);
1434 free((void *)to_free
);
1436 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1437 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1438 NULL
, atom
->u
.remote_ref
.push
,
1439 AHEAD_BEHIND_FULL
) < 0) {
1443 if (!num_ours
&& !num_theirs
)
1447 else if (!num_theirs
)
1451 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1453 const char *remote
= atom
->u
.remote_ref
.push
?
1454 pushremote_for_branch(branch
, &explicit) :
1455 remote_for_branch(branch
, &explicit);
1456 *s
= xstrdup(explicit ? remote
: "");
1457 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1461 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
,
1463 *s
= xstrdup(explicit ? merge
: "");
1465 BUG("unhandled RR_* enum");
1468 char *get_head_description(void)
1470 struct strbuf desc
= STRBUF_INIT
;
1471 struct wt_status_state state
;
1472 memset(&state
, 0, sizeof(state
));
1473 wt_status_get_state(the_repository
, &state
, 1);
1476 * The ( character must be hard-coded and not part of a localizable
1477 * string, since the description is used as a sort key and compared
1480 strbuf_addch(&desc
, '(');
1481 if (state
.rebase_in_progress
||
1482 state
.rebase_interactive_in_progress
) {
1484 strbuf_addf(&desc
, _("no branch, rebasing %s"),
1487 strbuf_addf(&desc
, _("no branch, rebasing detached HEAD %s"),
1488 state
.detached_from
);
1489 } else if (state
.bisect_in_progress
)
1490 strbuf_addf(&desc
, _("no branch, bisect started on %s"),
1492 else if (state
.detached_from
) {
1493 if (state
.detached_at
)
1494 strbuf_addstr(&desc
, HEAD_DETACHED_AT
);
1496 strbuf_addstr(&desc
, HEAD_DETACHED_FROM
);
1497 strbuf_addstr(&desc
, state
.detached_from
);
1500 strbuf_addstr(&desc
, _("no branch"));
1501 strbuf_addch(&desc
, ')');
1505 free(state
.detached_from
);
1506 return strbuf_detach(&desc
, NULL
);
1509 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1514 return show_ref(&atom
->u
.refname
, ref
->symref
);
1517 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1519 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1520 return get_head_description();
1521 return show_ref(&atom
->u
.refname
, ref
->refname
);
1524 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
1525 struct expand_data
*oi
, struct strbuf
*err
)
1527 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1529 if (oi
->info
.contentp
) {
1530 /* We need to know that to use parse_object_buffer properly */
1531 oi
->info
.sizep
= &oi
->size
;
1532 oi
->info
.typep
= &oi
->type
;
1534 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
1535 OBJECT_INFO_LOOKUP_REPLACE
))
1536 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1537 oid_to_hex(&oi
->oid
), ref
->refname
);
1538 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
1539 BUG("Object size is less than zero.");
1541 if (oi
->info
.contentp
) {
1542 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
1546 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1547 oid_to_hex(&oi
->oid
), ref
->refname
);
1549 grab_values(ref
->value
, deref
, *obj
, oi
->content
);
1552 grab_common_values(ref
->value
, deref
, oi
);
1558 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
1562 for (i
= 0; worktrees
[i
]; i
++) {
1563 if (worktrees
[i
]->head_ref
) {
1564 struct ref_to_worktree_entry
*entry
;
1565 entry
= xmalloc(sizeof(*entry
));
1566 entry
->wt
= worktrees
[i
];
1567 hashmap_entry_init(entry
, strhash(worktrees
[i
]->head_ref
));
1569 hashmap_add(map
, entry
);
1574 static void lazy_init_worktree_map(void)
1576 if (ref_to_worktree_map
.worktrees
)
1579 ref_to_worktree_map
.worktrees
= get_worktrees(0);
1580 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
1581 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
1584 static char *get_worktree_path(const struct used_atom
*atom
, const struct ref_array_item
*ref
)
1586 struct hashmap_entry entry
;
1587 struct ref_to_worktree_entry
*lookup_result
;
1589 lazy_init_worktree_map();
1591 hashmap_entry_init(&entry
, strhash(ref
->refname
));
1592 lookup_result
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
1595 return xstrdup(lookup_result
->wt
->path
);
1601 * Parse the object referred by ref, and grab needed value.
1603 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
1607 struct object_info empty
= OBJECT_INFO_INIT
;
1609 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1611 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1612 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1615 ref
->symref
= xstrdup("");
1618 /* Fill in specials first */
1619 for (i
= 0; i
< used_atom_cnt
; i
++) {
1620 struct used_atom
*atom
= &used_atom
[i
];
1621 const char *name
= used_atom
[i
].name
;
1622 struct atom_value
*v
= &ref
->value
[i
];
1624 const char *refname
;
1625 struct branch
*branch
= NULL
;
1627 v
->handler
= append_atom
;
1635 if (starts_with(name
, "refname"))
1636 refname
= get_refname(atom
, ref
);
1637 else if (!strcmp(name
, "worktreepath")) {
1638 if (ref
->kind
== FILTER_REFS_BRANCHES
)
1639 v
->s
= get_worktree_path(atom
, ref
);
1644 else if (starts_with(name
, "symref"))
1645 refname
= get_symref(atom
, ref
);
1646 else if (starts_with(name
, "upstream")) {
1647 const char *branch_name
;
1648 /* only local branches may have an upstream */
1649 if (!skip_prefix(ref
->refname
, "refs/heads/",
1654 branch
= branch_get(branch_name
);
1656 refname
= branch_get_upstream(branch
, NULL
);
1658 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1662 } else if (atom
->u
.remote_ref
.push
) {
1663 const char *branch_name
;
1665 if (!skip_prefix(ref
->refname
, "refs/heads/",
1668 branch
= branch_get(branch_name
);
1670 if (atom
->u
.remote_ref
.push_remote
)
1673 refname
= branch_get_push(branch
, NULL
);
1677 /* We will definitely re-init v->s on the next line. */
1679 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1681 } else if (starts_with(name
, "color:")) {
1682 v
->s
= xstrdup(atom
->u
.color
);
1684 } else if (!strcmp(name
, "flag")) {
1685 char buf
[256], *cp
= buf
;
1686 if (ref
->flag
& REF_ISSYMREF
)
1687 cp
= copy_advance(cp
, ",symref");
1688 if (ref
->flag
& REF_ISPACKED
)
1689 cp
= copy_advance(cp
, ",packed");
1694 v
->s
= xstrdup(buf
+ 1);
1697 } else if (!deref
&& grab_objectname(name
, &ref
->objectname
, v
, atom
)) {
1699 } else if (!strcmp(name
, "HEAD")) {
1700 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1701 v
->s
= xstrdup("*");
1703 v
->s
= xstrdup(" ");
1705 } else if (starts_with(name
, "align")) {
1706 v
->handler
= align_atom_handler
;
1709 } else if (!strcmp(name
, "end")) {
1710 v
->handler
= end_atom_handler
;
1713 } else if (starts_with(name
, "if")) {
1715 if (skip_prefix(name
, "if:", &s
))
1719 v
->handler
= if_atom_handler
;
1721 } else if (!strcmp(name
, "then")) {
1722 v
->handler
= then_atom_handler
;
1725 } else if (!strcmp(name
, "else")) {
1726 v
->handler
= else_atom_handler
;
1733 v
->s
= xstrdup(refname
);
1735 v
->s
= xstrfmt("%s^{}", refname
);
1736 free((char *)refname
);
1739 for (i
= 0; i
< used_atom_cnt
; i
++) {
1740 struct atom_value
*v
= &ref
->value
[i
];
1741 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
1742 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1743 oid_to_hex(&ref
->objectname
), ref
->refname
);
1747 oi
.info
.contentp
= &oi
.content
;
1748 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
1749 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
1753 oi
.oid
= ref
->objectname
;
1754 if (get_object(ref
, 0, &obj
, &oi
, err
))
1758 * If there is no atom that wants to know about tagged
1759 * object, we are done.
1761 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1765 * If it is a tag object, see if we use a value that derefs
1766 * the object, and if we do grab the object it refers to.
1768 oi_deref
.oid
= ((struct tag
*)obj
)->tagged
->oid
;
1771 * NEEDSWORK: This derefs tag only once, which
1772 * is good to deal with chains of trust, but
1773 * is not consistent with what deref_tag() does
1774 * which peels the onion to the core.
1776 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
1780 * Given a ref, return the value for the atom. This lazily gets value
1781 * out of the object by calling populate value.
1783 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
1784 struct atom_value
**v
, struct strbuf
*err
)
1787 if (populate_value(ref
, err
))
1789 fill_missing_values(ref
->value
);
1791 *v
= &ref
->value
[atom
];
1796 * Return 1 if the refname matches one of the patterns, otherwise 0.
1797 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1798 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1799 * matches "refs/heads/mas*", too).
1801 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1803 const char **patterns
= filter
->name_patterns
;
1806 if (filter
->ignore_case
)
1807 flags
|= WM_CASEFOLD
;
1810 * When no '--format' option is given we need to skip the prefix
1811 * for matching refs of tags and branches.
1813 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1814 skip_prefix(refname
, "refs/heads/", &refname
) ||
1815 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1816 skip_prefix(refname
, "refs/", &refname
));
1818 for (; *patterns
; patterns
++) {
1819 if (!wildmatch(*patterns
, refname
, flags
))
1826 * Return 1 if the refname matches one of the patterns, otherwise 0.
1827 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1828 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1829 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1831 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1833 const char **pattern
= filter
->name_patterns
;
1834 int namelen
= strlen(refname
);
1835 unsigned flags
= WM_PATHNAME
;
1837 if (filter
->ignore_case
)
1838 flags
|= WM_CASEFOLD
;
1840 for (; *pattern
; pattern
++) {
1841 const char *p
= *pattern
;
1842 int plen
= strlen(p
);
1844 if ((plen
<= namelen
) &&
1845 !strncmp(refname
, p
, plen
) &&
1846 (refname
[plen
] == '\0' ||
1847 refname
[plen
] == '/' ||
1850 if (!wildmatch(p
, refname
, flags
))
1856 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1857 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1859 if (!*filter
->name_patterns
)
1860 return 1; /* No pattern always matches */
1861 if (filter
->match_as_path
)
1862 return match_name_as_path(filter
, refname
);
1863 return match_pattern(filter
, refname
);
1867 * Find the longest prefix of pattern we can pass to
1868 * `for_each_fullref_in()`, namely the part of pattern preceding the
1869 * first glob character. (Note that `for_each_fullref_in()` is
1870 * perfectly happy working with a prefix that doesn't end at a
1871 * pathname component boundary.)
1873 static void find_longest_prefix(struct strbuf
*out
, const char *pattern
)
1877 for (p
= pattern
; *p
&& !is_glob_special(*p
); p
++)
1880 strbuf_add(out
, pattern
, p
- pattern
);
1884 * This is the same as for_each_fullref_in(), but it tries to iterate
1885 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1886 * pattern match, so the callback still has to match each ref individually.
1888 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1893 struct strbuf prefix
= STRBUF_INIT
;
1896 if (!filter
->match_as_path
) {
1898 * in this case, the patterns are applied after
1899 * prefixes like "refs/heads/" etc. are stripped off,
1900 * so we have to look at everything:
1902 return for_each_fullref_in("", cb
, cb_data
, broken
);
1905 if (filter
->ignore_case
) {
1907 * we can't handle case-insensitive comparisons,
1908 * so just return everything and let the caller
1911 return for_each_fullref_in("", cb
, cb_data
, broken
);
1914 if (!filter
->name_patterns
[0]) {
1915 /* no patterns; we have to look at everything */
1916 return for_each_fullref_in("", cb
, cb_data
, broken
);
1919 if (filter
->name_patterns
[1]) {
1921 * multiple patterns; in theory this could still work as long
1922 * as the patterns are disjoint. We'd just make multiple calls
1923 * to for_each_ref(). But if they're not disjoint, we'd end up
1924 * reporting the same ref multiple times. So let's punt on that
1927 return for_each_fullref_in("", cb
, cb_data
, broken
);
1930 find_longest_prefix(&prefix
, filter
->name_patterns
[0]);
1932 ret
= for_each_fullref_in(prefix
.buf
, cb
, cb_data
, broken
);
1933 strbuf_release(&prefix
);
1938 * Given a ref (sha1, refname), check if the ref belongs to the array
1939 * of sha1s. If the given ref is a tag, check if the given tag points
1940 * at one of the sha1s in the given sha1 array.
1941 * the given sha1_array.
1943 * 1. Only a single level of inderection is obtained, we might want to
1944 * change this to account for multiple levels (e.g. annotated tags
1945 * pointing to annotated tags pointing to a commit.)
1946 * 2. As the refs are cached we might know what refname peels to without
1947 * the need to parse the object via parse_object(). peel_ref() might be a
1948 * more efficient alternative to obtain the pointee.
1950 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1951 const struct object_id
*oid
,
1952 const char *refname
)
1954 const struct object_id
*tagged_oid
= NULL
;
1957 if (oid_array_lookup(points_at
, oid
) >= 0)
1959 obj
= parse_object(the_repository
, oid
);
1961 die(_("malformed object at '%s'"), refname
);
1962 if (obj
->type
== OBJ_TAG
)
1963 tagged_oid
= &((struct tag
*)obj
)->tagged
->oid
;
1964 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
1970 * Allocate space for a new ref_array_item and copy the name and oid to it.
1972 * Callers can then fill in other struct members at their leisure.
1974 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1975 const struct object_id
*oid
)
1977 struct ref_array_item
*ref
;
1979 FLEX_ALLOC_STR(ref
, refname
, refname
);
1980 oidcpy(&ref
->objectname
, oid
);
1985 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
1986 const char *refname
,
1987 const struct object_id
*oid
)
1989 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
1991 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
1992 array
->items
[array
->nr
++] = ref
;
1997 static int ref_kind_from_refname(const char *refname
)
2005 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2006 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2007 { "refs/tags/", FILTER_REFS_TAGS
}
2010 if (!strcmp(refname
, "HEAD"))
2011 return FILTER_REFS_DETACHED_HEAD
;
2013 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2014 if (starts_with(refname
, ref_kind
[i
].prefix
))
2015 return ref_kind
[i
].kind
;
2018 return FILTER_REFS_OTHERS
;
2021 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2023 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2024 filter
->kind
== FILTER_REFS_REMOTES
||
2025 filter
->kind
== FILTER_REFS_TAGS
)
2026 return filter
->kind
;
2027 return ref_kind_from_refname(refname
);
2030 struct ref_filter_cbdata
{
2031 struct ref_array
*array
;
2032 struct ref_filter
*filter
;
2033 struct contains_cache contains_cache
;
2034 struct contains_cache no_contains_cache
;
2038 * A call-back given to for_each_ref(). Filter refs and keep them for
2039 * later object processing.
2041 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2043 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2044 struct ref_filter
*filter
= ref_cbdata
->filter
;
2045 struct ref_array_item
*ref
;
2046 struct commit
*commit
= NULL
;
2049 if (flag
& REF_BAD_NAME
) {
2050 warning(_("ignoring ref with broken name %s"), refname
);
2054 if (flag
& REF_ISBROKEN
) {
2055 warning(_("ignoring broken ref %s"), refname
);
2059 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2060 kind
= filter_ref_kind(filter
, refname
);
2061 if (!(kind
& filter
->kind
))
2064 if (!filter_pattern_match(filter
, refname
))
2067 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2071 * A merge filter is applied on refs pointing to commits. Hence
2072 * obtain the commit using the 'oid' available and discard all
2073 * non-commits early. The actual filtering is done later.
2075 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2076 commit
= lookup_commit_reference_gently(the_repository
, oid
,
2080 /* We perform the filtering for the '--contains' option... */
2081 if (filter
->with_commit
&&
2082 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2084 /* ...or for the `--no-contains' option */
2085 if (filter
->no_commit
&&
2086 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2091 * We do not open the object yet; sort may only need refname
2092 * to do its job and the resulting list may yet to be pruned
2093 * by maxcount logic.
2095 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2096 ref
->commit
= commit
;
2103 /* Free memory allocated for a ref_array_item */
2104 static void free_array_item(struct ref_array_item
*item
)
2106 free((char *)item
->symref
);
2108 free((char *)item
->value
->s
);
2114 /* Free all memory allocated for ref_array */
2115 void ref_array_clear(struct ref_array
*array
)
2119 for (i
= 0; i
< used_atom_cnt
; i
++)
2120 free((char *)used_atom
[i
].name
);
2121 FREE_AND_NULL(used_atom
);
2123 for (i
= 0; i
< array
->nr
; i
++)
2124 free_array_item(array
->items
[i
]);
2125 FREE_AND_NULL(array
->items
);
2126 array
->nr
= array
->alloc
= 0;
2127 if (ref_to_worktree_map
.worktrees
) {
2128 hashmap_free(&(ref_to_worktree_map
.map
), 1);
2129 free_worktrees(ref_to_worktree_map
.worktrees
);
2130 ref_to_worktree_map
.worktrees
= NULL
;
2134 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
2136 struct rev_info revs
;
2138 struct ref_filter
*filter
= ref_cbdata
->filter
;
2139 struct ref_array
*array
= ref_cbdata
->array
;
2140 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
2142 repo_init_revisions(the_repository
, &revs
, NULL
);
2144 for (i
= 0; i
< array
->nr
; i
++) {
2145 struct ref_array_item
*item
= array
->items
[i
];
2146 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
2147 to_clear
[i
] = item
->commit
;
2150 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
2151 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
2154 if (prepare_revision_walk(&revs
))
2155 die(_("revision walk setup failed"));
2160 for (i
= 0; i
< old_nr
; i
++) {
2161 struct ref_array_item
*item
= array
->items
[i
];
2162 struct commit
*commit
= item
->commit
;
2164 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2166 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
2167 array
->items
[array
->nr
++] = array
->items
[i
];
2169 free_array_item(item
);
2172 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2173 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
2178 * API for filtering a set of refs. Based on the type of refs the user
2179 * has requested, we iterate through those refs and apply filters
2180 * as per the given ref_filter structure and finally store the
2181 * filtered refs in the ref_array structure.
2183 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2185 struct ref_filter_cbdata ref_cbdata
;
2187 unsigned int broken
= 0;
2189 ref_cbdata
.array
= array
;
2190 ref_cbdata
.filter
= filter
;
2192 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
2194 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2196 init_contains_cache(&ref_cbdata
.contains_cache
);
2197 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2199 /* Simple per-ref filtering */
2201 die("filter_refs: invalid type");
2204 * For common cases where we need only branches or remotes or tags,
2205 * we only iterate through those refs. If a mix of refs is needed,
2206 * we iterate over all refs and filter out required refs with the help
2207 * of filter_ref_kind().
2209 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2210 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
2211 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2212 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2213 else if (filter
->kind
== FILTER_REFS_TAGS
)
2214 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2215 else if (filter
->kind
& FILTER_REFS_ALL
)
2216 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2217 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2218 head_ref(ref_filter_handler
, &ref_cbdata
);
2221 clear_contains_cache(&ref_cbdata
.contains_cache
);
2222 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2224 /* Filters that need revision walking */
2225 if (filter
->merge_commit
)
2226 do_merge_filter(&ref_cbdata
);
2231 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2233 struct atom_value
*va
, *vb
;
2235 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2236 int (*cmp_fn
)(const char *, const char *);
2237 struct strbuf err
= STRBUF_INIT
;
2239 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2241 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2243 strbuf_release(&err
);
2244 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2246 cmp
= versioncmp(va
->s
, vb
->s
);
2247 else if (cmp_type
== FIELD_STR
)
2248 cmp
= cmp_fn(va
->s
, vb
->s
);
2250 if (va
->value
< vb
->value
)
2252 else if (va
->value
== vb
->value
)
2253 cmp
= cmp_fn(a
->refname
, b
->refname
);
2258 return (s
->reverse
) ? -cmp
: cmp
;
2261 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2263 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2264 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2265 struct ref_sorting
*s
;
2267 for (s
= ref_sorting
; s
; s
= s
->next
) {
2268 int cmp
= cmp_ref_sorting(s
, a
, b
);
2275 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2277 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2280 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2282 struct strbuf
*s
= &state
->stack
->output
;
2284 while (*cp
&& (!ep
|| cp
< ep
)) {
2289 int ch
= hex2chr(cp
+ 1);
2291 strbuf_addch(s
, ch
);
2297 strbuf_addch(s
, *cp
);
2302 int format_ref_array_item(struct ref_array_item
*info
,
2303 const struct ref_format
*format
,
2304 struct strbuf
*final_buf
,
2305 struct strbuf
*error_buf
)
2307 const char *cp
, *sp
, *ep
;
2308 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2310 state
.quote_style
= format
->quote_style
;
2311 push_stack_element(&state
.stack
);
2313 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2314 struct atom_value
*atomv
;
2317 ep
= strchr(sp
, ')');
2319 append_literal(cp
, sp
, &state
);
2320 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2321 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2322 atomv
->handler(atomv
, &state
, error_buf
)) {
2323 pop_stack_element(&state
.stack
);
2328 sp
= cp
+ strlen(cp
);
2329 append_literal(cp
, sp
, &state
);
2331 if (format
->need_color_reset_at_eol
) {
2332 struct atom_value resetv
;
2333 resetv
.s
= GIT_COLOR_RESET
;
2334 if (append_atom(&resetv
, &state
, error_buf
)) {
2335 pop_stack_element(&state
.stack
);
2339 if (state
.stack
->prev
) {
2340 pop_stack_element(&state
.stack
);
2341 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2343 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2344 pop_stack_element(&state
.stack
);
2348 void show_ref_array_item(struct ref_array_item
*info
,
2349 const struct ref_format
*format
)
2351 struct strbuf final_buf
= STRBUF_INIT
;
2352 struct strbuf error_buf
= STRBUF_INIT
;
2354 if (format_ref_array_item(info
, format
, &final_buf
, &error_buf
))
2355 die("%s", error_buf
.buf
);
2356 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2357 strbuf_release(&error_buf
);
2358 strbuf_release(&final_buf
);
2362 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2363 const struct ref_format
*format
)
2365 struct ref_array_item
*ref_item
;
2366 ref_item
= new_ref_array_item(name
, oid
);
2367 ref_item
->kind
= ref_kind_from_refname(name
);
2368 show_ref_array_item(ref_item
, format
);
2369 free_array_item(ref_item
);
2372 static int parse_sorting_atom(const char *atom
)
2375 * This parses an atom using a dummy ref_format, since we don't
2376 * actually care about the formatting details.
2378 struct ref_format dummy
= REF_FORMAT_INIT
;
2379 const char *end
= atom
+ strlen(atom
);
2380 struct strbuf err
= STRBUF_INIT
;
2381 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2384 strbuf_release(&err
);
2388 /* If no sorting option is given, use refname to sort as default */
2389 struct ref_sorting
*ref_default_sorting(void)
2391 static const char cstr_name
[] = "refname";
2393 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2395 sorting
->next
= NULL
;
2396 sorting
->atom
= parse_sorting_atom(cstr_name
);
2400 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2402 struct ref_sorting
*s
;
2404 s
= xcalloc(1, sizeof(*s
));
2405 s
->next
= *sorting_tail
;
2412 if (skip_prefix(arg
, "version:", &arg
) ||
2413 skip_prefix(arg
, "v:", &arg
))
2415 s
->atom
= parse_sorting_atom(arg
);
2418 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2421 * NEEDSWORK: We should probably clear the list in this case, but we've
2422 * already munged the global used_atoms list, which would need to be
2425 BUG_ON_OPT_NEG(unset
);
2427 parse_ref_sorting(opt
->value
, arg
);
2431 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2433 struct ref_filter
*rf
= opt
->value
;
2434 struct object_id oid
;
2435 int no_merged
= starts_with(opt
->long_name
, "no");
2437 BUG_ON_OPT_NEG(unset
);
2441 return error(_("option `%s' is incompatible with --merged"),
2444 return error(_("option `%s' is incompatible with --no-merged"),
2449 rf
->merge
= no_merged
2450 ? REF_FILTER_MERGED_OMIT
2451 : REF_FILTER_MERGED_INCLUDE
;
2453 if (get_oid(arg
, &oid
))
2454 die(_("malformed object name %s"), arg
);
2456 rf
->merge_commit
= lookup_commit_reference_gently(the_repository
,
2458 if (!rf
->merge_commit
)
2459 return error(_("option `%s' must point to a commit"), opt
->long_name
);