3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
17 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
20 * An atom is a valid field atom listed below, possibly prefixed with
21 * a "*" to denote deref_tag().
23 * We parse given format string and sort specifiers, and make a list
24 * of properties that we need to extract out of objects. ref_array_item
25 * structure will hold an array of values extracted that can be
26 * indexed with the "atom number", which is an index into this
29 static struct used_atom
{
33 static int used_atom_cnt
, need_tagged
, need_symref
;
34 static int need_color_reset_at_eol
;
42 { "objectsize", FIELD_ULONG
},
46 { "numparent", FIELD_ULONG
},
53 { "authordate", FIELD_TIME
},
57 { "committerdate", FIELD_TIME
},
61 { "taggerdate", FIELD_TIME
},
63 { "creatordate", FIELD_TIME
},
77 #define REF_FORMATTING_STATE_INIT { 0, NULL }
89 struct ref_formatting_stack
{
90 struct ref_formatting_stack
*prev
;
92 void (*at_end
)(struct ref_formatting_stack
*stack
);
96 struct ref_formatting_state
{
98 struct ref_formatting_stack
*stack
;
105 struct contents contents
;
107 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
108 unsigned long ul
; /* used for sorting when not FIELD_STR */
112 * Used to parse format string and sort specifiers
114 int parse_ref_filter_atom(const char *atom
, const char *ep
)
120 if (*sp
== '*' && sp
< ep
)
123 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
125 /* Do we have the atom already used elsewhere? */
126 for (i
= 0; i
< used_atom_cnt
; i
++) {
127 int len
= strlen(used_atom
[i
].name
);
128 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
132 /* Is the atom a valid one? */
133 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
134 int len
= strlen(valid_atom
[i
].name
);
136 * If the atom name has a colon, strip it and everything after
137 * it off - it specifies the format for this entry, and
138 * shouldn't be used for checking against the valid_atom
141 const char *formatp
= strchr(sp
, ':');
142 if (!formatp
|| ep
< formatp
)
144 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
148 if (ARRAY_SIZE(valid_atom
) <= i
)
149 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
151 /* Add it in, including the deref prefix */
154 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
155 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
156 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
159 if (!strcmp(used_atom
[at
].name
, "symref"))
164 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
166 switch (quote_style
) {
168 strbuf_addstr(s
, str
);
171 sq_quote_buf(s
, str
);
174 perl_quote_buf(s
, str
);
177 python_quote_buf(s
, str
);
180 tcl_quote_buf(s
, str
);
185 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
188 * Quote formatting is only done when the stack has a single
189 * element. Otherwise quote formatting is done on the
190 * element's entire output strbuf when the %(end) atom is
193 if (!state
->stack
->prev
)
194 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
196 strbuf_addstr(&state
->stack
->output
, v
->s
);
199 static void push_stack_element(struct ref_formatting_stack
**stack
)
201 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
203 strbuf_init(&s
->output
, 0);
208 static void pop_stack_element(struct ref_formatting_stack
**stack
)
210 struct ref_formatting_stack
*current
= *stack
;
211 struct ref_formatting_stack
*prev
= current
->prev
;
214 strbuf_addbuf(&prev
->output
, ¤t
->output
);
215 strbuf_release(¤t
->output
);
220 static void end_align_handler(struct ref_formatting_stack
*stack
)
222 struct align
*align
= (struct align
*)stack
->at_end_data
;
223 struct strbuf s
= STRBUF_INIT
;
225 strbuf_utf8_align(&s
, align
->position
, align
->width
, stack
->output
.buf
);
226 strbuf_swap(&stack
->output
, &s
);
230 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
232 struct ref_formatting_stack
*new;
234 push_stack_element(&state
->stack
);
236 new->at_end
= end_align_handler
;
237 new->at_end_data
= &atomv
->u
.align
;
240 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
242 struct ref_formatting_stack
*current
= state
->stack
;
243 struct strbuf s
= STRBUF_INIT
;
245 if (!current
->at_end
)
246 die(_("format: %%(end) atom used without corresponding atom"));
247 current
->at_end(current
);
250 * Perform quote formatting when the stack element is that of
251 * a supporting atom. If nested then perform quote formatting
252 * only on the topmost supporting atom.
254 if (!state
->stack
->prev
->prev
) {
255 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
256 strbuf_swap(¤t
->output
, &s
);
259 pop_stack_element(&state
->stack
);
262 static int match_atom_name(const char *name
, const char *atom_name
, const char **val
)
266 if (!skip_prefix(name
, atom_name
, &body
))
267 return 0; /* doesn't even begin with "atom_name" */
269 *val
= NULL
; /* %(atom_name) and no customization */
273 return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
274 *val
= body
+ 1; /* "atom_name:val" */
279 * In a format string, find the next occurrence of %(atom).
281 static const char *find_next(const char *cp
)
286 * %( is the start of an atom;
287 * %% is a quoted per-cent.
291 else if (cp
[1] == '%')
292 cp
++; /* skip over two % */
293 /* otherwise this is a singleton, literal % */
301 * Make sure the format string is well formed, and parse out
304 int verify_ref_format(const char *format
)
308 need_color_reset_at_eol
= 0;
309 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
310 const char *color
, *ep
= strchr(sp
, ')');
314 return error("malformed format string %s", sp
);
315 /* sp points at "%(" and ep points at the closing ")" */
316 at
= parse_ref_filter_atom(sp
+ 2, ep
);
319 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
320 need_color_reset_at_eol
= !!strcmp(color
, "reset");
326 * Given an object name, read the object data and size, and return a
327 * "struct object". If the object data we are returning is also borrowed
328 * by the "struct object" representation, set *eaten as well---it is a
329 * signal from parse_object_buffer to us not to free the buffer.
331 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
333 enum object_type type
;
334 void *buf
= read_sha1_file(sha1
, &type
, sz
);
337 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
343 static int grab_objectname(const char *name
, const unsigned char *sha1
,
344 struct atom_value
*v
)
346 if (!strcmp(name
, "objectname")) {
347 v
->s
= xstrdup(sha1_to_hex(sha1
));
350 if (!strcmp(name
, "objectname:short")) {
351 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
357 /* See grab_values */
358 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
362 for (i
= 0; i
< used_atom_cnt
; i
++) {
363 const char *name
= used_atom
[i
].name
;
364 struct atom_value
*v
= &val
[i
];
365 if (!!deref
!= (*name
== '*'))
369 if (!strcmp(name
, "objecttype"))
370 v
->s
= typename(obj
->type
);
371 else if (!strcmp(name
, "objectsize")) {
373 v
->s
= xstrfmt("%lu", sz
);
376 grab_objectname(name
, obj
->oid
.hash
, v
);
380 /* See grab_values */
381 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
384 struct tag
*tag
= (struct tag
*) obj
;
386 for (i
= 0; i
< used_atom_cnt
; i
++) {
387 const char *name
= used_atom
[i
].name
;
388 struct atom_value
*v
= &val
[i
];
389 if (!!deref
!= (*name
== '*'))
393 if (!strcmp(name
, "tag"))
395 else if (!strcmp(name
, "type") && tag
->tagged
)
396 v
->s
= typename(tag
->tagged
->type
);
397 else if (!strcmp(name
, "object") && tag
->tagged
)
398 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
402 /* See grab_values */
403 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
406 struct commit
*commit
= (struct commit
*) obj
;
408 for (i
= 0; i
< used_atom_cnt
; i
++) {
409 const char *name
= used_atom
[i
].name
;
410 struct atom_value
*v
= &val
[i
];
411 if (!!deref
!= (*name
== '*'))
415 if (!strcmp(name
, "tree")) {
416 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
418 else if (!strcmp(name
, "numparent")) {
419 v
->ul
= commit_list_count(commit
->parents
);
420 v
->s
= xstrfmt("%lu", v
->ul
);
422 else if (!strcmp(name
, "parent")) {
423 struct commit_list
*parents
;
424 struct strbuf s
= STRBUF_INIT
;
425 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
426 struct commit
*parent
= parents
->item
;
427 if (parents
!= commit
->parents
)
428 strbuf_addch(&s
, ' ');
429 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
431 v
->s
= strbuf_detach(&s
, NULL
);
436 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
440 if (!strncmp(buf
, who
, wholen
) &&
442 return buf
+ wholen
+ 1;
443 eol
= strchr(buf
, '\n');
448 return ""; /* end of header */
454 static const char *copy_line(const char *buf
)
456 const char *eol
= strchrnul(buf
, '\n');
457 return xmemdupz(buf
, eol
- buf
);
460 static const char *copy_name(const char *buf
)
463 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
464 if (!strncmp(cp
, " <", 2))
465 return xmemdupz(buf
, cp
- buf
);
470 static const char *copy_email(const char *buf
)
472 const char *email
= strchr(buf
, '<');
476 eoemail
= strchr(email
, '>');
479 return xmemdupz(email
, eoemail
+ 1 - email
);
482 static char *copy_subject(const char *buf
, unsigned long len
)
484 char *r
= xmemdupz(buf
, len
);
487 for (i
= 0; i
< len
; i
++)
494 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
496 const char *eoemail
= strstr(buf
, "> ");
498 unsigned long timestamp
;
500 struct date_mode date_mode
= { DATE_NORMAL
};
504 * We got here because atomname ends in "date" or "date<something>";
505 * it's not possible that <something> is not ":<format>" because
506 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
507 * ":" means no format is specified, and use the default.
509 formatp
= strchr(atomname
, ':');
510 if (formatp
!= NULL
) {
512 parse_date_format(formatp
, &date_mode
);
517 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
518 if (timestamp
== ULONG_MAX
)
520 tz
= strtol(zone
, NULL
, 10);
521 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
523 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
531 /* See grab_values */
532 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
535 int wholen
= strlen(who
);
536 const char *wholine
= NULL
;
538 for (i
= 0; i
< used_atom_cnt
; i
++) {
539 const char *name
= used_atom
[i
].name
;
540 struct atom_value
*v
= &val
[i
];
541 if (!!deref
!= (*name
== '*'))
545 if (strncmp(who
, name
, wholen
))
547 if (name
[wholen
] != 0 &&
548 strcmp(name
+ wholen
, "name") &&
549 strcmp(name
+ wholen
, "email") &&
550 !starts_with(name
+ wholen
, "date"))
553 wholine
= find_wholine(who
, wholen
, buf
, sz
);
555 return; /* no point looking for it */
556 if (name
[wholen
] == 0)
557 v
->s
= copy_line(wholine
);
558 else if (!strcmp(name
+ wholen
, "name"))
559 v
->s
= copy_name(wholine
);
560 else if (!strcmp(name
+ wholen
, "email"))
561 v
->s
= copy_email(wholine
);
562 else if (starts_with(name
+ wholen
, "date"))
563 grab_date(wholine
, v
, name
);
567 * For a tag or a commit object, if "creator" or "creatordate" is
568 * requested, do something special.
570 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
571 return; /* "author" for commit object is not wanted */
573 wholine
= find_wholine(who
, wholen
, buf
, sz
);
576 for (i
= 0; i
< used_atom_cnt
; i
++) {
577 const char *name
= used_atom
[i
].name
;
578 struct atom_value
*v
= &val
[i
];
579 if (!!deref
!= (*name
== '*'))
584 if (starts_with(name
, "creatordate"))
585 grab_date(wholine
, v
, name
);
586 else if (!strcmp(name
, "creator"))
587 v
->s
= copy_line(wholine
);
591 static void find_subpos(const char *buf
, unsigned long sz
,
592 const char **sub
, unsigned long *sublen
,
593 const char **body
, unsigned long *bodylen
,
594 unsigned long *nonsiglen
,
595 const char **sig
, unsigned long *siglen
)
598 /* skip past header until we hit empty line */
599 while (*buf
&& *buf
!= '\n') {
600 eol
= strchrnul(buf
, '\n');
605 /* skip any empty lines */
609 /* parse signature first; we might not even have a subject line */
610 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
611 *siglen
= strlen(*sig
);
613 /* subject is first non-empty line */
615 /* subject goes to first empty line */
616 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
617 eol
= strchrnul(buf
, '\n');
622 *sublen
= buf
- *sub
;
623 /* drop trailing newline, if present */
624 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
627 /* skip any empty lines */
631 *bodylen
= strlen(buf
);
632 *nonsiglen
= *sig
- buf
;
636 * If 'lines' is greater than 0, append that many lines from the given
637 * 'buf' of length 'size' to the given strbuf.
639 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
642 const char *sp
, *eol
;
647 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
649 strbuf_addstr(out
, "\n ");
650 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
651 len
= eol
? eol
- sp
: size
- (sp
- buf
);
652 strbuf_add(out
, sp
, len
);
659 /* See grab_values */
660 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
663 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
664 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
666 for (i
= 0; i
< used_atom_cnt
; i
++) {
667 const char *name
= used_atom
[i
].name
;
668 struct atom_value
*v
= &val
[i
];
669 const char *valp
= NULL
;
670 if (!!deref
!= (*name
== '*'))
674 if (strcmp(name
, "subject") &&
675 strcmp(name
, "body") &&
676 strcmp(name
, "contents") &&
677 strcmp(name
, "contents:subject") &&
678 strcmp(name
, "contents:body") &&
679 strcmp(name
, "contents:signature") &&
680 !starts_with(name
, "contents:lines="))
685 &bodypos
, &bodylen
, &nonsiglen
,
688 if (!strcmp(name
, "subject"))
689 v
->s
= copy_subject(subpos
, sublen
);
690 else if (!strcmp(name
, "contents:subject"))
691 v
->s
= copy_subject(subpos
, sublen
);
692 else if (!strcmp(name
, "body"))
693 v
->s
= xmemdupz(bodypos
, bodylen
);
694 else if (!strcmp(name
, "contents:body"))
695 v
->s
= xmemdupz(bodypos
, nonsiglen
);
696 else if (!strcmp(name
, "contents:signature"))
697 v
->s
= xmemdupz(sigpos
, siglen
);
698 else if (!strcmp(name
, "contents"))
699 v
->s
= xstrdup(subpos
);
700 else if (skip_prefix(name
, "contents:lines=", &valp
)) {
701 struct strbuf s
= STRBUF_INIT
;
702 const char *contents_end
= bodylen
+ bodypos
- siglen
;
704 if (strtoul_ui(valp
, 10, &v
->u
.contents
.lines
))
705 die(_("positive value expected contents:lines=%s"), valp
);
706 /* Size is the length of the message after removing the signature */
707 append_lines(&s
, subpos
, contents_end
- subpos
, v
->u
.contents
.lines
);
708 v
->s
= strbuf_detach(&s
, NULL
);
714 * We want to have empty print-string for field requests
715 * that do not apply (e.g. "authordate" for a tag object)
717 static void fill_missing_values(struct atom_value
*val
)
720 for (i
= 0; i
< used_atom_cnt
; i
++) {
721 struct atom_value
*v
= &val
[i
];
728 * val is a list of atom_value to hold returned values. Extract
729 * the values for atoms in used_atom array out of (obj, buf, sz).
730 * when deref is false, (obj, buf, sz) is the object that is
731 * pointed at by the ref itself; otherwise it is the object the
732 * ref (which is a tag) refers to.
734 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
736 grab_common_values(val
, deref
, obj
, buf
, sz
);
739 grab_tag_values(val
, deref
, obj
, buf
, sz
);
740 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
741 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
744 grab_commit_values(val
, deref
, obj
, buf
, sz
);
745 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
746 grab_person("author", val
, deref
, obj
, buf
, sz
);
747 grab_person("committer", val
, deref
, obj
, buf
, sz
);
750 /* grab_tree_values(val, deref, obj, buf, sz); */
753 /* grab_blob_values(val, deref, obj, buf, sz); */
756 die("Eh? Object of type %d?", obj
->type
);
760 static inline char *copy_advance(char *dst
, const char *src
)
767 static const char *strip_ref_components(const char *refname
, const char *nr_arg
)
770 long nr
= strtol(nr_arg
, &end
, 10);
772 const char *start
= refname
;
774 if (nr
< 1 || *end
!= '\0')
775 die(":strip= requires a positive integer argument");
780 die("ref '%s' does not have %ld components to :strip",
791 * Parse the object referred by ref, and grab needed value.
793 static void populate_value(struct ref_array_item
*ref
)
799 const unsigned char *tagged
;
801 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
803 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
804 unsigned char unused1
[20];
805 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
811 /* Fill in specials first */
812 for (i
= 0; i
< used_atom_cnt
; i
++) {
813 const char *name
= used_atom
[i
].name
;
814 struct atom_value
*v
= &ref
->value
[i
];
819 struct branch
*branch
= NULL
;
821 v
->handler
= append_atom
;
828 if (starts_with(name
, "refname"))
829 refname
= ref
->refname
;
830 else if (starts_with(name
, "symref"))
831 refname
= ref
->symref
? ref
->symref
: "";
832 else if (starts_with(name
, "upstream")) {
833 const char *branch_name
;
834 /* only local branches may have an upstream */
835 if (!skip_prefix(ref
->refname
, "refs/heads/",
838 branch
= branch_get(branch_name
);
840 refname
= branch_get_upstream(branch
, NULL
);
843 } else if (starts_with(name
, "push")) {
844 const char *branch_name
;
845 if (!skip_prefix(ref
->refname
, "refs/heads/",
848 branch
= branch_get(branch_name
);
850 refname
= branch_get_push(branch
, NULL
);
853 } else if (match_atom_name(name
, "color", &valp
)) {
854 char color
[COLOR_MAXLEN
] = "";
857 die(_("expected format: %%(color:<color>)"));
858 if (color_parse(valp
, color
) < 0)
859 die(_("unable to parse format"));
860 v
->s
= xstrdup(color
);
862 } else if (!strcmp(name
, "flag")) {
863 char buf
[256], *cp
= buf
;
864 if (ref
->flag
& REF_ISSYMREF
)
865 cp
= copy_advance(cp
, ",symref");
866 if (ref
->flag
& REF_ISPACKED
)
867 cp
= copy_advance(cp
, ",packed");
872 v
->s
= xstrdup(buf
+ 1);
875 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
877 } else if (!strcmp(name
, "HEAD")) {
879 unsigned char sha1
[20];
881 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
883 if (!strcmp(ref
->refname
, head
))
888 } else if (match_atom_name(name
, "align", &valp
)) {
889 struct align
*align
= &v
->u
.align
;
890 struct string_list params
= STRING_LIST_INIT_DUP
;
895 die(_("expected format: %%(align:<width>,<position>)"));
897 align
->position
= ALIGN_LEFT
;
899 string_list_split(¶ms
, valp
, ',', -1);
900 for (i
= 0; i
< params
.nr
; i
++) {
901 const char *s
= params
.items
[i
].string
;
902 if (!strtoul_ui(s
, 10, (unsigned int *)&width
))
904 else if (!strcmp(s
, "left"))
905 align
->position
= ALIGN_LEFT
;
906 else if (!strcmp(s
, "right"))
907 align
->position
= ALIGN_RIGHT
;
908 else if (!strcmp(s
, "middle"))
909 align
->position
= ALIGN_MIDDLE
;
911 die(_("improper format entered align:%s"), s
);
915 die(_("positive width expected with the %%(align) atom"));
916 align
->width
= width
;
917 string_list_clear(¶ms
, 0);
918 v
->handler
= align_atom_handler
;
920 } else if (!strcmp(name
, "end")) {
921 v
->handler
= end_atom_handler
;
926 formatp
= strchr(name
, ':');
928 int num_ours
, num_theirs
;
932 if (!strcmp(formatp
, "short"))
933 refname
= shorten_unambiguous_ref(refname
,
934 warn_ambiguous_refs
);
935 else if (skip_prefix(formatp
, "strip=", &arg
))
936 refname
= strip_ref_components(refname
, arg
);
937 else if (!strcmp(formatp
, "track") &&
938 (starts_with(name
, "upstream") ||
939 starts_with(name
, "push"))) {
941 if (stat_tracking_info(branch
, &num_ours
,
945 if (!num_ours
&& !num_theirs
)
948 v
->s
= xstrfmt("[behind %d]", num_theirs
);
949 else if (!num_theirs
)
950 v
->s
= xstrfmt("[ahead %d]", num_ours
);
952 v
->s
= xstrfmt("[ahead %d, behind %d]",
953 num_ours
, num_theirs
);
955 } else if (!strcmp(formatp
, "trackshort") &&
956 (starts_with(name
, "upstream") ||
957 starts_with(name
, "push"))) {
960 if (stat_tracking_info(branch
, &num_ours
,
964 if (!num_ours
&& !num_theirs
)
968 else if (!num_theirs
)
974 die("unknown %.*s format %s",
975 (int)(formatp
- name
), name
, formatp
);
981 v
->s
= xstrfmt("%s^{}", refname
);
984 for (i
= 0; i
< used_atom_cnt
; i
++) {
985 struct atom_value
*v
= &ref
->value
[i
];
992 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
994 die("missing object %s for %s",
995 sha1_to_hex(ref
->objectname
), ref
->refname
);
997 die("parse_object_buffer failed on %s for %s",
998 sha1_to_hex(ref
->objectname
), ref
->refname
);
1000 grab_values(ref
->value
, 0, obj
, buf
, size
);
1005 * If there is no atom that wants to know about tagged
1006 * object, we are done.
1008 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1012 * If it is a tag object, see if we use a value that derefs
1013 * the object, and if we do grab the object it refers to.
1015 tagged
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1018 * NEEDSWORK: This derefs tag only once, which
1019 * is good to deal with chains of trust, but
1020 * is not consistent with what deref_tag() does
1021 * which peels the onion to the core.
1023 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1025 die("missing object %s for %s",
1026 sha1_to_hex(tagged
), ref
->refname
);
1028 die("parse_object_buffer failed on %s for %s",
1029 sha1_to_hex(tagged
), ref
->refname
);
1030 grab_values(ref
->value
, 1, obj
, buf
, size
);
1036 * Given a ref, return the value for the atom. This lazily gets value
1037 * out of the object by calling populate value.
1039 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1042 populate_value(ref
);
1043 fill_missing_values(ref
->value
);
1045 *v
= &ref
->value
[atom
];
1048 enum contains_result
{
1049 CONTAINS_UNKNOWN
= -1,
1055 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1058 * At each recursion step, the stack items points to the commits whose
1059 * ancestors are to be inspected.
1061 struct contains_stack
{
1063 struct contains_stack_entry
{
1064 struct commit
*commit
;
1065 struct commit_list
*parents
;
1069 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1071 for (; want
; want
= want
->next
)
1072 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1078 * Test whether the candidate or one of its parents is contained in the list.
1079 * Do not recurse to find out, though, but return -1 if inconclusive.
1081 static enum contains_result
contains_test(struct commit
*candidate
,
1082 const struct commit_list
*want
)
1084 /* was it previously marked as containing a want commit? */
1085 if (candidate
->object
.flags
& TMP_MARK
)
1087 /* or marked as not possibly containing a want commit? */
1088 if (candidate
->object
.flags
& UNINTERESTING
)
1091 if (in_commit_list(want
, candidate
)) {
1092 candidate
->object
.flags
|= TMP_MARK
;
1096 if (parse_commit(candidate
) < 0)
1102 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1104 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1105 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1106 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1109 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1110 const struct commit_list
*want
)
1112 struct contains_stack contains_stack
= { 0, 0, NULL
};
1113 int result
= contains_test(candidate
, want
);
1115 if (result
!= CONTAINS_UNKNOWN
)
1118 push_to_contains_stack(candidate
, &contains_stack
);
1119 while (contains_stack
.nr
) {
1120 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1121 struct commit
*commit
= entry
->commit
;
1122 struct commit_list
*parents
= entry
->parents
;
1125 commit
->object
.flags
|= UNINTERESTING
;
1126 contains_stack
.nr
--;
1129 * If we just popped the stack, parents->item has been marked,
1130 * therefore contains_test will return a meaningful 0 or 1.
1132 else switch (contains_test(parents
->item
, want
)) {
1134 commit
->object
.flags
|= TMP_MARK
;
1135 contains_stack
.nr
--;
1138 entry
->parents
= parents
->next
;
1140 case CONTAINS_UNKNOWN
:
1141 push_to_contains_stack(parents
->item
, &contains_stack
);
1145 free(contains_stack
.contains_stack
);
1146 return contains_test(candidate
, want
);
1149 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1151 if (filter
->with_commit_tag_algo
)
1152 return contains_tag_algo(commit
, filter
->with_commit
);
1153 return is_descendant_of(commit
, filter
->with_commit
);
1157 * Return 1 if the refname matches one of the patterns, otherwise 0.
1158 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1159 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1160 * matches "refs/heads/mas*", too).
1162 static int match_pattern(const char **patterns
, const char *refname
)
1165 * When no '--format' option is given we need to skip the prefix
1166 * for matching refs of tags and branches.
1168 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1169 skip_prefix(refname
, "refs/heads/", &refname
) ||
1170 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1171 skip_prefix(refname
, "refs/", &refname
));
1173 for (; *patterns
; patterns
++) {
1174 if (!wildmatch(*patterns
, refname
, 0, NULL
))
1181 * Return 1 if the refname matches one of the patterns, otherwise 0.
1182 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1183 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1184 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1186 static int match_name_as_path(const char **pattern
, const char *refname
)
1188 int namelen
= strlen(refname
);
1189 for (; *pattern
; pattern
++) {
1190 const char *p
= *pattern
;
1191 int plen
= strlen(p
);
1193 if ((plen
<= namelen
) &&
1194 !strncmp(refname
, p
, plen
) &&
1195 (refname
[plen
] == '\0' ||
1196 refname
[plen
] == '/' ||
1199 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1205 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1206 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1208 if (!*filter
->name_patterns
)
1209 return 1; /* No pattern always matches */
1210 if (filter
->match_as_path
)
1211 return match_name_as_path(filter
->name_patterns
, refname
);
1212 return match_pattern(filter
->name_patterns
, refname
);
1216 * Given a ref (sha1, refname), check if the ref belongs to the array
1217 * of sha1s. If the given ref is a tag, check if the given tag points
1218 * at one of the sha1s in the given sha1 array.
1219 * the given sha1_array.
1221 * 1. Only a single level of inderection is obtained, we might want to
1222 * change this to account for multiple levels (e.g. annotated tags
1223 * pointing to annotated tags pointing to a commit.)
1224 * 2. As the refs are cached we might know what refname peels to without
1225 * the need to parse the object via parse_object(). peel_ref() might be a
1226 * more efficient alternative to obtain the pointee.
1228 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1229 const unsigned char *sha1
,
1230 const char *refname
)
1232 const unsigned char *tagged_sha1
= NULL
;
1235 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1237 obj
= parse_object(sha1
);
1239 die(_("malformed object at '%s'"), refname
);
1240 if (obj
->type
== OBJ_TAG
)
1241 tagged_sha1
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1242 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1247 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1248 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1249 const unsigned char *objectname
,
1252 size_t len
= strlen(refname
);
1253 struct ref_array_item
*ref
= xcalloc(1, sizeof(struct ref_array_item
) + len
+ 1);
1254 memcpy(ref
->refname
, refname
, len
);
1255 ref
->refname
[len
] = '\0';
1256 hashcpy(ref
->objectname
, objectname
);
1262 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1270 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1271 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1272 { "refs/tags/", FILTER_REFS_TAGS
}
1275 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1276 filter
->kind
== FILTER_REFS_REMOTES
||
1277 filter
->kind
== FILTER_REFS_TAGS
)
1278 return filter
->kind
;
1279 else if (!strcmp(refname
, "HEAD"))
1280 return FILTER_REFS_DETACHED_HEAD
;
1282 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1283 if (starts_with(refname
, ref_kind
[i
].prefix
))
1284 return ref_kind
[i
].kind
;
1287 return FILTER_REFS_OTHERS
;
1291 * A call-back given to for_each_ref(). Filter refs and keep them for
1292 * later object processing.
1294 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1296 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1297 struct ref_filter
*filter
= ref_cbdata
->filter
;
1298 struct ref_array_item
*ref
;
1299 struct commit
*commit
= NULL
;
1302 if (flag
& REF_BAD_NAME
) {
1303 warning("ignoring ref with broken name %s", refname
);
1307 if (flag
& REF_ISBROKEN
) {
1308 warning("ignoring broken ref %s", refname
);
1312 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1313 kind
= filter_ref_kind(filter
, refname
);
1314 if (!(kind
& filter
->kind
))
1317 if (!filter_pattern_match(filter
, refname
))
1320 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1324 * A merge filter is applied on refs pointing to commits. Hence
1325 * obtain the commit using the 'oid' available and discard all
1326 * non-commits early. The actual filtering is done later.
1328 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->verbose
) {
1329 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1332 /* We perform the filtering for the '--contains' option */
1333 if (filter
->with_commit
&&
1334 !commit_contains(filter
, commit
))
1339 * We do not open the object yet; sort may only need refname
1340 * to do its job and the resulting list may yet to be pruned
1341 * by maxcount logic.
1343 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1344 ref
->commit
= commit
;
1346 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1347 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1352 /* Free memory allocated for a ref_array_item */
1353 static void free_array_item(struct ref_array_item
*item
)
1355 free((char *)item
->symref
);
1359 /* Free all memory allocated for ref_array */
1360 void ref_array_clear(struct ref_array
*array
)
1364 for (i
= 0; i
< array
->nr
; i
++)
1365 free_array_item(array
->items
[i
]);
1367 array
->items
= NULL
;
1368 array
->nr
= array
->alloc
= 0;
1371 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1373 struct rev_info revs
;
1375 struct ref_filter
*filter
= ref_cbdata
->filter
;
1376 struct ref_array
*array
= ref_cbdata
->array
;
1377 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1379 init_revisions(&revs
, NULL
);
1381 for (i
= 0; i
< array
->nr
; i
++) {
1382 struct ref_array_item
*item
= array
->items
[i
];
1383 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1384 to_clear
[i
] = item
->commit
;
1387 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1388 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1391 if (prepare_revision_walk(&revs
))
1392 die(_("revision walk setup failed"));
1397 for (i
= 0; i
< old_nr
; i
++) {
1398 struct ref_array_item
*item
= array
->items
[i
];
1399 struct commit
*commit
= item
->commit
;
1401 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1403 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1404 array
->items
[array
->nr
++] = array
->items
[i
];
1406 free_array_item(item
);
1409 for (i
= 0; i
< old_nr
; i
++)
1410 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1411 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1416 * API for filtering a set of refs. Based on the type of refs the user
1417 * has requested, we iterate through those refs and apply filters
1418 * as per the given ref_filter structure and finally store the
1419 * filtered refs in the ref_array structure.
1421 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1423 struct ref_filter_cbdata ref_cbdata
;
1425 unsigned int broken
= 0;
1427 ref_cbdata
.array
= array
;
1428 ref_cbdata
.filter
= filter
;
1430 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1432 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1434 /* Simple per-ref filtering */
1436 die("filter_refs: invalid type");
1439 * For common cases where we need only branches or remotes or tags,
1440 * we only iterate through those refs. If a mix of refs is needed,
1441 * we iterate over all refs and filter out required refs with the help
1442 * of filter_ref_kind().
1444 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1445 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1446 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1447 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1448 else if (filter
->kind
== FILTER_REFS_TAGS
)
1449 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1450 else if (filter
->kind
& FILTER_REFS_ALL
)
1451 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1452 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1453 head_ref(ref_filter_handler
, &ref_cbdata
);
1457 /* Filters that need revision walking */
1458 if (filter
->merge_commit
)
1459 do_merge_filter(&ref_cbdata
);
1464 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1466 struct atom_value
*va
, *vb
;
1468 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
1470 get_ref_atom_value(a
, s
->atom
, &va
);
1471 get_ref_atom_value(b
, s
->atom
, &vb
);
1473 cmp
= versioncmp(va
->s
, vb
->s
);
1474 else if (cmp_type
== FIELD_STR
)
1475 cmp
= strcmp(va
->s
, vb
->s
);
1477 if (va
->ul
< vb
->ul
)
1479 else if (va
->ul
== vb
->ul
)
1480 cmp
= strcmp(a
->refname
, b
->refname
);
1485 return (s
->reverse
) ? -cmp
: cmp
;
1488 static struct ref_sorting
*ref_sorting
;
1489 static int compare_refs(const void *a_
, const void *b_
)
1491 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1492 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1493 struct ref_sorting
*s
;
1495 for (s
= ref_sorting
; s
; s
= s
->next
) {
1496 int cmp
= cmp_ref_sorting(s
, a
, b
);
1503 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1505 ref_sorting
= sorting
;
1506 qsort(array
->items
, array
->nr
, sizeof(struct ref_array_item
*), compare_refs
);
1509 static int hex1(char ch
)
1511 if ('0' <= ch
&& ch
<= '9')
1513 else if ('a' <= ch
&& ch
<= 'f')
1514 return ch
- 'a' + 10;
1515 else if ('A' <= ch
&& ch
<= 'F')
1516 return ch
- 'A' + 10;
1519 static int hex2(const char *cp
)
1522 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
1527 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1529 struct strbuf
*s
= &state
->stack
->output
;
1531 while (*cp
&& (!ep
|| cp
< ep
)) {
1536 int ch
= hex2(cp
+ 1);
1538 strbuf_addch(s
, ch
);
1544 strbuf_addch(s
, *cp
);
1549 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1551 const char *cp
, *sp
, *ep
;
1552 struct strbuf
*final_buf
;
1553 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1555 state
.quote_style
= quote_style
;
1556 push_stack_element(&state
.stack
);
1558 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1559 struct atom_value
*atomv
;
1561 ep
= strchr(sp
, ')');
1563 append_literal(cp
, sp
, &state
);
1564 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1565 atomv
->handler(atomv
, &state
);
1568 sp
= cp
+ strlen(cp
);
1569 append_literal(cp
, sp
, &state
);
1571 if (need_color_reset_at_eol
) {
1572 struct atom_value resetv
;
1573 char color
[COLOR_MAXLEN
] = "";
1575 if (color_parse("reset", color
) < 0)
1576 die("BUG: couldn't parse 'reset' as a color");
1578 append_atom(&resetv
, &state
);
1580 if (state
.stack
->prev
)
1581 die(_("format: %%(end) atom missing"));
1582 final_buf
= &state
.stack
->output
;
1583 fwrite(final_buf
->buf
, 1, final_buf
->len
, stdout
);
1584 pop_stack_element(&state
.stack
);
1588 /* If no sorting option is given, use refname to sort as default */
1589 struct ref_sorting
*ref_default_sorting(void)
1591 static const char cstr_name
[] = "refname";
1593 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1595 sorting
->next
= NULL
;
1596 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1600 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1602 struct ref_sorting
**sorting_tail
= opt
->value
;
1603 struct ref_sorting
*s
;
1606 if (!arg
) /* should --no-sort void the list ? */
1609 s
= xcalloc(1, sizeof(*s
));
1610 s
->next
= *sorting_tail
;
1617 if (skip_prefix(arg
, "version:", &arg
) ||
1618 skip_prefix(arg
, "v:", &arg
))
1621 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1625 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1627 struct ref_filter
*rf
= opt
->value
;
1628 unsigned char sha1
[20];
1630 rf
->merge
= starts_with(opt
->long_name
, "no")
1631 ? REF_FILTER_MERGED_OMIT
1632 : REF_FILTER_MERGED_INCLUDE
;
1634 if (get_sha1(arg
, sha1
))
1635 die(_("malformed object name %s"), arg
);
1637 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1638 if (!rf
->merge_commit
)
1639 return opterror(opt
, "must point to a commit", 0);