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"
34 static struct ref_msg
{
38 const char *ahead_behind
;
40 /* Untranslated plumbing messages: */
47 void setup_ref_filter_porcelain_msg(void)
49 msgs
.gone
= _("gone");
50 msgs
.ahead
= _("ahead %d");
51 msgs
.behind
= _("behind %d");
52 msgs
.ahead_behind
= _("ahead %d, behind %d");
55 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
56 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
57 typedef enum { SOURCE_NONE
= 0, SOURCE_OBJ
, SOURCE_OTHER
} info_source
;
65 cmp_status cmp_status
;
67 unsigned int then_atom_seen
: 1,
69 condition_satisfied
: 1;
73 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
77 static struct ref_trailer_buf
{
78 struct string_list filter_list
;
80 struct strbuf kvsepbuf
;
81 } ref_trailer_buf
= {STRING_LIST_INIT_NODUP
, STRBUF_INIT
, STRBUF_INIT
};
83 static struct expand_data
{
85 enum object_type type
;
88 struct object_id delta_base_oid
;
91 struct object_info info
;
94 struct ref_to_worktree_entry
{
95 struct hashmap_entry ent
;
96 struct worktree
*wt
; /* key is wt->head_ref */
99 static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED
,
100 const struct hashmap_entry
*eptr
,
101 const struct hashmap_entry
*kptr
,
102 const void *keydata_aka_refname
)
104 const struct ref_to_worktree_entry
*e
, *k
;
106 e
= container_of(eptr
, const struct ref_to_worktree_entry
, ent
);
107 k
= container_of(kptr
, const struct ref_to_worktree_entry
, ent
);
109 return strcmp(e
->wt
->head_ref
,
110 keydata_aka_refname
? keydata_aka_refname
: k
->wt
->head_ref
);
113 static struct ref_to_worktree_map
{
115 struct worktree
**worktrees
;
116 } ref_to_worktree_map
;
119 * The enum atom_type is used as the index of valid_atom array.
120 * In the atom parsing stage, it will be passed to used_atom.atom_type
121 * as the identifier of the atom type. We can check the type of used_atom
122 * entry by `if (used_atom[i].atom_type == ATOM_*)`.
174 * An atom is a valid field atom listed below, possibly prefixed with
175 * a "*" to denote deref_tag().
177 * We parse given format string and sort specifiers, and make a list
178 * of properties that we need to extract out of objects. ref_array_item
179 * structure will hold an array of values extracted that can be
180 * indexed with the "atom number", which is an index into this
183 static struct used_atom
{
184 enum atom_type atom_type
;
189 char color
[COLOR_MAXLEN
];
193 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
195 struct refname_atom refname
;
196 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
199 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LENGTH
, C_LINES
,
200 C_SIG
, C_SUB
, C_SUB_SANITIZE
, C_TRAILERS
} option
;
201 struct process_trailer_options trailer_opts
;
205 enum { RAW_BARE
, RAW_LENGTH
} option
;
208 cmp_status cmp_status
;
212 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
216 enum { O_SIZE
, O_SIZE_DISK
} option
;
219 enum { N_RAW
, N_MAILMAP
} option
;
230 enum { S_BARE
, S_GRADE
, S_SIGNER
, S_KEY
,
231 S_FINGERPRINT
, S_PRI_KEY_FP
, S_TRUST_LEVEL
} option
;
233 const char **describe_args
;
234 struct refname_atom refname
;
238 static int used_atom_cnt
, need_tagged
, need_symref
;
241 * Expand string, append it to strbuf *sb, then return error code ret.
242 * Allow to save few lines of code.
244 __attribute__((format (printf
, 3, 4)))
245 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
249 strbuf_vaddf(sb
, fmt
, ap
);
254 static int err_no_arg(struct strbuf
*sb
, const char *name
)
256 size_t namelen
= strchrnul(name
, ':') - name
;
257 strbuf_addf(sb
, _("%%(%.*s) does not take arguments"),
262 static int err_bad_arg(struct strbuf
*sb
, const char *name
, const char *arg
)
264 size_t namelen
= strchrnul(name
, ':') - name
;
265 strbuf_addf(sb
, _("unrecognized %%(%.*s) argument: %s"),
266 (int)namelen
, name
, arg
);
271 * Parse option of name "candidate" in the option string "to_parse" of
274 * "candidate1[=val1],candidate2[=val2],candidate3[=val3],..."
276 * The remaining part of "to_parse" is stored in "end" (if we are
277 * parsing the last candidate, then this is NULL) and the value of
278 * the candidate is stored in "valuestart" and its length in "valuelen",
279 * that is the portion after "=". Since it is possible for a "candidate"
280 * to not have a value, in such cases, "valuestart" is set to point to
281 * NULL and "valuelen" to 0.
283 * The function returns 1 on success. It returns 0 if we don't find
284 * "candidate" in "to_parse" or we find "candidate" but it is followed
285 * by more chars (for example, "candidatefoo"), that is, we don't find
288 * This function only does the above for one "candidate" at a time. So
289 * it has to be called each time trying to parse a "candidate" in the
290 * option string "to_parse".
292 static int match_atom_arg_value(const char *to_parse
, const char *candidate
,
293 const char **end
, const char **valuestart
,
298 if (!skip_prefix(to_parse
, candidate
, &atom
))
299 return 0; /* definitely not "candidate" */
302 /* we just saw "candidate=" */
303 *valuestart
= atom
+ 1;
304 atom
= strchrnul(*valuestart
, ',');
305 *valuelen
= atom
- *valuestart
;
306 } else if (*atom
!= ',' && *atom
!= '\0') {
307 /* key begins with "candidate" but has more chars */
310 /* just "candidate" without "=val" */
315 /* atom points at either the ',' or NUL after this key[=val] */
319 BUG("Why is *atom not NULL yet?");
326 * Parse boolean option of name "candidate" in the option list "to_parse"
329 * "candidate1[=bool1],candidate2[=bool2],candidate3[=bool3],..."
331 * The remaining part of "to_parse" is stored in "end" (if we are parsing
332 * the last candidate, then this is NULL) and the value (if given) is
333 * parsed and stored in "val", so "val" always points to either 0 or 1.
334 * If the value is not given, then "val" is set to point to 1.
336 * The boolean value is parsed using "git_parse_maybe_bool()", so the
337 * accepted values are
339 * to set true - "1", "yes", "true"
340 * to set false - "0", "no", "false"
342 * This function returns 1 on success. It returns 0 when we don't find
343 * an exact match for "candidate" or when the boolean value given is
346 static int match_atom_bool_arg(const char *to_parse
, const char *candidate
,
347 const char **end
, int *val
)
354 if (!match_atom_arg_value(to_parse
, candidate
, end
, &argval
, &arglen
))
362 strval
= xstrndup(argval
, arglen
);
363 v
= git_parse_maybe_bool(strval
);
374 static int color_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
375 const char *color_value
, struct strbuf
*err
)
378 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
379 if (color_parse(color_value
, atom
->u
.color
) < 0)
380 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
383 * We check this after we've parsed the color, which lets us complain
384 * about syntactically bogus color names even if they won't be used.
386 if (!want_color(format
->use_color
))
387 color_parse("", atom
->u
.color
);
391 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
392 const char *name
, struct strbuf
*err
)
395 atom
->option
= R_NORMAL
;
396 else if (!strcmp(arg
, "short"))
397 atom
->option
= R_SHORT
;
398 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
399 skip_prefix(arg
, "strip=", &arg
)) {
400 atom
->option
= R_LSTRIP
;
401 if (strtol_i(arg
, 10, &atom
->lstrip
))
402 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
403 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
404 atom
->option
= R_RSTRIP
;
405 if (strtol_i(arg
, 10, &atom
->rstrip
))
406 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
408 return err_bad_arg(err
, name
, arg
);
412 static int remote_ref_atom_parser(struct ref_format
*format UNUSED
,
413 struct used_atom
*atom
,
414 const char *arg
, struct strbuf
*err
)
416 struct string_list params
= STRING_LIST_INIT_DUP
;
419 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
420 atom
->u
.remote_ref
.push
= 1;
423 atom
->u
.remote_ref
.option
= RR_REF
;
424 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
425 arg
, atom
->name
, err
);
428 atom
->u
.remote_ref
.nobracket
= 0;
429 string_list_split(¶ms
, arg
, ',', -1);
431 for (i
= 0; i
< params
.nr
; i
++) {
432 const char *s
= params
.items
[i
].string
;
434 if (!strcmp(s
, "track"))
435 atom
->u
.remote_ref
.option
= RR_TRACK
;
436 else if (!strcmp(s
, "trackshort"))
437 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
438 else if (!strcmp(s
, "nobracket"))
439 atom
->u
.remote_ref
.nobracket
= 1;
440 else if (!strcmp(s
, "remotename")) {
441 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
442 atom
->u
.remote_ref
.push_remote
= 1;
443 } else if (!strcmp(s
, "remoteref")) {
444 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
445 atom
->u
.remote_ref
.push_remote
= 1;
447 atom
->u
.remote_ref
.option
= RR_REF
;
448 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
449 arg
, atom
->name
, err
)) {
450 string_list_clear(¶ms
, 0);
456 string_list_clear(¶ms
, 0);
460 static int objecttype_atom_parser(struct ref_format
*format UNUSED
,
461 struct used_atom
*atom
,
462 const char *arg
, struct strbuf
*err
)
465 return err_no_arg(err
, "objecttype");
466 if (*atom
->name
== '*')
467 oi_deref
.info
.typep
= &oi_deref
.type
;
469 oi
.info
.typep
= &oi
.type
;
473 static int objectsize_atom_parser(struct ref_format
*format UNUSED
,
474 struct used_atom
*atom
,
475 const char *arg
, struct strbuf
*err
)
478 atom
->u
.objectsize
.option
= O_SIZE
;
479 if (*atom
->name
== '*')
480 oi_deref
.info
.sizep
= &oi_deref
.size
;
482 oi
.info
.sizep
= &oi
.size
;
483 } else if (!strcmp(arg
, "disk")) {
484 atom
->u
.objectsize
.option
= O_SIZE_DISK
;
485 if (*atom
->name
== '*')
486 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
488 oi
.info
.disk_sizep
= &oi
.disk_size
;
490 return err_bad_arg(err
, "objectsize", arg
);
494 static int deltabase_atom_parser(struct ref_format
*format UNUSED
,
495 struct used_atom
*atom
,
496 const char *arg
, struct strbuf
*err
)
499 return err_no_arg(err
, "deltabase");
500 if (*atom
->name
== '*')
501 oi_deref
.info
.delta_base_oid
= &oi_deref
.delta_base_oid
;
503 oi
.info
.delta_base_oid
= &oi
.delta_base_oid
;
507 static int body_atom_parser(struct ref_format
*format UNUSED
,
508 struct used_atom
*atom
,
509 const char *arg
, struct strbuf
*err
)
512 return err_no_arg(err
, "body");
513 atom
->u
.contents
.option
= C_BODY_DEP
;
517 static int subject_atom_parser(struct ref_format
*format UNUSED
,
518 struct used_atom
*atom
,
519 const char *arg
, struct strbuf
*err
)
522 atom
->u
.contents
.option
= C_SUB
;
523 else if (!strcmp(arg
, "sanitize"))
524 atom
->u
.contents
.option
= C_SUB_SANITIZE
;
526 return err_bad_arg(err
, "subject", arg
);
530 static int parse_signature_option(const char *arg
)
534 else if (!strcmp(arg
, "signer"))
536 else if (!strcmp(arg
, "grade"))
538 else if (!strcmp(arg
, "key"))
540 else if (!strcmp(arg
, "fingerprint"))
541 return S_FINGERPRINT
;
542 else if (!strcmp(arg
, "primarykeyfingerprint"))
544 else if (!strcmp(arg
, "trustlevel"))
545 return S_TRUST_LEVEL
;
549 static int signature_atom_parser(struct ref_format
*format UNUSED
,
550 struct used_atom
*atom
,
551 const char *arg
, struct strbuf
*err
)
553 int opt
= parse_signature_option(arg
);
555 return err_bad_arg(err
, "signature", arg
);
556 atom
->u
.signature
.option
= opt
;
560 static int trailers_atom_parser(struct ref_format
*format UNUSED
,
561 struct used_atom
*atom
,
562 const char *arg
, struct strbuf
*err
)
564 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
567 const char *argbuf
= xstrfmt("%s)", arg
);
568 char *invalid_arg
= NULL
;
570 if (format_set_trailers_options(&atom
->u
.contents
.trailer_opts
,
571 &ref_trailer_buf
.filter_list
,
572 &ref_trailer_buf
.sepbuf
,
573 &ref_trailer_buf
.kvsepbuf
,
574 &argbuf
, &invalid_arg
)) {
576 strbuf_addf(err
, _("expected %%(trailers:key=<value>)"));
578 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), invalid_arg
);
579 free((char *)invalid_arg
);
583 atom
->u
.contents
.option
= C_TRAILERS
;
587 static int contents_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
588 const char *arg
, struct strbuf
*err
)
591 atom
->u
.contents
.option
= C_BARE
;
592 else if (!strcmp(arg
, "body"))
593 atom
->u
.contents
.option
= C_BODY
;
594 else if (!strcmp(arg
, "size")) {
595 atom
->type
= FIELD_ULONG
;
596 atom
->u
.contents
.option
= C_LENGTH
;
597 } else if (!strcmp(arg
, "signature"))
598 atom
->u
.contents
.option
= C_SIG
;
599 else if (!strcmp(arg
, "subject"))
600 atom
->u
.contents
.option
= C_SUB
;
601 else if (!strcmp(arg
, "trailers")) {
602 if (trailers_atom_parser(format
, atom
, NULL
, err
))
604 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
605 if (trailers_atom_parser(format
, atom
, arg
, err
))
607 } else if (skip_prefix(arg
, "lines=", &arg
)) {
608 atom
->u
.contents
.option
= C_LINES
;
609 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
610 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
612 return err_bad_arg(err
, "contents", arg
);
616 static int describe_atom_option_parser(struct strvec
*args
, const char **arg
,
623 if (match_atom_bool_arg(*arg
, "tags", arg
, &optval
)) {
625 strvec_push(args
, "--no-tags");
627 strvec_push(args
, "--tags");
631 if (match_atom_arg_value(*arg
, "abbrev", arg
, &argval
, &arglen
)) {
635 return strbuf_addf_ret(err
, -1,
636 _("argument expected for %s"),
638 if (strtol(argval
, &endptr
, 10) < 0)
639 return strbuf_addf_ret(err
, -1,
640 _("positive value expected %s=%s"),
641 "describe:abbrev", argval
);
642 if (endptr
- argval
!= arglen
)
643 return strbuf_addf_ret(err
, -1,
644 _("cannot fully parse %s=%s"),
645 "describe:abbrev", argval
);
647 strvec_pushf(args
, "--abbrev=%.*s", (int)arglen
, argval
);
651 if (match_atom_arg_value(*arg
, "match", arg
, &argval
, &arglen
)) {
653 return strbuf_addf_ret(err
, -1,
654 _("value expected %s="),
657 strvec_pushf(args
, "--match=%.*s", (int)arglen
, argval
);
661 if (match_atom_arg_value(*arg
, "exclude", arg
, &argval
, &arglen
)) {
663 return strbuf_addf_ret(err
, -1,
664 _("value expected %s="),
667 strvec_pushf(args
, "--exclude=%.*s", (int)arglen
, argval
);
674 static int describe_atom_parser(struct ref_format
*format UNUSED
,
675 struct used_atom
*atom
,
676 const char *arg
, struct strbuf
*err
)
678 struct strvec args
= STRVEC_INIT
;
682 const char *bad_arg
= arg
;
687 found
= describe_atom_option_parser(&args
, &arg
, err
);
691 return err_bad_arg(err
, "describe", bad_arg
);
693 atom
->u
.describe_args
= strvec_detach(&args
);
697 static int raw_atom_parser(struct ref_format
*format UNUSED
,
698 struct used_atom
*atom
,
699 const char *arg
, struct strbuf
*err
)
702 atom
->u
.raw_data
.option
= RAW_BARE
;
703 else if (!strcmp(arg
, "size")) {
704 atom
->type
= FIELD_ULONG
;
705 atom
->u
.raw_data
.option
= RAW_LENGTH
;
707 return err_bad_arg(err
, "raw", arg
);
711 static int oid_atom_parser(struct ref_format
*format UNUSED
,
712 struct used_atom
*atom
,
713 const char *arg
, struct strbuf
*err
)
716 atom
->u
.oid
.option
= O_FULL
;
717 else if (!strcmp(arg
, "short"))
718 atom
->u
.oid
.option
= O_SHORT
;
719 else if (skip_prefix(arg
, "short=", &arg
)) {
720 atom
->u
.oid
.option
= O_LENGTH
;
721 if (strtoul_ui(arg
, 10, &atom
->u
.oid
.length
) ||
722 atom
->u
.oid
.length
== 0)
723 return strbuf_addf_ret(err
, -1, _("positive value expected '%s' in %%(%s)"), arg
, atom
->name
);
724 if (atom
->u
.oid
.length
< MINIMUM_ABBREV
)
725 atom
->u
.oid
.length
= MINIMUM_ABBREV
;
727 return err_bad_arg(err
, atom
->name
, arg
);
731 static int person_name_atom_parser(struct ref_format
*format UNUSED
,
732 struct used_atom
*atom
,
733 const char *arg
, struct strbuf
*err
)
736 atom
->u
.name_option
.option
= N_RAW
;
737 else if (!strcmp(arg
, "mailmap"))
738 atom
->u
.name_option
.option
= N_MAILMAP
;
740 return err_bad_arg(err
, atom
->name
, arg
);
744 static int email_atom_option_parser(struct used_atom
*atom
,
745 const char **arg
, struct strbuf
*err
)
749 if (skip_prefix(*arg
, "trim", arg
))
751 if (skip_prefix(*arg
, "localpart", arg
))
753 if (skip_prefix(*arg
, "mailmap", arg
))
758 static int person_email_atom_parser(struct ref_format
*format UNUSED
,
759 struct used_atom
*atom
,
760 const char *arg
, struct strbuf
*err
)
763 int opt
= email_atom_option_parser(atom
, &arg
, err
);
764 const char *bad_arg
= arg
;
767 return err_bad_arg(err
, atom
->name
, bad_arg
);
768 atom
->u
.email_option
.option
|= opt
;
775 return err_bad_arg(err
, atom
->name
, bad_arg
);
780 static int refname_atom_parser(struct ref_format
*format UNUSED
,
781 struct used_atom
*atom
,
782 const char *arg
, struct strbuf
*err
)
784 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
787 static align_type
parse_align_position(const char *s
)
789 if (!strcmp(s
, "right"))
791 else if (!strcmp(s
, "middle"))
793 else if (!strcmp(s
, "left"))
798 static int align_atom_parser(struct ref_format
*format UNUSED
,
799 struct used_atom
*atom
,
800 const char *arg
, struct strbuf
*err
)
802 struct align
*align
= &atom
->u
.align
;
803 struct string_list params
= STRING_LIST_INIT_DUP
;
805 unsigned int width
= ~0U;
808 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
810 align
->position
= ALIGN_LEFT
;
812 string_list_split(¶ms
, arg
, ',', -1);
813 for (i
= 0; i
< params
.nr
; i
++) {
814 const char *s
= params
.items
[i
].string
;
817 if (skip_prefix(s
, "position=", &s
)) {
818 position
= parse_align_position(s
);
820 strbuf_addf(err
, _("unrecognized position:%s"), s
);
821 string_list_clear(¶ms
, 0);
824 align
->position
= position
;
825 } else if (skip_prefix(s
, "width=", &s
)) {
826 if (strtoul_ui(s
, 10, &width
)) {
827 strbuf_addf(err
, _("unrecognized width:%s"), s
);
828 string_list_clear(¶ms
, 0);
831 } else if (!strtoul_ui(s
, 10, &width
))
833 else if ((position
= parse_align_position(s
)) >= 0)
834 align
->position
= position
;
836 strbuf_addf(err
, _("unrecognized %%(%s) argument: %s"), "align", s
);
837 string_list_clear(¶ms
, 0);
843 string_list_clear(¶ms
, 0);
844 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
846 align
->width
= width
;
847 string_list_clear(¶ms
, 0);
851 static int if_atom_parser(struct ref_format
*format UNUSED
,
852 struct used_atom
*atom
,
853 const char *arg
, struct strbuf
*err
)
856 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
858 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
859 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
860 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
861 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
863 return err_bad_arg(err
, "if", arg
);
867 static int rest_atom_parser(struct ref_format
*format UNUSED
,
868 struct used_atom
*atom UNUSED
,
869 const char *arg
, struct strbuf
*err
)
872 return err_no_arg(err
, "rest");
876 static int ahead_behind_atom_parser(struct ref_format
*format
,
877 struct used_atom
*atom UNUSED
,
878 const char *arg
, struct strbuf
*err
)
880 struct string_list_item
*item
;
883 return strbuf_addf_ret(err
, -1, _("expected format: %%(ahead-behind:<committish>)"));
885 item
= string_list_append(&format
->bases
, arg
);
886 item
->util
= lookup_commit_reference_by_name(arg
);
888 die("failed to find '%s'", arg
);
893 static int head_atom_parser(struct ref_format
*format UNUSED
,
894 struct used_atom
*atom
,
895 const char *arg
, struct strbuf
*err
)
898 return err_no_arg(err
, "HEAD");
899 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
907 int (*parser
)(struct ref_format
*format
, struct used_atom
*atom
,
908 const char *arg
, struct strbuf
*err
);
910 [ATOM_REFNAME
] = { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
911 [ATOM_OBJECTTYPE
] = { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
912 [ATOM_OBJECTSIZE
] = { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
913 [ATOM_OBJECTNAME
] = { "objectname", SOURCE_OTHER
, FIELD_STR
, oid_atom_parser
},
914 [ATOM_DELTABASE
] = { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
915 [ATOM_TREE
] = { "tree", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
916 [ATOM_PARENT
] = { "parent", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
917 [ATOM_NUMPARENT
] = { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
918 [ATOM_OBJECT
] = { "object", SOURCE_OBJ
},
919 [ATOM_TYPE
] = { "type", SOURCE_OBJ
},
920 [ATOM_TAG
] = { "tag", SOURCE_OBJ
},
921 [ATOM_AUTHOR
] = { "author", SOURCE_OBJ
},
922 [ATOM_AUTHORNAME
] = { "authorname", SOURCE_OBJ
, FIELD_STR
, person_name_atom_parser
},
923 [ATOM_AUTHOREMAIL
] = { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
924 [ATOM_AUTHORDATE
] = { "authordate", SOURCE_OBJ
, FIELD_TIME
},
925 [ATOM_COMMITTER
] = { "committer", SOURCE_OBJ
},
926 [ATOM_COMMITTERNAME
] = { "committername", SOURCE_OBJ
, FIELD_STR
, person_name_atom_parser
},
927 [ATOM_COMMITTEREMAIL
] = { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
928 [ATOM_COMMITTERDATE
] = { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
929 [ATOM_TAGGER
] = { "tagger", SOURCE_OBJ
},
930 [ATOM_TAGGERNAME
] = { "taggername", SOURCE_OBJ
, FIELD_STR
, person_name_atom_parser
},
931 [ATOM_TAGGEREMAIL
] = { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
932 [ATOM_TAGGERDATE
] = { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
933 [ATOM_CREATOR
] = { "creator", SOURCE_OBJ
},
934 [ATOM_CREATORDATE
] = { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
935 [ATOM_DESCRIBE
] = { "describe", SOURCE_OBJ
, FIELD_STR
, describe_atom_parser
},
936 [ATOM_SUBJECT
] = { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
937 [ATOM_BODY
] = { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
938 [ATOM_TRAILERS
] = { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
939 [ATOM_CONTENTS
] = { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
940 [ATOM_SIGNATURE
] = { "signature", SOURCE_OBJ
, FIELD_STR
, signature_atom_parser
},
941 [ATOM_RAW
] = { "raw", SOURCE_OBJ
, FIELD_STR
, raw_atom_parser
},
942 [ATOM_UPSTREAM
] = { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
943 [ATOM_PUSH
] = { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
944 [ATOM_SYMREF
] = { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
945 [ATOM_FLAG
] = { "flag", SOURCE_NONE
},
946 [ATOM_HEAD
] = { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
947 [ATOM_COLOR
] = { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
948 [ATOM_WORKTREEPATH
] = { "worktreepath", SOURCE_NONE
},
949 [ATOM_ALIGN
] = { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
950 [ATOM_END
] = { "end", SOURCE_NONE
},
951 [ATOM_IF
] = { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
952 [ATOM_THEN
] = { "then", SOURCE_NONE
},
953 [ATOM_ELSE
] = { "else", SOURCE_NONE
},
954 [ATOM_REST
] = { "rest", SOURCE_NONE
, FIELD_STR
, rest_atom_parser
},
955 [ATOM_AHEADBEHIND
] = { "ahead-behind", SOURCE_OTHER
, FIELD_STR
, ahead_behind_atom_parser
},
957 * Please update $__git_ref_fieldlist in git-completion.bash
958 * when you add new atoms
962 #define REF_FORMATTING_STATE_INIT { 0 }
964 struct ref_formatting_stack
{
965 struct ref_formatting_stack
*prev
;
966 struct strbuf output
;
967 void (*at_end
)(struct ref_formatting_stack
**stack
);
971 struct ref_formatting_state
{
973 struct ref_formatting_stack
*stack
;
979 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
981 uintmax_t value
; /* used for sorting when not FIELD_STR */
982 struct used_atom
*atom
;
985 #define ATOM_SIZE_UNSPECIFIED (-1)
987 #define ATOM_VALUE_INIT { \
988 .s_size = ATOM_SIZE_UNSPECIFIED \
992 * Used to parse format string and sort specifiers
994 static int parse_ref_filter_atom(struct ref_format
*format
,
995 const char *atom
, const char *ep
,
1000 int i
, at
, atom_len
;
1003 if (*sp
== '*' && sp
< ep
)
1006 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
1007 (int)(ep
-atom
), atom
);
1010 * If the atom name has a colon, strip it and everything after
1011 * it off - it specifies the format for this entry, and
1012 * shouldn't be used for checking against the valid_atom
1015 arg
= memchr(sp
, ':', ep
- sp
);
1016 atom_len
= (arg
? arg
: ep
) - sp
;
1018 /* Do we have the atom already used elsewhere? */
1019 for (i
= 0; i
< used_atom_cnt
; i
++) {
1020 int len
= strlen(used_atom
[i
].name
);
1021 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
1025 /* Is the atom a valid one? */
1026 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
1027 int len
= strlen(valid_atom
[i
].name
);
1028 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
1032 if (ARRAY_SIZE(valid_atom
) <= i
)
1033 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
1034 (int)(ep
-atom
), atom
);
1035 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
1036 return strbuf_addf_ret(err
, -1,
1037 _("not a git repository, but the field '%.*s' requires access to object data"),
1038 (int)(ep
-atom
), atom
);
1040 /* Add it in, including the deref prefix */
1043 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
1044 used_atom
[at
].atom_type
= i
;
1045 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
1046 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
1047 used_atom
[at
].source
= valid_atom
[i
].source
;
1048 if (used_atom
[at
].source
== SOURCE_OBJ
) {
1050 oi_deref
.info
.contentp
= &oi_deref
.content
;
1052 oi
.info
.contentp
= &oi
.content
;
1055 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
1058 * Treat empty sub-arguments list as NULL (i.e.,
1059 * "%(atom:)" is equivalent to "%(atom)").
1064 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
1065 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
1069 if (i
== ATOM_SYMREF
)
1074 static void quote_formatting(struct strbuf
*s
, const char *str
, ssize_t len
, int quote_style
)
1076 switch (quote_style
) {
1079 strbuf_addstr(s
, str
);
1081 strbuf_add(s
, str
, len
);
1084 sq_quote_buf(s
, str
);
1088 perl_quote_buf(s
, str
);
1090 perl_quote_buf_with_len(s
, str
, len
);
1093 python_quote_buf(s
, str
);
1096 tcl_quote_buf(s
, str
);
1101 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
1102 struct strbuf
*err UNUSED
)
1105 * Quote formatting is only done when the stack has a single
1106 * element. Otherwise quote formatting is done on the
1107 * element's entire output strbuf when the %(end) atom is
1110 if (!state
->stack
->prev
)
1111 quote_formatting(&state
->stack
->output
, v
->s
, v
->s_size
, state
->quote_style
);
1112 else if (v
->s_size
< 0)
1113 strbuf_addstr(&state
->stack
->output
, v
->s
);
1115 strbuf_add(&state
->stack
->output
, v
->s
, v
->s_size
);
1119 static void push_stack_element(struct ref_formatting_stack
**stack
)
1121 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
1123 strbuf_init(&s
->output
, 0);
1128 static void pop_stack_element(struct ref_formatting_stack
**stack
)
1130 struct ref_formatting_stack
*current
= *stack
;
1131 struct ref_formatting_stack
*prev
= current
->prev
;
1134 strbuf_addbuf(&prev
->output
, ¤t
->output
);
1135 strbuf_release(¤t
->output
);
1140 static void end_align_handler(struct ref_formatting_stack
**stack
)
1142 struct ref_formatting_stack
*cur
= *stack
;
1143 struct align
*align
= (struct align
*)cur
->at_end_data
;
1144 struct strbuf s
= STRBUF_INIT
;
1146 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
1147 strbuf_swap(&cur
->output
, &s
);
1151 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
1152 struct strbuf
*err UNUSED
)
1154 struct ref_formatting_stack
*new_stack
;
1156 push_stack_element(&state
->stack
);
1157 new_stack
= state
->stack
;
1158 new_stack
->at_end
= end_align_handler
;
1159 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
1163 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
1165 struct ref_formatting_stack
*cur
= *stack
;
1166 struct ref_formatting_stack
*prev
= cur
->prev
;
1167 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1169 if (!if_then_else
->then_atom_seen
)
1170 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
1172 if (if_then_else
->else_atom_seen
) {
1174 * There is an %(else) atom: we need to drop one state from the
1175 * stack, either the %(else) branch if the condition is satisfied, or
1176 * the %(then) branch if it isn't.
1178 if (if_then_else
->condition_satisfied
) {
1179 strbuf_reset(&cur
->output
);
1180 pop_stack_element(&cur
);
1182 strbuf_swap(&cur
->output
, &prev
->output
);
1183 strbuf_reset(&cur
->output
);
1184 pop_stack_element(&cur
);
1186 } else if (!if_then_else
->condition_satisfied
) {
1188 * No %(else) atom: just drop the %(then) branch if the
1189 * condition is not satisfied.
1191 strbuf_reset(&cur
->output
);
1198 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
1199 struct strbuf
*err UNUSED
)
1201 struct ref_formatting_stack
*new_stack
;
1202 struct if_then_else
*if_then_else
= xcalloc(1,
1203 sizeof(struct if_then_else
));
1205 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
1206 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
1208 push_stack_element(&state
->stack
);
1209 new_stack
= state
->stack
;
1210 new_stack
->at_end
= if_then_else_handler
;
1211 new_stack
->at_end_data
= if_then_else
;
1215 static int is_empty(struct strbuf
*buf
)
1217 const char *cur
= buf
->buf
;
1218 const char *end
= buf
->buf
+ buf
->len
;
1220 while (cur
!= end
&& (isspace(*cur
)))
1226 static int then_atom_handler(struct atom_value
*atomv UNUSED
,
1227 struct ref_formatting_state
*state
,
1230 struct ref_formatting_stack
*cur
= state
->stack
;
1231 struct if_then_else
*if_then_else
= NULL
;
1234 if (cur
->at_end
== if_then_else_handler
)
1235 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1237 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
1238 if (if_then_else
->then_atom_seen
)
1239 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
1240 if (if_then_else
->else_atom_seen
)
1241 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
1242 if_then_else
->then_atom_seen
= 1;
1243 if (if_then_else
->str
)
1244 str_len
= strlen(if_then_else
->str
);
1246 * If the 'equals' or 'notequals' attribute is used then
1247 * perform the required comparison. If not, only non-empty
1248 * strings satisfy the 'if' condition.
1250 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
1251 if (str_len
== cur
->output
.len
&&
1252 !memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1253 if_then_else
->condition_satisfied
= 1;
1254 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
1255 if (str_len
!= cur
->output
.len
||
1256 memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1257 if_then_else
->condition_satisfied
= 1;
1258 } else if (cur
->output
.len
&& !is_empty(&cur
->output
))
1259 if_then_else
->condition_satisfied
= 1;
1260 strbuf_reset(&cur
->output
);
1264 static int else_atom_handler(struct atom_value
*atomv UNUSED
,
1265 struct ref_formatting_state
*state
,
1268 struct ref_formatting_stack
*prev
= state
->stack
;
1269 struct if_then_else
*if_then_else
= NULL
;
1271 if (prev
->at_end
== if_then_else_handler
)
1272 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
1274 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
1275 if (!if_then_else
->then_atom_seen
)
1276 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
1277 if (if_then_else
->else_atom_seen
)
1278 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
1279 if_then_else
->else_atom_seen
= 1;
1280 push_stack_element(&state
->stack
);
1281 state
->stack
->at_end_data
= prev
->at_end_data
;
1282 state
->stack
->at_end
= prev
->at_end
;
1286 static int end_atom_handler(struct atom_value
*atomv UNUSED
,
1287 struct ref_formatting_state
*state
,
1290 struct ref_formatting_stack
*current
= state
->stack
;
1291 struct strbuf s
= STRBUF_INIT
;
1293 if (!current
->at_end
)
1294 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
1295 current
->at_end(&state
->stack
);
1297 /* Stack may have been popped within at_end(), hence reset the current pointer */
1298 current
= state
->stack
;
1301 * Perform quote formatting when the stack element is that of
1302 * a supporting atom. If nested then perform quote formatting
1303 * only on the topmost supporting atom.
1305 if (!current
->prev
->prev
) {
1306 quote_formatting(&s
, current
->output
.buf
, current
->output
.len
, state
->quote_style
);
1307 strbuf_swap(¤t
->output
, &s
);
1310 pop_stack_element(&state
->stack
);
1315 * In a format string, find the next occurrence of %(atom).
1317 static const char *find_next(const char *cp
)
1322 * %( is the start of an atom;
1323 * %% is a quoted per-cent.
1327 else if (cp
[1] == '%')
1328 cp
++; /* skip over two % */
1329 /* otherwise this is a singleton, literal % */
1336 static int reject_atom(enum atom_type atom_type
)
1338 return atom_type
== ATOM_REST
;
1342 * Make sure the format string is well formed, and parse out
1345 int verify_ref_format(struct ref_format
*format
)
1347 const char *cp
, *sp
;
1349 format
->need_color_reset_at_eol
= 0;
1350 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
1351 struct strbuf err
= STRBUF_INIT
;
1352 const char *color
, *ep
= strchr(sp
, ')');
1356 return error(_("malformed format string %s"), sp
);
1357 /* sp points at "%(" and ep points at the closing ")" */
1358 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
1361 if (reject_atom(used_atom
[at
].atom_type
))
1362 die(_("this command reject atom %%(%.*s)"), (int)(ep
- sp
- 2), sp
+ 2);
1364 if ((format
->quote_style
== QUOTE_PYTHON
||
1365 format
->quote_style
== QUOTE_SHELL
||
1366 format
->quote_style
== QUOTE_TCL
) &&
1367 used_atom
[at
].atom_type
== ATOM_RAW
&&
1368 used_atom
[at
].u
.raw_data
.option
== RAW_BARE
)
1369 die(_("--format=%.*s cannot be used with "
1370 "--python, --shell, --tcl"), (int)(ep
- sp
- 2), sp
+ 2);
1373 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
1374 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
1375 strbuf_release(&err
);
1377 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
1378 format
->need_color_reset_at_eol
= 0;
1382 static const char *do_grab_oid(const char *field
, const struct object_id
*oid
,
1383 struct used_atom
*atom
)
1385 switch (atom
->u
.oid
.option
) {
1387 return oid_to_hex(oid
);
1389 return repo_find_unique_abbrev(the_repository
, oid
,
1390 atom
->u
.oid
.length
);
1392 return repo_find_unique_abbrev(the_repository
, oid
,
1395 BUG("unknown %%(%s) option", field
);
1399 static int grab_oid(const char *name
, const char *field
, const struct object_id
*oid
,
1400 struct atom_value
*v
, struct used_atom
*atom
)
1402 if (starts_with(name
, field
)) {
1403 v
->s
= xstrdup(do_grab_oid(field
, oid
, atom
));
1409 /* See grab_values */
1410 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
1414 for (i
= 0; i
< used_atom_cnt
; i
++) {
1415 const char *name
= used_atom
[i
].name
;
1416 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1417 struct atom_value
*v
= &val
[i
];
1418 if (!!deref
!= (*name
== '*'))
1422 if (atom_type
== ATOM_OBJECTTYPE
)
1423 v
->s
= xstrdup(type_name(oi
->type
));
1424 else if (atom_type
== ATOM_OBJECTSIZE
) {
1425 if (used_atom
[i
].u
.objectsize
.option
== O_SIZE_DISK
) {
1426 v
->value
= oi
->disk_size
;
1427 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
1428 } else if (used_atom
[i
].u
.objectsize
.option
== O_SIZE
) {
1429 v
->value
= oi
->size
;
1430 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
1432 } else if (atom_type
== ATOM_DELTABASE
)
1433 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
1434 else if (atom_type
== ATOM_OBJECTNAME
&& deref
)
1435 grab_oid(name
, "objectname", &oi
->oid
, v
, &used_atom
[i
]);
1439 /* See grab_values */
1440 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1443 struct tag
*tag
= (struct tag
*) obj
;
1445 for (i
= 0; i
< used_atom_cnt
; i
++) {
1446 const char *name
= used_atom
[i
].name
;
1447 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1448 struct atom_value
*v
= &val
[i
];
1449 if (!!deref
!= (*name
== '*'))
1453 if (atom_type
== ATOM_TAG
)
1454 v
->s
= xstrdup(tag
->tag
);
1455 else if (atom_type
== ATOM_TYPE
&& tag
->tagged
)
1456 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
1457 else if (atom_type
== ATOM_OBJECT
&& tag
->tagged
)
1458 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
1462 /* See grab_values */
1463 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1466 struct commit
*commit
= (struct commit
*) obj
;
1468 for (i
= 0; i
< used_atom_cnt
; i
++) {
1469 const char *name
= used_atom
[i
].name
;
1470 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1471 struct atom_value
*v
= &val
[i
];
1472 if (!!deref
!= (*name
== '*'))
1476 if (atom_type
== ATOM_TREE
&&
1477 grab_oid(name
, "tree", get_commit_tree_oid(commit
), v
, &used_atom
[i
]))
1479 if (atom_type
== ATOM_NUMPARENT
) {
1480 v
->value
= commit_list_count(commit
->parents
);
1481 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1483 else if (atom_type
== ATOM_PARENT
) {
1484 struct commit_list
*parents
;
1485 struct strbuf s
= STRBUF_INIT
;
1486 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1487 struct object_id
*oid
= &parents
->item
->object
.oid
;
1488 if (parents
!= commit
->parents
)
1489 strbuf_addch(&s
, ' ');
1490 strbuf_addstr(&s
, do_grab_oid("parent", oid
, &used_atom
[i
]));
1492 v
->s
= strbuf_detach(&s
, NULL
);
1497 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1501 if (!strncmp(buf
, who
, wholen
) &&
1503 return buf
+ wholen
+ 1;
1504 eol
= strchr(buf
, '\n');
1509 return ""; /* end of header */
1515 static const char *copy_line(const char *buf
)
1517 const char *eol
= strchrnul(buf
, '\n');
1518 return xmemdupz(buf
, eol
- buf
);
1521 static const char *copy_name(const char *buf
)
1524 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1525 if (starts_with(cp
, " <"))
1526 return xmemdupz(buf
, cp
- buf
);
1531 static const char *find_end_of_email(const char *email
, int opt
)
1533 const char *eoemail
;
1535 if (opt
& EO_LOCALPART
) {
1536 eoemail
= strchr(email
, '@');
1539 return strchr(email
, '>');
1543 return strchr(email
, '>');
1546 * The option here is either the raw email option or the raw
1547 * mailmap option (that is EO_RAW or EO_MAILMAP). In such cases,
1548 * we directly grab the whole email including the closing
1551 * If EO_MAILMAP was set with any other option (that is either
1552 * EO_TRIM or EO_LOCALPART), we already grab the end of email
1555 eoemail
= strchr(email
, '>');
1561 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1563 const char *email
= strchr(buf
, '<');
1564 const char *eoemail
;
1565 int opt
= atom
->u
.email_option
.option
;
1570 if (opt
& (EO_LOCALPART
| EO_TRIM
))
1573 eoemail
= find_end_of_email(email
, opt
);
1576 return xmemdupz(email
, eoemail
- email
);
1579 static char *copy_subject(const char *buf
, unsigned long len
)
1581 struct strbuf sb
= STRBUF_INIT
;
1584 for (i
= 0; i
< len
; i
++) {
1585 if (buf
[i
] == '\r' && i
+ 1 < len
&& buf
[i
+ 1] == '\n')
1586 continue; /* ignore CR in CRLF */
1589 strbuf_addch(&sb
, ' ');
1591 strbuf_addch(&sb
, buf
[i
]);
1593 return strbuf_detach(&sb
, NULL
);
1596 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1598 const char *eoemail
= strstr(buf
, "> ");
1600 timestamp_t timestamp
;
1602 struct date_mode date_mode
= DATE_MODE_INIT
;
1603 const char *formatp
;
1606 * We got here because atomname ends in "date" or "date<something>";
1607 * it's not possible that <something> is not ":<format>" because
1608 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1609 * ":" means no format is specified, and use the default.
1611 formatp
= strchr(atomname
, ':');
1614 parse_date_format(formatp
, &date_mode
);
1619 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1620 if (timestamp
== TIME_MAX
)
1622 tz
= strtol(zone
, NULL
, 10);
1623 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1625 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1626 v
->value
= timestamp
;
1627 date_mode_release(&date_mode
);
1634 static struct string_list mailmap
= STRING_LIST_INIT_NODUP
;
1636 /* See grab_values */
1637 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1640 int wholen
= strlen(who
);
1641 const char *wholine
= NULL
;
1642 const char *headers
[] = { "author ", "committer ",
1645 for (i
= 0; i
< used_atom_cnt
; i
++) {
1646 struct used_atom
*atom
= &used_atom
[i
];
1647 const char *name
= atom
->name
;
1648 struct atom_value
*v
= &val
[i
];
1649 struct strbuf mailmap_buf
= STRBUF_INIT
;
1651 if (!!deref
!= (*name
== '*'))
1655 if (strncmp(who
, name
, wholen
))
1657 if (name
[wholen
] != 0 &&
1658 !starts_with(name
+ wholen
, "name") &&
1659 !starts_with(name
+ wholen
, "email") &&
1660 !starts_with(name
+ wholen
, "date"))
1663 if ((starts_with(name
+ wholen
, "name") &&
1664 (atom
->u
.name_option
.option
== N_MAILMAP
)) ||
1665 (starts_with(name
+ wholen
, "email") &&
1666 (atom
->u
.email_option
.option
& EO_MAILMAP
))) {
1668 read_mailmap(&mailmap
);
1669 strbuf_addstr(&mailmap_buf
, buf
);
1670 apply_mailmap_to_header(&mailmap_buf
, headers
, &mailmap
);
1671 wholine
= find_wholine(who
, wholen
, mailmap_buf
.buf
);
1673 wholine
= find_wholine(who
, wholen
, buf
);
1677 return; /* no point looking for it */
1678 if (name
[wholen
] == 0)
1679 v
->s
= copy_line(wholine
);
1680 else if (starts_with(name
+ wholen
, "name"))
1681 v
->s
= copy_name(wholine
);
1682 else if (starts_with(name
+ wholen
, "email"))
1683 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1684 else if (starts_with(name
+ wholen
, "date"))
1685 grab_date(wholine
, v
, name
);
1687 strbuf_release(&mailmap_buf
);
1691 * For a tag or a commit object, if "creator" or "creatordate" is
1692 * requested, do something special.
1694 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1695 return; /* "author" for commit object is not wanted */
1697 wholine
= find_wholine(who
, wholen
, buf
);
1700 for (i
= 0; i
< used_atom_cnt
; i
++) {
1701 const char *name
= used_atom
[i
].name
;
1702 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1703 struct atom_value
*v
= &val
[i
];
1704 if (!!deref
!= (*name
== '*'))
1709 if (atom_type
== ATOM_CREATORDATE
)
1710 grab_date(wholine
, v
, name
);
1711 else if (atom_type
== ATOM_CREATOR
)
1712 v
->s
= copy_line(wholine
);
1716 static void grab_signature(struct atom_value
*val
, int deref
, struct object
*obj
)
1719 struct commit
*commit
= (struct commit
*) obj
;
1720 struct signature_check sigc
= { 0 };
1721 int signature_checked
= 0;
1723 for (i
= 0; i
< used_atom_cnt
; i
++) {
1724 struct used_atom
*atom
= &used_atom
[i
];
1725 const char *name
= atom
->name
;
1726 struct atom_value
*v
= &val
[i
];
1729 if (!!deref
!= (*name
== '*'))
1734 if (!skip_prefix(name
, "signature", &name
) ||
1735 (*name
&& *name
!= ':'))
1742 opt
= parse_signature_option(name
);
1746 if (!signature_checked
) {
1747 check_commit_signature(commit
, &sigc
);
1748 signature_checked
= 1;
1753 v
->s
= xstrdup(sigc
.output
? sigc
.output
: "");
1756 v
->s
= xstrdup(sigc
.signer
? sigc
.signer
: "");
1759 switch (sigc
.result
) {
1761 switch (sigc
.trust_level
) {
1762 case TRUST_UNDEFINED
:
1764 v
->s
= xstrfmt("%c", (char)'U');
1767 v
->s
= xstrfmt("%c", (char)'G');
1777 v
->s
= xstrfmt("%c", (char)sigc
.result
);
1782 v
->s
= xstrdup(sigc
.key
? sigc
.key
: "");
1785 v
->s
= xstrdup(sigc
.fingerprint
?
1786 sigc
.fingerprint
: "");
1789 v
->s
= xstrdup(sigc
.primary_key_fingerprint
?
1790 sigc
.primary_key_fingerprint
: "");
1793 v
->s
= xstrdup(gpg_trust_level_to_str(sigc
.trust_level
));
1798 if (signature_checked
)
1799 signature_check_clear(&sigc
);
1802 static void find_subpos(const char *buf
,
1803 const char **sub
, size_t *sublen
,
1804 const char **body
, size_t *bodylen
,
1806 const char **sig
, size_t *siglen
)
1808 struct strbuf payload
= STRBUF_INIT
;
1809 struct strbuf signature
= STRBUF_INIT
;
1811 const char *end
= buf
+ strlen(buf
);
1812 const char *sigstart
;
1814 /* parse signature first; we might not even have a subject line */
1815 parse_signature(buf
, end
- buf
, &payload
, &signature
);
1816 strbuf_release(&payload
);
1818 /* skip past header until we hit empty line */
1819 while (*buf
&& *buf
!= '\n') {
1820 eol
= strchrnul(buf
, '\n');
1825 /* skip any empty lines */
1826 while (*buf
== '\n')
1828 *sig
= strbuf_detach(&signature
, siglen
);
1829 sigstart
= buf
+ parse_signed_buffer(buf
, strlen(buf
));
1831 /* subject is first non-empty line */
1833 /* subject goes to first empty line before signature begins */
1834 if ((eol
= strstr(*sub
, "\n\n")) ||
1835 (eol
= strstr(*sub
, "\r\n\r\n"))) {
1836 eol
= eol
< sigstart
? eol
: sigstart
;
1838 /* treat whole message as subject */
1842 *sublen
= buf
- *sub
;
1843 /* drop trailing newline, if present */
1844 while (*sublen
&& ((*sub
)[*sublen
- 1] == '\n' ||
1845 (*sub
)[*sublen
- 1] == '\r'))
1848 /* skip any empty lines */
1849 while (*buf
== '\n' || *buf
== '\r')
1852 *bodylen
= strlen(buf
);
1853 *nonsiglen
= sigstart
- buf
;
1857 * If 'lines' is greater than 0, append that many lines from the given
1858 * 'buf' of length 'size' to the given strbuf.
1860 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1863 const char *sp
, *eol
;
1868 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1870 strbuf_addstr(out
, "\n ");
1871 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1872 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1873 strbuf_add(out
, sp
, len
);
1880 static void grab_describe_values(struct atom_value
*val
, int deref
,
1883 struct commit
*commit
= (struct commit
*)obj
;
1886 for (i
= 0; i
< used_atom_cnt
; i
++) {
1887 struct used_atom
*atom
= &used_atom
[i
];
1888 enum atom_type type
= atom
->atom_type
;
1889 const char *name
= atom
->name
;
1890 struct atom_value
*v
= &val
[i
];
1892 struct child_process cmd
= CHILD_PROCESS_INIT
;
1893 struct strbuf out
= STRBUF_INIT
;
1894 struct strbuf err
= STRBUF_INIT
;
1896 if (type
!= ATOM_DESCRIBE
)
1899 if (!!deref
!= (*name
== '*'))
1903 strvec_push(&cmd
.args
, "describe");
1904 strvec_pushv(&cmd
.args
, atom
->u
.describe_args
);
1905 strvec_push(&cmd
.args
, oid_to_hex(&commit
->object
.oid
));
1906 if (pipe_command(&cmd
, NULL
, 0, &out
, 0, &err
, 0) < 0) {
1907 error(_("failed to run 'describe'"));
1912 v
->s
= strbuf_detach(&out
, NULL
);
1914 strbuf_release(&err
);
1918 /* See grab_values */
1919 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct expand_data
*data
)
1922 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1923 size_t sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1924 void *buf
= data
->content
;
1926 for (i
= 0; i
< used_atom_cnt
; i
++) {
1927 struct used_atom
*atom
= &used_atom
[i
];
1928 const char *name
= atom
->name
;
1929 struct atom_value
*v
= &val
[i
];
1930 enum atom_type atom_type
= atom
->atom_type
;
1932 if (!!deref
!= (*name
== '*'))
1937 if (atom_type
== ATOM_RAW
) {
1938 unsigned long buf_size
= data
->size
;
1940 if (atom
->u
.raw_data
.option
== RAW_BARE
) {
1941 v
->s
= xmemdupz(buf
, buf_size
);
1942 v
->s_size
= buf_size
;
1943 } else if (atom
->u
.raw_data
.option
== RAW_LENGTH
) {
1944 v
->value
= buf_size
;
1945 v
->s
= xstrfmt("%"PRIuMAX
, v
->value
);
1950 if ((data
->type
!= OBJ_TAG
&&
1951 data
->type
!= OBJ_COMMIT
) ||
1952 (strcmp(name
, "body") &&
1953 !starts_with(name
, "subject") &&
1954 !starts_with(name
, "trailers") &&
1955 !starts_with(name
, "contents")))
1960 &bodypos
, &bodylen
, &nonsiglen
,
1963 if (atom
->u
.contents
.option
== C_SUB
)
1964 v
->s
= copy_subject(subpos
, sublen
);
1965 else if (atom
->u
.contents
.option
== C_SUB_SANITIZE
) {
1966 struct strbuf sb
= STRBUF_INIT
;
1967 format_sanitized_subject(&sb
, subpos
, sublen
);
1968 v
->s
= strbuf_detach(&sb
, NULL
);
1969 } else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1970 v
->s
= xmemdupz(bodypos
, bodylen
);
1971 else if (atom
->u
.contents
.option
== C_LENGTH
) {
1972 v
->value
= strlen(subpos
);
1973 v
->s
= xstrfmt("%"PRIuMAX
, v
->value
);
1974 } else if (atom
->u
.contents
.option
== C_BODY
)
1975 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1976 else if (atom
->u
.contents
.option
== C_SIG
)
1977 v
->s
= xmemdupz(sigpos
, siglen
);
1978 else if (atom
->u
.contents
.option
== C_LINES
) {
1979 struct strbuf s
= STRBUF_INIT
;
1980 const char *contents_end
= bodypos
+ nonsiglen
;
1982 /* Size is the length of the message after removing the signature */
1983 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1984 v
->s
= strbuf_detach(&s
, NULL
);
1985 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1986 struct strbuf s
= STRBUF_INIT
;
1988 /* Format the trailer info according to the trailer_opts given */
1989 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1991 v
->s
= strbuf_detach(&s
, NULL
);
1992 } else if (atom
->u
.contents
.option
== C_BARE
)
1993 v
->s
= xstrdup(subpos
);
1996 free((void *)sigpos
);
2000 * We want to have empty print-string for field requests
2001 * that do not apply (e.g. "authordate" for a tag object)
2003 static void fill_missing_values(struct atom_value
*val
)
2006 for (i
= 0; i
< used_atom_cnt
; i
++) {
2007 struct atom_value
*v
= &val
[i
];
2014 * val is a list of atom_value to hold returned values. Extract
2015 * the values for atoms in used_atom array out of (obj, buf, sz).
2016 * when deref is false, (obj, buf, sz) is the object that is
2017 * pointed at by the ref itself; otherwise it is the object the
2018 * ref (which is a tag) refers to.
2020 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, struct expand_data
*data
)
2022 void *buf
= data
->content
;
2024 switch (obj
->type
) {
2026 grab_tag_values(val
, deref
, obj
);
2027 grab_sub_body_contents(val
, deref
, data
);
2028 grab_person("tagger", val
, deref
, buf
);
2029 grab_describe_values(val
, deref
, obj
);
2032 grab_commit_values(val
, deref
, obj
);
2033 grab_sub_body_contents(val
, deref
, data
);
2034 grab_person("author", val
, deref
, buf
);
2035 grab_person("committer", val
, deref
, buf
);
2036 grab_signature(val
, deref
, obj
);
2037 grab_describe_values(val
, deref
, obj
);
2040 /* grab_tree_values(val, deref, obj, buf, sz); */
2041 grab_sub_body_contents(val
, deref
, data
);
2044 /* grab_blob_values(val, deref, obj, buf, sz); */
2045 grab_sub_body_contents(val
, deref
, data
);
2048 die("Eh? Object of type %d?", obj
->type
);
2052 static inline char *copy_advance(char *dst
, const char *src
)
2059 static const char *lstrip_ref_components(const char *refname
, int len
)
2061 long remaining
= len
;
2062 const char *start
= xstrdup(refname
);
2063 const char *to_free
= start
;
2067 const char *p
= refname
;
2069 /* Find total no of '/' separated path-components */
2070 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
2073 * The number of components we need to strip is now
2074 * the total minus the components to be left (Plus one
2075 * because we count the number of '/', but the number
2076 * of components is one more than the no of '/').
2078 remaining
= i
+ len
+ 1;
2081 while (remaining
> 0) {
2084 free((char *)to_free
);
2092 start
= xstrdup(start
);
2093 free((char *)to_free
);
2097 static const char *rstrip_ref_components(const char *refname
, int len
)
2099 long remaining
= len
;
2100 const char *start
= xstrdup(refname
);
2101 const char *to_free
= start
;
2105 const char *p
= refname
;
2107 /* Find total no of '/' separated path-components */
2108 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
2111 * The number of components we need to strip is now
2112 * the total minus the components to be left (Plus one
2113 * because we count the number of '/', but the number
2114 * of components is one more than the no of '/').
2116 remaining
= i
+ len
+ 1;
2119 while (remaining
-- > 0) {
2120 char *p
= strrchr(start
, '/');
2122 free((char *)to_free
);
2130 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
2132 if (atom
->option
== R_SHORT
)
2133 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
2134 else if (atom
->option
== R_LSTRIP
)
2135 return lstrip_ref_components(refname
, atom
->lstrip
);
2136 else if (atom
->option
== R_RSTRIP
)
2137 return rstrip_ref_components(refname
, atom
->rstrip
);
2139 return xstrdup(refname
);
2142 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
2143 struct branch
*branch
, const char **s
)
2145 int num_ours
, num_theirs
;
2146 if (atom
->u
.remote_ref
.option
== RR_REF
)
2147 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
2148 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
2149 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
2150 NULL
, atom
->u
.remote_ref
.push
,
2151 AHEAD_BEHIND_FULL
) < 0) {
2152 *s
= xstrdup(msgs
.gone
);
2153 } else if (!num_ours
&& !num_theirs
)
2156 *s
= xstrfmt(msgs
.behind
, num_theirs
);
2157 else if (!num_theirs
)
2158 *s
= xstrfmt(msgs
.ahead
, num_ours
);
2160 *s
= xstrfmt(msgs
.ahead_behind
,
2161 num_ours
, num_theirs
);
2162 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
2163 const char *to_free
= *s
;
2164 *s
= xstrfmt("[%s]", *s
);
2165 free((void *)to_free
);
2167 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
2168 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
2169 NULL
, atom
->u
.remote_ref
.push
,
2170 AHEAD_BEHIND_FULL
) < 0) {
2174 if (!num_ours
&& !num_theirs
)
2178 else if (!num_theirs
)
2182 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
2184 const char *remote
= atom
->u
.remote_ref
.push
?
2185 pushremote_for_branch(branch
, &explicit) :
2186 remote_for_branch(branch
, &explicit);
2187 *s
= xstrdup(explicit ? remote
: "");
2188 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
2191 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
2192 *s
= xstrdup(merge
? merge
: "");
2194 BUG("unhandled RR_* enum");
2197 char *get_head_description(void)
2199 struct strbuf desc
= STRBUF_INIT
;
2200 struct wt_status_state state
;
2201 memset(&state
, 0, sizeof(state
));
2202 wt_status_get_state(the_repository
, &state
, 1);
2203 if (state
.rebase_in_progress
||
2204 state
.rebase_interactive_in_progress
) {
2206 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
2209 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
2210 state
.detached_from
);
2211 } else if (state
.bisect_in_progress
)
2212 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
2214 else if (state
.detached_from
) {
2215 if (state
.detached_at
)
2216 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
2217 state
.detached_from
);
2219 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
2220 state
.detached_from
);
2222 strbuf_addstr(&desc
, _("(no branch)"));
2224 wt_status_state_free_buffers(&state
);
2226 return strbuf_detach(&desc
, NULL
);
2229 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
2234 return show_ref(&atom
->u
.refname
, ref
->symref
);
2237 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
2239 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
2240 return get_head_description();
2241 return show_ref(&atom
->u
.refname
, ref
->refname
);
2244 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
2245 struct expand_data
*oi
, struct strbuf
*err
)
2247 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
2249 if (oi
->info
.contentp
) {
2250 /* We need to know that to use parse_object_buffer properly */
2251 oi
->info
.sizep
= &oi
->size
;
2252 oi
->info
.typep
= &oi
->type
;
2254 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
2255 OBJECT_INFO_LOOKUP_REPLACE
))
2256 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2257 oid_to_hex(&oi
->oid
), ref
->refname
);
2258 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
2259 BUG("Object size is less than zero.");
2261 if (oi
->info
.contentp
) {
2262 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
2266 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
2267 oid_to_hex(&oi
->oid
), ref
->refname
);
2269 grab_values(ref
->value
, deref
, *obj
, oi
);
2272 grab_common_values(ref
->value
, deref
, oi
);
2278 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
2282 for (i
= 0; worktrees
[i
]; i
++) {
2283 if (worktrees
[i
]->head_ref
) {
2284 struct ref_to_worktree_entry
*entry
;
2285 entry
= xmalloc(sizeof(*entry
));
2286 entry
->wt
= worktrees
[i
];
2287 hashmap_entry_init(&entry
->ent
,
2288 strhash(worktrees
[i
]->head_ref
));
2290 hashmap_add(map
, &entry
->ent
);
2295 static void lazy_init_worktree_map(void)
2297 if (ref_to_worktree_map
.worktrees
)
2300 ref_to_worktree_map
.worktrees
= get_worktrees();
2301 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
2302 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
2305 static char *get_worktree_path(const struct ref_array_item
*ref
)
2307 struct hashmap_entry entry
, *e
;
2308 struct ref_to_worktree_entry
*lookup_result
;
2310 lazy_init_worktree_map();
2312 hashmap_entry_init(&entry
, strhash(ref
->refname
));
2313 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
2318 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
2320 return xstrdup(lookup_result
->wt
->path
);
2324 * Parse the object referred by ref, and grab needed value.
2326 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
2330 struct object_info empty
= OBJECT_INFO_INIT
;
2331 int ahead_behind_atoms
= 0;
2333 CALLOC_ARRAY(ref
->value
, used_atom_cnt
);
2335 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
2336 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
2339 ref
->symref
= xstrdup("");
2342 /* Fill in specials first */
2343 for (i
= 0; i
< used_atom_cnt
; i
++) {
2344 struct used_atom
*atom
= &used_atom
[i
];
2345 enum atom_type atom_type
= atom
->atom_type
;
2346 const char *name
= used_atom
[i
].name
;
2347 struct atom_value
*v
= &ref
->value
[i
];
2349 const char *refname
;
2350 struct branch
*branch
= NULL
;
2352 v
->s_size
= ATOM_SIZE_UNSPECIFIED
;
2353 v
->handler
= append_atom
;
2362 if (atom_type
== ATOM_REFNAME
)
2363 refname
= get_refname(atom
, ref
);
2364 else if (atom_type
== ATOM_WORKTREEPATH
) {
2365 if (ref
->kind
== FILTER_REFS_BRANCHES
)
2366 v
->s
= get_worktree_path(ref
);
2371 else if (atom_type
== ATOM_SYMREF
)
2372 refname
= get_symref(atom
, ref
);
2373 else if (atom_type
== ATOM_UPSTREAM
) {
2374 const char *branch_name
;
2375 /* only local branches may have an upstream */
2376 if (!skip_prefix(ref
->refname
, "refs/heads/",
2381 branch
= branch_get(branch_name
);
2383 refname
= branch_get_upstream(branch
, NULL
);
2385 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2389 } else if (atom_type
== ATOM_PUSH
&& atom
->u
.remote_ref
.push
) {
2390 const char *branch_name
;
2392 if (!skip_prefix(ref
->refname
, "refs/heads/",
2395 branch
= branch_get(branch_name
);
2397 if (atom
->u
.remote_ref
.push_remote
)
2400 refname
= branch_get_push(branch
, NULL
);
2404 /* We will definitely re-init v->s on the next line. */
2406 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2408 } else if (atom_type
== ATOM_COLOR
) {
2409 v
->s
= xstrdup(atom
->u
.color
);
2411 } else if (atom_type
== ATOM_FLAG
) {
2412 char buf
[256], *cp
= buf
;
2413 if (ref
->flag
& REF_ISSYMREF
)
2414 cp
= copy_advance(cp
, ",symref");
2415 if (ref
->flag
& REF_ISPACKED
)
2416 cp
= copy_advance(cp
, ",packed");
2421 v
->s
= xstrdup(buf
+ 1);
2424 } else if (!deref
&& atom_type
== ATOM_OBJECTNAME
&&
2425 grab_oid(name
, "objectname", &ref
->objectname
, v
, atom
)) {
2427 } else if (atom_type
== ATOM_HEAD
) {
2428 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
2429 v
->s
= xstrdup("*");
2431 v
->s
= xstrdup(" ");
2433 } else if (atom_type
== ATOM_ALIGN
) {
2434 v
->handler
= align_atom_handler
;
2437 } else if (atom_type
== ATOM_END
) {
2438 v
->handler
= end_atom_handler
;
2441 } else if (atom_type
== ATOM_IF
) {
2443 if (skip_prefix(name
, "if:", &s
))
2447 v
->handler
= if_atom_handler
;
2449 } else if (atom_type
== ATOM_THEN
) {
2450 v
->handler
= then_atom_handler
;
2453 } else if (atom_type
== ATOM_ELSE
) {
2454 v
->handler
= else_atom_handler
;
2457 } else if (atom_type
== ATOM_REST
) {
2459 v
->s
= xstrdup(ref
->rest
);
2463 } else if (atom_type
== ATOM_AHEADBEHIND
) {
2465 const struct ahead_behind_count
*count
;
2466 count
= ref
->counts
[ahead_behind_atoms
++];
2467 v
->s
= xstrfmt("%d %d", count
->ahead
, count
->behind
);
2477 v
->s
= xstrdup(refname
);
2479 v
->s
= xstrfmt("%s^{}", refname
);
2480 free((char *)refname
);
2483 for (i
= 0; i
< used_atom_cnt
; i
++) {
2484 struct atom_value
*v
= &ref
->value
[i
];
2485 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
2486 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2487 oid_to_hex(&ref
->objectname
), ref
->refname
);
2491 oi
.info
.contentp
= &oi
.content
;
2492 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
2493 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
2497 oi
.oid
= ref
->objectname
;
2498 if (get_object(ref
, 0, &obj
, &oi
, err
))
2502 * If there is no atom that wants to know about tagged
2503 * object, we are done.
2505 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
2509 * If it is a tag object, see if we use a value that derefs
2510 * the object, and if we do grab the object it refers to.
2512 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
2515 * NEEDSWORK: This derefs tag only once, which
2516 * is good to deal with chains of trust, but
2517 * is not consistent with what deref_tag() does
2518 * which peels the onion to the core.
2520 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
2524 * Given a ref, return the value for the atom. This lazily gets value
2525 * out of the object by calling populate value.
2527 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
2528 struct atom_value
**v
, struct strbuf
*err
)
2531 if (populate_value(ref
, err
))
2533 fill_missing_values(ref
->value
);
2535 *v
= &ref
->value
[atom
];
2540 * Return 1 if the refname matches one of the patterns, otherwise 0.
2541 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2542 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2543 * matches "refs/heads/mas*", too).
2545 static int match_pattern(const char **patterns
, const char *refname
,
2551 flags
|= WM_CASEFOLD
;
2554 * When no '--format' option is given we need to skip the prefix
2555 * for matching refs of tags and branches.
2557 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
2558 skip_prefix(refname
, "refs/heads/", &refname
) ||
2559 skip_prefix(refname
, "refs/remotes/", &refname
) ||
2560 skip_prefix(refname
, "refs/", &refname
));
2562 for (; *patterns
; patterns
++) {
2563 if (!wildmatch(*patterns
, refname
, flags
))
2570 * Return 1 if the refname matches one of the patterns, otherwise 0.
2571 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
2572 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2573 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2575 static int match_name_as_path(const char **pattern
, const char *refname
,
2578 int namelen
= strlen(refname
);
2579 unsigned flags
= WM_PATHNAME
;
2582 flags
|= WM_CASEFOLD
;
2584 for (; *pattern
; pattern
++) {
2585 const char *p
= *pattern
;
2586 int plen
= strlen(p
);
2588 if ((plen
<= namelen
) &&
2589 !strncmp(refname
, p
, plen
) &&
2590 (refname
[plen
] == '\0' ||
2591 refname
[plen
] == '/' ||
2594 if (!wildmatch(p
, refname
, flags
))
2600 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
2601 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
2603 if (!*filter
->name_patterns
)
2604 return 1; /* No pattern always matches */
2605 if (filter
->match_as_path
)
2606 return match_name_as_path(filter
->name_patterns
, refname
,
2607 filter
->ignore_case
);
2608 return match_pattern(filter
->name_patterns
, refname
,
2609 filter
->ignore_case
);
2612 static int filter_exclude_match(struct ref_filter
*filter
, const char *refname
)
2614 if (!filter
->exclude
.nr
)
2616 if (filter
->match_as_path
)
2617 return match_name_as_path(filter
->exclude
.v
, refname
,
2618 filter
->ignore_case
);
2619 return match_pattern(filter
->exclude
.v
, refname
, filter
->ignore_case
);
2623 * This is the same as for_each_fullref_in(), but it tries to iterate
2624 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2625 * pattern match, so the callback still has to match each ref individually.
2627 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
2631 if (!filter
->match_as_path
) {
2633 * in this case, the patterns are applied after
2634 * prefixes like "refs/heads/" etc. are stripped off,
2635 * so we have to look at everything:
2637 return for_each_fullref_in("", cb
, cb_data
);
2640 if (filter
->ignore_case
) {
2642 * we can't handle case-insensitive comparisons,
2643 * so just return everything and let the caller
2646 return for_each_fullref_in("", cb
, cb_data
);
2649 if (!filter
->name_patterns
[0]) {
2650 /* no patterns; we have to look at everything */
2651 return refs_for_each_fullref_in(get_main_ref_store(the_repository
),
2652 "", filter
->exclude
.v
, cb
, cb_data
);
2655 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository
),
2656 NULL
, filter
->name_patterns
,
2662 * Given a ref (oid, refname), check if the ref belongs to the array
2663 * of oids. If the given ref is a tag, check if the given tag points
2664 * at one of the oids in the given oid array. Returns non-zero if a
2668 * As the refs are cached we might know what refname peels to without
2669 * the need to parse the object via parse_object(). peel_ref() might be a
2670 * more efficient alternative to obtain the pointee.
2672 static int match_points_at(struct oid_array
*points_at
,
2673 const struct object_id
*oid
,
2674 const char *refname
)
2678 if (oid_array_lookup(points_at
, oid
) >= 0)
2680 obj
= parse_object_with_flags(the_repository
, oid
,
2681 PARSE_OBJECT_SKIP_HASH_CHECK
);
2682 while (obj
&& obj
->type
== OBJ_TAG
) {
2683 struct tag
*tag
= (struct tag
*)obj
;
2685 if (parse_tag(tag
) < 0) {
2690 if (oid_array_lookup(points_at
, get_tagged_oid(tag
)) >= 0)
2696 die(_("malformed object at '%s'"), refname
);
2701 * Allocate space for a new ref_array_item and copy the name and oid to it.
2703 * Callers can then fill in other struct members at their leisure.
2705 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2706 const struct object_id
*oid
)
2708 struct ref_array_item
*ref
;
2710 FLEX_ALLOC_STR(ref
, refname
, refname
);
2711 oidcpy(&ref
->objectname
, oid
);
2717 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2718 const char *refname
,
2719 const struct object_id
*oid
)
2721 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2723 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2724 array
->items
[array
->nr
++] = ref
;
2729 static int ref_kind_from_refname(const char *refname
)
2737 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2738 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2739 { "refs/tags/", FILTER_REFS_TAGS
}
2742 if (!strcmp(refname
, "HEAD"))
2743 return FILTER_REFS_DETACHED_HEAD
;
2745 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2746 if (starts_with(refname
, ref_kind
[i
].prefix
))
2747 return ref_kind
[i
].kind
;
2750 return FILTER_REFS_OTHERS
;
2753 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2755 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2756 filter
->kind
== FILTER_REFS_REMOTES
||
2757 filter
->kind
== FILTER_REFS_TAGS
)
2758 return filter
->kind
;
2759 return ref_kind_from_refname(refname
);
2762 struct ref_filter_cbdata
{
2763 struct ref_array
*array
;
2764 struct ref_filter
*filter
;
2765 struct contains_cache contains_cache
;
2766 struct contains_cache no_contains_cache
;
2770 * A call-back given to for_each_ref(). Filter refs and keep them for
2771 * later object processing.
2773 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2775 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2776 struct ref_filter
*filter
= ref_cbdata
->filter
;
2777 struct ref_array_item
*ref
;
2778 struct commit
*commit
= NULL
;
2781 if (flag
& REF_BAD_NAME
) {
2782 warning(_("ignoring ref with broken name %s"), refname
);
2786 if (flag
& REF_ISBROKEN
) {
2787 warning(_("ignoring broken ref %s"), refname
);
2791 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2792 kind
= filter_ref_kind(filter
, refname
);
2793 if (!(kind
& filter
->kind
))
2796 if (!filter_pattern_match(filter
, refname
))
2799 if (filter_exclude_match(filter
, refname
))
2802 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2806 * A merge filter is applied on refs pointing to commits. Hence
2807 * obtain the commit using the 'oid' available and discard all
2808 * non-commits early. The actual filtering is done later.
2810 if (filter
->reachable_from
|| filter
->unreachable_from
||
2811 filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2812 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
2815 /* We perform the filtering for the '--contains' option... */
2816 if (filter
->with_commit
&&
2817 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2819 /* ...or for the `--no-contains' option */
2820 if (filter
->no_commit
&&
2821 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2826 * We do not open the object yet; sort may only need refname
2827 * to do its job and the resulting list may yet to be pruned
2828 * by maxcount logic.
2830 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2831 ref
->commit
= commit
;
2838 /* Free memory allocated for a ref_array_item */
2839 static void free_array_item(struct ref_array_item
*item
)
2841 free((char *)item
->symref
);
2844 for (i
= 0; i
< used_atom_cnt
; i
++)
2845 free((char *)item
->value
[i
].s
);
2852 /* Free all memory allocated for ref_array */
2853 void ref_array_clear(struct ref_array
*array
)
2857 for (i
= 0; i
< array
->nr
; i
++)
2858 free_array_item(array
->items
[i
]);
2859 FREE_AND_NULL(array
->items
);
2860 array
->nr
= array
->alloc
= 0;
2862 for (i
= 0; i
< used_atom_cnt
; i
++) {
2863 struct used_atom
*atom
= &used_atom
[i
];
2864 if (atom
->atom_type
== ATOM_HEAD
)
2866 free((char *)atom
->name
);
2868 FREE_AND_NULL(used_atom
);
2871 if (ref_to_worktree_map
.worktrees
) {
2872 hashmap_clear_and_free(&(ref_to_worktree_map
.map
),
2873 struct ref_to_worktree_entry
, ent
);
2874 free_worktrees(ref_to_worktree_map
.worktrees
);
2875 ref_to_worktree_map
.worktrees
= NULL
;
2878 FREE_AND_NULL(array
->counts
);
2881 #define EXCLUDE_REACHED 0
2882 #define INCLUDE_REACHED 1
2883 static void reach_filter(struct ref_array
*array
,
2884 struct commit_list
**check_reachable
,
2885 int include_reached
)
2888 struct commit
**to_clear
;
2890 if (!*check_reachable
)
2893 CALLOC_ARRAY(to_clear
, array
->nr
);
2894 for (i
= 0; i
< array
->nr
; i
++) {
2895 struct ref_array_item
*item
= array
->items
[i
];
2896 to_clear
[i
] = item
->commit
;
2899 tips_reachable_from_bases(the_repository
,
2901 to_clear
, array
->nr
,
2907 for (i
= 0; i
< old_nr
; i
++) {
2908 struct ref_array_item
*item
= array
->items
[i
];
2909 struct commit
*commit
= item
->commit
;
2911 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2913 if (is_merged
== include_reached
)
2914 array
->items
[array
->nr
++] = array
->items
[i
];
2916 free_array_item(item
);
2919 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2921 while (*check_reachable
) {
2922 struct commit
*merge_commit
= pop_commit(check_reachable
);
2923 clear_commit_marks(merge_commit
, ALL_REV_FLAGS
);
2929 void filter_ahead_behind(struct repository
*r
,
2930 struct ref_format
*format
,
2931 struct ref_array
*array
)
2933 struct commit
**commits
;
2934 size_t commits_nr
= format
->bases
.nr
+ array
->nr
;
2936 if (!format
->bases
.nr
|| !array
->nr
)
2939 ALLOC_ARRAY(commits
, commits_nr
);
2940 for (size_t i
= 0; i
< format
->bases
.nr
; i
++)
2941 commits
[i
] = format
->bases
.items
[i
].util
;
2943 ALLOC_ARRAY(array
->counts
, st_mult(format
->bases
.nr
, array
->nr
));
2945 commits_nr
= format
->bases
.nr
;
2946 array
->counts_nr
= 0;
2947 for (size_t i
= 0; i
< array
->nr
; i
++) {
2948 const char *name
= array
->items
[i
]->refname
;
2949 commits
[commits_nr
] = lookup_commit_reference_by_name(name
);
2951 if (!commits
[commits_nr
])
2954 CALLOC_ARRAY(array
->items
[i
]->counts
, format
->bases
.nr
);
2955 for (size_t j
= 0; j
< format
->bases
.nr
; j
++) {
2956 struct ahead_behind_count
*count
;
2957 count
= &array
->counts
[array
->counts_nr
++];
2958 count
->tip_index
= commits_nr
;
2959 count
->base_index
= j
;
2961 array
->items
[i
]->counts
[j
] = count
;
2966 ahead_behind(r
, commits
, commits_nr
, array
->counts
, array
->counts_nr
);
2971 * API for filtering a set of refs. Based on the type of refs the user
2972 * has requested, we iterate through those refs and apply filters
2973 * as per the given ref_filter structure and finally store the
2974 * filtered refs in the ref_array structure.
2976 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2978 struct ref_filter_cbdata ref_cbdata
;
2979 int save_commit_buffer_orig
;
2982 ref_cbdata
.array
= array
;
2983 ref_cbdata
.filter
= filter
;
2985 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2987 save_commit_buffer_orig
= save_commit_buffer
;
2988 save_commit_buffer
= 0;
2990 init_contains_cache(&ref_cbdata
.contains_cache
);
2991 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2993 /* Simple per-ref filtering */
2995 die("filter_refs: invalid type");
2998 * For common cases where we need only branches or remotes or tags,
2999 * we only iterate through those refs. If a mix of refs is needed,
3000 * we iterate over all refs and filter out required refs with the help
3001 * of filter_ref_kind().
3003 if (filter
->kind
== FILTER_REFS_BRANCHES
)
3004 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
);
3005 else if (filter
->kind
== FILTER_REFS_REMOTES
)
3006 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
);
3007 else if (filter
->kind
== FILTER_REFS_TAGS
)
3008 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
);
3009 else if (filter
->kind
& FILTER_REFS_ALL
)
3010 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
);
3011 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
3012 head_ref(ref_filter_handler
, &ref_cbdata
);
3015 clear_contains_cache(&ref_cbdata
.contains_cache
);
3016 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
3018 /* Filters that need revision walking */
3019 reach_filter(array
, &filter
->reachable_from
, INCLUDE_REACHED
);
3020 reach_filter(array
, &filter
->unreachable_from
, EXCLUDE_REACHED
);
3022 save_commit_buffer
= save_commit_buffer_orig
;
3026 static int compare_detached_head(struct ref_array_item
*a
, struct ref_array_item
*b
)
3028 if (!(a
->kind
^ b
->kind
))
3029 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
3030 if (a
->kind
& FILTER_REFS_DETACHED_HEAD
)
3032 else if (b
->kind
& FILTER_REFS_DETACHED_HEAD
)
3034 BUG("should have died in the xor check above");
3038 static int memcasecmp(const void *vs1
, const void *vs2
, size_t n
)
3040 const char *s1
= vs1
, *s2
= vs2
;
3041 const char *end
= s1
+ n
;
3043 for (; s1
< end
; s1
++, s2
++) {
3044 int diff
= tolower(*s1
) - tolower(*s2
);
3051 struct ref_sorting
{
3052 struct ref_sorting
*next
;
3053 int atom
; /* index into used_atom array (internal) */
3054 enum ref_sorting_order sort_flags
;
3057 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
3059 struct atom_value
*va
, *vb
;
3061 int cmp_detached_head
= 0;
3062 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
3063 struct strbuf err
= STRBUF_INIT
;
3065 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
3067 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
3069 strbuf_release(&err
);
3070 if (s
->sort_flags
& REF_SORTING_DETACHED_HEAD_FIRST
&&
3071 ((a
->kind
| b
->kind
) & FILTER_REFS_DETACHED_HEAD
)) {
3072 cmp
= compare_detached_head(a
, b
);
3073 cmp_detached_head
= 1;
3074 } else if (s
->sort_flags
& REF_SORTING_VERSION
) {
3075 cmp
= versioncmp(va
->s
, vb
->s
);
3076 } else if (cmp_type
== FIELD_STR
) {
3077 if (va
->s_size
< 0 && vb
->s_size
< 0) {
3078 int (*cmp_fn
)(const char *, const char *);
3079 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
3080 ? strcasecmp
: strcmp
;
3081 cmp
= cmp_fn(va
->s
, vb
->s
);
3083 size_t a_size
= va
->s_size
< 0 ?
3084 strlen(va
->s
) : va
->s_size
;
3085 size_t b_size
= vb
->s_size
< 0 ?
3086 strlen(vb
->s
) : vb
->s_size
;
3087 int (*cmp_fn
)(const void *, const void *, size_t);
3088 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
3089 ? memcasecmp
: memcmp
;
3091 cmp
= cmp_fn(va
->s
, vb
->s
, b_size
> a_size
?
3094 if (a_size
> b_size
)
3096 else if (a_size
< b_size
)
3101 if (va
->value
< vb
->value
)
3103 else if (va
->value
== vb
->value
)
3109 return (s
->sort_flags
& REF_SORTING_REVERSE
&& !cmp_detached_head
)
3113 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
3115 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
3116 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
3117 struct ref_sorting
*s
;
3119 for (s
= ref_sorting
; s
; s
= s
->next
) {
3120 int cmp
= cmp_ref_sorting(s
, a
, b
);
3125 return s
&& s
->sort_flags
& REF_SORTING_ICASE
?
3126 strcasecmp(a
->refname
, b
->refname
) :
3127 strcmp(a
->refname
, b
->refname
);
3130 void ref_sorting_set_sort_flags_all(struct ref_sorting
*sorting
,
3131 unsigned int mask
, int on
)
3133 for (; sorting
; sorting
= sorting
->next
) {
3135 sorting
->sort_flags
|= mask
;
3137 sorting
->sort_flags
&= ~mask
;
3141 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
3143 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
3146 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
3148 struct strbuf
*s
= &state
->stack
->output
;
3150 while (*cp
&& (!ep
|| cp
< ep
)) {
3155 int ch
= hex2chr(cp
+ 1);
3157 strbuf_addch(s
, ch
);
3163 strbuf_addch(s
, *cp
);
3168 int format_ref_array_item(struct ref_array_item
*info
,
3169 struct ref_format
*format
,
3170 struct strbuf
*final_buf
,
3171 struct strbuf
*error_buf
)
3173 const char *cp
, *sp
, *ep
;
3174 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
3176 state
.quote_style
= format
->quote_style
;
3177 push_stack_element(&state
.stack
);
3179 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
3180 struct atom_value
*atomv
;
3183 ep
= strchr(sp
, ')');
3185 append_literal(cp
, sp
, &state
);
3186 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
3187 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
3188 atomv
->handler(atomv
, &state
, error_buf
)) {
3189 pop_stack_element(&state
.stack
);
3194 sp
= cp
+ strlen(cp
);
3195 append_literal(cp
, sp
, &state
);
3197 if (format
->need_color_reset_at_eol
) {
3198 struct atom_value resetv
= ATOM_VALUE_INIT
;
3199 resetv
.s
= GIT_COLOR_RESET
;
3200 if (append_atom(&resetv
, &state
, error_buf
)) {
3201 pop_stack_element(&state
.stack
);
3205 if (state
.stack
->prev
) {
3206 pop_stack_element(&state
.stack
);
3207 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
3209 strbuf_addbuf(final_buf
, &state
.stack
->output
);
3210 pop_stack_element(&state
.stack
);
3214 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
3215 struct ref_format
*format
)
3217 struct ref_array_item
*ref_item
;
3218 struct strbuf output
= STRBUF_INIT
;
3219 struct strbuf err
= STRBUF_INIT
;
3221 ref_item
= new_ref_array_item(name
, oid
);
3222 ref_item
->kind
= ref_kind_from_refname(name
);
3223 if (format_ref_array_item(ref_item
, format
, &output
, &err
))
3225 fwrite(output
.buf
, 1, output
.len
, stdout
);
3228 strbuf_release(&err
);
3229 strbuf_release(&output
);
3230 free_array_item(ref_item
);
3233 static int parse_sorting_atom(const char *atom
)
3236 * This parses an atom using a dummy ref_format, since we don't
3237 * actually care about the formatting details.
3239 struct ref_format dummy
= REF_FORMAT_INIT
;
3240 const char *end
= atom
+ strlen(atom
);
3241 struct strbuf err
= STRBUF_INIT
;
3242 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
3245 strbuf_release(&err
);
3249 /* If no sorting option is given, use refname to sort as default */
3250 static struct ref_sorting
*ref_default_sorting(void)
3252 static const char cstr_name
[] = "refname";
3254 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
3256 sorting
->next
= NULL
;
3257 sorting
->atom
= parse_sorting_atom(cstr_name
);
3261 static void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
3263 struct ref_sorting
*s
;
3266 s
->next
= *sorting_tail
;
3270 s
->sort_flags
|= REF_SORTING_REVERSE
;
3273 if (skip_prefix(arg
, "version:", &arg
) ||
3274 skip_prefix(arg
, "v:", &arg
))
3275 s
->sort_flags
|= REF_SORTING_VERSION
;
3276 s
->atom
= parse_sorting_atom(arg
);
3279 struct ref_sorting
*ref_sorting_options(struct string_list
*options
)
3281 struct string_list_item
*item
;
3282 struct ref_sorting
*sorting
= NULL
, **tail
= &sorting
;
3285 sorting
= ref_default_sorting();
3287 for_each_string_list_item(item
, options
)
3288 parse_ref_sorting(tail
, item
->string
);
3292 * From here on, the ref_sorting list should be used to talk
3293 * about the sort order used for the output. The caller
3294 * should not touch the string form anymore.
3296 string_list_clear(options
, 0);
3300 void ref_sorting_release(struct ref_sorting
*sorting
)
3303 struct ref_sorting
*next
= sorting
->next
;
3309 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
3311 struct ref_filter
*rf
= opt
->value
;
3312 struct object_id oid
;
3313 struct commit
*merge_commit
;
3315 BUG_ON_OPT_NEG(unset
);
3317 if (repo_get_oid(the_repository
, arg
, &oid
))
3318 die(_("malformed object name %s"), arg
);
3320 merge_commit
= lookup_commit_reference_gently(the_repository
, &oid
, 0);
3323 return error(_("option `%s' must point to a commit"), opt
->long_name
);
3325 if (starts_with(opt
->long_name
, "no"))
3326 commit_list_insert(merge_commit
, &rf
->unreachable_from
);
3328 commit_list_insert(merge_commit
, &rf
->reachable_from
);
3333 void ref_filter_init(struct ref_filter
*filter
)
3335 struct ref_filter blank
= REF_FILTER_INIT
;
3336 memcpy(filter
, &blank
, sizeof(blank
));
3339 void ref_filter_clear(struct ref_filter
*filter
)
3341 strvec_clear(&filter
->exclude
);
3342 oid_array_clear(&filter
->points_at
);
3343 free_commit_list(filter
->with_commit
);
3344 free_commit_list(filter
->no_commit
);
3345 free_commit_list(filter
->reachable_from
);
3346 free_commit_list(filter
->unreachable_from
);
3347 ref_filter_init(filter
);