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"
20 #include "ref-filter.h"
24 #include "versioncmp.h"
26 #include "wt-status.h"
27 #include "commit-slab.h"
28 #include "commit-graph.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
;
218 struct email_option
{
219 enum { EO_RAW
, EO_TRIM
, EO_LOCALPART
} option
;
222 enum { S_BARE
, S_GRADE
, S_SIGNER
, S_KEY
,
223 S_FINGERPRINT
, S_PRI_KEY_FP
, S_TRUST_LEVEL
} option
;
225 const char **describe_args
;
226 struct refname_atom refname
;
230 static int used_atom_cnt
, need_tagged
, need_symref
;
233 * Expand string, append it to strbuf *sb, then return error code ret.
234 * Allow to save few lines of code.
236 __attribute__((format (printf
, 3, 4)))
237 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
241 strbuf_vaddf(sb
, fmt
, ap
);
246 static int err_no_arg(struct strbuf
*sb
, const char *name
)
248 size_t namelen
= strchrnul(name
, ':') - name
;
249 strbuf_addf(sb
, _("%%(%.*s) does not take arguments"),
254 static int err_bad_arg(struct strbuf
*sb
, const char *name
, const char *arg
)
256 size_t namelen
= strchrnul(name
, ':') - name
;
257 strbuf_addf(sb
, _("unrecognized %%(%.*s) argument: %s"),
258 (int)namelen
, name
, arg
);
263 * Parse option of name "candidate" in the option string "to_parse" of
266 * "candidate1[=val1],candidate2[=val2],candidate3[=val3],..."
268 * The remaining part of "to_parse" is stored in "end" (if we are
269 * parsing the last candidate, then this is NULL) and the value of
270 * the candidate is stored in "valuestart" and its length in "valuelen",
271 * that is the portion after "=". Since it is possible for a "candidate"
272 * to not have a value, in such cases, "valuestart" is set to point to
273 * NULL and "valuelen" to 0.
275 * The function returns 1 on success. It returns 0 if we don't find
276 * "candidate" in "to_parse" or we find "candidate" but it is followed
277 * by more chars (for example, "candidatefoo"), that is, we don't find
280 * This function only does the above for one "candidate" at a time. So
281 * it has to be called each time trying to parse a "candidate" in the
282 * option string "to_parse".
284 static int match_atom_arg_value(const char *to_parse
, const char *candidate
,
285 const char **end
, const char **valuestart
,
290 if (!skip_prefix(to_parse
, candidate
, &atom
))
291 return 0; /* definitely not "candidate" */
294 /* we just saw "candidate=" */
295 *valuestart
= atom
+ 1;
296 atom
= strchrnul(*valuestart
, ',');
297 *valuelen
= atom
- *valuestart
;
298 } else if (*atom
!= ',' && *atom
!= '\0') {
299 /* key begins with "candidate" but has more chars */
302 /* just "candidate" without "=val" */
307 /* atom points at either the ',' or NUL after this key[=val] */
311 BUG("Why is *atom not NULL yet?");
318 * Parse boolean option of name "candidate" in the option list "to_parse"
321 * "candidate1[=bool1],candidate2[=bool2],candidate3[=bool3],..."
323 * The remaining part of "to_parse" is stored in "end" (if we are parsing
324 * the last candidate, then this is NULL) and the value (if given) is
325 * parsed and stored in "val", so "val" always points to either 0 or 1.
326 * If the value is not given, then "val" is set to point to 1.
328 * The boolean value is parsed using "git_parse_maybe_bool()", so the
329 * accepted values are
331 * to set true - "1", "yes", "true"
332 * to set false - "0", "no", "false"
334 * This function returns 1 on success. It returns 0 when we don't find
335 * an exact match for "candidate" or when the boolean value given is
338 static int match_atom_bool_arg(const char *to_parse
, const char *candidate
,
339 const char **end
, int *val
)
346 if (!match_atom_arg_value(to_parse
, candidate
, end
, &argval
, &arglen
))
354 strval
= xstrndup(argval
, arglen
);
355 v
= git_parse_maybe_bool(strval
);
366 static int color_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
367 const char *color_value
, struct strbuf
*err
)
370 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
371 if (color_parse(color_value
, atom
->u
.color
) < 0)
372 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
375 * We check this after we've parsed the color, which lets us complain
376 * about syntactically bogus color names even if they won't be used.
378 if (!want_color(format
->use_color
))
379 color_parse("", atom
->u
.color
);
383 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
384 const char *name
, struct strbuf
*err
)
387 atom
->option
= R_NORMAL
;
388 else if (!strcmp(arg
, "short"))
389 atom
->option
= R_SHORT
;
390 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
391 skip_prefix(arg
, "strip=", &arg
)) {
392 atom
->option
= R_LSTRIP
;
393 if (strtol_i(arg
, 10, &atom
->lstrip
))
394 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
395 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
396 atom
->option
= R_RSTRIP
;
397 if (strtol_i(arg
, 10, &atom
->rstrip
))
398 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
400 return err_bad_arg(err
, name
, arg
);
404 static int remote_ref_atom_parser(struct ref_format
*format UNUSED
,
405 struct used_atom
*atom
,
406 const char *arg
, struct strbuf
*err
)
408 struct string_list params
= STRING_LIST_INIT_DUP
;
411 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
412 atom
->u
.remote_ref
.push
= 1;
415 atom
->u
.remote_ref
.option
= RR_REF
;
416 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
417 arg
, atom
->name
, err
);
420 atom
->u
.remote_ref
.nobracket
= 0;
421 string_list_split(¶ms
, arg
, ',', -1);
423 for (i
= 0; i
< params
.nr
; i
++) {
424 const char *s
= params
.items
[i
].string
;
426 if (!strcmp(s
, "track"))
427 atom
->u
.remote_ref
.option
= RR_TRACK
;
428 else if (!strcmp(s
, "trackshort"))
429 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
430 else if (!strcmp(s
, "nobracket"))
431 atom
->u
.remote_ref
.nobracket
= 1;
432 else if (!strcmp(s
, "remotename")) {
433 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
434 atom
->u
.remote_ref
.push_remote
= 1;
435 } else if (!strcmp(s
, "remoteref")) {
436 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
437 atom
->u
.remote_ref
.push_remote
= 1;
439 atom
->u
.remote_ref
.option
= RR_REF
;
440 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
441 arg
, atom
->name
, err
)) {
442 string_list_clear(¶ms
, 0);
448 string_list_clear(¶ms
, 0);
452 static int objecttype_atom_parser(struct ref_format
*format UNUSED
,
453 struct used_atom
*atom
,
454 const char *arg
, struct strbuf
*err
)
457 return err_no_arg(err
, "objecttype");
458 if (*atom
->name
== '*')
459 oi_deref
.info
.typep
= &oi_deref
.type
;
461 oi
.info
.typep
= &oi
.type
;
465 static int objectsize_atom_parser(struct ref_format
*format UNUSED
,
466 struct used_atom
*atom
,
467 const char *arg
, struct strbuf
*err
)
470 atom
->u
.objectsize
.option
= O_SIZE
;
471 if (*atom
->name
== '*')
472 oi_deref
.info
.sizep
= &oi_deref
.size
;
474 oi
.info
.sizep
= &oi
.size
;
475 } else if (!strcmp(arg
, "disk")) {
476 atom
->u
.objectsize
.option
= O_SIZE_DISK
;
477 if (*atom
->name
== '*')
478 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
480 oi
.info
.disk_sizep
= &oi
.disk_size
;
482 return err_bad_arg(err
, "objectsize", arg
);
486 static int deltabase_atom_parser(struct ref_format
*format UNUSED
,
487 struct used_atom
*atom
,
488 const char *arg
, struct strbuf
*err
)
491 return err_no_arg(err
, "deltabase");
492 if (*atom
->name
== '*')
493 oi_deref
.info
.delta_base_oid
= &oi_deref
.delta_base_oid
;
495 oi
.info
.delta_base_oid
= &oi
.delta_base_oid
;
499 static int body_atom_parser(struct ref_format
*format UNUSED
,
500 struct used_atom
*atom
,
501 const char *arg
, struct strbuf
*err
)
504 return err_no_arg(err
, "body");
505 atom
->u
.contents
.option
= C_BODY_DEP
;
509 static int subject_atom_parser(struct ref_format
*format UNUSED
,
510 struct used_atom
*atom
,
511 const char *arg
, struct strbuf
*err
)
514 atom
->u
.contents
.option
= C_SUB
;
515 else if (!strcmp(arg
, "sanitize"))
516 atom
->u
.contents
.option
= C_SUB_SANITIZE
;
518 return err_bad_arg(err
, "subject", arg
);
522 static int parse_signature_option(const char *arg
)
526 else if (!strcmp(arg
, "signer"))
528 else if (!strcmp(arg
, "grade"))
530 else if (!strcmp(arg
, "key"))
532 else if (!strcmp(arg
, "fingerprint"))
533 return S_FINGERPRINT
;
534 else if (!strcmp(arg
, "primarykeyfingerprint"))
536 else if (!strcmp(arg
, "trustlevel"))
537 return S_TRUST_LEVEL
;
541 static int signature_atom_parser(struct ref_format
*format UNUSED
,
542 struct used_atom
*atom
,
543 const char *arg
, struct strbuf
*err
)
545 int opt
= parse_signature_option(arg
);
547 return err_bad_arg(err
, "signature", arg
);
548 atom
->u
.signature
.option
= opt
;
552 static int trailers_atom_parser(struct ref_format
*format UNUSED
,
553 struct used_atom
*atom
,
554 const char *arg
, struct strbuf
*err
)
556 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
559 const char *argbuf
= xstrfmt("%s)", arg
);
560 char *invalid_arg
= NULL
;
562 if (format_set_trailers_options(&atom
->u
.contents
.trailer_opts
,
563 &ref_trailer_buf
.filter_list
,
564 &ref_trailer_buf
.sepbuf
,
565 &ref_trailer_buf
.kvsepbuf
,
566 &argbuf
, &invalid_arg
)) {
568 strbuf_addf(err
, _("expected %%(trailers:key=<value>)"));
570 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), invalid_arg
);
571 free((char *)invalid_arg
);
575 atom
->u
.contents
.option
= C_TRAILERS
;
579 static int contents_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
580 const char *arg
, struct strbuf
*err
)
583 atom
->u
.contents
.option
= C_BARE
;
584 else if (!strcmp(arg
, "body"))
585 atom
->u
.contents
.option
= C_BODY
;
586 else if (!strcmp(arg
, "size"))
587 atom
->u
.contents
.option
= C_LENGTH
;
588 else if (!strcmp(arg
, "signature"))
589 atom
->u
.contents
.option
= C_SIG
;
590 else if (!strcmp(arg
, "subject"))
591 atom
->u
.contents
.option
= C_SUB
;
592 else if (!strcmp(arg
, "trailers")) {
593 if (trailers_atom_parser(format
, atom
, NULL
, err
))
595 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
596 if (trailers_atom_parser(format
, atom
, arg
, err
))
598 } else if (skip_prefix(arg
, "lines=", &arg
)) {
599 atom
->u
.contents
.option
= C_LINES
;
600 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
601 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
603 return err_bad_arg(err
, "contents", arg
);
607 static int describe_atom_option_parser(struct strvec
*args
, const char **arg
,
614 if (match_atom_bool_arg(*arg
, "tags", arg
, &optval
)) {
616 strvec_push(args
, "--no-tags");
618 strvec_push(args
, "--tags");
622 if (match_atom_arg_value(*arg
, "abbrev", arg
, &argval
, &arglen
)) {
626 return strbuf_addf_ret(err
, -1,
627 _("argument expected for %s"),
629 if (strtol(argval
, &endptr
, 10) < 0)
630 return strbuf_addf_ret(err
, -1,
631 _("positive value expected %s=%s"),
632 "describe:abbrev", argval
);
633 if (endptr
- argval
!= arglen
)
634 return strbuf_addf_ret(err
, -1,
635 _("cannot fully parse %s=%s"),
636 "describe:abbrev", argval
);
638 strvec_pushf(args
, "--abbrev=%.*s", (int)arglen
, argval
);
642 if (match_atom_arg_value(*arg
, "match", arg
, &argval
, &arglen
)) {
644 return strbuf_addf_ret(err
, -1,
645 _("value expected %s="),
648 strvec_pushf(args
, "--match=%.*s", (int)arglen
, argval
);
652 if (match_atom_arg_value(*arg
, "exclude", arg
, &argval
, &arglen
)) {
654 return strbuf_addf_ret(err
, -1,
655 _("value expected %s="),
658 strvec_pushf(args
, "--exclude=%.*s", (int)arglen
, argval
);
665 static int describe_atom_parser(struct ref_format
*format UNUSED
,
666 struct used_atom
*atom
,
667 const char *arg
, struct strbuf
*err
)
669 struct strvec args
= STRVEC_INIT
;
673 const char *bad_arg
= arg
;
678 found
= describe_atom_option_parser(&args
, &arg
, err
);
682 return err_bad_arg(err
, "describe", bad_arg
);
684 atom
->u
.describe_args
= strvec_detach(&args
);
688 static int raw_atom_parser(struct ref_format
*format UNUSED
,
689 struct used_atom
*atom
,
690 const char *arg
, struct strbuf
*err
)
693 atom
->u
.raw_data
.option
= RAW_BARE
;
694 else if (!strcmp(arg
, "size"))
695 atom
->u
.raw_data
.option
= RAW_LENGTH
;
697 return err_bad_arg(err
, "raw", arg
);
701 static int oid_atom_parser(struct ref_format
*format UNUSED
,
702 struct used_atom
*atom
,
703 const char *arg
, struct strbuf
*err
)
706 atom
->u
.oid
.option
= O_FULL
;
707 else if (!strcmp(arg
, "short"))
708 atom
->u
.oid
.option
= O_SHORT
;
709 else if (skip_prefix(arg
, "short=", &arg
)) {
710 atom
->u
.oid
.option
= O_LENGTH
;
711 if (strtoul_ui(arg
, 10, &atom
->u
.oid
.length
) ||
712 atom
->u
.oid
.length
== 0)
713 return strbuf_addf_ret(err
, -1, _("positive value expected '%s' in %%(%s)"), arg
, atom
->name
);
714 if (atom
->u
.oid
.length
< MINIMUM_ABBREV
)
715 atom
->u
.oid
.length
= MINIMUM_ABBREV
;
717 return err_bad_arg(err
, atom
->name
, arg
);
721 static int person_email_atom_parser(struct ref_format
*format UNUSED
,
722 struct used_atom
*atom
,
723 const char *arg
, struct strbuf
*err
)
726 atom
->u
.email_option
.option
= EO_RAW
;
727 else if (!strcmp(arg
, "trim"))
728 atom
->u
.email_option
.option
= EO_TRIM
;
729 else if (!strcmp(arg
, "localpart"))
730 atom
->u
.email_option
.option
= EO_LOCALPART
;
732 return err_bad_arg(err
, atom
->name
, arg
);
736 static int refname_atom_parser(struct ref_format
*format UNUSED
,
737 struct used_atom
*atom
,
738 const char *arg
, struct strbuf
*err
)
740 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
743 static align_type
parse_align_position(const char *s
)
745 if (!strcmp(s
, "right"))
747 else if (!strcmp(s
, "middle"))
749 else if (!strcmp(s
, "left"))
754 static int align_atom_parser(struct ref_format
*format UNUSED
,
755 struct used_atom
*atom
,
756 const char *arg
, struct strbuf
*err
)
758 struct align
*align
= &atom
->u
.align
;
759 struct string_list params
= STRING_LIST_INIT_DUP
;
761 unsigned int width
= ~0U;
764 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
766 align
->position
= ALIGN_LEFT
;
768 string_list_split(¶ms
, arg
, ',', -1);
769 for (i
= 0; i
< params
.nr
; i
++) {
770 const char *s
= params
.items
[i
].string
;
773 if (skip_prefix(s
, "position=", &s
)) {
774 position
= parse_align_position(s
);
776 strbuf_addf(err
, _("unrecognized position:%s"), s
);
777 string_list_clear(¶ms
, 0);
780 align
->position
= position
;
781 } else if (skip_prefix(s
, "width=", &s
)) {
782 if (strtoul_ui(s
, 10, &width
)) {
783 strbuf_addf(err
, _("unrecognized width:%s"), s
);
784 string_list_clear(¶ms
, 0);
787 } else if (!strtoul_ui(s
, 10, &width
))
789 else if ((position
= parse_align_position(s
)) >= 0)
790 align
->position
= position
;
792 strbuf_addf(err
, _("unrecognized %%(%s) argument: %s"), "align", s
);
793 string_list_clear(¶ms
, 0);
799 string_list_clear(¶ms
, 0);
800 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
802 align
->width
= width
;
803 string_list_clear(¶ms
, 0);
807 static int if_atom_parser(struct ref_format
*format UNUSED
,
808 struct used_atom
*atom
,
809 const char *arg
, struct strbuf
*err
)
812 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
814 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
815 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
816 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
817 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
819 return err_bad_arg(err
, "if", arg
);
823 static int rest_atom_parser(struct ref_format
*format UNUSED
,
824 struct used_atom
*atom UNUSED
,
825 const char *arg
, struct strbuf
*err
)
828 return err_no_arg(err
, "rest");
832 static int ahead_behind_atom_parser(struct ref_format
*format
,
833 struct used_atom
*atom UNUSED
,
834 const char *arg
, struct strbuf
*err
)
836 struct string_list_item
*item
;
839 return strbuf_addf_ret(err
, -1, _("expected format: %%(ahead-behind:<committish>)"));
841 item
= string_list_append(&format
->bases
, arg
);
842 item
->util
= lookup_commit_reference_by_name(arg
);
844 die("failed to find '%s'", arg
);
849 static int head_atom_parser(struct ref_format
*format UNUSED
,
850 struct used_atom
*atom
,
851 const char *arg
, struct strbuf
*err
)
854 return err_no_arg(err
, "HEAD");
855 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
863 int (*parser
)(struct ref_format
*format
, struct used_atom
*atom
,
864 const char *arg
, struct strbuf
*err
);
866 [ATOM_REFNAME
] = { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
867 [ATOM_OBJECTTYPE
] = { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
868 [ATOM_OBJECTSIZE
] = { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
869 [ATOM_OBJECTNAME
] = { "objectname", SOURCE_OTHER
, FIELD_STR
, oid_atom_parser
},
870 [ATOM_DELTABASE
] = { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
871 [ATOM_TREE
] = { "tree", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
872 [ATOM_PARENT
] = { "parent", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
873 [ATOM_NUMPARENT
] = { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
874 [ATOM_OBJECT
] = { "object", SOURCE_OBJ
},
875 [ATOM_TYPE
] = { "type", SOURCE_OBJ
},
876 [ATOM_TAG
] = { "tag", SOURCE_OBJ
},
877 [ATOM_AUTHOR
] = { "author", SOURCE_OBJ
},
878 [ATOM_AUTHORNAME
] = { "authorname", SOURCE_OBJ
},
879 [ATOM_AUTHOREMAIL
] = { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
880 [ATOM_AUTHORDATE
] = { "authordate", SOURCE_OBJ
, FIELD_TIME
},
881 [ATOM_COMMITTER
] = { "committer", SOURCE_OBJ
},
882 [ATOM_COMMITTERNAME
] = { "committername", SOURCE_OBJ
},
883 [ATOM_COMMITTEREMAIL
] = { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
884 [ATOM_COMMITTERDATE
] = { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
885 [ATOM_TAGGER
] = { "tagger", SOURCE_OBJ
},
886 [ATOM_TAGGERNAME
] = { "taggername", SOURCE_OBJ
},
887 [ATOM_TAGGEREMAIL
] = { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
888 [ATOM_TAGGERDATE
] = { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
889 [ATOM_CREATOR
] = { "creator", SOURCE_OBJ
},
890 [ATOM_CREATORDATE
] = { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
891 [ATOM_DESCRIBE
] = { "describe", SOURCE_OBJ
, FIELD_STR
, describe_atom_parser
},
892 [ATOM_SUBJECT
] = { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
893 [ATOM_BODY
] = { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
894 [ATOM_TRAILERS
] = { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
895 [ATOM_CONTENTS
] = { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
896 [ATOM_SIGNATURE
] = { "signature", SOURCE_OBJ
, FIELD_STR
, signature_atom_parser
},
897 [ATOM_RAW
] = { "raw", SOURCE_OBJ
, FIELD_STR
, raw_atom_parser
},
898 [ATOM_UPSTREAM
] = { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
899 [ATOM_PUSH
] = { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
900 [ATOM_SYMREF
] = { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
901 [ATOM_FLAG
] = { "flag", SOURCE_NONE
},
902 [ATOM_HEAD
] = { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
903 [ATOM_COLOR
] = { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
904 [ATOM_WORKTREEPATH
] = { "worktreepath", SOURCE_NONE
},
905 [ATOM_ALIGN
] = { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
906 [ATOM_END
] = { "end", SOURCE_NONE
},
907 [ATOM_IF
] = { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
908 [ATOM_THEN
] = { "then", SOURCE_NONE
},
909 [ATOM_ELSE
] = { "else", SOURCE_NONE
},
910 [ATOM_REST
] = { "rest", SOURCE_NONE
, FIELD_STR
, rest_atom_parser
},
911 [ATOM_AHEADBEHIND
] = { "ahead-behind", SOURCE_OTHER
, FIELD_STR
, ahead_behind_atom_parser
},
913 * Please update $__git_ref_fieldlist in git-completion.bash
914 * when you add new atoms
918 #define REF_FORMATTING_STATE_INIT { 0 }
920 struct ref_formatting_stack
{
921 struct ref_formatting_stack
*prev
;
922 struct strbuf output
;
923 void (*at_end
)(struct ref_formatting_stack
**stack
);
927 struct ref_formatting_state
{
929 struct ref_formatting_stack
*stack
;
935 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
937 uintmax_t value
; /* used for sorting when not FIELD_STR */
938 struct used_atom
*atom
;
941 #define ATOM_SIZE_UNSPECIFIED (-1)
943 #define ATOM_VALUE_INIT { \
944 .s_size = ATOM_SIZE_UNSPECIFIED \
948 * Used to parse format string and sort specifiers
950 static int parse_ref_filter_atom(struct ref_format
*format
,
951 const char *atom
, const char *ep
,
959 if (*sp
== '*' && sp
< ep
)
962 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
963 (int)(ep
-atom
), atom
);
966 * If the atom name has a colon, strip it and everything after
967 * it off - it specifies the format for this entry, and
968 * shouldn't be used for checking against the valid_atom
971 arg
= memchr(sp
, ':', ep
- sp
);
972 atom_len
= (arg
? arg
: ep
) - sp
;
974 /* Do we have the atom already used elsewhere? */
975 for (i
= 0; i
< used_atom_cnt
; i
++) {
976 int len
= strlen(used_atom
[i
].name
);
977 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
981 /* Is the atom a valid one? */
982 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
983 int len
= strlen(valid_atom
[i
].name
);
984 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
988 if (ARRAY_SIZE(valid_atom
) <= i
)
989 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
990 (int)(ep
-atom
), atom
);
991 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
992 return strbuf_addf_ret(err
, -1,
993 _("not a git repository, but the field '%.*s' requires access to object data"),
994 (int)(ep
-atom
), atom
);
996 /* Add it in, including the deref prefix */
999 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
1000 used_atom
[at
].atom_type
= i
;
1001 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
1002 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
1003 used_atom
[at
].source
= valid_atom
[i
].source
;
1004 if (used_atom
[at
].source
== SOURCE_OBJ
) {
1006 oi_deref
.info
.contentp
= &oi_deref
.content
;
1008 oi
.info
.contentp
= &oi
.content
;
1011 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
1014 * Treat empty sub-arguments list as NULL (i.e.,
1015 * "%(atom:)" is equivalent to "%(atom)").
1020 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
1021 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
1025 if (i
== ATOM_SYMREF
)
1030 static void quote_formatting(struct strbuf
*s
, const char *str
, ssize_t len
, int quote_style
)
1032 switch (quote_style
) {
1035 strbuf_addstr(s
, str
);
1037 strbuf_add(s
, str
, len
);
1040 sq_quote_buf(s
, str
);
1044 perl_quote_buf(s
, str
);
1046 perl_quote_buf_with_len(s
, str
, len
);
1049 python_quote_buf(s
, str
);
1052 tcl_quote_buf(s
, str
);
1057 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
1058 struct strbuf
*err UNUSED
)
1061 * Quote formatting is only done when the stack has a single
1062 * element. Otherwise quote formatting is done on the
1063 * element's entire output strbuf when the %(end) atom is
1066 if (!state
->stack
->prev
)
1067 quote_formatting(&state
->stack
->output
, v
->s
, v
->s_size
, state
->quote_style
);
1068 else if (v
->s_size
< 0)
1069 strbuf_addstr(&state
->stack
->output
, v
->s
);
1071 strbuf_add(&state
->stack
->output
, v
->s
, v
->s_size
);
1075 static void push_stack_element(struct ref_formatting_stack
**stack
)
1077 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
1079 strbuf_init(&s
->output
, 0);
1084 static void pop_stack_element(struct ref_formatting_stack
**stack
)
1086 struct ref_formatting_stack
*current
= *stack
;
1087 struct ref_formatting_stack
*prev
= current
->prev
;
1090 strbuf_addbuf(&prev
->output
, ¤t
->output
);
1091 strbuf_release(¤t
->output
);
1096 static void end_align_handler(struct ref_formatting_stack
**stack
)
1098 struct ref_formatting_stack
*cur
= *stack
;
1099 struct align
*align
= (struct align
*)cur
->at_end_data
;
1100 struct strbuf s
= STRBUF_INIT
;
1102 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
1103 strbuf_swap(&cur
->output
, &s
);
1107 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
1108 struct strbuf
*err UNUSED
)
1110 struct ref_formatting_stack
*new_stack
;
1112 push_stack_element(&state
->stack
);
1113 new_stack
= state
->stack
;
1114 new_stack
->at_end
= end_align_handler
;
1115 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
1119 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
1121 struct ref_formatting_stack
*cur
= *stack
;
1122 struct ref_formatting_stack
*prev
= cur
->prev
;
1123 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1125 if (!if_then_else
->then_atom_seen
)
1126 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
1128 if (if_then_else
->else_atom_seen
) {
1130 * There is an %(else) atom: we need to drop one state from the
1131 * stack, either the %(else) branch if the condition is satisfied, or
1132 * the %(then) branch if it isn't.
1134 if (if_then_else
->condition_satisfied
) {
1135 strbuf_reset(&cur
->output
);
1136 pop_stack_element(&cur
);
1138 strbuf_swap(&cur
->output
, &prev
->output
);
1139 strbuf_reset(&cur
->output
);
1140 pop_stack_element(&cur
);
1142 } else if (!if_then_else
->condition_satisfied
) {
1144 * No %(else) atom: just drop the %(then) branch if the
1145 * condition is not satisfied.
1147 strbuf_reset(&cur
->output
);
1154 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
1155 struct strbuf
*err UNUSED
)
1157 struct ref_formatting_stack
*new_stack
;
1158 struct if_then_else
*if_then_else
= xcalloc(1,
1159 sizeof(struct if_then_else
));
1161 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
1162 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
1164 push_stack_element(&state
->stack
);
1165 new_stack
= state
->stack
;
1166 new_stack
->at_end
= if_then_else_handler
;
1167 new_stack
->at_end_data
= if_then_else
;
1171 static int is_empty(struct strbuf
*buf
)
1173 const char *cur
= buf
->buf
;
1174 const char *end
= buf
->buf
+ buf
->len
;
1176 while (cur
!= end
&& (isspace(*cur
)))
1182 static int then_atom_handler(struct atom_value
*atomv UNUSED
,
1183 struct ref_formatting_state
*state
,
1186 struct ref_formatting_stack
*cur
= state
->stack
;
1187 struct if_then_else
*if_then_else
= NULL
;
1190 if (cur
->at_end
== if_then_else_handler
)
1191 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1193 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
1194 if (if_then_else
->then_atom_seen
)
1195 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
1196 if (if_then_else
->else_atom_seen
)
1197 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
1198 if_then_else
->then_atom_seen
= 1;
1199 if (if_then_else
->str
)
1200 str_len
= strlen(if_then_else
->str
);
1202 * If the 'equals' or 'notequals' attribute is used then
1203 * perform the required comparison. If not, only non-empty
1204 * strings satisfy the 'if' condition.
1206 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
1207 if (str_len
== cur
->output
.len
&&
1208 !memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1209 if_then_else
->condition_satisfied
= 1;
1210 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
1211 if (str_len
!= cur
->output
.len
||
1212 memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1213 if_then_else
->condition_satisfied
= 1;
1214 } else if (cur
->output
.len
&& !is_empty(&cur
->output
))
1215 if_then_else
->condition_satisfied
= 1;
1216 strbuf_reset(&cur
->output
);
1220 static int else_atom_handler(struct atom_value
*atomv UNUSED
,
1221 struct ref_formatting_state
*state
,
1224 struct ref_formatting_stack
*prev
= state
->stack
;
1225 struct if_then_else
*if_then_else
= NULL
;
1227 if (prev
->at_end
== if_then_else_handler
)
1228 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
1230 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
1231 if (!if_then_else
->then_atom_seen
)
1232 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
1233 if (if_then_else
->else_atom_seen
)
1234 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
1235 if_then_else
->else_atom_seen
= 1;
1236 push_stack_element(&state
->stack
);
1237 state
->stack
->at_end_data
= prev
->at_end_data
;
1238 state
->stack
->at_end
= prev
->at_end
;
1242 static int end_atom_handler(struct atom_value
*atomv UNUSED
,
1243 struct ref_formatting_state
*state
,
1246 struct ref_formatting_stack
*current
= state
->stack
;
1247 struct strbuf s
= STRBUF_INIT
;
1249 if (!current
->at_end
)
1250 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
1251 current
->at_end(&state
->stack
);
1253 /* Stack may have been popped within at_end(), hence reset the current pointer */
1254 current
= state
->stack
;
1257 * Perform quote formatting when the stack element is that of
1258 * a supporting atom. If nested then perform quote formatting
1259 * only on the topmost supporting atom.
1261 if (!current
->prev
->prev
) {
1262 quote_formatting(&s
, current
->output
.buf
, current
->output
.len
, state
->quote_style
);
1263 strbuf_swap(¤t
->output
, &s
);
1266 pop_stack_element(&state
->stack
);
1271 * In a format string, find the next occurrence of %(atom).
1273 static const char *find_next(const char *cp
)
1278 * %( is the start of an atom;
1279 * %% is a quoted per-cent.
1283 else if (cp
[1] == '%')
1284 cp
++; /* skip over two % */
1285 /* otherwise this is a singleton, literal % */
1292 static int reject_atom(enum atom_type atom_type
)
1294 return atom_type
== ATOM_REST
;
1298 * Make sure the format string is well formed, and parse out
1301 int verify_ref_format(struct ref_format
*format
)
1303 const char *cp
, *sp
;
1305 format
->need_color_reset_at_eol
= 0;
1306 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
1307 struct strbuf err
= STRBUF_INIT
;
1308 const char *color
, *ep
= strchr(sp
, ')');
1312 return error(_("malformed format string %s"), sp
);
1313 /* sp points at "%(" and ep points at the closing ")" */
1314 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
1317 if (reject_atom(used_atom
[at
].atom_type
))
1318 die(_("this command reject atom %%(%.*s)"), (int)(ep
- sp
- 2), sp
+ 2);
1320 if ((format
->quote_style
== QUOTE_PYTHON
||
1321 format
->quote_style
== QUOTE_SHELL
||
1322 format
->quote_style
== QUOTE_TCL
) &&
1323 used_atom
[at
].atom_type
== ATOM_RAW
&&
1324 used_atom
[at
].u
.raw_data
.option
== RAW_BARE
)
1325 die(_("--format=%.*s cannot be used with "
1326 "--python, --shell, --tcl"), (int)(ep
- sp
- 2), sp
+ 2);
1329 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
1330 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
1331 strbuf_release(&err
);
1333 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
1334 format
->need_color_reset_at_eol
= 0;
1338 static const char *do_grab_oid(const char *field
, const struct object_id
*oid
,
1339 struct used_atom
*atom
)
1341 switch (atom
->u
.oid
.option
) {
1343 return oid_to_hex(oid
);
1345 return repo_find_unique_abbrev(the_repository
, oid
,
1346 atom
->u
.oid
.length
);
1348 return repo_find_unique_abbrev(the_repository
, oid
,
1351 BUG("unknown %%(%s) option", field
);
1355 static int grab_oid(const char *name
, const char *field
, const struct object_id
*oid
,
1356 struct atom_value
*v
, struct used_atom
*atom
)
1358 if (starts_with(name
, field
)) {
1359 v
->s
= xstrdup(do_grab_oid(field
, oid
, atom
));
1365 /* See grab_values */
1366 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
1370 for (i
= 0; i
< used_atom_cnt
; i
++) {
1371 const char *name
= used_atom
[i
].name
;
1372 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1373 struct atom_value
*v
= &val
[i
];
1374 if (!!deref
!= (*name
== '*'))
1378 if (atom_type
== ATOM_OBJECTTYPE
)
1379 v
->s
= xstrdup(type_name(oi
->type
));
1380 else if (atom_type
== ATOM_OBJECTSIZE
) {
1381 if (used_atom
[i
].u
.objectsize
.option
== O_SIZE_DISK
) {
1382 v
->value
= oi
->disk_size
;
1383 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
1384 } else if (used_atom
[i
].u
.objectsize
.option
== O_SIZE
) {
1385 v
->value
= oi
->size
;
1386 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
1388 } else if (atom_type
== ATOM_DELTABASE
)
1389 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
1390 else if (atom_type
== ATOM_OBJECTNAME
&& deref
)
1391 grab_oid(name
, "objectname", &oi
->oid
, v
, &used_atom
[i
]);
1395 /* See grab_values */
1396 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1399 struct tag
*tag
= (struct tag
*) obj
;
1401 for (i
= 0; i
< used_atom_cnt
; i
++) {
1402 const char *name
= used_atom
[i
].name
;
1403 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1404 struct atom_value
*v
= &val
[i
];
1405 if (!!deref
!= (*name
== '*'))
1409 if (atom_type
== ATOM_TAG
)
1410 v
->s
= xstrdup(tag
->tag
);
1411 else if (atom_type
== ATOM_TYPE
&& tag
->tagged
)
1412 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
1413 else if (atom_type
== ATOM_OBJECT
&& tag
->tagged
)
1414 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
1418 /* See grab_values */
1419 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1422 struct commit
*commit
= (struct commit
*) obj
;
1424 for (i
= 0; i
< used_atom_cnt
; i
++) {
1425 const char *name
= used_atom
[i
].name
;
1426 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1427 struct atom_value
*v
= &val
[i
];
1428 if (!!deref
!= (*name
== '*'))
1432 if (atom_type
== ATOM_TREE
&&
1433 grab_oid(name
, "tree", get_commit_tree_oid(commit
), v
, &used_atom
[i
]))
1435 if (atom_type
== ATOM_NUMPARENT
) {
1436 v
->value
= commit_list_count(commit
->parents
);
1437 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1439 else if (atom_type
== ATOM_PARENT
) {
1440 struct commit_list
*parents
;
1441 struct strbuf s
= STRBUF_INIT
;
1442 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1443 struct object_id
*oid
= &parents
->item
->object
.oid
;
1444 if (parents
!= commit
->parents
)
1445 strbuf_addch(&s
, ' ');
1446 strbuf_addstr(&s
, do_grab_oid("parent", oid
, &used_atom
[i
]));
1448 v
->s
= strbuf_detach(&s
, NULL
);
1453 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1457 if (!strncmp(buf
, who
, wholen
) &&
1459 return buf
+ wholen
+ 1;
1460 eol
= strchr(buf
, '\n');
1465 return ""; /* end of header */
1471 static const char *copy_line(const char *buf
)
1473 const char *eol
= strchrnul(buf
, '\n');
1474 return xmemdupz(buf
, eol
- buf
);
1477 static const char *copy_name(const char *buf
)
1480 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1481 if (starts_with(cp
, " <"))
1482 return xmemdupz(buf
, cp
- buf
);
1487 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1489 const char *email
= strchr(buf
, '<');
1490 const char *eoemail
;
1493 switch (atom
->u
.email_option
.option
) {
1495 eoemail
= strchr(email
, '>');
1501 eoemail
= strchr(email
, '>');
1505 eoemail
= strchr(email
, '@');
1507 eoemail
= strchr(email
, '>');
1510 BUG("unknown email option");
1515 return xmemdupz(email
, eoemail
- email
);
1518 static char *copy_subject(const char *buf
, unsigned long len
)
1520 struct strbuf sb
= STRBUF_INIT
;
1523 for (i
= 0; i
< len
; i
++) {
1524 if (buf
[i
] == '\r' && i
+ 1 < len
&& buf
[i
+ 1] == '\n')
1525 continue; /* ignore CR in CRLF */
1528 strbuf_addch(&sb
, ' ');
1530 strbuf_addch(&sb
, buf
[i
]);
1532 return strbuf_detach(&sb
, NULL
);
1535 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1537 const char *eoemail
= strstr(buf
, "> ");
1539 timestamp_t timestamp
;
1541 struct date_mode date_mode
= DATE_MODE_INIT
;
1542 const char *formatp
;
1545 * We got here because atomname ends in "date" or "date<something>";
1546 * it's not possible that <something> is not ":<format>" because
1547 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1548 * ":" means no format is specified, and use the default.
1550 formatp
= strchr(atomname
, ':');
1553 parse_date_format(formatp
, &date_mode
);
1558 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1559 if (timestamp
== TIME_MAX
)
1561 tz
= strtol(zone
, NULL
, 10);
1562 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1564 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1565 v
->value
= timestamp
;
1566 date_mode_release(&date_mode
);
1573 /* See grab_values */
1574 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1577 int wholen
= strlen(who
);
1578 const char *wholine
= NULL
;
1580 for (i
= 0; i
< used_atom_cnt
; i
++) {
1581 const char *name
= used_atom
[i
].name
;
1582 struct atom_value
*v
= &val
[i
];
1583 if (!!deref
!= (*name
== '*'))
1587 if (strncmp(who
, name
, wholen
))
1589 if (name
[wholen
] != 0 &&
1590 strcmp(name
+ wholen
, "name") &&
1591 !starts_with(name
+ wholen
, "email") &&
1592 !starts_with(name
+ wholen
, "date"))
1595 wholine
= find_wholine(who
, wholen
, buf
);
1597 return; /* no point looking for it */
1598 if (name
[wholen
] == 0)
1599 v
->s
= copy_line(wholine
);
1600 else if (!strcmp(name
+ wholen
, "name"))
1601 v
->s
= copy_name(wholine
);
1602 else if (starts_with(name
+ wholen
, "email"))
1603 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1604 else if (starts_with(name
+ wholen
, "date"))
1605 grab_date(wholine
, v
, name
);
1609 * For a tag or a commit object, if "creator" or "creatordate" is
1610 * requested, do something special.
1612 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1613 return; /* "author" for commit object is not wanted */
1615 wholine
= find_wholine(who
, wholen
, buf
);
1618 for (i
= 0; i
< used_atom_cnt
; i
++) {
1619 const char *name
= used_atom
[i
].name
;
1620 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1621 struct atom_value
*v
= &val
[i
];
1622 if (!!deref
!= (*name
== '*'))
1627 if (atom_type
== ATOM_CREATORDATE
)
1628 grab_date(wholine
, v
, name
);
1629 else if (atom_type
== ATOM_CREATOR
)
1630 v
->s
= copy_line(wholine
);
1634 static void grab_signature(struct atom_value
*val
, int deref
, struct object
*obj
)
1637 struct commit
*commit
= (struct commit
*) obj
;
1638 struct signature_check sigc
= { 0 };
1639 int signature_checked
= 0;
1641 for (i
= 0; i
< used_atom_cnt
; i
++) {
1642 struct used_atom
*atom
= &used_atom
[i
];
1643 const char *name
= atom
->name
;
1644 struct atom_value
*v
= &val
[i
];
1647 if (!!deref
!= (*name
== '*'))
1652 if (!skip_prefix(name
, "signature", &name
) ||
1653 (*name
&& *name
!= ':'))
1660 opt
= parse_signature_option(name
);
1664 if (!signature_checked
) {
1665 check_commit_signature(commit
, &sigc
);
1666 signature_checked
= 1;
1671 v
->s
= xstrdup(sigc
.output
? sigc
.output
: "");
1674 v
->s
= xstrdup(sigc
.signer
? sigc
.signer
: "");
1677 switch (sigc
.result
) {
1679 switch (sigc
.trust_level
) {
1680 case TRUST_UNDEFINED
:
1682 v
->s
= xstrfmt("%c", (char)'U');
1685 v
->s
= xstrfmt("%c", (char)'G');
1695 v
->s
= xstrfmt("%c", (char)sigc
.result
);
1700 v
->s
= xstrdup(sigc
.key
? sigc
.key
: "");
1703 v
->s
= xstrdup(sigc
.fingerprint
?
1704 sigc
.fingerprint
: "");
1707 v
->s
= xstrdup(sigc
.primary_key_fingerprint
?
1708 sigc
.primary_key_fingerprint
: "");
1711 v
->s
= xstrdup(gpg_trust_level_to_str(sigc
.trust_level
));
1716 if (signature_checked
)
1717 signature_check_clear(&sigc
);
1720 static void find_subpos(const char *buf
,
1721 const char **sub
, size_t *sublen
,
1722 const char **body
, size_t *bodylen
,
1724 const char **sig
, size_t *siglen
)
1726 struct strbuf payload
= STRBUF_INIT
;
1727 struct strbuf signature
= STRBUF_INIT
;
1729 const char *end
= buf
+ strlen(buf
);
1730 const char *sigstart
;
1732 /* parse signature first; we might not even have a subject line */
1733 parse_signature(buf
, end
- buf
, &payload
, &signature
);
1734 strbuf_release(&payload
);
1736 /* skip past header until we hit empty line */
1737 while (*buf
&& *buf
!= '\n') {
1738 eol
= strchrnul(buf
, '\n');
1743 /* skip any empty lines */
1744 while (*buf
== '\n')
1746 *sig
= strbuf_detach(&signature
, siglen
);
1747 sigstart
= buf
+ parse_signed_buffer(buf
, strlen(buf
));
1749 /* subject is first non-empty line */
1751 /* subject goes to first empty line before signature begins */
1752 if ((eol
= strstr(*sub
, "\n\n")) ||
1753 (eol
= strstr(*sub
, "\r\n\r\n"))) {
1754 eol
= eol
< sigstart
? eol
: sigstart
;
1756 /* treat whole message as subject */
1760 *sublen
= buf
- *sub
;
1761 /* drop trailing newline, if present */
1762 while (*sublen
&& ((*sub
)[*sublen
- 1] == '\n' ||
1763 (*sub
)[*sublen
- 1] == '\r'))
1766 /* skip any empty lines */
1767 while (*buf
== '\n' || *buf
== '\r')
1770 *bodylen
= strlen(buf
);
1771 *nonsiglen
= sigstart
- buf
;
1775 * If 'lines' is greater than 0, append that many lines from the given
1776 * 'buf' of length 'size' to the given strbuf.
1778 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1781 const char *sp
, *eol
;
1786 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1788 strbuf_addstr(out
, "\n ");
1789 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1790 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1791 strbuf_add(out
, sp
, len
);
1798 static void grab_describe_values(struct atom_value
*val
, int deref
,
1801 struct commit
*commit
= (struct commit
*)obj
;
1804 for (i
= 0; i
< used_atom_cnt
; i
++) {
1805 struct used_atom
*atom
= &used_atom
[i
];
1806 enum atom_type type
= atom
->atom_type
;
1807 const char *name
= atom
->name
;
1808 struct atom_value
*v
= &val
[i
];
1810 struct child_process cmd
= CHILD_PROCESS_INIT
;
1811 struct strbuf out
= STRBUF_INIT
;
1812 struct strbuf err
= STRBUF_INIT
;
1814 if (type
!= ATOM_DESCRIBE
)
1817 if (!!deref
!= (*name
== '*'))
1821 strvec_push(&cmd
.args
, "describe");
1822 strvec_pushv(&cmd
.args
, atom
->u
.describe_args
);
1823 strvec_push(&cmd
.args
, oid_to_hex(&commit
->object
.oid
));
1824 if (pipe_command(&cmd
, NULL
, 0, &out
, 0, &err
, 0) < 0) {
1825 error(_("failed to run 'describe'"));
1830 v
->s
= strbuf_detach(&out
, NULL
);
1832 strbuf_release(&err
);
1836 /* See grab_values */
1837 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct expand_data
*data
)
1840 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1841 size_t sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1842 void *buf
= data
->content
;
1844 for (i
= 0; i
< used_atom_cnt
; i
++) {
1845 struct used_atom
*atom
= &used_atom
[i
];
1846 const char *name
= atom
->name
;
1847 struct atom_value
*v
= &val
[i
];
1848 enum atom_type atom_type
= atom
->atom_type
;
1850 if (!!deref
!= (*name
== '*'))
1855 if (atom_type
== ATOM_RAW
) {
1856 unsigned long buf_size
= data
->size
;
1858 if (atom
->u
.raw_data
.option
== RAW_BARE
) {
1859 v
->s
= xmemdupz(buf
, buf_size
);
1860 v
->s_size
= buf_size
;
1861 } else if (atom
->u
.raw_data
.option
== RAW_LENGTH
) {
1862 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)buf_size
);
1867 if ((data
->type
!= OBJ_TAG
&&
1868 data
->type
!= OBJ_COMMIT
) ||
1869 (strcmp(name
, "body") &&
1870 !starts_with(name
, "subject") &&
1871 !starts_with(name
, "trailers") &&
1872 !starts_with(name
, "contents")))
1877 &bodypos
, &bodylen
, &nonsiglen
,
1880 if (atom
->u
.contents
.option
== C_SUB
)
1881 v
->s
= copy_subject(subpos
, sublen
);
1882 else if (atom
->u
.contents
.option
== C_SUB_SANITIZE
) {
1883 struct strbuf sb
= STRBUF_INIT
;
1884 format_sanitized_subject(&sb
, subpos
, sublen
);
1885 v
->s
= strbuf_detach(&sb
, NULL
);
1886 } else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1887 v
->s
= xmemdupz(bodypos
, bodylen
);
1888 else if (atom
->u
.contents
.option
== C_LENGTH
)
1889 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)strlen(subpos
));
1890 else if (atom
->u
.contents
.option
== C_BODY
)
1891 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1892 else if (atom
->u
.contents
.option
== C_SIG
)
1893 v
->s
= xmemdupz(sigpos
, siglen
);
1894 else if (atom
->u
.contents
.option
== C_LINES
) {
1895 struct strbuf s
= STRBUF_INIT
;
1896 const char *contents_end
= bodypos
+ nonsiglen
;
1898 /* Size is the length of the message after removing the signature */
1899 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1900 v
->s
= strbuf_detach(&s
, NULL
);
1901 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1902 struct strbuf s
= STRBUF_INIT
;
1904 /* Format the trailer info according to the trailer_opts given */
1905 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1907 v
->s
= strbuf_detach(&s
, NULL
);
1908 } else if (atom
->u
.contents
.option
== C_BARE
)
1909 v
->s
= xstrdup(subpos
);
1912 free((void *)sigpos
);
1916 * We want to have empty print-string for field requests
1917 * that do not apply (e.g. "authordate" for a tag object)
1919 static void fill_missing_values(struct atom_value
*val
)
1922 for (i
= 0; i
< used_atom_cnt
; i
++) {
1923 struct atom_value
*v
= &val
[i
];
1930 * val is a list of atom_value to hold returned values. Extract
1931 * the values for atoms in used_atom array out of (obj, buf, sz).
1932 * when deref is false, (obj, buf, sz) is the object that is
1933 * pointed at by the ref itself; otherwise it is the object the
1934 * ref (which is a tag) refers to.
1936 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, struct expand_data
*data
)
1938 void *buf
= data
->content
;
1940 switch (obj
->type
) {
1942 grab_tag_values(val
, deref
, obj
);
1943 grab_sub_body_contents(val
, deref
, data
);
1944 grab_person("tagger", val
, deref
, buf
);
1945 grab_describe_values(val
, deref
, obj
);
1948 grab_commit_values(val
, deref
, obj
);
1949 grab_sub_body_contents(val
, deref
, data
);
1950 grab_person("author", val
, deref
, buf
);
1951 grab_person("committer", val
, deref
, buf
);
1952 grab_signature(val
, deref
, obj
);
1953 grab_describe_values(val
, deref
, obj
);
1956 /* grab_tree_values(val, deref, obj, buf, sz); */
1957 grab_sub_body_contents(val
, deref
, data
);
1960 /* grab_blob_values(val, deref, obj, buf, sz); */
1961 grab_sub_body_contents(val
, deref
, data
);
1964 die("Eh? Object of type %d?", obj
->type
);
1968 static inline char *copy_advance(char *dst
, const char *src
)
1975 static const char *lstrip_ref_components(const char *refname
, int len
)
1977 long remaining
= len
;
1978 const char *start
= xstrdup(refname
);
1979 const char *to_free
= start
;
1983 const char *p
= refname
;
1985 /* Find total no of '/' separated path-components */
1986 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1989 * The number of components we need to strip is now
1990 * the total minus the components to be left (Plus one
1991 * because we count the number of '/', but the number
1992 * of components is one more than the no of '/').
1994 remaining
= i
+ len
+ 1;
1997 while (remaining
> 0) {
2000 free((char *)to_free
);
2008 start
= xstrdup(start
);
2009 free((char *)to_free
);
2013 static const char *rstrip_ref_components(const char *refname
, int len
)
2015 long remaining
= len
;
2016 const char *start
= xstrdup(refname
);
2017 const char *to_free
= start
;
2021 const char *p
= refname
;
2023 /* Find total no of '/' separated path-components */
2024 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
2027 * The number of components we need to strip is now
2028 * the total minus the components to be left (Plus one
2029 * because we count the number of '/', but the number
2030 * of components is one more than the no of '/').
2032 remaining
= i
+ len
+ 1;
2035 while (remaining
-- > 0) {
2036 char *p
= strrchr(start
, '/');
2038 free((char *)to_free
);
2046 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
2048 if (atom
->option
== R_SHORT
)
2049 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
2050 else if (atom
->option
== R_LSTRIP
)
2051 return lstrip_ref_components(refname
, atom
->lstrip
);
2052 else if (atom
->option
== R_RSTRIP
)
2053 return rstrip_ref_components(refname
, atom
->rstrip
);
2055 return xstrdup(refname
);
2058 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
2059 struct branch
*branch
, const char **s
)
2061 int num_ours
, num_theirs
;
2062 if (atom
->u
.remote_ref
.option
== RR_REF
)
2063 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
2064 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
2065 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
2066 NULL
, atom
->u
.remote_ref
.push
,
2067 AHEAD_BEHIND_FULL
) < 0) {
2068 *s
= xstrdup(msgs
.gone
);
2069 } else if (!num_ours
&& !num_theirs
)
2072 *s
= xstrfmt(msgs
.behind
, num_theirs
);
2073 else if (!num_theirs
)
2074 *s
= xstrfmt(msgs
.ahead
, num_ours
);
2076 *s
= xstrfmt(msgs
.ahead_behind
,
2077 num_ours
, num_theirs
);
2078 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
2079 const char *to_free
= *s
;
2080 *s
= xstrfmt("[%s]", *s
);
2081 free((void *)to_free
);
2083 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
2084 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
2085 NULL
, atom
->u
.remote_ref
.push
,
2086 AHEAD_BEHIND_FULL
) < 0) {
2090 if (!num_ours
&& !num_theirs
)
2094 else if (!num_theirs
)
2098 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
2100 const char *remote
= atom
->u
.remote_ref
.push
?
2101 pushremote_for_branch(branch
, &explicit) :
2102 remote_for_branch(branch
, &explicit);
2103 *s
= xstrdup(explicit ? remote
: "");
2104 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
2107 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
2108 *s
= xstrdup(merge
? merge
: "");
2110 BUG("unhandled RR_* enum");
2113 char *get_head_description(void)
2115 struct strbuf desc
= STRBUF_INIT
;
2116 struct wt_status_state state
;
2117 memset(&state
, 0, sizeof(state
));
2118 wt_status_get_state(the_repository
, &state
, 1);
2119 if (state
.rebase_in_progress
||
2120 state
.rebase_interactive_in_progress
) {
2122 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
2125 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
2126 state
.detached_from
);
2127 } else if (state
.bisect_in_progress
)
2128 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
2130 else if (state
.detached_from
) {
2131 if (state
.detached_at
)
2132 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
2133 state
.detached_from
);
2135 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
2136 state
.detached_from
);
2138 strbuf_addstr(&desc
, _("(no branch)"));
2140 wt_status_state_free_buffers(&state
);
2142 return strbuf_detach(&desc
, NULL
);
2145 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
2150 return show_ref(&atom
->u
.refname
, ref
->symref
);
2153 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
2155 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
2156 return get_head_description();
2157 return show_ref(&atom
->u
.refname
, ref
->refname
);
2160 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
2161 struct expand_data
*oi
, struct strbuf
*err
)
2163 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
2165 if (oi
->info
.contentp
) {
2166 /* We need to know that to use parse_object_buffer properly */
2167 oi
->info
.sizep
= &oi
->size
;
2168 oi
->info
.typep
= &oi
->type
;
2170 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
2171 OBJECT_INFO_LOOKUP_REPLACE
))
2172 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2173 oid_to_hex(&oi
->oid
), ref
->refname
);
2174 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
2175 BUG("Object size is less than zero.");
2177 if (oi
->info
.contentp
) {
2178 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
2182 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
2183 oid_to_hex(&oi
->oid
), ref
->refname
);
2185 grab_values(ref
->value
, deref
, *obj
, oi
);
2188 grab_common_values(ref
->value
, deref
, oi
);
2194 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
2198 for (i
= 0; worktrees
[i
]; i
++) {
2199 if (worktrees
[i
]->head_ref
) {
2200 struct ref_to_worktree_entry
*entry
;
2201 entry
= xmalloc(sizeof(*entry
));
2202 entry
->wt
= worktrees
[i
];
2203 hashmap_entry_init(&entry
->ent
,
2204 strhash(worktrees
[i
]->head_ref
));
2206 hashmap_add(map
, &entry
->ent
);
2211 static void lazy_init_worktree_map(void)
2213 if (ref_to_worktree_map
.worktrees
)
2216 ref_to_worktree_map
.worktrees
= get_worktrees();
2217 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
2218 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
2221 static char *get_worktree_path(const struct ref_array_item
*ref
)
2223 struct hashmap_entry entry
, *e
;
2224 struct ref_to_worktree_entry
*lookup_result
;
2226 lazy_init_worktree_map();
2228 hashmap_entry_init(&entry
, strhash(ref
->refname
));
2229 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
2234 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
2236 return xstrdup(lookup_result
->wt
->path
);
2240 * Parse the object referred by ref, and grab needed value.
2242 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
2246 struct object_info empty
= OBJECT_INFO_INIT
;
2247 int ahead_behind_atoms
= 0;
2249 CALLOC_ARRAY(ref
->value
, used_atom_cnt
);
2251 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
2252 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
2255 ref
->symref
= xstrdup("");
2258 /* Fill in specials first */
2259 for (i
= 0; i
< used_atom_cnt
; i
++) {
2260 struct used_atom
*atom
= &used_atom
[i
];
2261 enum atom_type atom_type
= atom
->atom_type
;
2262 const char *name
= used_atom
[i
].name
;
2263 struct atom_value
*v
= &ref
->value
[i
];
2265 const char *refname
;
2266 struct branch
*branch
= NULL
;
2268 v
->s_size
= ATOM_SIZE_UNSPECIFIED
;
2269 v
->handler
= append_atom
;
2277 if (atom_type
== ATOM_REFNAME
)
2278 refname
= get_refname(atom
, ref
);
2279 else if (atom_type
== ATOM_WORKTREEPATH
) {
2280 if (ref
->kind
== FILTER_REFS_BRANCHES
)
2281 v
->s
= get_worktree_path(ref
);
2286 else if (atom_type
== ATOM_SYMREF
)
2287 refname
= get_symref(atom
, ref
);
2288 else if (atom_type
== ATOM_UPSTREAM
) {
2289 const char *branch_name
;
2290 /* only local branches may have an upstream */
2291 if (!skip_prefix(ref
->refname
, "refs/heads/",
2296 branch
= branch_get(branch_name
);
2298 refname
= branch_get_upstream(branch
, NULL
);
2300 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2304 } else if (atom_type
== ATOM_PUSH
&& atom
->u
.remote_ref
.push
) {
2305 const char *branch_name
;
2307 if (!skip_prefix(ref
->refname
, "refs/heads/",
2310 branch
= branch_get(branch_name
);
2312 if (atom
->u
.remote_ref
.push_remote
)
2315 refname
= branch_get_push(branch
, NULL
);
2319 /* We will definitely re-init v->s on the next line. */
2321 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2323 } else if (atom_type
== ATOM_COLOR
) {
2324 v
->s
= xstrdup(atom
->u
.color
);
2326 } else if (atom_type
== ATOM_FLAG
) {
2327 char buf
[256], *cp
= buf
;
2328 if (ref
->flag
& REF_ISSYMREF
)
2329 cp
= copy_advance(cp
, ",symref");
2330 if (ref
->flag
& REF_ISPACKED
)
2331 cp
= copy_advance(cp
, ",packed");
2336 v
->s
= xstrdup(buf
+ 1);
2339 } else if (!deref
&& atom_type
== ATOM_OBJECTNAME
&&
2340 grab_oid(name
, "objectname", &ref
->objectname
, v
, atom
)) {
2342 } else if (atom_type
== ATOM_HEAD
) {
2343 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
2344 v
->s
= xstrdup("*");
2346 v
->s
= xstrdup(" ");
2348 } else if (atom_type
== ATOM_ALIGN
) {
2349 v
->handler
= align_atom_handler
;
2352 } else if (atom_type
== ATOM_END
) {
2353 v
->handler
= end_atom_handler
;
2356 } else if (atom_type
== ATOM_IF
) {
2358 if (skip_prefix(name
, "if:", &s
))
2362 v
->handler
= if_atom_handler
;
2364 } else if (atom_type
== ATOM_THEN
) {
2365 v
->handler
= then_atom_handler
;
2368 } else if (atom_type
== ATOM_ELSE
) {
2369 v
->handler
= else_atom_handler
;
2372 } else if (atom_type
== ATOM_REST
) {
2374 v
->s
= xstrdup(ref
->rest
);
2378 } else if (atom_type
== ATOM_AHEADBEHIND
) {
2380 const struct ahead_behind_count
*count
;
2381 count
= ref
->counts
[ahead_behind_atoms
++];
2382 v
->s
= xstrfmt("%d %d", count
->ahead
, count
->behind
);
2392 v
->s
= xstrdup(refname
);
2394 v
->s
= xstrfmt("%s^{}", refname
);
2395 free((char *)refname
);
2398 for (i
= 0; i
< used_atom_cnt
; i
++) {
2399 struct atom_value
*v
= &ref
->value
[i
];
2400 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
2401 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2402 oid_to_hex(&ref
->objectname
), ref
->refname
);
2406 oi
.info
.contentp
= &oi
.content
;
2407 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
2408 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
2412 oi
.oid
= ref
->objectname
;
2413 if (get_object(ref
, 0, &obj
, &oi
, err
))
2417 * If there is no atom that wants to know about tagged
2418 * object, we are done.
2420 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
2424 * If it is a tag object, see if we use a value that derefs
2425 * the object, and if we do grab the object it refers to.
2427 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
2430 * NEEDSWORK: This derefs tag only once, which
2431 * is good to deal with chains of trust, but
2432 * is not consistent with what deref_tag() does
2433 * which peels the onion to the core.
2435 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
2439 * Given a ref, return the value for the atom. This lazily gets value
2440 * out of the object by calling populate value.
2442 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
2443 struct atom_value
**v
, struct strbuf
*err
)
2446 if (populate_value(ref
, err
))
2448 fill_missing_values(ref
->value
);
2450 *v
= &ref
->value
[atom
];
2455 * Return 1 if the refname matches one of the patterns, otherwise 0.
2456 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2457 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2458 * matches "refs/heads/mas*", too).
2460 static int match_pattern(const char **patterns
, const char *refname
,
2466 flags
|= WM_CASEFOLD
;
2469 * When no '--format' option is given we need to skip the prefix
2470 * for matching refs of tags and branches.
2472 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
2473 skip_prefix(refname
, "refs/heads/", &refname
) ||
2474 skip_prefix(refname
, "refs/remotes/", &refname
) ||
2475 skip_prefix(refname
, "refs/", &refname
));
2477 for (; *patterns
; patterns
++) {
2478 if (!wildmatch(*patterns
, refname
, flags
))
2485 * Return 1 if the refname matches one of the patterns, otherwise 0.
2486 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
2487 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2488 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2490 static int match_name_as_path(const char **pattern
, const char *refname
,
2493 int namelen
= strlen(refname
);
2494 unsigned flags
= WM_PATHNAME
;
2497 flags
|= WM_CASEFOLD
;
2499 for (; *pattern
; pattern
++) {
2500 const char *p
= *pattern
;
2501 int plen
= strlen(p
);
2503 if ((plen
<= namelen
) &&
2504 !strncmp(refname
, p
, plen
) &&
2505 (refname
[plen
] == '\0' ||
2506 refname
[plen
] == '/' ||
2509 if (!wildmatch(p
, refname
, flags
))
2515 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
2516 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
2518 if (!*filter
->name_patterns
)
2519 return 1; /* No pattern always matches */
2520 if (filter
->match_as_path
)
2521 return match_name_as_path(filter
->name_patterns
, refname
,
2522 filter
->ignore_case
);
2523 return match_pattern(filter
->name_patterns
, refname
,
2524 filter
->ignore_case
);
2527 static int filter_exclude_match(struct ref_filter
*filter
, const char *refname
)
2529 if (!filter
->exclude
.nr
)
2531 if (filter
->match_as_path
)
2532 return match_name_as_path(filter
->exclude
.v
, refname
,
2533 filter
->ignore_case
);
2534 return match_pattern(filter
->exclude
.v
, refname
, filter
->ignore_case
);
2538 * This is the same as for_each_fullref_in(), but it tries to iterate
2539 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2540 * pattern match, so the callback still has to match each ref individually.
2542 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
2546 if (!filter
->match_as_path
) {
2548 * in this case, the patterns are applied after
2549 * prefixes like "refs/heads/" etc. are stripped off,
2550 * so we have to look at everything:
2552 return for_each_fullref_in("", cb
, cb_data
);
2555 if (filter
->ignore_case
) {
2557 * we can't handle case-insensitive comparisons,
2558 * so just return everything and let the caller
2561 return for_each_fullref_in("", cb
, cb_data
);
2564 if (!filter
->name_patterns
[0]) {
2565 /* no patterns; we have to look at everything */
2566 return refs_for_each_fullref_in(get_main_ref_store(the_repository
),
2567 "", filter
->exclude
.v
, cb
, cb_data
);
2570 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository
),
2571 NULL
, filter
->name_patterns
,
2577 * Given a ref (oid, refname), check if the ref belongs to the array
2578 * of oids. If the given ref is a tag, check if the given tag points
2579 * at one of the oids in the given oid array. Returns non-zero if a
2583 * As the refs are cached we might know what refname peels to without
2584 * the need to parse the object via parse_object(). peel_ref() might be a
2585 * more efficient alternative to obtain the pointee.
2587 static int match_points_at(struct oid_array
*points_at
,
2588 const struct object_id
*oid
,
2589 const char *refname
)
2593 if (oid_array_lookup(points_at
, oid
) >= 0)
2595 obj
= parse_object_with_flags(the_repository
, oid
,
2596 PARSE_OBJECT_SKIP_HASH_CHECK
);
2597 while (obj
&& obj
->type
== OBJ_TAG
) {
2598 struct tag
*tag
= (struct tag
*)obj
;
2600 if (parse_tag(tag
) < 0) {
2605 if (oid_array_lookup(points_at
, get_tagged_oid(tag
)) >= 0)
2611 die(_("malformed object at '%s'"), refname
);
2616 * Allocate space for a new ref_array_item and copy the name and oid to it.
2618 * Callers can then fill in other struct members at their leisure.
2620 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2621 const struct object_id
*oid
)
2623 struct ref_array_item
*ref
;
2625 FLEX_ALLOC_STR(ref
, refname
, refname
);
2626 oidcpy(&ref
->objectname
, oid
);
2632 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2633 const char *refname
,
2634 const struct object_id
*oid
)
2636 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2638 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2639 array
->items
[array
->nr
++] = ref
;
2644 static int ref_kind_from_refname(const char *refname
)
2652 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2653 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2654 { "refs/tags/", FILTER_REFS_TAGS
}
2657 if (!strcmp(refname
, "HEAD"))
2658 return FILTER_REFS_DETACHED_HEAD
;
2660 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2661 if (starts_with(refname
, ref_kind
[i
].prefix
))
2662 return ref_kind
[i
].kind
;
2665 return FILTER_REFS_OTHERS
;
2668 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2670 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2671 filter
->kind
== FILTER_REFS_REMOTES
||
2672 filter
->kind
== FILTER_REFS_TAGS
)
2673 return filter
->kind
;
2674 return ref_kind_from_refname(refname
);
2677 struct ref_filter_cbdata
{
2678 struct ref_array
*array
;
2679 struct ref_filter
*filter
;
2680 struct contains_cache contains_cache
;
2681 struct contains_cache no_contains_cache
;
2685 * A call-back given to for_each_ref(). Filter refs and keep them for
2686 * later object processing.
2688 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2690 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2691 struct ref_filter
*filter
= ref_cbdata
->filter
;
2692 struct ref_array_item
*ref
;
2693 struct commit
*commit
= NULL
;
2696 if (flag
& REF_BAD_NAME
) {
2697 warning(_("ignoring ref with broken name %s"), refname
);
2701 if (flag
& REF_ISBROKEN
) {
2702 warning(_("ignoring broken ref %s"), refname
);
2706 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2707 kind
= filter_ref_kind(filter
, refname
);
2708 if (!(kind
& filter
->kind
))
2711 if (!filter_pattern_match(filter
, refname
))
2714 if (filter_exclude_match(filter
, refname
))
2717 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2721 * A merge filter is applied on refs pointing to commits. Hence
2722 * obtain the commit using the 'oid' available and discard all
2723 * non-commits early. The actual filtering is done later.
2725 if (filter
->reachable_from
|| filter
->unreachable_from
||
2726 filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2727 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
2730 /* We perform the filtering for the '--contains' option... */
2731 if (filter
->with_commit
&&
2732 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2734 /* ...or for the `--no-contains' option */
2735 if (filter
->no_commit
&&
2736 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2741 * We do not open the object yet; sort may only need refname
2742 * to do its job and the resulting list may yet to be pruned
2743 * by maxcount logic.
2745 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2746 ref
->commit
= commit
;
2753 /* Free memory allocated for a ref_array_item */
2754 static void free_array_item(struct ref_array_item
*item
)
2756 free((char *)item
->symref
);
2759 for (i
= 0; i
< used_atom_cnt
; i
++)
2760 free((char *)item
->value
[i
].s
);
2767 /* Free all memory allocated for ref_array */
2768 void ref_array_clear(struct ref_array
*array
)
2772 for (i
= 0; i
< array
->nr
; i
++)
2773 free_array_item(array
->items
[i
]);
2774 FREE_AND_NULL(array
->items
);
2775 array
->nr
= array
->alloc
= 0;
2777 for (i
= 0; i
< used_atom_cnt
; i
++) {
2778 struct used_atom
*atom
= &used_atom
[i
];
2779 if (atom
->atom_type
== ATOM_HEAD
)
2781 free((char *)atom
->name
);
2783 FREE_AND_NULL(used_atom
);
2786 if (ref_to_worktree_map
.worktrees
) {
2787 hashmap_clear_and_free(&(ref_to_worktree_map
.map
),
2788 struct ref_to_worktree_entry
, ent
);
2789 free_worktrees(ref_to_worktree_map
.worktrees
);
2790 ref_to_worktree_map
.worktrees
= NULL
;
2793 FREE_AND_NULL(array
->counts
);
2796 #define EXCLUDE_REACHED 0
2797 #define INCLUDE_REACHED 1
2798 static void reach_filter(struct ref_array
*array
,
2799 struct commit_list
**check_reachable
,
2800 int include_reached
)
2803 struct commit
**to_clear
;
2805 if (!*check_reachable
)
2808 CALLOC_ARRAY(to_clear
, array
->nr
);
2809 for (i
= 0; i
< array
->nr
; i
++) {
2810 struct ref_array_item
*item
= array
->items
[i
];
2811 to_clear
[i
] = item
->commit
;
2814 tips_reachable_from_bases(the_repository
,
2816 to_clear
, array
->nr
,
2822 for (i
= 0; i
< old_nr
; i
++) {
2823 struct ref_array_item
*item
= array
->items
[i
];
2824 struct commit
*commit
= item
->commit
;
2826 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2828 if (is_merged
== include_reached
)
2829 array
->items
[array
->nr
++] = array
->items
[i
];
2831 free_array_item(item
);
2834 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2836 while (*check_reachable
) {
2837 struct commit
*merge_commit
= pop_commit(check_reachable
);
2838 clear_commit_marks(merge_commit
, ALL_REV_FLAGS
);
2844 void filter_ahead_behind(struct repository
*r
,
2845 struct ref_format
*format
,
2846 struct ref_array
*array
)
2848 struct commit
**commits
;
2849 size_t commits_nr
= format
->bases
.nr
+ array
->nr
;
2851 if (!format
->bases
.nr
|| !array
->nr
)
2854 ALLOC_ARRAY(commits
, commits_nr
);
2855 for (size_t i
= 0; i
< format
->bases
.nr
; i
++)
2856 commits
[i
] = format
->bases
.items
[i
].util
;
2858 ALLOC_ARRAY(array
->counts
, st_mult(format
->bases
.nr
, array
->nr
));
2860 commits_nr
= format
->bases
.nr
;
2861 array
->counts_nr
= 0;
2862 for (size_t i
= 0; i
< array
->nr
; i
++) {
2863 const char *name
= array
->items
[i
]->refname
;
2864 commits
[commits_nr
] = lookup_commit_reference_by_name(name
);
2866 if (!commits
[commits_nr
])
2869 CALLOC_ARRAY(array
->items
[i
]->counts
, format
->bases
.nr
);
2870 for (size_t j
= 0; j
< format
->bases
.nr
; j
++) {
2871 struct ahead_behind_count
*count
;
2872 count
= &array
->counts
[array
->counts_nr
++];
2873 count
->tip_index
= commits_nr
;
2874 count
->base_index
= j
;
2876 array
->items
[i
]->counts
[j
] = count
;
2881 ahead_behind(r
, commits
, commits_nr
, array
->counts
, array
->counts_nr
);
2886 * API for filtering a set of refs. Based on the type of refs the user
2887 * has requested, we iterate through those refs and apply filters
2888 * as per the given ref_filter structure and finally store the
2889 * filtered refs in the ref_array structure.
2891 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2893 struct ref_filter_cbdata ref_cbdata
;
2894 int save_commit_buffer_orig
;
2897 ref_cbdata
.array
= array
;
2898 ref_cbdata
.filter
= filter
;
2900 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2902 save_commit_buffer_orig
= save_commit_buffer
;
2903 save_commit_buffer
= 0;
2905 init_contains_cache(&ref_cbdata
.contains_cache
);
2906 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2908 /* Simple per-ref filtering */
2910 die("filter_refs: invalid type");
2913 * For common cases where we need only branches or remotes or tags,
2914 * we only iterate through those refs. If a mix of refs is needed,
2915 * we iterate over all refs and filter out required refs with the help
2916 * of filter_ref_kind().
2918 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2919 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
);
2920 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2921 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
);
2922 else if (filter
->kind
== FILTER_REFS_TAGS
)
2923 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
);
2924 else if (filter
->kind
& FILTER_REFS_ALL
)
2925 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
);
2926 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2927 head_ref(ref_filter_handler
, &ref_cbdata
);
2930 clear_contains_cache(&ref_cbdata
.contains_cache
);
2931 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2933 /* Filters that need revision walking */
2934 reach_filter(array
, &filter
->reachable_from
, INCLUDE_REACHED
);
2935 reach_filter(array
, &filter
->unreachable_from
, EXCLUDE_REACHED
);
2937 save_commit_buffer
= save_commit_buffer_orig
;
2941 static int compare_detached_head(struct ref_array_item
*a
, struct ref_array_item
*b
)
2943 if (!(a
->kind
^ b
->kind
))
2944 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
2945 if (a
->kind
& FILTER_REFS_DETACHED_HEAD
)
2947 else if (b
->kind
& FILTER_REFS_DETACHED_HEAD
)
2949 BUG("should have died in the xor check above");
2953 static int memcasecmp(const void *vs1
, const void *vs2
, size_t n
)
2955 const char *s1
= vs1
, *s2
= vs2
;
2956 const char *end
= s1
+ n
;
2958 for (; s1
< end
; s1
++, s2
++) {
2959 int diff
= tolower(*s1
) - tolower(*s2
);
2966 struct ref_sorting
{
2967 struct ref_sorting
*next
;
2968 int atom
; /* index into used_atom array (internal) */
2969 enum ref_sorting_order sort_flags
;
2972 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2974 struct atom_value
*va
, *vb
;
2976 int cmp_detached_head
= 0;
2977 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2978 struct strbuf err
= STRBUF_INIT
;
2980 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2982 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2984 strbuf_release(&err
);
2985 if (s
->sort_flags
& REF_SORTING_DETACHED_HEAD_FIRST
&&
2986 ((a
->kind
| b
->kind
) & FILTER_REFS_DETACHED_HEAD
)) {
2987 cmp
= compare_detached_head(a
, b
);
2988 cmp_detached_head
= 1;
2989 } else if (s
->sort_flags
& REF_SORTING_VERSION
) {
2990 cmp
= versioncmp(va
->s
, vb
->s
);
2991 } else if (cmp_type
== FIELD_STR
) {
2992 if (va
->s_size
< 0 && vb
->s_size
< 0) {
2993 int (*cmp_fn
)(const char *, const char *);
2994 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
2995 ? strcasecmp
: strcmp
;
2996 cmp
= cmp_fn(va
->s
, vb
->s
);
2998 size_t a_size
= va
->s_size
< 0 ?
2999 strlen(va
->s
) : va
->s_size
;
3000 size_t b_size
= vb
->s_size
< 0 ?
3001 strlen(vb
->s
) : vb
->s_size
;
3002 int (*cmp_fn
)(const void *, const void *, size_t);
3003 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
3004 ? memcasecmp
: memcmp
;
3006 cmp
= cmp_fn(va
->s
, vb
->s
, b_size
> a_size
?
3009 if (a_size
> b_size
)
3011 else if (a_size
< b_size
)
3016 if (va
->value
< vb
->value
)
3018 else if (va
->value
== vb
->value
)
3024 return (s
->sort_flags
& REF_SORTING_REVERSE
&& !cmp_detached_head
)
3028 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
3030 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
3031 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
3032 struct ref_sorting
*s
;
3034 for (s
= ref_sorting
; s
; s
= s
->next
) {
3035 int cmp
= cmp_ref_sorting(s
, a
, b
);
3040 return s
&& s
->sort_flags
& REF_SORTING_ICASE
?
3041 strcasecmp(a
->refname
, b
->refname
) :
3042 strcmp(a
->refname
, b
->refname
);
3045 void ref_sorting_set_sort_flags_all(struct ref_sorting
*sorting
,
3046 unsigned int mask
, int on
)
3048 for (; sorting
; sorting
= sorting
->next
) {
3050 sorting
->sort_flags
|= mask
;
3052 sorting
->sort_flags
&= ~mask
;
3056 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
3058 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
3061 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
3063 struct strbuf
*s
= &state
->stack
->output
;
3065 while (*cp
&& (!ep
|| cp
< ep
)) {
3070 int ch
= hex2chr(cp
+ 1);
3072 strbuf_addch(s
, ch
);
3078 strbuf_addch(s
, *cp
);
3083 int format_ref_array_item(struct ref_array_item
*info
,
3084 struct ref_format
*format
,
3085 struct strbuf
*final_buf
,
3086 struct strbuf
*error_buf
)
3088 const char *cp
, *sp
, *ep
;
3089 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
3091 state
.quote_style
= format
->quote_style
;
3092 push_stack_element(&state
.stack
);
3094 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
3095 struct atom_value
*atomv
;
3098 ep
= strchr(sp
, ')');
3100 append_literal(cp
, sp
, &state
);
3101 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
3102 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
3103 atomv
->handler(atomv
, &state
, error_buf
)) {
3104 pop_stack_element(&state
.stack
);
3109 sp
= cp
+ strlen(cp
);
3110 append_literal(cp
, sp
, &state
);
3112 if (format
->need_color_reset_at_eol
) {
3113 struct atom_value resetv
= ATOM_VALUE_INIT
;
3114 resetv
.s
= GIT_COLOR_RESET
;
3115 if (append_atom(&resetv
, &state
, error_buf
)) {
3116 pop_stack_element(&state
.stack
);
3120 if (state
.stack
->prev
) {
3121 pop_stack_element(&state
.stack
);
3122 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
3124 strbuf_addbuf(final_buf
, &state
.stack
->output
);
3125 pop_stack_element(&state
.stack
);
3129 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
3130 struct ref_format
*format
)
3132 struct ref_array_item
*ref_item
;
3133 struct strbuf output
= STRBUF_INIT
;
3134 struct strbuf err
= STRBUF_INIT
;
3136 ref_item
= new_ref_array_item(name
, oid
);
3137 ref_item
->kind
= ref_kind_from_refname(name
);
3138 if (format_ref_array_item(ref_item
, format
, &output
, &err
))
3140 fwrite(output
.buf
, 1, output
.len
, stdout
);
3143 strbuf_release(&err
);
3144 strbuf_release(&output
);
3145 free_array_item(ref_item
);
3148 static int parse_sorting_atom(const char *atom
)
3151 * This parses an atom using a dummy ref_format, since we don't
3152 * actually care about the formatting details.
3154 struct ref_format dummy
= REF_FORMAT_INIT
;
3155 const char *end
= atom
+ strlen(atom
);
3156 struct strbuf err
= STRBUF_INIT
;
3157 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
3160 strbuf_release(&err
);
3164 /* If no sorting option is given, use refname to sort as default */
3165 static struct ref_sorting
*ref_default_sorting(void)
3167 static const char cstr_name
[] = "refname";
3169 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
3171 sorting
->next
= NULL
;
3172 sorting
->atom
= parse_sorting_atom(cstr_name
);
3176 static void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
3178 struct ref_sorting
*s
;
3181 s
->next
= *sorting_tail
;
3185 s
->sort_flags
|= REF_SORTING_REVERSE
;
3188 if (skip_prefix(arg
, "version:", &arg
) ||
3189 skip_prefix(arg
, "v:", &arg
))
3190 s
->sort_flags
|= REF_SORTING_VERSION
;
3191 s
->atom
= parse_sorting_atom(arg
);
3194 struct ref_sorting
*ref_sorting_options(struct string_list
*options
)
3196 struct string_list_item
*item
;
3197 struct ref_sorting
*sorting
= NULL
, **tail
= &sorting
;
3200 sorting
= ref_default_sorting();
3202 for_each_string_list_item(item
, options
)
3203 parse_ref_sorting(tail
, item
->string
);
3207 * From here on, the ref_sorting list should be used to talk
3208 * about the sort order used for the output. The caller
3209 * should not touch the string form anymore.
3211 string_list_clear(options
, 0);
3215 void ref_sorting_release(struct ref_sorting
*sorting
)
3218 struct ref_sorting
*next
= sorting
->next
;
3224 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
3226 struct ref_filter
*rf
= opt
->value
;
3227 struct object_id oid
;
3228 struct commit
*merge_commit
;
3230 BUG_ON_OPT_NEG(unset
);
3232 if (repo_get_oid(the_repository
, arg
, &oid
))
3233 die(_("malformed object name %s"), arg
);
3235 merge_commit
= lookup_commit_reference_gently(the_repository
, &oid
, 0);
3238 return error(_("option `%s' must point to a commit"), opt
->long_name
);
3240 if (starts_with(opt
->long_name
, "no"))
3241 commit_list_insert(merge_commit
, &rf
->unreachable_from
);
3243 commit_list_insert(merge_commit
, &rf
->reachable_from
);
3248 void ref_filter_init(struct ref_filter
*filter
)
3250 struct ref_filter blank
= REF_FILTER_INIT
;
3251 memcpy(filter
, &blank
, sizeof(blank
));
3254 void ref_filter_clear(struct ref_filter
*filter
)
3256 strvec_clear(&filter
->exclude
);
3257 oid_array_clear(&filter
->points_at
);
3258 free_commit_list(filter
->with_commit
);
3259 free_commit_list(filter
->no_commit
);
3260 free_commit_list(filter
->reachable_from
);
3261 free_commit_list(filter
->unreachable_from
);
3262 ref_filter_init(filter
);