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
->type
= FIELD_ULONG
;
588 atom
->u
.contents
.option
= C_LENGTH
;
589 } else if (!strcmp(arg
, "signature"))
590 atom
->u
.contents
.option
= C_SIG
;
591 else if (!strcmp(arg
, "subject"))
592 atom
->u
.contents
.option
= C_SUB
;
593 else if (!strcmp(arg
, "trailers")) {
594 if (trailers_atom_parser(format
, atom
, NULL
, err
))
596 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
597 if (trailers_atom_parser(format
, atom
, arg
, err
))
599 } else if (skip_prefix(arg
, "lines=", &arg
)) {
600 atom
->u
.contents
.option
= C_LINES
;
601 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
602 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
604 return err_bad_arg(err
, "contents", arg
);
608 static int describe_atom_option_parser(struct strvec
*args
, const char **arg
,
615 if (match_atom_bool_arg(*arg
, "tags", arg
, &optval
)) {
617 strvec_push(args
, "--no-tags");
619 strvec_push(args
, "--tags");
623 if (match_atom_arg_value(*arg
, "abbrev", arg
, &argval
, &arglen
)) {
627 return strbuf_addf_ret(err
, -1,
628 _("argument expected for %s"),
630 if (strtol(argval
, &endptr
, 10) < 0)
631 return strbuf_addf_ret(err
, -1,
632 _("positive value expected %s=%s"),
633 "describe:abbrev", argval
);
634 if (endptr
- argval
!= arglen
)
635 return strbuf_addf_ret(err
, -1,
636 _("cannot fully parse %s=%s"),
637 "describe:abbrev", argval
);
639 strvec_pushf(args
, "--abbrev=%.*s", (int)arglen
, argval
);
643 if (match_atom_arg_value(*arg
, "match", arg
, &argval
, &arglen
)) {
645 return strbuf_addf_ret(err
, -1,
646 _("value expected %s="),
649 strvec_pushf(args
, "--match=%.*s", (int)arglen
, argval
);
653 if (match_atom_arg_value(*arg
, "exclude", arg
, &argval
, &arglen
)) {
655 return strbuf_addf_ret(err
, -1,
656 _("value expected %s="),
659 strvec_pushf(args
, "--exclude=%.*s", (int)arglen
, argval
);
666 static int describe_atom_parser(struct ref_format
*format UNUSED
,
667 struct used_atom
*atom
,
668 const char *arg
, struct strbuf
*err
)
670 struct strvec args
= STRVEC_INIT
;
674 const char *bad_arg
= arg
;
679 found
= describe_atom_option_parser(&args
, &arg
, err
);
683 return err_bad_arg(err
, "describe", bad_arg
);
685 atom
->u
.describe_args
= strvec_detach(&args
);
689 static int raw_atom_parser(struct ref_format
*format UNUSED
,
690 struct used_atom
*atom
,
691 const char *arg
, struct strbuf
*err
)
694 atom
->u
.raw_data
.option
= RAW_BARE
;
695 else if (!strcmp(arg
, "size")) {
696 atom
->type
= FIELD_ULONG
;
697 atom
->u
.raw_data
.option
= RAW_LENGTH
;
699 return err_bad_arg(err
, "raw", arg
);
703 static int oid_atom_parser(struct ref_format
*format UNUSED
,
704 struct used_atom
*atom
,
705 const char *arg
, struct strbuf
*err
)
708 atom
->u
.oid
.option
= O_FULL
;
709 else if (!strcmp(arg
, "short"))
710 atom
->u
.oid
.option
= O_SHORT
;
711 else if (skip_prefix(arg
, "short=", &arg
)) {
712 atom
->u
.oid
.option
= O_LENGTH
;
713 if (strtoul_ui(arg
, 10, &atom
->u
.oid
.length
) ||
714 atom
->u
.oid
.length
== 0)
715 return strbuf_addf_ret(err
, -1, _("positive value expected '%s' in %%(%s)"), arg
, atom
->name
);
716 if (atom
->u
.oid
.length
< MINIMUM_ABBREV
)
717 atom
->u
.oid
.length
= MINIMUM_ABBREV
;
719 return err_bad_arg(err
, atom
->name
, arg
);
723 static int person_email_atom_parser(struct ref_format
*format UNUSED
,
724 struct used_atom
*atom
,
725 const char *arg
, struct strbuf
*err
)
728 atom
->u
.email_option
.option
= EO_RAW
;
729 else if (!strcmp(arg
, "trim"))
730 atom
->u
.email_option
.option
= EO_TRIM
;
731 else if (!strcmp(arg
, "localpart"))
732 atom
->u
.email_option
.option
= EO_LOCALPART
;
734 return err_bad_arg(err
, atom
->name
, arg
);
738 static int refname_atom_parser(struct ref_format
*format UNUSED
,
739 struct used_atom
*atom
,
740 const char *arg
, struct strbuf
*err
)
742 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
745 static align_type
parse_align_position(const char *s
)
747 if (!strcmp(s
, "right"))
749 else if (!strcmp(s
, "middle"))
751 else if (!strcmp(s
, "left"))
756 static int align_atom_parser(struct ref_format
*format UNUSED
,
757 struct used_atom
*atom
,
758 const char *arg
, struct strbuf
*err
)
760 struct align
*align
= &atom
->u
.align
;
761 struct string_list params
= STRING_LIST_INIT_DUP
;
763 unsigned int width
= ~0U;
766 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
768 align
->position
= ALIGN_LEFT
;
770 string_list_split(¶ms
, arg
, ',', -1);
771 for (i
= 0; i
< params
.nr
; i
++) {
772 const char *s
= params
.items
[i
].string
;
775 if (skip_prefix(s
, "position=", &s
)) {
776 position
= parse_align_position(s
);
778 strbuf_addf(err
, _("unrecognized position:%s"), s
);
779 string_list_clear(¶ms
, 0);
782 align
->position
= position
;
783 } else if (skip_prefix(s
, "width=", &s
)) {
784 if (strtoul_ui(s
, 10, &width
)) {
785 strbuf_addf(err
, _("unrecognized width:%s"), s
);
786 string_list_clear(¶ms
, 0);
789 } else if (!strtoul_ui(s
, 10, &width
))
791 else if ((position
= parse_align_position(s
)) >= 0)
792 align
->position
= position
;
794 strbuf_addf(err
, _("unrecognized %%(%s) argument: %s"), "align", s
);
795 string_list_clear(¶ms
, 0);
801 string_list_clear(¶ms
, 0);
802 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
804 align
->width
= width
;
805 string_list_clear(¶ms
, 0);
809 static int if_atom_parser(struct ref_format
*format UNUSED
,
810 struct used_atom
*atom
,
811 const char *arg
, struct strbuf
*err
)
814 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
816 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
817 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
818 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
819 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
821 return err_bad_arg(err
, "if", arg
);
825 static int rest_atom_parser(struct ref_format
*format UNUSED
,
826 struct used_atom
*atom UNUSED
,
827 const char *arg
, struct strbuf
*err
)
830 return err_no_arg(err
, "rest");
834 static int ahead_behind_atom_parser(struct ref_format
*format
,
835 struct used_atom
*atom UNUSED
,
836 const char *arg
, struct strbuf
*err
)
838 struct string_list_item
*item
;
841 return strbuf_addf_ret(err
, -1, _("expected format: %%(ahead-behind:<committish>)"));
843 item
= string_list_append(&format
->bases
, arg
);
844 item
->util
= lookup_commit_reference_by_name(arg
);
846 die("failed to find '%s'", arg
);
851 static int head_atom_parser(struct ref_format
*format UNUSED
,
852 struct used_atom
*atom
,
853 const char *arg
, struct strbuf
*err
)
856 return err_no_arg(err
, "HEAD");
857 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
865 int (*parser
)(struct ref_format
*format
, struct used_atom
*atom
,
866 const char *arg
, struct strbuf
*err
);
868 [ATOM_REFNAME
] = { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
869 [ATOM_OBJECTTYPE
] = { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
870 [ATOM_OBJECTSIZE
] = { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
871 [ATOM_OBJECTNAME
] = { "objectname", SOURCE_OTHER
, FIELD_STR
, oid_atom_parser
},
872 [ATOM_DELTABASE
] = { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
873 [ATOM_TREE
] = { "tree", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
874 [ATOM_PARENT
] = { "parent", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
875 [ATOM_NUMPARENT
] = { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
876 [ATOM_OBJECT
] = { "object", SOURCE_OBJ
},
877 [ATOM_TYPE
] = { "type", SOURCE_OBJ
},
878 [ATOM_TAG
] = { "tag", SOURCE_OBJ
},
879 [ATOM_AUTHOR
] = { "author", SOURCE_OBJ
},
880 [ATOM_AUTHORNAME
] = { "authorname", SOURCE_OBJ
},
881 [ATOM_AUTHOREMAIL
] = { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
882 [ATOM_AUTHORDATE
] = { "authordate", SOURCE_OBJ
, FIELD_TIME
},
883 [ATOM_COMMITTER
] = { "committer", SOURCE_OBJ
},
884 [ATOM_COMMITTERNAME
] = { "committername", SOURCE_OBJ
},
885 [ATOM_COMMITTEREMAIL
] = { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
886 [ATOM_COMMITTERDATE
] = { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
887 [ATOM_TAGGER
] = { "tagger", SOURCE_OBJ
},
888 [ATOM_TAGGERNAME
] = { "taggername", SOURCE_OBJ
},
889 [ATOM_TAGGEREMAIL
] = { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
890 [ATOM_TAGGERDATE
] = { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
891 [ATOM_CREATOR
] = { "creator", SOURCE_OBJ
},
892 [ATOM_CREATORDATE
] = { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
893 [ATOM_DESCRIBE
] = { "describe", SOURCE_OBJ
, FIELD_STR
, describe_atom_parser
},
894 [ATOM_SUBJECT
] = { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
895 [ATOM_BODY
] = { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
896 [ATOM_TRAILERS
] = { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
897 [ATOM_CONTENTS
] = { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
898 [ATOM_SIGNATURE
] = { "signature", SOURCE_OBJ
, FIELD_STR
, signature_atom_parser
},
899 [ATOM_RAW
] = { "raw", SOURCE_OBJ
, FIELD_STR
, raw_atom_parser
},
900 [ATOM_UPSTREAM
] = { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
901 [ATOM_PUSH
] = { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
902 [ATOM_SYMREF
] = { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
903 [ATOM_FLAG
] = { "flag", SOURCE_NONE
},
904 [ATOM_HEAD
] = { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
905 [ATOM_COLOR
] = { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
906 [ATOM_WORKTREEPATH
] = { "worktreepath", SOURCE_NONE
},
907 [ATOM_ALIGN
] = { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
908 [ATOM_END
] = { "end", SOURCE_NONE
},
909 [ATOM_IF
] = { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
910 [ATOM_THEN
] = { "then", SOURCE_NONE
},
911 [ATOM_ELSE
] = { "else", SOURCE_NONE
},
912 [ATOM_REST
] = { "rest", SOURCE_NONE
, FIELD_STR
, rest_atom_parser
},
913 [ATOM_AHEADBEHIND
] = { "ahead-behind", SOURCE_OTHER
, FIELD_STR
, ahead_behind_atom_parser
},
915 * Please update $__git_ref_fieldlist in git-completion.bash
916 * when you add new atoms
920 #define REF_FORMATTING_STATE_INIT { 0 }
922 struct ref_formatting_stack
{
923 struct ref_formatting_stack
*prev
;
924 struct strbuf output
;
925 void (*at_end
)(struct ref_formatting_stack
**stack
);
929 struct ref_formatting_state
{
931 struct ref_formatting_stack
*stack
;
937 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
939 uintmax_t value
; /* used for sorting when not FIELD_STR */
940 struct used_atom
*atom
;
943 #define ATOM_SIZE_UNSPECIFIED (-1)
945 #define ATOM_VALUE_INIT { \
946 .s_size = ATOM_SIZE_UNSPECIFIED \
950 * Used to parse format string and sort specifiers
952 static int parse_ref_filter_atom(struct ref_format
*format
,
953 const char *atom
, const char *ep
,
961 if (*sp
== '*' && sp
< ep
)
964 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
965 (int)(ep
-atom
), atom
);
968 * If the atom name has a colon, strip it and everything after
969 * it off - it specifies the format for this entry, and
970 * shouldn't be used for checking against the valid_atom
973 arg
= memchr(sp
, ':', ep
- sp
);
974 atom_len
= (arg
? arg
: ep
) - sp
;
976 /* Do we have the atom already used elsewhere? */
977 for (i
= 0; i
< used_atom_cnt
; i
++) {
978 int len
= strlen(used_atom
[i
].name
);
979 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
983 /* Is the atom a valid one? */
984 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
985 int len
= strlen(valid_atom
[i
].name
);
986 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
990 if (ARRAY_SIZE(valid_atom
) <= i
)
991 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
992 (int)(ep
-atom
), atom
);
993 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
994 return strbuf_addf_ret(err
, -1,
995 _("not a git repository, but the field '%.*s' requires access to object data"),
996 (int)(ep
-atom
), atom
);
998 /* Add it in, including the deref prefix */
1001 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
1002 used_atom
[at
].atom_type
= i
;
1003 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
1004 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
1005 used_atom
[at
].source
= valid_atom
[i
].source
;
1006 if (used_atom
[at
].source
== SOURCE_OBJ
) {
1008 oi_deref
.info
.contentp
= &oi_deref
.content
;
1010 oi
.info
.contentp
= &oi
.content
;
1013 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
1016 * Treat empty sub-arguments list as NULL (i.e.,
1017 * "%(atom:)" is equivalent to "%(atom)").
1022 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
1023 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
1027 if (i
== ATOM_SYMREF
)
1032 static void quote_formatting(struct strbuf
*s
, const char *str
, ssize_t len
, int quote_style
)
1034 switch (quote_style
) {
1037 strbuf_addstr(s
, str
);
1039 strbuf_add(s
, str
, len
);
1042 sq_quote_buf(s
, str
);
1046 perl_quote_buf(s
, str
);
1048 perl_quote_buf_with_len(s
, str
, len
);
1051 python_quote_buf(s
, str
);
1054 tcl_quote_buf(s
, str
);
1059 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
1060 struct strbuf
*err UNUSED
)
1063 * Quote formatting is only done when the stack has a single
1064 * element. Otherwise quote formatting is done on the
1065 * element's entire output strbuf when the %(end) atom is
1068 if (!state
->stack
->prev
)
1069 quote_formatting(&state
->stack
->output
, v
->s
, v
->s_size
, state
->quote_style
);
1070 else if (v
->s_size
< 0)
1071 strbuf_addstr(&state
->stack
->output
, v
->s
);
1073 strbuf_add(&state
->stack
->output
, v
->s
, v
->s_size
);
1077 static void push_stack_element(struct ref_formatting_stack
**stack
)
1079 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
1081 strbuf_init(&s
->output
, 0);
1086 static void pop_stack_element(struct ref_formatting_stack
**stack
)
1088 struct ref_formatting_stack
*current
= *stack
;
1089 struct ref_formatting_stack
*prev
= current
->prev
;
1092 strbuf_addbuf(&prev
->output
, ¤t
->output
);
1093 strbuf_release(¤t
->output
);
1098 static void end_align_handler(struct ref_formatting_stack
**stack
)
1100 struct ref_formatting_stack
*cur
= *stack
;
1101 struct align
*align
= (struct align
*)cur
->at_end_data
;
1102 struct strbuf s
= STRBUF_INIT
;
1104 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
1105 strbuf_swap(&cur
->output
, &s
);
1109 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
1110 struct strbuf
*err UNUSED
)
1112 struct ref_formatting_stack
*new_stack
;
1114 push_stack_element(&state
->stack
);
1115 new_stack
= state
->stack
;
1116 new_stack
->at_end
= end_align_handler
;
1117 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
1121 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
1123 struct ref_formatting_stack
*cur
= *stack
;
1124 struct ref_formatting_stack
*prev
= cur
->prev
;
1125 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1127 if (!if_then_else
->then_atom_seen
)
1128 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
1130 if (if_then_else
->else_atom_seen
) {
1132 * There is an %(else) atom: we need to drop one state from the
1133 * stack, either the %(else) branch if the condition is satisfied, or
1134 * the %(then) branch if it isn't.
1136 if (if_then_else
->condition_satisfied
) {
1137 strbuf_reset(&cur
->output
);
1138 pop_stack_element(&cur
);
1140 strbuf_swap(&cur
->output
, &prev
->output
);
1141 strbuf_reset(&cur
->output
);
1142 pop_stack_element(&cur
);
1144 } else if (!if_then_else
->condition_satisfied
) {
1146 * No %(else) atom: just drop the %(then) branch if the
1147 * condition is not satisfied.
1149 strbuf_reset(&cur
->output
);
1156 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
1157 struct strbuf
*err UNUSED
)
1159 struct ref_formatting_stack
*new_stack
;
1160 struct if_then_else
*if_then_else
= xcalloc(1,
1161 sizeof(struct if_then_else
));
1163 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
1164 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
1166 push_stack_element(&state
->stack
);
1167 new_stack
= state
->stack
;
1168 new_stack
->at_end
= if_then_else_handler
;
1169 new_stack
->at_end_data
= if_then_else
;
1173 static int is_empty(struct strbuf
*buf
)
1175 const char *cur
= buf
->buf
;
1176 const char *end
= buf
->buf
+ buf
->len
;
1178 while (cur
!= end
&& (isspace(*cur
)))
1184 static int then_atom_handler(struct atom_value
*atomv UNUSED
,
1185 struct ref_formatting_state
*state
,
1188 struct ref_formatting_stack
*cur
= state
->stack
;
1189 struct if_then_else
*if_then_else
= NULL
;
1192 if (cur
->at_end
== if_then_else_handler
)
1193 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1195 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
1196 if (if_then_else
->then_atom_seen
)
1197 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
1198 if (if_then_else
->else_atom_seen
)
1199 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
1200 if_then_else
->then_atom_seen
= 1;
1201 if (if_then_else
->str
)
1202 str_len
= strlen(if_then_else
->str
);
1204 * If the 'equals' or 'notequals' attribute is used then
1205 * perform the required comparison. If not, only non-empty
1206 * strings satisfy the 'if' condition.
1208 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
1209 if (str_len
== cur
->output
.len
&&
1210 !memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1211 if_then_else
->condition_satisfied
= 1;
1212 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
1213 if (str_len
!= cur
->output
.len
||
1214 memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1215 if_then_else
->condition_satisfied
= 1;
1216 } else if (cur
->output
.len
&& !is_empty(&cur
->output
))
1217 if_then_else
->condition_satisfied
= 1;
1218 strbuf_reset(&cur
->output
);
1222 static int else_atom_handler(struct atom_value
*atomv UNUSED
,
1223 struct ref_formatting_state
*state
,
1226 struct ref_formatting_stack
*prev
= state
->stack
;
1227 struct if_then_else
*if_then_else
= NULL
;
1229 if (prev
->at_end
== if_then_else_handler
)
1230 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
1232 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
1233 if (!if_then_else
->then_atom_seen
)
1234 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
1235 if (if_then_else
->else_atom_seen
)
1236 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
1237 if_then_else
->else_atom_seen
= 1;
1238 push_stack_element(&state
->stack
);
1239 state
->stack
->at_end_data
= prev
->at_end_data
;
1240 state
->stack
->at_end
= prev
->at_end
;
1244 static int end_atom_handler(struct atom_value
*atomv UNUSED
,
1245 struct ref_formatting_state
*state
,
1248 struct ref_formatting_stack
*current
= state
->stack
;
1249 struct strbuf s
= STRBUF_INIT
;
1251 if (!current
->at_end
)
1252 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
1253 current
->at_end(&state
->stack
);
1255 /* Stack may have been popped within at_end(), hence reset the current pointer */
1256 current
= state
->stack
;
1259 * Perform quote formatting when the stack element is that of
1260 * a supporting atom. If nested then perform quote formatting
1261 * only on the topmost supporting atom.
1263 if (!current
->prev
->prev
) {
1264 quote_formatting(&s
, current
->output
.buf
, current
->output
.len
, state
->quote_style
);
1265 strbuf_swap(¤t
->output
, &s
);
1268 pop_stack_element(&state
->stack
);
1273 * In a format string, find the next occurrence of %(atom).
1275 static const char *find_next(const char *cp
)
1280 * %( is the start of an atom;
1281 * %% is a quoted per-cent.
1285 else if (cp
[1] == '%')
1286 cp
++; /* skip over two % */
1287 /* otherwise this is a singleton, literal % */
1294 static int reject_atom(enum atom_type atom_type
)
1296 return atom_type
== ATOM_REST
;
1300 * Make sure the format string is well formed, and parse out
1303 int verify_ref_format(struct ref_format
*format
)
1305 const char *cp
, *sp
;
1307 format
->need_color_reset_at_eol
= 0;
1308 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
1309 struct strbuf err
= STRBUF_INIT
;
1310 const char *color
, *ep
= strchr(sp
, ')');
1314 return error(_("malformed format string %s"), sp
);
1315 /* sp points at "%(" and ep points at the closing ")" */
1316 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
1319 if (reject_atom(used_atom
[at
].atom_type
))
1320 die(_("this command reject atom %%(%.*s)"), (int)(ep
- sp
- 2), sp
+ 2);
1322 if ((format
->quote_style
== QUOTE_PYTHON
||
1323 format
->quote_style
== QUOTE_SHELL
||
1324 format
->quote_style
== QUOTE_TCL
) &&
1325 used_atom
[at
].atom_type
== ATOM_RAW
&&
1326 used_atom
[at
].u
.raw_data
.option
== RAW_BARE
)
1327 die(_("--format=%.*s cannot be used with "
1328 "--python, --shell, --tcl"), (int)(ep
- sp
- 2), sp
+ 2);
1331 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
1332 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
1333 strbuf_release(&err
);
1335 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
1336 format
->need_color_reset_at_eol
= 0;
1340 static const char *do_grab_oid(const char *field
, const struct object_id
*oid
,
1341 struct used_atom
*atom
)
1343 switch (atom
->u
.oid
.option
) {
1345 return oid_to_hex(oid
);
1347 return repo_find_unique_abbrev(the_repository
, oid
,
1348 atom
->u
.oid
.length
);
1350 return repo_find_unique_abbrev(the_repository
, oid
,
1353 BUG("unknown %%(%s) option", field
);
1357 static int grab_oid(const char *name
, const char *field
, const struct object_id
*oid
,
1358 struct atom_value
*v
, struct used_atom
*atom
)
1360 if (starts_with(name
, field
)) {
1361 v
->s
= xstrdup(do_grab_oid(field
, oid
, atom
));
1367 /* See grab_values */
1368 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
1372 for (i
= 0; i
< used_atom_cnt
; i
++) {
1373 const char *name
= used_atom
[i
].name
;
1374 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1375 struct atom_value
*v
= &val
[i
];
1376 if (!!deref
!= (*name
== '*'))
1380 if (atom_type
== ATOM_OBJECTTYPE
)
1381 v
->s
= xstrdup(type_name(oi
->type
));
1382 else if (atom_type
== ATOM_OBJECTSIZE
) {
1383 if (used_atom
[i
].u
.objectsize
.option
== O_SIZE_DISK
) {
1384 v
->value
= oi
->disk_size
;
1385 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
1386 } else if (used_atom
[i
].u
.objectsize
.option
== O_SIZE
) {
1387 v
->value
= oi
->size
;
1388 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
1390 } else if (atom_type
== ATOM_DELTABASE
)
1391 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
1392 else if (atom_type
== ATOM_OBJECTNAME
&& deref
)
1393 grab_oid(name
, "objectname", &oi
->oid
, v
, &used_atom
[i
]);
1397 /* See grab_values */
1398 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1401 struct tag
*tag
= (struct tag
*) obj
;
1403 for (i
= 0; i
< used_atom_cnt
; i
++) {
1404 const char *name
= used_atom
[i
].name
;
1405 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1406 struct atom_value
*v
= &val
[i
];
1407 if (!!deref
!= (*name
== '*'))
1411 if (atom_type
== ATOM_TAG
)
1412 v
->s
= xstrdup(tag
->tag
);
1413 else if (atom_type
== ATOM_TYPE
&& tag
->tagged
)
1414 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
1415 else if (atom_type
== ATOM_OBJECT
&& tag
->tagged
)
1416 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
1420 /* See grab_values */
1421 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1424 struct commit
*commit
= (struct commit
*) obj
;
1426 for (i
= 0; i
< used_atom_cnt
; i
++) {
1427 const char *name
= used_atom
[i
].name
;
1428 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1429 struct atom_value
*v
= &val
[i
];
1430 if (!!deref
!= (*name
== '*'))
1434 if (atom_type
== ATOM_TREE
&&
1435 grab_oid(name
, "tree", get_commit_tree_oid(commit
), v
, &used_atom
[i
]))
1437 if (atom_type
== ATOM_NUMPARENT
) {
1438 v
->value
= commit_list_count(commit
->parents
);
1439 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1441 else if (atom_type
== ATOM_PARENT
) {
1442 struct commit_list
*parents
;
1443 struct strbuf s
= STRBUF_INIT
;
1444 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1445 struct object_id
*oid
= &parents
->item
->object
.oid
;
1446 if (parents
!= commit
->parents
)
1447 strbuf_addch(&s
, ' ');
1448 strbuf_addstr(&s
, do_grab_oid("parent", oid
, &used_atom
[i
]));
1450 v
->s
= strbuf_detach(&s
, NULL
);
1455 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1459 if (!strncmp(buf
, who
, wholen
) &&
1461 return buf
+ wholen
+ 1;
1462 eol
= strchr(buf
, '\n');
1467 return ""; /* end of header */
1473 static const char *copy_line(const char *buf
)
1475 const char *eol
= strchrnul(buf
, '\n');
1476 return xmemdupz(buf
, eol
- buf
);
1479 static const char *copy_name(const char *buf
)
1482 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1483 if (starts_with(cp
, " <"))
1484 return xmemdupz(buf
, cp
- buf
);
1489 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1491 const char *email
= strchr(buf
, '<');
1492 const char *eoemail
;
1495 switch (atom
->u
.email_option
.option
) {
1497 eoemail
= strchr(email
, '>');
1503 eoemail
= strchr(email
, '>');
1507 eoemail
= strchr(email
, '@');
1509 eoemail
= strchr(email
, '>');
1512 BUG("unknown email option");
1517 return xmemdupz(email
, eoemail
- email
);
1520 static char *copy_subject(const char *buf
, unsigned long len
)
1522 struct strbuf sb
= STRBUF_INIT
;
1525 for (i
= 0; i
< len
; i
++) {
1526 if (buf
[i
] == '\r' && i
+ 1 < len
&& buf
[i
+ 1] == '\n')
1527 continue; /* ignore CR in CRLF */
1530 strbuf_addch(&sb
, ' ');
1532 strbuf_addch(&sb
, buf
[i
]);
1534 return strbuf_detach(&sb
, NULL
);
1537 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1539 const char *eoemail
= strstr(buf
, "> ");
1541 timestamp_t timestamp
;
1543 struct date_mode date_mode
= DATE_MODE_INIT
;
1544 const char *formatp
;
1547 * We got here because atomname ends in "date" or "date<something>";
1548 * it's not possible that <something> is not ":<format>" because
1549 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1550 * ":" means no format is specified, and use the default.
1552 formatp
= strchr(atomname
, ':');
1555 parse_date_format(formatp
, &date_mode
);
1560 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1561 if (timestamp
== TIME_MAX
)
1563 tz
= strtol(zone
, NULL
, 10);
1564 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1566 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1567 v
->value
= timestamp
;
1568 date_mode_release(&date_mode
);
1575 /* See grab_values */
1576 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1579 int wholen
= strlen(who
);
1580 const char *wholine
= NULL
;
1582 for (i
= 0; i
< used_atom_cnt
; i
++) {
1583 const char *name
= used_atom
[i
].name
;
1584 struct atom_value
*v
= &val
[i
];
1585 if (!!deref
!= (*name
== '*'))
1589 if (strncmp(who
, name
, wholen
))
1591 if (name
[wholen
] != 0 &&
1592 strcmp(name
+ wholen
, "name") &&
1593 !starts_with(name
+ wholen
, "email") &&
1594 !starts_with(name
+ wholen
, "date"))
1597 wholine
= find_wholine(who
, wholen
, buf
);
1599 return; /* no point looking for it */
1600 if (name
[wholen
] == 0)
1601 v
->s
= copy_line(wholine
);
1602 else if (!strcmp(name
+ wholen
, "name"))
1603 v
->s
= copy_name(wholine
);
1604 else if (starts_with(name
+ wholen
, "email"))
1605 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1606 else if (starts_with(name
+ wholen
, "date"))
1607 grab_date(wholine
, v
, name
);
1611 * For a tag or a commit object, if "creator" or "creatordate" is
1612 * requested, do something special.
1614 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1615 return; /* "author" for commit object is not wanted */
1617 wholine
= find_wholine(who
, wholen
, buf
);
1620 for (i
= 0; i
< used_atom_cnt
; i
++) {
1621 const char *name
= used_atom
[i
].name
;
1622 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1623 struct atom_value
*v
= &val
[i
];
1624 if (!!deref
!= (*name
== '*'))
1629 if (atom_type
== ATOM_CREATORDATE
)
1630 grab_date(wholine
, v
, name
);
1631 else if (atom_type
== ATOM_CREATOR
)
1632 v
->s
= copy_line(wholine
);
1636 static void grab_signature(struct atom_value
*val
, int deref
, struct object
*obj
)
1639 struct commit
*commit
= (struct commit
*) obj
;
1640 struct signature_check sigc
= { 0 };
1641 int signature_checked
= 0;
1643 for (i
= 0; i
< used_atom_cnt
; i
++) {
1644 struct used_atom
*atom
= &used_atom
[i
];
1645 const char *name
= atom
->name
;
1646 struct atom_value
*v
= &val
[i
];
1649 if (!!deref
!= (*name
== '*'))
1654 if (!skip_prefix(name
, "signature", &name
) ||
1655 (*name
&& *name
!= ':'))
1662 opt
= parse_signature_option(name
);
1666 if (!signature_checked
) {
1667 check_commit_signature(commit
, &sigc
);
1668 signature_checked
= 1;
1673 v
->s
= xstrdup(sigc
.output
? sigc
.output
: "");
1676 v
->s
= xstrdup(sigc
.signer
? sigc
.signer
: "");
1679 switch (sigc
.result
) {
1681 switch (sigc
.trust_level
) {
1682 case TRUST_UNDEFINED
:
1684 v
->s
= xstrfmt("%c", (char)'U');
1687 v
->s
= xstrfmt("%c", (char)'G');
1697 v
->s
= xstrfmt("%c", (char)sigc
.result
);
1702 v
->s
= xstrdup(sigc
.key
? sigc
.key
: "");
1705 v
->s
= xstrdup(sigc
.fingerprint
?
1706 sigc
.fingerprint
: "");
1709 v
->s
= xstrdup(sigc
.primary_key_fingerprint
?
1710 sigc
.primary_key_fingerprint
: "");
1713 v
->s
= xstrdup(gpg_trust_level_to_str(sigc
.trust_level
));
1718 if (signature_checked
)
1719 signature_check_clear(&sigc
);
1722 static void find_subpos(const char *buf
,
1723 const char **sub
, size_t *sublen
,
1724 const char **body
, size_t *bodylen
,
1726 const char **sig
, size_t *siglen
)
1728 struct strbuf payload
= STRBUF_INIT
;
1729 struct strbuf signature
= STRBUF_INIT
;
1731 const char *end
= buf
+ strlen(buf
);
1732 const char *sigstart
;
1734 /* parse signature first; we might not even have a subject line */
1735 parse_signature(buf
, end
- buf
, &payload
, &signature
);
1736 strbuf_release(&payload
);
1738 /* skip past header until we hit empty line */
1739 while (*buf
&& *buf
!= '\n') {
1740 eol
= strchrnul(buf
, '\n');
1745 /* skip any empty lines */
1746 while (*buf
== '\n')
1748 *sig
= strbuf_detach(&signature
, siglen
);
1749 sigstart
= buf
+ parse_signed_buffer(buf
, strlen(buf
));
1751 /* subject is first non-empty line */
1753 /* subject goes to first empty line before signature begins */
1754 if ((eol
= strstr(*sub
, "\n\n")) ||
1755 (eol
= strstr(*sub
, "\r\n\r\n"))) {
1756 eol
= eol
< sigstart
? eol
: sigstart
;
1758 /* treat whole message as subject */
1762 *sublen
= buf
- *sub
;
1763 /* drop trailing newline, if present */
1764 while (*sublen
&& ((*sub
)[*sublen
- 1] == '\n' ||
1765 (*sub
)[*sublen
- 1] == '\r'))
1768 /* skip any empty lines */
1769 while (*buf
== '\n' || *buf
== '\r')
1772 *bodylen
= strlen(buf
);
1773 *nonsiglen
= sigstart
- buf
;
1777 * If 'lines' is greater than 0, append that many lines from the given
1778 * 'buf' of length 'size' to the given strbuf.
1780 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1783 const char *sp
, *eol
;
1788 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1790 strbuf_addstr(out
, "\n ");
1791 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1792 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1793 strbuf_add(out
, sp
, len
);
1800 static void grab_describe_values(struct atom_value
*val
, int deref
,
1803 struct commit
*commit
= (struct commit
*)obj
;
1806 for (i
= 0; i
< used_atom_cnt
; i
++) {
1807 struct used_atom
*atom
= &used_atom
[i
];
1808 enum atom_type type
= atom
->atom_type
;
1809 const char *name
= atom
->name
;
1810 struct atom_value
*v
= &val
[i
];
1812 struct child_process cmd
= CHILD_PROCESS_INIT
;
1813 struct strbuf out
= STRBUF_INIT
;
1814 struct strbuf err
= STRBUF_INIT
;
1816 if (type
!= ATOM_DESCRIBE
)
1819 if (!!deref
!= (*name
== '*'))
1823 strvec_push(&cmd
.args
, "describe");
1824 strvec_pushv(&cmd
.args
, atom
->u
.describe_args
);
1825 strvec_push(&cmd
.args
, oid_to_hex(&commit
->object
.oid
));
1826 if (pipe_command(&cmd
, NULL
, 0, &out
, 0, &err
, 0) < 0) {
1827 error(_("failed to run 'describe'"));
1832 v
->s
= strbuf_detach(&out
, NULL
);
1834 strbuf_release(&err
);
1838 /* See grab_values */
1839 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct expand_data
*data
)
1842 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1843 size_t sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1844 void *buf
= data
->content
;
1846 for (i
= 0; i
< used_atom_cnt
; i
++) {
1847 struct used_atom
*atom
= &used_atom
[i
];
1848 const char *name
= atom
->name
;
1849 struct atom_value
*v
= &val
[i
];
1850 enum atom_type atom_type
= atom
->atom_type
;
1852 if (!!deref
!= (*name
== '*'))
1857 if (atom_type
== ATOM_RAW
) {
1858 unsigned long buf_size
= data
->size
;
1860 if (atom
->u
.raw_data
.option
== RAW_BARE
) {
1861 v
->s
= xmemdupz(buf
, buf_size
);
1862 v
->s_size
= buf_size
;
1863 } else if (atom
->u
.raw_data
.option
== RAW_LENGTH
) {
1864 v
->value
= buf_size
;
1865 v
->s
= xstrfmt("%"PRIuMAX
, v
->value
);
1870 if ((data
->type
!= OBJ_TAG
&&
1871 data
->type
!= OBJ_COMMIT
) ||
1872 (strcmp(name
, "body") &&
1873 !starts_with(name
, "subject") &&
1874 !starts_with(name
, "trailers") &&
1875 !starts_with(name
, "contents")))
1880 &bodypos
, &bodylen
, &nonsiglen
,
1883 if (atom
->u
.contents
.option
== C_SUB
)
1884 v
->s
= copy_subject(subpos
, sublen
);
1885 else if (atom
->u
.contents
.option
== C_SUB_SANITIZE
) {
1886 struct strbuf sb
= STRBUF_INIT
;
1887 format_sanitized_subject(&sb
, subpos
, sublen
);
1888 v
->s
= strbuf_detach(&sb
, NULL
);
1889 } else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1890 v
->s
= xmemdupz(bodypos
, bodylen
);
1891 else if (atom
->u
.contents
.option
== C_LENGTH
) {
1892 v
->value
= strlen(subpos
);
1893 v
->s
= xstrfmt("%"PRIuMAX
, v
->value
);
1894 } else if (atom
->u
.contents
.option
== C_BODY
)
1895 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1896 else if (atom
->u
.contents
.option
== C_SIG
)
1897 v
->s
= xmemdupz(sigpos
, siglen
);
1898 else if (atom
->u
.contents
.option
== C_LINES
) {
1899 struct strbuf s
= STRBUF_INIT
;
1900 const char *contents_end
= bodypos
+ nonsiglen
;
1902 /* Size is the length of the message after removing the signature */
1903 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1904 v
->s
= strbuf_detach(&s
, NULL
);
1905 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1906 struct strbuf s
= STRBUF_INIT
;
1908 /* Format the trailer info according to the trailer_opts given */
1909 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1911 v
->s
= strbuf_detach(&s
, NULL
);
1912 } else if (atom
->u
.contents
.option
== C_BARE
)
1913 v
->s
= xstrdup(subpos
);
1916 free((void *)sigpos
);
1920 * We want to have empty print-string for field requests
1921 * that do not apply (e.g. "authordate" for a tag object)
1923 static void fill_missing_values(struct atom_value
*val
)
1926 for (i
= 0; i
< used_atom_cnt
; i
++) {
1927 struct atom_value
*v
= &val
[i
];
1934 * val is a list of atom_value to hold returned values. Extract
1935 * the values for atoms in used_atom array out of (obj, buf, sz).
1936 * when deref is false, (obj, buf, sz) is the object that is
1937 * pointed at by the ref itself; otherwise it is the object the
1938 * ref (which is a tag) refers to.
1940 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, struct expand_data
*data
)
1942 void *buf
= data
->content
;
1944 switch (obj
->type
) {
1946 grab_tag_values(val
, deref
, obj
);
1947 grab_sub_body_contents(val
, deref
, data
);
1948 grab_person("tagger", val
, deref
, buf
);
1949 grab_describe_values(val
, deref
, obj
);
1952 grab_commit_values(val
, deref
, obj
);
1953 grab_sub_body_contents(val
, deref
, data
);
1954 grab_person("author", val
, deref
, buf
);
1955 grab_person("committer", val
, deref
, buf
);
1956 grab_signature(val
, deref
, obj
);
1957 grab_describe_values(val
, deref
, obj
);
1960 /* grab_tree_values(val, deref, obj, buf, sz); */
1961 grab_sub_body_contents(val
, deref
, data
);
1964 /* grab_blob_values(val, deref, obj, buf, sz); */
1965 grab_sub_body_contents(val
, deref
, data
);
1968 die("Eh? Object of type %d?", obj
->type
);
1972 static inline char *copy_advance(char *dst
, const char *src
)
1979 static const char *lstrip_ref_components(const char *refname
, int len
)
1981 long remaining
= len
;
1982 const char *start
= xstrdup(refname
);
1983 const char *to_free
= start
;
1987 const char *p
= refname
;
1989 /* Find total no of '/' separated path-components */
1990 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1993 * The number of components we need to strip is now
1994 * the total minus the components to be left (Plus one
1995 * because we count the number of '/', but the number
1996 * of components is one more than the no of '/').
1998 remaining
= i
+ len
+ 1;
2001 while (remaining
> 0) {
2004 free((char *)to_free
);
2012 start
= xstrdup(start
);
2013 free((char *)to_free
);
2017 static const char *rstrip_ref_components(const char *refname
, int len
)
2019 long remaining
= len
;
2020 const char *start
= xstrdup(refname
);
2021 const char *to_free
= start
;
2025 const char *p
= refname
;
2027 /* Find total no of '/' separated path-components */
2028 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
2031 * The number of components we need to strip is now
2032 * the total minus the components to be left (Plus one
2033 * because we count the number of '/', but the number
2034 * of components is one more than the no of '/').
2036 remaining
= i
+ len
+ 1;
2039 while (remaining
-- > 0) {
2040 char *p
= strrchr(start
, '/');
2042 free((char *)to_free
);
2050 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
2052 if (atom
->option
== R_SHORT
)
2053 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
2054 else if (atom
->option
== R_LSTRIP
)
2055 return lstrip_ref_components(refname
, atom
->lstrip
);
2056 else if (atom
->option
== R_RSTRIP
)
2057 return rstrip_ref_components(refname
, atom
->rstrip
);
2059 return xstrdup(refname
);
2062 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
2063 struct branch
*branch
, const char **s
)
2065 int num_ours
, num_theirs
;
2066 if (atom
->u
.remote_ref
.option
== RR_REF
)
2067 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
2068 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
2069 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
2070 NULL
, atom
->u
.remote_ref
.push
,
2071 AHEAD_BEHIND_FULL
) < 0) {
2072 *s
= xstrdup(msgs
.gone
);
2073 } else if (!num_ours
&& !num_theirs
)
2076 *s
= xstrfmt(msgs
.behind
, num_theirs
);
2077 else if (!num_theirs
)
2078 *s
= xstrfmt(msgs
.ahead
, num_ours
);
2080 *s
= xstrfmt(msgs
.ahead_behind
,
2081 num_ours
, num_theirs
);
2082 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
2083 const char *to_free
= *s
;
2084 *s
= xstrfmt("[%s]", *s
);
2085 free((void *)to_free
);
2087 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
2088 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
2089 NULL
, atom
->u
.remote_ref
.push
,
2090 AHEAD_BEHIND_FULL
) < 0) {
2094 if (!num_ours
&& !num_theirs
)
2098 else if (!num_theirs
)
2102 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
2104 const char *remote
= atom
->u
.remote_ref
.push
?
2105 pushremote_for_branch(branch
, &explicit) :
2106 remote_for_branch(branch
, &explicit);
2107 *s
= xstrdup(explicit ? remote
: "");
2108 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
2111 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
2112 *s
= xstrdup(merge
? merge
: "");
2114 BUG("unhandled RR_* enum");
2117 char *get_head_description(void)
2119 struct strbuf desc
= STRBUF_INIT
;
2120 struct wt_status_state state
;
2121 memset(&state
, 0, sizeof(state
));
2122 wt_status_get_state(the_repository
, &state
, 1);
2123 if (state
.rebase_in_progress
||
2124 state
.rebase_interactive_in_progress
) {
2126 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
2129 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
2130 state
.detached_from
);
2131 } else if (state
.bisect_in_progress
)
2132 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
2134 else if (state
.detached_from
) {
2135 if (state
.detached_at
)
2136 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
2137 state
.detached_from
);
2139 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
2140 state
.detached_from
);
2142 strbuf_addstr(&desc
, _("(no branch)"));
2144 wt_status_state_free_buffers(&state
);
2146 return strbuf_detach(&desc
, NULL
);
2149 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
2154 return show_ref(&atom
->u
.refname
, ref
->symref
);
2157 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
2159 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
2160 return get_head_description();
2161 return show_ref(&atom
->u
.refname
, ref
->refname
);
2164 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
2165 struct expand_data
*oi
, struct strbuf
*err
)
2167 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
2169 if (oi
->info
.contentp
) {
2170 /* We need to know that to use parse_object_buffer properly */
2171 oi
->info
.sizep
= &oi
->size
;
2172 oi
->info
.typep
= &oi
->type
;
2174 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
2175 OBJECT_INFO_LOOKUP_REPLACE
))
2176 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2177 oid_to_hex(&oi
->oid
), ref
->refname
);
2178 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
2179 BUG("Object size is less than zero.");
2181 if (oi
->info
.contentp
) {
2182 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
2186 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
2187 oid_to_hex(&oi
->oid
), ref
->refname
);
2189 grab_values(ref
->value
, deref
, *obj
, oi
);
2192 grab_common_values(ref
->value
, deref
, oi
);
2198 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
2202 for (i
= 0; worktrees
[i
]; i
++) {
2203 if (worktrees
[i
]->head_ref
) {
2204 struct ref_to_worktree_entry
*entry
;
2205 entry
= xmalloc(sizeof(*entry
));
2206 entry
->wt
= worktrees
[i
];
2207 hashmap_entry_init(&entry
->ent
,
2208 strhash(worktrees
[i
]->head_ref
));
2210 hashmap_add(map
, &entry
->ent
);
2215 static void lazy_init_worktree_map(void)
2217 if (ref_to_worktree_map
.worktrees
)
2220 ref_to_worktree_map
.worktrees
= get_worktrees();
2221 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
2222 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
2225 static char *get_worktree_path(const struct ref_array_item
*ref
)
2227 struct hashmap_entry entry
, *e
;
2228 struct ref_to_worktree_entry
*lookup_result
;
2230 lazy_init_worktree_map();
2232 hashmap_entry_init(&entry
, strhash(ref
->refname
));
2233 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
2238 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
2240 return xstrdup(lookup_result
->wt
->path
);
2244 * Parse the object referred by ref, and grab needed value.
2246 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
2250 struct object_info empty
= OBJECT_INFO_INIT
;
2251 int ahead_behind_atoms
= 0;
2253 CALLOC_ARRAY(ref
->value
, used_atom_cnt
);
2255 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
2256 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
2259 ref
->symref
= xstrdup("");
2262 /* Fill in specials first */
2263 for (i
= 0; i
< used_atom_cnt
; i
++) {
2264 struct used_atom
*atom
= &used_atom
[i
];
2265 enum atom_type atom_type
= atom
->atom_type
;
2266 const char *name
= used_atom
[i
].name
;
2267 struct atom_value
*v
= &ref
->value
[i
];
2269 const char *refname
;
2270 struct branch
*branch
= NULL
;
2272 v
->s_size
= ATOM_SIZE_UNSPECIFIED
;
2273 v
->handler
= append_atom
;
2282 if (atom_type
== ATOM_REFNAME
)
2283 refname
= get_refname(atom
, ref
);
2284 else if (atom_type
== ATOM_WORKTREEPATH
) {
2285 if (ref
->kind
== FILTER_REFS_BRANCHES
)
2286 v
->s
= get_worktree_path(ref
);
2291 else if (atom_type
== ATOM_SYMREF
)
2292 refname
= get_symref(atom
, ref
);
2293 else if (atom_type
== ATOM_UPSTREAM
) {
2294 const char *branch_name
;
2295 /* only local branches may have an upstream */
2296 if (!skip_prefix(ref
->refname
, "refs/heads/",
2301 branch
= branch_get(branch_name
);
2303 refname
= branch_get_upstream(branch
, NULL
);
2305 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2309 } else if (atom_type
== ATOM_PUSH
&& atom
->u
.remote_ref
.push
) {
2310 const char *branch_name
;
2312 if (!skip_prefix(ref
->refname
, "refs/heads/",
2315 branch
= branch_get(branch_name
);
2317 if (atom
->u
.remote_ref
.push_remote
)
2320 refname
= branch_get_push(branch
, NULL
);
2324 /* We will definitely re-init v->s on the next line. */
2326 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2328 } else if (atom_type
== ATOM_COLOR
) {
2329 v
->s
= xstrdup(atom
->u
.color
);
2331 } else if (atom_type
== ATOM_FLAG
) {
2332 char buf
[256], *cp
= buf
;
2333 if (ref
->flag
& REF_ISSYMREF
)
2334 cp
= copy_advance(cp
, ",symref");
2335 if (ref
->flag
& REF_ISPACKED
)
2336 cp
= copy_advance(cp
, ",packed");
2341 v
->s
= xstrdup(buf
+ 1);
2344 } else if (!deref
&& atom_type
== ATOM_OBJECTNAME
&&
2345 grab_oid(name
, "objectname", &ref
->objectname
, v
, atom
)) {
2347 } else if (atom_type
== ATOM_HEAD
) {
2348 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
2349 v
->s
= xstrdup("*");
2351 v
->s
= xstrdup(" ");
2353 } else if (atom_type
== ATOM_ALIGN
) {
2354 v
->handler
= align_atom_handler
;
2357 } else if (atom_type
== ATOM_END
) {
2358 v
->handler
= end_atom_handler
;
2361 } else if (atom_type
== ATOM_IF
) {
2363 if (skip_prefix(name
, "if:", &s
))
2367 v
->handler
= if_atom_handler
;
2369 } else if (atom_type
== ATOM_THEN
) {
2370 v
->handler
= then_atom_handler
;
2373 } else if (atom_type
== ATOM_ELSE
) {
2374 v
->handler
= else_atom_handler
;
2377 } else if (atom_type
== ATOM_REST
) {
2379 v
->s
= xstrdup(ref
->rest
);
2383 } else if (atom_type
== ATOM_AHEADBEHIND
) {
2385 const struct ahead_behind_count
*count
;
2386 count
= ref
->counts
[ahead_behind_atoms
++];
2387 v
->s
= xstrfmt("%d %d", count
->ahead
, count
->behind
);
2397 v
->s
= xstrdup(refname
);
2399 v
->s
= xstrfmt("%s^{}", refname
);
2400 free((char *)refname
);
2403 for (i
= 0; i
< used_atom_cnt
; i
++) {
2404 struct atom_value
*v
= &ref
->value
[i
];
2405 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
2406 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2407 oid_to_hex(&ref
->objectname
), ref
->refname
);
2411 oi
.info
.contentp
= &oi
.content
;
2412 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
2413 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
2417 oi
.oid
= ref
->objectname
;
2418 if (get_object(ref
, 0, &obj
, &oi
, err
))
2422 * If there is no atom that wants to know about tagged
2423 * object, we are done.
2425 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
2429 * If it is a tag object, see if we use a value that derefs
2430 * the object, and if we do grab the object it refers to.
2432 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
2435 * NEEDSWORK: This derefs tag only once, which
2436 * is good to deal with chains of trust, but
2437 * is not consistent with what deref_tag() does
2438 * which peels the onion to the core.
2440 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
2444 * Given a ref, return the value for the atom. This lazily gets value
2445 * out of the object by calling populate value.
2447 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
2448 struct atom_value
**v
, struct strbuf
*err
)
2451 if (populate_value(ref
, err
))
2453 fill_missing_values(ref
->value
);
2455 *v
= &ref
->value
[atom
];
2460 * Return 1 if the refname matches one of the patterns, otherwise 0.
2461 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2462 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2463 * matches "refs/heads/mas*", too).
2465 static int match_pattern(const char **patterns
, const char *refname
,
2471 flags
|= WM_CASEFOLD
;
2474 * When no '--format' option is given we need to skip the prefix
2475 * for matching refs of tags and branches.
2477 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
2478 skip_prefix(refname
, "refs/heads/", &refname
) ||
2479 skip_prefix(refname
, "refs/remotes/", &refname
) ||
2480 skip_prefix(refname
, "refs/", &refname
));
2482 for (; *patterns
; patterns
++) {
2483 if (!wildmatch(*patterns
, refname
, flags
))
2490 * Return 1 if the refname matches one of the patterns, otherwise 0.
2491 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
2492 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2493 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2495 static int match_name_as_path(const char **pattern
, const char *refname
,
2498 int namelen
= strlen(refname
);
2499 unsigned flags
= WM_PATHNAME
;
2502 flags
|= WM_CASEFOLD
;
2504 for (; *pattern
; pattern
++) {
2505 const char *p
= *pattern
;
2506 int plen
= strlen(p
);
2508 if ((plen
<= namelen
) &&
2509 !strncmp(refname
, p
, plen
) &&
2510 (refname
[plen
] == '\0' ||
2511 refname
[plen
] == '/' ||
2514 if (!wildmatch(p
, refname
, flags
))
2520 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
2521 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
2523 if (!*filter
->name_patterns
)
2524 return 1; /* No pattern always matches */
2525 if (filter
->match_as_path
)
2526 return match_name_as_path(filter
->name_patterns
, refname
,
2527 filter
->ignore_case
);
2528 return match_pattern(filter
->name_patterns
, refname
,
2529 filter
->ignore_case
);
2532 static int filter_exclude_match(struct ref_filter
*filter
, const char *refname
)
2534 if (!filter
->exclude
.nr
)
2536 if (filter
->match_as_path
)
2537 return match_name_as_path(filter
->exclude
.v
, refname
,
2538 filter
->ignore_case
);
2539 return match_pattern(filter
->exclude
.v
, refname
, filter
->ignore_case
);
2543 * This is the same as for_each_fullref_in(), but it tries to iterate
2544 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2545 * pattern match, so the callback still has to match each ref individually.
2547 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
2551 if (!filter
->match_as_path
) {
2553 * in this case, the patterns are applied after
2554 * prefixes like "refs/heads/" etc. are stripped off,
2555 * so we have to look at everything:
2557 return for_each_fullref_in("", cb
, cb_data
);
2560 if (filter
->ignore_case
) {
2562 * we can't handle case-insensitive comparisons,
2563 * so just return everything and let the caller
2566 return for_each_fullref_in("", cb
, cb_data
);
2569 if (!filter
->name_patterns
[0]) {
2570 /* no patterns; we have to look at everything */
2571 return refs_for_each_fullref_in(get_main_ref_store(the_repository
),
2572 "", filter
->exclude
.v
, cb
, cb_data
);
2575 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository
),
2576 NULL
, filter
->name_patterns
,
2582 * Given a ref (oid, refname), check if the ref belongs to the array
2583 * of oids. If the given ref is a tag, check if the given tag points
2584 * at one of the oids in the given oid array. Returns non-zero if a
2588 * As the refs are cached we might know what refname peels to without
2589 * the need to parse the object via parse_object(). peel_ref() might be a
2590 * more efficient alternative to obtain the pointee.
2592 static int match_points_at(struct oid_array
*points_at
,
2593 const struct object_id
*oid
,
2594 const char *refname
)
2598 if (oid_array_lookup(points_at
, oid
) >= 0)
2600 obj
= parse_object_with_flags(the_repository
, oid
,
2601 PARSE_OBJECT_SKIP_HASH_CHECK
);
2602 while (obj
&& obj
->type
== OBJ_TAG
) {
2603 struct tag
*tag
= (struct tag
*)obj
;
2605 if (parse_tag(tag
) < 0) {
2610 if (oid_array_lookup(points_at
, get_tagged_oid(tag
)) >= 0)
2616 die(_("malformed object at '%s'"), refname
);
2621 * Allocate space for a new ref_array_item and copy the name and oid to it.
2623 * Callers can then fill in other struct members at their leisure.
2625 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2626 const struct object_id
*oid
)
2628 struct ref_array_item
*ref
;
2630 FLEX_ALLOC_STR(ref
, refname
, refname
);
2631 oidcpy(&ref
->objectname
, oid
);
2637 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2638 const char *refname
,
2639 const struct object_id
*oid
)
2641 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2643 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2644 array
->items
[array
->nr
++] = ref
;
2649 static int ref_kind_from_refname(const char *refname
)
2657 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2658 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2659 { "refs/tags/", FILTER_REFS_TAGS
}
2662 if (!strcmp(refname
, "HEAD"))
2663 return FILTER_REFS_DETACHED_HEAD
;
2665 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2666 if (starts_with(refname
, ref_kind
[i
].prefix
))
2667 return ref_kind
[i
].kind
;
2670 return FILTER_REFS_OTHERS
;
2673 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2675 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2676 filter
->kind
== FILTER_REFS_REMOTES
||
2677 filter
->kind
== FILTER_REFS_TAGS
)
2678 return filter
->kind
;
2679 return ref_kind_from_refname(refname
);
2682 struct ref_filter_cbdata
{
2683 struct ref_array
*array
;
2684 struct ref_filter
*filter
;
2685 struct contains_cache contains_cache
;
2686 struct contains_cache no_contains_cache
;
2690 * A call-back given to for_each_ref(). Filter refs and keep them for
2691 * later object processing.
2693 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2695 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2696 struct ref_filter
*filter
= ref_cbdata
->filter
;
2697 struct ref_array_item
*ref
;
2698 struct commit
*commit
= NULL
;
2701 if (flag
& REF_BAD_NAME
) {
2702 warning(_("ignoring ref with broken name %s"), refname
);
2706 if (flag
& REF_ISBROKEN
) {
2707 warning(_("ignoring broken ref %s"), refname
);
2711 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2712 kind
= filter_ref_kind(filter
, refname
);
2713 if (!(kind
& filter
->kind
))
2716 if (!filter_pattern_match(filter
, refname
))
2719 if (filter_exclude_match(filter
, refname
))
2722 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2726 * A merge filter is applied on refs pointing to commits. Hence
2727 * obtain the commit using the 'oid' available and discard all
2728 * non-commits early. The actual filtering is done later.
2730 if (filter
->reachable_from
|| filter
->unreachable_from
||
2731 filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2732 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
2735 /* We perform the filtering for the '--contains' option... */
2736 if (filter
->with_commit
&&
2737 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2739 /* ...or for the `--no-contains' option */
2740 if (filter
->no_commit
&&
2741 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2746 * We do not open the object yet; sort may only need refname
2747 * to do its job and the resulting list may yet to be pruned
2748 * by maxcount logic.
2750 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2751 ref
->commit
= commit
;
2758 /* Free memory allocated for a ref_array_item */
2759 static void free_array_item(struct ref_array_item
*item
)
2761 free((char *)item
->symref
);
2764 for (i
= 0; i
< used_atom_cnt
; i
++)
2765 free((char *)item
->value
[i
].s
);
2772 /* Free all memory allocated for ref_array */
2773 void ref_array_clear(struct ref_array
*array
)
2777 for (i
= 0; i
< array
->nr
; i
++)
2778 free_array_item(array
->items
[i
]);
2779 FREE_AND_NULL(array
->items
);
2780 array
->nr
= array
->alloc
= 0;
2782 for (i
= 0; i
< used_atom_cnt
; i
++) {
2783 struct used_atom
*atom
= &used_atom
[i
];
2784 if (atom
->atom_type
== ATOM_HEAD
)
2786 free((char *)atom
->name
);
2788 FREE_AND_NULL(used_atom
);
2791 if (ref_to_worktree_map
.worktrees
) {
2792 hashmap_clear_and_free(&(ref_to_worktree_map
.map
),
2793 struct ref_to_worktree_entry
, ent
);
2794 free_worktrees(ref_to_worktree_map
.worktrees
);
2795 ref_to_worktree_map
.worktrees
= NULL
;
2798 FREE_AND_NULL(array
->counts
);
2801 #define EXCLUDE_REACHED 0
2802 #define INCLUDE_REACHED 1
2803 static void reach_filter(struct ref_array
*array
,
2804 struct commit_list
**check_reachable
,
2805 int include_reached
)
2808 struct commit
**to_clear
;
2810 if (!*check_reachable
)
2813 CALLOC_ARRAY(to_clear
, array
->nr
);
2814 for (i
= 0; i
< array
->nr
; i
++) {
2815 struct ref_array_item
*item
= array
->items
[i
];
2816 to_clear
[i
] = item
->commit
;
2819 tips_reachable_from_bases(the_repository
,
2821 to_clear
, array
->nr
,
2827 for (i
= 0; i
< old_nr
; i
++) {
2828 struct ref_array_item
*item
= array
->items
[i
];
2829 struct commit
*commit
= item
->commit
;
2831 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2833 if (is_merged
== include_reached
)
2834 array
->items
[array
->nr
++] = array
->items
[i
];
2836 free_array_item(item
);
2839 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2841 while (*check_reachable
) {
2842 struct commit
*merge_commit
= pop_commit(check_reachable
);
2843 clear_commit_marks(merge_commit
, ALL_REV_FLAGS
);
2849 void filter_ahead_behind(struct repository
*r
,
2850 struct ref_format
*format
,
2851 struct ref_array
*array
)
2853 struct commit
**commits
;
2854 size_t commits_nr
= format
->bases
.nr
+ array
->nr
;
2856 if (!format
->bases
.nr
|| !array
->nr
)
2859 ALLOC_ARRAY(commits
, commits_nr
);
2860 for (size_t i
= 0; i
< format
->bases
.nr
; i
++)
2861 commits
[i
] = format
->bases
.items
[i
].util
;
2863 ALLOC_ARRAY(array
->counts
, st_mult(format
->bases
.nr
, array
->nr
));
2865 commits_nr
= format
->bases
.nr
;
2866 array
->counts_nr
= 0;
2867 for (size_t i
= 0; i
< array
->nr
; i
++) {
2868 const char *name
= array
->items
[i
]->refname
;
2869 commits
[commits_nr
] = lookup_commit_reference_by_name(name
);
2871 if (!commits
[commits_nr
])
2874 CALLOC_ARRAY(array
->items
[i
]->counts
, format
->bases
.nr
);
2875 for (size_t j
= 0; j
< format
->bases
.nr
; j
++) {
2876 struct ahead_behind_count
*count
;
2877 count
= &array
->counts
[array
->counts_nr
++];
2878 count
->tip_index
= commits_nr
;
2879 count
->base_index
= j
;
2881 array
->items
[i
]->counts
[j
] = count
;
2886 ahead_behind(r
, commits
, commits_nr
, array
->counts
, array
->counts_nr
);
2891 * API for filtering a set of refs. Based on the type of refs the user
2892 * has requested, we iterate through those refs and apply filters
2893 * as per the given ref_filter structure and finally store the
2894 * filtered refs in the ref_array structure.
2896 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2898 struct ref_filter_cbdata ref_cbdata
;
2899 int save_commit_buffer_orig
;
2902 ref_cbdata
.array
= array
;
2903 ref_cbdata
.filter
= filter
;
2905 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2907 save_commit_buffer_orig
= save_commit_buffer
;
2908 save_commit_buffer
= 0;
2910 init_contains_cache(&ref_cbdata
.contains_cache
);
2911 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2913 /* Simple per-ref filtering */
2915 die("filter_refs: invalid type");
2918 * For common cases where we need only branches or remotes or tags,
2919 * we only iterate through those refs. If a mix of refs is needed,
2920 * we iterate over all refs and filter out required refs with the help
2921 * of filter_ref_kind().
2923 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2924 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
);
2925 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2926 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
);
2927 else if (filter
->kind
== FILTER_REFS_TAGS
)
2928 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
);
2929 else if (filter
->kind
& FILTER_REFS_ALL
)
2930 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
);
2931 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2932 head_ref(ref_filter_handler
, &ref_cbdata
);
2935 clear_contains_cache(&ref_cbdata
.contains_cache
);
2936 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2938 /* Filters that need revision walking */
2939 reach_filter(array
, &filter
->reachable_from
, INCLUDE_REACHED
);
2940 reach_filter(array
, &filter
->unreachable_from
, EXCLUDE_REACHED
);
2942 save_commit_buffer
= save_commit_buffer_orig
;
2946 static int compare_detached_head(struct ref_array_item
*a
, struct ref_array_item
*b
)
2948 if (!(a
->kind
^ b
->kind
))
2949 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
2950 if (a
->kind
& FILTER_REFS_DETACHED_HEAD
)
2952 else if (b
->kind
& FILTER_REFS_DETACHED_HEAD
)
2954 BUG("should have died in the xor check above");
2958 static int memcasecmp(const void *vs1
, const void *vs2
, size_t n
)
2960 const char *s1
= vs1
, *s2
= vs2
;
2961 const char *end
= s1
+ n
;
2963 for (; s1
< end
; s1
++, s2
++) {
2964 int diff
= tolower(*s1
) - tolower(*s2
);
2971 struct ref_sorting
{
2972 struct ref_sorting
*next
;
2973 int atom
; /* index into used_atom array (internal) */
2974 enum ref_sorting_order sort_flags
;
2977 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2979 struct atom_value
*va
, *vb
;
2981 int cmp_detached_head
= 0;
2982 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2983 struct strbuf err
= STRBUF_INIT
;
2985 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2987 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2989 strbuf_release(&err
);
2990 if (s
->sort_flags
& REF_SORTING_DETACHED_HEAD_FIRST
&&
2991 ((a
->kind
| b
->kind
) & FILTER_REFS_DETACHED_HEAD
)) {
2992 cmp
= compare_detached_head(a
, b
);
2993 cmp_detached_head
= 1;
2994 } else if (s
->sort_flags
& REF_SORTING_VERSION
) {
2995 cmp
= versioncmp(va
->s
, vb
->s
);
2996 } else if (cmp_type
== FIELD_STR
) {
2997 if (va
->s_size
< 0 && vb
->s_size
< 0) {
2998 int (*cmp_fn
)(const char *, const char *);
2999 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
3000 ? strcasecmp
: strcmp
;
3001 cmp
= cmp_fn(va
->s
, vb
->s
);
3003 size_t a_size
= va
->s_size
< 0 ?
3004 strlen(va
->s
) : va
->s_size
;
3005 size_t b_size
= vb
->s_size
< 0 ?
3006 strlen(vb
->s
) : vb
->s_size
;
3007 int (*cmp_fn
)(const void *, const void *, size_t);
3008 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
3009 ? memcasecmp
: memcmp
;
3011 cmp
= cmp_fn(va
->s
, vb
->s
, b_size
> a_size
?
3014 if (a_size
> b_size
)
3016 else if (a_size
< b_size
)
3021 if (va
->value
< vb
->value
)
3023 else if (va
->value
== vb
->value
)
3029 return (s
->sort_flags
& REF_SORTING_REVERSE
&& !cmp_detached_head
)
3033 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
3035 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
3036 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
3037 struct ref_sorting
*s
;
3039 for (s
= ref_sorting
; s
; s
= s
->next
) {
3040 int cmp
= cmp_ref_sorting(s
, a
, b
);
3045 return s
&& s
->sort_flags
& REF_SORTING_ICASE
?
3046 strcasecmp(a
->refname
, b
->refname
) :
3047 strcmp(a
->refname
, b
->refname
);
3050 void ref_sorting_set_sort_flags_all(struct ref_sorting
*sorting
,
3051 unsigned int mask
, int on
)
3053 for (; sorting
; sorting
= sorting
->next
) {
3055 sorting
->sort_flags
|= mask
;
3057 sorting
->sort_flags
&= ~mask
;
3061 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
3063 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
3066 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
3068 struct strbuf
*s
= &state
->stack
->output
;
3070 while (*cp
&& (!ep
|| cp
< ep
)) {
3075 int ch
= hex2chr(cp
+ 1);
3077 strbuf_addch(s
, ch
);
3083 strbuf_addch(s
, *cp
);
3088 int format_ref_array_item(struct ref_array_item
*info
,
3089 struct ref_format
*format
,
3090 struct strbuf
*final_buf
,
3091 struct strbuf
*error_buf
)
3093 const char *cp
, *sp
, *ep
;
3094 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
3096 state
.quote_style
= format
->quote_style
;
3097 push_stack_element(&state
.stack
);
3099 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
3100 struct atom_value
*atomv
;
3103 ep
= strchr(sp
, ')');
3105 append_literal(cp
, sp
, &state
);
3106 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
3107 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
3108 atomv
->handler(atomv
, &state
, error_buf
)) {
3109 pop_stack_element(&state
.stack
);
3114 sp
= cp
+ strlen(cp
);
3115 append_literal(cp
, sp
, &state
);
3117 if (format
->need_color_reset_at_eol
) {
3118 struct atom_value resetv
= ATOM_VALUE_INIT
;
3119 resetv
.s
= GIT_COLOR_RESET
;
3120 if (append_atom(&resetv
, &state
, error_buf
)) {
3121 pop_stack_element(&state
.stack
);
3125 if (state
.stack
->prev
) {
3126 pop_stack_element(&state
.stack
);
3127 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
3129 strbuf_addbuf(final_buf
, &state
.stack
->output
);
3130 pop_stack_element(&state
.stack
);
3134 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
3135 struct ref_format
*format
)
3137 struct ref_array_item
*ref_item
;
3138 struct strbuf output
= STRBUF_INIT
;
3139 struct strbuf err
= STRBUF_INIT
;
3141 ref_item
= new_ref_array_item(name
, oid
);
3142 ref_item
->kind
= ref_kind_from_refname(name
);
3143 if (format_ref_array_item(ref_item
, format
, &output
, &err
))
3145 fwrite(output
.buf
, 1, output
.len
, stdout
);
3148 strbuf_release(&err
);
3149 strbuf_release(&output
);
3150 free_array_item(ref_item
);
3153 static int parse_sorting_atom(const char *atom
)
3156 * This parses an atom using a dummy ref_format, since we don't
3157 * actually care about the formatting details.
3159 struct ref_format dummy
= REF_FORMAT_INIT
;
3160 const char *end
= atom
+ strlen(atom
);
3161 struct strbuf err
= STRBUF_INIT
;
3162 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
3165 strbuf_release(&err
);
3169 /* If no sorting option is given, use refname to sort as default */
3170 static struct ref_sorting
*ref_default_sorting(void)
3172 static const char cstr_name
[] = "refname";
3174 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
3176 sorting
->next
= NULL
;
3177 sorting
->atom
= parse_sorting_atom(cstr_name
);
3181 static void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
3183 struct ref_sorting
*s
;
3186 s
->next
= *sorting_tail
;
3190 s
->sort_flags
|= REF_SORTING_REVERSE
;
3193 if (skip_prefix(arg
, "version:", &arg
) ||
3194 skip_prefix(arg
, "v:", &arg
))
3195 s
->sort_flags
|= REF_SORTING_VERSION
;
3196 s
->atom
= parse_sorting_atom(arg
);
3199 struct ref_sorting
*ref_sorting_options(struct string_list
*options
)
3201 struct string_list_item
*item
;
3202 struct ref_sorting
*sorting
= NULL
, **tail
= &sorting
;
3205 sorting
= ref_default_sorting();
3207 for_each_string_list_item(item
, options
)
3208 parse_ref_sorting(tail
, item
->string
);
3212 * From here on, the ref_sorting list should be used to talk
3213 * about the sort order used for the output. The caller
3214 * should not touch the string form anymore.
3216 string_list_clear(options
, 0);
3220 void ref_sorting_release(struct ref_sorting
*sorting
)
3223 struct ref_sorting
*next
= sorting
->next
;
3229 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
3231 struct ref_filter
*rf
= opt
->value
;
3232 struct object_id oid
;
3233 struct commit
*merge_commit
;
3235 BUG_ON_OPT_NEG(unset
);
3237 if (repo_get_oid(the_repository
, arg
, &oid
))
3238 die(_("malformed object name %s"), arg
);
3240 merge_commit
= lookup_commit_reference_gently(the_repository
, &oid
, 0);
3243 return error(_("option `%s' must point to a commit"), opt
->long_name
);
3245 if (starts_with(opt
->long_name
, "no"))
3246 commit_list_insert(merge_commit
, &rf
->unreachable_from
);
3248 commit_list_insert(merge_commit
, &rf
->reachable_from
);
3253 void ref_filter_init(struct ref_filter
*filter
)
3255 struct ref_filter blank
= REF_FILTER_INIT
;
3256 memcpy(filter
, &blank
, sizeof(blank
));
3259 void ref_filter_clear(struct ref_filter
*filter
)
3261 strvec_clear(&filter
->exclude
);
3262 oid_array_clear(&filter
->points_at
);
3263 free_commit_list(filter
->with_commit
);
3264 free_commit_list(filter
->no_commit
);
3265 free_commit_list(filter
->reachable_from
);
3266 free_commit_list(filter
->unreachable_from
);
3267 ref_filter_init(filter
);