1 #include "git-compat-util.h"
2 #include "environment.h"
5 #include "gpg-interface.h"
7 #include "parse-options.h"
8 #include "run-command.h"
10 #include "wildmatch.h"
11 #include "object-name.h"
12 #include "object-store-ll.h"
13 #include "oid-array.h"
14 #include "repository.h"
22 #include "ref-filter.h"
25 #include "versioncmp.h"
27 #include "wt-status.h"
28 #include "commit-slab.h"
29 #include "commit-reach.h"
33 static struct ref_msg
{
37 const char *ahead_behind
;
39 /* Untranslated plumbing messages: */
46 void setup_ref_filter_porcelain_msg(void)
48 msgs
.gone
= _("gone");
49 msgs
.ahead
= _("ahead %d");
50 msgs
.behind
= _("behind %d");
51 msgs
.ahead_behind
= _("ahead %d, behind %d");
54 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
55 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
56 typedef enum { SOURCE_NONE
= 0, SOURCE_OBJ
, SOURCE_OTHER
} info_source
;
64 cmp_status cmp_status
;
66 unsigned int then_atom_seen
: 1,
68 condition_satisfied
: 1;
72 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
76 static struct ref_trailer_buf
{
77 struct string_list filter_list
;
79 struct strbuf kvsepbuf
;
80 } ref_trailer_buf
= {STRING_LIST_INIT_NODUP
, STRBUF_INIT
, STRBUF_INIT
};
82 static struct expand_data
{
84 enum object_type type
;
87 struct object_id delta_base_oid
;
90 struct object_info info
;
93 struct ref_to_worktree_entry
{
94 struct hashmap_entry ent
;
95 struct worktree
*wt
; /* key is wt->head_ref */
98 static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED
,
99 const struct hashmap_entry
*eptr
,
100 const struct hashmap_entry
*kptr
,
101 const void *keydata_aka_refname
)
103 const struct ref_to_worktree_entry
*e
, *k
;
105 e
= container_of(eptr
, const struct ref_to_worktree_entry
, ent
);
106 k
= container_of(kptr
, const struct ref_to_worktree_entry
, ent
);
108 return strcmp(e
->wt
->head_ref
,
109 keydata_aka_refname
? keydata_aka_refname
: k
->wt
->head_ref
);
112 static struct ref_to_worktree_map
{
114 struct worktree
**worktrees
;
115 } ref_to_worktree_map
;
118 * The enum atom_type is used as the index of valid_atom array.
119 * In the atom parsing stage, it will be passed to used_atom.atom_type
120 * as the identifier of the atom type. We can check the type of used_atom
121 * entry by `if (used_atom[i].atom_type == ATOM_*)`.
173 * An atom is a valid field atom listed below, possibly prefixed with
174 * a "*" to denote deref_tag().
176 * We parse given format string and sort specifiers, and make a list
177 * of properties that we need to extract out of objects. ref_array_item
178 * structure will hold an array of values extracted that can be
179 * indexed with the "atom number", which is an index into this
182 static struct used_atom
{
183 enum atom_type atom_type
;
188 char color
[COLOR_MAXLEN
];
192 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
194 struct refname_atom refname
;
195 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
198 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LENGTH
, C_LINES
,
199 C_SIG
, C_SUB
, C_SUB_SANITIZE
, C_TRAILERS
} option
;
200 struct process_trailer_options trailer_opts
;
204 enum { RAW_BARE
, RAW_LENGTH
} option
;
207 cmp_status cmp_status
;
211 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
215 enum { O_SIZE
, O_SIZE_DISK
} option
;
218 enum { N_RAW
, N_MAILMAP
} option
;
229 enum { S_BARE
, S_GRADE
, S_SIGNER
, S_KEY
,
230 S_FINGERPRINT
, S_PRI_KEY_FP
, S_TRUST_LEVEL
} option
;
232 const char **describe_args
;
233 struct refname_atom refname
;
237 static int used_atom_cnt
, need_tagged
, need_symref
;
240 * Expand string, append it to strbuf *sb, then return error code ret.
241 * Allow to save few lines of code.
243 __attribute__((format (printf
, 3, 4)))
244 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
248 strbuf_vaddf(sb
, fmt
, ap
);
253 static int err_no_arg(struct strbuf
*sb
, const char *name
)
255 size_t namelen
= strchrnul(name
, ':') - name
;
256 strbuf_addf(sb
, _("%%(%.*s) does not take arguments"),
261 static int err_bad_arg(struct strbuf
*sb
, const char *name
, const char *arg
)
263 size_t namelen
= strchrnul(name
, ':') - name
;
264 strbuf_addf(sb
, _("unrecognized %%(%.*s) argument: %s"),
265 (int)namelen
, name
, arg
);
270 * Parse option of name "candidate" in the option string "to_parse" of
273 * "candidate1[=val1],candidate2[=val2],candidate3[=val3],..."
275 * The remaining part of "to_parse" is stored in "end" (if we are
276 * parsing the last candidate, then this is NULL) and the value of
277 * the candidate is stored in "valuestart" and its length in "valuelen",
278 * that is the portion after "=". Since it is possible for a "candidate"
279 * to not have a value, in such cases, "valuestart" is set to point to
280 * NULL and "valuelen" to 0.
282 * The function returns 1 on success. It returns 0 if we don't find
283 * "candidate" in "to_parse" or we find "candidate" but it is followed
284 * by more chars (for example, "candidatefoo"), that is, we don't find
287 * This function only does the above for one "candidate" at a time. So
288 * it has to be called each time trying to parse a "candidate" in the
289 * option string "to_parse".
291 static int match_atom_arg_value(const char *to_parse
, const char *candidate
,
292 const char **end
, const char **valuestart
,
297 if (!skip_prefix(to_parse
, candidate
, &atom
))
298 return 0; /* definitely not "candidate" */
301 /* we just saw "candidate=" */
302 *valuestart
= atom
+ 1;
303 atom
= strchrnul(*valuestart
, ',');
304 *valuelen
= atom
- *valuestart
;
305 } else if (*atom
!= ',' && *atom
!= '\0') {
306 /* key begins with "candidate" but has more chars */
309 /* just "candidate" without "=val" */
314 /* atom points at either the ',' or NUL after this key[=val] */
318 BUG("Why is *atom not NULL yet?");
325 * Parse boolean option of name "candidate" in the option list "to_parse"
328 * "candidate1[=bool1],candidate2[=bool2],candidate3[=bool3],..."
330 * The remaining part of "to_parse" is stored in "end" (if we are parsing
331 * the last candidate, then this is NULL) and the value (if given) is
332 * parsed and stored in "val", so "val" always points to either 0 or 1.
333 * If the value is not given, then "val" is set to point to 1.
335 * The boolean value is parsed using "git_parse_maybe_bool()", so the
336 * accepted values are
338 * to set true - "1", "yes", "true"
339 * to set false - "0", "no", "false"
341 * This function returns 1 on success. It returns 0 when we don't find
342 * an exact match for "candidate" or when the boolean value given is
345 static int match_atom_bool_arg(const char *to_parse
, const char *candidate
,
346 const char **end
, int *val
)
353 if (!match_atom_arg_value(to_parse
, candidate
, end
, &argval
, &arglen
))
361 strval
= xstrndup(argval
, arglen
);
362 v
= git_parse_maybe_bool(strval
);
373 static int color_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
374 const char *color_value
, struct strbuf
*err
)
377 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
378 if (color_parse(color_value
, atom
->u
.color
) < 0)
379 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
382 * We check this after we've parsed the color, which lets us complain
383 * about syntactically bogus color names even if they won't be used.
385 if (!want_color(format
->use_color
))
386 color_parse("", atom
->u
.color
);
390 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
391 const char *name
, struct strbuf
*err
)
394 atom
->option
= R_NORMAL
;
395 else if (!strcmp(arg
, "short"))
396 atom
->option
= R_SHORT
;
397 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
398 skip_prefix(arg
, "strip=", &arg
)) {
399 atom
->option
= R_LSTRIP
;
400 if (strtol_i(arg
, 10, &atom
->lstrip
))
401 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
402 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
403 atom
->option
= R_RSTRIP
;
404 if (strtol_i(arg
, 10, &atom
->rstrip
))
405 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
407 return err_bad_arg(err
, name
, arg
);
411 static int remote_ref_atom_parser(struct ref_format
*format UNUSED
,
412 struct used_atom
*atom
,
413 const char *arg
, struct strbuf
*err
)
415 struct string_list params
= STRING_LIST_INIT_DUP
;
418 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
419 atom
->u
.remote_ref
.push
= 1;
422 atom
->u
.remote_ref
.option
= RR_REF
;
423 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
424 arg
, atom
->name
, err
);
427 atom
->u
.remote_ref
.nobracket
= 0;
428 string_list_split(¶ms
, arg
, ',', -1);
430 for (i
= 0; i
< params
.nr
; i
++) {
431 const char *s
= params
.items
[i
].string
;
433 if (!strcmp(s
, "track"))
434 atom
->u
.remote_ref
.option
= RR_TRACK
;
435 else if (!strcmp(s
, "trackshort"))
436 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
437 else if (!strcmp(s
, "nobracket"))
438 atom
->u
.remote_ref
.nobracket
= 1;
439 else if (!strcmp(s
, "remotename")) {
440 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
441 atom
->u
.remote_ref
.push_remote
= 1;
442 } else if (!strcmp(s
, "remoteref")) {
443 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
444 atom
->u
.remote_ref
.push_remote
= 1;
446 atom
->u
.remote_ref
.option
= RR_REF
;
447 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
448 arg
, atom
->name
, err
)) {
449 string_list_clear(¶ms
, 0);
455 string_list_clear(¶ms
, 0);
459 static int objecttype_atom_parser(struct ref_format
*format UNUSED
,
460 struct used_atom
*atom
,
461 const char *arg
, struct strbuf
*err
)
464 return err_no_arg(err
, "objecttype");
465 if (*atom
->name
== '*')
466 oi_deref
.info
.typep
= &oi_deref
.type
;
468 oi
.info
.typep
= &oi
.type
;
472 static int objectsize_atom_parser(struct ref_format
*format UNUSED
,
473 struct used_atom
*atom
,
474 const char *arg
, struct strbuf
*err
)
477 atom
->u
.objectsize
.option
= O_SIZE
;
478 if (*atom
->name
== '*')
479 oi_deref
.info
.sizep
= &oi_deref
.size
;
481 oi
.info
.sizep
= &oi
.size
;
482 } else if (!strcmp(arg
, "disk")) {
483 atom
->u
.objectsize
.option
= O_SIZE_DISK
;
484 if (*atom
->name
== '*')
485 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
487 oi
.info
.disk_sizep
= &oi
.disk_size
;
489 return err_bad_arg(err
, "objectsize", arg
);
493 static int deltabase_atom_parser(struct ref_format
*format UNUSED
,
494 struct used_atom
*atom
,
495 const char *arg
, struct strbuf
*err
)
498 return err_no_arg(err
, "deltabase");
499 if (*atom
->name
== '*')
500 oi_deref
.info
.delta_base_oid
= &oi_deref
.delta_base_oid
;
502 oi
.info
.delta_base_oid
= &oi
.delta_base_oid
;
506 static int body_atom_parser(struct ref_format
*format UNUSED
,
507 struct used_atom
*atom
,
508 const char *arg
, struct strbuf
*err
)
511 return err_no_arg(err
, "body");
512 atom
->u
.contents
.option
= C_BODY_DEP
;
516 static int subject_atom_parser(struct ref_format
*format UNUSED
,
517 struct used_atom
*atom
,
518 const char *arg
, struct strbuf
*err
)
521 atom
->u
.contents
.option
= C_SUB
;
522 else if (!strcmp(arg
, "sanitize"))
523 atom
->u
.contents
.option
= C_SUB_SANITIZE
;
525 return err_bad_arg(err
, "subject", arg
);
529 static int parse_signature_option(const char *arg
)
533 else if (!strcmp(arg
, "signer"))
535 else if (!strcmp(arg
, "grade"))
537 else if (!strcmp(arg
, "key"))
539 else if (!strcmp(arg
, "fingerprint"))
540 return S_FINGERPRINT
;
541 else if (!strcmp(arg
, "primarykeyfingerprint"))
543 else if (!strcmp(arg
, "trustlevel"))
544 return S_TRUST_LEVEL
;
548 static int signature_atom_parser(struct ref_format
*format UNUSED
,
549 struct used_atom
*atom
,
550 const char *arg
, struct strbuf
*err
)
552 int opt
= parse_signature_option(arg
);
554 return err_bad_arg(err
, "signature", arg
);
555 atom
->u
.signature
.option
= opt
;
559 static int trailers_atom_parser(struct ref_format
*format UNUSED
,
560 struct used_atom
*atom
,
561 const char *arg
, struct strbuf
*err
)
563 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
566 const char *argbuf
= xstrfmt("%s)", arg
);
567 char *invalid_arg
= NULL
;
569 if (format_set_trailers_options(&atom
->u
.contents
.trailer_opts
,
570 &ref_trailer_buf
.filter_list
,
571 &ref_trailer_buf
.sepbuf
,
572 &ref_trailer_buf
.kvsepbuf
,
573 &argbuf
, &invalid_arg
)) {
575 strbuf_addf(err
, _("expected %%(trailers:key=<value>)"));
577 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), invalid_arg
);
578 free((char *)invalid_arg
);
582 atom
->u
.contents
.option
= C_TRAILERS
;
586 static int contents_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
587 const char *arg
, struct strbuf
*err
)
590 atom
->u
.contents
.option
= C_BARE
;
591 else if (!strcmp(arg
, "body"))
592 atom
->u
.contents
.option
= C_BODY
;
593 else if (!strcmp(arg
, "size")) {
594 atom
->type
= FIELD_ULONG
;
595 atom
->u
.contents
.option
= C_LENGTH
;
596 } else if (!strcmp(arg
, "signature"))
597 atom
->u
.contents
.option
= C_SIG
;
598 else if (!strcmp(arg
, "subject"))
599 atom
->u
.contents
.option
= C_SUB
;
600 else if (!strcmp(arg
, "trailers")) {
601 if (trailers_atom_parser(format
, atom
, NULL
, err
))
603 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
604 if (trailers_atom_parser(format
, atom
, arg
, err
))
606 } else if (skip_prefix(arg
, "lines=", &arg
)) {
607 atom
->u
.contents
.option
= C_LINES
;
608 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
609 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
611 return err_bad_arg(err
, "contents", arg
);
615 static int describe_atom_option_parser(struct strvec
*args
, const char **arg
,
622 if (match_atom_bool_arg(*arg
, "tags", arg
, &optval
)) {
624 strvec_push(args
, "--no-tags");
626 strvec_push(args
, "--tags");
630 if (match_atom_arg_value(*arg
, "abbrev", arg
, &argval
, &arglen
)) {
634 return strbuf_addf_ret(err
, -1,
635 _("argument expected for %s"),
637 if (strtol(argval
, &endptr
, 10) < 0)
638 return strbuf_addf_ret(err
, -1,
639 _("positive value expected %s=%s"),
640 "describe:abbrev", argval
);
641 if (endptr
- argval
!= arglen
)
642 return strbuf_addf_ret(err
, -1,
643 _("cannot fully parse %s=%s"),
644 "describe:abbrev", argval
);
646 strvec_pushf(args
, "--abbrev=%.*s", (int)arglen
, argval
);
650 if (match_atom_arg_value(*arg
, "match", arg
, &argval
, &arglen
)) {
652 return strbuf_addf_ret(err
, -1,
653 _("value expected %s="),
656 strvec_pushf(args
, "--match=%.*s", (int)arglen
, argval
);
660 if (match_atom_arg_value(*arg
, "exclude", arg
, &argval
, &arglen
)) {
662 return strbuf_addf_ret(err
, -1,
663 _("value expected %s="),
666 strvec_pushf(args
, "--exclude=%.*s", (int)arglen
, argval
);
673 static int describe_atom_parser(struct ref_format
*format UNUSED
,
674 struct used_atom
*atom
,
675 const char *arg
, struct strbuf
*err
)
677 struct strvec args
= STRVEC_INIT
;
681 const char *bad_arg
= arg
;
686 found
= describe_atom_option_parser(&args
, &arg
, err
);
690 return err_bad_arg(err
, "describe", bad_arg
);
692 atom
->u
.describe_args
= strvec_detach(&args
);
696 static int raw_atom_parser(struct ref_format
*format UNUSED
,
697 struct used_atom
*atom
,
698 const char *arg
, struct strbuf
*err
)
701 atom
->u
.raw_data
.option
= RAW_BARE
;
702 else if (!strcmp(arg
, "size")) {
703 atom
->type
= FIELD_ULONG
;
704 atom
->u
.raw_data
.option
= RAW_LENGTH
;
706 return err_bad_arg(err
, "raw", arg
);
710 static int oid_atom_parser(struct ref_format
*format UNUSED
,
711 struct used_atom
*atom
,
712 const char *arg
, struct strbuf
*err
)
715 atom
->u
.oid
.option
= O_FULL
;
716 else if (!strcmp(arg
, "short"))
717 atom
->u
.oid
.option
= O_SHORT
;
718 else if (skip_prefix(arg
, "short=", &arg
)) {
719 atom
->u
.oid
.option
= O_LENGTH
;
720 if (strtoul_ui(arg
, 10, &atom
->u
.oid
.length
) ||
721 atom
->u
.oid
.length
== 0)
722 return strbuf_addf_ret(err
, -1, _("positive value expected '%s' in %%(%s)"), arg
, atom
->name
);
723 if (atom
->u
.oid
.length
< MINIMUM_ABBREV
)
724 atom
->u
.oid
.length
= MINIMUM_ABBREV
;
726 return err_bad_arg(err
, atom
->name
, arg
);
730 static int person_name_atom_parser(struct ref_format
*format UNUSED
,
731 struct used_atom
*atom
,
732 const char *arg
, struct strbuf
*err
)
735 atom
->u
.name_option
.option
= N_RAW
;
736 else if (!strcmp(arg
, "mailmap"))
737 atom
->u
.name_option
.option
= N_MAILMAP
;
739 return err_bad_arg(err
, atom
->name
, arg
);
743 static int email_atom_option_parser(struct used_atom
*atom
,
744 const char **arg
, struct strbuf
*err
)
748 if (skip_prefix(*arg
, "trim", arg
))
750 if (skip_prefix(*arg
, "localpart", arg
))
752 if (skip_prefix(*arg
, "mailmap", arg
))
757 static int person_email_atom_parser(struct ref_format
*format UNUSED
,
758 struct used_atom
*atom
,
759 const char *arg
, struct strbuf
*err
)
762 int opt
= email_atom_option_parser(atom
, &arg
, err
);
763 const char *bad_arg
= arg
;
766 return err_bad_arg(err
, atom
->name
, bad_arg
);
767 atom
->u
.email_option
.option
|= opt
;
774 return err_bad_arg(err
, atom
->name
, bad_arg
);
779 static int refname_atom_parser(struct ref_format
*format UNUSED
,
780 struct used_atom
*atom
,
781 const char *arg
, struct strbuf
*err
)
783 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
786 static align_type
parse_align_position(const char *s
)
788 if (!strcmp(s
, "right"))
790 else if (!strcmp(s
, "middle"))
792 else if (!strcmp(s
, "left"))
797 static int align_atom_parser(struct ref_format
*format UNUSED
,
798 struct used_atom
*atom
,
799 const char *arg
, struct strbuf
*err
)
801 struct align
*align
= &atom
->u
.align
;
802 struct string_list params
= STRING_LIST_INIT_DUP
;
804 unsigned int width
= ~0U;
807 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
809 align
->position
= ALIGN_LEFT
;
811 string_list_split(¶ms
, arg
, ',', -1);
812 for (i
= 0; i
< params
.nr
; i
++) {
813 const char *s
= params
.items
[i
].string
;
816 if (skip_prefix(s
, "position=", &s
)) {
817 position
= parse_align_position(s
);
819 strbuf_addf(err
, _("unrecognized position:%s"), s
);
820 string_list_clear(¶ms
, 0);
823 align
->position
= position
;
824 } else if (skip_prefix(s
, "width=", &s
)) {
825 if (strtoul_ui(s
, 10, &width
)) {
826 strbuf_addf(err
, _("unrecognized width:%s"), s
);
827 string_list_clear(¶ms
, 0);
830 } else if (!strtoul_ui(s
, 10, &width
))
832 else if ((position
= parse_align_position(s
)) >= 0)
833 align
->position
= position
;
835 strbuf_addf(err
, _("unrecognized %%(%s) argument: %s"), "align", s
);
836 string_list_clear(¶ms
, 0);
842 string_list_clear(¶ms
, 0);
843 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
845 align
->width
= width
;
846 string_list_clear(¶ms
, 0);
850 static int if_atom_parser(struct ref_format
*format UNUSED
,
851 struct used_atom
*atom
,
852 const char *arg
, struct strbuf
*err
)
855 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
857 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
858 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
859 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
860 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
862 return err_bad_arg(err
, "if", arg
);
866 static int rest_atom_parser(struct ref_format
*format UNUSED
,
867 struct used_atom
*atom UNUSED
,
868 const char *arg
, struct strbuf
*err
)
871 return err_no_arg(err
, "rest");
875 static int ahead_behind_atom_parser(struct ref_format
*format
,
876 struct used_atom
*atom UNUSED
,
877 const char *arg
, struct strbuf
*err
)
879 struct string_list_item
*item
;
882 return strbuf_addf_ret(err
, -1, _("expected format: %%(ahead-behind:<committish>)"));
884 item
= string_list_append(&format
->bases
, arg
);
885 item
->util
= lookup_commit_reference_by_name(arg
);
887 die("failed to find '%s'", arg
);
892 static int head_atom_parser(struct ref_format
*format UNUSED
,
893 struct used_atom
*atom
,
894 const char *arg
, struct strbuf
*err
)
897 return err_no_arg(err
, "HEAD");
898 atom
->u
.head
= refs_resolve_refdup(get_main_ref_store(the_repository
),
899 "HEAD", RESOLVE_REF_READING
, NULL
,
908 int (*parser
)(struct ref_format
*format
, struct used_atom
*atom
,
909 const char *arg
, struct strbuf
*err
);
911 [ATOM_REFNAME
] = { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
912 [ATOM_OBJECTTYPE
] = { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
913 [ATOM_OBJECTSIZE
] = { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
914 [ATOM_OBJECTNAME
] = { "objectname", SOURCE_OTHER
, FIELD_STR
, oid_atom_parser
},
915 [ATOM_DELTABASE
] = { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
916 [ATOM_TREE
] = { "tree", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
917 [ATOM_PARENT
] = { "parent", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
918 [ATOM_NUMPARENT
] = { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
919 [ATOM_OBJECT
] = { "object", SOURCE_OBJ
},
920 [ATOM_TYPE
] = { "type", SOURCE_OBJ
},
921 [ATOM_TAG
] = { "tag", SOURCE_OBJ
},
922 [ATOM_AUTHOR
] = { "author", SOURCE_OBJ
},
923 [ATOM_AUTHORNAME
] = { "authorname", SOURCE_OBJ
, FIELD_STR
, person_name_atom_parser
},
924 [ATOM_AUTHOREMAIL
] = { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
925 [ATOM_AUTHORDATE
] = { "authordate", SOURCE_OBJ
, FIELD_TIME
},
926 [ATOM_COMMITTER
] = { "committer", SOURCE_OBJ
},
927 [ATOM_COMMITTERNAME
] = { "committername", SOURCE_OBJ
, FIELD_STR
, person_name_atom_parser
},
928 [ATOM_COMMITTEREMAIL
] = { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
929 [ATOM_COMMITTERDATE
] = { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
930 [ATOM_TAGGER
] = { "tagger", SOURCE_OBJ
},
931 [ATOM_TAGGERNAME
] = { "taggername", SOURCE_OBJ
, FIELD_STR
, person_name_atom_parser
},
932 [ATOM_TAGGEREMAIL
] = { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
933 [ATOM_TAGGERDATE
] = { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
934 [ATOM_CREATOR
] = { "creator", SOURCE_OBJ
},
935 [ATOM_CREATORDATE
] = { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
936 [ATOM_DESCRIBE
] = { "describe", SOURCE_OBJ
, FIELD_STR
, describe_atom_parser
},
937 [ATOM_SUBJECT
] = { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
938 [ATOM_BODY
] = { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
939 [ATOM_TRAILERS
] = { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
940 [ATOM_CONTENTS
] = { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
941 [ATOM_SIGNATURE
] = { "signature", SOURCE_OBJ
, FIELD_STR
, signature_atom_parser
},
942 [ATOM_RAW
] = { "raw", SOURCE_OBJ
, FIELD_STR
, raw_atom_parser
},
943 [ATOM_UPSTREAM
] = { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
944 [ATOM_PUSH
] = { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
945 [ATOM_SYMREF
] = { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
946 [ATOM_FLAG
] = { "flag", SOURCE_NONE
},
947 [ATOM_HEAD
] = { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
948 [ATOM_COLOR
] = { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
949 [ATOM_WORKTREEPATH
] = { "worktreepath", SOURCE_NONE
},
950 [ATOM_ALIGN
] = { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
951 [ATOM_END
] = { "end", SOURCE_NONE
},
952 [ATOM_IF
] = { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
953 [ATOM_THEN
] = { "then", SOURCE_NONE
},
954 [ATOM_ELSE
] = { "else", SOURCE_NONE
},
955 [ATOM_REST
] = { "rest", SOURCE_NONE
, FIELD_STR
, rest_atom_parser
},
956 [ATOM_AHEADBEHIND
] = { "ahead-behind", SOURCE_OTHER
, FIELD_STR
, ahead_behind_atom_parser
},
958 * Please update $__git_ref_fieldlist in git-completion.bash
959 * when you add new atoms
963 #define REF_FORMATTING_STATE_INIT { 0 }
965 struct ref_formatting_stack
{
966 struct ref_formatting_stack
*prev
;
967 struct strbuf output
;
968 void (*at_end
)(struct ref_formatting_stack
**stack
);
972 struct ref_formatting_state
{
974 struct ref_formatting_stack
*stack
;
980 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
982 uintmax_t value
; /* used for sorting when not FIELD_STR */
983 struct used_atom
*atom
;
986 #define ATOM_SIZE_UNSPECIFIED (-1)
988 #define ATOM_VALUE_INIT { \
989 .s_size = ATOM_SIZE_UNSPECIFIED \
993 * Used to parse format string and sort specifiers
995 static int parse_ref_filter_atom(struct ref_format
*format
,
996 const char *atom
, const char *ep
,
1001 int i
, at
, atom_len
;
1004 if (*sp
== '*' && sp
< ep
)
1007 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
1008 (int)(ep
-atom
), atom
);
1011 * If the atom name has a colon, strip it and everything after
1012 * it off - it specifies the format for this entry, and
1013 * shouldn't be used for checking against the valid_atom
1016 arg
= memchr(sp
, ':', ep
- sp
);
1017 atom_len
= (arg
? arg
: ep
) - sp
;
1019 /* Do we have the atom already used elsewhere? */
1020 for (i
= 0; i
< used_atom_cnt
; i
++) {
1021 int len
= strlen(used_atom
[i
].name
);
1022 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
1026 /* Is the atom a valid one? */
1027 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
1028 int len
= strlen(valid_atom
[i
].name
);
1029 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
1033 if (ARRAY_SIZE(valid_atom
) <= i
)
1034 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
1035 (int)(ep
-atom
), atom
);
1036 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
1037 return strbuf_addf_ret(err
, -1,
1038 _("not a git repository, but the field '%.*s' requires access to object data"),
1039 (int)(ep
-atom
), atom
);
1041 /* Add it in, including the deref prefix */
1044 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
1045 used_atom
[at
].atom_type
= i
;
1046 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
1047 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
1048 used_atom
[at
].source
= valid_atom
[i
].source
;
1049 if (used_atom
[at
].source
== SOURCE_OBJ
) {
1051 oi_deref
.info
.contentp
= &oi_deref
.content
;
1053 oi
.info
.contentp
= &oi
.content
;
1056 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
1059 * Treat empty sub-arguments list as NULL (i.e.,
1060 * "%(atom:)" is equivalent to "%(atom)").
1065 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
1066 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
1070 if (i
== ATOM_SYMREF
)
1075 static void quote_formatting(struct strbuf
*s
, const char *str
, ssize_t len
, int quote_style
)
1077 switch (quote_style
) {
1080 strbuf_addstr(s
, str
);
1082 strbuf_add(s
, str
, len
);
1085 sq_quote_buf(s
, str
);
1089 perl_quote_buf(s
, str
);
1091 perl_quote_buf_with_len(s
, str
, len
);
1094 python_quote_buf(s
, str
);
1097 tcl_quote_buf(s
, str
);
1102 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
1103 struct strbuf
*err UNUSED
)
1106 * Quote formatting is only done when the stack has a single
1107 * element. Otherwise quote formatting is done on the
1108 * element's entire output strbuf when the %(end) atom is
1111 if (!state
->stack
->prev
)
1112 quote_formatting(&state
->stack
->output
, v
->s
, v
->s_size
, state
->quote_style
);
1113 else if (v
->s_size
< 0)
1114 strbuf_addstr(&state
->stack
->output
, v
->s
);
1116 strbuf_add(&state
->stack
->output
, v
->s
, v
->s_size
);
1120 static void push_stack_element(struct ref_formatting_stack
**stack
)
1122 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
1124 strbuf_init(&s
->output
, 0);
1129 static void pop_stack_element(struct ref_formatting_stack
**stack
)
1131 struct ref_formatting_stack
*current
= *stack
;
1132 struct ref_formatting_stack
*prev
= current
->prev
;
1135 strbuf_addbuf(&prev
->output
, ¤t
->output
);
1136 strbuf_release(¤t
->output
);
1141 static void end_align_handler(struct ref_formatting_stack
**stack
)
1143 struct ref_formatting_stack
*cur
= *stack
;
1144 struct align
*align
= (struct align
*)cur
->at_end_data
;
1145 struct strbuf s
= STRBUF_INIT
;
1147 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
1148 strbuf_swap(&cur
->output
, &s
);
1152 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
1153 struct strbuf
*err UNUSED
)
1155 struct ref_formatting_stack
*new_stack
;
1157 push_stack_element(&state
->stack
);
1158 new_stack
= state
->stack
;
1159 new_stack
->at_end
= end_align_handler
;
1160 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
1164 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
1166 struct ref_formatting_stack
*cur
= *stack
;
1167 struct ref_formatting_stack
*prev
= cur
->prev
;
1168 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1170 if (!if_then_else
->then_atom_seen
)
1171 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
1173 if (if_then_else
->else_atom_seen
) {
1175 * There is an %(else) atom: we need to drop one state from the
1176 * stack, either the %(else) branch if the condition is satisfied, or
1177 * the %(then) branch if it isn't.
1179 if (if_then_else
->condition_satisfied
) {
1180 strbuf_reset(&cur
->output
);
1181 pop_stack_element(&cur
);
1183 strbuf_swap(&cur
->output
, &prev
->output
);
1184 strbuf_reset(&cur
->output
);
1185 pop_stack_element(&cur
);
1187 } else if (!if_then_else
->condition_satisfied
) {
1189 * No %(else) atom: just drop the %(then) branch if the
1190 * condition is not satisfied.
1192 strbuf_reset(&cur
->output
);
1199 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
1200 struct strbuf
*err UNUSED
)
1202 struct ref_formatting_stack
*new_stack
;
1203 struct if_then_else
*if_then_else
= xcalloc(1,
1204 sizeof(struct if_then_else
));
1206 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
1207 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
1209 push_stack_element(&state
->stack
);
1210 new_stack
= state
->stack
;
1211 new_stack
->at_end
= if_then_else_handler
;
1212 new_stack
->at_end_data
= if_then_else
;
1216 static int is_empty(struct strbuf
*buf
)
1218 const char *cur
= buf
->buf
;
1219 const char *end
= buf
->buf
+ buf
->len
;
1221 while (cur
!= end
&& (isspace(*cur
)))
1227 static int then_atom_handler(struct atom_value
*atomv UNUSED
,
1228 struct ref_formatting_state
*state
,
1231 struct ref_formatting_stack
*cur
= state
->stack
;
1232 struct if_then_else
*if_then_else
= NULL
;
1235 if (cur
->at_end
== if_then_else_handler
)
1236 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1238 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
1239 if (if_then_else
->then_atom_seen
)
1240 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
1241 if (if_then_else
->else_atom_seen
)
1242 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
1243 if_then_else
->then_atom_seen
= 1;
1244 if (if_then_else
->str
)
1245 str_len
= strlen(if_then_else
->str
);
1247 * If the 'equals' or 'notequals' attribute is used then
1248 * perform the required comparison. If not, only non-empty
1249 * strings satisfy the 'if' condition.
1251 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
1252 if (str_len
== cur
->output
.len
&&
1253 !memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1254 if_then_else
->condition_satisfied
= 1;
1255 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
1256 if (str_len
!= cur
->output
.len
||
1257 memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1258 if_then_else
->condition_satisfied
= 1;
1259 } else if (cur
->output
.len
&& !is_empty(&cur
->output
))
1260 if_then_else
->condition_satisfied
= 1;
1261 strbuf_reset(&cur
->output
);
1265 static int else_atom_handler(struct atom_value
*atomv UNUSED
,
1266 struct ref_formatting_state
*state
,
1269 struct ref_formatting_stack
*prev
= state
->stack
;
1270 struct if_then_else
*if_then_else
= NULL
;
1272 if (prev
->at_end
== if_then_else_handler
)
1273 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
1275 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
1276 if (!if_then_else
->then_atom_seen
)
1277 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
1278 if (if_then_else
->else_atom_seen
)
1279 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
1280 if_then_else
->else_atom_seen
= 1;
1281 push_stack_element(&state
->stack
);
1282 state
->stack
->at_end_data
= prev
->at_end_data
;
1283 state
->stack
->at_end
= prev
->at_end
;
1287 static int end_atom_handler(struct atom_value
*atomv UNUSED
,
1288 struct ref_formatting_state
*state
,
1291 struct ref_formatting_stack
*current
= state
->stack
;
1292 struct strbuf s
= STRBUF_INIT
;
1294 if (!current
->at_end
)
1295 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
1296 current
->at_end(&state
->stack
);
1298 /* Stack may have been popped within at_end(), hence reset the current pointer */
1299 current
= state
->stack
;
1302 * Perform quote formatting when the stack element is that of
1303 * a supporting atom. If nested then perform quote formatting
1304 * only on the topmost supporting atom.
1306 if (!current
->prev
->prev
) {
1307 quote_formatting(&s
, current
->output
.buf
, current
->output
.len
, state
->quote_style
);
1308 strbuf_swap(¤t
->output
, &s
);
1311 pop_stack_element(&state
->stack
);
1316 * In a format string, find the next occurrence of %(atom).
1318 static const char *find_next(const char *cp
)
1323 * %( is the start of an atom;
1324 * %% is a quoted per-cent.
1328 else if (cp
[1] == '%')
1329 cp
++; /* skip over two % */
1330 /* otherwise this is a singleton, literal % */
1337 static int reject_atom(enum atom_type atom_type
)
1339 return atom_type
== ATOM_REST
;
1343 * Make sure the format string is well formed, and parse out
1346 int verify_ref_format(struct ref_format
*format
)
1348 const char *cp
, *sp
;
1350 format
->need_color_reset_at_eol
= 0;
1351 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
1352 struct strbuf err
= STRBUF_INIT
;
1353 const char *color
, *ep
= strchr(sp
, ')');
1357 return error(_("malformed format string %s"), sp
);
1358 /* sp points at "%(" and ep points at the closing ")" */
1359 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
1362 if (reject_atom(used_atom
[at
].atom_type
))
1363 die(_("this command reject atom %%(%.*s)"), (int)(ep
- sp
- 2), sp
+ 2);
1365 if ((format
->quote_style
== QUOTE_PYTHON
||
1366 format
->quote_style
== QUOTE_SHELL
||
1367 format
->quote_style
== QUOTE_TCL
) &&
1368 used_atom
[at
].atom_type
== ATOM_RAW
&&
1369 used_atom
[at
].u
.raw_data
.option
== RAW_BARE
)
1370 die(_("--format=%.*s cannot be used with "
1371 "--python, --shell, --tcl"), (int)(ep
- sp
- 2), sp
+ 2);
1374 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
1375 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
1376 strbuf_release(&err
);
1378 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
1379 format
->need_color_reset_at_eol
= 0;
1383 static const char *do_grab_oid(const char *field
, const struct object_id
*oid
,
1384 struct used_atom
*atom
)
1386 switch (atom
->u
.oid
.option
) {
1388 return oid_to_hex(oid
);
1390 return repo_find_unique_abbrev(the_repository
, oid
,
1391 atom
->u
.oid
.length
);
1393 return repo_find_unique_abbrev(the_repository
, oid
,
1396 BUG("unknown %%(%s) option", field
);
1400 static int grab_oid(const char *name
, const char *field
, const struct object_id
*oid
,
1401 struct atom_value
*v
, struct used_atom
*atom
)
1403 if (starts_with(name
, field
)) {
1404 v
->s
= xstrdup(do_grab_oid(field
, oid
, atom
));
1410 /* See grab_values */
1411 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
1415 for (i
= 0; i
< used_atom_cnt
; i
++) {
1416 const char *name
= used_atom
[i
].name
;
1417 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1418 struct atom_value
*v
= &val
[i
];
1419 if (!!deref
!= (*name
== '*'))
1423 if (atom_type
== ATOM_OBJECTTYPE
)
1424 v
->s
= xstrdup(type_name(oi
->type
));
1425 else if (atom_type
== ATOM_OBJECTSIZE
) {
1426 if (used_atom
[i
].u
.objectsize
.option
== O_SIZE_DISK
) {
1427 v
->value
= oi
->disk_size
;
1428 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
1429 } else if (used_atom
[i
].u
.objectsize
.option
== O_SIZE
) {
1430 v
->value
= oi
->size
;
1431 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
1433 } else if (atom_type
== ATOM_DELTABASE
)
1434 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
1435 else if (atom_type
== ATOM_OBJECTNAME
&& deref
)
1436 grab_oid(name
, "objectname", &oi
->oid
, v
, &used_atom
[i
]);
1440 /* See grab_values */
1441 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1444 struct tag
*tag
= (struct tag
*) obj
;
1446 for (i
= 0; i
< used_atom_cnt
; i
++) {
1447 const char *name
= used_atom
[i
].name
;
1448 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1449 struct atom_value
*v
= &val
[i
];
1450 if (!!deref
!= (*name
== '*'))
1454 if (atom_type
== ATOM_TAG
)
1455 v
->s
= xstrdup(tag
->tag
);
1456 else if (atom_type
== ATOM_TYPE
&& tag
->tagged
)
1457 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
1458 else if (atom_type
== ATOM_OBJECT
&& tag
->tagged
)
1459 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
1463 /* See grab_values */
1464 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1467 struct commit
*commit
= (struct commit
*) obj
;
1469 for (i
= 0; i
< used_atom_cnt
; i
++) {
1470 const char *name
= used_atom
[i
].name
;
1471 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1472 struct atom_value
*v
= &val
[i
];
1473 if (!!deref
!= (*name
== '*'))
1477 if (atom_type
== ATOM_TREE
&&
1478 grab_oid(name
, "tree", get_commit_tree_oid(commit
), v
, &used_atom
[i
]))
1480 if (atom_type
== ATOM_NUMPARENT
) {
1481 v
->value
= commit_list_count(commit
->parents
);
1482 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1484 else if (atom_type
== ATOM_PARENT
) {
1485 struct commit_list
*parents
;
1486 struct strbuf s
= STRBUF_INIT
;
1487 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1488 struct object_id
*oid
= &parents
->item
->object
.oid
;
1489 if (parents
!= commit
->parents
)
1490 strbuf_addch(&s
, ' ');
1491 strbuf_addstr(&s
, do_grab_oid("parent", oid
, &used_atom
[i
]));
1493 v
->s
= strbuf_detach(&s
, NULL
);
1498 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1502 if (!strncmp(buf
, who
, wholen
) &&
1504 return buf
+ wholen
+ 1;
1505 eol
= strchr(buf
, '\n');
1510 return ""; /* end of header */
1516 static const char *copy_line(const char *buf
)
1518 const char *eol
= strchrnul(buf
, '\n');
1519 return xmemdupz(buf
, eol
- buf
);
1522 static const char *copy_name(const char *buf
)
1525 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1526 if (starts_with(cp
, " <"))
1527 return xmemdupz(buf
, cp
- buf
);
1532 static const char *find_end_of_email(const char *email
, int opt
)
1534 const char *eoemail
;
1536 if (opt
& EO_LOCALPART
) {
1537 eoemail
= strchr(email
, '@');
1540 return strchr(email
, '>');
1544 return strchr(email
, '>');
1547 * The option here is either the raw email option or the raw
1548 * mailmap option (that is EO_RAW or EO_MAILMAP). In such cases,
1549 * we directly grab the whole email including the closing
1552 * If EO_MAILMAP was set with any other option (that is either
1553 * EO_TRIM or EO_LOCALPART), we already grab the end of email
1556 eoemail
= strchr(email
, '>');
1562 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1564 const char *email
= strchr(buf
, '<');
1565 const char *eoemail
;
1566 int opt
= atom
->u
.email_option
.option
;
1571 if (opt
& (EO_LOCALPART
| EO_TRIM
))
1574 eoemail
= find_end_of_email(email
, opt
);
1577 return xmemdupz(email
, eoemail
- email
);
1580 static char *copy_subject(const char *buf
, unsigned long len
)
1582 struct strbuf sb
= STRBUF_INIT
;
1585 for (i
= 0; i
< len
; i
++) {
1586 if (buf
[i
] == '\r' && i
+ 1 < len
&& buf
[i
+ 1] == '\n')
1587 continue; /* ignore CR in CRLF */
1590 strbuf_addch(&sb
, ' ');
1592 strbuf_addch(&sb
, buf
[i
]);
1594 return strbuf_detach(&sb
, NULL
);
1597 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1599 const char *eoemail
= strstr(buf
, "> ");
1601 timestamp_t timestamp
;
1603 struct date_mode date_mode
= DATE_MODE_INIT
;
1604 const char *formatp
;
1607 * We got here because atomname ends in "date" or "date<something>";
1608 * it's not possible that <something> is not ":<format>" because
1609 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1610 * ":" means no format is specified, and use the default.
1612 formatp
= strchr(atomname
, ':');
1615 parse_date_format(formatp
, &date_mode
);
1618 * If this is a sort field and a format was specified, we'll
1619 * want to compare formatted date by string value.
1621 v
->atom
->type
= FIELD_STR
;
1626 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1627 if (timestamp
== TIME_MAX
)
1629 tz
= strtol(zone
, NULL
, 10);
1630 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1632 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
1633 v
->value
= timestamp
;
1634 date_mode_release(&date_mode
);
1641 static struct string_list mailmap
= STRING_LIST_INIT_NODUP
;
1643 /* See grab_values */
1644 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1647 int wholen
= strlen(who
);
1648 const char *wholine
= NULL
;
1649 const char *headers
[] = { "author ", "committer ",
1652 for (i
= 0; i
< used_atom_cnt
; i
++) {
1653 struct used_atom
*atom
= &used_atom
[i
];
1654 const char *name
= atom
->name
;
1655 struct atom_value
*v
= &val
[i
];
1656 struct strbuf mailmap_buf
= STRBUF_INIT
;
1658 if (!!deref
!= (*name
== '*'))
1662 if (strncmp(who
, name
, wholen
))
1664 if (name
[wholen
] != 0 &&
1665 !starts_with(name
+ wholen
, "name") &&
1666 !starts_with(name
+ wholen
, "email") &&
1667 !starts_with(name
+ wholen
, "date"))
1670 if ((starts_with(name
+ wholen
, "name") &&
1671 (atom
->u
.name_option
.option
== N_MAILMAP
)) ||
1672 (starts_with(name
+ wholen
, "email") &&
1673 (atom
->u
.email_option
.option
& EO_MAILMAP
))) {
1675 read_mailmap(&mailmap
);
1676 strbuf_addstr(&mailmap_buf
, buf
);
1677 apply_mailmap_to_header(&mailmap_buf
, headers
, &mailmap
);
1678 wholine
= find_wholine(who
, wholen
, mailmap_buf
.buf
);
1680 wholine
= find_wholine(who
, wholen
, buf
);
1684 return; /* no point looking for it */
1685 if (name
[wholen
] == 0)
1686 v
->s
= copy_line(wholine
);
1687 else if (starts_with(name
+ wholen
, "name"))
1688 v
->s
= copy_name(wholine
);
1689 else if (starts_with(name
+ wholen
, "email"))
1690 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1691 else if (starts_with(name
+ wholen
, "date"))
1692 grab_date(wholine
, v
, name
);
1694 strbuf_release(&mailmap_buf
);
1698 * For a tag or a commit object, if "creator" or "creatordate" is
1699 * requested, do something special.
1701 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1702 return; /* "author" for commit object is not wanted */
1704 wholine
= find_wholine(who
, wholen
, buf
);
1707 for (i
= 0; i
< used_atom_cnt
; i
++) {
1708 const char *name
= used_atom
[i
].name
;
1709 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1710 struct atom_value
*v
= &val
[i
];
1711 if (!!deref
!= (*name
== '*'))
1716 if (atom_type
== ATOM_CREATORDATE
)
1717 grab_date(wholine
, v
, name
);
1718 else if (atom_type
== ATOM_CREATOR
)
1719 v
->s
= copy_line(wholine
);
1723 static void grab_signature(struct atom_value
*val
, int deref
, struct object
*obj
)
1726 struct commit
*commit
= (struct commit
*) obj
;
1727 struct signature_check sigc
= { 0 };
1728 int signature_checked
= 0;
1730 for (i
= 0; i
< used_atom_cnt
; i
++) {
1731 struct used_atom
*atom
= &used_atom
[i
];
1732 const char *name
= atom
->name
;
1733 struct atom_value
*v
= &val
[i
];
1736 if (!!deref
!= (*name
== '*'))
1741 if (!skip_prefix(name
, "signature", &name
) ||
1742 (*name
&& *name
!= ':'))
1749 opt
= parse_signature_option(name
);
1753 if (!signature_checked
) {
1754 check_commit_signature(commit
, &sigc
);
1755 signature_checked
= 1;
1760 v
->s
= xstrdup(sigc
.output
? sigc
.output
: "");
1763 v
->s
= xstrdup(sigc
.signer
? sigc
.signer
: "");
1766 switch (sigc
.result
) {
1768 switch (sigc
.trust_level
) {
1769 case TRUST_UNDEFINED
:
1771 v
->s
= xstrfmt("%c", (char)'U');
1774 v
->s
= xstrfmt("%c", (char)'G');
1784 v
->s
= xstrfmt("%c", (char)sigc
.result
);
1789 v
->s
= xstrdup(sigc
.key
? sigc
.key
: "");
1792 v
->s
= xstrdup(sigc
.fingerprint
?
1793 sigc
.fingerprint
: "");
1796 v
->s
= xstrdup(sigc
.primary_key_fingerprint
?
1797 sigc
.primary_key_fingerprint
: "");
1800 v
->s
= xstrdup(gpg_trust_level_to_str(sigc
.trust_level
));
1805 if (signature_checked
)
1806 signature_check_clear(&sigc
);
1809 static void find_subpos(const char *buf
,
1810 const char **sub
, size_t *sublen
,
1811 const char **body
, size_t *bodylen
,
1813 const char **sig
, size_t *siglen
)
1815 struct strbuf payload
= STRBUF_INIT
;
1816 struct strbuf signature
= STRBUF_INIT
;
1818 const char *end
= buf
+ strlen(buf
);
1819 const char *sigstart
;
1821 /* parse signature first; we might not even have a subject line */
1822 parse_signature(buf
, end
- buf
, &payload
, &signature
);
1823 strbuf_release(&payload
);
1825 /* skip past header until we hit empty line */
1826 while (*buf
&& *buf
!= '\n') {
1827 eol
= strchrnul(buf
, '\n');
1832 /* skip any empty lines */
1833 while (*buf
== '\n')
1835 *sig
= strbuf_detach(&signature
, siglen
);
1836 sigstart
= buf
+ parse_signed_buffer(buf
, strlen(buf
));
1838 /* subject is first non-empty line */
1840 /* subject goes to first empty line before signature begins */
1841 if ((eol
= strstr(*sub
, "\n\n")) ||
1842 (eol
= strstr(*sub
, "\r\n\r\n"))) {
1843 eol
= eol
< sigstart
? eol
: sigstart
;
1845 /* treat whole message as subject */
1849 *sublen
= buf
- *sub
;
1850 /* drop trailing newline, if present */
1851 while (*sublen
&& ((*sub
)[*sublen
- 1] == '\n' ||
1852 (*sub
)[*sublen
- 1] == '\r'))
1855 /* skip any empty lines */
1856 while (*buf
== '\n' || *buf
== '\r')
1859 *bodylen
= strlen(buf
);
1860 *nonsiglen
= sigstart
- buf
;
1864 * If 'lines' is greater than 0, append that many lines from the given
1865 * 'buf' of length 'size' to the given strbuf.
1867 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1870 const char *sp
, *eol
;
1875 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1877 strbuf_addstr(out
, "\n ");
1878 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1879 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1880 strbuf_add(out
, sp
, len
);
1887 static void grab_describe_values(struct atom_value
*val
, int deref
,
1890 struct commit
*commit
= (struct commit
*)obj
;
1893 for (i
= 0; i
< used_atom_cnt
; i
++) {
1894 struct used_atom
*atom
= &used_atom
[i
];
1895 enum atom_type type
= atom
->atom_type
;
1896 const char *name
= atom
->name
;
1897 struct atom_value
*v
= &val
[i
];
1899 struct child_process cmd
= CHILD_PROCESS_INIT
;
1900 struct strbuf out
= STRBUF_INIT
;
1901 struct strbuf err
= STRBUF_INIT
;
1903 if (type
!= ATOM_DESCRIBE
)
1906 if (!!deref
!= (*name
== '*'))
1910 strvec_push(&cmd
.args
, "describe");
1911 strvec_pushv(&cmd
.args
, atom
->u
.describe_args
);
1912 strvec_push(&cmd
.args
, oid_to_hex(&commit
->object
.oid
));
1913 if (pipe_command(&cmd
, NULL
, 0, &out
, 0, &err
, 0) < 0) {
1914 error(_("failed to run 'describe'"));
1919 v
->s
= strbuf_detach(&out
, NULL
);
1921 strbuf_release(&err
);
1925 /* See grab_values */
1926 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct expand_data
*data
)
1929 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1930 size_t sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1931 void *buf
= data
->content
;
1933 for (i
= 0; i
< used_atom_cnt
; i
++) {
1934 struct used_atom
*atom
= &used_atom
[i
];
1935 const char *name
= atom
->name
;
1936 struct atom_value
*v
= &val
[i
];
1937 enum atom_type atom_type
= atom
->atom_type
;
1939 if (!!deref
!= (*name
== '*'))
1944 if (atom_type
== ATOM_RAW
) {
1945 unsigned long buf_size
= data
->size
;
1947 if (atom
->u
.raw_data
.option
== RAW_BARE
) {
1948 v
->s
= xmemdupz(buf
, buf_size
);
1949 v
->s_size
= buf_size
;
1950 } else if (atom
->u
.raw_data
.option
== RAW_LENGTH
) {
1951 v
->value
= buf_size
;
1952 v
->s
= xstrfmt("%"PRIuMAX
, v
->value
);
1957 if ((data
->type
!= OBJ_TAG
&&
1958 data
->type
!= OBJ_COMMIT
) ||
1959 (strcmp(name
, "body") &&
1960 !starts_with(name
, "subject") &&
1961 !starts_with(name
, "trailers") &&
1962 !starts_with(name
, "contents")))
1967 &bodypos
, &bodylen
, &nonsiglen
,
1970 if (atom
->u
.contents
.option
== C_SUB
)
1971 v
->s
= copy_subject(subpos
, sublen
);
1972 else if (atom
->u
.contents
.option
== C_SUB_SANITIZE
) {
1973 struct strbuf sb
= STRBUF_INIT
;
1974 format_sanitized_subject(&sb
, subpos
, sublen
);
1975 v
->s
= strbuf_detach(&sb
, NULL
);
1976 } else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1977 v
->s
= xmemdupz(bodypos
, bodylen
);
1978 else if (atom
->u
.contents
.option
== C_LENGTH
) {
1979 v
->value
= strlen(subpos
);
1980 v
->s
= xstrfmt("%"PRIuMAX
, v
->value
);
1981 } else if (atom
->u
.contents
.option
== C_BODY
)
1982 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1983 else if (atom
->u
.contents
.option
== C_SIG
)
1984 v
->s
= xmemdupz(sigpos
, siglen
);
1985 else if (atom
->u
.contents
.option
== C_LINES
) {
1986 struct strbuf s
= STRBUF_INIT
;
1987 const char *contents_end
= bodypos
+ nonsiglen
;
1989 /* Size is the length of the message after removing the signature */
1990 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1991 v
->s
= strbuf_detach(&s
, NULL
);
1992 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1993 struct strbuf s
= STRBUF_INIT
;
1995 /* Format the trailer info according to the trailer_opts given */
1996 format_trailers_from_commit(&atom
->u
.contents
.trailer_opts
, subpos
, &s
);
1998 v
->s
= strbuf_detach(&s
, NULL
);
1999 } else if (atom
->u
.contents
.option
== C_BARE
)
2000 v
->s
= xstrdup(subpos
);
2003 free((void *)sigpos
);
2007 * We want to have empty print-string for field requests
2008 * that do not apply (e.g. "authordate" for a tag object)
2010 static void fill_missing_values(struct atom_value
*val
)
2013 for (i
= 0; i
< used_atom_cnt
; i
++) {
2014 struct atom_value
*v
= &val
[i
];
2021 * val is a list of atom_value to hold returned values. Extract
2022 * the values for atoms in used_atom array out of (obj, buf, sz).
2023 * when deref is false, (obj, buf, sz) is the object that is
2024 * pointed at by the ref itself; otherwise it is the object the
2025 * ref (which is a tag) refers to.
2027 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, struct expand_data
*data
)
2029 void *buf
= data
->content
;
2031 switch (obj
->type
) {
2033 grab_tag_values(val
, deref
, obj
);
2034 grab_sub_body_contents(val
, deref
, data
);
2035 grab_person("tagger", val
, deref
, buf
);
2036 grab_describe_values(val
, deref
, obj
);
2039 grab_commit_values(val
, deref
, obj
);
2040 grab_sub_body_contents(val
, deref
, data
);
2041 grab_person("author", val
, deref
, buf
);
2042 grab_person("committer", val
, deref
, buf
);
2043 grab_signature(val
, deref
, obj
);
2044 grab_describe_values(val
, deref
, obj
);
2047 /* grab_tree_values(val, deref, obj, buf, sz); */
2048 grab_sub_body_contents(val
, deref
, data
);
2051 /* grab_blob_values(val, deref, obj, buf, sz); */
2052 grab_sub_body_contents(val
, deref
, data
);
2055 die("Eh? Object of type %d?", obj
->type
);
2059 static inline char *copy_advance(char *dst
, const char *src
)
2066 static const char *lstrip_ref_components(const char *refname
, int len
)
2068 long remaining
= len
;
2069 const char *start
= xstrdup(refname
);
2070 const char *to_free
= start
;
2074 const char *p
= refname
;
2076 /* Find total no of '/' separated path-components */
2077 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
2080 * The number of components we need to strip is now
2081 * the total minus the components to be left (Plus one
2082 * because we count the number of '/', but the number
2083 * of components is one more than the no of '/').
2085 remaining
= i
+ len
+ 1;
2088 while (remaining
> 0) {
2091 free((char *)to_free
);
2099 start
= xstrdup(start
);
2100 free((char *)to_free
);
2104 static const char *rstrip_ref_components(const char *refname
, int len
)
2106 long remaining
= len
;
2107 const char *start
= xstrdup(refname
);
2108 const char *to_free
= start
;
2112 const char *p
= refname
;
2114 /* Find total no of '/' separated path-components */
2115 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
2118 * The number of components we need to strip is now
2119 * the total minus the components to be left (Plus one
2120 * because we count the number of '/', but the number
2121 * of components is one more than the no of '/').
2123 remaining
= i
+ len
+ 1;
2126 while (remaining
-- > 0) {
2127 char *p
= strrchr(start
, '/');
2129 free((char *)to_free
);
2137 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
2139 if (atom
->option
== R_SHORT
)
2140 return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
2142 warn_ambiguous_refs
);
2143 else if (atom
->option
== R_LSTRIP
)
2144 return lstrip_ref_components(refname
, atom
->lstrip
);
2145 else if (atom
->option
== R_RSTRIP
)
2146 return rstrip_ref_components(refname
, atom
->rstrip
);
2148 return xstrdup(refname
);
2151 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
2152 struct branch
*branch
, const char **s
)
2154 int num_ours
, num_theirs
;
2155 if (atom
->u
.remote_ref
.option
== RR_REF
)
2156 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
2157 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
2158 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
2159 NULL
, atom
->u
.remote_ref
.push
,
2160 AHEAD_BEHIND_FULL
) < 0) {
2161 *s
= xstrdup(msgs
.gone
);
2162 } else if (!num_ours
&& !num_theirs
)
2165 *s
= xstrfmt(msgs
.behind
, num_theirs
);
2166 else if (!num_theirs
)
2167 *s
= xstrfmt(msgs
.ahead
, num_ours
);
2169 *s
= xstrfmt(msgs
.ahead_behind
,
2170 num_ours
, num_theirs
);
2171 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
2172 const char *to_free
= *s
;
2173 *s
= xstrfmt("[%s]", *s
);
2174 free((void *)to_free
);
2176 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
2177 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
2178 NULL
, atom
->u
.remote_ref
.push
,
2179 AHEAD_BEHIND_FULL
) < 0) {
2183 if (!num_ours
&& !num_theirs
)
2187 else if (!num_theirs
)
2191 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
2193 const char *remote
= atom
->u
.remote_ref
.push
?
2194 pushremote_for_branch(branch
, &explicit) :
2195 remote_for_branch(branch
, &explicit);
2196 *s
= xstrdup(explicit ? remote
: "");
2197 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
2200 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
2201 *s
= xstrdup(merge
? merge
: "");
2203 BUG("unhandled RR_* enum");
2206 char *get_head_description(void)
2208 struct strbuf desc
= STRBUF_INIT
;
2209 struct wt_status_state state
;
2210 memset(&state
, 0, sizeof(state
));
2211 wt_status_get_state(the_repository
, &state
, 1);
2212 if (state
.rebase_in_progress
||
2213 state
.rebase_interactive_in_progress
) {
2215 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
2218 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
2219 state
.detached_from
);
2220 } else if (state
.bisect_in_progress
)
2221 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
2222 state
.bisecting_from
);
2223 else if (state
.detached_from
) {
2224 if (state
.detached_at
)
2225 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
2226 state
.detached_from
);
2228 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
2229 state
.detached_from
);
2231 strbuf_addstr(&desc
, _("(no branch)"));
2233 wt_status_state_free_buffers(&state
);
2235 return strbuf_detach(&desc
, NULL
);
2238 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
2243 return show_ref(&atom
->u
.refname
, ref
->symref
);
2246 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
2248 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
2249 return get_head_description();
2250 return show_ref(&atom
->u
.refname
, ref
->refname
);
2253 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
2254 struct expand_data
*oi
, struct strbuf
*err
)
2256 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
2258 if (oi
->info
.contentp
) {
2259 /* We need to know that to use parse_object_buffer properly */
2260 oi
->info
.sizep
= &oi
->size
;
2261 oi
->info
.typep
= &oi
->type
;
2263 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
2264 OBJECT_INFO_LOOKUP_REPLACE
))
2265 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2266 oid_to_hex(&oi
->oid
), ref
->refname
);
2267 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
2268 BUG("Object size is less than zero.");
2270 if (oi
->info
.contentp
) {
2271 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
2275 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
2276 oid_to_hex(&oi
->oid
), ref
->refname
);
2278 grab_values(ref
->value
, deref
, *obj
, oi
);
2281 grab_common_values(ref
->value
, deref
, oi
);
2287 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
2291 for (i
= 0; worktrees
[i
]; i
++) {
2292 if (worktrees
[i
]->head_ref
) {
2293 struct ref_to_worktree_entry
*entry
;
2294 entry
= xmalloc(sizeof(*entry
));
2295 entry
->wt
= worktrees
[i
];
2296 hashmap_entry_init(&entry
->ent
,
2297 strhash(worktrees
[i
]->head_ref
));
2299 hashmap_add(map
, &entry
->ent
);
2304 static void lazy_init_worktree_map(void)
2306 if (ref_to_worktree_map
.worktrees
)
2309 ref_to_worktree_map
.worktrees
= get_worktrees();
2310 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
2311 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
2314 static char *get_worktree_path(const struct ref_array_item
*ref
)
2316 struct hashmap_entry entry
, *e
;
2317 struct ref_to_worktree_entry
*lookup_result
;
2319 lazy_init_worktree_map();
2321 hashmap_entry_init(&entry
, strhash(ref
->refname
));
2322 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
2327 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
2329 return xstrdup(lookup_result
->wt
->path
);
2333 * Parse the object referred by ref, and grab needed value.
2335 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
2339 struct object_info empty
= OBJECT_INFO_INIT
;
2340 int ahead_behind_atoms
= 0;
2342 CALLOC_ARRAY(ref
->value
, used_atom_cnt
);
2344 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
2345 ref
->symref
= refs_resolve_refdup(get_main_ref_store(the_repository
),
2347 RESOLVE_REF_READING
,
2350 ref
->symref
= xstrdup("");
2353 /* Fill in specials first */
2354 for (i
= 0; i
< used_atom_cnt
; i
++) {
2355 struct used_atom
*atom
= &used_atom
[i
];
2356 enum atom_type atom_type
= atom
->atom_type
;
2357 const char *name
= used_atom
[i
].name
;
2358 struct atom_value
*v
= &ref
->value
[i
];
2360 const char *refname
;
2361 struct branch
*branch
= NULL
;
2363 v
->s_size
= ATOM_SIZE_UNSPECIFIED
;
2364 v
->handler
= append_atom
;
2373 if (atom_type
== ATOM_REFNAME
)
2374 refname
= get_refname(atom
, ref
);
2375 else if (atom_type
== ATOM_WORKTREEPATH
) {
2376 if (ref
->kind
== FILTER_REFS_BRANCHES
)
2377 v
->s
= get_worktree_path(ref
);
2382 else if (atom_type
== ATOM_SYMREF
)
2383 refname
= get_symref(atom
, ref
);
2384 else if (atom_type
== ATOM_UPSTREAM
) {
2385 const char *branch_name
;
2386 /* only local branches may have an upstream */
2387 if (!skip_prefix(ref
->refname
, "refs/heads/",
2392 branch
= branch_get(branch_name
);
2394 refname
= branch_get_upstream(branch
, NULL
);
2396 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2400 } else if (atom_type
== ATOM_PUSH
&& atom
->u
.remote_ref
.push
) {
2401 const char *branch_name
;
2403 if (!skip_prefix(ref
->refname
, "refs/heads/",
2406 branch
= branch_get(branch_name
);
2408 if (atom
->u
.remote_ref
.push_remote
)
2411 refname
= branch_get_push(branch
, NULL
);
2415 /* We will definitely re-init v->s on the next line. */
2417 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2419 } else if (atom_type
== ATOM_COLOR
) {
2420 v
->s
= xstrdup(atom
->u
.color
);
2422 } else if (atom_type
== ATOM_FLAG
) {
2423 char buf
[256], *cp
= buf
;
2424 if (ref
->flag
& REF_ISSYMREF
)
2425 cp
= copy_advance(cp
, ",symref");
2426 if (ref
->flag
& REF_ISPACKED
)
2427 cp
= copy_advance(cp
, ",packed");
2432 v
->s
= xstrdup(buf
+ 1);
2435 } else if (!deref
&& atom_type
== ATOM_OBJECTNAME
&&
2436 grab_oid(name
, "objectname", &ref
->objectname
, v
, atom
)) {
2438 } else if (atom_type
== ATOM_HEAD
) {
2439 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
2440 v
->s
= xstrdup("*");
2442 v
->s
= xstrdup(" ");
2444 } else if (atom_type
== ATOM_ALIGN
) {
2445 v
->handler
= align_atom_handler
;
2448 } else if (atom_type
== ATOM_END
) {
2449 v
->handler
= end_atom_handler
;
2452 } else if (atom_type
== ATOM_IF
) {
2454 if (skip_prefix(name
, "if:", &s
))
2458 v
->handler
= if_atom_handler
;
2460 } else if (atom_type
== ATOM_THEN
) {
2461 v
->handler
= then_atom_handler
;
2464 } else if (atom_type
== ATOM_ELSE
) {
2465 v
->handler
= else_atom_handler
;
2468 } else if (atom_type
== ATOM_REST
) {
2470 v
->s
= xstrdup(ref
->rest
);
2474 } else if (atom_type
== ATOM_AHEADBEHIND
) {
2476 const struct ahead_behind_count
*count
;
2477 count
= ref
->counts
[ahead_behind_atoms
++];
2478 v
->s
= xstrfmt("%d %d", count
->ahead
, count
->behind
);
2488 v
->s
= xstrdup(refname
);
2490 v
->s
= xstrfmt("%s^{}", refname
);
2491 free((char *)refname
);
2494 for (i
= 0; i
< used_atom_cnt
; i
++) {
2495 struct atom_value
*v
= &ref
->value
[i
];
2496 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
2497 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2498 oid_to_hex(&ref
->objectname
), ref
->refname
);
2502 oi
.info
.contentp
= &oi
.content
;
2503 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
2504 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
2508 oi
.oid
= ref
->objectname
;
2509 if (get_object(ref
, 0, &obj
, &oi
, err
))
2513 * If there is no atom that wants to know about tagged
2514 * object, we are done.
2516 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
2520 * If it is a tag object, see if we use the peeled value. If we do,
2521 * grab the peeled OID.
2523 if (need_tagged
&& peel_iterated_oid(&obj
->oid
, &oi_deref
.oid
))
2526 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
2530 * Given a ref, return the value for the atom. This lazily gets value
2531 * out of the object by calling populate value.
2533 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
2534 struct atom_value
**v
, struct strbuf
*err
)
2537 if (populate_value(ref
, err
))
2539 fill_missing_values(ref
->value
);
2541 *v
= &ref
->value
[atom
];
2546 * Return 1 if the refname matches one of the patterns, otherwise 0.
2547 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2548 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2549 * matches "refs/heads/mas*", too).
2551 static int match_pattern(const char **patterns
, const char *refname
,
2557 flags
|= WM_CASEFOLD
;
2560 * When no '--format' option is given we need to skip the prefix
2561 * for matching refs of tags and branches.
2563 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
2564 skip_prefix(refname
, "refs/heads/", &refname
) ||
2565 skip_prefix(refname
, "refs/remotes/", &refname
) ||
2566 skip_prefix(refname
, "refs/", &refname
));
2568 for (; *patterns
; patterns
++) {
2569 if (!wildmatch(*patterns
, refname
, flags
))
2576 * Return 1 if the refname matches one of the patterns, otherwise 0.
2577 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
2578 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2579 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2581 static int match_name_as_path(const char **pattern
, const char *refname
,
2584 int namelen
= strlen(refname
);
2585 unsigned flags
= WM_PATHNAME
;
2588 flags
|= WM_CASEFOLD
;
2590 for (; *pattern
; pattern
++) {
2591 const char *p
= *pattern
;
2592 int plen
= strlen(p
);
2594 if ((plen
<= namelen
) &&
2595 !strncmp(refname
, p
, plen
) &&
2596 (refname
[plen
] == '\0' ||
2597 refname
[plen
] == '/' ||
2600 if (!wildmatch(p
, refname
, flags
))
2606 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
2607 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
2609 if (!*filter
->name_patterns
)
2610 return 1; /* No pattern always matches */
2611 if (filter
->match_as_path
)
2612 return match_name_as_path(filter
->name_patterns
, refname
,
2613 filter
->ignore_case
);
2614 return match_pattern(filter
->name_patterns
, refname
,
2615 filter
->ignore_case
);
2618 static int filter_exclude_match(struct ref_filter
*filter
, const char *refname
)
2620 if (!filter
->exclude
.nr
)
2622 if (filter
->match_as_path
)
2623 return match_name_as_path(filter
->exclude
.v
, refname
,
2624 filter
->ignore_case
);
2625 return match_pattern(filter
->exclude
.v
, refname
, filter
->ignore_case
);
2629 * This is the same as for_each_fullref_in(), but it tries to iterate
2630 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2631 * pattern match, so the callback still has to match each ref individually.
2633 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
2637 if (filter
->kind
& FILTER_REFS_ROOT_REFS
) {
2638 /* In this case, we want to print all refs including root refs. */
2639 return refs_for_each_include_root_refs(get_main_ref_store(the_repository
),
2643 if (!filter
->match_as_path
) {
2645 * in this case, the patterns are applied after
2646 * prefixes like "refs/heads/" etc. are stripped off,
2647 * so we have to look at everything:
2649 return refs_for_each_fullref_in(get_main_ref_store(the_repository
),
2650 "", NULL
, cb
, cb_data
);
2653 if (filter
->ignore_case
) {
2655 * we can't handle case-insensitive comparisons,
2656 * so just return everything and let the caller
2659 return refs_for_each_fullref_in(get_main_ref_store(the_repository
),
2660 "", NULL
, cb
, cb_data
);
2663 if (!filter
->name_patterns
[0]) {
2664 /* no patterns; we have to look at everything */
2665 return refs_for_each_fullref_in(get_main_ref_store(the_repository
),
2666 "", filter
->exclude
.v
, cb
, cb_data
);
2669 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository
),
2670 NULL
, filter
->name_patterns
,
2676 * Given a ref (oid, refname), check if the ref belongs to the array
2677 * of oids. If the given ref is a tag, check if the given tag points
2678 * at one of the oids in the given oid array. Returns non-zero if a
2682 * As the refs are cached we might know what refname peels to without
2683 * the need to parse the object via parse_object(). peel_ref() might be a
2684 * more efficient alternative to obtain the pointee.
2686 static int match_points_at(struct oid_array
*points_at
,
2687 const struct object_id
*oid
,
2688 const char *refname
)
2692 if (oid_array_lookup(points_at
, oid
) >= 0)
2694 obj
= parse_object_with_flags(the_repository
, oid
,
2695 PARSE_OBJECT_SKIP_HASH_CHECK
);
2696 while (obj
&& obj
->type
== OBJ_TAG
) {
2697 struct tag
*tag
= (struct tag
*)obj
;
2699 if (parse_tag(tag
) < 0) {
2704 if (oid_array_lookup(points_at
, get_tagged_oid(tag
)) >= 0)
2710 die(_("malformed object at '%s'"), refname
);
2715 * Allocate space for a new ref_array_item and copy the name and oid to it.
2717 * Callers can then fill in other struct members at their leisure.
2719 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2720 const struct object_id
*oid
)
2722 struct ref_array_item
*ref
;
2724 FLEX_ALLOC_STR(ref
, refname
, refname
);
2725 oidcpy(&ref
->objectname
, oid
);
2731 static void ref_array_append(struct ref_array
*array
, struct ref_array_item
*ref
)
2733 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2734 array
->items
[array
->nr
++] = ref
;
2737 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2738 const char *refname
,
2739 const struct object_id
*oid
)
2741 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2742 ref_array_append(array
, ref
);
2746 static int ref_kind_from_refname(const char *refname
)
2754 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2755 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2756 { "refs/tags/", FILTER_REFS_TAGS
}
2759 if (!strcmp(refname
, "HEAD"))
2760 return FILTER_REFS_DETACHED_HEAD
;
2762 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2763 if (starts_with(refname
, ref_kind
[i
].prefix
))
2764 return ref_kind
[i
].kind
;
2767 if (is_pseudo_ref(refname
))
2768 return FILTER_REFS_PSEUDOREFS
;
2769 if (is_root_ref(refname
))
2770 return FILTER_REFS_ROOT_REFS
;
2772 return FILTER_REFS_OTHERS
;
2775 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2777 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2778 filter
->kind
== FILTER_REFS_REMOTES
||
2779 filter
->kind
== FILTER_REFS_TAGS
)
2780 return filter
->kind
;
2781 return ref_kind_from_refname(refname
);
2784 static struct ref_array_item
*apply_ref_filter(const char *refname
, const struct object_id
*oid
,
2785 int flag
, struct ref_filter
*filter
)
2787 struct ref_array_item
*ref
;
2788 struct commit
*commit
= NULL
;
2791 if (flag
& REF_BAD_NAME
) {
2792 warning(_("ignoring ref with broken name %s"), refname
);
2796 if (flag
& REF_ISBROKEN
) {
2797 warning(_("ignoring broken ref %s"), refname
);
2801 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2802 kind
= filter_ref_kind(filter
, refname
);
2805 * Generally HEAD refs are printed with special description denoting a rebase,
2806 * detached state and so forth. This is useful when only printing the HEAD ref
2807 * But when it is being printed along with other root refs, it makes sense to
2808 * keep the formatting consistent. So we mask the type to act like a root ref.
2810 if (filter
->kind
& FILTER_REFS_ROOT_REFS
&& kind
== FILTER_REFS_DETACHED_HEAD
)
2811 kind
= FILTER_REFS_ROOT_REFS
;
2812 else if (!(kind
& filter
->kind
))
2815 if (!filter_pattern_match(filter
, refname
))
2818 if (filter_exclude_match(filter
, refname
))
2821 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2825 * A merge filter is applied on refs pointing to commits. Hence
2826 * obtain the commit using the 'oid' available and discard all
2827 * non-commits early. The actual filtering is done later.
2829 if (filter
->reachable_from
|| filter
->unreachable_from
||
2830 filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2831 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
2834 /* We perform the filtering for the '--contains' option... */
2835 if (filter
->with_commit
&&
2836 !commit_contains(filter
, commit
, filter
->with_commit
, &filter
->internal
.contains_cache
))
2838 /* ...or for the `--no-contains' option */
2839 if (filter
->no_commit
&&
2840 commit_contains(filter
, commit
, filter
->no_commit
, &filter
->internal
.no_contains_cache
))
2845 * We do not open the object yet; sort may only need refname
2846 * to do its job and the resulting list may yet to be pruned
2847 * by maxcount logic.
2849 ref
= new_ref_array_item(refname
, oid
);
2850 ref
->commit
= commit
;
2857 struct ref_filter_cbdata
{
2858 struct ref_array
*array
;
2859 struct ref_filter
*filter
;
2863 * A call-back given to for_each_ref(). Filter refs and keep them for
2864 * later object processing.
2866 static int filter_one(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2868 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2869 struct ref_array_item
*ref
;
2871 ref
= apply_ref_filter(refname
, oid
, flag
, ref_cbdata
->filter
);
2873 ref_array_append(ref_cbdata
->array
, ref
);
2878 /* Free memory allocated for a ref_array_item */
2879 static void free_array_item(struct ref_array_item
*item
)
2881 free((char *)item
->symref
);
2884 for (i
= 0; i
< used_atom_cnt
; i
++)
2885 free((char *)item
->value
[i
].s
);
2892 struct ref_filter_and_format_cbdata
{
2893 struct ref_filter
*filter
;
2894 struct ref_format
*format
;
2896 struct ref_filter_and_format_internal
{
2901 static int filter_and_format_one(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2903 struct ref_filter_and_format_cbdata
*ref_cbdata
= cb_data
;
2904 struct ref_array_item
*ref
;
2905 struct strbuf output
= STRBUF_INIT
, err
= STRBUF_INIT
;
2907 ref
= apply_ref_filter(refname
, oid
, flag
, ref_cbdata
->filter
);
2911 if (format_ref_array_item(ref
, ref_cbdata
->format
, &output
, &err
))
2914 if (output
.len
|| !ref_cbdata
->format
->array_opts
.omit_empty
) {
2915 fwrite(output
.buf
, 1, output
.len
, stdout
);
2919 strbuf_release(&output
);
2920 strbuf_release(&err
);
2921 free_array_item(ref
);
2924 * Increment the running count of refs that match the filter. If
2925 * max_count is set and we've reached the max, stop the ref
2926 * iteration by returning a nonzero value.
2928 if (ref_cbdata
->format
->array_opts
.max_count
&&
2929 ++ref_cbdata
->internal
.count
>= ref_cbdata
->format
->array_opts
.max_count
)
2935 /* Free all memory allocated for ref_array */
2936 void ref_array_clear(struct ref_array
*array
)
2940 for (i
= 0; i
< array
->nr
; i
++)
2941 free_array_item(array
->items
[i
]);
2942 FREE_AND_NULL(array
->items
);
2943 array
->nr
= array
->alloc
= 0;
2945 for (i
= 0; i
< used_atom_cnt
; i
++) {
2946 struct used_atom
*atom
= &used_atom
[i
];
2947 if (atom
->atom_type
== ATOM_HEAD
)
2949 free((char *)atom
->name
);
2951 FREE_AND_NULL(used_atom
);
2954 if (ref_to_worktree_map
.worktrees
) {
2955 hashmap_clear_and_free(&(ref_to_worktree_map
.map
),
2956 struct ref_to_worktree_entry
, ent
);
2957 free_worktrees(ref_to_worktree_map
.worktrees
);
2958 ref_to_worktree_map
.worktrees
= NULL
;
2961 FREE_AND_NULL(array
->counts
);
2964 #define EXCLUDE_REACHED 0
2965 #define INCLUDE_REACHED 1
2966 static void reach_filter(struct ref_array
*array
,
2967 struct commit_list
**check_reachable
,
2968 int include_reached
)
2971 struct commit
**to_clear
;
2973 if (!*check_reachable
)
2976 CALLOC_ARRAY(to_clear
, array
->nr
);
2977 for (i
= 0; i
< array
->nr
; i
++) {
2978 struct ref_array_item
*item
= array
->items
[i
];
2979 to_clear
[i
] = item
->commit
;
2982 tips_reachable_from_bases(the_repository
,
2984 to_clear
, array
->nr
,
2990 for (i
= 0; i
< old_nr
; i
++) {
2991 struct ref_array_item
*item
= array
->items
[i
];
2992 struct commit
*commit
= item
->commit
;
2994 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2996 if (is_merged
== include_reached
)
2997 array
->items
[array
->nr
++] = array
->items
[i
];
2999 free_array_item(item
);
3002 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
3004 while (*check_reachable
) {
3005 struct commit
*merge_commit
= pop_commit(check_reachable
);
3006 clear_commit_marks(merge_commit
, ALL_REV_FLAGS
);
3012 void filter_ahead_behind(struct repository
*r
,
3013 struct ref_format
*format
,
3014 struct ref_array
*array
)
3016 struct commit
**commits
;
3017 size_t commits_nr
= format
->bases
.nr
+ array
->nr
;
3019 if (!format
->bases
.nr
|| !array
->nr
)
3022 ALLOC_ARRAY(commits
, commits_nr
);
3023 for (size_t i
= 0; i
< format
->bases
.nr
; i
++)
3024 commits
[i
] = format
->bases
.items
[i
].util
;
3026 ALLOC_ARRAY(array
->counts
, st_mult(format
->bases
.nr
, array
->nr
));
3028 commits_nr
= format
->bases
.nr
;
3029 array
->counts_nr
= 0;
3030 for (size_t i
= 0; i
< array
->nr
; i
++) {
3031 const char *name
= array
->items
[i
]->refname
;
3032 commits
[commits_nr
] = lookup_commit_reference_by_name(name
);
3034 if (!commits
[commits_nr
])
3037 CALLOC_ARRAY(array
->items
[i
]->counts
, format
->bases
.nr
);
3038 for (size_t j
= 0; j
< format
->bases
.nr
; j
++) {
3039 struct ahead_behind_count
*count
;
3040 count
= &array
->counts
[array
->counts_nr
++];
3041 count
->tip_index
= commits_nr
;
3042 count
->base_index
= j
;
3044 array
->items
[i
]->counts
[j
] = count
;
3049 ahead_behind(r
, commits
, commits_nr
, array
->counts
, array
->counts_nr
);
3053 static int do_filter_refs(struct ref_filter
*filter
, unsigned int type
, each_ref_fn fn
, void *cb_data
)
3057 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
3059 init_contains_cache(&filter
->internal
.contains_cache
);
3060 init_contains_cache(&filter
->internal
.no_contains_cache
);
3062 /* Simple per-ref filtering */
3064 die("filter_refs: invalid type");
3067 * For common cases where we need only branches or remotes or tags,
3068 * we only iterate through those refs. If a mix of refs is needed,
3069 * we iterate over all refs and filter out required refs with the help
3070 * of filter_ref_kind().
3072 if (filter
->kind
== FILTER_REFS_BRANCHES
)
3073 ret
= refs_for_each_fullref_in(get_main_ref_store(the_repository
),
3074 "refs/heads/", NULL
,
3076 else if (filter
->kind
== FILTER_REFS_REMOTES
)
3077 ret
= refs_for_each_fullref_in(get_main_ref_store(the_repository
),
3078 "refs/remotes/", NULL
,
3080 else if (filter
->kind
== FILTER_REFS_TAGS
)
3081 ret
= refs_for_each_fullref_in(get_main_ref_store(the_repository
),
3082 "refs/tags/", NULL
, fn
,
3084 else if (filter
->kind
& FILTER_REFS_REGULAR
)
3085 ret
= for_each_fullref_in_pattern(filter
, fn
, cb_data
);
3088 * When printing all ref types, HEAD is already included,
3089 * so we don't want to print HEAD again.
3091 if (!ret
&& !(filter
->kind
& FILTER_REFS_ROOT_REFS
) &&
3092 (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
3093 refs_head_ref(get_main_ref_store(the_repository
), fn
,
3097 clear_contains_cache(&filter
->internal
.contains_cache
);
3098 clear_contains_cache(&filter
->internal
.no_contains_cache
);
3104 * API for filtering a set of refs. Based on the type of refs the user
3105 * has requested, we iterate through those refs and apply filters
3106 * as per the given ref_filter structure and finally store the
3107 * filtered refs in the ref_array structure.
3109 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
3111 struct ref_filter_cbdata ref_cbdata
;
3112 int save_commit_buffer_orig
;
3115 ref_cbdata
.array
= array
;
3116 ref_cbdata
.filter
= filter
;
3118 save_commit_buffer_orig
= save_commit_buffer
;
3119 save_commit_buffer
= 0;
3121 ret
= do_filter_refs(filter
, type
, filter_one
, &ref_cbdata
);
3123 /* Filters that need revision walking */
3124 reach_filter(array
, &filter
->reachable_from
, INCLUDE_REACHED
);
3125 reach_filter(array
, &filter
->unreachable_from
, EXCLUDE_REACHED
);
3127 save_commit_buffer
= save_commit_buffer_orig
;
3131 static inline int can_do_iterative_format(struct ref_filter
*filter
,
3132 struct ref_sorting
*sorting
,
3133 struct ref_format
*format
)
3136 * Filtering & formatting results within a single ref iteration
3137 * callback is not compatible with options that require
3138 * post-processing a filtered ref_array. These include:
3139 * - filtering on reachability
3140 * - sorting the filtered results
3141 * - including ahead-behind information in the formatted output
3143 return !(filter
->reachable_from
||
3144 filter
->unreachable_from
||
3149 void filter_and_format_refs(struct ref_filter
*filter
, unsigned int type
,
3150 struct ref_sorting
*sorting
,
3151 struct ref_format
*format
)
3153 if (can_do_iterative_format(filter
, sorting
, format
)) {
3154 int save_commit_buffer_orig
;
3155 struct ref_filter_and_format_cbdata ref_cbdata
= {
3160 save_commit_buffer_orig
= save_commit_buffer
;
3161 save_commit_buffer
= 0;
3163 do_filter_refs(filter
, type
, filter_and_format_one
, &ref_cbdata
);
3165 save_commit_buffer
= save_commit_buffer_orig
;
3167 struct ref_array array
= { 0 };
3168 filter_refs(&array
, filter
, type
);
3169 filter_ahead_behind(the_repository
, format
, &array
);
3170 ref_array_sort(sorting
, &array
);
3171 print_formatted_ref_array(&array
, format
);
3172 ref_array_clear(&array
);
3176 static int compare_detached_head(struct ref_array_item
*a
, struct ref_array_item
*b
)
3178 if (!(a
->kind
^ b
->kind
))
3179 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
3180 if (a
->kind
& FILTER_REFS_DETACHED_HEAD
)
3182 else if (b
->kind
& FILTER_REFS_DETACHED_HEAD
)
3184 BUG("should have died in the xor check above");
3188 static int memcasecmp(const void *vs1
, const void *vs2
, size_t n
)
3190 const char *s1
= vs1
, *s2
= vs2
;
3191 const char *end
= s1
+ n
;
3193 for (; s1
< end
; s1
++, s2
++) {
3194 int diff
= tolower(*s1
) - tolower(*s2
);
3201 struct ref_sorting
{
3202 struct ref_sorting
*next
;
3203 int atom
; /* index into used_atom array (internal) */
3204 enum ref_sorting_order sort_flags
;
3207 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
3209 struct atom_value
*va
, *vb
;
3211 int cmp_detached_head
= 0;
3212 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
3213 struct strbuf err
= STRBUF_INIT
;
3215 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
3217 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
3219 strbuf_release(&err
);
3220 if (s
->sort_flags
& REF_SORTING_DETACHED_HEAD_FIRST
&&
3221 ((a
->kind
| b
->kind
) & FILTER_REFS_DETACHED_HEAD
)) {
3222 cmp
= compare_detached_head(a
, b
);
3223 cmp_detached_head
= 1;
3224 } else if (s
->sort_flags
& REF_SORTING_VERSION
) {
3225 cmp
= versioncmp(va
->s
, vb
->s
);
3226 } else if (cmp_type
== FIELD_STR
) {
3227 if (va
->s_size
< 0 && vb
->s_size
< 0) {
3228 int (*cmp_fn
)(const char *, const char *);
3229 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
3230 ? strcasecmp
: strcmp
;
3231 cmp
= cmp_fn(va
->s
, vb
->s
);
3233 size_t a_size
= va
->s_size
< 0 ?
3234 strlen(va
->s
) : va
->s_size
;
3235 size_t b_size
= vb
->s_size
< 0 ?
3236 strlen(vb
->s
) : vb
->s_size
;
3237 int (*cmp_fn
)(const void *, const void *, size_t);
3238 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
3239 ? memcasecmp
: memcmp
;
3241 cmp
= cmp_fn(va
->s
, vb
->s
, b_size
> a_size
?
3244 if (a_size
> b_size
)
3246 else if (a_size
< b_size
)
3251 if (va
->value
< vb
->value
)
3253 else if (va
->value
== vb
->value
)
3259 return (s
->sort_flags
& REF_SORTING_REVERSE
&& !cmp_detached_head
)
3263 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
3265 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
3266 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
3267 struct ref_sorting
*s
;
3269 for (s
= ref_sorting
; s
; s
= s
->next
) {
3270 int cmp
= cmp_ref_sorting(s
, a
, b
);
3275 return s
&& s
->sort_flags
& REF_SORTING_ICASE
?
3276 strcasecmp(a
->refname
, b
->refname
) :
3277 strcmp(a
->refname
, b
->refname
);
3280 void ref_sorting_set_sort_flags_all(struct ref_sorting
*sorting
,
3281 unsigned int mask
, int on
)
3283 for (; sorting
; sorting
= sorting
->next
) {
3285 sorting
->sort_flags
|= mask
;
3287 sorting
->sort_flags
&= ~mask
;
3291 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
3294 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
3297 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
3299 struct strbuf
*s
= &state
->stack
->output
;
3301 while (*cp
&& (!ep
|| cp
< ep
)) {
3306 int ch
= hex2chr(cp
+ 1);
3308 strbuf_addch(s
, ch
);
3314 strbuf_addch(s
, *cp
);
3319 int format_ref_array_item(struct ref_array_item
*info
,
3320 struct ref_format
*format
,
3321 struct strbuf
*final_buf
,
3322 struct strbuf
*error_buf
)
3324 const char *cp
, *sp
, *ep
;
3325 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
3327 state
.quote_style
= format
->quote_style
;
3328 push_stack_element(&state
.stack
);
3330 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
3331 struct atom_value
*atomv
;
3334 ep
= strchr(sp
, ')');
3336 append_literal(cp
, sp
, &state
);
3337 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
3338 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
3339 atomv
->handler(atomv
, &state
, error_buf
)) {
3340 pop_stack_element(&state
.stack
);
3345 sp
= cp
+ strlen(cp
);
3346 append_literal(cp
, sp
, &state
);
3348 if (format
->need_color_reset_at_eol
) {
3349 struct atom_value resetv
= ATOM_VALUE_INIT
;
3350 resetv
.s
= GIT_COLOR_RESET
;
3351 if (append_atom(&resetv
, &state
, error_buf
)) {
3352 pop_stack_element(&state
.stack
);
3356 if (state
.stack
->prev
) {
3357 pop_stack_element(&state
.stack
);
3358 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
3360 strbuf_addbuf(final_buf
, &state
.stack
->output
);
3361 pop_stack_element(&state
.stack
);
3365 void print_formatted_ref_array(struct ref_array
*array
, struct ref_format
*format
)
3368 struct strbuf output
= STRBUF_INIT
, err
= STRBUF_INIT
;
3370 total
= format
->array_opts
.max_count
;
3371 if (!total
|| array
->nr
< total
)
3373 for (int i
= 0; i
< total
; i
++) {
3375 strbuf_reset(&output
);
3376 if (format_ref_array_item(array
->items
[i
], format
, &output
, &err
))
3378 if (output
.len
|| !format
->array_opts
.omit_empty
) {
3379 fwrite(output
.buf
, 1, output
.len
, stdout
);
3384 strbuf_release(&err
);
3385 strbuf_release(&output
);
3388 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
3389 struct ref_format
*format
)
3391 struct ref_array_item
*ref_item
;
3392 struct strbuf output
= STRBUF_INIT
;
3393 struct strbuf err
= STRBUF_INIT
;
3395 ref_item
= new_ref_array_item(name
, oid
);
3396 ref_item
->kind
= ref_kind_from_refname(name
);
3397 if (format_ref_array_item(ref_item
, format
, &output
, &err
))
3399 fwrite(output
.buf
, 1, output
.len
, stdout
);
3402 strbuf_release(&err
);
3403 strbuf_release(&output
);
3404 free_array_item(ref_item
);
3407 static int parse_sorting_atom(const char *atom
)
3410 * This parses an atom using a dummy ref_format, since we don't
3411 * actually care about the formatting details.
3413 struct ref_format dummy
= REF_FORMAT_INIT
;
3414 const char *end
= atom
+ strlen(atom
);
3415 struct strbuf err
= STRBUF_INIT
;
3416 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
3419 strbuf_release(&err
);
3423 static void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
3425 struct ref_sorting
*s
;
3428 s
->next
= *sorting_tail
;
3432 s
->sort_flags
|= REF_SORTING_REVERSE
;
3435 if (skip_prefix(arg
, "version:", &arg
) ||
3436 skip_prefix(arg
, "v:", &arg
))
3437 s
->sort_flags
|= REF_SORTING_VERSION
;
3438 s
->atom
= parse_sorting_atom(arg
);
3441 struct ref_sorting
*ref_sorting_options(struct string_list
*options
)
3443 struct string_list_item
*item
;
3444 struct ref_sorting
*sorting
= NULL
, **tail
= &sorting
;
3447 for_each_string_list_item(item
, options
)
3448 parse_ref_sorting(tail
, item
->string
);
3452 * From here on, the ref_sorting list should be used to talk
3453 * about the sort order used for the output. The caller
3454 * should not touch the string form anymore.
3456 string_list_clear(options
, 0);
3460 void ref_sorting_release(struct ref_sorting
*sorting
)
3463 struct ref_sorting
*next
= sorting
->next
;
3469 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
3471 struct ref_filter
*rf
= opt
->value
;
3472 struct object_id oid
;
3473 struct commit
*merge_commit
;
3475 BUG_ON_OPT_NEG(unset
);
3477 if (repo_get_oid(the_repository
, arg
, &oid
))
3478 die(_("malformed object name %s"), arg
);
3480 merge_commit
= lookup_commit_reference_gently(the_repository
, &oid
, 0);
3483 return error(_("option `%s' must point to a commit"), opt
->long_name
);
3485 if (starts_with(opt
->long_name
, "no"))
3486 commit_list_insert(merge_commit
, &rf
->unreachable_from
);
3488 commit_list_insert(merge_commit
, &rf
->reachable_from
);
3493 void ref_filter_init(struct ref_filter
*filter
)
3495 struct ref_filter blank
= REF_FILTER_INIT
;
3496 memcpy(filter
, &blank
, sizeof(blank
));
3499 void ref_filter_clear(struct ref_filter
*filter
)
3501 strvec_clear(&filter
->exclude
);
3502 oid_array_clear(&filter
->points_at
);
3503 free_commit_list(filter
->with_commit
);
3504 free_commit_list(filter
->no_commit
);
3505 free_commit_list(filter
->reachable_from
);
3506 free_commit_list(filter
->unreachable_from
);
3507 ref_filter_init(filter
);