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_*)`.
170 * An atom is a valid field atom listed below, possibly prefixed with
171 * a "*" to denote deref_tag().
173 * We parse given format string and sort specifiers, and make a list
174 * of properties that we need to extract out of objects. ref_array_item
175 * structure will hold an array of values extracted that can be
176 * indexed with the "atom number", which is an index into this
179 static struct used_atom
{
180 enum atom_type atom_type
;
185 char color
[COLOR_MAXLEN
];
189 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
191 struct refname_atom refname
;
192 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
195 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LENGTH
, C_LINES
,
196 C_SIG
, C_SUB
, C_SUB_SANITIZE
, C_TRAILERS
} option
;
197 struct process_trailer_options trailer_opts
;
201 enum { RAW_BARE
, RAW_LENGTH
} option
;
204 cmp_status cmp_status
;
208 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
212 enum { O_SIZE
, O_SIZE_DISK
} option
;
214 struct email_option
{
215 enum { EO_RAW
, EO_TRIM
, EO_LOCALPART
} option
;
217 struct refname_atom refname
;
221 static int used_atom_cnt
, need_tagged
, need_symref
;
224 * Expand string, append it to strbuf *sb, then return error code ret.
225 * Allow to save few lines of code.
227 __attribute__((format (printf
, 3, 4)))
228 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
232 strbuf_vaddf(sb
, fmt
, ap
);
237 static int err_no_arg(struct strbuf
*sb
, const char *name
)
239 size_t namelen
= strchrnul(name
, ':') - name
;
240 strbuf_addf(sb
, _("%%(%.*s) does not take arguments"),
245 static int err_bad_arg(struct strbuf
*sb
, const char *name
, const char *arg
)
247 size_t namelen
= strchrnul(name
, ':') - name
;
248 strbuf_addf(sb
, _("unrecognized %%(%.*s) argument: %s"),
249 (int)namelen
, name
, arg
);
253 static int color_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
254 const char *color_value
, struct strbuf
*err
)
257 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
258 if (color_parse(color_value
, atom
->u
.color
) < 0)
259 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
262 * We check this after we've parsed the color, which lets us complain
263 * about syntactically bogus color names even if they won't be used.
265 if (!want_color(format
->use_color
))
266 color_parse("", atom
->u
.color
);
270 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
271 const char *name
, struct strbuf
*err
)
274 atom
->option
= R_NORMAL
;
275 else if (!strcmp(arg
, "short"))
276 atom
->option
= R_SHORT
;
277 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
278 skip_prefix(arg
, "strip=", &arg
)) {
279 atom
->option
= R_LSTRIP
;
280 if (strtol_i(arg
, 10, &atom
->lstrip
))
281 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
282 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
283 atom
->option
= R_RSTRIP
;
284 if (strtol_i(arg
, 10, &atom
->rstrip
))
285 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
287 return err_bad_arg(err
, name
, arg
);
291 static int remote_ref_atom_parser(struct ref_format
*format UNUSED
,
292 struct used_atom
*atom
,
293 const char *arg
, struct strbuf
*err
)
295 struct string_list params
= STRING_LIST_INIT_DUP
;
298 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
299 atom
->u
.remote_ref
.push
= 1;
302 atom
->u
.remote_ref
.option
= RR_REF
;
303 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
304 arg
, atom
->name
, err
);
307 atom
->u
.remote_ref
.nobracket
= 0;
308 string_list_split(¶ms
, arg
, ',', -1);
310 for (i
= 0; i
< params
.nr
; i
++) {
311 const char *s
= params
.items
[i
].string
;
313 if (!strcmp(s
, "track"))
314 atom
->u
.remote_ref
.option
= RR_TRACK
;
315 else if (!strcmp(s
, "trackshort"))
316 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
317 else if (!strcmp(s
, "nobracket"))
318 atom
->u
.remote_ref
.nobracket
= 1;
319 else if (!strcmp(s
, "remotename")) {
320 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
321 atom
->u
.remote_ref
.push_remote
= 1;
322 } else if (!strcmp(s
, "remoteref")) {
323 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
324 atom
->u
.remote_ref
.push_remote
= 1;
326 atom
->u
.remote_ref
.option
= RR_REF
;
327 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
328 arg
, atom
->name
, err
)) {
329 string_list_clear(¶ms
, 0);
335 string_list_clear(¶ms
, 0);
339 static int objecttype_atom_parser(struct ref_format
*format UNUSED
,
340 struct used_atom
*atom
,
341 const char *arg
, struct strbuf
*err
)
344 return err_no_arg(err
, "objecttype");
345 if (*atom
->name
== '*')
346 oi_deref
.info
.typep
= &oi_deref
.type
;
348 oi
.info
.typep
= &oi
.type
;
352 static int objectsize_atom_parser(struct ref_format
*format UNUSED
,
353 struct used_atom
*atom
,
354 const char *arg
, struct strbuf
*err
)
357 atom
->u
.objectsize
.option
= O_SIZE
;
358 if (*atom
->name
== '*')
359 oi_deref
.info
.sizep
= &oi_deref
.size
;
361 oi
.info
.sizep
= &oi
.size
;
362 } else if (!strcmp(arg
, "disk")) {
363 atom
->u
.objectsize
.option
= O_SIZE_DISK
;
364 if (*atom
->name
== '*')
365 oi_deref
.info
.disk_sizep
= &oi_deref
.disk_size
;
367 oi
.info
.disk_sizep
= &oi
.disk_size
;
369 return err_bad_arg(err
, "objectsize", arg
);
373 static int deltabase_atom_parser(struct ref_format
*format UNUSED
,
374 struct used_atom
*atom
,
375 const char *arg
, struct strbuf
*err
)
378 return err_no_arg(err
, "deltabase");
379 if (*atom
->name
== '*')
380 oi_deref
.info
.delta_base_oid
= &oi_deref
.delta_base_oid
;
382 oi
.info
.delta_base_oid
= &oi
.delta_base_oid
;
386 static int body_atom_parser(struct ref_format
*format UNUSED
,
387 struct used_atom
*atom
,
388 const char *arg
, struct strbuf
*err
)
391 return err_no_arg(err
, "body");
392 atom
->u
.contents
.option
= C_BODY_DEP
;
396 static int subject_atom_parser(struct ref_format
*format UNUSED
,
397 struct used_atom
*atom
,
398 const char *arg
, struct strbuf
*err
)
401 atom
->u
.contents
.option
= C_SUB
;
402 else if (!strcmp(arg
, "sanitize"))
403 atom
->u
.contents
.option
= C_SUB_SANITIZE
;
405 return err_bad_arg(err
, "subject", arg
);
409 static int trailers_atom_parser(struct ref_format
*format UNUSED
,
410 struct used_atom
*atom
,
411 const char *arg
, struct strbuf
*err
)
413 atom
->u
.contents
.trailer_opts
.no_divider
= 1;
416 const char *argbuf
= xstrfmt("%s)", arg
);
417 char *invalid_arg
= NULL
;
419 if (format_set_trailers_options(&atom
->u
.contents
.trailer_opts
,
420 &ref_trailer_buf
.filter_list
,
421 &ref_trailer_buf
.sepbuf
,
422 &ref_trailer_buf
.kvsepbuf
,
423 &argbuf
, &invalid_arg
)) {
425 strbuf_addf(err
, _("expected %%(trailers:key=<value>)"));
427 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), invalid_arg
);
428 free((char *)invalid_arg
);
432 atom
->u
.contents
.option
= C_TRAILERS
;
436 static int contents_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
437 const char *arg
, struct strbuf
*err
)
440 atom
->u
.contents
.option
= C_BARE
;
441 else if (!strcmp(arg
, "body"))
442 atom
->u
.contents
.option
= C_BODY
;
443 else if (!strcmp(arg
, "size"))
444 atom
->u
.contents
.option
= C_LENGTH
;
445 else if (!strcmp(arg
, "signature"))
446 atom
->u
.contents
.option
= C_SIG
;
447 else if (!strcmp(arg
, "subject"))
448 atom
->u
.contents
.option
= C_SUB
;
449 else if (!strcmp(arg
, "trailers")) {
450 if (trailers_atom_parser(format
, atom
, NULL
, err
))
452 } else if (skip_prefix(arg
, "trailers:", &arg
)) {
453 if (trailers_atom_parser(format
, atom
, arg
, err
))
455 } else if (skip_prefix(arg
, "lines=", &arg
)) {
456 atom
->u
.contents
.option
= C_LINES
;
457 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
458 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
460 return err_bad_arg(err
, "contents", arg
);
464 static int raw_atom_parser(struct ref_format
*format UNUSED
,
465 struct used_atom
*atom
,
466 const char *arg
, struct strbuf
*err
)
469 atom
->u
.raw_data
.option
= RAW_BARE
;
470 else if (!strcmp(arg
, "size"))
471 atom
->u
.raw_data
.option
= RAW_LENGTH
;
473 return err_bad_arg(err
, "raw", arg
);
477 static int oid_atom_parser(struct ref_format
*format UNUSED
,
478 struct used_atom
*atom
,
479 const char *arg
, struct strbuf
*err
)
482 atom
->u
.oid
.option
= O_FULL
;
483 else if (!strcmp(arg
, "short"))
484 atom
->u
.oid
.option
= O_SHORT
;
485 else if (skip_prefix(arg
, "short=", &arg
)) {
486 atom
->u
.oid
.option
= O_LENGTH
;
487 if (strtoul_ui(arg
, 10, &atom
->u
.oid
.length
) ||
488 atom
->u
.oid
.length
== 0)
489 return strbuf_addf_ret(err
, -1, _("positive value expected '%s' in %%(%s)"), arg
, atom
->name
);
490 if (atom
->u
.oid
.length
< MINIMUM_ABBREV
)
491 atom
->u
.oid
.length
= MINIMUM_ABBREV
;
493 return err_bad_arg(err
, atom
->name
, arg
);
497 static int person_email_atom_parser(struct ref_format
*format UNUSED
,
498 struct used_atom
*atom
,
499 const char *arg
, struct strbuf
*err
)
502 atom
->u
.email_option
.option
= EO_RAW
;
503 else if (!strcmp(arg
, "trim"))
504 atom
->u
.email_option
.option
= EO_TRIM
;
505 else if (!strcmp(arg
, "localpart"))
506 atom
->u
.email_option
.option
= EO_LOCALPART
;
508 return err_bad_arg(err
, atom
->name
, arg
);
512 static int refname_atom_parser(struct ref_format
*format UNUSED
,
513 struct used_atom
*atom
,
514 const char *arg
, struct strbuf
*err
)
516 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
519 static align_type
parse_align_position(const char *s
)
521 if (!strcmp(s
, "right"))
523 else if (!strcmp(s
, "middle"))
525 else if (!strcmp(s
, "left"))
530 static int align_atom_parser(struct ref_format
*format UNUSED
,
531 struct used_atom
*atom
,
532 const char *arg
, struct strbuf
*err
)
534 struct align
*align
= &atom
->u
.align
;
535 struct string_list params
= STRING_LIST_INIT_DUP
;
537 unsigned int width
= ~0U;
540 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
542 align
->position
= ALIGN_LEFT
;
544 string_list_split(¶ms
, arg
, ',', -1);
545 for (i
= 0; i
< params
.nr
; i
++) {
546 const char *s
= params
.items
[i
].string
;
549 if (skip_prefix(s
, "position=", &s
)) {
550 position
= parse_align_position(s
);
552 strbuf_addf(err
, _("unrecognized position:%s"), s
);
553 string_list_clear(¶ms
, 0);
556 align
->position
= position
;
557 } else if (skip_prefix(s
, "width=", &s
)) {
558 if (strtoul_ui(s
, 10, &width
)) {
559 strbuf_addf(err
, _("unrecognized width:%s"), s
);
560 string_list_clear(¶ms
, 0);
563 } else if (!strtoul_ui(s
, 10, &width
))
565 else if ((position
= parse_align_position(s
)) >= 0)
566 align
->position
= position
;
568 strbuf_addf(err
, _("unrecognized %%(%s) argument: %s"), "align", s
);
569 string_list_clear(¶ms
, 0);
575 string_list_clear(¶ms
, 0);
576 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
578 align
->width
= width
;
579 string_list_clear(¶ms
, 0);
583 static int if_atom_parser(struct ref_format
*format UNUSED
,
584 struct used_atom
*atom
,
585 const char *arg
, struct strbuf
*err
)
588 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
590 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
591 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
592 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
593 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
595 return err_bad_arg(err
, "if", arg
);
599 static int rest_atom_parser(struct ref_format
*format
,
600 struct used_atom
*atom UNUSED
,
601 const char *arg
, struct strbuf
*err
)
604 return err_no_arg(err
, "rest");
608 static int ahead_behind_atom_parser(struct ref_format
*format
, struct used_atom
*atom
,
609 const char *arg
, struct strbuf
*err
)
611 struct string_list_item
*item
;
614 return strbuf_addf_ret(err
, -1, _("expected format: %%(ahead-behind:<committish>)"));
616 item
= string_list_append(&format
->bases
, arg
);
617 item
->util
= lookup_commit_reference_by_name(arg
);
619 die("failed to find '%s'", arg
);
624 static int head_atom_parser(struct ref_format
*format UNUSED
,
625 struct used_atom
*atom
,
626 const char *arg
, struct strbuf
*err
)
629 return err_no_arg(err
, "HEAD");
630 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
638 int (*parser
)(struct ref_format
*format
, struct used_atom
*atom
,
639 const char *arg
, struct strbuf
*err
);
641 [ATOM_REFNAME
] = { "refname", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
642 [ATOM_OBJECTTYPE
] = { "objecttype", SOURCE_OTHER
, FIELD_STR
, objecttype_atom_parser
},
643 [ATOM_OBJECTSIZE
] = { "objectsize", SOURCE_OTHER
, FIELD_ULONG
, objectsize_atom_parser
},
644 [ATOM_OBJECTNAME
] = { "objectname", SOURCE_OTHER
, FIELD_STR
, oid_atom_parser
},
645 [ATOM_DELTABASE
] = { "deltabase", SOURCE_OTHER
, FIELD_STR
, deltabase_atom_parser
},
646 [ATOM_TREE
] = { "tree", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
647 [ATOM_PARENT
] = { "parent", SOURCE_OBJ
, FIELD_STR
, oid_atom_parser
},
648 [ATOM_NUMPARENT
] = { "numparent", SOURCE_OBJ
, FIELD_ULONG
},
649 [ATOM_OBJECT
] = { "object", SOURCE_OBJ
},
650 [ATOM_TYPE
] = { "type", SOURCE_OBJ
},
651 [ATOM_TAG
] = { "tag", SOURCE_OBJ
},
652 [ATOM_AUTHOR
] = { "author", SOURCE_OBJ
},
653 [ATOM_AUTHORNAME
] = { "authorname", SOURCE_OBJ
},
654 [ATOM_AUTHOREMAIL
] = { "authoremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
655 [ATOM_AUTHORDATE
] = { "authordate", SOURCE_OBJ
, FIELD_TIME
},
656 [ATOM_COMMITTER
] = { "committer", SOURCE_OBJ
},
657 [ATOM_COMMITTERNAME
] = { "committername", SOURCE_OBJ
},
658 [ATOM_COMMITTEREMAIL
] = { "committeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
659 [ATOM_COMMITTERDATE
] = { "committerdate", SOURCE_OBJ
, FIELD_TIME
},
660 [ATOM_TAGGER
] = { "tagger", SOURCE_OBJ
},
661 [ATOM_TAGGERNAME
] = { "taggername", SOURCE_OBJ
},
662 [ATOM_TAGGEREMAIL
] = { "taggeremail", SOURCE_OBJ
, FIELD_STR
, person_email_atom_parser
},
663 [ATOM_TAGGERDATE
] = { "taggerdate", SOURCE_OBJ
, FIELD_TIME
},
664 [ATOM_CREATOR
] = { "creator", SOURCE_OBJ
},
665 [ATOM_CREATORDATE
] = { "creatordate", SOURCE_OBJ
, FIELD_TIME
},
666 [ATOM_SUBJECT
] = { "subject", SOURCE_OBJ
, FIELD_STR
, subject_atom_parser
},
667 [ATOM_BODY
] = { "body", SOURCE_OBJ
, FIELD_STR
, body_atom_parser
},
668 [ATOM_TRAILERS
] = { "trailers", SOURCE_OBJ
, FIELD_STR
, trailers_atom_parser
},
669 [ATOM_CONTENTS
] = { "contents", SOURCE_OBJ
, FIELD_STR
, contents_atom_parser
},
670 [ATOM_RAW
] = { "raw", SOURCE_OBJ
, FIELD_STR
, raw_atom_parser
},
671 [ATOM_UPSTREAM
] = { "upstream", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
672 [ATOM_PUSH
] = { "push", SOURCE_NONE
, FIELD_STR
, remote_ref_atom_parser
},
673 [ATOM_SYMREF
] = { "symref", SOURCE_NONE
, FIELD_STR
, refname_atom_parser
},
674 [ATOM_FLAG
] = { "flag", SOURCE_NONE
},
675 [ATOM_HEAD
] = { "HEAD", SOURCE_NONE
, FIELD_STR
, head_atom_parser
},
676 [ATOM_COLOR
] = { "color", SOURCE_NONE
, FIELD_STR
, color_atom_parser
},
677 [ATOM_WORKTREEPATH
] = { "worktreepath", SOURCE_NONE
},
678 [ATOM_ALIGN
] = { "align", SOURCE_NONE
, FIELD_STR
, align_atom_parser
},
679 [ATOM_END
] = { "end", SOURCE_NONE
},
680 [ATOM_IF
] = { "if", SOURCE_NONE
, FIELD_STR
, if_atom_parser
},
681 [ATOM_THEN
] = { "then", SOURCE_NONE
},
682 [ATOM_ELSE
] = { "else", SOURCE_NONE
},
683 [ATOM_REST
] = { "rest", SOURCE_NONE
, FIELD_STR
, rest_atom_parser
},
684 [ATOM_AHEADBEHIND
] = { "ahead-behind", SOURCE_OTHER
, FIELD_STR
, ahead_behind_atom_parser
},
686 * Please update $__git_ref_fieldlist in git-completion.bash
687 * when you add new atoms
691 #define REF_FORMATTING_STATE_INIT { 0 }
693 struct ref_formatting_stack
{
694 struct ref_formatting_stack
*prev
;
695 struct strbuf output
;
696 void (*at_end
)(struct ref_formatting_stack
**stack
);
700 struct ref_formatting_state
{
702 struct ref_formatting_stack
*stack
;
708 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
710 uintmax_t value
; /* used for sorting when not FIELD_STR */
711 struct used_atom
*atom
;
714 #define ATOM_SIZE_UNSPECIFIED (-1)
716 #define ATOM_VALUE_INIT { \
717 .s_size = ATOM_SIZE_UNSPECIFIED \
721 * Used to parse format string and sort specifiers
723 static int parse_ref_filter_atom(struct ref_format
*format
,
724 const char *atom
, const char *ep
,
732 if (*sp
== '*' && sp
< ep
)
735 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
736 (int)(ep
-atom
), atom
);
739 * If the atom name has a colon, strip it and everything after
740 * it off - it specifies the format for this entry, and
741 * shouldn't be used for checking against the valid_atom
744 arg
= memchr(sp
, ':', ep
- sp
);
745 atom_len
= (arg
? arg
: ep
) - sp
;
747 /* Do we have the atom already used elsewhere? */
748 for (i
= 0; i
< used_atom_cnt
; i
++) {
749 int len
= strlen(used_atom
[i
].name
);
750 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
754 /* Is the atom a valid one? */
755 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
756 int len
= strlen(valid_atom
[i
].name
);
757 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
761 if (ARRAY_SIZE(valid_atom
) <= i
)
762 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
763 (int)(ep
-atom
), atom
);
764 if (valid_atom
[i
].source
!= SOURCE_NONE
&& !have_git_dir())
765 return strbuf_addf_ret(err
, -1,
766 _("not a git repository, but the field '%.*s' requires access to object data"),
767 (int)(ep
-atom
), atom
);
769 /* Add it in, including the deref prefix */
772 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
773 used_atom
[at
].atom_type
= i
;
774 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
775 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
776 used_atom
[at
].source
= valid_atom
[i
].source
;
777 if (used_atom
[at
].source
== SOURCE_OBJ
) {
779 oi_deref
.info
.contentp
= &oi_deref
.content
;
781 oi
.info
.contentp
= &oi
.content
;
784 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
787 * Treat empty sub-arguments list as NULL (i.e.,
788 * "%(atom:)" is equivalent to "%(atom)").
793 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
794 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
798 if (i
== ATOM_SYMREF
)
803 static void quote_formatting(struct strbuf
*s
, const char *str
, ssize_t len
, int quote_style
)
805 switch (quote_style
) {
808 strbuf_addstr(s
, str
);
810 strbuf_add(s
, str
, len
);
813 sq_quote_buf(s
, str
);
817 perl_quote_buf(s
, str
);
819 perl_quote_buf_with_len(s
, str
, len
);
822 python_quote_buf(s
, str
);
825 tcl_quote_buf(s
, str
);
830 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
831 struct strbuf
*err UNUSED
)
834 * Quote formatting is only done when the stack has a single
835 * element. Otherwise quote formatting is done on the
836 * element's entire output strbuf when the %(end) atom is
839 if (!state
->stack
->prev
)
840 quote_formatting(&state
->stack
->output
, v
->s
, v
->s_size
, state
->quote_style
);
841 else if (v
->s_size
< 0)
842 strbuf_addstr(&state
->stack
->output
, v
->s
);
844 strbuf_add(&state
->stack
->output
, v
->s
, v
->s_size
);
848 static void push_stack_element(struct ref_formatting_stack
**stack
)
850 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
852 strbuf_init(&s
->output
, 0);
857 static void pop_stack_element(struct ref_formatting_stack
**stack
)
859 struct ref_formatting_stack
*current
= *stack
;
860 struct ref_formatting_stack
*prev
= current
->prev
;
863 strbuf_addbuf(&prev
->output
, ¤t
->output
);
864 strbuf_release(¤t
->output
);
869 static void end_align_handler(struct ref_formatting_stack
**stack
)
871 struct ref_formatting_stack
*cur
= *stack
;
872 struct align
*align
= (struct align
*)cur
->at_end_data
;
873 struct strbuf s
= STRBUF_INIT
;
875 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
876 strbuf_swap(&cur
->output
, &s
);
880 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
881 struct strbuf
*err UNUSED
)
883 struct ref_formatting_stack
*new_stack
;
885 push_stack_element(&state
->stack
);
886 new_stack
= state
->stack
;
887 new_stack
->at_end
= end_align_handler
;
888 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
892 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
894 struct ref_formatting_stack
*cur
= *stack
;
895 struct ref_formatting_stack
*prev
= cur
->prev
;
896 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
898 if (!if_then_else
->then_atom_seen
)
899 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
901 if (if_then_else
->else_atom_seen
) {
903 * There is an %(else) atom: we need to drop one state from the
904 * stack, either the %(else) branch if the condition is satisfied, or
905 * the %(then) branch if it isn't.
907 if (if_then_else
->condition_satisfied
) {
908 strbuf_reset(&cur
->output
);
909 pop_stack_element(&cur
);
911 strbuf_swap(&cur
->output
, &prev
->output
);
912 strbuf_reset(&cur
->output
);
913 pop_stack_element(&cur
);
915 } else if (!if_then_else
->condition_satisfied
) {
917 * No %(else) atom: just drop the %(then) branch if the
918 * condition is not satisfied.
920 strbuf_reset(&cur
->output
);
927 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
928 struct strbuf
*err UNUSED
)
930 struct ref_formatting_stack
*new_stack
;
931 struct if_then_else
*if_then_else
= xcalloc(1,
932 sizeof(struct if_then_else
));
934 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
935 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
937 push_stack_element(&state
->stack
);
938 new_stack
= state
->stack
;
939 new_stack
->at_end
= if_then_else_handler
;
940 new_stack
->at_end_data
= if_then_else
;
944 static int is_empty(struct strbuf
*buf
)
946 const char *cur
= buf
->buf
;
947 const char *end
= buf
->buf
+ buf
->len
;
949 while (cur
!= end
&& (isspace(*cur
)))
955 static int then_atom_handler(struct atom_value
*atomv UNUSED
,
956 struct ref_formatting_state
*state
,
959 struct ref_formatting_stack
*cur
= state
->stack
;
960 struct if_then_else
*if_then_else
= NULL
;
963 if (cur
->at_end
== if_then_else_handler
)
964 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
966 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
967 if (if_then_else
->then_atom_seen
)
968 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
969 if (if_then_else
->else_atom_seen
)
970 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
971 if_then_else
->then_atom_seen
= 1;
972 if (if_then_else
->str
)
973 str_len
= strlen(if_then_else
->str
);
975 * If the 'equals' or 'notequals' attribute is used then
976 * perform the required comparison. If not, only non-empty
977 * strings satisfy the 'if' condition.
979 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
980 if (str_len
== cur
->output
.len
&&
981 !memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
982 if_then_else
->condition_satisfied
= 1;
983 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
984 if (str_len
!= cur
->output
.len
||
985 memcmp(if_then_else
->str
, cur
->output
.buf
, cur
->output
.len
))
986 if_then_else
->condition_satisfied
= 1;
987 } else if (cur
->output
.len
&& !is_empty(&cur
->output
))
988 if_then_else
->condition_satisfied
= 1;
989 strbuf_reset(&cur
->output
);
993 static int else_atom_handler(struct atom_value
*atomv UNUSED
,
994 struct ref_formatting_state
*state
,
997 struct ref_formatting_stack
*prev
= state
->stack
;
998 struct if_then_else
*if_then_else
= NULL
;
1000 if (prev
->at_end
== if_then_else_handler
)
1001 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
1003 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
1004 if (!if_then_else
->then_atom_seen
)
1005 return strbuf_addf_ret(err
, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
1006 if (if_then_else
->else_atom_seen
)
1007 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
1008 if_then_else
->else_atom_seen
= 1;
1009 push_stack_element(&state
->stack
);
1010 state
->stack
->at_end_data
= prev
->at_end_data
;
1011 state
->stack
->at_end
= prev
->at_end
;
1015 static int end_atom_handler(struct atom_value
*atomv UNUSED
,
1016 struct ref_formatting_state
*state
,
1019 struct ref_formatting_stack
*current
= state
->stack
;
1020 struct strbuf s
= STRBUF_INIT
;
1022 if (!current
->at_end
)
1023 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
1024 current
->at_end(&state
->stack
);
1026 /* Stack may have been popped within at_end(), hence reset the current pointer */
1027 current
= state
->stack
;
1030 * Perform quote formatting when the stack element is that of
1031 * a supporting atom. If nested then perform quote formatting
1032 * only on the topmost supporting atom.
1034 if (!current
->prev
->prev
) {
1035 quote_formatting(&s
, current
->output
.buf
, current
->output
.len
, state
->quote_style
);
1036 strbuf_swap(¤t
->output
, &s
);
1039 pop_stack_element(&state
->stack
);
1044 * In a format string, find the next occurrence of %(atom).
1046 static const char *find_next(const char *cp
)
1051 * %( is the start of an atom;
1052 * %% is a quoted per-cent.
1056 else if (cp
[1] == '%')
1057 cp
++; /* skip over two % */
1058 /* otherwise this is a singleton, literal % */
1065 static int reject_atom(enum atom_type atom_type
)
1067 return atom_type
== ATOM_REST
;
1071 * Make sure the format string is well formed, and parse out
1074 int verify_ref_format(struct ref_format
*format
)
1076 const char *cp
, *sp
;
1078 format
->need_color_reset_at_eol
= 0;
1079 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
1080 struct strbuf err
= STRBUF_INIT
;
1081 const char *color
, *ep
= strchr(sp
, ')');
1085 return error(_("malformed format string %s"), sp
);
1086 /* sp points at "%(" and ep points at the closing ")" */
1087 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
1090 if (reject_atom(used_atom
[at
].atom_type
))
1091 die(_("this command reject atom %%(%.*s)"), (int)(ep
- sp
- 2), sp
+ 2);
1093 if ((format
->quote_style
== QUOTE_PYTHON
||
1094 format
->quote_style
== QUOTE_SHELL
||
1095 format
->quote_style
== QUOTE_TCL
) &&
1096 used_atom
[at
].atom_type
== ATOM_RAW
&&
1097 used_atom
[at
].u
.raw_data
.option
== RAW_BARE
)
1098 die(_("--format=%.*s cannot be used with "
1099 "--python, --shell, --tcl"), (int)(ep
- sp
- 2), sp
+ 2);
1102 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
1103 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
1104 strbuf_release(&err
);
1106 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
1107 format
->need_color_reset_at_eol
= 0;
1111 static const char *do_grab_oid(const char *field
, const struct object_id
*oid
,
1112 struct used_atom
*atom
)
1114 switch (atom
->u
.oid
.option
) {
1116 return oid_to_hex(oid
);
1118 return repo_find_unique_abbrev(the_repository
, oid
,
1119 atom
->u
.oid
.length
);
1121 return repo_find_unique_abbrev(the_repository
, oid
,
1124 BUG("unknown %%(%s) option", field
);
1128 static int grab_oid(const char *name
, const char *field
, const struct object_id
*oid
,
1129 struct atom_value
*v
, struct used_atom
*atom
)
1131 if (starts_with(name
, field
)) {
1132 v
->s
= xstrdup(do_grab_oid(field
, oid
, atom
));
1138 /* See grab_values */
1139 static void grab_common_values(struct atom_value
*val
, int deref
, struct expand_data
*oi
)
1143 for (i
= 0; i
< used_atom_cnt
; i
++) {
1144 const char *name
= used_atom
[i
].name
;
1145 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1146 struct atom_value
*v
= &val
[i
];
1147 if (!!deref
!= (*name
== '*'))
1151 if (atom_type
== ATOM_OBJECTTYPE
)
1152 v
->s
= xstrdup(type_name(oi
->type
));
1153 else if (atom_type
== ATOM_OBJECTSIZE
) {
1154 if (used_atom
[i
].u
.objectsize
.option
== O_SIZE_DISK
) {
1155 v
->value
= oi
->disk_size
;
1156 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->disk_size
);
1157 } else if (used_atom
[i
].u
.objectsize
.option
== O_SIZE
) {
1158 v
->value
= oi
->size
;
1159 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)oi
->size
);
1161 } else if (atom_type
== ATOM_DELTABASE
)
1162 v
->s
= xstrdup(oid_to_hex(&oi
->delta_base_oid
));
1163 else if (atom_type
== ATOM_OBJECTNAME
&& deref
)
1164 grab_oid(name
, "objectname", &oi
->oid
, v
, &used_atom
[i
]);
1168 /* See grab_values */
1169 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1172 struct tag
*tag
= (struct tag
*) obj
;
1174 for (i
= 0; i
< used_atom_cnt
; i
++) {
1175 const char *name
= used_atom
[i
].name
;
1176 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1177 struct atom_value
*v
= &val
[i
];
1178 if (!!deref
!= (*name
== '*'))
1182 if (atom_type
== ATOM_TAG
)
1183 v
->s
= xstrdup(tag
->tag
);
1184 else if (atom_type
== ATOM_TYPE
&& tag
->tagged
)
1185 v
->s
= xstrdup(type_name(tag
->tagged
->type
));
1186 else if (atom_type
== ATOM_OBJECT
&& tag
->tagged
)
1187 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
1191 /* See grab_values */
1192 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
)
1195 struct commit
*commit
= (struct commit
*) obj
;
1197 for (i
= 0; i
< used_atom_cnt
; i
++) {
1198 const char *name
= used_atom
[i
].name
;
1199 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1200 struct atom_value
*v
= &val
[i
];
1201 if (!!deref
!= (*name
== '*'))
1205 if (atom_type
== ATOM_TREE
&&
1206 grab_oid(name
, "tree", get_commit_tree_oid(commit
), v
, &used_atom
[i
]))
1208 if (atom_type
== ATOM_NUMPARENT
) {
1209 v
->value
= commit_list_count(commit
->parents
);
1210 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
1212 else if (atom_type
== ATOM_PARENT
) {
1213 struct commit_list
*parents
;
1214 struct strbuf s
= STRBUF_INIT
;
1215 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1216 struct object_id
*oid
= &parents
->item
->object
.oid
;
1217 if (parents
!= commit
->parents
)
1218 strbuf_addch(&s
, ' ');
1219 strbuf_addstr(&s
, do_grab_oid("parent", oid
, &used_atom
[i
]));
1221 v
->s
= strbuf_detach(&s
, NULL
);
1226 static const char *find_wholine(const char *who
, int wholen
, const char *buf
)
1230 if (!strncmp(buf
, who
, wholen
) &&
1232 return buf
+ wholen
+ 1;
1233 eol
= strchr(buf
, '\n');
1238 return ""; /* end of header */
1244 static const char *copy_line(const char *buf
)
1246 const char *eol
= strchrnul(buf
, '\n');
1247 return xmemdupz(buf
, eol
- buf
);
1250 static const char *copy_name(const char *buf
)
1253 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
1254 if (starts_with(cp
, " <"))
1255 return xmemdupz(buf
, cp
- buf
);
1260 static const char *copy_email(const char *buf
, struct used_atom
*atom
)
1262 const char *email
= strchr(buf
, '<');
1263 const char *eoemail
;
1266 switch (atom
->u
.email_option
.option
) {
1268 eoemail
= strchr(email
, '>');
1274 eoemail
= strchr(email
, '>');
1278 eoemail
= strchr(email
, '@');
1280 eoemail
= strchr(email
, '>');
1283 BUG("unknown email option");
1288 return xmemdupz(email
, eoemail
- email
);
1291 static char *copy_subject(const char *buf
, unsigned long len
)
1293 struct strbuf sb
= STRBUF_INIT
;
1296 for (i
= 0; i
< len
; i
++) {
1297 if (buf
[i
] == '\r' && i
+ 1 < len
&& buf
[i
+ 1] == '\n')
1298 continue; /* ignore CR in CRLF */
1301 strbuf_addch(&sb
, ' ');
1303 strbuf_addch(&sb
, buf
[i
]);
1305 return strbuf_detach(&sb
, NULL
);
1308 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
1310 const char *eoemail
= strstr(buf
, "> ");
1312 timestamp_t timestamp
;
1314 struct date_mode date_mode
= DATE_MODE_INIT
;
1315 const char *formatp
;
1318 * We got here because atomname ends in "date" or "date<something>";
1319 * it's not possible that <something> is not ":<format>" because
1320 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1321 * ":" means no format is specified, and use the default.
1323 formatp
= strchr(atomname
, ':');
1326 parse_date_format(formatp
, &date_mode
);
1331 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
1332 if (timestamp
== TIME_MAX
)
1334 tz
= strtol(zone
, NULL
, 10);
1335 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1337 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1338 v
->value
= timestamp
;
1339 date_mode_release(&date_mode
);
1346 /* See grab_values */
1347 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, void *buf
)
1350 int wholen
= strlen(who
);
1351 const char *wholine
= NULL
;
1353 for (i
= 0; i
< used_atom_cnt
; i
++) {
1354 const char *name
= used_atom
[i
].name
;
1355 struct atom_value
*v
= &val
[i
];
1356 if (!!deref
!= (*name
== '*'))
1360 if (strncmp(who
, name
, wholen
))
1362 if (name
[wholen
] != 0 &&
1363 strcmp(name
+ wholen
, "name") &&
1364 !starts_with(name
+ wholen
, "email") &&
1365 !starts_with(name
+ wholen
, "date"))
1368 wholine
= find_wholine(who
, wholen
, buf
);
1370 return; /* no point looking for it */
1371 if (name
[wholen
] == 0)
1372 v
->s
= copy_line(wholine
);
1373 else if (!strcmp(name
+ wholen
, "name"))
1374 v
->s
= copy_name(wholine
);
1375 else if (starts_with(name
+ wholen
, "email"))
1376 v
->s
= copy_email(wholine
, &used_atom
[i
]);
1377 else if (starts_with(name
+ wholen
, "date"))
1378 grab_date(wholine
, v
, name
);
1382 * For a tag or a commit object, if "creator" or "creatordate" is
1383 * requested, do something special.
1385 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1386 return; /* "author" for commit object is not wanted */
1388 wholine
= find_wholine(who
, wholen
, buf
);
1391 for (i
= 0; i
< used_atom_cnt
; i
++) {
1392 const char *name
= used_atom
[i
].name
;
1393 enum atom_type atom_type
= used_atom
[i
].atom_type
;
1394 struct atom_value
*v
= &val
[i
];
1395 if (!!deref
!= (*name
== '*'))
1400 if (atom_type
== ATOM_CREATORDATE
)
1401 grab_date(wholine
, v
, name
);
1402 else if (atom_type
== ATOM_CREATOR
)
1403 v
->s
= copy_line(wholine
);
1407 static void find_subpos(const char *buf
,
1408 const char **sub
, size_t *sublen
,
1409 const char **body
, size_t *bodylen
,
1411 const char **sig
, size_t *siglen
)
1413 struct strbuf payload
= STRBUF_INIT
;
1414 struct strbuf signature
= STRBUF_INIT
;
1416 const char *end
= buf
+ strlen(buf
);
1417 const char *sigstart
;
1419 /* parse signature first; we might not even have a subject line */
1420 parse_signature(buf
, end
- buf
, &payload
, &signature
);
1421 strbuf_release(&payload
);
1423 /* skip past header until we hit empty line */
1424 while (*buf
&& *buf
!= '\n') {
1425 eol
= strchrnul(buf
, '\n');
1430 /* skip any empty lines */
1431 while (*buf
== '\n')
1433 *sig
= strbuf_detach(&signature
, siglen
);
1434 sigstart
= buf
+ parse_signed_buffer(buf
, strlen(buf
));
1436 /* subject is first non-empty line */
1438 /* subject goes to first empty line before signature begins */
1439 if ((eol
= strstr(*sub
, "\n\n")) ||
1440 (eol
= strstr(*sub
, "\r\n\r\n"))) {
1441 eol
= eol
< sigstart
? eol
: sigstart
;
1443 /* treat whole message as subject */
1447 *sublen
= buf
- *sub
;
1448 /* drop trailing newline, if present */
1449 while (*sublen
&& ((*sub
)[*sublen
- 1] == '\n' ||
1450 (*sub
)[*sublen
- 1] == '\r'))
1453 /* skip any empty lines */
1454 while (*buf
== '\n' || *buf
== '\r')
1457 *bodylen
= strlen(buf
);
1458 *nonsiglen
= sigstart
- buf
;
1462 * If 'lines' is greater than 0, append that many lines from the given
1463 * 'buf' of length 'size' to the given strbuf.
1465 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1468 const char *sp
, *eol
;
1473 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1475 strbuf_addstr(out
, "\n ");
1476 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1477 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1478 strbuf_add(out
, sp
, len
);
1485 /* See grab_values */
1486 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct expand_data
*data
)
1489 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1490 size_t sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1491 void *buf
= data
->content
;
1493 for (i
= 0; i
< used_atom_cnt
; i
++) {
1494 struct used_atom
*atom
= &used_atom
[i
];
1495 const char *name
= atom
->name
;
1496 struct atom_value
*v
= &val
[i
];
1497 enum atom_type atom_type
= atom
->atom_type
;
1499 if (!!deref
!= (*name
== '*'))
1504 if (atom_type
== ATOM_RAW
) {
1505 unsigned long buf_size
= data
->size
;
1507 if (atom
->u
.raw_data
.option
== RAW_BARE
) {
1508 v
->s
= xmemdupz(buf
, buf_size
);
1509 v
->s_size
= buf_size
;
1510 } else if (atom
->u
.raw_data
.option
== RAW_LENGTH
) {
1511 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)buf_size
);
1516 if ((data
->type
!= OBJ_TAG
&&
1517 data
->type
!= OBJ_COMMIT
) ||
1518 (strcmp(name
, "body") &&
1519 !starts_with(name
, "subject") &&
1520 !starts_with(name
, "trailers") &&
1521 !starts_with(name
, "contents")))
1526 &bodypos
, &bodylen
, &nonsiglen
,
1529 if (atom
->u
.contents
.option
== C_SUB
)
1530 v
->s
= copy_subject(subpos
, sublen
);
1531 else if (atom
->u
.contents
.option
== C_SUB_SANITIZE
) {
1532 struct strbuf sb
= STRBUF_INIT
;
1533 format_sanitized_subject(&sb
, subpos
, sublen
);
1534 v
->s
= strbuf_detach(&sb
, NULL
);
1535 } else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1536 v
->s
= xmemdupz(bodypos
, bodylen
);
1537 else if (atom
->u
.contents
.option
== C_LENGTH
)
1538 v
->s
= xstrfmt("%"PRIuMAX
, (uintmax_t)strlen(subpos
));
1539 else if (atom
->u
.contents
.option
== C_BODY
)
1540 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1541 else if (atom
->u
.contents
.option
== C_SIG
)
1542 v
->s
= xmemdupz(sigpos
, siglen
);
1543 else if (atom
->u
.contents
.option
== C_LINES
) {
1544 struct strbuf s
= STRBUF_INIT
;
1545 const char *contents_end
= bodypos
+ nonsiglen
;
1547 /* Size is the length of the message after removing the signature */
1548 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1549 v
->s
= strbuf_detach(&s
, NULL
);
1550 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1551 struct strbuf s
= STRBUF_INIT
;
1553 /* Format the trailer info according to the trailer_opts given */
1554 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1556 v
->s
= strbuf_detach(&s
, NULL
);
1557 } else if (atom
->u
.contents
.option
== C_BARE
)
1558 v
->s
= xstrdup(subpos
);
1561 free((void *)sigpos
);
1565 * We want to have empty print-string for field requests
1566 * that do not apply (e.g. "authordate" for a tag object)
1568 static void fill_missing_values(struct atom_value
*val
)
1571 for (i
= 0; i
< used_atom_cnt
; i
++) {
1572 struct atom_value
*v
= &val
[i
];
1579 * val is a list of atom_value to hold returned values. Extract
1580 * the values for atoms in used_atom array out of (obj, buf, sz).
1581 * when deref is false, (obj, buf, sz) is the object that is
1582 * pointed at by the ref itself; otherwise it is the object the
1583 * ref (which is a tag) refers to.
1585 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, struct expand_data
*data
)
1587 void *buf
= data
->content
;
1589 switch (obj
->type
) {
1591 grab_tag_values(val
, deref
, obj
);
1592 grab_sub_body_contents(val
, deref
, data
);
1593 grab_person("tagger", val
, deref
, buf
);
1596 grab_commit_values(val
, deref
, obj
);
1597 grab_sub_body_contents(val
, deref
, data
);
1598 grab_person("author", val
, deref
, buf
);
1599 grab_person("committer", val
, deref
, buf
);
1602 /* grab_tree_values(val, deref, obj, buf, sz); */
1603 grab_sub_body_contents(val
, deref
, data
);
1606 /* grab_blob_values(val, deref, obj, buf, sz); */
1607 grab_sub_body_contents(val
, deref
, data
);
1610 die("Eh? Object of type %d?", obj
->type
);
1614 static inline char *copy_advance(char *dst
, const char *src
)
1621 static const char *lstrip_ref_components(const char *refname
, int len
)
1623 long remaining
= len
;
1624 const char *start
= xstrdup(refname
);
1625 const char *to_free
= start
;
1629 const char *p
= refname
;
1631 /* Find total no of '/' separated path-components */
1632 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1635 * The number of components we need to strip is now
1636 * the total minus the components to be left (Plus one
1637 * because we count the number of '/', but the number
1638 * of components is one more than the no of '/').
1640 remaining
= i
+ len
+ 1;
1643 while (remaining
> 0) {
1646 free((char *)to_free
);
1654 start
= xstrdup(start
);
1655 free((char *)to_free
);
1659 static const char *rstrip_ref_components(const char *refname
, int len
)
1661 long remaining
= len
;
1662 const char *start
= xstrdup(refname
);
1663 const char *to_free
= start
;
1667 const char *p
= refname
;
1669 /* Find total no of '/' separated path-components */
1670 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1673 * The number of components we need to strip is now
1674 * the total minus the components to be left (Plus one
1675 * because we count the number of '/', but the number
1676 * of components is one more than the no of '/').
1678 remaining
= i
+ len
+ 1;
1681 while (remaining
-- > 0) {
1682 char *p
= strrchr(start
, '/');
1684 free((char *)to_free
);
1692 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1694 if (atom
->option
== R_SHORT
)
1695 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1696 else if (atom
->option
== R_LSTRIP
)
1697 return lstrip_ref_components(refname
, atom
->lstrip
);
1698 else if (atom
->option
== R_RSTRIP
)
1699 return rstrip_ref_components(refname
, atom
->rstrip
);
1701 return xstrdup(refname
);
1704 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1705 struct branch
*branch
, const char **s
)
1707 int num_ours
, num_theirs
;
1708 if (atom
->u
.remote_ref
.option
== RR_REF
)
1709 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1710 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1711 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1712 NULL
, atom
->u
.remote_ref
.push
,
1713 AHEAD_BEHIND_FULL
) < 0) {
1714 *s
= xstrdup(msgs
.gone
);
1715 } else if (!num_ours
&& !num_theirs
)
1718 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1719 else if (!num_theirs
)
1720 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1722 *s
= xstrfmt(msgs
.ahead_behind
,
1723 num_ours
, num_theirs
);
1724 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1725 const char *to_free
= *s
;
1726 *s
= xstrfmt("[%s]", *s
);
1727 free((void *)to_free
);
1729 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1730 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1731 NULL
, atom
->u
.remote_ref
.push
,
1732 AHEAD_BEHIND_FULL
) < 0) {
1736 if (!num_ours
&& !num_theirs
)
1740 else if (!num_theirs
)
1744 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1746 const char *remote
= atom
->u
.remote_ref
.push
?
1747 pushremote_for_branch(branch
, &explicit) :
1748 remote_for_branch(branch
, &explicit);
1749 *s
= xstrdup(explicit ? remote
: "");
1750 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1753 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
);
1754 *s
= xstrdup(merge
? merge
: "");
1756 BUG("unhandled RR_* enum");
1759 char *get_head_description(void)
1761 struct strbuf desc
= STRBUF_INIT
;
1762 struct wt_status_state state
;
1763 memset(&state
, 0, sizeof(state
));
1764 wt_status_get_state(the_repository
, &state
, 1);
1765 if (state
.rebase_in_progress
||
1766 state
.rebase_interactive_in_progress
) {
1768 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1771 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
1772 state
.detached_from
);
1773 } else if (state
.bisect_in_progress
)
1774 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1776 else if (state
.detached_from
) {
1777 if (state
.detached_at
)
1778 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1779 state
.detached_from
);
1781 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1782 state
.detached_from
);
1784 strbuf_addstr(&desc
, _("(no branch)"));
1786 wt_status_state_free_buffers(&state
);
1788 return strbuf_detach(&desc
, NULL
);
1791 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1796 return show_ref(&atom
->u
.refname
, ref
->symref
);
1799 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1801 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1802 return get_head_description();
1803 return show_ref(&atom
->u
.refname
, ref
->refname
);
1806 static int get_object(struct ref_array_item
*ref
, int deref
, struct object
**obj
,
1807 struct expand_data
*oi
, struct strbuf
*err
)
1809 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1811 if (oi
->info
.contentp
) {
1812 /* We need to know that to use parse_object_buffer properly */
1813 oi
->info
.sizep
= &oi
->size
;
1814 oi
->info
.typep
= &oi
->type
;
1816 if (oid_object_info_extended(the_repository
, &oi
->oid
, &oi
->info
,
1817 OBJECT_INFO_LOOKUP_REPLACE
))
1818 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1819 oid_to_hex(&oi
->oid
), ref
->refname
);
1820 if (oi
->info
.disk_sizep
&& oi
->disk_size
< 0)
1821 BUG("Object size is less than zero.");
1823 if (oi
->info
.contentp
) {
1824 *obj
= parse_object_buffer(the_repository
, &oi
->oid
, oi
->type
, oi
->size
, oi
->content
, &eaten
);
1828 return strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1829 oid_to_hex(&oi
->oid
), ref
->refname
);
1831 grab_values(ref
->value
, deref
, *obj
, oi
);
1834 grab_common_values(ref
->value
, deref
, oi
);
1840 static void populate_worktree_map(struct hashmap
*map
, struct worktree
**worktrees
)
1844 for (i
= 0; worktrees
[i
]; i
++) {
1845 if (worktrees
[i
]->head_ref
) {
1846 struct ref_to_worktree_entry
*entry
;
1847 entry
= xmalloc(sizeof(*entry
));
1848 entry
->wt
= worktrees
[i
];
1849 hashmap_entry_init(&entry
->ent
,
1850 strhash(worktrees
[i
]->head_ref
));
1852 hashmap_add(map
, &entry
->ent
);
1857 static void lazy_init_worktree_map(void)
1859 if (ref_to_worktree_map
.worktrees
)
1862 ref_to_worktree_map
.worktrees
= get_worktrees();
1863 hashmap_init(&(ref_to_worktree_map
.map
), ref_to_worktree_map_cmpfnc
, NULL
, 0);
1864 populate_worktree_map(&(ref_to_worktree_map
.map
), ref_to_worktree_map
.worktrees
);
1867 static char *get_worktree_path(const struct ref_array_item
*ref
)
1869 struct hashmap_entry entry
, *e
;
1870 struct ref_to_worktree_entry
*lookup_result
;
1872 lazy_init_worktree_map();
1874 hashmap_entry_init(&entry
, strhash(ref
->refname
));
1875 e
= hashmap_get(&(ref_to_worktree_map
.map
), &entry
, ref
->refname
);
1880 lookup_result
= container_of(e
, struct ref_to_worktree_entry
, ent
);
1882 return xstrdup(lookup_result
->wt
->path
);
1886 * Parse the object referred by ref, and grab needed value.
1888 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
1892 struct object_info empty
= OBJECT_INFO_INIT
;
1893 int ahead_behind_atoms
= 0;
1895 CALLOC_ARRAY(ref
->value
, used_atom_cnt
);
1897 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1898 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1901 ref
->symref
= xstrdup("");
1904 /* Fill in specials first */
1905 for (i
= 0; i
< used_atom_cnt
; i
++) {
1906 struct used_atom
*atom
= &used_atom
[i
];
1907 enum atom_type atom_type
= atom
->atom_type
;
1908 const char *name
= used_atom
[i
].name
;
1909 struct atom_value
*v
= &ref
->value
[i
];
1911 const char *refname
;
1912 struct branch
*branch
= NULL
;
1914 v
->s_size
= ATOM_SIZE_UNSPECIFIED
;
1915 v
->handler
= append_atom
;
1923 if (atom_type
== ATOM_REFNAME
)
1924 refname
= get_refname(atom
, ref
);
1925 else if (atom_type
== ATOM_WORKTREEPATH
) {
1926 if (ref
->kind
== FILTER_REFS_BRANCHES
)
1927 v
->s
= get_worktree_path(ref
);
1932 else if (atom_type
== ATOM_SYMREF
)
1933 refname
= get_symref(atom
, ref
);
1934 else if (atom_type
== ATOM_UPSTREAM
) {
1935 const char *branch_name
;
1936 /* only local branches may have an upstream */
1937 if (!skip_prefix(ref
->refname
, "refs/heads/",
1942 branch
= branch_get(branch_name
);
1944 refname
= branch_get_upstream(branch
, NULL
);
1946 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1950 } else if (atom_type
== ATOM_PUSH
&& atom
->u
.remote_ref
.push
) {
1951 const char *branch_name
;
1953 if (!skip_prefix(ref
->refname
, "refs/heads/",
1956 branch
= branch_get(branch_name
);
1958 if (atom
->u
.remote_ref
.push_remote
)
1961 refname
= branch_get_push(branch
, NULL
);
1965 /* We will definitely re-init v->s on the next line. */
1967 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1969 } else if (atom_type
== ATOM_COLOR
) {
1970 v
->s
= xstrdup(atom
->u
.color
);
1972 } else if (atom_type
== ATOM_FLAG
) {
1973 char buf
[256], *cp
= buf
;
1974 if (ref
->flag
& REF_ISSYMREF
)
1975 cp
= copy_advance(cp
, ",symref");
1976 if (ref
->flag
& REF_ISPACKED
)
1977 cp
= copy_advance(cp
, ",packed");
1982 v
->s
= xstrdup(buf
+ 1);
1985 } else if (!deref
&& atom_type
== ATOM_OBJECTNAME
&&
1986 grab_oid(name
, "objectname", &ref
->objectname
, v
, atom
)) {
1988 } else if (atom_type
== ATOM_HEAD
) {
1989 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1990 v
->s
= xstrdup("*");
1992 v
->s
= xstrdup(" ");
1994 } else if (atom_type
== ATOM_ALIGN
) {
1995 v
->handler
= align_atom_handler
;
1998 } else if (atom_type
== ATOM_END
) {
1999 v
->handler
= end_atom_handler
;
2002 } else if (atom_type
== ATOM_IF
) {
2004 if (skip_prefix(name
, "if:", &s
))
2008 v
->handler
= if_atom_handler
;
2010 } else if (atom_type
== ATOM_THEN
) {
2011 v
->handler
= then_atom_handler
;
2014 } else if (atom_type
== ATOM_ELSE
) {
2015 v
->handler
= else_atom_handler
;
2018 } else if (atom_type
== ATOM_REST
) {
2020 v
->s
= xstrdup(ref
->rest
);
2024 } else if (atom_type
== ATOM_AHEADBEHIND
) {
2026 const struct ahead_behind_count
*count
;
2027 count
= ref
->counts
[ahead_behind_atoms
++];
2028 v
->s
= xstrfmt("%d %d", count
->ahead
, count
->behind
);
2038 v
->s
= xstrdup(refname
);
2040 v
->s
= xstrfmt("%s^{}", refname
);
2041 free((char *)refname
);
2044 for (i
= 0; i
< used_atom_cnt
; i
++) {
2045 struct atom_value
*v
= &ref
->value
[i
];
2046 if (v
->s
== NULL
&& used_atom
[i
].source
== SOURCE_NONE
)
2047 return strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
2048 oid_to_hex(&ref
->objectname
), ref
->refname
);
2052 oi
.info
.contentp
= &oi
.content
;
2053 if (!memcmp(&oi
.info
, &empty
, sizeof(empty
)) &&
2054 !memcmp(&oi_deref
.info
, &empty
, sizeof(empty
)))
2058 oi
.oid
= ref
->objectname
;
2059 if (get_object(ref
, 0, &obj
, &oi
, err
))
2063 * If there is no atom that wants to know about tagged
2064 * object, we are done.
2066 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
2070 * If it is a tag object, see if we use a value that derefs
2071 * the object, and if we do grab the object it refers to.
2073 oi_deref
.oid
= *get_tagged_oid((struct tag
*)obj
);
2076 * NEEDSWORK: This derefs tag only once, which
2077 * is good to deal with chains of trust, but
2078 * is not consistent with what deref_tag() does
2079 * which peels the onion to the core.
2081 return get_object(ref
, 1, &obj
, &oi_deref
, err
);
2085 * Given a ref, return the value for the atom. This lazily gets value
2086 * out of the object by calling populate value.
2088 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
2089 struct atom_value
**v
, struct strbuf
*err
)
2092 if (populate_value(ref
, err
))
2094 fill_missing_values(ref
->value
);
2096 *v
= &ref
->value
[atom
];
2101 * Return 1 if the refname matches one of the patterns, otherwise 0.
2102 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2103 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2104 * matches "refs/heads/mas*", too).
2106 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
2108 const char **patterns
= filter
->name_patterns
;
2111 if (filter
->ignore_case
)
2112 flags
|= WM_CASEFOLD
;
2115 * When no '--format' option is given we need to skip the prefix
2116 * for matching refs of tags and branches.
2118 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
2119 skip_prefix(refname
, "refs/heads/", &refname
) ||
2120 skip_prefix(refname
, "refs/remotes/", &refname
) ||
2121 skip_prefix(refname
, "refs/", &refname
));
2123 for (; *patterns
; patterns
++) {
2124 if (!wildmatch(*patterns
, refname
, flags
))
2131 * Return 1 if the refname matches one of the patterns, otherwise 0.
2132 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
2133 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2134 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2136 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
2138 const char **pattern
= filter
->name_patterns
;
2139 int namelen
= strlen(refname
);
2140 unsigned flags
= WM_PATHNAME
;
2142 if (filter
->ignore_case
)
2143 flags
|= WM_CASEFOLD
;
2145 for (; *pattern
; pattern
++) {
2146 const char *p
= *pattern
;
2147 int plen
= strlen(p
);
2149 if ((plen
<= namelen
) &&
2150 !strncmp(refname
, p
, plen
) &&
2151 (refname
[plen
] == '\0' ||
2152 refname
[plen
] == '/' ||
2155 if (!wildmatch(p
, refname
, flags
))
2161 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
2162 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
2164 if (!*filter
->name_patterns
)
2165 return 1; /* No pattern always matches */
2166 if (filter
->match_as_path
)
2167 return match_name_as_path(filter
, refname
);
2168 return match_pattern(filter
, refname
);
2172 * This is the same as for_each_fullref_in(), but it tries to iterate
2173 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2174 * pattern match, so the callback still has to match each ref individually.
2176 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
2180 if (!filter
->match_as_path
) {
2182 * in this case, the patterns are applied after
2183 * prefixes like "refs/heads/" etc. are stripped off,
2184 * so we have to look at everything:
2186 return for_each_fullref_in("", cb
, cb_data
);
2189 if (filter
->ignore_case
) {
2191 * we can't handle case-insensitive comparisons,
2192 * so just return everything and let the caller
2195 return for_each_fullref_in("", cb
, cb_data
);
2198 if (!filter
->name_patterns
[0]) {
2199 /* no patterns; we have to look at everything */
2200 return for_each_fullref_in("", cb
, cb_data
);
2203 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository
),
2204 NULL
, filter
->name_patterns
,
2209 * Given a ref (oid, refname), check if the ref belongs to the array
2210 * of oids. If the given ref is a tag, check if the given tag points
2211 * at one of the oids in the given oid array.
2213 * 1. Only a single level of indirection is obtained, we might want to
2214 * change this to account for multiple levels (e.g. annotated tags
2215 * pointing to annotated tags pointing to a commit.)
2216 * 2. As the refs are cached we might know what refname peels to without
2217 * the need to parse the object via parse_object(). peel_ref() might be a
2218 * more efficient alternative to obtain the pointee.
2220 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
2221 const struct object_id
*oid
,
2222 const char *refname
)
2224 const struct object_id
*tagged_oid
= NULL
;
2227 if (oid_array_lookup(points_at
, oid
) >= 0)
2229 obj
= parse_object(the_repository
, oid
);
2231 die(_("malformed object at '%s'"), refname
);
2232 if (obj
->type
== OBJ_TAG
)
2233 tagged_oid
= get_tagged_oid((struct tag
*)obj
);
2234 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
2240 * Allocate space for a new ref_array_item and copy the name and oid to it.
2242 * Callers can then fill in other struct members at their leisure.
2244 static struct ref_array_item
*new_ref_array_item(const char *refname
,
2245 const struct object_id
*oid
)
2247 struct ref_array_item
*ref
;
2249 FLEX_ALLOC_STR(ref
, refname
, refname
);
2250 oidcpy(&ref
->objectname
, oid
);
2256 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
2257 const char *refname
,
2258 const struct object_id
*oid
)
2260 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
2262 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
2263 array
->items
[array
->nr
++] = ref
;
2268 static int ref_kind_from_refname(const char *refname
)
2276 { "refs/heads/" , FILTER_REFS_BRANCHES
},
2277 { "refs/remotes/" , FILTER_REFS_REMOTES
},
2278 { "refs/tags/", FILTER_REFS_TAGS
}
2281 if (!strcmp(refname
, "HEAD"))
2282 return FILTER_REFS_DETACHED_HEAD
;
2284 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
2285 if (starts_with(refname
, ref_kind
[i
].prefix
))
2286 return ref_kind
[i
].kind
;
2289 return FILTER_REFS_OTHERS
;
2292 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
2294 if (filter
->kind
== FILTER_REFS_BRANCHES
||
2295 filter
->kind
== FILTER_REFS_REMOTES
||
2296 filter
->kind
== FILTER_REFS_TAGS
)
2297 return filter
->kind
;
2298 return ref_kind_from_refname(refname
);
2301 struct ref_filter_cbdata
{
2302 struct ref_array
*array
;
2303 struct ref_filter
*filter
;
2304 struct contains_cache contains_cache
;
2305 struct contains_cache no_contains_cache
;
2309 * A call-back given to for_each_ref(). Filter refs and keep them for
2310 * later object processing.
2312 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
2314 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
2315 struct ref_filter
*filter
= ref_cbdata
->filter
;
2316 struct ref_array_item
*ref
;
2317 struct commit
*commit
= NULL
;
2320 if (flag
& REF_BAD_NAME
) {
2321 warning(_("ignoring ref with broken name %s"), refname
);
2325 if (flag
& REF_ISBROKEN
) {
2326 warning(_("ignoring broken ref %s"), refname
);
2330 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2331 kind
= filter_ref_kind(filter
, refname
);
2332 if (!(kind
& filter
->kind
))
2335 if (!filter_pattern_match(filter
, refname
))
2338 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2342 * A merge filter is applied on refs pointing to commits. Hence
2343 * obtain the commit using the 'oid' available and discard all
2344 * non-commits early. The actual filtering is done later.
2346 if (filter
->reachable_from
|| filter
->unreachable_from
||
2347 filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2348 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
2351 /* We perform the filtering for the '--contains' option... */
2352 if (filter
->with_commit
&&
2353 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2355 /* ...or for the `--no-contains' option */
2356 if (filter
->no_commit
&&
2357 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2362 * We do not open the object yet; sort may only need refname
2363 * to do its job and the resulting list may yet to be pruned
2364 * by maxcount logic.
2366 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2367 ref
->commit
= commit
;
2374 /* Free memory allocated for a ref_array_item */
2375 static void free_array_item(struct ref_array_item
*item
)
2377 free((char *)item
->symref
);
2380 for (i
= 0; i
< used_atom_cnt
; i
++)
2381 free((char *)item
->value
[i
].s
);
2388 /* Free all memory allocated for ref_array */
2389 void ref_array_clear(struct ref_array
*array
)
2393 for (i
= 0; i
< array
->nr
; i
++)
2394 free_array_item(array
->items
[i
]);
2395 FREE_AND_NULL(array
->items
);
2396 array
->nr
= array
->alloc
= 0;
2398 for (i
= 0; i
< used_atom_cnt
; i
++) {
2399 struct used_atom
*atom
= &used_atom
[i
];
2400 if (atom
->atom_type
== ATOM_HEAD
)
2402 free((char *)atom
->name
);
2404 FREE_AND_NULL(used_atom
);
2407 if (ref_to_worktree_map
.worktrees
) {
2408 hashmap_clear_and_free(&(ref_to_worktree_map
.map
),
2409 struct ref_to_worktree_entry
, ent
);
2410 free_worktrees(ref_to_worktree_map
.worktrees
);
2411 ref_to_worktree_map
.worktrees
= NULL
;
2414 FREE_AND_NULL(array
->counts
);
2417 #define EXCLUDE_REACHED 0
2418 #define INCLUDE_REACHED 1
2419 static void reach_filter(struct ref_array
*array
,
2420 struct commit_list
*check_reachable
,
2421 int include_reached
)
2424 struct commit
**to_clear
;
2426 if (!check_reachable
)
2429 CALLOC_ARRAY(to_clear
, array
->nr
);
2430 for (i
= 0; i
< array
->nr
; i
++) {
2431 struct ref_array_item
*item
= array
->items
[i
];
2432 to_clear
[i
] = item
->commit
;
2435 tips_reachable_from_bases(the_repository
,
2437 to_clear
, array
->nr
,
2443 for (i
= 0; i
< old_nr
; i
++) {
2444 struct ref_array_item
*item
= array
->items
[i
];
2445 struct commit
*commit
= item
->commit
;
2447 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2449 if (is_merged
== include_reached
)
2450 array
->items
[array
->nr
++] = array
->items
[i
];
2452 free_array_item(item
);
2455 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2457 while (check_reachable
) {
2458 struct commit
*merge_commit
= pop_commit(&check_reachable
);
2459 clear_commit_marks(merge_commit
, ALL_REV_FLAGS
);
2465 void filter_ahead_behind(struct repository
*r
,
2466 struct ref_format
*format
,
2467 struct ref_array
*array
)
2469 struct commit
**commits
;
2470 size_t commits_nr
= format
->bases
.nr
+ array
->nr
;
2472 if (!format
->bases
.nr
|| !array
->nr
)
2475 ALLOC_ARRAY(commits
, commits_nr
);
2476 for (size_t i
= 0; i
< format
->bases
.nr
; i
++)
2477 commits
[i
] = format
->bases
.items
[i
].util
;
2479 ALLOC_ARRAY(array
->counts
, st_mult(format
->bases
.nr
, array
->nr
));
2481 commits_nr
= format
->bases
.nr
;
2482 array
->counts_nr
= 0;
2483 for (size_t i
= 0; i
< array
->nr
; i
++) {
2484 const char *name
= array
->items
[i
]->refname
;
2485 commits
[commits_nr
] = lookup_commit_reference_by_name(name
);
2487 if (!commits
[commits_nr
])
2490 CALLOC_ARRAY(array
->items
[i
]->counts
, format
->bases
.nr
);
2491 for (size_t j
= 0; j
< format
->bases
.nr
; j
++) {
2492 struct ahead_behind_count
*count
;
2493 count
= &array
->counts
[array
->counts_nr
++];
2494 count
->tip_index
= commits_nr
;
2495 count
->base_index
= j
;
2497 array
->items
[i
]->counts
[j
] = count
;
2502 ahead_behind(r
, commits
, commits_nr
, array
->counts
, array
->counts_nr
);
2507 * API for filtering a set of refs. Based on the type of refs the user
2508 * has requested, we iterate through those refs and apply filters
2509 * as per the given ref_filter structure and finally store the
2510 * filtered refs in the ref_array structure.
2512 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2514 struct ref_filter_cbdata ref_cbdata
;
2515 int save_commit_buffer_orig
;
2518 ref_cbdata
.array
= array
;
2519 ref_cbdata
.filter
= filter
;
2521 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2523 save_commit_buffer_orig
= save_commit_buffer
;
2524 save_commit_buffer
= 0;
2526 init_contains_cache(&ref_cbdata
.contains_cache
);
2527 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2529 /* Simple per-ref filtering */
2531 die("filter_refs: invalid type");
2534 * For common cases where we need only branches or remotes or tags,
2535 * we only iterate through those refs. If a mix of refs is needed,
2536 * we iterate over all refs and filter out required refs with the help
2537 * of filter_ref_kind().
2539 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2540 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
);
2541 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2542 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
);
2543 else if (filter
->kind
== FILTER_REFS_TAGS
)
2544 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
);
2545 else if (filter
->kind
& FILTER_REFS_ALL
)
2546 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
);
2547 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2548 head_ref(ref_filter_handler
, &ref_cbdata
);
2551 clear_contains_cache(&ref_cbdata
.contains_cache
);
2552 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2554 /* Filters that need revision walking */
2555 reach_filter(array
, filter
->reachable_from
, INCLUDE_REACHED
);
2556 reach_filter(array
, filter
->unreachable_from
, EXCLUDE_REACHED
);
2558 save_commit_buffer
= save_commit_buffer_orig
;
2562 static int compare_detached_head(struct ref_array_item
*a
, struct ref_array_item
*b
)
2564 if (!(a
->kind
^ b
->kind
))
2565 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
2566 if (a
->kind
& FILTER_REFS_DETACHED_HEAD
)
2568 else if (b
->kind
& FILTER_REFS_DETACHED_HEAD
)
2570 BUG("should have died in the xor check above");
2574 static int memcasecmp(const void *vs1
, const void *vs2
, size_t n
)
2576 const char *s1
= vs1
, *s2
= vs2
;
2577 const char *end
= s1
+ n
;
2579 for (; s1
< end
; s1
++, s2
++) {
2580 int diff
= tolower(*s1
) - tolower(*s2
);
2587 struct ref_sorting
{
2588 struct ref_sorting
*next
;
2589 int atom
; /* index into used_atom array (internal) */
2590 enum ref_sorting_order sort_flags
;
2593 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2595 struct atom_value
*va
, *vb
;
2597 int cmp_detached_head
= 0;
2598 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2599 struct strbuf err
= STRBUF_INIT
;
2601 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2603 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2605 strbuf_release(&err
);
2606 if (s
->sort_flags
& REF_SORTING_DETACHED_HEAD_FIRST
&&
2607 ((a
->kind
| b
->kind
) & FILTER_REFS_DETACHED_HEAD
)) {
2608 cmp
= compare_detached_head(a
, b
);
2609 cmp_detached_head
= 1;
2610 } else if (s
->sort_flags
& REF_SORTING_VERSION
) {
2611 cmp
= versioncmp(va
->s
, vb
->s
);
2612 } else if (cmp_type
== FIELD_STR
) {
2613 if (va
->s_size
< 0 && vb
->s_size
< 0) {
2614 int (*cmp_fn
)(const char *, const char *);
2615 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
2616 ? strcasecmp
: strcmp
;
2617 cmp
= cmp_fn(va
->s
, vb
->s
);
2619 size_t a_size
= va
->s_size
< 0 ?
2620 strlen(va
->s
) : va
->s_size
;
2621 size_t b_size
= vb
->s_size
< 0 ?
2622 strlen(vb
->s
) : vb
->s_size
;
2623 int (*cmp_fn
)(const void *, const void *, size_t);
2624 cmp_fn
= s
->sort_flags
& REF_SORTING_ICASE
2625 ? memcasecmp
: memcmp
;
2627 cmp
= cmp_fn(va
->s
, vb
->s
, b_size
> a_size
?
2630 if (a_size
> b_size
)
2632 else if (a_size
< b_size
)
2637 if (va
->value
< vb
->value
)
2639 else if (va
->value
== vb
->value
)
2645 return (s
->sort_flags
& REF_SORTING_REVERSE
&& !cmp_detached_head
)
2649 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2651 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2652 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2653 struct ref_sorting
*s
;
2655 for (s
= ref_sorting
; s
; s
= s
->next
) {
2656 int cmp
= cmp_ref_sorting(s
, a
, b
);
2661 return s
&& s
->sort_flags
& REF_SORTING_ICASE
?
2662 strcasecmp(a
->refname
, b
->refname
) :
2663 strcmp(a
->refname
, b
->refname
);
2666 void ref_sorting_set_sort_flags_all(struct ref_sorting
*sorting
,
2667 unsigned int mask
, int on
)
2669 for (; sorting
; sorting
= sorting
->next
) {
2671 sorting
->sort_flags
|= mask
;
2673 sorting
->sort_flags
&= ~mask
;
2677 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2679 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2682 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2684 struct strbuf
*s
= &state
->stack
->output
;
2686 while (*cp
&& (!ep
|| cp
< ep
)) {
2691 int ch
= hex2chr(cp
+ 1);
2693 strbuf_addch(s
, ch
);
2699 strbuf_addch(s
, *cp
);
2704 int format_ref_array_item(struct ref_array_item
*info
,
2705 struct ref_format
*format
,
2706 struct strbuf
*final_buf
,
2707 struct strbuf
*error_buf
)
2709 const char *cp
, *sp
, *ep
;
2710 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2712 state
.quote_style
= format
->quote_style
;
2713 push_stack_element(&state
.stack
);
2715 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2716 struct atom_value
*atomv
;
2719 ep
= strchr(sp
, ')');
2721 append_literal(cp
, sp
, &state
);
2722 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2723 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2724 atomv
->handler(atomv
, &state
, error_buf
)) {
2725 pop_stack_element(&state
.stack
);
2730 sp
= cp
+ strlen(cp
);
2731 append_literal(cp
, sp
, &state
);
2733 if (format
->need_color_reset_at_eol
) {
2734 struct atom_value resetv
= ATOM_VALUE_INIT
;
2735 resetv
.s
= GIT_COLOR_RESET
;
2736 if (append_atom(&resetv
, &state
, error_buf
)) {
2737 pop_stack_element(&state
.stack
);
2741 if (state
.stack
->prev
) {
2742 pop_stack_element(&state
.stack
);
2743 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2745 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2746 pop_stack_element(&state
.stack
);
2750 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2751 struct ref_format
*format
)
2753 struct ref_array_item
*ref_item
;
2754 struct strbuf output
= STRBUF_INIT
;
2755 struct strbuf err
= STRBUF_INIT
;
2757 ref_item
= new_ref_array_item(name
, oid
);
2758 ref_item
->kind
= ref_kind_from_refname(name
);
2759 if (format_ref_array_item(ref_item
, format
, &output
, &err
))
2761 fwrite(output
.buf
, 1, output
.len
, stdout
);
2764 strbuf_release(&err
);
2765 strbuf_release(&output
);
2766 free_array_item(ref_item
);
2769 static int parse_sorting_atom(const char *atom
)
2772 * This parses an atom using a dummy ref_format, since we don't
2773 * actually care about the formatting details.
2775 struct ref_format dummy
= REF_FORMAT_INIT
;
2776 const char *end
= atom
+ strlen(atom
);
2777 struct strbuf err
= STRBUF_INIT
;
2778 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2781 strbuf_release(&err
);
2785 /* If no sorting option is given, use refname to sort as default */
2786 static struct ref_sorting
*ref_default_sorting(void)
2788 static const char cstr_name
[] = "refname";
2790 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2792 sorting
->next
= NULL
;
2793 sorting
->atom
= parse_sorting_atom(cstr_name
);
2797 static void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2799 struct ref_sorting
*s
;
2802 s
->next
= *sorting_tail
;
2806 s
->sort_flags
|= REF_SORTING_REVERSE
;
2809 if (skip_prefix(arg
, "version:", &arg
) ||
2810 skip_prefix(arg
, "v:", &arg
))
2811 s
->sort_flags
|= REF_SORTING_VERSION
;
2812 s
->atom
= parse_sorting_atom(arg
);
2815 struct ref_sorting
*ref_sorting_options(struct string_list
*options
)
2817 struct string_list_item
*item
;
2818 struct ref_sorting
*sorting
= NULL
, **tail
= &sorting
;
2821 sorting
= ref_default_sorting();
2823 for_each_string_list_item(item
, options
)
2824 parse_ref_sorting(tail
, item
->string
);
2828 * From here on, the ref_sorting list should be used to talk
2829 * about the sort order used for the output. The caller
2830 * should not touch the string form anymore.
2832 string_list_clear(options
, 0);
2836 void ref_sorting_release(struct ref_sorting
*sorting
)
2839 struct ref_sorting
*next
= sorting
->next
;
2845 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2847 struct ref_filter
*rf
= opt
->value
;
2848 struct object_id oid
;
2849 struct commit
*merge_commit
;
2851 BUG_ON_OPT_NEG(unset
);
2853 if (repo_get_oid(the_repository
, arg
, &oid
))
2854 die(_("malformed object name %s"), arg
);
2856 merge_commit
= lookup_commit_reference_gently(the_repository
, &oid
, 0);
2859 return error(_("option `%s' must point to a commit"), opt
->long_name
);
2861 if (starts_with(opt
->long_name
, "no"))
2862 commit_list_insert(merge_commit
, &rf
->unreachable_from
);
2864 commit_list_insert(merge_commit
, &rf
->reachable_from
);