3 #include "parse-options.h"
6 #include "object-store.h"
7 #include "repository.h"
13 #include "ref-filter.h"
16 #include "git-compat-util.h"
19 #include "wt-status.h"
20 #include "commit-slab.h"
21 #include "commit-graph.h"
22 #include "commit-reach.h"
27 static struct ref_msg
{
31 const char *ahead_behind
;
33 /* Untranslated plumbing messages: */
40 void setup_ref_filter_porcelain_msg(void)
42 msgs
.gone
= _("gone");
43 msgs
.ahead
= _("ahead %d");
44 msgs
.behind
= _("behind %d");
45 msgs
.ahead_behind
= _("ahead %d, behind %d");
48 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
49 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
50 typedef enum { SOURCE_NONE
= 0, SOURCE_OBJ
, SOURCE_OTHER
} info_source
;
58 cmp_status cmp_status
;
60 unsigned int then_atom_seen
: 1,
62 condition_satisfied
: 1;
66 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
70 static struct expand_data
{
72 enum object_type type
;
75 struct object_id delta_base_oid
;
78 struct object_info info
;
81 struct ref_to_worktree_entry
{
82 struct hashmap_entry ent
;
83 struct worktree
*wt
; /* key is wt->head_ref */
86 static int ref_to_worktree_map_cmpfnc(const void *unused_lookupdata
,
87 const struct hashmap_entry
*eptr
,
88 const struct hashmap_entry
*kptr
,
89 const void *keydata_aka_refname
)
91 const struct ref_to_worktree_entry
*e
, *k
;
93 e
= container_of(eptr
, const struct ref_to_worktree_entry
, ent
);
94 k
= container_of(kptr
, const struct ref_to_worktree_entry
, ent
);
96 return strcmp(e
->wt
->head_ref
,
97 keydata_aka_refname
? keydata_aka_refname
: k
->wt
->head_ref
);
100 static struct ref_to_worktree_map
{
102 struct worktree
**worktrees
;
103 } ref_to_worktree_map
;
106 * An atom is a valid field atom listed below, possibly prefixed with
107 * a "*" to denote deref_tag().
109 * We parse given format string and sort specifiers, and make a list
110 * of properties that we need to extract out of objects. ref_array_item
111 * structure will hold an array of values extracted that can be
112 * indexed with the "atom number", which is an index into this
115 static struct used_atom
{
120 char color
[COLOR_MAXLEN
];
124 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
126 struct refname_atom refname
;
127 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
130 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LENGTH
,
131 C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
132 struct process_trailer_options trailer_opts
;
136 cmp_status cmp_status
;
140 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
143 struct refname_atom refname
;
147 static int used_atom_cnt
, need_tagged
, need_symref
;
150 * Expand string, append it to strbuf *sb, then return error code ret.
151 * Allow to save few lines of code.
153 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
157 strbuf_vaddf(sb
, fmt
, ap
);
162 static int color_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
163 const char *color_value
, struct strbuf
*err
)
166 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
167 if (color_parse(color_value
, atom
->u
.color
) < 0)
168 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
171 * We check this after we've parsed the color, which lets us complain
172 * about syntactically bogus color names even if they won't be used.
174 if (!want_color(format
->use_color
))
175 color_parse("", atom
->u
.color
);
179 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
180 const char *name
, struct strbuf
*err
)
183 atom
->option
= R_NORMAL
;
184 else if (!strcmp(arg
, "short"))
185 atom
->option
= R_SHORT
;
186 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
187 skip_prefix(arg
, "strip=", &arg
)) {
188 atom
->option
= R_LSTRIP
;
189 if (strtol_i(arg
, 10, &atom
->lstrip
))
190 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
191 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
192 atom
->option
= R_RSTRIP
;
193 if (strtol_i(arg
, 10, &atom
->rstrip
))
194 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
196 return strbuf_addf_ret(err
, -1, _("unrecognized %%(%s) argument: %s"), name
, arg
);
200 static int remote_ref_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
201 const char *arg
, struct strbuf
*err
)
203 struct string_list params
= STRING_LIST_INIT_DUP
;
206 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
207 atom
->u
.remote_ref
.push
= 1;
210 atom
->u
.remote_ref
.option
= RR_REF
;
211 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
212 arg
, atom
->name
, err
);
215 atom
->u
.remote_ref
.nobracket
= 0;
216 string_list_split(¶ms
, arg
, ',', -1);
218 for (i
= 0; i
< params
.nr
; i
++) {
219 const char *s
= params
.items
[i
].string
;
221 if (!strcmp(s
, "track"))
222 atom
->u
.remote_ref
.option
= RR_TRACK
;
223 else if (!strcmp(s
, "trackshort"))
224 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
225 else if (!strcmp(s
, "nobracket"))
226 atom
->u
.remote_ref
.nobracket
= 1;
227 else if (!strcmp(s
, "remotename")) {
228 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
229 atom
->u
.remote_ref
.push_remote
= 1;
230 } else if (!strcmp(s
, "remoteref")) {
231 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
232 atom
->u
.remote_ref
.push_remote
= 1;
234 atom
->u
.remote_ref
.option
= RR_REF
;
235 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
236 arg
, atom
->name
, err
)) {
237 string_list_clear(¶ms
, 0);
243 string_list_clear(¶ms
, 0);
247 static int objecttype_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
248 const char *arg
, struct strbuf
*err
)
251 return strbuf_addf_ret(err
, -1, _("%%(objecttype) does not take arguments"));
252 if (*atom
->name
== '*')
253 oi_deref
.info
.typep
= &oi_deref
.type
;
255 oi
.info
.typep
= &oi
.type
;
259 static int objectsize_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
260 const char *arg
, struct strbuf
*err
)
263 if (*atom
->name
== '*')
264 oi_deref
.info
.sizep
= &oi_deref
.size
;
266 oi
.info
.sizep
= &oi
.size
;
267 } else if (!strcmp(arg
, "disk")) {
268 if (*atom
->name
== '*')
269 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
271 oi
.info
.disk_sizep
= &oi
.disk_size
;
273 return strbuf_addf_ret(err
, -1, _("unrecognized %%(objectsize) argument: %s"), arg
);
277 static int deltabase_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
278 const char *arg
, struct strbuf
*err
)
281 return strbuf_addf_ret(err
, -1, _("%%(deltabase) does not take arguments"));
282 if (*atom
->name
== '*')
283 oi_deref
.info
.delta_base_oid
= &oi_deref
.delta_base_oid
;
285 oi
.info
.delta_base_oid
= &oi
.delta_base_oid
;
289 static int body_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
290 const char *arg
, struct strbuf
*err
)
293 return strbuf_addf_ret(err
, -1, _("%%(body) does not take arguments"));
294 atom
->u
.contents
.option
= C_BODY_DEP
;
298 static int subject_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
299 const char *arg
, struct strbuf
*err
)
302 return strbuf_addf_ret(err
, -1, _("%%(subject) does not take arguments"));
303 atom
->u
.contents
.option
= C_SUB
;
307 static int trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
308 const char *arg
, struct strbuf
*err
)
310 struct string_list params
= STRING_LIST_INIT_DUP
;
313 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
316 string_list_split(¶ms
, arg
, ',', -1);
317 for (i
= 0; i
< params
.nr
; i
++) {
318 const char *s
= params
.items
[i
].string
;
319 if (!strcmp(s
, "unfold"))
320 atom
->u
.contents
.trailer_opts
.unfold
= 1;
321 else if (!strcmp(s
, "only"))
322 atom
->u
.contents
.trailer_opts
.only_trailers
= 1;
324 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), s
);
325 string_list_clear(¶ms
, 0);
330 atom
->u
.contents
.option
= C_TRAILERS
;
331 string_list_clear(¶ms
, 0);
335 static int contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
336 const char *arg
, struct strbuf
*err
)
339 atom
->u
.contents
.option
= C_BARE
;
340 else if (!strcmp(arg
, "body"))
341 atom
->u
.contents
.option
= C_BODY
;
342 else if (!strcmp(arg
, "size"))
343 atom
->u
.contents
.option
= C_LENGTH
;
344 else if (!strcmp(arg
, "signature"))
345 atom
->u
.contents
.option
= C_SIG
;
346 else if (!strcmp(arg
, "subject"))
347 atom
->u
.contents
.option
= C_SUB
;
348 else if (!strcmp(arg
, "trailers")) {
349 if (trailers_atom_parser(format
, atom
, NULL
, err
))
351 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
352 if (trailers_atom_parser(format
, atom
, arg
, err
))
354 } else if (skip_prefix(arg
, "lines=", &arg
)) {
355 atom
->u
.contents
.option
= C_LINES
;
356 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
357 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
359 return strbuf_addf_ret(err
, -1, _("unrecognized %%(contents) argument: %s"), arg
);
363 static int objectname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
364 const char *arg
, struct strbuf
*err
)
367 atom
->u
.objectname
.option
= O_FULL
;
368 else if (!strcmp(arg
, "short"))
369 atom
->u
.objectname
.option
= O_SHORT
;
370 else if (skip_prefix(arg
, "short=", &arg
)) {
371 atom
->u
.objectname
.option
= O_LENGTH
;
372 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
373 atom
->u
.objectname
.length
== 0)
374 return strbuf_addf_ret(err
, -1, _("positive value expected objectname:short=%s"), arg
);
375 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
376 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
378 return strbuf_addf_ret(err
, -1, _("unrecognized %%(objectname) argument: %s"), arg
);
382 static int refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
383 const char *arg
, struct strbuf
*err
)
385 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
388 static align_type
parse_align_position(const char *s
)
390 if (!strcmp(s
, "right"))
392 else if (!strcmp(s
, "middle"))
394 else if (!strcmp(s
, "left"))
399 static int align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
400 const char *arg
, struct strbuf
*err
)
402 struct align
*align
= &atom
->u
.align
;
403 struct string_list params
= STRING_LIST_INIT_DUP
;
405 unsigned int width
= ~0U;
408 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
410 align
->position
= ALIGN_LEFT
;
412 string_list_split(¶ms
, arg
, ',', -1);
413 for (i
= 0; i
< params
.nr
; i
++) {
414 const char *s
= params
.items
[i
].string
;
417 if (skip_prefix(s
, "position=", &s
)) {
418 position
= parse_align_position(s
);
420 strbuf_addf(err
, _("unrecognized position:%s"), s
);
421 string_list_clear(¶ms
, 0);
424 align
->position
= position
;
425 } else if (skip_prefix(s
, "width=", &s
)) {
426 if (strtoul_ui(s
, 10, &width
)) {
427 strbuf_addf(err
, _("unrecognized width:%s"), s
);
428 string_list_clear(¶ms
, 0);
431 } else if (!strtoul_ui(s
, 10, &width
))
433 else if ((position
= parse_align_position(s
)) >= 0)
434 align
->position
= position
;
436 strbuf_addf(err
, _("unrecognized %%(align) argument: %s"), s
);
437 string_list_clear(¶ms
, 0);
443 string_list_clear(¶ms
, 0);
444 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
446 align
->width
= width
;
447 string_list_clear(¶ms
, 0);
451 static int if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
452 const char *arg
, struct strbuf
*err
)
455 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
457 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
458 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
459 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
460 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
462 return strbuf_addf_ret(err
, -1, _("unrecognized %%(if) argument: %s"), arg
);
466 static int head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
467 const char *arg
, struct strbuf
*unused_err
)
469 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
477 int (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
,
478 const char *arg
, struct strbuf
*err
);
480 { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
481 { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
482 { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
483 { "objectname", SOURCE_OTHER
, FIELD_STR
, objectname_atom_parser
},
484 { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
485 { "tree", SOURCE_OBJ
},
486 { "parent", SOURCE_OBJ
},
487 { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
488 { "object", SOURCE_OBJ
},
489 { "type", SOURCE_OBJ
},
490 { "tag", SOURCE_OBJ
},
491 { "author", SOURCE_OBJ
},
492 { "authorname", SOURCE_OBJ
},
493 { "authoremail", SOURCE_OBJ
},
494 { "authordate", SOURCE_OBJ
, FIELD_TIME
},
495 { "committer", SOURCE_OBJ
},
496 { "committername", SOURCE_OBJ
},
497 { "committeremail", SOURCE_OBJ
},
498 { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
499 { "tagger", SOURCE_OBJ
},
500 { "taggername", SOURCE_OBJ
},
501 { "taggeremail", SOURCE_OBJ
},
502 { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
503 { "creator", SOURCE_OBJ
},
504 { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
505 { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
506 { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
507 { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
508 { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
509 { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
510 { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
511 { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
512 { "flag", SOURCE_NONE
},
513 { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
514 { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
515 { "worktreepath", SOURCE_NONE
},
516 { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
517 { "end", SOURCE_NONE
},
518 { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
519 { "then", SOURCE_NONE
},
520 { "else", SOURCE_NONE
},
522 * Please update $__git_ref_fieldlist in git-completion.bash
523 * when you add new atoms
527 #define REF_FORMATTING_STATE_INIT { 0, NULL }
529 struct ref_formatting_stack
{
530 struct ref_formatting_stack
*prev
;
531 struct strbuf output
;
532 void (*at_end
)(struct ref_formatting_stack
**stack
);
536 struct ref_formatting_state
{
538 struct ref_formatting_stack
*stack
;
543 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
545 uintmax_t value
; /* used for sorting when not FIELD_STR */
546 struct used_atom
*atom
;
550 * Used to parse format string and sort specifiers
552 static int parse_ref_filter_atom(const struct ref_format
*format
,
553 const char *atom
, const char *ep
,
561 if (*sp
== '*' && sp
< ep
)
564 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
565 (int)(ep
-atom
), atom
);
567 /* Do we have the atom already used elsewhere? */
568 for (i
= 0; i
< used_atom_cnt
; i
++) {
569 int len
= strlen(used_atom
[i
].name
);
570 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
575 * If the atom name has a colon, strip it and everything after
576 * it off - it specifies the format for this entry, and
577 * shouldn't be used for checking against the valid_atom
580 arg
= memchr(sp
, ':', ep
- sp
);
581 atom_len
= (arg
? arg
: ep
) - sp
;
583 /* Is the atom a valid one? */
584 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
585 int len
= strlen(valid_atom
[i
].name
);
586 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
590 if (ARRAY_SIZE(valid_atom
) <= i
)
591 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
592 (int)(ep
-atom
), atom
);
593 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
594 return strbuf_addf_ret(err
, -1,
595 _("not a git repository, but the field '%.*s' requires access to object data"),
596 (int)(ep
-atom
), atom
);
598 /* Add it in, including the deref prefix */
601 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
602 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
603 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
604 used_atom
[at
].source
= valid_atom
[i
].source
;
605 if (used_atom
[at
].source
== SOURCE_OBJ
) {
607 oi_deref
.info
.contentp
= &oi_deref
.content
;
609 oi
.info
.contentp
= &oi
.content
;
612 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
615 * Treat empty sub-arguments list as NULL (i.e.,
616 * "%(atom:)" is equivalent to "%(atom)").
621 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
622 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
626 if (!strcmp(valid_atom
[i
].name
, "symref"))
631 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
633 switch (quote_style
) {
635 strbuf_addstr(s
, str
);
638 sq_quote_buf(s
, str
);
641 perl_quote_buf(s
, str
);
644 python_quote_buf(s
, str
);
647 tcl_quote_buf(s
, str
);
652 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
653 struct strbuf
*unused_err
)
656 * Quote formatting is only done when the stack has a single
657 * element. Otherwise quote formatting is done on the
658 * element's entire output strbuf when the %(end) atom is
661 if (!state
->stack
->prev
)
662 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
664 strbuf_addstr(&state
->stack
->output
, v
->s
);
668 static void push_stack_element(struct ref_formatting_stack
**stack
)
670 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
672 strbuf_init(&s
->output
, 0);
677 static void pop_stack_element(struct ref_formatting_stack
**stack
)
679 struct ref_formatting_stack
*current
= *stack
;
680 struct ref_formatting_stack
*prev
= current
->prev
;
683 strbuf_addbuf(&prev
->output
, ¤t
->output
);
684 strbuf_release(¤t
->output
);
689 static void end_align_handler(struct ref_formatting_stack
**stack
)
691 struct ref_formatting_stack
*cur
= *stack
;
692 struct align
*align
= (struct align
*)cur
->at_end_data
;
693 struct strbuf s
= STRBUF_INIT
;
695 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
696 strbuf_swap(&cur
->output
, &s
);
700 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
701 struct strbuf
*unused_err
)
703 struct ref_formatting_stack
*new_stack
;
705 push_stack_element(&state
->stack
);
706 new_stack
= state
->stack
;
707 new_stack
->at_end
= end_align_handler
;
708 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
712 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
714 struct ref_formatting_stack
*cur
= *stack
;
715 struct ref_formatting_stack
*prev
= cur
->prev
;
716 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
718 if (!if_then_else
->then_atom_seen
)
719 die(_("format: %%(if) atom used without a %%(then) atom"));
721 if (if_then_else
->else_atom_seen
) {
723 * There is an %(else) atom: we need to drop one state from the
724 * stack, either the %(else) branch if the condition is satisfied, or
725 * the %(then) branch if it isn't.
727 if (if_then_else
->condition_satisfied
) {
728 strbuf_reset(&cur
->output
);
729 pop_stack_element(&cur
);
731 strbuf_swap(&cur
->output
, &prev
->output
);
732 strbuf_reset(&cur
->output
);
733 pop_stack_element(&cur
);
735 } else if (!if_then_else
->condition_satisfied
) {
737 * No %(else) atom: just drop the %(then) branch if the
738 * condition is not satisfied.
740 strbuf_reset(&cur
->output
);
747 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
748 struct strbuf
*unused_err
)
750 struct ref_formatting_stack
*new_stack
;
751 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
753 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
754 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
756 push_stack_element(&state
->stack
);
757 new_stack
= state
->stack
;
758 new_stack
->at_end
= if_then_else_handler
;
759 new_stack
->at_end_data
= if_then_else
;
763 static int is_empty(const char *s
)
773 static int then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
776 struct ref_formatting_stack
*cur
= state
->stack
;
777 struct if_then_else
*if_then_else
= NULL
;
779 if (cur
->at_end
== if_then_else_handler
)
780 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
782 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used without an %%(if) atom"));
783 if (if_then_else
->then_atom_seen
)
784 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
785 if (if_then_else
->else_atom_seen
)
786 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
787 if_then_else
->then_atom_seen
= 1;
789 * If the 'equals' or 'notequals' attribute is used then
790 * perform the required comparison. If not, only non-empty
791 * strings satisfy the 'if' condition.
793 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
794 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
795 if_then_else
->condition_satisfied
= 1;
796 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
797 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
798 if_then_else
->condition_satisfied
= 1;
799 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
800 if_then_else
->condition_satisfied
= 1;
801 strbuf_reset(&cur
->output
);
805 static int else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
808 struct ref_formatting_stack
*prev
= state
->stack
;
809 struct if_then_else
*if_then_else
= NULL
;
811 if (prev
->at_end
== if_then_else_handler
)
812 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
814 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without an %%(if) atom"));
815 if (!if_then_else
->then_atom_seen
)
816 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without a %%(then) atom"));
817 if (if_then_else
->else_atom_seen
)
818 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
819 if_then_else
->else_atom_seen
= 1;
820 push_stack_element(&state
->stack
);
821 state
->stack
->at_end_data
= prev
->at_end_data
;
822 state
->stack
->at_end
= prev
->at_end
;
826 static int end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
829 struct ref_formatting_stack
*current
= state
->stack
;
830 struct strbuf s
= STRBUF_INIT
;
832 if (!current
->at_end
)
833 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
834 current
->at_end(&state
->stack
);
836 /* Stack may have been popped within at_end(), hence reset the current pointer */
837 current
= state
->stack
;
840 * Perform quote formatting when the stack element is that of
841 * a supporting atom. If nested then perform quote formatting
842 * only on the topmost supporting atom.
844 if (!current
->prev
->prev
) {
845 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
846 strbuf_swap(¤t
->output
, &s
);
849 pop_stack_element(&state
->stack
);
854 * In a format string, find the next occurrence of %(atom).
856 static const char *find_next(const char *cp
)
861 * %( is the start of an atom;
862 * %% is a quoted per-cent.
866 else if (cp
[1] == '%')
867 cp
++; /* skip over two % */
868 /* otherwise this is a singleton, literal % */
876 * Make sure the format string is well formed, and parse out
879 int verify_ref_format(struct ref_format
*format
)
883 format
->need_color_reset_at_eol
= 0;
884 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
885 struct strbuf err
= STRBUF_INIT
;
886 const char *color
, *ep
= strchr(sp
, ')');
890 return error(_("malformed format string %s"), sp
);
891 /* sp points at "%(" and ep points at the closing ")" */
892 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
897 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
898 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
899 strbuf_release(&err
);
901 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
902 format
->need_color_reset_at_eol
= 0;
906 static int grab_objectname(const char *name
, const struct object_id
*oid
,
907 struct atom_value
*v
, struct used_atom
*atom
)
909 if (starts_with(name
, "objectname")) {
910 if (atom
->u
.objectname
.option
== O_SHORT
) {
911 v
->s
= xstrdup(find_unique_abbrev(oid
, DEFAULT_ABBREV
));
913 } else if (atom
->u
.objectname
.option
== O_FULL
) {
914 v
->s
= xstrdup(oid_to_hex(oid
));
916 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
917 v
->s
= xstrdup(find_unique_abbrev(oid
, atom
->u
.objectname
.length
));
920 BUG("unknown %%(objectname) option");
925 /* See grab_values */
926 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
930 for (i
= 0; i
< used_atom_cnt
; i
++) {
931 const char *name
= used_atom
[i
].name
;
932 struct atom_value
*v
= &val
[i
];
933 if (!!deref
!= (*name
== '*'))
937 if (!strcmp(name
, "objecttype"))
938 v
->s
= xstrdup(type_name(oi
->type
));
939 else if (!strcmp(name
, "objectsize:disk")) {
940 v
->value
= oi
->disk_size
;
941 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
942 } else if (!strcmp(name
, "objectsize")) {
944 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
945 } else if (!strcmp(name
, "deltabase"))
946 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
948 grab_objectname(name
, &oi
->oid
, v
, &used_atom
[i
]);
952 /* See grab_values */
953 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
956 struct tag
*tag
= (struct tag
*) obj
;
958 for (i
= 0; i
< used_atom_cnt
; i
++) {
959 const char *name
= used_atom
[i
].name
;
960 struct atom_value
*v
= &val
[i
];
961 if (!!deref
!= (*name
== '*'))
965 if (!strcmp(name
, "tag"))
966 v
->s
= xstrdup(tag
->tag
);
967 else if (!strcmp(name
, "type") && tag
->tagged
)
968 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
969 else if (!strcmp(name
, "object") && tag
->tagged
)
970 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
974 /* See grab_values */
975 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
978 struct commit
*commit
= (struct commit
*) obj
;
980 for (i
= 0; i
< used_atom_cnt
; i
++) {
981 const char *name
= used_atom
[i
].name
;
982 struct atom_value
*v
= &val
[i
];
983 if (!!deref
!= (*name
== '*'))
987 if (!strcmp(name
, "tree")) {
988 v
->s
= xstrdup(oid_to_hex(get_commit_tree_oid(commit
)));
990 else if (!strcmp(name
, "numparent")) {
991 v
->value
= commit_list_count(commit
->parents
);
992 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
994 else if (!strcmp(name
, "parent")) {
995 struct commit_list
*parents
;
996 struct strbuf s
= STRBUF_INIT
;
997 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
998 struct commit
*parent
= parents
->item
;
999 if (parents
!= commit
->parents
)
1000 strbuf_addch(&s
, ' ');
1001 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
1003 v
->s
= strbuf_detach(&s
, NULL
);
1008 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1012 if (!strncmp(buf
, who
, wholen
) &&
1014 return buf
+ wholen
+ 1;
1015 eol
= strchr(buf
, '\n');
1020 return ""; /* end of header */
1026 static const char *copy_line(const char *buf
)
1028 const char *eol
= strchrnul(buf
, '\n');
1029 return xmemdupz(buf
, eol
- buf
);
1032 static const char *copy_name(const char *buf
)
1035 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1036 if (!strncmp(cp
, " <", 2))
1037 return xmemdupz(buf
, cp
- buf
);
1042 static const char *copy_email(const char *buf
)
1044 const char *email
= strchr(buf
, '<');
1045 const char *eoemail
;
1048 eoemail
= strchr(email
, '>');
1051 return xmemdupz(email
, eoemail
+ 1 - email
);
1054 static char *copy_subject(const char *buf
, unsigned long len
)
1056 char *r
= xmemdupz(buf
, len
);
1059 for (i
= 0; i
< len
; i
++)
1066 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1068 const char *eoemail
= strstr(buf
, "> ");
1070 timestamp_t timestamp
;
1072 struct date_mode date_mode
= { DATE_NORMAL
};
1073 const char *formatp
;
1076 * We got here because atomname ends in "date" or "date<something>";
1077 * it's not possible that <something> is not ":<format>" because
1078 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1079 * ":" means no format is specified, and use the default.
1081 formatp
= strchr(atomname
, ':');
1082 if (formatp
!= NULL
) {
1084 parse_date_format(formatp
, &date_mode
);
1089 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1090 if (timestamp
== TIME_MAX
)
1092 tz
= strtol(zone
, NULL
, 10);
1093 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1095 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1096 v
->value
= timestamp
;
1103 /* See grab_values */
1104 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1107 int wholen
= strlen(who
);
1108 const char *wholine
= NULL
;
1110 for (i
= 0; i
< used_atom_cnt
; i
++) {
1111 const char *name
= used_atom
[i
].name
;
1112 struct atom_value
*v
= &val
[i
];
1113 if (!!deref
!= (*name
== '*'))
1117 if (strncmp(who
, name
, wholen
))
1119 if (name
[wholen
] != 0 &&
1120 strcmp(name
+ wholen
, "name") &&
1121 strcmp(name
+ wholen
, "email") &&
1122 !starts_with(name
+ wholen
, "date"))
1125 wholine
= find_wholine(who
, wholen
, buf
);
1127 return; /* no point looking for it */
1128 if (name
[wholen
] == 0)
1129 v
->s
= copy_line(wholine
);
1130 else if (!strcmp(name
+ wholen
, "name"))
1131 v
->s
= copy_name(wholine
);
1132 else if (!strcmp(name
+ wholen
, "email"))
1133 v
->s
= copy_email(wholine
);
1134 else if (starts_with(name
+ wholen
, "date"))
1135 grab_date(wholine
, v
, name
);
1139 * For a tag or a commit object, if "creator" or "creatordate" is
1140 * requested, do something special.
1142 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1143 return; /* "author" for commit object is not wanted */
1145 wholine
= find_wholine(who
, wholen
, buf
);
1148 for (i
= 0; i
< used_atom_cnt
; i
++) {
1149 const char *name
= used_atom
[i
].name
;
1150 struct atom_value
*v
= &val
[i
];
1151 if (!!deref
!= (*name
== '*'))
1156 if (starts_with(name
, "creatordate"))
1157 grab_date(wholine
, v
, name
);
1158 else if (!strcmp(name
, "creator"))
1159 v
->s
= copy_line(wholine
);
1163 static void find_subpos(const char *buf
,
1164 const char **sub
, unsigned long *sublen
,
1165 const char **body
, unsigned long *bodylen
,
1166 unsigned long *nonsiglen
,
1167 const char **sig
, unsigned long *siglen
)
1170 /* skip past header until we hit empty line */
1171 while (*buf
&& *buf
!= '\n') {
1172 eol
= strchrnul(buf
, '\n');
1177 /* skip any empty lines */
1178 while (*buf
== '\n')
1181 /* parse signature first; we might not even have a subject line */
1182 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
1183 *siglen
= strlen(*sig
);
1185 /* subject is first non-empty line */
1187 /* subject goes to first empty line */
1188 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
1189 eol
= strchrnul(buf
, '\n');
1194 *sublen
= buf
- *sub
;
1195 /* drop trailing newline, if present */
1196 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1199 /* skip any empty lines */
1200 while (*buf
== '\n')
1203 *bodylen
= strlen(buf
);
1204 *nonsiglen
= *sig
- buf
;
1208 * If 'lines' is greater than 0, append that many lines from the given
1209 * 'buf' of length 'size' to the given strbuf.
1211 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1214 const char *sp
, *eol
;
1219 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1221 strbuf_addstr(out
, "\n ");
1222 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1223 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1224 strbuf_add(out
, sp
, len
);
1231 /* See grab_values */
1232 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, void *buf
)
1235 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1236 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1238 for (i
= 0; i
< used_atom_cnt
; i
++) {
1239 struct used_atom
*atom
= &used_atom
[i
];
1240 const char *name
= atom
->name
;
1241 struct atom_value
*v
= &val
[i
];
1242 if (!!deref
!= (*name
== '*'))
1246 if (strcmp(name
, "subject") &&
1247 strcmp(name
, "body") &&
1248 !starts_with(name
, "trailers") &&
1249 !starts_with(name
, "contents"))
1254 &bodypos
, &bodylen
, &nonsiglen
,
1257 if (atom
->u
.contents
.option
== C_SUB
)
1258 v
->s
= copy_subject(subpos
, sublen
);
1259 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1260 v
->s
= xmemdupz(bodypos
, bodylen
);
1261 else if (atom
->u
.contents
.option
== C_LENGTH
)
1262 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)strlen(subpos
));
1263 else if (atom
->u
.contents
.option
== C_BODY
)
1264 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1265 else if (atom
->u
.contents
.option
== C_SIG
)
1266 v
->s
= xmemdupz(sigpos
, siglen
);
1267 else if (atom
->u
.contents
.option
== C_LINES
) {
1268 struct strbuf s
= STRBUF_INIT
;
1269 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1271 /* Size is the length of the message after removing the signature */
1272 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1273 v
->s
= strbuf_detach(&s
, NULL
);
1274 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1275 struct strbuf s
= STRBUF_INIT
;
1277 /* Format the trailer info according to the trailer_opts given */
1278 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1280 v
->s
= strbuf_detach(&s
, NULL
);
1281 } else if (atom
->u
.contents
.option
== C_BARE
)
1282 v
->s
= xstrdup(subpos
);
1287 * We want to have empty print-string for field requests
1288 * that do not apply (e.g. "authordate" for a tag object)
1290 static void fill_missing_values(struct atom_value
*val
)
1293 for (i
= 0; i
< used_atom_cnt
; i
++) {
1294 struct atom_value
*v
= &val
[i
];
1301 * val is a list of atom_value to hold returned values. Extract
1302 * the values for atoms in used_atom array out of (obj, buf, sz).
1303 * when deref is false, (obj, buf, sz) is the object that is
1304 * pointed at by the ref itself; otherwise it is the object the
1305 * ref (which is a tag) refers to.
1307 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
)
1309 switch (obj
->type
) {
1311 grab_tag_values(val
, deref
, obj
);
1312 grab_sub_body_contents(val
, deref
, buf
);
1313 grab_person("tagger", val
, deref
, buf
);
1316 grab_commit_values(val
, deref
, obj
);
1317 grab_sub_body_contents(val
, deref
, buf
);
1318 grab_person("author", val
, deref
, buf
);
1319 grab_person("committer", val
, deref
, buf
);
1322 /* grab_tree_values(val, deref, obj, buf, sz); */
1325 /* grab_blob_values(val, deref, obj, buf, sz); */
1328 die("Eh? Object of type %d?", obj
->type
);
1332 static inline char *copy_advance(char *dst
, const char *src
)
1339 static const char *lstrip_ref_components(const char *refname
, int len
)
1341 long remaining
= len
;
1342 const char *start
= xstrdup(refname
);
1343 const char *to_free
= start
;
1347 const char *p
= refname
;
1349 /* Find total no of '/' separated path-components */
1350 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1353 * The number of components we need to strip is now
1354 * the total minus the components to be left (Plus one
1355 * because we count the number of '/', but the number
1356 * of components is one more than the no of '/').
1358 remaining
= i
+ len
+ 1;
1361 while (remaining
> 0) {
1364 free((char *)to_free
);
1372 start
= xstrdup(start
);
1373 free((char *)to_free
);
1377 static const char *rstrip_ref_components(const char *refname
, int len
)
1379 long remaining
= len
;
1380 const char *start
= xstrdup(refname
);
1381 const char *to_free
= start
;
1385 const char *p
= refname
;
1387 /* Find total no of '/' separated path-components */
1388 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1391 * The number of components we need to strip is now
1392 * the total minus the components to be left (Plus one
1393 * because we count the number of '/', but the number
1394 * of components is one more than the no of '/').
1396 remaining
= i
+ len
+ 1;
1399 while (remaining
-- > 0) {
1400 char *p
= strrchr(start
, '/');
1402 free((char *)to_free
);
1410 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1412 if (atom
->option
== R_SHORT
)
1413 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1414 else if (atom
->option
== R_LSTRIP
)
1415 return lstrip_ref_components(refname
, atom
->lstrip
);
1416 else if (atom
->option
== R_RSTRIP
)
1417 return rstrip_ref_components(refname
, atom
->rstrip
);
1419 return xstrdup(refname
);
1422 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1423 struct branch
*branch
, const char **s
)
1425 int num_ours
, num_theirs
;
1426 if (atom
->u
.remote_ref
.option
== RR_REF
)
1427 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1428 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1429 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1430 NULL
, atom
->u
.remote_ref
.push
,
1431 AHEAD_BEHIND_FULL
) < 0) {
1432 *s
= xstrdup(msgs
.gone
);
1433 } else if (!num_ours
&& !num_theirs
)
1436 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1437 else if (!num_theirs
)
1438 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1440 *s
= xstrfmt(msgs
.ahead_behind
,
1441 num_ours
, num_theirs
);
1442 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1443 const char *to_free
= *s
;
1444 *s
= xstrfmt("[%s]", *s
);
1445 free((void *)to_free
);
1447 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1448 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1449 NULL
, atom
->u
.remote_ref
.push
,
1450 AHEAD_BEHIND_FULL
) < 0) {
1454 if (!num_ours
&& !num_theirs
)
1458 else if (!num_theirs
)
1462 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1464 const char *remote
= atom
->u
.remote_ref
.push
?
1465 pushremote_for_branch(branch
, &explicit) :
1466 remote_for_branch(branch
, &explicit);
1467 *s
= xstrdup(explicit ? remote
: "");
1468 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1471 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
1472 *s
= xstrdup(merge
? merge
: "");
1474 BUG("unhandled RR_* enum");
1477 char *get_head_description(void)
1479 struct strbuf desc
= STRBUF_INIT
;
1480 struct wt_status_state state
;
1481 memset(&state
, 0, sizeof(state
));
1482 wt_status_get_state(the_repository
, &state
, 1);
1485 * The ( character must be hard-coded and not part of a localizable
1486 * string, since the description is used as a sort key and compared
1489 strbuf_addch(&desc
, '(');
1490 if (state
.rebase_in_progress
||
1491 state
.rebase_interactive_in_progress
) {
1493 strbuf_addf(&desc
, _("no branch, rebasing %s"),
1496 strbuf_addf(&desc
, _("no branch, rebasing detached HEAD %s"),
1497 state
.detached_from
);
1498 } else if (state
.bisect_in_progress
)
1499 strbuf_addf(&desc
, _("no branch, bisect started on %s"),
1501 else if (state
.detached_from
) {
1502 if (state
.detached_at
)
1503 strbuf_addstr(&desc
, HEAD_DETACHED_AT
);
1505 strbuf_addstr(&desc
, HEAD_DETACHED_FROM
);
1506 strbuf_addstr(&desc
, state
.detached_from
);
1509 strbuf_addstr(&desc
, _("no branch"));
1510 strbuf_addch(&desc
, ')');
1514 free(state
.detached_from
);
1515 return strbuf_detach(&desc
, NULL
);
1518 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1523 return show_ref(&atom
->u
.refname
, ref
->symref
);
1526 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1528 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1529 return get_head_description();
1530 return show_ref(&atom
->u
.refname
, ref
->refname
);
1533 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
1534 struct expand_data
*oi
, struct strbuf
*err
)
1536 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1538 if (oi
->info
.contentp
) {
1539 /* We need to know that to use parse_object_buffer properly */
1540 oi
->info
.sizep
= &oi
->size
;
1541 oi
->info
.typep
= &oi
->type
;
1543 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
1544 OBJECT_INFO_LOOKUP_REPLACE
))
1545 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1546 oid_to_hex(&oi
->oid
), ref
->refname
);
1547 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
1548 BUG("Object size is less than zero.");
1550 if (oi
->info
.contentp
) {
1551 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
1555 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1556 oid_to_hex(&oi
->oid
), ref
->refname
);
1558 grab_values(ref
->value
, deref
, *obj
, oi
->content
);
1561 grab_common_values(ref
->value
, deref
, oi
);
1567 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
1571 for (i
= 0; worktrees
[i
]; i
++) {
1572 if (worktrees
[i
]->head_ref
) {
1573 struct ref_to_worktree_entry
*entry
;
1574 entry
= xmalloc(sizeof(*entry
));
1575 entry
->wt
= worktrees
[i
];
1576 hashmap_entry_init(&entry
->ent
,
1577 strhash(worktrees
[i
]->head_ref
));
1579 hashmap_add(map
, &entry
->ent
);
1584 static void lazy_init_worktree_map(void)
1586 if (ref_to_worktree_map
.worktrees
)
1589 ref_to_worktree_map
.worktrees
= get_worktrees();
1590 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
1591 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
1594 static char *get_worktree_path(const struct used_atom
*atom
, const struct ref_array_item
*ref
)
1596 struct hashmap_entry entry
, *e
;
1597 struct ref_to_worktree_entry
*lookup_result
;
1599 lazy_init_worktree_map();
1601 hashmap_entry_init(&entry
, strhash(ref
->refname
));
1602 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
1607 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
1609 return xstrdup(lookup_result
->wt
->path
);
1613 * Parse the object referred by ref, and grab needed value.
1615 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
1619 struct object_info empty
= OBJECT_INFO_INIT
;
1621 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1623 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1624 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1627 ref
->symref
= xstrdup("");
1630 /* Fill in specials first */
1631 for (i
= 0; i
< used_atom_cnt
; i
++) {
1632 struct used_atom
*atom
= &used_atom
[i
];
1633 const char *name
= used_atom
[i
].name
;
1634 struct atom_value
*v
= &ref
->value
[i
];
1636 const char *refname
;
1637 struct branch
*branch
= NULL
;
1639 v
->handler
= append_atom
;
1647 if (starts_with(name
, "refname"))
1648 refname
= get_refname(atom
, ref
);
1649 else if (!strcmp(name
, "worktreepath")) {
1650 if (ref
->kind
== FILTER_REFS_BRANCHES
)
1651 v
->s
= get_worktree_path(atom
, ref
);
1656 else if (starts_with(name
, "symref"))
1657 refname
= get_symref(atom
, ref
);
1658 else if (starts_with(name
, "upstream")) {
1659 const char *branch_name
;
1660 /* only local branches may have an upstream */
1661 if (!skip_prefix(ref
->refname
, "refs/heads/",
1666 branch
= branch_get(branch_name
);
1668 refname
= branch_get_upstream(branch
, NULL
);
1670 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1674 } else if (atom
->u
.remote_ref
.push
) {
1675 const char *branch_name
;
1677 if (!skip_prefix(ref
->refname
, "refs/heads/",
1680 branch
= branch_get(branch_name
);
1682 if (atom
->u
.remote_ref
.push_remote
)
1685 refname
= branch_get_push(branch
, NULL
);
1689 /* We will definitely re-init v->s on the next line. */
1691 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1693 } else if (starts_with(name
, "color:")) {
1694 v
->s
= xstrdup(atom
->u
.color
);
1696 } else if (!strcmp(name
, "flag")) {
1697 char buf
[256], *cp
= buf
;
1698 if (ref
->flag
& REF_ISSYMREF
)
1699 cp
= copy_advance(cp
, ",symref");
1700 if (ref
->flag
& REF_ISPACKED
)
1701 cp
= copy_advance(cp
, ",packed");
1706 v
->s
= xstrdup(buf
+ 1);
1709 } else if (!deref
&& grab_objectname(name
, &ref
->objectname
, v
, atom
)) {
1711 } else if (!strcmp(name
, "HEAD")) {
1712 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1713 v
->s
= xstrdup("*");
1715 v
->s
= xstrdup(" ");
1717 } else if (starts_with(name
, "align")) {
1718 v
->handler
= align_atom_handler
;
1721 } else if (!strcmp(name
, "end")) {
1722 v
->handler
= end_atom_handler
;
1725 } else if (starts_with(name
, "if")) {
1727 if (skip_prefix(name
, "if:", &s
))
1731 v
->handler
= if_atom_handler
;
1733 } else if (!strcmp(name
, "then")) {
1734 v
->handler
= then_atom_handler
;
1737 } else if (!strcmp(name
, "else")) {
1738 v
->handler
= else_atom_handler
;
1745 v
->s
= xstrdup(refname
);
1747 v
->s
= xstrfmt("%s^{}", refname
);
1748 free((char *)refname
);
1751 for (i
= 0; i
< used_atom_cnt
; i
++) {
1752 struct atom_value
*v
= &ref
->value
[i
];
1753 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
1754 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1755 oid_to_hex(&ref
->objectname
), ref
->refname
);
1759 oi
.info
.contentp
= &oi
.content
;
1760 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
1761 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
1765 oi
.oid
= ref
->objectname
;
1766 if (get_object(ref
, 0, &obj
, &oi
, err
))
1770 * If there is no atom that wants to know about tagged
1771 * object, we are done.
1773 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1777 * If it is a tag object, see if we use a value that derefs
1778 * the object, and if we do grab the object it refers to.
1780 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
1783 * NEEDSWORK: This derefs tag only once, which
1784 * is good to deal with chains of trust, but
1785 * is not consistent with what deref_tag() does
1786 * which peels the onion to the core.
1788 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
1792 * Given a ref, return the value for the atom. This lazily gets value
1793 * out of the object by calling populate value.
1795 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
1796 struct atom_value
**v
, struct strbuf
*err
)
1799 if (populate_value(ref
, err
))
1801 fill_missing_values(ref
->value
);
1803 *v
= &ref
->value
[atom
];
1808 * Return 1 if the refname matches one of the patterns, otherwise 0.
1809 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1810 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1811 * matches "refs/heads/mas*", too).
1813 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1815 const char **patterns
= filter
->name_patterns
;
1818 if (filter
->ignore_case
)
1819 flags
|= WM_CASEFOLD
;
1822 * When no '--format' option is given we need to skip the prefix
1823 * for matching refs of tags and branches.
1825 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1826 skip_prefix(refname
, "refs/heads/", &refname
) ||
1827 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1828 skip_prefix(refname
, "refs/", &refname
));
1830 for (; *patterns
; patterns
++) {
1831 if (!wildmatch(*patterns
, refname
, flags
))
1838 * Return 1 if the refname matches one of the patterns, otherwise 0.
1839 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1840 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1841 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1843 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1845 const char **pattern
= filter
->name_patterns
;
1846 int namelen
= strlen(refname
);
1847 unsigned flags
= WM_PATHNAME
;
1849 if (filter
->ignore_case
)
1850 flags
|= WM_CASEFOLD
;
1852 for (; *pattern
; pattern
++) {
1853 const char *p
= *pattern
;
1854 int plen
= strlen(p
);
1856 if ((plen
<= namelen
) &&
1857 !strncmp(refname
, p
, plen
) &&
1858 (refname
[plen
] == '\0' ||
1859 refname
[plen
] == '/' ||
1862 if (!wildmatch(p
, refname
, flags
))
1868 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1869 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1871 if (!*filter
->name_patterns
)
1872 return 1; /* No pattern always matches */
1873 if (filter
->match_as_path
)
1874 return match_name_as_path(filter
, refname
);
1875 return match_pattern(filter
, refname
);
1878 static int qsort_strcmp(const void *va
, const void *vb
)
1880 const char *a
= *(const char **)va
;
1881 const char *b
= *(const char **)vb
;
1883 return strcmp(a
, b
);
1886 static void find_longest_prefixes_1(struct string_list
*out
,
1887 struct strbuf
*prefix
,
1888 const char **patterns
, size_t nr
)
1892 for (i
= 0; i
< nr
; i
++) {
1893 char c
= patterns
[i
][prefix
->len
];
1894 if (!c
|| is_glob_special(c
)) {
1895 string_list_append(out
, prefix
->buf
);
1905 * Set "end" to the index of the element _after_ the last one
1908 for (end
= i
+ 1; end
< nr
; end
++) {
1909 if (patterns
[i
][prefix
->len
] != patterns
[end
][prefix
->len
])
1913 strbuf_addch(prefix
, patterns
[i
][prefix
->len
]);
1914 find_longest_prefixes_1(out
, prefix
, patterns
+ i
, end
- i
);
1915 strbuf_setlen(prefix
, prefix
->len
- 1);
1921 static void find_longest_prefixes(struct string_list
*out
,
1922 const char **patterns
)
1924 struct strvec sorted
= STRVEC_INIT
;
1925 struct strbuf prefix
= STRBUF_INIT
;
1927 strvec_pushv(&sorted
, patterns
);
1928 QSORT(sorted
.v
, sorted
.nr
, qsort_strcmp
);
1930 find_longest_prefixes_1(out
, &prefix
, sorted
.v
, sorted
.nr
);
1932 strvec_clear(&sorted
);
1933 strbuf_release(&prefix
);
1937 * This is the same as for_each_fullref_in(), but it tries to iterate
1938 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1939 * pattern match, so the callback still has to match each ref individually.
1941 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1946 struct string_list prefixes
= STRING_LIST_INIT_DUP
;
1947 struct string_list_item
*prefix
;
1950 if (!filter
->match_as_path
) {
1952 * in this case, the patterns are applied after
1953 * prefixes like "refs/heads/" etc. are stripped off,
1954 * so we have to look at everything:
1956 return for_each_fullref_in("", cb
, cb_data
, broken
);
1959 if (filter
->ignore_case
) {
1961 * we can't handle case-insensitive comparisons,
1962 * so just return everything and let the caller
1965 return for_each_fullref_in("", cb
, cb_data
, broken
);
1968 if (!filter
->name_patterns
[0]) {
1969 /* no patterns; we have to look at everything */
1970 return for_each_fullref_in("", cb
, cb_data
, broken
);
1973 find_longest_prefixes(&prefixes
, filter
->name_patterns
);
1975 for_each_string_list_item(prefix
, &prefixes
) {
1976 ret
= for_each_fullref_in(prefix
->string
, cb
, cb_data
, broken
);
1981 string_list_clear(&prefixes
, 0);
1986 * Given a ref (oid, refname), check if the ref belongs to the array
1987 * of oids. If the given ref is a tag, check if the given tag points
1988 * at one of the oids in the given oid array.
1990 * 1. Only a single level of indirection is obtained, we might want to
1991 * change this to account for multiple levels (e.g. annotated tags
1992 * pointing to annotated tags pointing to a commit.)
1993 * 2. As the refs are cached we might know what refname peels to without
1994 * the need to parse the object via parse_object(). peel_ref() might be a
1995 * more efficient alternative to obtain the pointee.
1997 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1998 const struct object_id
*oid
,
1999 const char *refname
)
2001 const struct object_id
*tagged_oid
= NULL
;
2004 if (oid_array_lookup(points_at
, oid
) >= 0)
2006 obj
= parse_object(the_repository
, oid
);
2008 die(_("malformed object at '%s'"), refname
);
2009 if (obj
->type
== OBJ_TAG
)
2010 tagged_oid
= get_tagged_oid((struct tag
*)obj
);
2011 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
2017 * Allocate space for a new ref_array_item and copy the name and oid to it.
2019 * Callers can then fill in other struct members at their leisure.
2021 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2022 const struct object_id
*oid
)
2024 struct ref_array_item
*ref
;
2026 FLEX_ALLOC_STR(ref
, refname
, refname
);
2027 oidcpy(&ref
->objectname
, oid
);
2032 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2033 const char *refname
,
2034 const struct object_id
*oid
)
2036 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2038 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2039 array
->items
[array
->nr
++] = ref
;
2044 static int ref_kind_from_refname(const char *refname
)
2052 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2053 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2054 { "refs/tags/", FILTER_REFS_TAGS
}
2057 if (!strcmp(refname
, "HEAD"))
2058 return FILTER_REFS_DETACHED_HEAD
;
2060 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2061 if (starts_with(refname
, ref_kind
[i
].prefix
))
2062 return ref_kind
[i
].kind
;
2065 return FILTER_REFS_OTHERS
;
2068 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2070 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2071 filter
->kind
== FILTER_REFS_REMOTES
||
2072 filter
->kind
== FILTER_REFS_TAGS
)
2073 return filter
->kind
;
2074 return ref_kind_from_refname(refname
);
2077 struct ref_filter_cbdata
{
2078 struct ref_array
*array
;
2079 struct ref_filter
*filter
;
2080 struct contains_cache contains_cache
;
2081 struct contains_cache no_contains_cache
;
2085 * A call-back given to for_each_ref(). Filter refs and keep them for
2086 * later object processing.
2088 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2090 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2091 struct ref_filter
*filter
= ref_cbdata
->filter
;
2092 struct ref_array_item
*ref
;
2093 struct commit
*commit
= NULL
;
2096 if (flag
& REF_BAD_NAME
) {
2097 warning(_("ignoring ref with broken name %s"), refname
);
2101 if (flag
& REF_ISBROKEN
) {
2102 warning(_("ignoring broken ref %s"), refname
);
2106 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2107 kind
= filter_ref_kind(filter
, refname
);
2108 if (!(kind
& filter
->kind
))
2111 if (!filter_pattern_match(filter
, refname
))
2114 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2118 * A merge filter is applied on refs pointing to commits. Hence
2119 * obtain the commit using the 'oid' available and discard all
2120 * non-commits early. The actual filtering is done later.
2122 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2123 commit
= lookup_commit_reference_gently(the_repository
, oid
,
2127 /* We perform the filtering for the '--contains' option... */
2128 if (filter
->with_commit
&&
2129 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2131 /* ...or for the `--no-contains' option */
2132 if (filter
->no_commit
&&
2133 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2138 * We do not open the object yet; sort may only need refname
2139 * to do its job and the resulting list may yet to be pruned
2140 * by maxcount logic.
2142 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2143 ref
->commit
= commit
;
2150 /* Free memory allocated for a ref_array_item */
2151 static void free_array_item(struct ref_array_item
*item
)
2153 free((char *)item
->symref
);
2156 for (i
= 0; i
< used_atom_cnt
; i
++)
2157 free((char *)item
->value
[i
].s
);
2163 /* Free all memory allocated for ref_array */
2164 void ref_array_clear(struct ref_array
*array
)
2168 for (i
= 0; i
< array
->nr
; i
++)
2169 free_array_item(array
->items
[i
]);
2170 FREE_AND_NULL(array
->items
);
2171 array
->nr
= array
->alloc
= 0;
2173 for (i
= 0; i
< used_atom_cnt
; i
++)
2174 free((char *)used_atom
[i
].name
);
2175 FREE_AND_NULL(used_atom
);
2178 if (ref_to_worktree_map
.worktrees
) {
2179 hashmap_free_entries(&(ref_to_worktree_map
.map
),
2180 struct ref_to_worktree_entry
, ent
);
2181 free_worktrees(ref_to_worktree_map
.worktrees
);
2182 ref_to_worktree_map
.worktrees
= NULL
;
2186 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
2188 struct rev_info revs
;
2190 struct ref_filter
*filter
= ref_cbdata
->filter
;
2191 struct ref_array
*array
= ref_cbdata
->array
;
2192 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
2194 repo_init_revisions(the_repository
, &revs
, NULL
);
2196 for (i
= 0; i
< array
->nr
; i
++) {
2197 struct ref_array_item
*item
= array
->items
[i
];
2198 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
2199 to_clear
[i
] = item
->commit
;
2202 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
2203 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
2206 if (prepare_revision_walk(&revs
))
2207 die(_("revision walk setup failed"));
2212 for (i
= 0; i
< old_nr
; i
++) {
2213 struct ref_array_item
*item
= array
->items
[i
];
2214 struct commit
*commit
= item
->commit
;
2216 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2218 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
2219 array
->items
[array
->nr
++] = array
->items
[i
];
2221 free_array_item(item
);
2224 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2225 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
2230 * API for filtering a set of refs. Based on the type of refs the user
2231 * has requested, we iterate through those refs and apply filters
2232 * as per the given ref_filter structure and finally store the
2233 * filtered refs in the ref_array structure.
2235 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2237 struct ref_filter_cbdata ref_cbdata
;
2239 unsigned int broken
= 0;
2241 ref_cbdata
.array
= array
;
2242 ref_cbdata
.filter
= filter
;
2244 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
2246 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2248 init_contains_cache(&ref_cbdata
.contains_cache
);
2249 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2251 /* Simple per-ref filtering */
2253 die("filter_refs: invalid type");
2256 * For common cases where we need only branches or remotes or tags,
2257 * we only iterate through those refs. If a mix of refs is needed,
2258 * we iterate over all refs and filter out required refs with the help
2259 * of filter_ref_kind().
2261 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2262 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
2263 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2264 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2265 else if (filter
->kind
== FILTER_REFS_TAGS
)
2266 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2267 else if (filter
->kind
& FILTER_REFS_ALL
)
2268 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2269 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2270 head_ref(ref_filter_handler
, &ref_cbdata
);
2273 clear_contains_cache(&ref_cbdata
.contains_cache
);
2274 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2276 /* Filters that need revision walking */
2277 if (filter
->merge_commit
)
2278 do_merge_filter(&ref_cbdata
);
2283 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2285 struct atom_value
*va
, *vb
;
2287 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2288 int (*cmp_fn
)(const char *, const char *);
2289 struct strbuf err
= STRBUF_INIT
;
2291 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2293 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2295 strbuf_release(&err
);
2296 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2298 cmp
= versioncmp(va
->s
, vb
->s
);
2299 else if (cmp_type
== FIELD_STR
)
2300 cmp
= cmp_fn(va
->s
, vb
->s
);
2302 if (va
->value
< vb
->value
)
2304 else if (va
->value
== vb
->value
)
2310 return (s
->reverse
) ? -cmp
: cmp
;
2313 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2315 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2316 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2317 struct ref_sorting
*s
;
2319 for (s
= ref_sorting
; s
; s
= s
->next
) {
2320 int cmp
= cmp_ref_sorting(s
, a
, b
);
2325 return s
&& s
->ignore_case
?
2326 strcasecmp(a
->refname
, b
->refname
) :
2327 strcmp(a
->refname
, b
->refname
);
2330 void ref_sorting_icase_all(struct ref_sorting
*sorting
, int flag
)
2332 for (; sorting
; sorting
= sorting
->next
)
2333 sorting
->ignore_case
= !!flag
;
2336 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2338 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2341 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2343 struct strbuf
*s
= &state
->stack
->output
;
2345 while (*cp
&& (!ep
|| cp
< ep
)) {
2350 int ch
= hex2chr(cp
+ 1);
2352 strbuf_addch(s
, ch
);
2358 strbuf_addch(s
, *cp
);
2363 int format_ref_array_item(struct ref_array_item
*info
,
2364 const struct ref_format
*format
,
2365 struct strbuf
*final_buf
,
2366 struct strbuf
*error_buf
)
2368 const char *cp
, *sp
, *ep
;
2369 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2371 state
.quote_style
= format
->quote_style
;
2372 push_stack_element(&state
.stack
);
2374 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2375 struct atom_value
*atomv
;
2378 ep
= strchr(sp
, ')');
2380 append_literal(cp
, sp
, &state
);
2381 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2382 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2383 atomv
->handler(atomv
, &state
, error_buf
)) {
2384 pop_stack_element(&state
.stack
);
2389 sp
= cp
+ strlen(cp
);
2390 append_literal(cp
, sp
, &state
);
2392 if (format
->need_color_reset_at_eol
) {
2393 struct atom_value resetv
;
2394 resetv
.s
= GIT_COLOR_RESET
;
2395 if (append_atom(&resetv
, &state
, error_buf
)) {
2396 pop_stack_element(&state
.stack
);
2400 if (state
.stack
->prev
) {
2401 pop_stack_element(&state
.stack
);
2402 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2404 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2405 pop_stack_element(&state
.stack
);
2409 void show_ref_array_item(struct ref_array_item
*info
,
2410 const struct ref_format
*format
)
2412 struct strbuf final_buf
= STRBUF_INIT
;
2413 struct strbuf error_buf
= STRBUF_INIT
;
2415 if (format_ref_array_item(info
, format
, &final_buf
, &error_buf
))
2416 die("%s", error_buf
.buf
);
2417 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2418 strbuf_release(&error_buf
);
2419 strbuf_release(&final_buf
);
2423 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2424 const struct ref_format
*format
)
2426 struct ref_array_item
*ref_item
;
2427 ref_item
= new_ref_array_item(name
, oid
);
2428 ref_item
->kind
= ref_kind_from_refname(name
);
2429 show_ref_array_item(ref_item
, format
);
2430 free_array_item(ref_item
);
2433 static int parse_sorting_atom(const char *atom
)
2436 * This parses an atom using a dummy ref_format, since we don't
2437 * actually care about the formatting details.
2439 struct ref_format dummy
= REF_FORMAT_INIT
;
2440 const char *end
= atom
+ strlen(atom
);
2441 struct strbuf err
= STRBUF_INIT
;
2442 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2445 strbuf_release(&err
);
2449 /* If no sorting option is given, use refname to sort as default */
2450 struct ref_sorting
*ref_default_sorting(void)
2452 static const char cstr_name
[] = "refname";
2454 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2456 sorting
->next
= NULL
;
2457 sorting
->atom
= parse_sorting_atom(cstr_name
);
2461 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2463 struct ref_sorting
*s
;
2465 s
= xcalloc(1, sizeof(*s
));
2466 s
->next
= *sorting_tail
;
2473 if (skip_prefix(arg
, "version:", &arg
) ||
2474 skip_prefix(arg
, "v:", &arg
))
2476 s
->atom
= parse_sorting_atom(arg
);
2479 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2482 * NEEDSWORK: We should probably clear the list in this case, but we've
2483 * already munged the global used_atoms list, which would need to be
2486 BUG_ON_OPT_NEG(unset
);
2488 parse_ref_sorting(opt
->value
, arg
);
2492 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2494 struct ref_filter
*rf
= opt
->value
;
2495 struct object_id oid
;
2496 int no_merged
= starts_with(opt
->long_name
, "no");
2498 BUG_ON_OPT_NEG(unset
);
2502 return error(_("option `%s' is incompatible with --merged"),
2505 return error(_("option `%s' is incompatible with --no-merged"),
2510 rf
->merge
= no_merged
2511 ? REF_FILTER_MERGED_OMIT
2512 : REF_FILTER_MERGED_INCLUDE
;
2514 if (get_oid(arg
, &oid
))
2515 die(_("malformed object name %s"), arg
);
2517 rf
->merge_commit
= lookup_commit_reference_gently(the_repository
,
2519 if (!rf
->merge_commit
)
2520 return error(_("option `%s' must point to a commit"), opt
->long_name
);