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
;
25 * An atom is a valid field atom listed below, possibly prefixed with
26 * a "*" to denote deref_tag().
28 * We parse given format string and sort specifiers, and make a list
29 * of properties that we need to extract out of objects. ref_array_item
30 * structure will hold an array of values extracted that can be
31 * indexed with the "atom number", which is an index into this
34 static struct used_atom
{
38 char color
[COLOR_MAXLEN
];
42 static int used_atom_cnt
, need_tagged
, need_symref
;
43 static int need_color_reset_at_eol
;
45 static void color_atom_parser(struct used_atom
*atom
, const char *color_value
)
48 die(_("expected format: %%(color:<color>)"));
49 if (color_parse(color_value
, atom
->u
.color
) < 0)
50 die(_("unrecognized color: %%(color:%s)"), color_value
);
53 static align_type
parse_align_position(const char *s
)
55 if (!strcmp(s
, "right"))
57 else if (!strcmp(s
, "middle"))
59 else if (!strcmp(s
, "left"))
64 static void align_atom_parser(struct used_atom
*atom
, const char *arg
)
66 struct align
*align
= &atom
->u
.align
;
67 struct string_list params
= STRING_LIST_INIT_DUP
;
69 unsigned int width
= ~0U;
72 die(_("expected format: %%(align:<width>,<position>)"));
74 align
->position
= ALIGN_LEFT
;
76 string_list_split(¶ms
, arg
, ',', -1);
77 for (i
= 0; i
< params
.nr
; i
++) {
78 const char *s
= params
.items
[i
].string
;
81 if (!strtoul_ui(s
, 10, &width
))
83 else if ((position
= parse_align_position(s
)) >= 0)
84 align
->position
= position
;
86 die(_("unrecognized %%(align) argument: %s"), s
);
90 die(_("positive width expected with the %%(align) atom"));
92 string_list_clear(¶ms
, 0);
98 void (*parser
)(struct used_atom
*atom
, const char *arg
);
102 { "objectsize", FIELD_ULONG
},
106 { "numparent", FIELD_ULONG
},
113 { "authordate", FIELD_TIME
},
116 { "committeremail" },
117 { "committerdate", FIELD_TIME
},
121 { "taggerdate", FIELD_TIME
},
123 { "creatordate", FIELD_TIME
},
132 { "color", FIELD_STR
, color_atom_parser
},
133 { "align", FIELD_STR
, align_atom_parser
},
137 #define REF_FORMATTING_STATE_INIT { 0, NULL }
141 struct object_id oid
;
144 struct ref_formatting_stack
{
145 struct ref_formatting_stack
*prev
;
146 struct strbuf output
;
147 void (*at_end
)(struct ref_formatting_stack
*stack
);
151 struct ref_formatting_state
{
153 struct ref_formatting_stack
*stack
;
160 struct contents contents
;
162 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
163 unsigned long ul
; /* used for sorting when not FIELD_STR */
167 * Used to parse format string and sort specifiers
169 int parse_ref_filter_atom(const char *atom
, const char *ep
)
176 if (*sp
== '*' && sp
< ep
)
179 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
181 /* Do we have the atom already used elsewhere? */
182 for (i
= 0; i
< used_atom_cnt
; i
++) {
183 int len
= strlen(used_atom
[i
].name
);
184 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
188 /* Is the atom a valid one? */
189 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
190 int len
= strlen(valid_atom
[i
].name
);
193 * If the atom name has a colon, strip it and everything after
194 * it off - it specifies the format for this entry, and
195 * shouldn't be used for checking against the valid_atom
198 arg
= memchr(sp
, ':', ep
- sp
);
199 if (len
== (arg
? arg
: ep
) - sp
&&
200 !memcmp(valid_atom
[i
].name
, sp
, len
))
204 if (ARRAY_SIZE(valid_atom
) <= i
)
205 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
207 /* Add it in, including the deref prefix */
210 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
211 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
212 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
214 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
215 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
216 if (valid_atom
[i
].parser
)
217 valid_atom
[i
].parser(&used_atom
[at
], arg
);
220 if (!strcmp(used_atom
[at
].name
, "symref"))
225 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
227 switch (quote_style
) {
229 strbuf_addstr(s
, str
);
232 sq_quote_buf(s
, str
);
235 perl_quote_buf(s
, str
);
238 python_quote_buf(s
, str
);
241 tcl_quote_buf(s
, str
);
246 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
249 * Quote formatting is only done when the stack has a single
250 * element. Otherwise quote formatting is done on the
251 * element's entire output strbuf when the %(end) atom is
254 if (!state
->stack
->prev
)
255 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
257 strbuf_addstr(&state
->stack
->output
, v
->s
);
260 static void push_stack_element(struct ref_formatting_stack
**stack
)
262 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
264 strbuf_init(&s
->output
, 0);
269 static void pop_stack_element(struct ref_formatting_stack
**stack
)
271 struct ref_formatting_stack
*current
= *stack
;
272 struct ref_formatting_stack
*prev
= current
->prev
;
275 strbuf_addbuf(&prev
->output
, ¤t
->output
);
276 strbuf_release(¤t
->output
);
281 static void end_align_handler(struct ref_formatting_stack
*stack
)
283 struct align
*align
= (struct align
*)stack
->at_end_data
;
284 struct strbuf s
= STRBUF_INIT
;
286 strbuf_utf8_align(&s
, align
->position
, align
->width
, stack
->output
.buf
);
287 strbuf_swap(&stack
->output
, &s
);
291 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
293 struct ref_formatting_stack
*new;
295 push_stack_element(&state
->stack
);
297 new->at_end
= end_align_handler
;
298 new->at_end_data
= &atomv
->u
.align
;
301 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
303 struct ref_formatting_stack
*current
= state
->stack
;
304 struct strbuf s
= STRBUF_INIT
;
306 if (!current
->at_end
)
307 die(_("format: %%(end) atom used without corresponding atom"));
308 current
->at_end(current
);
311 * Perform quote formatting when the stack element is that of
312 * a supporting atom. If nested then perform quote formatting
313 * only on the topmost supporting atom.
315 if (!state
->stack
->prev
->prev
) {
316 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
317 strbuf_swap(¤t
->output
, &s
);
320 pop_stack_element(&state
->stack
);
324 * In a format string, find the next occurrence of %(atom).
326 static const char *find_next(const char *cp
)
331 * %( is the start of an atom;
332 * %% is a quoted per-cent.
336 else if (cp
[1] == '%')
337 cp
++; /* skip over two % */
338 /* otherwise this is a singleton, literal % */
346 * Make sure the format string is well formed, and parse out
349 int verify_ref_format(const char *format
)
353 need_color_reset_at_eol
= 0;
354 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
355 const char *color
, *ep
= strchr(sp
, ')');
359 return error("malformed format string %s", sp
);
360 /* sp points at "%(" and ep points at the closing ")" */
361 at
= parse_ref_filter_atom(sp
+ 2, ep
);
364 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
365 need_color_reset_at_eol
= !!strcmp(color
, "reset");
371 * Given an object name, read the object data and size, and return a
372 * "struct object". If the object data we are returning is also borrowed
373 * by the "struct object" representation, set *eaten as well---it is a
374 * signal from parse_object_buffer to us not to free the buffer.
376 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
378 enum object_type type
;
379 void *buf
= read_sha1_file(sha1
, &type
, sz
);
382 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
388 static int grab_objectname(const char *name
, const unsigned char *sha1
,
389 struct atom_value
*v
)
391 if (!strcmp(name
, "objectname")) {
392 v
->s
= xstrdup(sha1_to_hex(sha1
));
395 if (!strcmp(name
, "objectname:short")) {
396 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
402 /* See grab_values */
403 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
407 for (i
= 0; i
< used_atom_cnt
; i
++) {
408 const char *name
= used_atom
[i
].name
;
409 struct atom_value
*v
= &val
[i
];
410 if (!!deref
!= (*name
== '*'))
414 if (!strcmp(name
, "objecttype"))
415 v
->s
= typename(obj
->type
);
416 else if (!strcmp(name
, "objectsize")) {
418 v
->s
= xstrfmt("%lu", sz
);
421 grab_objectname(name
, obj
->oid
.hash
, v
);
425 /* See grab_values */
426 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
429 struct tag
*tag
= (struct tag
*) obj
;
431 for (i
= 0; i
< used_atom_cnt
; i
++) {
432 const char *name
= used_atom
[i
].name
;
433 struct atom_value
*v
= &val
[i
];
434 if (!!deref
!= (*name
== '*'))
438 if (!strcmp(name
, "tag"))
440 else if (!strcmp(name
, "type") && tag
->tagged
)
441 v
->s
= typename(tag
->tagged
->type
);
442 else if (!strcmp(name
, "object") && tag
->tagged
)
443 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
447 /* See grab_values */
448 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
451 struct commit
*commit
= (struct commit
*) obj
;
453 for (i
= 0; i
< used_atom_cnt
; i
++) {
454 const char *name
= used_atom
[i
].name
;
455 struct atom_value
*v
= &val
[i
];
456 if (!!deref
!= (*name
== '*'))
460 if (!strcmp(name
, "tree")) {
461 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
463 else if (!strcmp(name
, "numparent")) {
464 v
->ul
= commit_list_count(commit
->parents
);
465 v
->s
= xstrfmt("%lu", v
->ul
);
467 else if (!strcmp(name
, "parent")) {
468 struct commit_list
*parents
;
469 struct strbuf s
= STRBUF_INIT
;
470 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
471 struct commit
*parent
= parents
->item
;
472 if (parents
!= commit
->parents
)
473 strbuf_addch(&s
, ' ');
474 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
476 v
->s
= strbuf_detach(&s
, NULL
);
481 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
485 if (!strncmp(buf
, who
, wholen
) &&
487 return buf
+ wholen
+ 1;
488 eol
= strchr(buf
, '\n');
493 return ""; /* end of header */
499 static const char *copy_line(const char *buf
)
501 const char *eol
= strchrnul(buf
, '\n');
502 return xmemdupz(buf
, eol
- buf
);
505 static const char *copy_name(const char *buf
)
508 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
509 if (!strncmp(cp
, " <", 2))
510 return xmemdupz(buf
, cp
- buf
);
515 static const char *copy_email(const char *buf
)
517 const char *email
= strchr(buf
, '<');
521 eoemail
= strchr(email
, '>');
524 return xmemdupz(email
, eoemail
+ 1 - email
);
527 static char *copy_subject(const char *buf
, unsigned long len
)
529 char *r
= xmemdupz(buf
, len
);
532 for (i
= 0; i
< len
; i
++)
539 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
541 const char *eoemail
= strstr(buf
, "> ");
543 unsigned long timestamp
;
545 struct date_mode date_mode
= { DATE_NORMAL
};
549 * We got here because atomname ends in "date" or "date<something>";
550 * it's not possible that <something> is not ":<format>" because
551 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
552 * ":" means no format is specified, and use the default.
554 formatp
= strchr(atomname
, ':');
555 if (formatp
!= NULL
) {
557 parse_date_format(formatp
, &date_mode
);
562 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
563 if (timestamp
== ULONG_MAX
)
565 tz
= strtol(zone
, NULL
, 10);
566 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
568 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
576 /* See grab_values */
577 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
580 int wholen
= strlen(who
);
581 const char *wholine
= NULL
;
583 for (i
= 0; i
< used_atom_cnt
; i
++) {
584 const char *name
= used_atom
[i
].name
;
585 struct atom_value
*v
= &val
[i
];
586 if (!!deref
!= (*name
== '*'))
590 if (strncmp(who
, name
, wholen
))
592 if (name
[wholen
] != 0 &&
593 strcmp(name
+ wholen
, "name") &&
594 strcmp(name
+ wholen
, "email") &&
595 !starts_with(name
+ wholen
, "date"))
598 wholine
= find_wholine(who
, wholen
, buf
, sz
);
600 return; /* no point looking for it */
601 if (name
[wholen
] == 0)
602 v
->s
= copy_line(wholine
);
603 else if (!strcmp(name
+ wholen
, "name"))
604 v
->s
= copy_name(wholine
);
605 else if (!strcmp(name
+ wholen
, "email"))
606 v
->s
= copy_email(wholine
);
607 else if (starts_with(name
+ wholen
, "date"))
608 grab_date(wholine
, v
, name
);
612 * For a tag or a commit object, if "creator" or "creatordate" is
613 * requested, do something special.
615 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
616 return; /* "author" for commit object is not wanted */
618 wholine
= find_wholine(who
, wholen
, buf
, sz
);
621 for (i
= 0; i
< used_atom_cnt
; i
++) {
622 const char *name
= used_atom
[i
].name
;
623 struct atom_value
*v
= &val
[i
];
624 if (!!deref
!= (*name
== '*'))
629 if (starts_with(name
, "creatordate"))
630 grab_date(wholine
, v
, name
);
631 else if (!strcmp(name
, "creator"))
632 v
->s
= copy_line(wholine
);
636 static void find_subpos(const char *buf
, unsigned long sz
,
637 const char **sub
, unsigned long *sublen
,
638 const char **body
, unsigned long *bodylen
,
639 unsigned long *nonsiglen
,
640 const char **sig
, unsigned long *siglen
)
643 /* skip past header until we hit empty line */
644 while (*buf
&& *buf
!= '\n') {
645 eol
= strchrnul(buf
, '\n');
650 /* skip any empty lines */
654 /* parse signature first; we might not even have a subject line */
655 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
656 *siglen
= strlen(*sig
);
658 /* subject is first non-empty line */
660 /* subject goes to first empty line */
661 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
662 eol
= strchrnul(buf
, '\n');
667 *sublen
= buf
- *sub
;
668 /* drop trailing newline, if present */
669 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
672 /* skip any empty lines */
676 *bodylen
= strlen(buf
);
677 *nonsiglen
= *sig
- buf
;
681 * If 'lines' is greater than 0, append that many lines from the given
682 * 'buf' of length 'size' to the given strbuf.
684 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
687 const char *sp
, *eol
;
692 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
694 strbuf_addstr(out
, "\n ");
695 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
696 len
= eol
? eol
- sp
: size
- (sp
- buf
);
697 strbuf_add(out
, sp
, len
);
704 /* See grab_values */
705 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
708 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
709 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
711 for (i
= 0; i
< used_atom_cnt
; i
++) {
712 const char *name
= used_atom
[i
].name
;
713 struct atom_value
*v
= &val
[i
];
714 const char *valp
= NULL
;
715 if (!!deref
!= (*name
== '*'))
719 if (strcmp(name
, "subject") &&
720 strcmp(name
, "body") &&
721 strcmp(name
, "contents") &&
722 strcmp(name
, "contents:subject") &&
723 strcmp(name
, "contents:body") &&
724 strcmp(name
, "contents:signature") &&
725 !starts_with(name
, "contents:lines="))
730 &bodypos
, &bodylen
, &nonsiglen
,
733 if (!strcmp(name
, "subject"))
734 v
->s
= copy_subject(subpos
, sublen
);
735 else if (!strcmp(name
, "contents:subject"))
736 v
->s
= copy_subject(subpos
, sublen
);
737 else if (!strcmp(name
, "body"))
738 v
->s
= xmemdupz(bodypos
, bodylen
);
739 else if (!strcmp(name
, "contents:body"))
740 v
->s
= xmemdupz(bodypos
, nonsiglen
);
741 else if (!strcmp(name
, "contents:signature"))
742 v
->s
= xmemdupz(sigpos
, siglen
);
743 else if (!strcmp(name
, "contents"))
744 v
->s
= xstrdup(subpos
);
745 else if (skip_prefix(name
, "contents:lines=", &valp
)) {
746 struct strbuf s
= STRBUF_INIT
;
747 const char *contents_end
= bodylen
+ bodypos
- siglen
;
749 if (strtoul_ui(valp
, 10, &v
->u
.contents
.lines
))
750 die(_("positive value expected contents:lines=%s"), valp
);
751 /* Size is the length of the message after removing the signature */
752 append_lines(&s
, subpos
, contents_end
- subpos
, v
->u
.contents
.lines
);
753 v
->s
= strbuf_detach(&s
, NULL
);
759 * We want to have empty print-string for field requests
760 * that do not apply (e.g. "authordate" for a tag object)
762 static void fill_missing_values(struct atom_value
*val
)
765 for (i
= 0; i
< used_atom_cnt
; i
++) {
766 struct atom_value
*v
= &val
[i
];
773 * val is a list of atom_value to hold returned values. Extract
774 * the values for atoms in used_atom array out of (obj, buf, sz).
775 * when deref is false, (obj, buf, sz) is the object that is
776 * pointed at by the ref itself; otherwise it is the object the
777 * ref (which is a tag) refers to.
779 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
781 grab_common_values(val
, deref
, obj
, buf
, sz
);
784 grab_tag_values(val
, deref
, obj
, buf
, sz
);
785 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
786 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
789 grab_commit_values(val
, deref
, obj
, buf
, sz
);
790 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
791 grab_person("author", val
, deref
, obj
, buf
, sz
);
792 grab_person("committer", val
, deref
, obj
, buf
, sz
);
795 /* grab_tree_values(val, deref, obj, buf, sz); */
798 /* grab_blob_values(val, deref, obj, buf, sz); */
801 die("Eh? Object of type %d?", obj
->type
);
805 static inline char *copy_advance(char *dst
, const char *src
)
812 static const char *strip_ref_components(const char *refname
, const char *nr_arg
)
815 long nr
= strtol(nr_arg
, &end
, 10);
817 const char *start
= refname
;
819 if (nr
< 1 || *end
!= '\0')
820 die(":strip= requires a positive integer argument");
825 die("ref '%s' does not have %ld components to :strip",
836 * Parse the object referred by ref, and grab needed value.
838 static void populate_value(struct ref_array_item
*ref
)
844 const unsigned char *tagged
;
846 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
848 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
849 unsigned char unused1
[20];
850 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
856 /* Fill in specials first */
857 for (i
= 0; i
< used_atom_cnt
; i
++) {
858 struct used_atom
*atom
= &used_atom
[i
];
859 const char *name
= used_atom
[i
].name
;
860 struct atom_value
*v
= &ref
->value
[i
];
864 struct branch
*branch
= NULL
;
866 v
->handler
= append_atom
;
873 if (starts_with(name
, "refname"))
874 refname
= ref
->refname
;
875 else if (starts_with(name
, "symref"))
876 refname
= ref
->symref
? ref
->symref
: "";
877 else if (starts_with(name
, "upstream")) {
878 const char *branch_name
;
879 /* only local branches may have an upstream */
880 if (!skip_prefix(ref
->refname
, "refs/heads/",
883 branch
= branch_get(branch_name
);
885 refname
= branch_get_upstream(branch
, NULL
);
888 } else if (starts_with(name
, "push")) {
889 const char *branch_name
;
890 if (!skip_prefix(ref
->refname
, "refs/heads/",
893 branch
= branch_get(branch_name
);
895 refname
= branch_get_push(branch
, NULL
);
898 } else if (starts_with(name
, "color:")) {
899 v
->s
= atom
->u
.color
;
901 } else if (!strcmp(name
, "flag")) {
902 char buf
[256], *cp
= buf
;
903 if (ref
->flag
& REF_ISSYMREF
)
904 cp
= copy_advance(cp
, ",symref");
905 if (ref
->flag
& REF_ISPACKED
)
906 cp
= copy_advance(cp
, ",packed");
911 v
->s
= xstrdup(buf
+ 1);
914 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
916 } else if (!strcmp(name
, "HEAD")) {
918 unsigned char sha1
[20];
920 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
922 if (!strcmp(ref
->refname
, head
))
927 } else if (starts_with(name
, "align")) {
928 v
->u
.align
= atom
->u
.align
;
929 v
->handler
= align_atom_handler
;
931 } else if (!strcmp(name
, "end")) {
932 v
->handler
= end_atom_handler
;
937 formatp
= strchr(name
, ':');
939 int num_ours
, num_theirs
;
943 if (!strcmp(formatp
, "short"))
944 refname
= shorten_unambiguous_ref(refname
,
945 warn_ambiguous_refs
);
946 else if (skip_prefix(formatp
, "strip=", &arg
))
947 refname
= strip_ref_components(refname
, arg
);
948 else if (!strcmp(formatp
, "track") &&
949 (starts_with(name
, "upstream") ||
950 starts_with(name
, "push"))) {
952 if (stat_tracking_info(branch
, &num_ours
,
956 if (!num_ours
&& !num_theirs
)
959 v
->s
= xstrfmt("[behind %d]", num_theirs
);
960 else if (!num_theirs
)
961 v
->s
= xstrfmt("[ahead %d]", num_ours
);
963 v
->s
= xstrfmt("[ahead %d, behind %d]",
964 num_ours
, num_theirs
);
966 } else if (!strcmp(formatp
, "trackshort") &&
967 (starts_with(name
, "upstream") ||
968 starts_with(name
, "push"))) {
971 if (stat_tracking_info(branch
, &num_ours
,
975 if (!num_ours
&& !num_theirs
)
979 else if (!num_theirs
)
985 die("unknown %.*s format %s",
986 (int)(formatp
- name
), name
, formatp
);
992 v
->s
= xstrfmt("%s^{}", refname
);
995 for (i
= 0; i
< used_atom_cnt
; i
++) {
996 struct atom_value
*v
= &ref
->value
[i
];
1003 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
1005 die("missing object %s for %s",
1006 sha1_to_hex(ref
->objectname
), ref
->refname
);
1008 die("parse_object_buffer failed on %s for %s",
1009 sha1_to_hex(ref
->objectname
), ref
->refname
);
1011 grab_values(ref
->value
, 0, obj
, buf
, size
);
1016 * If there is no atom that wants to know about tagged
1017 * object, we are done.
1019 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1023 * If it is a tag object, see if we use a value that derefs
1024 * the object, and if we do grab the object it refers to.
1026 tagged
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1029 * NEEDSWORK: This derefs tag only once, which
1030 * is good to deal with chains of trust, but
1031 * is not consistent with what deref_tag() does
1032 * which peels the onion to the core.
1034 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1036 die("missing object %s for %s",
1037 sha1_to_hex(tagged
), ref
->refname
);
1039 die("parse_object_buffer failed on %s for %s",
1040 sha1_to_hex(tagged
), ref
->refname
);
1041 grab_values(ref
->value
, 1, obj
, buf
, size
);
1047 * Given a ref, return the value for the atom. This lazily gets value
1048 * out of the object by calling populate value.
1050 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1053 populate_value(ref
);
1054 fill_missing_values(ref
->value
);
1056 *v
= &ref
->value
[atom
];
1059 enum contains_result
{
1060 CONTAINS_UNKNOWN
= -1,
1066 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1069 * At each recursion step, the stack items points to the commits whose
1070 * ancestors are to be inspected.
1072 struct contains_stack
{
1074 struct contains_stack_entry
{
1075 struct commit
*commit
;
1076 struct commit_list
*parents
;
1080 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1082 for (; want
; want
= want
->next
)
1083 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1089 * Test whether the candidate or one of its parents is contained in the list.
1090 * Do not recurse to find out, though, but return -1 if inconclusive.
1092 static enum contains_result
contains_test(struct commit
*candidate
,
1093 const struct commit_list
*want
)
1095 /* was it previously marked as containing a want commit? */
1096 if (candidate
->object
.flags
& TMP_MARK
)
1098 /* or marked as not possibly containing a want commit? */
1099 if (candidate
->object
.flags
& UNINTERESTING
)
1102 if (in_commit_list(want
, candidate
)) {
1103 candidate
->object
.flags
|= TMP_MARK
;
1107 if (parse_commit(candidate
) < 0)
1113 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1115 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1116 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1117 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1120 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1121 const struct commit_list
*want
)
1123 struct contains_stack contains_stack
= { 0, 0, NULL
};
1124 int result
= contains_test(candidate
, want
);
1126 if (result
!= CONTAINS_UNKNOWN
)
1129 push_to_contains_stack(candidate
, &contains_stack
);
1130 while (contains_stack
.nr
) {
1131 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1132 struct commit
*commit
= entry
->commit
;
1133 struct commit_list
*parents
= entry
->parents
;
1136 commit
->object
.flags
|= UNINTERESTING
;
1137 contains_stack
.nr
--;
1140 * If we just popped the stack, parents->item has been marked,
1141 * therefore contains_test will return a meaningful 0 or 1.
1143 else switch (contains_test(parents
->item
, want
)) {
1145 commit
->object
.flags
|= TMP_MARK
;
1146 contains_stack
.nr
--;
1149 entry
->parents
= parents
->next
;
1151 case CONTAINS_UNKNOWN
:
1152 push_to_contains_stack(parents
->item
, &contains_stack
);
1156 free(contains_stack
.contains_stack
);
1157 return contains_test(candidate
, want
);
1160 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1162 if (filter
->with_commit_tag_algo
)
1163 return contains_tag_algo(commit
, filter
->with_commit
);
1164 return is_descendant_of(commit
, filter
->with_commit
);
1168 * Return 1 if the refname matches one of the patterns, otherwise 0.
1169 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1170 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1171 * matches "refs/heads/mas*", too).
1173 static int match_pattern(const char **patterns
, const char *refname
)
1176 * When no '--format' option is given we need to skip the prefix
1177 * for matching refs of tags and branches.
1179 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1180 skip_prefix(refname
, "refs/heads/", &refname
) ||
1181 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1182 skip_prefix(refname
, "refs/", &refname
));
1184 for (; *patterns
; patterns
++) {
1185 if (!wildmatch(*patterns
, refname
, 0, NULL
))
1192 * Return 1 if the refname matches one of the patterns, otherwise 0.
1193 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1194 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1195 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1197 static int match_name_as_path(const char **pattern
, const char *refname
)
1199 int namelen
= strlen(refname
);
1200 for (; *pattern
; pattern
++) {
1201 const char *p
= *pattern
;
1202 int plen
= strlen(p
);
1204 if ((plen
<= namelen
) &&
1205 !strncmp(refname
, p
, plen
) &&
1206 (refname
[plen
] == '\0' ||
1207 refname
[plen
] == '/' ||
1210 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1216 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1217 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1219 if (!*filter
->name_patterns
)
1220 return 1; /* No pattern always matches */
1221 if (filter
->match_as_path
)
1222 return match_name_as_path(filter
->name_patterns
, refname
);
1223 return match_pattern(filter
->name_patterns
, refname
);
1227 * Given a ref (sha1, refname), check if the ref belongs to the array
1228 * of sha1s. If the given ref is a tag, check if the given tag points
1229 * at one of the sha1s in the given sha1 array.
1230 * the given sha1_array.
1232 * 1. Only a single level of inderection is obtained, we might want to
1233 * change this to account for multiple levels (e.g. annotated tags
1234 * pointing to annotated tags pointing to a commit.)
1235 * 2. As the refs are cached we might know what refname peels to without
1236 * the need to parse the object via parse_object(). peel_ref() might be a
1237 * more efficient alternative to obtain the pointee.
1239 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1240 const unsigned char *sha1
,
1241 const char *refname
)
1243 const unsigned char *tagged_sha1
= NULL
;
1246 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1248 obj
= parse_object(sha1
);
1250 die(_("malformed object at '%s'"), refname
);
1251 if (obj
->type
== OBJ_TAG
)
1252 tagged_sha1
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1253 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1258 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1259 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1260 const unsigned char *objectname
,
1263 size_t len
= strlen(refname
);
1264 struct ref_array_item
*ref
= xcalloc(1, sizeof(struct ref_array_item
) + len
+ 1);
1265 memcpy(ref
->refname
, refname
, len
);
1266 ref
->refname
[len
] = '\0';
1267 hashcpy(ref
->objectname
, objectname
);
1273 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1281 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1282 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1283 { "refs/tags/", FILTER_REFS_TAGS
}
1286 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1287 filter
->kind
== FILTER_REFS_REMOTES
||
1288 filter
->kind
== FILTER_REFS_TAGS
)
1289 return filter
->kind
;
1290 else if (!strcmp(refname
, "HEAD"))
1291 return FILTER_REFS_DETACHED_HEAD
;
1293 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1294 if (starts_with(refname
, ref_kind
[i
].prefix
))
1295 return ref_kind
[i
].kind
;
1298 return FILTER_REFS_OTHERS
;
1302 * A call-back given to for_each_ref(). Filter refs and keep them for
1303 * later object processing.
1305 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1307 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1308 struct ref_filter
*filter
= ref_cbdata
->filter
;
1309 struct ref_array_item
*ref
;
1310 struct commit
*commit
= NULL
;
1313 if (flag
& REF_BAD_NAME
) {
1314 warning("ignoring ref with broken name %s", refname
);
1318 if (flag
& REF_ISBROKEN
) {
1319 warning("ignoring broken ref %s", refname
);
1323 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1324 kind
= filter_ref_kind(filter
, refname
);
1325 if (!(kind
& filter
->kind
))
1328 if (!filter_pattern_match(filter
, refname
))
1331 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1335 * A merge filter is applied on refs pointing to commits. Hence
1336 * obtain the commit using the 'oid' available and discard all
1337 * non-commits early. The actual filtering is done later.
1339 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->verbose
) {
1340 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1343 /* We perform the filtering for the '--contains' option */
1344 if (filter
->with_commit
&&
1345 !commit_contains(filter
, commit
))
1350 * We do not open the object yet; sort may only need refname
1351 * to do its job and the resulting list may yet to be pruned
1352 * by maxcount logic.
1354 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1355 ref
->commit
= commit
;
1357 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1358 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1363 /* Free memory allocated for a ref_array_item */
1364 static void free_array_item(struct ref_array_item
*item
)
1366 free((char *)item
->symref
);
1370 /* Free all memory allocated for ref_array */
1371 void ref_array_clear(struct ref_array
*array
)
1375 for (i
= 0; i
< array
->nr
; i
++)
1376 free_array_item(array
->items
[i
]);
1378 array
->items
= NULL
;
1379 array
->nr
= array
->alloc
= 0;
1382 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1384 struct rev_info revs
;
1386 struct ref_filter
*filter
= ref_cbdata
->filter
;
1387 struct ref_array
*array
= ref_cbdata
->array
;
1388 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1390 init_revisions(&revs
, NULL
);
1392 for (i
= 0; i
< array
->nr
; i
++) {
1393 struct ref_array_item
*item
= array
->items
[i
];
1394 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1395 to_clear
[i
] = item
->commit
;
1398 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1399 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1402 if (prepare_revision_walk(&revs
))
1403 die(_("revision walk setup failed"));
1408 for (i
= 0; i
< old_nr
; i
++) {
1409 struct ref_array_item
*item
= array
->items
[i
];
1410 struct commit
*commit
= item
->commit
;
1412 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1414 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1415 array
->items
[array
->nr
++] = array
->items
[i
];
1417 free_array_item(item
);
1420 for (i
= 0; i
< old_nr
; i
++)
1421 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1422 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1427 * API for filtering a set of refs. Based on the type of refs the user
1428 * has requested, we iterate through those refs and apply filters
1429 * as per the given ref_filter structure and finally store the
1430 * filtered refs in the ref_array structure.
1432 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1434 struct ref_filter_cbdata ref_cbdata
;
1436 unsigned int broken
= 0;
1438 ref_cbdata
.array
= array
;
1439 ref_cbdata
.filter
= filter
;
1441 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1443 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1445 /* Simple per-ref filtering */
1447 die("filter_refs: invalid type");
1450 * For common cases where we need only branches or remotes or tags,
1451 * we only iterate through those refs. If a mix of refs is needed,
1452 * we iterate over all refs and filter out required refs with the help
1453 * of filter_ref_kind().
1455 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1456 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1457 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1458 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1459 else if (filter
->kind
== FILTER_REFS_TAGS
)
1460 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1461 else if (filter
->kind
& FILTER_REFS_ALL
)
1462 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1463 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1464 head_ref(ref_filter_handler
, &ref_cbdata
);
1468 /* Filters that need revision walking */
1469 if (filter
->merge_commit
)
1470 do_merge_filter(&ref_cbdata
);
1475 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1477 struct atom_value
*va
, *vb
;
1479 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
1481 get_ref_atom_value(a
, s
->atom
, &va
);
1482 get_ref_atom_value(b
, s
->atom
, &vb
);
1484 cmp
= versioncmp(va
->s
, vb
->s
);
1485 else if (cmp_type
== FIELD_STR
)
1486 cmp
= strcmp(va
->s
, vb
->s
);
1488 if (va
->ul
< vb
->ul
)
1490 else if (va
->ul
== vb
->ul
)
1491 cmp
= strcmp(a
->refname
, b
->refname
);
1496 return (s
->reverse
) ? -cmp
: cmp
;
1499 static struct ref_sorting
*ref_sorting
;
1500 static int compare_refs(const void *a_
, const void *b_
)
1502 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1503 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1504 struct ref_sorting
*s
;
1506 for (s
= ref_sorting
; s
; s
= s
->next
) {
1507 int cmp
= cmp_ref_sorting(s
, a
, b
);
1514 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1516 ref_sorting
= sorting
;
1517 qsort(array
->items
, array
->nr
, sizeof(struct ref_array_item
*), compare_refs
);
1520 static int hex1(char ch
)
1522 if ('0' <= ch
&& ch
<= '9')
1524 else if ('a' <= ch
&& ch
<= 'f')
1525 return ch
- 'a' + 10;
1526 else if ('A' <= ch
&& ch
<= 'F')
1527 return ch
- 'A' + 10;
1530 static int hex2(const char *cp
)
1533 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
1538 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1540 struct strbuf
*s
= &state
->stack
->output
;
1542 while (*cp
&& (!ep
|| cp
< ep
)) {
1547 int ch
= hex2(cp
+ 1);
1549 strbuf_addch(s
, ch
);
1555 strbuf_addch(s
, *cp
);
1560 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1562 const char *cp
, *sp
, *ep
;
1563 struct strbuf
*final_buf
;
1564 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1566 state
.quote_style
= quote_style
;
1567 push_stack_element(&state
.stack
);
1569 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1570 struct atom_value
*atomv
;
1572 ep
= strchr(sp
, ')');
1574 append_literal(cp
, sp
, &state
);
1575 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1576 atomv
->handler(atomv
, &state
);
1579 sp
= cp
+ strlen(cp
);
1580 append_literal(cp
, sp
, &state
);
1582 if (need_color_reset_at_eol
) {
1583 struct atom_value resetv
;
1584 char color
[COLOR_MAXLEN
] = "";
1586 if (color_parse("reset", color
) < 0)
1587 die("BUG: couldn't parse 'reset' as a color");
1589 append_atom(&resetv
, &state
);
1591 if (state
.stack
->prev
)
1592 die(_("format: %%(end) atom missing"));
1593 final_buf
= &state
.stack
->output
;
1594 fwrite(final_buf
->buf
, 1, final_buf
->len
, stdout
);
1595 pop_stack_element(&state
.stack
);
1599 /* If no sorting option is given, use refname to sort as default */
1600 struct ref_sorting
*ref_default_sorting(void)
1602 static const char cstr_name
[] = "refname";
1604 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1606 sorting
->next
= NULL
;
1607 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1611 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1613 struct ref_sorting
**sorting_tail
= opt
->value
;
1614 struct ref_sorting
*s
;
1617 if (!arg
) /* should --no-sort void the list ? */
1620 s
= xcalloc(1, sizeof(*s
));
1621 s
->next
= *sorting_tail
;
1628 if (skip_prefix(arg
, "version:", &arg
) ||
1629 skip_prefix(arg
, "v:", &arg
))
1632 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1636 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1638 struct ref_filter
*rf
= opt
->value
;
1639 unsigned char sha1
[20];
1641 rf
->merge
= starts_with(opt
->long_name
, "no")
1642 ? REF_FILTER_MERGED_OMIT
1643 : REF_FILTER_MERGED_INCLUDE
;
1645 if (get_sha1(arg
, sha1
))
1646 die(_("malformed object name %s"), arg
);
1648 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1649 if (!rf
->merge_commit
)
1650 return opterror(opt
, "must point to a commit", 0);