1 #include "git-compat-util.h"
2 #include "environment.h"
4 #include "gpg-interface.h"
6 #include "parse-options.h"
9 #include "object-name.h"
10 #include "object-store-ll.h"
11 #include "oid-array.h"
12 #include "repository.h"
18 #include "ref-filter.h"
22 #include "versioncmp.h"
24 #include "wt-status.h"
25 #include "commit-slab.h"
26 #include "commit-graph.h"
27 #include "commit-reach.h"
32 static struct ref_msg
{
36 const char *ahead_behind
;
38 /* Untranslated plumbing messages: */
45 void setup_ref_filter_porcelain_msg(void)
47 msgs
.gone
= _("gone");
48 msgs
.ahead
= _("ahead %d");
49 msgs
.behind
= _("behind %d");
50 msgs
.ahead_behind
= _("ahead %d, behind %d");
53 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
54 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
55 typedef enum { SOURCE_NONE
= 0, SOURCE_OBJ
, SOURCE_OTHER
} info_source
;
63 cmp_status cmp_status
;
65 unsigned int then_atom_seen
: 1,
67 condition_satisfied
: 1;
71 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
75 static struct ref_trailer_buf
{
76 struct string_list filter_list
;
78 struct strbuf kvsepbuf
;
79 } ref_trailer_buf
= {STRING_LIST_INIT_NODUP
, STRBUF_INIT
, STRBUF_INIT
};
81 static struct expand_data
{
83 enum object_type type
;
86 struct object_id delta_base_oid
;
89 struct object_info info
;
92 struct ref_to_worktree_entry
{
93 struct hashmap_entry ent
;
94 struct worktree
*wt
; /* key is wt->head_ref */
97 static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED
,
98 const struct hashmap_entry
*eptr
,
99 const struct hashmap_entry
*kptr
,
100 const void *keydata_aka_refname
)
102 const struct ref_to_worktree_entry
*e
, *k
;
104 e
= container_of(eptr
, const struct ref_to_worktree_entry
, ent
);
105 k
= container_of(kptr
, const struct ref_to_worktree_entry
, ent
);
107 return strcmp(e
->wt
->head_ref
,
108 keydata_aka_refname
? keydata_aka_refname
: k
->wt
->head_ref
);
111 static struct ref_to_worktree_map
{
113 struct worktree
**worktrees
;
114 } ref_to_worktree_map
;
117 * The enum atom_type is used as the index of valid_atom array.
118 * In the atom parsing stage, it will be passed to used_atom.atom_type
119 * as the identifier of the atom type. We can check the type of used_atom
120 * entry by `if (used_atom[i].atom_type == ATOM_*)`.
171 * An atom is a valid field atom listed below, possibly prefixed with
172 * a "*" to denote deref_tag().
174 * We parse given format string and sort specifiers, and make a list
175 * of properties that we need to extract out of objects. ref_array_item
176 * structure will hold an array of values extracted that can be
177 * indexed with the "atom number", which is an index into this
180 static struct used_atom
{
181 enum atom_type atom_type
;
186 char color
[COLOR_MAXLEN
];
190 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
192 struct refname_atom refname
;
193 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
196 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LENGTH
, C_LINES
,
197 C_SIG
, C_SUB
, C_SUB_SANITIZE
, C_TRAILERS
} option
;
198 struct process_trailer_options trailer_opts
;
202 enum { RAW_BARE
, RAW_LENGTH
} option
;
205 cmp_status cmp_status
;
209 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
213 enum { O_SIZE
, O_SIZE_DISK
} option
;
215 struct email_option
{
216 enum { EO_RAW
, EO_TRIM
, EO_LOCALPART
} option
;
219 enum { S_BARE
, S_GRADE
, S_SIGNER
, S_KEY
,
220 S_FINGERPRINT
, S_PRI_KEY_FP
, S_TRUST_LEVEL
} option
;
222 struct refname_atom refname
;
226 static int used_atom_cnt
, need_tagged
, need_symref
;
229 * Expand string, append it to strbuf *sb, then return error code ret.
230 * Allow to save few lines of code.
232 __attribute__((format (printf
, 3, 4)))
233 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
237 strbuf_vaddf(sb
, fmt
, ap
);
242 static int err_no_arg(struct strbuf
*sb
, const char *name
)
244 size_t namelen
= strchrnul(name
, ':') - name
;
245 strbuf_addf(sb
, _("%%(%.*s) does not take arguments"),
250 static int err_bad_arg(struct strbuf
*sb
, const char *name
, const char *arg
)
252 size_t namelen
= strchrnul(name
, ':') - name
;
253 strbuf_addf(sb
, _("unrecognized %%(%.*s) argument: %s"),
254 (int)namelen
, name
, arg
);
258 static int color_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
259 const char *color_value
, struct strbuf
*err
)
262 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
263 if (color_parse(color_value
, atom
->u
.color
) < 0)
264 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
267 * We check this after we've parsed the color, which lets us complain
268 * about syntactically bogus color names even if they won't be used.
270 if (!want_color(format
->use_color
))
271 color_parse("", atom
->u
.color
);
275 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
276 const char *name
, struct strbuf
*err
)
279 atom
->option
= R_NORMAL
;
280 else if (!strcmp(arg
, "short"))
281 atom
->option
= R_SHORT
;
282 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
283 skip_prefix(arg
, "strip=", &arg
)) {
284 atom
->option
= R_LSTRIP
;
285 if (strtol_i(arg
, 10, &atom
->lstrip
))
286 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
287 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
288 atom
->option
= R_RSTRIP
;
289 if (strtol_i(arg
, 10, &atom
->rstrip
))
290 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
292 return err_bad_arg(err
, name
, arg
);
296 static int remote_ref_atom_parser(struct ref_format
*format UNUSED
,
297 struct used_atom
*atom
,
298 const char *arg
, struct strbuf
*err
)
300 struct string_list params
= STRING_LIST_INIT_DUP
;
303 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
304 atom
->u
.remote_ref
.push
= 1;
307 atom
->u
.remote_ref
.option
= RR_REF
;
308 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
309 arg
, atom
->name
, err
);
312 atom
->u
.remote_ref
.nobracket
= 0;
313 string_list_split(¶ms
, arg
, ',', -1);
315 for (i
= 0; i
< params
.nr
; i
++) {
316 const char *s
= params
.items
[i
].string
;
318 if (!strcmp(s
, "track"))
319 atom
->u
.remote_ref
.option
= RR_TRACK
;
320 else if (!strcmp(s
, "trackshort"))
321 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
322 else if (!strcmp(s
, "nobracket"))
323 atom
->u
.remote_ref
.nobracket
= 1;
324 else if (!strcmp(s
, "remotename")) {
325 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
326 atom
->u
.remote_ref
.push_remote
= 1;
327 } else if (!strcmp(s
, "remoteref")) {
328 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
329 atom
->u
.remote_ref
.push_remote
= 1;
331 atom
->u
.remote_ref
.option
= RR_REF
;
332 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
333 arg
, atom
->name
, err
)) {
334 string_list_clear(¶ms
, 0);
340 string_list_clear(¶ms
, 0);
344 static int objecttype_atom_parser(struct ref_format
*format UNUSED
,
345 struct used_atom
*atom
,
346 const char *arg
, struct strbuf
*err
)
349 return err_no_arg(err
, "objecttype");
350 if (*atom
->name
== '*')
351 oi_deref
.info
.typep
= &oi_deref
.type
;
353 oi
.info
.typep
= &oi
.type
;
357 static int objectsize_atom_parser(struct ref_format
*format UNUSED
,
358 struct used_atom
*atom
,
359 const char *arg
, struct strbuf
*err
)
362 atom
->u
.objectsize
.option
= O_SIZE
;
363 if (*atom
->name
== '*')
364 oi_deref
.info
.sizep
= &oi_deref
.size
;
366 oi
.info
.sizep
= &oi
.size
;
367 } else if (!strcmp(arg
, "disk")) {
368 atom
->u
.objectsize
.option
= O_SIZE_DISK
;
369 if (*atom
->name
== '*')
370 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
372 oi
.info
.disk_sizep
= &oi
.disk_size
;
374 return err_bad_arg(err
, "objectsize", arg
);
378 static int deltabase_atom_parser(struct ref_format
*format UNUSED
,
379 struct used_atom
*atom
,
380 const char *arg
, struct strbuf
*err
)
383 return err_no_arg(err
, "deltabase");
384 if (*atom
->name
== '*')
385 oi_deref
.info
.delta_base_oid
= &oi_deref
.delta_base_oid
;
387 oi
.info
.delta_base_oid
= &oi
.delta_base_oid
;
391 static int body_atom_parser(struct ref_format
*format UNUSED
,
392 struct used_atom
*atom
,
393 const char *arg
, struct strbuf
*err
)
396 return err_no_arg(err
, "body");
397 atom
->u
.contents
.option
= C_BODY_DEP
;
401 static int subject_atom_parser(struct ref_format
*format UNUSED
,
402 struct used_atom
*atom
,
403 const char *arg
, struct strbuf
*err
)
406 atom
->u
.contents
.option
= C_SUB
;
407 else if (!strcmp(arg
, "sanitize"))
408 atom
->u
.contents
.option
= C_SUB_SANITIZE
;
410 return err_bad_arg(err
, "subject", arg
);
414 static int parse_signature_option(const char *arg
)
418 else if (!strcmp(arg
, "signer"))
420 else if (!strcmp(arg
, "grade"))
422 else if (!strcmp(arg
, "key"))
424 else if (!strcmp(arg
, "fingerprint"))
425 return S_FINGERPRINT
;
426 else if (!strcmp(arg
, "primarykeyfingerprint"))
428 else if (!strcmp(arg
, "trustlevel"))
429 return S_TRUST_LEVEL
;
433 static int signature_atom_parser(struct ref_format
*format UNUSED
,
434 struct used_atom
*atom
,
435 const char *arg
, struct strbuf
*err
)
437 int opt
= parse_signature_option(arg
);
439 return err_bad_arg(err
, "signature", arg
);
440 atom
->u
.signature
.option
= opt
;
444 static int trailers_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
445 const char *arg
, struct strbuf
*err
)
447 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
450 const char *argbuf
= xstrfmt("%s)", arg
);
451 char *invalid_arg
= NULL
;
453 if (format_set_trailers_options(&atom
->u
.contents
.trailer_opts
,
454 &ref_trailer_buf
.filter_list
,
455 &ref_trailer_buf
.sepbuf
,
456 &ref_trailer_buf
.kvsepbuf
,
457 &argbuf
, &invalid_arg
)) {
459 strbuf_addf(err
, _("expected %%(trailers:key=<value>)"));
461 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), invalid_arg
);
462 free((char *)invalid_arg
);
466 atom
->u
.contents
.option
= C_TRAILERS
;
470 static int contents_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
471 const char *arg
, struct strbuf
*err
)
474 atom
->u
.contents
.option
= C_BARE
;
475 else if (!strcmp(arg
, "body"))
476 atom
->u
.contents
.option
= C_BODY
;
477 else if (!strcmp(arg
, "size"))
478 atom
->u
.contents
.option
= C_LENGTH
;
479 else if (!strcmp(arg
, "signature"))
480 atom
->u
.contents
.option
= C_SIG
;
481 else if (!strcmp(arg
, "subject"))
482 atom
->u
.contents
.option
= C_SUB
;
483 else if (!strcmp(arg
, "trailers")) {
484 if (trailers_atom_parser(format
, atom
, NULL
, err
))
486 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
487 if (trailers_atom_parser(format
, atom
, arg
, err
))
489 } else if (skip_prefix(arg
, "lines=", &arg
)) {
490 atom
->u
.contents
.option
= C_LINES
;
491 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
492 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
494 return err_bad_arg(err
, "contents", arg
);
498 static int raw_atom_parser(struct ref_format
*format UNUSED
,
499 struct used_atom
*atom
,
500 const char *arg
, struct strbuf
*err
)
503 atom
->u
.raw_data
.option
= RAW_BARE
;
504 else if (!strcmp(arg
, "size"))
505 atom
->u
.raw_data
.option
= RAW_LENGTH
;
507 return err_bad_arg(err
, "raw", arg
);
511 static int oid_atom_parser(struct ref_format
*format UNUSED
,
512 struct used_atom
*atom
,
513 const char *arg
, struct strbuf
*err
)
516 atom
->u
.oid
.option
= O_FULL
;
517 else if (!strcmp(arg
, "short"))
518 atom
->u
.oid
.option
= O_SHORT
;
519 else if (skip_prefix(arg
, "short=", &arg
)) {
520 atom
->u
.oid
.option
= O_LENGTH
;
521 if (strtoul_ui(arg
, 10, &atom
->u
.oid
.length
) ||
522 atom
->u
.oid
.length
== 0)
523 return strbuf_addf_ret(err
, -1, _("positive value expected '%s' in %%(%s)"), arg
, atom
->name
);
524 if (atom
->u
.oid
.length
< MINIMUM_ABBREV
)
525 atom
->u
.oid
.length
= MINIMUM_ABBREV
;
527 return err_bad_arg(err
, atom
->name
, arg
);
531 static int person_email_atom_parser(struct ref_format
*format UNUSED
,
532 struct used_atom
*atom
,
533 const char *arg
, struct strbuf
*err
)
536 atom
->u
.email_option
.option
= EO_RAW
;
537 else if (!strcmp(arg
, "trim"))
538 atom
->u
.email_option
.option
= EO_TRIM
;
539 else if (!strcmp(arg
, "localpart"))
540 atom
->u
.email_option
.option
= EO_LOCALPART
;
542 return err_bad_arg(err
, atom
->name
, arg
);
546 static int refname_atom_parser(struct ref_format
*format UNUSED
,
547 struct used_atom
*atom
,
548 const char *arg
, struct strbuf
*err
)
550 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
553 static align_type
parse_align_position(const char *s
)
555 if (!strcmp(s
, "right"))
557 else if (!strcmp(s
, "middle"))
559 else if (!strcmp(s
, "left"))
564 static int align_atom_parser(struct ref_format
*format UNUSED
,
565 struct used_atom
*atom
,
566 const char *arg
, struct strbuf
*err
)
568 struct align
*align
= &atom
->u
.align
;
569 struct string_list params
= STRING_LIST_INIT_DUP
;
571 unsigned int width
= ~0U;
574 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
576 align
->position
= ALIGN_LEFT
;
578 string_list_split(¶ms
, arg
, ',', -1);
579 for (i
= 0; i
< params
.nr
; i
++) {
580 const char *s
= params
.items
[i
].string
;
583 if (skip_prefix(s
, "position=", &s
)) {
584 position
= parse_align_position(s
);
586 strbuf_addf(err
, _("unrecognized position:%s"), s
);
587 string_list_clear(¶ms
, 0);
590 align
->position
= position
;
591 } else if (skip_prefix(s
, "width=", &s
)) {
592 if (strtoul_ui(s
, 10, &width
)) {
593 strbuf_addf(err
, _("unrecognized width:%s"), s
);
594 string_list_clear(¶ms
, 0);
597 } else if (!strtoul_ui(s
, 10, &width
))
599 else if ((position
= parse_align_position(s
)) >= 0)
600 align
->position
= position
;
602 strbuf_addf(err
, _("unrecognized %%(%s) argument: %s"), "align", s
);
603 string_list_clear(¶ms
, 0);
609 string_list_clear(¶ms
, 0);
610 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
612 align
->width
= width
;
613 string_list_clear(¶ms
, 0);
617 static int if_atom_parser(struct ref_format
*format UNUSED
,
618 struct used_atom
*atom
,
619 const char *arg
, struct strbuf
*err
)
622 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
624 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
625 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
626 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
627 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
629 return err_bad_arg(err
, "if", arg
);
633 static int rest_atom_parser(struct ref_format
*format
,
634 struct used_atom
*atom UNUSED
,
635 const char *arg
, struct strbuf
*err
)
638 return err_no_arg(err
, "rest");
642 static int ahead_behind_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
643 const char *arg
, struct strbuf
*err
)
645 struct string_list_item
*item
;
648 return strbuf_addf_ret(err
, -1, _("expected format: %%(ahead-behind:<committish>)"));
650 item
= string_list_append(&format
->bases
, arg
);
651 item
->util
= lookup_commit_reference_by_name(arg
);
653 die("failed to find '%s'", arg
);
658 static int head_atom_parser(struct ref_format
*format UNUSED
,
659 struct used_atom
*atom
,
660 const char *arg
, struct strbuf
*err
)
663 return err_no_arg(err
, "HEAD");
664 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
672 int (*parser
)(struct ref_format
*format
, struct used_atom
*atom
,
673 const char *arg
, struct strbuf
*err
);
675 [ATOM_REFNAME
] = { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
676 [ATOM_OBJECTTYPE
] = { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
677 [ATOM_OBJECTSIZE
] = { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
678 [ATOM_OBJECTNAME
] = { "objectname", SOURCE_OTHER
, FIELD_STR
, oid_atom_parser
},
679 [ATOM_DELTABASE
] = { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
680 [ATOM_TREE
] = { "tree", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
681 [ATOM_PARENT
] = { "parent", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
682 [ATOM_NUMPARENT
] = { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
683 [ATOM_OBJECT
] = { "object", SOURCE_OBJ
},
684 [ATOM_TYPE
] = { "type", SOURCE_OBJ
},
685 [ATOM_TAG
] = { "tag", SOURCE_OBJ
},
686 [ATOM_AUTHOR
] = { "author", SOURCE_OBJ
},
687 [ATOM_AUTHORNAME
] = { "authorname", SOURCE_OBJ
},
688 [ATOM_AUTHOREMAIL
] = { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
689 [ATOM_AUTHORDATE
] = { "authordate", SOURCE_OBJ
, FIELD_TIME
},
690 [ATOM_COMMITTER
] = { "committer", SOURCE_OBJ
},
691 [ATOM_COMMITTERNAME
] = { "committername", SOURCE_OBJ
},
692 [ATOM_COMMITTEREMAIL
] = { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
693 [ATOM_COMMITTERDATE
] = { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
694 [ATOM_TAGGER
] = { "tagger", SOURCE_OBJ
},
695 [ATOM_TAGGERNAME
] = { "taggername", SOURCE_OBJ
},
696 [ATOM_TAGGEREMAIL
] = { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
697 [ATOM_TAGGERDATE
] = { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
698 [ATOM_CREATOR
] = { "creator", SOURCE_OBJ
},
699 [ATOM_CREATORDATE
] = { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
700 [ATOM_SUBJECT
] = { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
701 [ATOM_BODY
] = { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
702 [ATOM_TRAILERS
] = { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
703 [ATOM_CONTENTS
] = { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
704 [ATOM_SIGNATURE
] = { "signature", SOURCE_OBJ
, FIELD_STR
, signature_atom_parser
},
705 [ATOM_RAW
] = { "raw", SOURCE_OBJ
, FIELD_STR
, raw_atom_parser
},
706 [ATOM_UPSTREAM
] = { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
707 [ATOM_PUSH
] = { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
708 [ATOM_SYMREF
] = { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
709 [ATOM_FLAG
] = { "flag", SOURCE_NONE
},
710 [ATOM_HEAD
] = { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
711 [ATOM_COLOR
] = { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
712 [ATOM_WORKTREEPATH
] = { "worktreepath", SOURCE_NONE
},
713 [ATOM_ALIGN
] = { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
714 [ATOM_END
] = { "end", SOURCE_NONE
},
715 [ATOM_IF
] = { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
716 [ATOM_THEN
] = { "then", SOURCE_NONE
},
717 [ATOM_ELSE
] = { "else", SOURCE_NONE
},
718 [ATOM_REST
] = { "rest", SOURCE_NONE
, FIELD_STR
, rest_atom_parser
},
719 [ATOM_AHEADBEHIND
] = { "ahead-behind", SOURCE_OTHER
, FIELD_STR
, ahead_behind_atom_parser
},
721 * Please update $__git_ref_fieldlist in git-completion.bash
722 * when you add new atoms
726 #define REF_FORMATTING_STATE_INIT { 0 }
728 struct ref_formatting_stack
{
729 struct ref_formatting_stack
*prev
;
730 struct strbuf output
;
731 void (*at_end
)(struct ref_formatting_stack
**stack
);
735 struct ref_formatting_state
{
737 struct ref_formatting_stack
*stack
;
743 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
745 uintmax_t value
; /* used for sorting when not FIELD_STR */
746 struct used_atom
*atom
;
749 #define ATOM_SIZE_UNSPECIFIED (-1)
751 #define ATOM_VALUE_INIT { \
752 .s_size = ATOM_SIZE_UNSPECIFIED \
756 * Used to parse format string and sort specifiers
758 static int parse_ref_filter_atom(struct ref_format
*format
,
759 const char *atom
, const char *ep
,
767 if (*sp
== '*' && sp
< ep
)
770 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
771 (int)(ep
-atom
), atom
);
774 * If the atom name has a colon, strip it and everything after
775 * it off - it specifies the format for this entry, and
776 * shouldn't be used for checking against the valid_atom
779 arg
= memchr(sp
, ':', ep
- sp
);
780 atom_len
= (arg
? arg
: ep
) - sp
;
782 /* Do we have the atom already used elsewhere? */
783 for (i
= 0; i
< used_atom_cnt
; i
++) {
784 int len
= strlen(used_atom
[i
].name
);
785 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
789 /* Is the atom a valid one? */
790 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
791 int len
= strlen(valid_atom
[i
].name
);
792 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
796 if (ARRAY_SIZE(valid_atom
) <= i
)
797 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
798 (int)(ep
-atom
), atom
);
799 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
800 return strbuf_addf_ret(err
, -1,
801 _("not a git repository, but the field '%.*s' requires access to object data"),
802 (int)(ep
-atom
), atom
);
804 /* Add it in, including the deref prefix */
807 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
808 used_atom
[at
].atom_type
= i
;
809 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
810 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
811 used_atom
[at
].source
= valid_atom
[i
].source
;
812 if (used_atom
[at
].source
== SOURCE_OBJ
) {
814 oi_deref
.info
.contentp
= &oi_deref
.content
;
816 oi
.info
.contentp
= &oi
.content
;
819 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
822 * Treat empty sub-arguments list as NULL (i.e.,
823 * "%(atom:)" is equivalent to "%(atom)").
828 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
829 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
833 if (i
== ATOM_SYMREF
)
838 static void quote_formatting(struct strbuf
*s
, const char *str
, ssize_t len
, int quote_style
)
840 switch (quote_style
) {
843 strbuf_addstr(s
, str
);
845 strbuf_add(s
, str
, len
);
848 sq_quote_buf(s
, str
);
852 perl_quote_buf(s
, str
);
854 perl_quote_buf_with_len(s
, str
, len
);
857 python_quote_buf(s
, str
);
860 tcl_quote_buf(s
, str
);
865 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
866 struct strbuf
*err UNUSED
)
869 * Quote formatting is only done when the stack has a single
870 * element. Otherwise quote formatting is done on the
871 * element's entire output strbuf when the %(end) atom is
874 if (!state
->stack
->prev
)
875 quote_formatting(&state
->stack
->output
, v
->s
, v
->s_size
, state
->quote_style
);
876 else if (v
->s_size
< 0)
877 strbuf_addstr(&state
->stack
->output
, v
->s
);
879 strbuf_add(&state
->stack
->output
, v
->s
, v
->s_size
);
883 static void push_stack_element(struct ref_formatting_stack
**stack
)
885 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
887 strbuf_init(&s
->output
, 0);
892 static void pop_stack_element(struct ref_formatting_stack
**stack
)
894 struct ref_formatting_stack
*current
= *stack
;
895 struct ref_formatting_stack
*prev
= current
->prev
;
898 strbuf_addbuf(&prev
->output
, ¤t
->output
);
899 strbuf_release(¤t
->output
);
904 static void end_align_handler(struct ref_formatting_stack
**stack
)
906 struct ref_formatting_stack
*cur
= *stack
;
907 struct align
*align
= (struct align
*)cur
->at_end_data
;
908 struct strbuf s
= STRBUF_INIT
;
910 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
911 strbuf_swap(&cur
->output
, &s
);
915 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
916 struct strbuf
*err UNUSED
)
918 struct ref_formatting_stack
*new_stack
;
920 push_stack_element(&state
->stack
);
921 new_stack
= state
->stack
;
922 new_stack
->at_end
= end_align_handler
;
923 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
927 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
929 struct ref_formatting_stack
*cur
= *stack
;
930 struct ref_formatting_stack
*prev
= cur
->prev
;
931 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
933 if (!if_then_else
->then_atom_seen
)
934 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
936 if (if_then_else
->else_atom_seen
) {
938 * There is an %(else) atom: we need to drop one state from the
939 * stack, either the %(else) branch if the condition is satisfied, or
940 * the %(then) branch if it isn't.
942 if (if_then_else
->condition_satisfied
) {
943 strbuf_reset(&cur
->output
);
944 pop_stack_element(&cur
);
946 strbuf_swap(&cur
->output
, &prev
->output
);
947 strbuf_reset(&cur
->output
);
948 pop_stack_element(&cur
);
950 } else if (!if_then_else
->condition_satisfied
) {
952 * No %(else) atom: just drop the %(then) branch if the
953 * condition is not satisfied.
955 strbuf_reset(&cur
->output
);
962 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
963 struct strbuf
*err UNUSED
)
965 struct ref_formatting_stack
*new_stack
;
966 struct if_then_else
*if_then_else
= xcalloc(1,
967 sizeof(struct if_then_else
));
969 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
970 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
972 push_stack_element(&state
->stack
);
973 new_stack
= state
->stack
;
974 new_stack
->at_end
= if_then_else_handler
;
975 new_stack
->at_end_data
= if_then_else
;
979 static int is_empty(struct strbuf
*buf
)
981 const char *cur
= buf
->buf
;
982 const char *end
= buf
->buf
+ buf
->len
;
984 while (cur
!= end
&& (isspace(*cur
)))
990 static int then_atom_handler(struct atom_value
*atomv UNUSED
,
991 struct ref_formatting_state
*state
,
994 struct ref_formatting_stack
*cur
= state
->stack
;
995 struct if_then_else
*if_then_else
= NULL
;
998 if (cur
->at_end
== if_then_else_handler
)
999 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
1001 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
1002 if (if_then_else
->then_atom_seen
)
1003 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
1004 if (if_then_else
->else_atom_seen
)
1005 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
1006 if_then_else
->then_atom_seen
= 1;
1007 if (if_then_else
->str
)
1008 str_len
= strlen(if_then_else
->str
);
1010 * If the 'equals' or 'notequals' attribute is used then
1011 * perform the required comparison. If not, only non-empty
1012 * strings satisfy the 'if' condition.
1014 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
1015 if (str_len
== cur
->output
.len
&&
1016 !memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1017 if_then_else
->condition_satisfied
= 1;
1018 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
1019 if (str_len
!= cur
->output
.len
||
1020 memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
1021 if_then_else
->condition_satisfied
= 1;
1022 } else if (cur
->output
.len
&& !is_empty(&cur
->output
))
1023 if_then_else
->condition_satisfied
= 1;
1024 strbuf_reset(&cur
->output
);
1028 static int else_atom_handler(struct atom_value
*atomv UNUSED
,
1029 struct ref_formatting_state
*state
,
1032 struct ref_formatting_stack
*prev
= state
->stack
;
1033 struct if_then_else
*if_then_else
= NULL
;
1035 if (prev
->at_end
== if_then_else_handler
)
1036 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
1038 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
1039 if (!if_then_else
->then_atom_seen
)
1040 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
1041 if (if_then_else
->else_atom_seen
)
1042 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
1043 if_then_else
->else_atom_seen
= 1;
1044 push_stack_element(&state
->stack
);
1045 state
->stack
->at_end_data
= prev
->at_end_data
;
1046 state
->stack
->at_end
= prev
->at_end
;
1050 static int end_atom_handler(struct atom_value
*atomv UNUSED
,
1051 struct ref_formatting_state
*state
,
1054 struct ref_formatting_stack
*current
= state
->stack
;
1055 struct strbuf s
= STRBUF_INIT
;
1057 if (!current
->at_end
)
1058 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
1059 current
->at_end(&state
->stack
);
1061 /* Stack may have been popped within at_end(), hence reset the current pointer */
1062 current
= state
->stack
;
1065 * Perform quote formatting when the stack element is that of
1066 * a supporting atom. If nested then perform quote formatting
1067 * only on the topmost supporting atom.
1069 if (!current
->prev
->prev
) {
1070 quote_formatting(&s
, current
->output
.buf
, current
->output
.len
, state
->quote_style
);
1071 strbuf_swap(¤t
->output
, &s
);
1074 pop_stack_element(&state
->stack
);
1079 * In a format string, find the next occurrence of %(atom).
1081 static const char *find_next(const char *cp
)
1086 * %( is the start of an atom;
1087 * %% is a quoted per-cent.
1091 else if (cp
[1] == '%')
1092 cp
++; /* skip over two % */
1093 /* otherwise this is a singleton, literal % */
1100 static int reject_atom(enum atom_type atom_type
)
1102 return atom_type
== ATOM_REST
;
1106 * Make sure the format string is well formed, and parse out
1109 int verify_ref_format(struct ref_format
*format
)
1111 const char *cp
, *sp
;
1113 format
->need_color_reset_at_eol
= 0;
1114 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
1115 struct strbuf err
= STRBUF_INIT
;
1116 const char *color
, *ep
= strchr(sp
, ')');
1120 return error(_("malformed format string %s"), sp
);
1121 /* sp points at "%(" and ep points at the closing ")" */
1122 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
1125 if (reject_atom(used_atom
[at
].atom_type
))
1126 die(_("this command reject atom %%(%.*s)"), (int)(ep
- sp
- 2), sp
+ 2);
1128 if ((format
->quote_style
== QUOTE_PYTHON
||
1129 format
->quote_style
== QUOTE_SHELL
||
1130 format
->quote_style
== QUOTE_TCL
) &&
1131 used_atom
[at
].atom_type
== ATOM_RAW
&&
1132 used_atom
[at
].u
.raw_data
.option
== RAW_BARE
)
1133 die(_("--format=%.*s cannot be used with "
1134 "--python, --shell, --tcl"), (int)(ep
- sp
- 2), sp
+ 2);
1137 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
1138 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
1139 strbuf_release(&err
);
1141 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
1142 format
->need_color_reset_at_eol
= 0;
1146 static const char *do_grab_oid(const char *field
, const struct object_id
*oid
,
1147 struct used_atom
*atom
)
1149 switch (atom
->u
.oid
.option
) {
1151 return oid_to_hex(oid
);
1153 return repo_find_unique_abbrev(the_repository
, oid
,
1154 atom
->u
.oid
.length
);
1156 return repo_find_unique_abbrev(the_repository
, oid
,
1159 BUG("unknown %%(%s) option", field
);
1163 static int grab_oid(const char *name
, const char *field
, const struct object_id
*oid
,
1164 struct atom_value
*v
, struct used_atom
*atom
)
1166 if (starts_with(name
, field
)) {
1167 v
->s
= xstrdup(do_grab_oid(field
, oid
, atom
));
1173 /* See grab_values */
1174 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
1178 for (i
= 0; i
< used_atom_cnt
; i
++) {
1179 const char *name
= used_atom
[i
].name
;
1180 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1181 struct atom_value
*v
= &val
[i
];
1182 if (!!deref
!= (*name
== '*'))
1186 if (atom_type
== ATOM_OBJECTTYPE
)
1187 v
->s
= xstrdup(type_name(oi
->type
));
1188 else if (atom_type
== ATOM_OBJECTSIZE
) {
1189 if (used_atom
[i
].u
.objectsize
.option
== O_SIZE_DISK
) {
1190 v
->value
= oi
->disk_size
;
1191 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
1192 } else if (used_atom
[i
].u
.objectsize
.option
== O_SIZE
) {
1193 v
->value
= oi
->size
;
1194 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
1196 } else if (atom_type
== ATOM_DELTABASE
)
1197 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
1198 else if (atom_type
== ATOM_OBJECTNAME
&& deref
)
1199 grab_oid(name
, "objectname", &oi
->oid
, v
, &used_atom
[i
]);
1203 /* See grab_values */
1204 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1207 struct tag
*tag
= (struct tag
*) obj
;
1209 for (i
= 0; i
< used_atom_cnt
; i
++) {
1210 const char *name
= used_atom
[i
].name
;
1211 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1212 struct atom_value
*v
= &val
[i
];
1213 if (!!deref
!= (*name
== '*'))
1217 if (atom_type
== ATOM_TAG
)
1218 v
->s
= xstrdup(tag
->tag
);
1219 else if (atom_type
== ATOM_TYPE
&& tag
->tagged
)
1220 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
1221 else if (atom_type
== ATOM_OBJECT
&& tag
->tagged
)
1222 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
1226 /* See grab_values */
1227 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1230 struct commit
*commit
= (struct commit
*) obj
;
1232 for (i
= 0; i
< used_atom_cnt
; i
++) {
1233 const char *name
= used_atom
[i
].name
;
1234 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1235 struct atom_value
*v
= &val
[i
];
1236 if (!!deref
!= (*name
== '*'))
1240 if (atom_type
== ATOM_TREE
&&
1241 grab_oid(name
, "tree", get_commit_tree_oid(commit
), v
, &used_atom
[i
]))
1243 if (atom_type
== ATOM_NUMPARENT
) {
1244 v
->value
= commit_list_count(commit
->parents
);
1245 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1247 else if (atom_type
== ATOM_PARENT
) {
1248 struct commit_list
*parents
;
1249 struct strbuf s
= STRBUF_INIT
;
1250 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1251 struct object_id
*oid
= &parents
->item
->object
.oid
;
1252 if (parents
!= commit
->parents
)
1253 strbuf_addch(&s
, ' ');
1254 strbuf_addstr(&s
, do_grab_oid("parent", oid
, &used_atom
[i
]));
1256 v
->s
= strbuf_detach(&s
, NULL
);
1261 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1265 if (!strncmp(buf
, who
, wholen
) &&
1267 return buf
+ wholen
+ 1;
1268 eol
= strchr(buf
, '\n');
1273 return ""; /* end of header */
1279 static const char *copy_line(const char *buf
)
1281 const char *eol
= strchrnul(buf
, '\n');
1282 return xmemdupz(buf
, eol
- buf
);
1285 static const char *copy_name(const char *buf
)
1288 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1289 if (starts_with(cp
, " <"))
1290 return xmemdupz(buf
, cp
- buf
);
1295 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1297 const char *email
= strchr(buf
, '<');
1298 const char *eoemail
;
1301 switch (atom
->u
.email_option
.option
) {
1303 eoemail
= strchr(email
, '>');
1309 eoemail
= strchr(email
, '>');
1313 eoemail
= strchr(email
, '@');
1315 eoemail
= strchr(email
, '>');
1318 BUG("unknown email option");
1323 return xmemdupz(email
, eoemail
- email
);
1326 static char *copy_subject(const char *buf
, unsigned long len
)
1328 struct strbuf sb
= STRBUF_INIT
;
1331 for (i
= 0; i
< len
; i
++) {
1332 if (buf
[i
] == '\r' && i
+ 1 < len
&& buf
[i
+ 1] == '\n')
1333 continue; /* ignore CR in CRLF */
1336 strbuf_addch(&sb
, ' ');
1338 strbuf_addch(&sb
, buf
[i
]);
1340 return strbuf_detach(&sb
, NULL
);
1343 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1345 const char *eoemail
= strstr(buf
, "> ");
1347 timestamp_t timestamp
;
1349 struct date_mode date_mode
= DATE_MODE_INIT
;
1350 const char *formatp
;
1353 * We got here because atomname ends in "date" or "date<something>";
1354 * it's not possible that <something> is not ":<format>" because
1355 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1356 * ":" means no format is specified, and use the default.
1358 formatp
= strchr(atomname
, ':');
1361 parse_date_format(formatp
, &date_mode
);
1366 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1367 if (timestamp
== TIME_MAX
)
1369 tz
= strtol(zone
, NULL
, 10);
1370 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1372 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1373 v
->value
= timestamp
;
1374 date_mode_release(&date_mode
);
1381 /* See grab_values */
1382 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1385 int wholen
= strlen(who
);
1386 const char *wholine
= NULL
;
1388 for (i
= 0; i
< used_atom_cnt
; i
++) {
1389 const char *name
= used_atom
[i
].name
;
1390 struct atom_value
*v
= &val
[i
];
1391 if (!!deref
!= (*name
== '*'))
1395 if (strncmp(who
, name
, wholen
))
1397 if (name
[wholen
] != 0 &&
1398 strcmp(name
+ wholen
, "name") &&
1399 !starts_with(name
+ wholen
, "email") &&
1400 !starts_with(name
+ wholen
, "date"))
1403 wholine
= find_wholine(who
, wholen
, buf
);
1405 return; /* no point looking for it */
1406 if (name
[wholen
] == 0)
1407 v
->s
= copy_line(wholine
);
1408 else if (!strcmp(name
+ wholen
, "name"))
1409 v
->s
= copy_name(wholine
);
1410 else if (starts_with(name
+ wholen
, "email"))
1411 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1412 else if (starts_with(name
+ wholen
, "date"))
1413 grab_date(wholine
, v
, name
);
1417 * For a tag or a commit object, if "creator" or "creatordate" is
1418 * requested, do something special.
1420 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1421 return; /* "author" for commit object is not wanted */
1423 wholine
= find_wholine(who
, wholen
, buf
);
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
== '*'))
1435 if (atom_type
== ATOM_CREATORDATE
)
1436 grab_date(wholine
, v
, name
);
1437 else if (atom_type
== ATOM_CREATOR
)
1438 v
->s
= copy_line(wholine
);
1442 static void grab_signature(struct atom_value
*val
, int deref
, struct object
*obj
)
1445 struct commit
*commit
= (struct commit
*) obj
;
1446 struct signature_check sigc
= { 0 };
1447 int signature_checked
= 0;
1449 for (i
= 0; i
< used_atom_cnt
; i
++) {
1450 struct used_atom
*atom
= &used_atom
[i
];
1451 const char *name
= atom
->name
;
1452 struct atom_value
*v
= &val
[i
];
1455 if (!!deref
!= (*name
== '*'))
1460 if (!skip_prefix(name
, "signature", &name
) ||
1461 (*name
&& *name
!= ':'))
1468 opt
= parse_signature_option(name
);
1472 if (!signature_checked
) {
1473 check_commit_signature(commit
, &sigc
);
1474 signature_checked
= 1;
1479 v
->s
= xstrdup(sigc
.output
? sigc
.output
: "");
1482 v
->s
= xstrdup(sigc
.signer
? sigc
.signer
: "");
1485 switch (sigc
.result
) {
1487 switch (sigc
.trust_level
) {
1488 case TRUST_UNDEFINED
:
1490 v
->s
= xstrfmt("%c", (char)'U');
1493 v
->s
= xstrfmt("%c", (char)'G');
1503 v
->s
= xstrfmt("%c", (char)sigc
.result
);
1508 v
->s
= xstrdup(sigc
.key
? sigc
.key
: "");
1511 v
->s
= xstrdup(sigc
.fingerprint
?
1512 sigc
.fingerprint
: "");
1515 v
->s
= xstrdup(sigc
.primary_key_fingerprint
?
1516 sigc
.primary_key_fingerprint
: "");
1519 v
->s
= xstrdup(gpg_trust_level_to_str(sigc
.trust_level
));
1524 if (signature_checked
)
1525 signature_check_clear(&sigc
);
1528 static void find_subpos(const char *buf
,
1529 const char **sub
, size_t *sublen
,
1530 const char **body
, size_t *bodylen
,
1532 const char **sig
, size_t *siglen
)
1534 struct strbuf payload
= STRBUF_INIT
;
1535 struct strbuf signature
= STRBUF_INIT
;
1537 const char *end
= buf
+ strlen(buf
);
1538 const char *sigstart
;
1540 /* parse signature first; we might not even have a subject line */
1541 parse_signature(buf
, end
- buf
, &payload
, &signature
);
1542 strbuf_release(&payload
);
1544 /* skip past header until we hit empty line */
1545 while (*buf
&& *buf
!= '\n') {
1546 eol
= strchrnul(buf
, '\n');
1551 /* skip any empty lines */
1552 while (*buf
== '\n')
1554 *sig
= strbuf_detach(&signature
, siglen
);
1555 sigstart
= buf
+ parse_signed_buffer(buf
, strlen(buf
));
1557 /* subject is first non-empty line */
1559 /* subject goes to first empty line before signature begins */
1560 if ((eol
= strstr(*sub
, "\n\n")) ||
1561 (eol
= strstr(*sub
, "\r\n\r\n"))) {
1562 eol
= eol
< sigstart
? eol
: sigstart
;
1564 /* treat whole message as subject */
1568 *sublen
= buf
- *sub
;
1569 /* drop trailing newline, if present */
1570 while (*sublen
&& ((*sub
)[*sublen
- 1] == '\n' ||
1571 (*sub
)[*sublen
- 1] == '\r'))
1574 /* skip any empty lines */
1575 while (*buf
== '\n' || *buf
== '\r')
1578 *bodylen
= strlen(buf
);
1579 *nonsiglen
= sigstart
- buf
;
1583 * If 'lines' is greater than 0, append that many lines from the given
1584 * 'buf' of length 'size' to the given strbuf.
1586 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1589 const char *sp
, *eol
;
1594 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1596 strbuf_addstr(out
, "\n ");
1597 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1598 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1599 strbuf_add(out
, sp
, len
);
1606 /* See grab_values */
1607 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct expand_data
*data
)
1610 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1611 size_t sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1612 void *buf
= data
->content
;
1614 for (i
= 0; i
< used_atom_cnt
; i
++) {
1615 struct used_atom
*atom
= &used_atom
[i
];
1616 const char *name
= atom
->name
;
1617 struct atom_value
*v
= &val
[i
];
1618 enum atom_type atom_type
= atom
->atom_type
;
1620 if (!!deref
!= (*name
== '*'))
1625 if (atom_type
== ATOM_RAW
) {
1626 unsigned long buf_size
= data
->size
;
1628 if (atom
->u
.raw_data
.option
== RAW_BARE
) {
1629 v
->s
= xmemdupz(buf
, buf_size
);
1630 v
->s_size
= buf_size
;
1631 } else if (atom
->u
.raw_data
.option
== RAW_LENGTH
) {
1632 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)buf_size
);
1637 if ((data
->type
!= OBJ_TAG
&&
1638 data
->type
!= OBJ_COMMIT
) ||
1639 (strcmp(name
, "body") &&
1640 !starts_with(name
, "subject") &&
1641 !starts_with(name
, "trailers") &&
1642 !starts_with(name
, "contents")))
1647 &bodypos
, &bodylen
, &nonsiglen
,
1650 if (atom
->u
.contents
.option
== C_SUB
)
1651 v
->s
= copy_subject(subpos
, sublen
);
1652 else if (atom
->u
.contents
.option
== C_SUB_SANITIZE
) {
1653 struct strbuf sb
= STRBUF_INIT
;
1654 format_sanitized_subject(&sb
, subpos
, sublen
);
1655 v
->s
= strbuf_detach(&sb
, NULL
);
1656 } else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1657 v
->s
= xmemdupz(bodypos
, bodylen
);
1658 else if (atom
->u
.contents
.option
== C_LENGTH
)
1659 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)strlen(subpos
));
1660 else if (atom
->u
.contents
.option
== C_BODY
)
1661 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1662 else if (atom
->u
.contents
.option
== C_SIG
)
1663 v
->s
= xmemdupz(sigpos
, siglen
);
1664 else if (atom
->u
.contents
.option
== C_LINES
) {
1665 struct strbuf s
= STRBUF_INIT
;
1666 const char *contents_end
= bodypos
+ nonsiglen
;
1668 /* Size is the length of the message after removing the signature */
1669 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1670 v
->s
= strbuf_detach(&s
, NULL
);
1671 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1672 struct strbuf s
= STRBUF_INIT
;
1674 /* Format the trailer info according to the trailer_opts given */
1675 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1677 v
->s
= strbuf_detach(&s
, NULL
);
1678 } else if (atom
->u
.contents
.option
== C_BARE
)
1679 v
->s
= xstrdup(subpos
);
1682 free((void *)sigpos
);
1686 * We want to have empty print-string for field requests
1687 * that do not apply (e.g. "authordate" for a tag object)
1689 static void fill_missing_values(struct atom_value
*val
)
1692 for (i
= 0; i
< used_atom_cnt
; i
++) {
1693 struct atom_value
*v
= &val
[i
];
1700 * val is a list of atom_value to hold returned values. Extract
1701 * the values for atoms in used_atom array out of (obj, buf, sz).
1702 * when deref is false, (obj, buf, sz) is the object that is
1703 * pointed at by the ref itself; otherwise it is the object the
1704 * ref (which is a tag) refers to.
1706 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, struct expand_data
*data
)
1708 void *buf
= data
->content
;
1710 switch (obj
->type
) {
1712 grab_tag_values(val
, deref
, obj
);
1713 grab_sub_body_contents(val
, deref
, data
);
1714 grab_person("tagger", val
, deref
, buf
);
1717 grab_commit_values(val
, deref
, obj
);
1718 grab_sub_body_contents(val
, deref
, data
);
1719 grab_person("author", val
, deref
, buf
);
1720 grab_person("committer", val
, deref
, buf
);
1721 grab_signature(val
, deref
, obj
);
1724 /* grab_tree_values(val, deref, obj, buf, sz); */
1725 grab_sub_body_contents(val
, deref
, data
);
1728 /* grab_blob_values(val, deref, obj, buf, sz); */
1729 grab_sub_body_contents(val
, deref
, data
);
1732 die("Eh? Object of type %d?", obj
->type
);
1736 static inline char *copy_advance(char *dst
, const char *src
)
1743 static const char *lstrip_ref_components(const char *refname
, int len
)
1745 long remaining
= len
;
1746 const char *start
= xstrdup(refname
);
1747 const char *to_free
= start
;
1751 const char *p
= refname
;
1753 /* Find total no of '/' separated path-components */
1754 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1757 * The number of components we need to strip is now
1758 * the total minus the components to be left (Plus one
1759 * because we count the number of '/', but the number
1760 * of components is one more than the no of '/').
1762 remaining
= i
+ len
+ 1;
1765 while (remaining
> 0) {
1768 free((char *)to_free
);
1776 start
= xstrdup(start
);
1777 free((char *)to_free
);
1781 static const char *rstrip_ref_components(const char *refname
, int len
)
1783 long remaining
= len
;
1784 const char *start
= xstrdup(refname
);
1785 const char *to_free
= start
;
1789 const char *p
= refname
;
1791 /* Find total no of '/' separated path-components */
1792 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1795 * The number of components we need to strip is now
1796 * the total minus the components to be left (Plus one
1797 * because we count the number of '/', but the number
1798 * of components is one more than the no of '/').
1800 remaining
= i
+ len
+ 1;
1803 while (remaining
-- > 0) {
1804 char *p
= strrchr(start
, '/');
1806 free((char *)to_free
);
1814 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1816 if (atom
->option
== R_SHORT
)
1817 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1818 else if (atom
->option
== R_LSTRIP
)
1819 return lstrip_ref_components(refname
, atom
->lstrip
);
1820 else if (atom
->option
== R_RSTRIP
)
1821 return rstrip_ref_components(refname
, atom
->rstrip
);
1823 return xstrdup(refname
);
1826 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1827 struct branch
*branch
, const char **s
)
1829 int num_ours
, num_theirs
;
1830 if (atom
->u
.remote_ref
.option
== RR_REF
)
1831 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1832 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1833 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1834 NULL
, atom
->u
.remote_ref
.push
,
1835 AHEAD_BEHIND_FULL
) < 0) {
1836 *s
= xstrdup(msgs
.gone
);
1837 } else if (!num_ours
&& !num_theirs
)
1840 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1841 else if (!num_theirs
)
1842 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1844 *s
= xstrfmt(msgs
.ahead_behind
,
1845 num_ours
, num_theirs
);
1846 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1847 const char *to_free
= *s
;
1848 *s
= xstrfmt("[%s]", *s
);
1849 free((void *)to_free
);
1851 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1852 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1853 NULL
, atom
->u
.remote_ref
.push
,
1854 AHEAD_BEHIND_FULL
) < 0) {
1858 if (!num_ours
&& !num_theirs
)
1862 else if (!num_theirs
)
1866 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1868 const char *remote
= atom
->u
.remote_ref
.push
?
1869 pushremote_for_branch(branch
, &explicit) :
1870 remote_for_branch(branch
, &explicit);
1871 *s
= xstrdup(explicit ? remote
: "");
1872 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1875 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
1876 *s
= xstrdup(merge
? merge
: "");
1878 BUG("unhandled RR_* enum");
1881 char *get_head_description(void)
1883 struct strbuf desc
= STRBUF_INIT
;
1884 struct wt_status_state state
;
1885 memset(&state
, 0, sizeof(state
));
1886 wt_status_get_state(the_repository
, &state
, 1);
1887 if (state
.rebase_in_progress
||
1888 state
.rebase_interactive_in_progress
) {
1890 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1893 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
1894 state
.detached_from
);
1895 } else if (state
.bisect_in_progress
)
1896 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1898 else if (state
.detached_from
) {
1899 if (state
.detached_at
)
1900 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1901 state
.detached_from
);
1903 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1904 state
.detached_from
);
1906 strbuf_addstr(&desc
, _("(no branch)"));
1908 wt_status_state_free_buffers(&state
);
1910 return strbuf_detach(&desc
, NULL
);
1913 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1918 return show_ref(&atom
->u
.refname
, ref
->symref
);
1921 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1923 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1924 return get_head_description();
1925 return show_ref(&atom
->u
.refname
, ref
->refname
);
1928 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
1929 struct expand_data
*oi
, struct strbuf
*err
)
1931 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1933 if (oi
->info
.contentp
) {
1934 /* We need to know that to use parse_object_buffer properly */
1935 oi
->info
.sizep
= &oi
->size
;
1936 oi
->info
.typep
= &oi
->type
;
1938 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
1939 OBJECT_INFO_LOOKUP_REPLACE
))
1940 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1941 oid_to_hex(&oi
->oid
), ref
->refname
);
1942 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
1943 BUG("Object size is less than zero.");
1945 if (oi
->info
.contentp
) {
1946 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
1950 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1951 oid_to_hex(&oi
->oid
), ref
->refname
);
1953 grab_values(ref
->value
, deref
, *obj
, oi
);
1956 grab_common_values(ref
->value
, deref
, oi
);
1962 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
1966 for (i
= 0; worktrees
[i
]; i
++) {
1967 if (worktrees
[i
]->head_ref
) {
1968 struct ref_to_worktree_entry
*entry
;
1969 entry
= xmalloc(sizeof(*entry
));
1970 entry
->wt
= worktrees
[i
];
1971 hashmap_entry_init(&entry
->ent
,
1972 strhash(worktrees
[i
]->head_ref
));
1974 hashmap_add(map
, &entry
->ent
);
1979 static void lazy_init_worktree_map(void)
1981 if (ref_to_worktree_map
.worktrees
)
1984 ref_to_worktree_map
.worktrees
= get_worktrees();
1985 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
1986 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
1989 static char *get_worktree_path(const struct ref_array_item
*ref
)
1991 struct hashmap_entry entry
, *e
;
1992 struct ref_to_worktree_entry
*lookup_result
;
1994 lazy_init_worktree_map();
1996 hashmap_entry_init(&entry
, strhash(ref
->refname
));
1997 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
2002 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
2004 return xstrdup(lookup_result
->wt
->path
);
2008 * Parse the object referred by ref, and grab needed value.
2010 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
2014 struct object_info empty
= OBJECT_INFO_INIT
;
2015 int ahead_behind_atoms
= 0;
2017 CALLOC_ARRAY(ref
->value
, used_atom_cnt
);
2019 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
2020 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
2023 ref
->symref
= xstrdup("");
2026 /* Fill in specials first */
2027 for (i
= 0; i
< used_atom_cnt
; i
++) {
2028 struct used_atom
*atom
= &used_atom
[i
];
2029 enum atom_type atom_type
= atom
->atom_type
;
2030 const char *name
= used_atom
[i
].name
;
2031 struct atom_value
*v
= &ref
->value
[i
];
2033 const char *refname
;
2034 struct branch
*branch
= NULL
;
2036 v
->s_size
= ATOM_SIZE_UNSPECIFIED
;
2037 v
->handler
= append_atom
;
2045 if (atom_type
== ATOM_REFNAME
)
2046 refname
= get_refname(atom
, ref
);
2047 else if (atom_type
== ATOM_WORKTREEPATH
) {
2048 if (ref
->kind
== FILTER_REFS_BRANCHES
)
2049 v
->s
= get_worktree_path(ref
);
2054 else if (atom_type
== ATOM_SYMREF
)
2055 refname
= get_symref(atom
, ref
);
2056 else if (atom_type
== ATOM_UPSTREAM
) {
2057 const char *branch_name
;
2058 /* only local branches may have an upstream */
2059 if (!skip_prefix(ref
->refname
, "refs/heads/",
2064 branch
= branch_get(branch_name
);
2066 refname
= branch_get_upstream(branch
, NULL
);
2068 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2072 } else if (atom_type
== ATOM_PUSH
&& atom
->u
.remote_ref
.push
) {
2073 const char *branch_name
;
2075 if (!skip_prefix(ref
->refname
, "refs/heads/",
2078 branch
= branch_get(branch_name
);
2080 if (atom
->u
.remote_ref
.push_remote
)
2083 refname
= branch_get_push(branch
, NULL
);
2087 /* We will definitely re-init v->s on the next line. */
2089 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
2091 } else if (atom_type
== ATOM_COLOR
) {
2092 v
->s
= xstrdup(atom
->u
.color
);
2094 } else if (atom_type
== ATOM_FLAG
) {
2095 char buf
[256], *cp
= buf
;
2096 if (ref
->flag
& REF_ISSYMREF
)
2097 cp
= copy_advance(cp
, ",symref");
2098 if (ref
->flag
& REF_ISPACKED
)
2099 cp
= copy_advance(cp
, ",packed");
2104 v
->s
= xstrdup(buf
+ 1);
2107 } else if (!deref
&& atom_type
== ATOM_OBJECTNAME
&&
2108 grab_oid(name
, "objectname", &ref
->objectname
, v
, atom
)) {
2110 } else if (atom_type
== ATOM_HEAD
) {
2111 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
2112 v
->s
= xstrdup("*");
2114 v
->s
= xstrdup(" ");
2116 } else if (atom_type
== ATOM_ALIGN
) {
2117 v
->handler
= align_atom_handler
;
2120 } else if (atom_type
== ATOM_END
) {
2121 v
->handler
= end_atom_handler
;
2124 } else if (atom_type
== ATOM_IF
) {
2126 if (skip_prefix(name
, "if:", &s
))
2130 v
->handler
= if_atom_handler
;
2132 } else if (atom_type
== ATOM_THEN
) {
2133 v
->handler
= then_atom_handler
;
2136 } else if (atom_type
== ATOM_ELSE
) {
2137 v
->handler
= else_atom_handler
;
2140 } else if (atom_type
== ATOM_REST
) {
2142 v
->s
= xstrdup(ref
->rest
);
2146 } else if (atom_type
== ATOM_AHEADBEHIND
) {
2148 const struct ahead_behind_count
*count
;
2149 count
= ref
->counts
[ahead_behind_atoms
++];
2150 v
->s
= xstrfmt("%d %d", count
->ahead
, count
->behind
);
2160 v
->s
= xstrdup(refname
);
2162 v
->s
= xstrfmt("%s^{}", refname
);
2163 free((char *)refname
);
2166 for (i
= 0; i
< used_atom_cnt
; i
++) {
2167 struct atom_value
*v
= &ref
->value
[i
];
2168 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
2169 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2170 oid_to_hex(&ref
->objectname
), ref
->refname
);
2174 oi
.info
.contentp
= &oi
.content
;
2175 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
2176 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
2180 oi
.oid
= ref
->objectname
;
2181 if (get_object(ref
, 0, &obj
, &oi
, err
))
2185 * If there is no atom that wants to know about tagged
2186 * object, we are done.
2188 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
2192 * If it is a tag object, see if we use a value that derefs
2193 * the object, and if we do grab the object it refers to.
2195 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
2198 * NEEDSWORK: This derefs tag only once, which
2199 * is good to deal with chains of trust, but
2200 * is not consistent with what deref_tag() does
2201 * which peels the onion to the core.
2203 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
2207 * Given a ref, return the value for the atom. This lazily gets value
2208 * out of the object by calling populate value.
2210 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
2211 struct atom_value
**v
, struct strbuf
*err
)
2214 if (populate_value(ref
, err
))
2216 fill_missing_values(ref
->value
);
2218 *v
= &ref
->value
[atom
];
2223 * Return 1 if the refname matches one of the patterns, otherwise 0.
2224 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2225 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2226 * matches "refs/heads/mas*", too).
2228 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
2230 const char **patterns
= filter
->name_patterns
;
2233 if (filter
->ignore_case
)
2234 flags
|= WM_CASEFOLD
;
2237 * When no '--format' option is given we need to skip the prefix
2238 * for matching refs of tags and branches.
2240 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
2241 skip_prefix(refname
, "refs/heads/", &refname
) ||
2242 skip_prefix(refname
, "refs/remotes/", &refname
) ||
2243 skip_prefix(refname
, "refs/", &refname
));
2245 for (; *patterns
; patterns
++) {
2246 if (!wildmatch(*patterns
, refname
, flags
))
2253 * Return 1 if the refname matches one of the patterns, otherwise 0.
2254 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
2255 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2256 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2258 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
2260 const char **pattern
= filter
->name_patterns
;
2261 int namelen
= strlen(refname
);
2262 unsigned flags
= WM_PATHNAME
;
2264 if (filter
->ignore_case
)
2265 flags
|= WM_CASEFOLD
;
2267 for (; *pattern
; pattern
++) {
2268 const char *p
= *pattern
;
2269 int plen
= strlen(p
);
2271 if ((plen
<= namelen
) &&
2272 !strncmp(refname
, p
, plen
) &&
2273 (refname
[plen
] == '\0' ||
2274 refname
[plen
] == '/' ||
2277 if (!wildmatch(p
, refname
, flags
))
2283 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
2284 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
2286 if (!*filter
->name_patterns
)
2287 return 1; /* No pattern always matches */
2288 if (filter
->match_as_path
)
2289 return match_name_as_path(filter
, refname
);
2290 return match_pattern(filter
, refname
);
2294 * This is the same as for_each_fullref_in(), but it tries to iterate
2295 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2296 * pattern match, so the callback still has to match each ref individually.
2298 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
2302 if (!filter
->match_as_path
) {
2304 * in this case, the patterns are applied after
2305 * prefixes like "refs/heads/" etc. are stripped off,
2306 * so we have to look at everything:
2308 return for_each_fullref_in("", cb
, cb_data
);
2311 if (filter
->ignore_case
) {
2313 * we can't handle case-insensitive comparisons,
2314 * so just return everything and let the caller
2317 return for_each_fullref_in("", cb
, cb_data
);
2320 if (!filter
->name_patterns
[0]) {
2321 /* no patterns; we have to look at everything */
2322 return for_each_fullref_in("", cb
, cb_data
);
2325 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository
),
2326 NULL
, filter
->name_patterns
,
2331 * Given a ref (oid, refname), check if the ref belongs to the array
2332 * of oids. If the given ref is a tag, check if the given tag points
2333 * at one of the oids in the given oid array.
2335 * As the refs are cached we might know what refname peels to without
2336 * the need to parse the object via parse_object(). peel_ref() might be a
2337 * more efficient alternative to obtain the pointee.
2339 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
2340 const struct object_id
*oid
,
2341 const char *refname
)
2345 if (oid_array_lookup(points_at
, oid
) >= 0)
2347 obj
= parse_object_with_flags(the_repository
, oid
,
2348 PARSE_OBJECT_SKIP_HASH_CHECK
);
2349 while (obj
&& obj
->type
== OBJ_TAG
) {
2350 struct tag
*tag
= (struct tag
*)obj
;
2352 if (parse_tag(tag
) < 0) {
2357 oid
= get_tagged_oid(tag
);
2358 if (oid_array_lookup(points_at
, oid
) >= 0)
2364 die(_("malformed object at '%s'"), refname
);
2369 * Allocate space for a new ref_array_item and copy the name and oid to it.
2371 * Callers can then fill in other struct members at their leisure.
2373 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2374 const struct object_id
*oid
)
2376 struct ref_array_item
*ref
;
2378 FLEX_ALLOC_STR(ref
, refname
, refname
);
2379 oidcpy(&ref
->objectname
, oid
);
2385 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2386 const char *refname
,
2387 const struct object_id
*oid
)
2389 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2391 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2392 array
->items
[array
->nr
++] = ref
;
2397 static int ref_kind_from_refname(const char *refname
)
2405 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2406 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2407 { "refs/tags/", FILTER_REFS_TAGS
}
2410 if (!strcmp(refname
, "HEAD"))
2411 return FILTER_REFS_DETACHED_HEAD
;
2413 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2414 if (starts_with(refname
, ref_kind
[i
].prefix
))
2415 return ref_kind
[i
].kind
;
2418 return FILTER_REFS_OTHERS
;
2421 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2423 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2424 filter
->kind
== FILTER_REFS_REMOTES
||
2425 filter
->kind
== FILTER_REFS_TAGS
)
2426 return filter
->kind
;
2427 return ref_kind_from_refname(refname
);
2430 struct ref_filter_cbdata
{
2431 struct ref_array
*array
;
2432 struct ref_filter
*filter
;
2433 struct contains_cache contains_cache
;
2434 struct contains_cache no_contains_cache
;
2438 * A call-back given to for_each_ref(). Filter refs and keep them for
2439 * later object processing.
2441 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2443 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2444 struct ref_filter
*filter
= ref_cbdata
->filter
;
2445 struct ref_array_item
*ref
;
2446 struct commit
*commit
= NULL
;
2449 if (flag
& REF_BAD_NAME
) {
2450 warning(_("ignoring ref with broken name %s"), refname
);
2454 if (flag
& REF_ISBROKEN
) {
2455 warning(_("ignoring broken ref %s"), refname
);
2459 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2460 kind
= filter_ref_kind(filter
, refname
);
2461 if (!(kind
& filter
->kind
))
2464 if (!filter_pattern_match(filter
, refname
))
2467 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2471 * A merge filter is applied on refs pointing to commits. Hence
2472 * obtain the commit using the 'oid' available and discard all
2473 * non-commits early. The actual filtering is done later.
2475 if (filter
->reachable_from
|| filter
->unreachable_from
||
2476 filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2477 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
2480 /* We perform the filtering for the '--contains' option... */
2481 if (filter
->with_commit
&&
2482 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2484 /* ...or for the `--no-contains' option */
2485 if (filter
->no_commit
&&
2486 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2491 * We do not open the object yet; sort may only need refname
2492 * to do its job and the resulting list may yet to be pruned
2493 * by maxcount logic.
2495 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2496 ref
->commit
= commit
;
2503 /* Free memory allocated for a ref_array_item */
2504 static void free_array_item(struct ref_array_item
*item
)
2506 free((char *)item
->symref
);
2509 for (i
= 0; i
< used_atom_cnt
; i
++)
2510 free((char *)item
->value
[i
].s
);
2517 /* Free all memory allocated for ref_array */
2518 void ref_array_clear(struct ref_array
*array
)
2522 for (i
= 0; i
< array
->nr
; i
++)
2523 free_array_item(array
->items
[i
]);
2524 FREE_AND_NULL(array
->items
);
2525 array
->nr
= array
->alloc
= 0;
2527 for (i
= 0; i
< used_atom_cnt
; i
++) {
2528 struct used_atom
*atom
= &used_atom
[i
];
2529 if (atom
->atom_type
== ATOM_HEAD
)
2531 free((char *)atom
->name
);
2533 FREE_AND_NULL(used_atom
);
2536 if (ref_to_worktree_map
.worktrees
) {
2537 hashmap_clear_and_free(&(ref_to_worktree_map
.map
),
2538 struct ref_to_worktree_entry
, ent
);
2539 free_worktrees(ref_to_worktree_map
.worktrees
);
2540 ref_to_worktree_map
.worktrees
= NULL
;
2543 FREE_AND_NULL(array
->counts
);
2546 #define EXCLUDE_REACHED 0
2547 #define INCLUDE_REACHED 1
2548 static void reach_filter(struct ref_array
*array
,
2549 struct commit_list
*check_reachable
,
2550 int include_reached
)
2553 struct commit
**to_clear
;
2555 if (!check_reachable
)
2558 CALLOC_ARRAY(to_clear
, array
->nr
);
2559 for (i
= 0; i
< array
->nr
; i
++) {
2560 struct ref_array_item
*item
= array
->items
[i
];
2561 to_clear
[i
] = item
->commit
;
2564 tips_reachable_from_bases(the_repository
,
2566 to_clear
, array
->nr
,
2572 for (i
= 0; i
< old_nr
; i
++) {
2573 struct ref_array_item
*item
= array
->items
[i
];
2574 struct commit
*commit
= item
->commit
;
2576 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2578 if (is_merged
== include_reached
)
2579 array
->items
[array
->nr
++] = array
->items
[i
];
2581 free_array_item(item
);
2584 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2586 while (check_reachable
) {
2587 struct commit
*merge_commit
= pop_commit(&check_reachable
);
2588 clear_commit_marks(merge_commit
, ALL_REV_FLAGS
);
2594 void filter_ahead_behind(struct repository
*r
,
2595 struct ref_format
*format
,
2596 struct ref_array
*array
)
2598 struct commit
**commits
;
2599 size_t commits_nr
= format
->bases
.nr
+ array
->nr
;
2601 if (!format
->bases
.nr
|| !array
->nr
)
2604 ALLOC_ARRAY(commits
, commits_nr
);
2605 for (size_t i
= 0; i
< format
->bases
.nr
; i
++)
2606 commits
[i
] = format
->bases
.items
[i
].util
;
2608 ALLOC_ARRAY(array
->counts
, st_mult(format
->bases
.nr
, array
->nr
));
2610 commits_nr
= format
->bases
.nr
;
2611 array
->counts_nr
= 0;
2612 for (size_t i
= 0; i
< array
->nr
; i
++) {
2613 const char *name
= array
->items
[i
]->refname
;
2614 commits
[commits_nr
] = lookup_commit_reference_by_name(name
);
2616 if (!commits
[commits_nr
])
2619 CALLOC_ARRAY(array
->items
[i
]->counts
, format
->bases
.nr
);
2620 for (size_t j
= 0; j
< format
->bases
.nr
; j
++) {
2621 struct ahead_behind_count
*count
;
2622 count
= &array
->counts
[array
->counts_nr
++];
2623 count
->tip_index
= commits_nr
;
2624 count
->base_index
= j
;
2626 array
->items
[i
]->counts
[j
] = count
;
2631 ahead_behind(r
, commits
, commits_nr
, array
->counts
, array
->counts_nr
);
2636 * API for filtering a set of refs. Based on the type of refs the user
2637 * has requested, we iterate through those refs and apply filters
2638 * as per the given ref_filter structure and finally store the
2639 * filtered refs in the ref_array structure.
2641 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2643 struct ref_filter_cbdata ref_cbdata
;
2644 int save_commit_buffer_orig
;
2647 ref_cbdata
.array
= array
;
2648 ref_cbdata
.filter
= filter
;
2650 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2652 save_commit_buffer_orig
= save_commit_buffer
;
2653 save_commit_buffer
= 0;
2655 init_contains_cache(&ref_cbdata
.contains_cache
);
2656 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2658 /* Simple per-ref filtering */
2660 die("filter_refs: invalid type");
2663 * For common cases where we need only branches or remotes or tags,
2664 * we only iterate through those refs. If a mix of refs is needed,
2665 * we iterate over all refs and filter out required refs with the help
2666 * of filter_ref_kind().
2668 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2669 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
);
2670 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2671 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
);
2672 else if (filter
->kind
== FILTER_REFS_TAGS
)
2673 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
);
2674 else if (filter
->kind
& FILTER_REFS_ALL
)
2675 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
);
2676 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2677 head_ref(ref_filter_handler
, &ref_cbdata
);
2680 clear_contains_cache(&ref_cbdata
.contains_cache
);
2681 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2683 /* Filters that need revision walking */
2684 reach_filter(array
, filter
->reachable_from
, INCLUDE_REACHED
);
2685 reach_filter(array
, filter
->unreachable_from
, EXCLUDE_REACHED
);
2687 save_commit_buffer
= save_commit_buffer_orig
;
2691 static int compare_detached_head(struct ref_array_item
*a
, struct ref_array_item
*b
)
2693 if (!(a
->kind
^ b
->kind
))
2694 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
2695 if (a
->kind
& FILTER_REFS_DETACHED_HEAD
)
2697 else if (b
->kind
& FILTER_REFS_DETACHED_HEAD
)
2699 BUG("should have died in the xor check above");
2703 static int memcasecmp(const void *vs1
, const void *vs2
, size_t n
)
2705 const char *s1
= vs1
, *s2
= vs2
;
2706 const char *end
= s1
+ n
;
2708 for (; s1
< end
; s1
++, s2
++) {
2709 int diff
= tolower(*s1
) - tolower(*s2
);
2716 struct ref_sorting
{
2717 struct ref_sorting
*next
;
2718 int atom
; /* index into used_atom array (internal) */
2719 enum ref_sorting_order sort_flags
;
2722 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2724 struct atom_value
*va
, *vb
;
2726 int cmp_detached_head
= 0;
2727 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2728 struct strbuf err
= STRBUF_INIT
;
2730 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2732 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2734 strbuf_release(&err
);
2735 if (s
->sort_flags
& REF_SORTING_DETACHED_HEAD_FIRST
&&
2736 ((a
->kind
| b
->kind
) & FILTER_REFS_DETACHED_HEAD
)) {
2737 cmp
= compare_detached_head(a
, b
);
2738 cmp_detached_head
= 1;
2739 } else if (s
->sort_flags
& REF_SORTING_VERSION
) {
2740 cmp
= versioncmp(va
->s
, vb
->s
);
2741 } else if (cmp_type
== FIELD_STR
) {
2742 if (va
->s_size
< 0 && vb
->s_size
< 0) {
2743 int (*cmp_fn
)(const char *, const char *);
2744 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
2745 ? strcasecmp
: strcmp
;
2746 cmp
= cmp_fn(va
->s
, vb
->s
);
2748 size_t a_size
= va
->s_size
< 0 ?
2749 strlen(va
->s
) : va
->s_size
;
2750 size_t b_size
= vb
->s_size
< 0 ?
2751 strlen(vb
->s
) : vb
->s_size
;
2752 int (*cmp_fn
)(const void *, const void *, size_t);
2753 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
2754 ? memcasecmp
: memcmp
;
2756 cmp
= cmp_fn(va
->s
, vb
->s
, b_size
> a_size
?
2759 if (a_size
> b_size
)
2761 else if (a_size
< b_size
)
2766 if (va
->value
< vb
->value
)
2768 else if (va
->value
== vb
->value
)
2774 return (s
->sort_flags
& REF_SORTING_REVERSE
&& !cmp_detached_head
)
2778 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2780 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2781 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2782 struct ref_sorting
*s
;
2784 for (s
= ref_sorting
; s
; s
= s
->next
) {
2785 int cmp
= cmp_ref_sorting(s
, a
, b
);
2790 return s
&& s
->sort_flags
& REF_SORTING_ICASE
?
2791 strcasecmp(a
->refname
, b
->refname
) :
2792 strcmp(a
->refname
, b
->refname
);
2795 void ref_sorting_set_sort_flags_all(struct ref_sorting
*sorting
,
2796 unsigned int mask
, int on
)
2798 for (; sorting
; sorting
= sorting
->next
) {
2800 sorting
->sort_flags
|= mask
;
2802 sorting
->sort_flags
&= ~mask
;
2806 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2808 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2811 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2813 struct strbuf
*s
= &state
->stack
->output
;
2815 while (*cp
&& (!ep
|| cp
< ep
)) {
2820 int ch
= hex2chr(cp
+ 1);
2822 strbuf_addch(s
, ch
);
2828 strbuf_addch(s
, *cp
);
2833 int format_ref_array_item(struct ref_array_item
*info
,
2834 struct ref_format
*format
,
2835 struct strbuf
*final_buf
,
2836 struct strbuf
*error_buf
)
2838 const char *cp
, *sp
, *ep
;
2839 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2841 state
.quote_style
= format
->quote_style
;
2842 push_stack_element(&state
.stack
);
2844 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2845 struct atom_value
*atomv
;
2848 ep
= strchr(sp
, ')');
2850 append_literal(cp
, sp
, &state
);
2851 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2852 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2853 atomv
->handler(atomv
, &state
, error_buf
)) {
2854 pop_stack_element(&state
.stack
);
2859 sp
= cp
+ strlen(cp
);
2860 append_literal(cp
, sp
, &state
);
2862 if (format
->need_color_reset_at_eol
) {
2863 struct atom_value resetv
= ATOM_VALUE_INIT
;
2864 resetv
.s
= GIT_COLOR_RESET
;
2865 if (append_atom(&resetv
, &state
, error_buf
)) {
2866 pop_stack_element(&state
.stack
);
2870 if (state
.stack
->prev
) {
2871 pop_stack_element(&state
.stack
);
2872 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2874 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2875 pop_stack_element(&state
.stack
);
2879 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2880 struct ref_format
*format
)
2882 struct ref_array_item
*ref_item
;
2883 struct strbuf output
= STRBUF_INIT
;
2884 struct strbuf err
= STRBUF_INIT
;
2886 ref_item
= new_ref_array_item(name
, oid
);
2887 ref_item
->kind
= ref_kind_from_refname(name
);
2888 if (format_ref_array_item(ref_item
, format
, &output
, &err
))
2890 fwrite(output
.buf
, 1, output
.len
, stdout
);
2893 strbuf_release(&err
);
2894 strbuf_release(&output
);
2895 free_array_item(ref_item
);
2898 static int parse_sorting_atom(const char *atom
)
2901 * This parses an atom using a dummy ref_format, since we don't
2902 * actually care about the formatting details.
2904 struct ref_format dummy
= REF_FORMAT_INIT
;
2905 const char *end
= atom
+ strlen(atom
);
2906 struct strbuf err
= STRBUF_INIT
;
2907 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2910 strbuf_release(&err
);
2914 /* If no sorting option is given, use refname to sort as default */
2915 static struct ref_sorting
*ref_default_sorting(void)
2917 static const char cstr_name
[] = "refname";
2919 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2921 sorting
->next
= NULL
;
2922 sorting
->atom
= parse_sorting_atom(cstr_name
);
2926 static void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2928 struct ref_sorting
*s
;
2931 s
->next
= *sorting_tail
;
2935 s
->sort_flags
|= REF_SORTING_REVERSE
;
2938 if (skip_prefix(arg
, "version:", &arg
) ||
2939 skip_prefix(arg
, "v:", &arg
))
2940 s
->sort_flags
|= REF_SORTING_VERSION
;
2941 s
->atom
= parse_sorting_atom(arg
);
2944 struct ref_sorting
*ref_sorting_options(struct string_list
*options
)
2946 struct string_list_item
*item
;
2947 struct ref_sorting
*sorting
= NULL
, **tail
= &sorting
;
2950 sorting
= ref_default_sorting();
2952 for_each_string_list_item(item
, options
)
2953 parse_ref_sorting(tail
, item
->string
);
2957 * From here on, the ref_sorting list should be used to talk
2958 * about the sort order used for the output. The caller
2959 * should not touch the string form anymore.
2961 string_list_clear(options
, 0);
2965 void ref_sorting_release(struct ref_sorting
*sorting
)
2968 struct ref_sorting
*next
= sorting
->next
;
2974 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2976 struct ref_filter
*rf
= opt
->value
;
2977 struct object_id oid
;
2978 struct commit
*merge_commit
;
2980 BUG_ON_OPT_NEG(unset
);
2982 if (repo_get_oid(the_repository
, arg
, &oid
))
2983 die(_("malformed object name %s"), arg
);
2985 merge_commit
= lookup_commit_reference_gently(the_repository
, &oid
, 0);
2988 return error(_("option `%s' must point to a commit"), opt
->long_name
);
2990 if (starts_with(opt
->long_name
, "no"))
2991 commit_list_insert(merge_commit
, &rf
->unreachable_from
);
2993 commit_list_insert(merge_commit
, &rf
->reachable_from
);