1 #include "git-compat-util.h"
3 #include "parse-options.h"
6 #include "object-store.h"
7 #include "repository.h"
13 #include "ref-filter.h"
18 #include "wt-status.h"
19 #include "commit-slab.h"
20 #include "commit-graph.h"
21 #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 ref_trailer_buf
{
70 struct string_list filter_list
;
72 struct strbuf kvsepbuf
;
73 } ref_trailer_buf
= {STRING_LIST_INIT_NODUP
, STRBUF_INIT
, STRBUF_INIT
};
75 static struct expand_data
{
77 enum object_type type
;
80 struct object_id delta_base_oid
;
83 struct object_info info
;
86 struct ref_to_worktree_entry
{
87 struct hashmap_entry ent
;
88 struct worktree
*wt
; /* key is wt->head_ref */
91 static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED
,
92 const struct hashmap_entry
*eptr
,
93 const struct hashmap_entry
*kptr
,
94 const void *keydata_aka_refname
)
96 const struct ref_to_worktree_entry
*e
, *k
;
98 e
= container_of(eptr
, const struct ref_to_worktree_entry
, ent
);
99 k
= container_of(kptr
, const struct ref_to_worktree_entry
, ent
);
101 return strcmp(e
->wt
->head_ref
,
102 keydata_aka_refname
? keydata_aka_refname
: k
->wt
->head_ref
);
105 static struct ref_to_worktree_map
{
107 struct worktree
**worktrees
;
108 } ref_to_worktree_map
;
111 * The enum atom_type is used as the index of valid_atom array.
112 * In the atom parsing stage, it will be passed to used_atom.atom_type
113 * as the identifier of the atom type. We can check the type of used_atom
114 * entry by `if (used_atom[i].atom_type == ATOM_*)`.
163 * An atom is a valid field atom listed below, possibly prefixed with
164 * a "*" to denote deref_tag().
166 * We parse given format string and sort specifiers, and make a list
167 * of properties that we need to extract out of objects. ref_array_item
168 * structure will hold an array of values extracted that can be
169 * indexed with the "atom number", which is an index into this
172 static struct used_atom
{
173 enum atom_type atom_type
;
178 char color
[COLOR_MAXLEN
];
182 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
184 struct refname_atom refname
;
185 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
188 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LENGTH
, C_LINES
,
189 C_SIG
, C_SUB
, C_SUB_SANITIZE
, C_TRAILERS
} option
;
190 struct process_trailer_options trailer_opts
;
194 enum { RAW_BARE
, RAW_LENGTH
} option
;
197 cmp_status cmp_status
;
201 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
205 enum { O_SIZE
, O_SIZE_DISK
} option
;
207 struct email_option
{
208 enum { EO_RAW
, EO_TRIM
, EO_LOCALPART
} option
;
210 struct refname_atom refname
;
214 static int used_atom_cnt
, need_tagged
, need_symref
;
217 * Expand string, append it to strbuf *sb, then return error code ret.
218 * Allow to save few lines of code.
220 __attribute__((format (printf
, 3, 4)))
221 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
225 strbuf_vaddf(sb
, fmt
, ap
);
230 static int err_no_arg(struct strbuf
*sb
, const char *name
)
232 size_t namelen
= strchrnul(name
, ':') - name
;
233 strbuf_addf(sb
, _("%%(%.*s) does not take arguments"),
238 static int err_bad_arg(struct strbuf
*sb
, const char *name
, const char *arg
)
240 size_t namelen
= strchrnul(name
, ':') - name
;
241 strbuf_addf(sb
, _("unrecognized %%(%.*s) argument: %s"),
242 (int)namelen
, name
, arg
);
246 static int color_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
247 const char *color_value
, struct strbuf
*err
)
250 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
251 if (color_parse(color_value
, atom
->u
.color
) < 0)
252 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
255 * We check this after we've parsed the color, which lets us complain
256 * about syntactically bogus color names even if they won't be used.
258 if (!want_color(format
->use_color
))
259 color_parse("", atom
->u
.color
);
263 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
264 const char *name
, struct strbuf
*err
)
267 atom
->option
= R_NORMAL
;
268 else if (!strcmp(arg
, "short"))
269 atom
->option
= R_SHORT
;
270 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
271 skip_prefix(arg
, "strip=", &arg
)) {
272 atom
->option
= R_LSTRIP
;
273 if (strtol_i(arg
, 10, &atom
->lstrip
))
274 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
275 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
276 atom
->option
= R_RSTRIP
;
277 if (strtol_i(arg
, 10, &atom
->rstrip
))
278 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
280 return err_bad_arg(err
, name
, arg
);
284 static int remote_ref_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
285 const char *arg
, struct strbuf
*err
)
287 struct string_list params
= STRING_LIST_INIT_DUP
;
290 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
291 atom
->u
.remote_ref
.push
= 1;
294 atom
->u
.remote_ref
.option
= RR_REF
;
295 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
296 arg
, atom
->name
, err
);
299 atom
->u
.remote_ref
.nobracket
= 0;
300 string_list_split(¶ms
, arg
, ',', -1);
302 for (i
= 0; i
< params
.nr
; i
++) {
303 const char *s
= params
.items
[i
].string
;
305 if (!strcmp(s
, "track"))
306 atom
->u
.remote_ref
.option
= RR_TRACK
;
307 else if (!strcmp(s
, "trackshort"))
308 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
309 else if (!strcmp(s
, "nobracket"))
310 atom
->u
.remote_ref
.nobracket
= 1;
311 else if (!strcmp(s
, "remotename")) {
312 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
313 atom
->u
.remote_ref
.push_remote
= 1;
314 } else if (!strcmp(s
, "remoteref")) {
315 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
316 atom
->u
.remote_ref
.push_remote
= 1;
318 atom
->u
.remote_ref
.option
= RR_REF
;
319 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
320 arg
, atom
->name
, err
)) {
321 string_list_clear(¶ms
, 0);
327 string_list_clear(¶ms
, 0);
331 static int objecttype_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
332 const char *arg
, struct strbuf
*err
)
335 return err_no_arg(err
, "objecttype");
336 if (*atom
->name
== '*')
337 oi_deref
.info
.typep
= &oi_deref
.type
;
339 oi
.info
.typep
= &oi
.type
;
343 static int objectsize_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
344 const char *arg
, struct strbuf
*err
)
347 atom
->u
.objectsize
.option
= O_SIZE
;
348 if (*atom
->name
== '*')
349 oi_deref
.info
.sizep
= &oi_deref
.size
;
351 oi
.info
.sizep
= &oi
.size
;
352 } else if (!strcmp(arg
, "disk")) {
353 atom
->u
.objectsize
.option
= O_SIZE_DISK
;
354 if (*atom
->name
== '*')
355 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
357 oi
.info
.disk_sizep
= &oi
.disk_size
;
359 return err_bad_arg(err
, "objectsize", arg
);
363 static int deltabase_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
364 const char *arg
, struct strbuf
*err
)
367 return err_no_arg(err
, "deltabase");
368 if (*atom
->name
== '*')
369 oi_deref
.info
.delta_base_oid
= &oi_deref
.delta_base_oid
;
371 oi
.info
.delta_base_oid
= &oi
.delta_base_oid
;
375 static int body_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
376 const char *arg
, struct strbuf
*err
)
379 return err_no_arg(err
, "body");
380 atom
->u
.contents
.option
= C_BODY_DEP
;
384 static int subject_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
385 const char *arg
, struct strbuf
*err
)
388 atom
->u
.contents
.option
= C_SUB
;
389 else if (!strcmp(arg
, "sanitize"))
390 atom
->u
.contents
.option
= C_SUB_SANITIZE
;
392 return err_bad_arg(err
, "subject", arg
);
396 static int trailers_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
397 const char *arg
, struct strbuf
*err
)
399 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
402 const char *argbuf
= xstrfmt("%s)", arg
);
403 char *invalid_arg
= NULL
;
405 if (format_set_trailers_options(&atom
->u
.contents
.trailer_opts
,
406 &ref_trailer_buf
.filter_list
,
407 &ref_trailer_buf
.sepbuf
,
408 &ref_trailer_buf
.kvsepbuf
,
409 &argbuf
, &invalid_arg
)) {
411 strbuf_addf(err
, _("expected %%(trailers:key=<value>)"));
413 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), invalid_arg
);
414 free((char *)invalid_arg
);
418 atom
->u
.contents
.option
= C_TRAILERS
;
422 static int contents_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
423 const char *arg
, struct strbuf
*err
)
426 atom
->u
.contents
.option
= C_BARE
;
427 else if (!strcmp(arg
, "body"))
428 atom
->u
.contents
.option
= C_BODY
;
429 else if (!strcmp(arg
, "size"))
430 atom
->u
.contents
.option
= C_LENGTH
;
431 else if (!strcmp(arg
, "signature"))
432 atom
->u
.contents
.option
= C_SIG
;
433 else if (!strcmp(arg
, "subject"))
434 atom
->u
.contents
.option
= C_SUB
;
435 else if (!strcmp(arg
, "trailers")) {
436 if (trailers_atom_parser(format
, atom
, NULL
, err
))
438 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
439 if (trailers_atom_parser(format
, atom
, arg
, err
))
441 } else if (skip_prefix(arg
, "lines=", &arg
)) {
442 atom
->u
.contents
.option
= C_LINES
;
443 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
444 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
446 return err_bad_arg(err
, "contents", arg
);
450 static int raw_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
451 const char *arg
, struct strbuf
*err
)
454 atom
->u
.raw_data
.option
= RAW_BARE
;
455 else if (!strcmp(arg
, "size"))
456 atom
->u
.raw_data
.option
= RAW_LENGTH
;
458 return err_bad_arg(err
, "raw", arg
);
462 static int oid_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
463 const char *arg
, struct strbuf
*err
)
466 atom
->u
.oid
.option
= O_FULL
;
467 else if (!strcmp(arg
, "short"))
468 atom
->u
.oid
.option
= O_SHORT
;
469 else if (skip_prefix(arg
, "short=", &arg
)) {
470 atom
->u
.oid
.option
= O_LENGTH
;
471 if (strtoul_ui(arg
, 10, &atom
->u
.oid
.length
) ||
472 atom
->u
.oid
.length
== 0)
473 return strbuf_addf_ret(err
, -1, _("positive value expected '%s' in %%(%s)"), arg
, atom
->name
);
474 if (atom
->u
.oid
.length
< MINIMUM_ABBREV
)
475 atom
->u
.oid
.length
= MINIMUM_ABBREV
;
477 return err_bad_arg(err
, atom
->name
, arg
);
481 static int person_email_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
482 const char *arg
, struct strbuf
*err
)
485 atom
->u
.email_option
.option
= EO_RAW
;
486 else if (!strcmp(arg
, "trim"))
487 atom
->u
.email_option
.option
= EO_TRIM
;
488 else if (!strcmp(arg
, "localpart"))
489 atom
->u
.email_option
.option
= EO_LOCALPART
;
491 return err_bad_arg(err
, atom
->name
, arg
);
495 static int refname_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
496 const char *arg
, struct strbuf
*err
)
498 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
501 static align_type
parse_align_position(const char *s
)
503 if (!strcmp(s
, "right"))
505 else if (!strcmp(s
, "middle"))
507 else if (!strcmp(s
, "left"))
512 static int align_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
513 const char *arg
, struct strbuf
*err
)
515 struct align
*align
= &atom
->u
.align
;
516 struct string_list params
= STRING_LIST_INIT_DUP
;
518 unsigned int width
= ~0U;
521 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
523 align
->position
= ALIGN_LEFT
;
525 string_list_split(¶ms
, arg
, ',', -1);
526 for (i
= 0; i
< params
.nr
; i
++) {
527 const char *s
= params
.items
[i
].string
;
530 if (skip_prefix(s
, "position=", &s
)) {
531 position
= parse_align_position(s
);
533 strbuf_addf(err
, _("unrecognized position:%s"), s
);
534 string_list_clear(¶ms
, 0);
537 align
->position
= position
;
538 } else if (skip_prefix(s
, "width=", &s
)) {
539 if (strtoul_ui(s
, 10, &width
)) {
540 strbuf_addf(err
, _("unrecognized width:%s"), s
);
541 string_list_clear(¶ms
, 0);
544 } else if (!strtoul_ui(s
, 10, &width
))
546 else if ((position
= parse_align_position(s
)) >= 0)
547 align
->position
= position
;
549 strbuf_addf(err
, _("unrecognized %%(%s) argument: %s"), "align", s
);
550 string_list_clear(¶ms
, 0);
556 string_list_clear(¶ms
, 0);
557 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
559 align
->width
= width
;
560 string_list_clear(¶ms
, 0);
564 static int if_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
565 const char *arg
, struct strbuf
*err
)
568 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
570 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
571 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
572 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
573 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
575 return err_bad_arg(err
, "if", arg
);
579 static int rest_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
580 const char *arg
, struct strbuf
*err
)
583 return err_no_arg(err
, "rest");
584 format
->use_rest
= 1;
588 static int head_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
589 const char *arg
, struct strbuf
*err
)
592 return err_no_arg(err
, "HEAD");
593 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
601 int (*parser
)(struct ref_format
*format
, struct used_atom
*atom
,
602 const char *arg
, struct strbuf
*err
);
604 [ATOM_REFNAME
] = { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
605 [ATOM_OBJECTTYPE
] = { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
606 [ATOM_OBJECTSIZE
] = { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
607 [ATOM_OBJECTNAME
] = { "objectname", SOURCE_OTHER
, FIELD_STR
, oid_atom_parser
},
608 [ATOM_DELTABASE
] = { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
609 [ATOM_TREE
] = { "tree", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
610 [ATOM_PARENT
] = { "parent", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
611 [ATOM_NUMPARENT
] = { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
612 [ATOM_OBJECT
] = { "object", SOURCE_OBJ
},
613 [ATOM_TYPE
] = { "type", SOURCE_OBJ
},
614 [ATOM_TAG
] = { "tag", SOURCE_OBJ
},
615 [ATOM_AUTHOR
] = { "author", SOURCE_OBJ
},
616 [ATOM_AUTHORNAME
] = { "authorname", SOURCE_OBJ
},
617 [ATOM_AUTHOREMAIL
] = { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
618 [ATOM_AUTHORDATE
] = { "authordate", SOURCE_OBJ
, FIELD_TIME
},
619 [ATOM_COMMITTER
] = { "committer", SOURCE_OBJ
},
620 [ATOM_COMMITTERNAME
] = { "committername", SOURCE_OBJ
},
621 [ATOM_COMMITTEREMAIL
] = { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
622 [ATOM_COMMITTERDATE
] = { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
623 [ATOM_TAGGER
] = { "tagger", SOURCE_OBJ
},
624 [ATOM_TAGGERNAME
] = { "taggername", SOURCE_OBJ
},
625 [ATOM_TAGGEREMAIL
] = { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
626 [ATOM_TAGGERDATE
] = { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
627 [ATOM_CREATOR
] = { "creator", SOURCE_OBJ
},
628 [ATOM_CREATORDATE
] = { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
629 [ATOM_SUBJECT
] = { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
630 [ATOM_BODY
] = { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
631 [ATOM_TRAILERS
] = { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
632 [ATOM_CONTENTS
] = { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
633 [ATOM_RAW
] = { "raw", SOURCE_OBJ
, FIELD_STR
, raw_atom_parser
},
634 [ATOM_UPSTREAM
] = { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
635 [ATOM_PUSH
] = { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
636 [ATOM_SYMREF
] = { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
637 [ATOM_FLAG
] = { "flag", SOURCE_NONE
},
638 [ATOM_HEAD
] = { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
639 [ATOM_COLOR
] = { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
640 [ATOM_WORKTREEPATH
] = { "worktreepath", SOURCE_NONE
},
641 [ATOM_ALIGN
] = { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
642 [ATOM_END
] = { "end", SOURCE_NONE
},
643 [ATOM_IF
] = { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
644 [ATOM_THEN
] = { "then", SOURCE_NONE
},
645 [ATOM_ELSE
] = { "else", SOURCE_NONE
},
646 [ATOM_REST
] = { "rest", SOURCE_NONE
, FIELD_STR
, rest_atom_parser
},
648 * Please update $__git_ref_fieldlist in git-completion.bash
649 * when you add new atoms
653 #define REF_FORMATTING_STATE_INIT { 0 }
655 struct ref_formatting_stack
{
656 struct ref_formatting_stack
*prev
;
657 struct strbuf output
;
658 void (*at_end
)(struct ref_formatting_stack
**stack
);
662 struct ref_formatting_state
{
664 struct ref_formatting_stack
*stack
;
670 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
672 uintmax_t value
; /* used for sorting when not FIELD_STR */
673 struct used_atom
*atom
;
676 #define ATOM_SIZE_UNSPECIFIED (-1)
678 #define ATOM_VALUE_INIT { \
679 .s_size = ATOM_SIZE_UNSPECIFIED \
683 * Used to parse format string and sort specifiers
685 static int parse_ref_filter_atom(struct ref_format
*format
,
686 const char *atom
, const char *ep
,
694 if (*sp
== '*' && sp
< ep
)
697 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
698 (int)(ep
-atom
), atom
);
701 * If the atom name has a colon, strip it and everything after
702 * it off - it specifies the format for this entry, and
703 * shouldn't be used for checking against the valid_atom
706 arg
= memchr(sp
, ':', ep
- sp
);
707 atom_len
= (arg
? arg
: ep
) - sp
;
709 /* Do we have the atom already used elsewhere? */
710 for (i
= 0; i
< used_atom_cnt
; i
++) {
711 int len
= strlen(used_atom
[i
].name
);
712 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
716 /* Is the atom a valid one? */
717 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
718 int len
= strlen(valid_atom
[i
].name
);
719 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
723 if (ARRAY_SIZE(valid_atom
) <= i
)
724 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
725 (int)(ep
-atom
), atom
);
726 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
727 return strbuf_addf_ret(err
, -1,
728 _("not a git repository, but the field '%.*s' requires access to object data"),
729 (int)(ep
-atom
), atom
);
731 /* Add it in, including the deref prefix */
734 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
735 used_atom
[at
].atom_type
= i
;
736 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
737 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
738 used_atom
[at
].source
= valid_atom
[i
].source
;
739 if (used_atom
[at
].source
== SOURCE_OBJ
) {
741 oi_deref
.info
.contentp
= &oi_deref
.content
;
743 oi
.info
.contentp
= &oi
.content
;
746 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
749 * Treat empty sub-arguments list as NULL (i.e.,
750 * "%(atom:)" is equivalent to "%(atom)").
755 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
756 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
760 if (i
== ATOM_SYMREF
)
765 static void quote_formatting(struct strbuf
*s
, const char *str
, ssize_t len
, int quote_style
)
767 switch (quote_style
) {
770 strbuf_addstr(s
, str
);
772 strbuf_add(s
, str
, len
);
775 sq_quote_buf(s
, str
);
779 perl_quote_buf(s
, str
);
781 perl_quote_buf_with_len(s
, str
, len
);
784 python_quote_buf(s
, str
);
787 tcl_quote_buf(s
, str
);
792 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
793 struct strbuf
*unused_err
)
796 * Quote formatting is only done when the stack has a single
797 * element. Otherwise quote formatting is done on the
798 * element's entire output strbuf when the %(end) atom is
801 if (!state
->stack
->prev
)
802 quote_formatting(&state
->stack
->output
, v
->s
, v
->s_size
, state
->quote_style
);
803 else if (v
->s_size
< 0)
804 strbuf_addstr(&state
->stack
->output
, v
->s
);
806 strbuf_add(&state
->stack
->output
, v
->s
, v
->s_size
);
810 static void push_stack_element(struct ref_formatting_stack
**stack
)
812 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
814 strbuf_init(&s
->output
, 0);
819 static void pop_stack_element(struct ref_formatting_stack
**stack
)
821 struct ref_formatting_stack
*current
= *stack
;
822 struct ref_formatting_stack
*prev
= current
->prev
;
825 strbuf_addbuf(&prev
->output
, ¤t
->output
);
826 strbuf_release(¤t
->output
);
831 static void end_align_handler(struct ref_formatting_stack
**stack
)
833 struct ref_formatting_stack
*cur
= *stack
;
834 struct align
*align
= (struct align
*)cur
->at_end_data
;
835 struct strbuf s
= STRBUF_INIT
;
837 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
838 strbuf_swap(&cur
->output
, &s
);
842 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
843 struct strbuf
*unused_err
)
845 struct ref_formatting_stack
*new_stack
;
847 push_stack_element(&state
->stack
);
848 new_stack
= state
->stack
;
849 new_stack
->at_end
= end_align_handler
;
850 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
854 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
856 struct ref_formatting_stack
*cur
= *stack
;
857 struct ref_formatting_stack
*prev
= cur
->prev
;
858 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
860 if (!if_then_else
->then_atom_seen
)
861 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
863 if (if_then_else
->else_atom_seen
) {
865 * There is an %(else) atom: we need to drop one state from the
866 * stack, either the %(else) branch if the condition is satisfied, or
867 * the %(then) branch if it isn't.
869 if (if_then_else
->condition_satisfied
) {
870 strbuf_reset(&cur
->output
);
871 pop_stack_element(&cur
);
873 strbuf_swap(&cur
->output
, &prev
->output
);
874 strbuf_reset(&cur
->output
);
875 pop_stack_element(&cur
);
877 } else if (!if_then_else
->condition_satisfied
) {
879 * No %(else) atom: just drop the %(then) branch if the
880 * condition is not satisfied.
882 strbuf_reset(&cur
->output
);
889 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
890 struct strbuf
*unused_err
)
892 struct ref_formatting_stack
*new_stack
;
893 struct if_then_else
*if_then_else
= xcalloc(1,
894 sizeof(struct if_then_else
));
896 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
897 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
899 push_stack_element(&state
->stack
);
900 new_stack
= state
->stack
;
901 new_stack
->at_end
= if_then_else_handler
;
902 new_stack
->at_end_data
= if_then_else
;
906 static int is_empty(struct strbuf
*buf
)
908 const char *cur
= buf
->buf
;
909 const char *end
= buf
->buf
+ buf
->len
;
911 while (cur
!= end
&& (isspace(*cur
)))
917 static int then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
920 struct ref_formatting_stack
*cur
= state
->stack
;
921 struct if_then_else
*if_then_else
= NULL
;
924 if (cur
->at_end
== if_then_else_handler
)
925 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
927 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
928 if (if_then_else
->then_atom_seen
)
929 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
930 if (if_then_else
->else_atom_seen
)
931 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
932 if_then_else
->then_atom_seen
= 1;
933 if (if_then_else
->str
)
934 str_len
= strlen(if_then_else
->str
);
936 * If the 'equals' or 'notequals' attribute is used then
937 * perform the required comparison. If not, only non-empty
938 * strings satisfy the 'if' condition.
940 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
941 if (str_len
== cur
->output
.len
&&
942 !memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
943 if_then_else
->condition_satisfied
= 1;
944 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
945 if (str_len
!= cur
->output
.len
||
946 memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
947 if_then_else
->condition_satisfied
= 1;
948 } else if (cur
->output
.len
&& !is_empty(&cur
->output
))
949 if_then_else
->condition_satisfied
= 1;
950 strbuf_reset(&cur
->output
);
954 static int else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
957 struct ref_formatting_stack
*prev
= state
->stack
;
958 struct if_then_else
*if_then_else
= NULL
;
960 if (prev
->at_end
== if_then_else_handler
)
961 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
963 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
964 if (!if_then_else
->then_atom_seen
)
965 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
966 if (if_then_else
->else_atom_seen
)
967 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
968 if_then_else
->else_atom_seen
= 1;
969 push_stack_element(&state
->stack
);
970 state
->stack
->at_end_data
= prev
->at_end_data
;
971 state
->stack
->at_end
= prev
->at_end
;
975 static int end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
978 struct ref_formatting_stack
*current
= state
->stack
;
979 struct strbuf s
= STRBUF_INIT
;
981 if (!current
->at_end
)
982 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
983 current
->at_end(&state
->stack
);
985 /* Stack may have been popped within at_end(), hence reset the current pointer */
986 current
= state
->stack
;
989 * Perform quote formatting when the stack element is that of
990 * a supporting atom. If nested then perform quote formatting
991 * only on the topmost supporting atom.
993 if (!current
->prev
->prev
) {
994 quote_formatting(&s
, current
->output
.buf
, current
->output
.len
, state
->quote_style
);
995 strbuf_swap(¤t
->output
, &s
);
998 pop_stack_element(&state
->stack
);
1003 * In a format string, find the next occurrence of %(atom).
1005 static const char *find_next(const char *cp
)
1010 * %( is the start of an atom;
1011 * %% is a quoted per-cent.
1015 else if (cp
[1] == '%')
1016 cp
++; /* skip over two % */
1017 /* otherwise this is a singleton, literal % */
1024 static int reject_atom(enum atom_type atom_type
)
1026 return atom_type
== ATOM_REST
;
1030 * Make sure the format string is well formed, and parse out
1033 int verify_ref_format(struct ref_format
*format
)
1035 const char *cp
, *sp
;
1037 format
->need_color_reset_at_eol
= 0;
1038 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
1039 struct strbuf err
= STRBUF_INIT
;
1040 const char *color
, *ep
= strchr(sp
, ')');
1044 return error(_("malformed format string %s"), sp
);
1045 /* sp points at "%(" and ep points at the closing ")" */
1046 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
1049 if (reject_atom(used_atom
[at
].atom_type
))
1050 die(_("this command reject atom %%(%.*s)"), (int)(ep
- sp
- 2), sp
+ 2);
1052 if ((format
->quote_style
== QUOTE_PYTHON
||
1053 format
->quote_style
== QUOTE_SHELL
||
1054 format
->quote_style
== QUOTE_TCL
) &&
1055 used_atom
[at
].atom_type
== ATOM_RAW
&&
1056 used_atom
[at
].u
.raw_data
.option
== RAW_BARE
)
1057 die(_("--format=%.*s cannot be used with "
1058 "--python, --shell, --tcl"), (int)(ep
- sp
- 2), sp
+ 2);
1061 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
1062 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
1063 strbuf_release(&err
);
1065 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
1066 format
->need_color_reset_at_eol
= 0;
1070 static const char *do_grab_oid(const char *field
, const struct object_id
*oid
,
1071 struct used_atom
*atom
)
1073 switch (atom
->u
.oid
.option
) {
1075 return oid_to_hex(oid
);
1077 return find_unique_abbrev(oid
, atom
->u
.oid
.length
);
1079 return find_unique_abbrev(oid
, DEFAULT_ABBREV
);
1081 BUG("unknown %%(%s) option", field
);
1085 static int grab_oid(const char *name
, const char *field
, const struct object_id
*oid
,
1086 struct atom_value
*v
, struct used_atom
*atom
)
1088 if (starts_with(name
, field
)) {
1089 v
->s
= xstrdup(do_grab_oid(field
, oid
, atom
));
1095 /* See grab_values */
1096 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
1100 for (i
= 0; i
< used_atom_cnt
; i
++) {
1101 const char *name
= used_atom
[i
].name
;
1102 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1103 struct atom_value
*v
= &val
[i
];
1104 if (!!deref
!= (*name
== '*'))
1108 if (atom_type
== ATOM_OBJECTTYPE
)
1109 v
->s
= xstrdup(type_name(oi
->type
));
1110 else if (atom_type
== ATOM_OBJECTSIZE
) {
1111 if (used_atom
[i
].u
.objectsize
.option
== O_SIZE_DISK
) {
1112 v
->value
= oi
->disk_size
;
1113 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
1114 } else if (used_atom
[i
].u
.objectsize
.option
== O_SIZE
) {
1115 v
->value
= oi
->size
;
1116 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
1118 } else if (atom_type
== ATOM_DELTABASE
)
1119 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
1120 else if (atom_type
== ATOM_OBJECTNAME
&& deref
)
1121 grab_oid(name
, "objectname", &oi
->oid
, v
, &used_atom
[i
]);
1125 /* See grab_values */
1126 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1129 struct tag
*tag
= (struct tag
*) obj
;
1131 for (i
= 0; i
< used_atom_cnt
; i
++) {
1132 const char *name
= used_atom
[i
].name
;
1133 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1134 struct atom_value
*v
= &val
[i
];
1135 if (!!deref
!= (*name
== '*'))
1139 if (atom_type
== ATOM_TAG
)
1140 v
->s
= xstrdup(tag
->tag
);
1141 else if (atom_type
== ATOM_TYPE
&& tag
->tagged
)
1142 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
1143 else if (atom_type
== ATOM_OBJECT
&& tag
->tagged
)
1144 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
1148 /* See grab_values */
1149 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1152 struct commit
*commit
= (struct commit
*) obj
;
1154 for (i
= 0; i
< used_atom_cnt
; i
++) {
1155 const char *name
= used_atom
[i
].name
;
1156 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1157 struct atom_value
*v
= &val
[i
];
1158 if (!!deref
!= (*name
== '*'))
1162 if (atom_type
== ATOM_TREE
&&
1163 grab_oid(name
, "tree", get_commit_tree_oid(commit
), v
, &used_atom
[i
]))
1165 if (atom_type
== ATOM_NUMPARENT
) {
1166 v
->value
= commit_list_count(commit
->parents
);
1167 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1169 else if (atom_type
== ATOM_PARENT
) {
1170 struct commit_list
*parents
;
1171 struct strbuf s
= STRBUF_INIT
;
1172 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1173 struct object_id
*oid
= &parents
->item
->object
.oid
;
1174 if (parents
!= commit
->parents
)
1175 strbuf_addch(&s
, ' ');
1176 strbuf_addstr(&s
, do_grab_oid("parent", oid
, &used_atom
[i
]));
1178 v
->s
= strbuf_detach(&s
, NULL
);
1183 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1187 if (!strncmp(buf
, who
, wholen
) &&
1189 return buf
+ wholen
+ 1;
1190 eol
= strchr(buf
, '\n');
1195 return ""; /* end of header */
1201 static const char *copy_line(const char *buf
)
1203 const char *eol
= strchrnul(buf
, '\n');
1204 return xmemdupz(buf
, eol
- buf
);
1207 static const char *copy_name(const char *buf
)
1210 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1211 if (starts_with(cp
, " <"))
1212 return xmemdupz(buf
, cp
- buf
);
1217 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1219 const char *email
= strchr(buf
, '<');
1220 const char *eoemail
;
1223 switch (atom
->u
.email_option
.option
) {
1225 eoemail
= strchr(email
, '>');
1231 eoemail
= strchr(email
, '>');
1235 eoemail
= strchr(email
, '@');
1237 eoemail
= strchr(email
, '>');
1240 BUG("unknown email option");
1245 return xmemdupz(email
, eoemail
- email
);
1248 static char *copy_subject(const char *buf
, unsigned long len
)
1250 struct strbuf sb
= STRBUF_INIT
;
1253 for (i
= 0; i
< len
; i
++) {
1254 if (buf
[i
] == '\r' && i
+ 1 < len
&& buf
[i
+ 1] == '\n')
1255 continue; /* ignore CR in CRLF */
1258 strbuf_addch(&sb
, ' ');
1260 strbuf_addch(&sb
, buf
[i
]);
1262 return strbuf_detach(&sb
, NULL
);
1265 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1267 const char *eoemail
= strstr(buf
, "> ");
1269 timestamp_t timestamp
;
1271 struct date_mode date_mode
= DATE_MODE_INIT
;
1272 const char *formatp
;
1275 * We got here because atomname ends in "date" or "date<something>";
1276 * it's not possible that <something> is not ":<format>" because
1277 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1278 * ":" means no format is specified, and use the default.
1280 formatp
= strchr(atomname
, ':');
1283 parse_date_format(formatp
, &date_mode
);
1288 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1289 if (timestamp
== TIME_MAX
)
1291 tz
= strtol(zone
, NULL
, 10);
1292 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1294 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1295 v
->value
= timestamp
;
1296 date_mode_release(&date_mode
);
1303 /* See grab_values */
1304 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1307 int wholen
= strlen(who
);
1308 const char *wholine
= NULL
;
1310 for (i
= 0; i
< used_atom_cnt
; i
++) {
1311 const char *name
= used_atom
[i
].name
;
1312 struct atom_value
*v
= &val
[i
];
1313 if (!!deref
!= (*name
== '*'))
1317 if (strncmp(who
, name
, wholen
))
1319 if (name
[wholen
] != 0 &&
1320 strcmp(name
+ wholen
, "name") &&
1321 !starts_with(name
+ wholen
, "email") &&
1322 !starts_with(name
+ wholen
, "date"))
1325 wholine
= find_wholine(who
, wholen
, buf
);
1327 return; /* no point looking for it */
1328 if (name
[wholen
] == 0)
1329 v
->s
= copy_line(wholine
);
1330 else if (!strcmp(name
+ wholen
, "name"))
1331 v
->s
= copy_name(wholine
);
1332 else if (starts_with(name
+ wholen
, "email"))
1333 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1334 else if (starts_with(name
+ wholen
, "date"))
1335 grab_date(wholine
, v
, name
);
1339 * For a tag or a commit object, if "creator" or "creatordate" is
1340 * requested, do something special.
1342 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1343 return; /* "author" for commit object is not wanted */
1345 wholine
= find_wholine(who
, wholen
, buf
);
1348 for (i
= 0; i
< used_atom_cnt
; i
++) {
1349 const char *name
= used_atom
[i
].name
;
1350 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1351 struct atom_value
*v
= &val
[i
];
1352 if (!!deref
!= (*name
== '*'))
1357 if (atom_type
== ATOM_CREATORDATE
)
1358 grab_date(wholine
, v
, name
);
1359 else if (atom_type
== ATOM_CREATOR
)
1360 v
->s
= copy_line(wholine
);
1364 static void find_subpos(const char *buf
,
1365 const char **sub
, size_t *sublen
,
1366 const char **body
, size_t *bodylen
,
1368 const char **sig
, size_t *siglen
)
1370 struct strbuf payload
= STRBUF_INIT
;
1371 struct strbuf signature
= STRBUF_INIT
;
1373 const char *end
= buf
+ strlen(buf
);
1374 const char *sigstart
;
1376 /* parse signature first; we might not even have a subject line */
1377 parse_signature(buf
, end
- buf
, &payload
, &signature
);
1378 strbuf_release(&payload
);
1380 /* skip past header until we hit empty line */
1381 while (*buf
&& *buf
!= '\n') {
1382 eol
= strchrnul(buf
, '\n');
1387 /* skip any empty lines */
1388 while (*buf
== '\n')
1390 *sig
= strbuf_detach(&signature
, siglen
);
1391 sigstart
= buf
+ parse_signed_buffer(buf
, strlen(buf
));
1393 /* subject is first non-empty line */
1395 /* subject goes to first empty line before signature begins */
1396 if ((eol
= strstr(*sub
, "\n\n")) ||
1397 (eol
= strstr(*sub
, "\r\n\r\n"))) {
1398 eol
= eol
< sigstart
? eol
: sigstart
;
1400 /* treat whole message as subject */
1404 *sublen
= buf
- *sub
;
1405 /* drop trailing newline, if present */
1406 while (*sublen
&& ((*sub
)[*sublen
- 1] == '\n' ||
1407 (*sub
)[*sublen
- 1] == '\r'))
1410 /* skip any empty lines */
1411 while (*buf
== '\n' || *buf
== '\r')
1414 *bodylen
= strlen(buf
);
1415 *nonsiglen
= sigstart
- buf
;
1419 * If 'lines' is greater than 0, append that many lines from the given
1420 * 'buf' of length 'size' to the given strbuf.
1422 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1425 const char *sp
, *eol
;
1430 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1432 strbuf_addstr(out
, "\n ");
1433 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1434 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1435 strbuf_add(out
, sp
, len
);
1442 /* See grab_values */
1443 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct expand_data
*data
)
1446 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1447 size_t sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1448 void *buf
= data
->content
;
1450 for (i
= 0; i
< used_atom_cnt
; i
++) {
1451 struct used_atom
*atom
= &used_atom
[i
];
1452 const char *name
= atom
->name
;
1453 struct atom_value
*v
= &val
[i
];
1454 enum atom_type atom_type
= atom
->atom_type
;
1456 if (!!deref
!= (*name
== '*'))
1461 if (atom_type
== ATOM_RAW
) {
1462 unsigned long buf_size
= data
->size
;
1464 if (atom
->u
.raw_data
.option
== RAW_BARE
) {
1465 v
->s
= xmemdupz(buf
, buf_size
);
1466 v
->s_size
= buf_size
;
1467 } else if (atom
->u
.raw_data
.option
== RAW_LENGTH
) {
1468 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)buf_size
);
1473 if ((data
->type
!= OBJ_TAG
&&
1474 data
->type
!= OBJ_COMMIT
) ||
1475 (strcmp(name
, "body") &&
1476 !starts_with(name
, "subject") &&
1477 !starts_with(name
, "trailers") &&
1478 !starts_with(name
, "contents")))
1483 &bodypos
, &bodylen
, &nonsiglen
,
1486 if (atom
->u
.contents
.option
== C_SUB
)
1487 v
->s
= copy_subject(subpos
, sublen
);
1488 else if (atom
->u
.contents
.option
== C_SUB_SANITIZE
) {
1489 struct strbuf sb
= STRBUF_INIT
;
1490 format_sanitized_subject(&sb
, subpos
, sublen
);
1491 v
->s
= strbuf_detach(&sb
, NULL
);
1492 } else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1493 v
->s
= xmemdupz(bodypos
, bodylen
);
1494 else if (atom
->u
.contents
.option
== C_LENGTH
)
1495 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)strlen(subpos
));
1496 else if (atom
->u
.contents
.option
== C_BODY
)
1497 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1498 else if (atom
->u
.contents
.option
== C_SIG
)
1499 v
->s
= xmemdupz(sigpos
, siglen
);
1500 else if (atom
->u
.contents
.option
== C_LINES
) {
1501 struct strbuf s
= STRBUF_INIT
;
1502 const char *contents_end
= bodypos
+ nonsiglen
;
1504 /* Size is the length of the message after removing the signature */
1505 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1506 v
->s
= strbuf_detach(&s
, NULL
);
1507 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1508 struct strbuf s
= STRBUF_INIT
;
1510 /* Format the trailer info according to the trailer_opts given */
1511 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1513 v
->s
= strbuf_detach(&s
, NULL
);
1514 } else if (atom
->u
.contents
.option
== C_BARE
)
1515 v
->s
= xstrdup(subpos
);
1518 free((void *)sigpos
);
1522 * We want to have empty print-string for field requests
1523 * that do not apply (e.g. "authordate" for a tag object)
1525 static void fill_missing_values(struct atom_value
*val
)
1528 for (i
= 0; i
< used_atom_cnt
; i
++) {
1529 struct atom_value
*v
= &val
[i
];
1536 * val is a list of atom_value to hold returned values. Extract
1537 * the values for atoms in used_atom array out of (obj, buf, sz).
1538 * when deref is false, (obj, buf, sz) is the object that is
1539 * pointed at by the ref itself; otherwise it is the object the
1540 * ref (which is a tag) refers to.
1542 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, struct expand_data
*data
)
1544 void *buf
= data
->content
;
1546 switch (obj
->type
) {
1548 grab_tag_values(val
, deref
, obj
);
1549 grab_sub_body_contents(val
, deref
, data
);
1550 grab_person("tagger", val
, deref
, buf
);
1553 grab_commit_values(val
, deref
, obj
);
1554 grab_sub_body_contents(val
, deref
, data
);
1555 grab_person("author", val
, deref
, buf
);
1556 grab_person("committer", val
, deref
, buf
);
1559 /* grab_tree_values(val, deref, obj, buf, sz); */
1560 grab_sub_body_contents(val
, deref
, data
);
1563 /* grab_blob_values(val, deref, obj, buf, sz); */
1564 grab_sub_body_contents(val
, deref
, data
);
1567 die("Eh? Object of type %d?", obj
->type
);
1571 static inline char *copy_advance(char *dst
, const char *src
)
1578 static const char *lstrip_ref_components(const char *refname
, int len
)
1580 long remaining
= len
;
1581 const char *start
= xstrdup(refname
);
1582 const char *to_free
= start
;
1586 const char *p
= refname
;
1588 /* Find total no of '/' separated path-components */
1589 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1592 * The number of components we need to strip is now
1593 * the total minus the components to be left (Plus one
1594 * because we count the number of '/', but the number
1595 * of components is one more than the no of '/').
1597 remaining
= i
+ len
+ 1;
1600 while (remaining
> 0) {
1603 free((char *)to_free
);
1611 start
= xstrdup(start
);
1612 free((char *)to_free
);
1616 static const char *rstrip_ref_components(const char *refname
, int len
)
1618 long remaining
= len
;
1619 const char *start
= xstrdup(refname
);
1620 const char *to_free
= start
;
1624 const char *p
= refname
;
1626 /* Find total no of '/' separated path-components */
1627 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1630 * The number of components we need to strip is now
1631 * the total minus the components to be left (Plus one
1632 * because we count the number of '/', but the number
1633 * of components is one more than the no of '/').
1635 remaining
= i
+ len
+ 1;
1638 while (remaining
-- > 0) {
1639 char *p
= strrchr(start
, '/');
1641 free((char *)to_free
);
1649 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1651 if (atom
->option
== R_SHORT
)
1652 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1653 else if (atom
->option
== R_LSTRIP
)
1654 return lstrip_ref_components(refname
, atom
->lstrip
);
1655 else if (atom
->option
== R_RSTRIP
)
1656 return rstrip_ref_components(refname
, atom
->rstrip
);
1658 return xstrdup(refname
);
1661 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1662 struct branch
*branch
, const char **s
)
1664 int num_ours
, num_theirs
;
1665 if (atom
->u
.remote_ref
.option
== RR_REF
)
1666 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1667 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1668 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1669 NULL
, atom
->u
.remote_ref
.push
,
1670 AHEAD_BEHIND_FULL
) < 0) {
1671 *s
= xstrdup(msgs
.gone
);
1672 } else if (!num_ours
&& !num_theirs
)
1675 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1676 else if (!num_theirs
)
1677 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1679 *s
= xstrfmt(msgs
.ahead_behind
,
1680 num_ours
, num_theirs
);
1681 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1682 const char *to_free
= *s
;
1683 *s
= xstrfmt("[%s]", *s
);
1684 free((void *)to_free
);
1686 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1687 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1688 NULL
, atom
->u
.remote_ref
.push
,
1689 AHEAD_BEHIND_FULL
) < 0) {
1693 if (!num_ours
&& !num_theirs
)
1697 else if (!num_theirs
)
1701 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1703 const char *remote
= atom
->u
.remote_ref
.push
?
1704 pushremote_for_branch(branch
, &explicit) :
1705 remote_for_branch(branch
, &explicit);
1706 *s
= xstrdup(explicit ? remote
: "");
1707 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1710 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
1711 *s
= xstrdup(merge
? merge
: "");
1713 BUG("unhandled RR_* enum");
1716 char *get_head_description(void)
1718 struct strbuf desc
= STRBUF_INIT
;
1719 struct wt_status_state state
;
1720 memset(&state
, 0, sizeof(state
));
1721 wt_status_get_state(the_repository
, &state
, 1);
1722 if (state
.rebase_in_progress
||
1723 state
.rebase_interactive_in_progress
) {
1725 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1728 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
1729 state
.detached_from
);
1730 } else if (state
.bisect_in_progress
)
1731 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1733 else if (state
.detached_from
) {
1734 if (state
.detached_at
)
1735 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1736 state
.detached_from
);
1738 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1739 state
.detached_from
);
1741 strbuf_addstr(&desc
, _("(no branch)"));
1743 wt_status_state_free_buffers(&state
);
1745 return strbuf_detach(&desc
, NULL
);
1748 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1753 return show_ref(&atom
->u
.refname
, ref
->symref
);
1756 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1758 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1759 return get_head_description();
1760 return show_ref(&atom
->u
.refname
, ref
->refname
);
1763 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
1764 struct expand_data
*oi
, struct strbuf
*err
)
1766 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1768 if (oi
->info
.contentp
) {
1769 /* We need to know that to use parse_object_buffer properly */
1770 oi
->info
.sizep
= &oi
->size
;
1771 oi
->info
.typep
= &oi
->type
;
1773 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
1774 OBJECT_INFO_LOOKUP_REPLACE
))
1775 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1776 oid_to_hex(&oi
->oid
), ref
->refname
);
1777 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
1778 BUG("Object size is less than zero.");
1780 if (oi
->info
.contentp
) {
1781 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
1785 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1786 oid_to_hex(&oi
->oid
), ref
->refname
);
1788 grab_values(ref
->value
, deref
, *obj
, oi
);
1791 grab_common_values(ref
->value
, deref
, oi
);
1797 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
1801 for (i
= 0; worktrees
[i
]; i
++) {
1802 if (worktrees
[i
]->head_ref
) {
1803 struct ref_to_worktree_entry
*entry
;
1804 entry
= xmalloc(sizeof(*entry
));
1805 entry
->wt
= worktrees
[i
];
1806 hashmap_entry_init(&entry
->ent
,
1807 strhash(worktrees
[i
]->head_ref
));
1809 hashmap_add(map
, &entry
->ent
);
1814 static void lazy_init_worktree_map(void)
1816 if (ref_to_worktree_map
.worktrees
)
1819 ref_to_worktree_map
.worktrees
= get_worktrees();
1820 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
1821 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
1824 static char *get_worktree_path(const struct used_atom
*atom
, const struct ref_array_item
*ref
)
1826 struct hashmap_entry entry
, *e
;
1827 struct ref_to_worktree_entry
*lookup_result
;
1829 lazy_init_worktree_map();
1831 hashmap_entry_init(&entry
, strhash(ref
->refname
));
1832 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
1837 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
1839 return xstrdup(lookup_result
->wt
->path
);
1843 * Parse the object referred by ref, and grab needed value.
1845 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
1849 struct object_info empty
= OBJECT_INFO_INIT
;
1851 CALLOC_ARRAY(ref
->value
, used_atom_cnt
);
1853 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1854 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1857 ref
->symref
= xstrdup("");
1860 /* Fill in specials first */
1861 for (i
= 0; i
< used_atom_cnt
; i
++) {
1862 struct used_atom
*atom
= &used_atom
[i
];
1863 enum atom_type atom_type
= atom
->atom_type
;
1864 const char *name
= used_atom
[i
].name
;
1865 struct atom_value
*v
= &ref
->value
[i
];
1867 const char *refname
;
1868 struct branch
*branch
= NULL
;
1870 v
->s_size
= ATOM_SIZE_UNSPECIFIED
;
1871 v
->handler
= append_atom
;
1879 if (atom_type
== ATOM_REFNAME
)
1880 refname
= get_refname(atom
, ref
);
1881 else if (atom_type
== ATOM_WORKTREEPATH
) {
1882 if (ref
->kind
== FILTER_REFS_BRANCHES
)
1883 v
->s
= get_worktree_path(atom
, ref
);
1888 else if (atom_type
== ATOM_SYMREF
)
1889 refname
= get_symref(atom
, ref
);
1890 else if (atom_type
== ATOM_UPSTREAM
) {
1891 const char *branch_name
;
1892 /* only local branches may have an upstream */
1893 if (!skip_prefix(ref
->refname
, "refs/heads/",
1898 branch
= branch_get(branch_name
);
1900 refname
= branch_get_upstream(branch
, NULL
);
1902 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1906 } else if (atom_type
== ATOM_PUSH
&& atom
->u
.remote_ref
.push
) {
1907 const char *branch_name
;
1909 if (!skip_prefix(ref
->refname
, "refs/heads/",
1912 branch
= branch_get(branch_name
);
1914 if (atom
->u
.remote_ref
.push_remote
)
1917 refname
= branch_get_push(branch
, NULL
);
1921 /* We will definitely re-init v->s on the next line. */
1923 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1925 } else if (atom_type
== ATOM_COLOR
) {
1926 v
->s
= xstrdup(atom
->u
.color
);
1928 } else if (atom_type
== ATOM_FLAG
) {
1929 char buf
[256], *cp
= buf
;
1930 if (ref
->flag
& REF_ISSYMREF
)
1931 cp
= copy_advance(cp
, ",symref");
1932 if (ref
->flag
& REF_ISPACKED
)
1933 cp
= copy_advance(cp
, ",packed");
1938 v
->s
= xstrdup(buf
+ 1);
1941 } else if (!deref
&& atom_type
== ATOM_OBJECTNAME
&&
1942 grab_oid(name
, "objectname", &ref
->objectname
, v
, atom
)) {
1944 } else if (atom_type
== ATOM_HEAD
) {
1945 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1946 v
->s
= xstrdup("*");
1948 v
->s
= xstrdup(" ");
1950 } else if (atom_type
== ATOM_ALIGN
) {
1951 v
->handler
= align_atom_handler
;
1954 } else if (atom_type
== ATOM_END
) {
1955 v
->handler
= end_atom_handler
;
1958 } else if (atom_type
== ATOM_IF
) {
1960 if (skip_prefix(name
, "if:", &s
))
1964 v
->handler
= if_atom_handler
;
1966 } else if (atom_type
== ATOM_THEN
) {
1967 v
->handler
= then_atom_handler
;
1970 } else if (atom_type
== ATOM_ELSE
) {
1971 v
->handler
= else_atom_handler
;
1974 } else if (atom_type
== ATOM_REST
) {
1976 v
->s
= xstrdup(ref
->rest
);
1984 v
->s
= xstrdup(refname
);
1986 v
->s
= xstrfmt("%s^{}", refname
);
1987 free((char *)refname
);
1990 for (i
= 0; i
< used_atom_cnt
; i
++) {
1991 struct atom_value
*v
= &ref
->value
[i
];
1992 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
1993 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1994 oid_to_hex(&ref
->objectname
), ref
->refname
);
1998 oi
.info
.contentp
= &oi
.content
;
1999 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
2000 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
2004 oi
.oid
= ref
->objectname
;
2005 if (get_object(ref
, 0, &obj
, &oi
, err
))
2009 * If there is no atom that wants to know about tagged
2010 * object, we are done.
2012 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
2016 * If it is a tag object, see if we use a value that derefs
2017 * the object, and if we do grab the object it refers to.
2019 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
2022 * NEEDSWORK: This derefs tag only once, which
2023 * is good to deal with chains of trust, but
2024 * is not consistent with what deref_tag() does
2025 * which peels the onion to the core.
2027 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
2031 * Given a ref, return the value for the atom. This lazily gets value
2032 * out of the object by calling populate value.
2034 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
2035 struct atom_value
**v
, struct strbuf
*err
)
2038 if (populate_value(ref
, err
))
2040 fill_missing_values(ref
->value
);
2042 *v
= &ref
->value
[atom
];
2047 * Return 1 if the refname matches one of the patterns, otherwise 0.
2048 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2049 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2050 * matches "refs/heads/mas*", too).
2052 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
2054 const char **patterns
= filter
->name_patterns
;
2057 if (filter
->ignore_case
)
2058 flags
|= WM_CASEFOLD
;
2061 * When no '--format' option is given we need to skip the prefix
2062 * for matching refs of tags and branches.
2064 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
2065 skip_prefix(refname
, "refs/heads/", &refname
) ||
2066 skip_prefix(refname
, "refs/remotes/", &refname
) ||
2067 skip_prefix(refname
, "refs/", &refname
));
2069 for (; *patterns
; patterns
++) {
2070 if (!wildmatch(*patterns
, refname
, flags
))
2077 * Return 1 if the refname matches one of the patterns, otherwise 0.
2078 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
2079 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2080 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2082 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
2084 const char **pattern
= filter
->name_patterns
;
2085 int namelen
= strlen(refname
);
2086 unsigned flags
= WM_PATHNAME
;
2088 if (filter
->ignore_case
)
2089 flags
|= WM_CASEFOLD
;
2091 for (; *pattern
; pattern
++) {
2092 const char *p
= *pattern
;
2093 int plen
= strlen(p
);
2095 if ((plen
<= namelen
) &&
2096 !strncmp(refname
, p
, plen
) &&
2097 (refname
[plen
] == '\0' ||
2098 refname
[plen
] == '/' ||
2101 if (!wildmatch(p
, refname
, flags
))
2107 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
2108 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
2110 if (!*filter
->name_patterns
)
2111 return 1; /* No pattern always matches */
2112 if (filter
->match_as_path
)
2113 return match_name_as_path(filter
, refname
);
2114 return match_pattern(filter
, refname
);
2118 * This is the same as for_each_fullref_in(), but it tries to iterate
2119 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2120 * pattern match, so the callback still has to match each ref individually.
2122 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
2126 if (!filter
->match_as_path
) {
2128 * in this case, the patterns are applied after
2129 * prefixes like "refs/heads/" etc. are stripped off,
2130 * so we have to look at everything:
2132 return for_each_fullref_in("", cb
, cb_data
);
2135 if (filter
->ignore_case
) {
2137 * we can't handle case-insensitive comparisons,
2138 * so just return everything and let the caller
2141 return for_each_fullref_in("", cb
, cb_data
);
2144 if (!filter
->name_patterns
[0]) {
2145 /* no patterns; we have to look at everything */
2146 return for_each_fullref_in("", cb
, cb_data
);
2149 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository
),
2150 NULL
, filter
->name_patterns
,
2155 * Given a ref (oid, refname), check if the ref belongs to the array
2156 * of oids. If the given ref is a tag, check if the given tag points
2157 * at one of the oids in the given oid array.
2159 * 1. Only a single level of indirection is obtained, we might want to
2160 * change this to account for multiple levels (e.g. annotated tags
2161 * pointing to annotated tags pointing to a commit.)
2162 * 2. As the refs are cached we might know what refname peels to without
2163 * the need to parse the object via parse_object(). peel_ref() might be a
2164 * more efficient alternative to obtain the pointee.
2166 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
2167 const struct object_id
*oid
,
2168 const char *refname
)
2170 const struct object_id
*tagged_oid
= NULL
;
2173 if (oid_array_lookup(points_at
, oid
) >= 0)
2175 obj
= parse_object(the_repository
, oid
);
2177 die(_("malformed object at '%s'"), refname
);
2178 if (obj
->type
== OBJ_TAG
)
2179 tagged_oid
= get_tagged_oid((struct tag
*)obj
);
2180 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
2186 * Allocate space for a new ref_array_item and copy the name and oid to it.
2188 * Callers can then fill in other struct members at their leisure.
2190 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2191 const struct object_id
*oid
)
2193 struct ref_array_item
*ref
;
2195 FLEX_ALLOC_STR(ref
, refname
, refname
);
2196 oidcpy(&ref
->objectname
, oid
);
2202 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2203 const char *refname
,
2204 const struct object_id
*oid
)
2206 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2208 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2209 array
->items
[array
->nr
++] = ref
;
2214 static int ref_kind_from_refname(const char *refname
)
2222 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2223 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2224 { "refs/tags/", FILTER_REFS_TAGS
}
2227 if (!strcmp(refname
, "HEAD"))
2228 return FILTER_REFS_DETACHED_HEAD
;
2230 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2231 if (starts_with(refname
, ref_kind
[i
].prefix
))
2232 return ref_kind
[i
].kind
;
2235 return FILTER_REFS_OTHERS
;
2238 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2240 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2241 filter
->kind
== FILTER_REFS_REMOTES
||
2242 filter
->kind
== FILTER_REFS_TAGS
)
2243 return filter
->kind
;
2244 return ref_kind_from_refname(refname
);
2247 struct ref_filter_cbdata
{
2248 struct ref_array
*array
;
2249 struct ref_filter
*filter
;
2250 struct contains_cache contains_cache
;
2251 struct contains_cache no_contains_cache
;
2255 * A call-back given to for_each_ref(). Filter refs and keep them for
2256 * later object processing.
2258 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2260 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2261 struct ref_filter
*filter
= ref_cbdata
->filter
;
2262 struct ref_array_item
*ref
;
2263 struct commit
*commit
= NULL
;
2266 if (flag
& REF_BAD_NAME
) {
2267 warning(_("ignoring ref with broken name %s"), refname
);
2271 if (flag
& REF_ISBROKEN
) {
2272 warning(_("ignoring broken ref %s"), refname
);
2276 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2277 kind
= filter_ref_kind(filter
, refname
);
2278 if (!(kind
& filter
->kind
))
2281 if (!filter_pattern_match(filter
, refname
))
2284 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2288 * A merge filter is applied on refs pointing to commits. Hence
2289 * obtain the commit using the 'oid' available and discard all
2290 * non-commits early. The actual filtering is done later.
2292 if (filter
->reachable_from
|| filter
->unreachable_from
||
2293 filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2294 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
2297 /* We perform the filtering for the '--contains' option... */
2298 if (filter
->with_commit
&&
2299 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2301 /* ...or for the `--no-contains' option */
2302 if (filter
->no_commit
&&
2303 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2308 * We do not open the object yet; sort may only need refname
2309 * to do its job and the resulting list may yet to be pruned
2310 * by maxcount logic.
2312 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2313 ref
->commit
= commit
;
2320 /* Free memory allocated for a ref_array_item */
2321 static void free_array_item(struct ref_array_item
*item
)
2323 free((char *)item
->symref
);
2326 for (i
= 0; i
< used_atom_cnt
; i
++)
2327 free((char *)item
->value
[i
].s
);
2333 /* Free all memory allocated for ref_array */
2334 void ref_array_clear(struct ref_array
*array
)
2338 for (i
= 0; i
< array
->nr
; i
++)
2339 free_array_item(array
->items
[i
]);
2340 FREE_AND_NULL(array
->items
);
2341 array
->nr
= array
->alloc
= 0;
2343 for (i
= 0; i
< used_atom_cnt
; i
++) {
2344 struct used_atom
*atom
= &used_atom
[i
];
2345 if (atom
->atom_type
== ATOM_HEAD
)
2347 free((char *)atom
->name
);
2349 FREE_AND_NULL(used_atom
);
2352 if (ref_to_worktree_map
.worktrees
) {
2353 hashmap_clear_and_free(&(ref_to_worktree_map
.map
),
2354 struct ref_to_worktree_entry
, ent
);
2355 free_worktrees(ref_to_worktree_map
.worktrees
);
2356 ref_to_worktree_map
.worktrees
= NULL
;
2360 #define EXCLUDE_REACHED 0
2361 #define INCLUDE_REACHED 1
2362 static void reach_filter(struct ref_array
*array
,
2363 struct commit_list
*check_reachable
,
2364 int include_reached
)
2366 struct rev_info revs
;
2368 struct commit
**to_clear
;
2369 struct commit_list
*cr
;
2371 if (!check_reachable
)
2374 CALLOC_ARRAY(to_clear
, array
->nr
);
2376 repo_init_revisions(the_repository
, &revs
, NULL
);
2378 for (i
= 0; i
< array
->nr
; i
++) {
2379 struct ref_array_item
*item
= array
->items
[i
];
2380 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
2381 to_clear
[i
] = item
->commit
;
2384 for (cr
= check_reachable
; cr
; cr
= cr
->next
) {
2385 struct commit
*merge_commit
= cr
->item
;
2386 merge_commit
->object
.flags
|= UNINTERESTING
;
2387 add_pending_object(&revs
, &merge_commit
->object
, "");
2391 if (prepare_revision_walk(&revs
))
2392 die(_("revision walk setup failed"));
2397 for (i
= 0; i
< old_nr
; i
++) {
2398 struct ref_array_item
*item
= array
->items
[i
];
2399 struct commit
*commit
= item
->commit
;
2401 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2403 if (is_merged
== include_reached
)
2404 array
->items
[array
->nr
++] = array
->items
[i
];
2406 free_array_item(item
);
2409 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2411 while (check_reachable
) {
2412 struct commit
*merge_commit
= pop_commit(&check_reachable
);
2413 clear_commit_marks(merge_commit
, ALL_REV_FLAGS
);
2416 release_revisions(&revs
);
2421 * API for filtering a set of refs. Based on the type of refs the user
2422 * has requested, we iterate through those refs and apply filters
2423 * as per the given ref_filter structure and finally store the
2424 * filtered refs in the ref_array structure.
2426 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2428 struct ref_filter_cbdata ref_cbdata
;
2429 int save_commit_buffer_orig
;
2432 ref_cbdata
.array
= array
;
2433 ref_cbdata
.filter
= filter
;
2435 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2437 save_commit_buffer_orig
= save_commit_buffer
;
2438 save_commit_buffer
= 0;
2440 init_contains_cache(&ref_cbdata
.contains_cache
);
2441 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2443 /* Simple per-ref filtering */
2445 die("filter_refs: invalid type");
2448 * For common cases where we need only branches or remotes or tags,
2449 * we only iterate through those refs. If a mix of refs is needed,
2450 * we iterate over all refs and filter out required refs with the help
2451 * of filter_ref_kind().
2453 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2454 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
);
2455 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2456 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
);
2457 else if (filter
->kind
== FILTER_REFS_TAGS
)
2458 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
);
2459 else if (filter
->kind
& FILTER_REFS_ALL
)
2460 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
);
2461 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2462 head_ref(ref_filter_handler
, &ref_cbdata
);
2465 clear_contains_cache(&ref_cbdata
.contains_cache
);
2466 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2468 /* Filters that need revision walking */
2469 reach_filter(array
, filter
->reachable_from
, INCLUDE_REACHED
);
2470 reach_filter(array
, filter
->unreachable_from
, EXCLUDE_REACHED
);
2472 save_commit_buffer
= save_commit_buffer_orig
;
2476 static int compare_detached_head(struct ref_array_item
*a
, struct ref_array_item
*b
)
2478 if (!(a
->kind
^ b
->kind
))
2479 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
2480 if (a
->kind
& FILTER_REFS_DETACHED_HEAD
)
2482 else if (b
->kind
& FILTER_REFS_DETACHED_HEAD
)
2484 BUG("should have died in the xor check above");
2488 static int memcasecmp(const void *vs1
, const void *vs2
, size_t n
)
2490 const char *s1
= vs1
, *s2
= vs2
;
2491 const char *end
= s1
+ n
;
2493 for (; s1
< end
; s1
++, s2
++) {
2494 int diff
= tolower(*s1
) - tolower(*s2
);
2501 struct ref_sorting
{
2502 struct ref_sorting
*next
;
2503 int atom
; /* index into used_atom array (internal) */
2504 enum ref_sorting_order sort_flags
;
2507 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2509 struct atom_value
*va
, *vb
;
2511 int cmp_detached_head
= 0;
2512 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2513 struct strbuf err
= STRBUF_INIT
;
2515 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2517 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2519 strbuf_release(&err
);
2520 if (s
->sort_flags
& REF_SORTING_DETACHED_HEAD_FIRST
&&
2521 ((a
->kind
| b
->kind
) & FILTER_REFS_DETACHED_HEAD
)) {
2522 cmp
= compare_detached_head(a
, b
);
2523 cmp_detached_head
= 1;
2524 } else if (s
->sort_flags
& REF_SORTING_VERSION
) {
2525 cmp
= versioncmp(va
->s
, vb
->s
);
2526 } else if (cmp_type
== FIELD_STR
) {
2527 if (va
->s_size
< 0 && vb
->s_size
< 0) {
2528 int (*cmp_fn
)(const char *, const char *);
2529 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
2530 ? strcasecmp
: strcmp
;
2531 cmp
= cmp_fn(va
->s
, vb
->s
);
2533 size_t a_size
= va
->s_size
< 0 ?
2534 strlen(va
->s
) : va
->s_size
;
2535 size_t b_size
= vb
->s_size
< 0 ?
2536 strlen(vb
->s
) : vb
->s_size
;
2537 int (*cmp_fn
)(const void *, const void *, size_t);
2538 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
2539 ? memcasecmp
: memcmp
;
2541 cmp
= cmp_fn(va
->s
, vb
->s
, b_size
> a_size
?
2544 if (a_size
> b_size
)
2546 else if (a_size
< b_size
)
2551 if (va
->value
< vb
->value
)
2553 else if (va
->value
== vb
->value
)
2559 return (s
->sort_flags
& REF_SORTING_REVERSE
&& !cmp_detached_head
)
2563 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2565 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2566 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2567 struct ref_sorting
*s
;
2569 for (s
= ref_sorting
; s
; s
= s
->next
) {
2570 int cmp
= cmp_ref_sorting(s
, a
, b
);
2575 return s
&& s
->sort_flags
& REF_SORTING_ICASE
?
2576 strcasecmp(a
->refname
, b
->refname
) :
2577 strcmp(a
->refname
, b
->refname
);
2580 void ref_sorting_set_sort_flags_all(struct ref_sorting
*sorting
,
2581 unsigned int mask
, int on
)
2583 for (; sorting
; sorting
= sorting
->next
) {
2585 sorting
->sort_flags
|= mask
;
2587 sorting
->sort_flags
&= ~mask
;
2591 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2593 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2596 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2598 struct strbuf
*s
= &state
->stack
->output
;
2600 while (*cp
&& (!ep
|| cp
< ep
)) {
2605 int ch
= hex2chr(cp
+ 1);
2607 strbuf_addch(s
, ch
);
2613 strbuf_addch(s
, *cp
);
2618 int format_ref_array_item(struct ref_array_item
*info
,
2619 struct ref_format
*format
,
2620 struct strbuf
*final_buf
,
2621 struct strbuf
*error_buf
)
2623 const char *cp
, *sp
, *ep
;
2624 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2626 state
.quote_style
= format
->quote_style
;
2627 push_stack_element(&state
.stack
);
2629 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2630 struct atom_value
*atomv
;
2633 ep
= strchr(sp
, ')');
2635 append_literal(cp
, sp
, &state
);
2636 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2637 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2638 atomv
->handler(atomv
, &state
, error_buf
)) {
2639 pop_stack_element(&state
.stack
);
2644 sp
= cp
+ strlen(cp
);
2645 append_literal(cp
, sp
, &state
);
2647 if (format
->need_color_reset_at_eol
) {
2648 struct atom_value resetv
= ATOM_VALUE_INIT
;
2649 resetv
.s
= GIT_COLOR_RESET
;
2650 if (append_atom(&resetv
, &state
, error_buf
)) {
2651 pop_stack_element(&state
.stack
);
2655 if (state
.stack
->prev
) {
2656 pop_stack_element(&state
.stack
);
2657 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2659 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2660 pop_stack_element(&state
.stack
);
2664 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2665 struct ref_format
*format
)
2667 struct ref_array_item
*ref_item
;
2668 struct strbuf output
= STRBUF_INIT
;
2669 struct strbuf err
= STRBUF_INIT
;
2671 ref_item
= new_ref_array_item(name
, oid
);
2672 ref_item
->kind
= ref_kind_from_refname(name
);
2673 if (format_ref_array_item(ref_item
, format
, &output
, &err
))
2675 fwrite(output
.buf
, 1, output
.len
, stdout
);
2678 strbuf_release(&err
);
2679 strbuf_release(&output
);
2680 free_array_item(ref_item
);
2683 static int parse_sorting_atom(const char *atom
)
2686 * This parses an atom using a dummy ref_format, since we don't
2687 * actually care about the formatting details.
2689 struct ref_format dummy
= REF_FORMAT_INIT
;
2690 const char *end
= atom
+ strlen(atom
);
2691 struct strbuf err
= STRBUF_INIT
;
2692 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2695 strbuf_release(&err
);
2699 /* If no sorting option is given, use refname to sort as default */
2700 static struct ref_sorting
*ref_default_sorting(void)
2702 static const char cstr_name
[] = "refname";
2704 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2706 sorting
->next
= NULL
;
2707 sorting
->atom
= parse_sorting_atom(cstr_name
);
2711 static void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2713 struct ref_sorting
*s
;
2716 s
->next
= *sorting_tail
;
2720 s
->sort_flags
|= REF_SORTING_REVERSE
;
2723 if (skip_prefix(arg
, "version:", &arg
) ||
2724 skip_prefix(arg
, "v:", &arg
))
2725 s
->sort_flags
|= REF_SORTING_VERSION
;
2726 s
->atom
= parse_sorting_atom(arg
);
2729 struct ref_sorting
*ref_sorting_options(struct string_list
*options
)
2731 struct string_list_item
*item
;
2732 struct ref_sorting
*sorting
= NULL
, **tail
= &sorting
;
2735 sorting
= ref_default_sorting();
2737 for_each_string_list_item(item
, options
)
2738 parse_ref_sorting(tail
, item
->string
);
2742 * From here on, the ref_sorting list should be used to talk
2743 * about the sort order used for the output. The caller
2744 * should not touch the string form anymore.
2746 string_list_clear(options
, 0);
2750 void ref_sorting_release(struct ref_sorting
*sorting
)
2753 struct ref_sorting
*next
= sorting
->next
;
2759 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2761 struct ref_filter
*rf
= opt
->value
;
2762 struct object_id oid
;
2763 struct commit
*merge_commit
;
2765 BUG_ON_OPT_NEG(unset
);
2767 if (get_oid(arg
, &oid
))
2768 die(_("malformed object name %s"), arg
);
2770 merge_commit
= lookup_commit_reference_gently(the_repository
, &oid
, 0);
2773 return error(_("option `%s' must point to a commit"), opt
->long_name
);
2775 if (starts_with(opt
->long_name
, "no"))
2776 commit_list_insert(merge_commit
, &rf
->unreachable_from
);
2778 commit_list_insert(merge_commit
, &rf
->reachable_from
);