3 #include "parse-options.h"
11 #include "ref-filter.h"
14 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
22 { "objectsize", FIELD_ULONG
},
26 { "numparent", FIELD_ULONG
},
33 { "authordate", FIELD_TIME
},
37 { "committerdate", FIELD_TIME
},
41 { "taggerdate", FIELD_TIME
},
43 { "creatordate", FIELD_TIME
},
47 { "contents:subject" },
49 { "contents:signature" },
58 #define REF_FORMATTING_STATE_INIT { 0, NULL }
60 struct ref_formatting_stack
{
61 struct ref_formatting_stack
*prev
;
65 struct ref_formatting_state
{
67 struct ref_formatting_stack
*stack
;
72 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
73 unsigned long ul
; /* used for sorting when not FIELD_STR */
77 * An atom is a valid field atom listed above, possibly prefixed with
78 * a "*" to denote deref_tag().
80 * We parse given format string and sort specifiers, and make a list
81 * of properties that we need to extract out of objects. ref_array_item
82 * structure will hold an array of values extracted that can be
83 * indexed with the "atom number", which is an index into this
86 static const char **used_atom
;
87 static cmp_type
*used_atom_type
;
88 static int used_atom_cnt
, need_tagged
, need_symref
;
89 static int need_color_reset_at_eol
;
92 * Used to parse format string and sort specifiers
94 int parse_ref_filter_atom(const char *atom
, const char *ep
)
100 if (*sp
== '*' && sp
< ep
)
103 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
105 /* Do we have the atom already used elsewhere? */
106 for (i
= 0; i
< used_atom_cnt
; i
++) {
107 int len
= strlen(used_atom
[i
]);
108 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
112 /* Is the atom a valid one? */
113 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
114 int len
= strlen(valid_atom
[i
].name
);
116 * If the atom name has a colon, strip it and everything after
117 * it off - it specifies the format for this entry, and
118 * shouldn't be used for checking against the valid_atom
121 const char *formatp
= strchr(sp
, ':');
122 if (!formatp
|| ep
< formatp
)
124 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
128 if (ARRAY_SIZE(valid_atom
) <= i
)
129 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
131 /* Add it in, including the deref prefix */
134 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
135 REALLOC_ARRAY(used_atom_type
, used_atom_cnt
);
136 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
137 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
140 if (!strcmp(used_atom
[at
], "symref"))
145 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
147 switch (quote_style
) {
149 strbuf_addstr(s
, str
);
152 sq_quote_buf(s
, str
);
155 perl_quote_buf(s
, str
);
158 python_quote_buf(s
, str
);
161 tcl_quote_buf(s
, str
);
166 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
168 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
171 static void push_stack_element(struct ref_formatting_stack
**stack
)
173 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
175 strbuf_init(&s
->output
, 0);
180 static void pop_stack_element(struct ref_formatting_stack
**stack
)
182 struct ref_formatting_stack
*current
= *stack
;
183 struct ref_formatting_stack
*prev
= current
->prev
;
186 strbuf_addbuf(&prev
->output
, ¤t
->output
);
187 strbuf_release(¤t
->output
);
192 static int match_atom_name(const char *name
, const char *atom_name
, const char **val
)
196 if (!skip_prefix(name
, atom_name
, &body
))
197 return 0; /* doesn't even begin with "atom_name" */
199 *val
= NULL
; /* %(atom_name) and no customization */
203 return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
204 *val
= body
+ 1; /* "atom_name:val" */
209 * In a format string, find the next occurrence of %(atom).
211 static const char *find_next(const char *cp
)
216 * %( is the start of an atom;
217 * %% is a quoted per-cent.
221 else if (cp
[1] == '%')
222 cp
++; /* skip over two % */
223 /* otherwise this is a singleton, literal % */
231 * Make sure the format string is well formed, and parse out
234 int verify_ref_format(const char *format
)
238 need_color_reset_at_eol
= 0;
239 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
240 const char *color
, *ep
= strchr(sp
, ')');
244 return error("malformed format string %s", sp
);
245 /* sp points at "%(" and ep points at the closing ")" */
246 at
= parse_ref_filter_atom(sp
+ 2, ep
);
249 if (skip_prefix(used_atom
[at
], "color:", &color
))
250 need_color_reset_at_eol
= !!strcmp(color
, "reset");
256 * Given an object name, read the object data and size, and return a
257 * "struct object". If the object data we are returning is also borrowed
258 * by the "struct object" representation, set *eaten as well---it is a
259 * signal from parse_object_buffer to us not to free the buffer.
261 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
263 enum object_type type
;
264 void *buf
= read_sha1_file(sha1
, &type
, sz
);
267 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
273 static int grab_objectname(const char *name
, const unsigned char *sha1
,
274 struct atom_value
*v
)
276 if (!strcmp(name
, "objectname")) {
277 char *s
= xmalloc(41);
278 strcpy(s
, sha1_to_hex(sha1
));
282 if (!strcmp(name
, "objectname:short")) {
283 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
289 /* See grab_values */
290 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
294 for (i
= 0; i
< used_atom_cnt
; i
++) {
295 const char *name
= used_atom
[i
];
296 struct atom_value
*v
= &val
[i
];
297 if (!!deref
!= (*name
== '*'))
301 if (!strcmp(name
, "objecttype"))
302 v
->s
= typename(obj
->type
);
303 else if (!strcmp(name
, "objectsize")) {
304 char *s
= xmalloc(40);
305 sprintf(s
, "%lu", sz
);
310 grab_objectname(name
, obj
->sha1
, v
);
314 /* See grab_values */
315 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
318 struct tag
*tag
= (struct tag
*) obj
;
320 for (i
= 0; i
< used_atom_cnt
; i
++) {
321 const char *name
= used_atom
[i
];
322 struct atom_value
*v
= &val
[i
];
323 if (!!deref
!= (*name
== '*'))
327 if (!strcmp(name
, "tag"))
329 else if (!strcmp(name
, "type") && tag
->tagged
)
330 v
->s
= typename(tag
->tagged
->type
);
331 else if (!strcmp(name
, "object") && tag
->tagged
) {
332 char *s
= xmalloc(41);
333 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
339 /* See grab_values */
340 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
343 struct commit
*commit
= (struct commit
*) obj
;
345 for (i
= 0; i
< used_atom_cnt
; i
++) {
346 const char *name
= used_atom
[i
];
347 struct atom_value
*v
= &val
[i
];
348 if (!!deref
!= (*name
== '*'))
352 if (!strcmp(name
, "tree")) {
353 char *s
= xmalloc(41);
354 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
357 if (!strcmp(name
, "numparent")) {
358 char *s
= xmalloc(40);
359 v
->ul
= commit_list_count(commit
->parents
);
360 sprintf(s
, "%lu", v
->ul
);
363 else if (!strcmp(name
, "parent")) {
364 int num
= commit_list_count(commit
->parents
);
366 struct commit_list
*parents
;
367 char *s
= xmalloc(41 * num
+ 1);
369 for (i
= 0, parents
= commit
->parents
;
371 parents
= parents
->next
, i
= i
+ 41) {
372 struct commit
*parent
= parents
->item
;
373 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
383 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
387 if (!strncmp(buf
, who
, wholen
) &&
389 return buf
+ wholen
+ 1;
390 eol
= strchr(buf
, '\n');
395 return ""; /* end of header */
401 static const char *copy_line(const char *buf
)
403 const char *eol
= strchrnul(buf
, '\n');
404 return xmemdupz(buf
, eol
- buf
);
407 static const char *copy_name(const char *buf
)
410 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
411 if (!strncmp(cp
, " <", 2))
412 return xmemdupz(buf
, cp
- buf
);
417 static const char *copy_email(const char *buf
)
419 const char *email
= strchr(buf
, '<');
423 eoemail
= strchr(email
, '>');
426 return xmemdupz(email
, eoemail
+ 1 - email
);
429 static char *copy_subject(const char *buf
, unsigned long len
)
431 char *r
= xmemdupz(buf
, len
);
434 for (i
= 0; i
< len
; i
++)
441 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
443 const char *eoemail
= strstr(buf
, "> ");
445 unsigned long timestamp
;
447 struct date_mode date_mode
= { DATE_NORMAL
};
451 * We got here because atomname ends in "date" or "date<something>";
452 * it's not possible that <something> is not ":<format>" because
453 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
454 * ":" means no format is specified, and use the default.
456 formatp
= strchr(atomname
, ':');
457 if (formatp
!= NULL
) {
459 parse_date_format(formatp
, &date_mode
);
464 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
465 if (timestamp
== ULONG_MAX
)
467 tz
= strtol(zone
, NULL
, 10);
468 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
470 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
478 /* See grab_values */
479 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
482 int wholen
= strlen(who
);
483 const char *wholine
= NULL
;
485 for (i
= 0; i
< used_atom_cnt
; i
++) {
486 const char *name
= used_atom
[i
];
487 struct atom_value
*v
= &val
[i
];
488 if (!!deref
!= (*name
== '*'))
492 if (strncmp(who
, name
, wholen
))
494 if (name
[wholen
] != 0 &&
495 strcmp(name
+ wholen
, "name") &&
496 strcmp(name
+ wholen
, "email") &&
497 !starts_with(name
+ wholen
, "date"))
500 wholine
= find_wholine(who
, wholen
, buf
, sz
);
502 return; /* no point looking for it */
503 if (name
[wholen
] == 0)
504 v
->s
= copy_line(wholine
);
505 else if (!strcmp(name
+ wholen
, "name"))
506 v
->s
= copy_name(wholine
);
507 else if (!strcmp(name
+ wholen
, "email"))
508 v
->s
= copy_email(wholine
);
509 else if (starts_with(name
+ wholen
, "date"))
510 grab_date(wholine
, v
, name
);
514 * For a tag or a commit object, if "creator" or "creatordate" is
515 * requested, do something special.
517 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
518 return; /* "author" for commit object is not wanted */
520 wholine
= find_wholine(who
, wholen
, buf
, sz
);
523 for (i
= 0; i
< used_atom_cnt
; i
++) {
524 const char *name
= used_atom
[i
];
525 struct atom_value
*v
= &val
[i
];
526 if (!!deref
!= (*name
== '*'))
531 if (starts_with(name
, "creatordate"))
532 grab_date(wholine
, v
, name
);
533 else if (!strcmp(name
, "creator"))
534 v
->s
= copy_line(wholine
);
538 static void find_subpos(const char *buf
, unsigned long sz
,
539 const char **sub
, unsigned long *sublen
,
540 const char **body
, unsigned long *bodylen
,
541 unsigned long *nonsiglen
,
542 const char **sig
, unsigned long *siglen
)
545 /* skip past header until we hit empty line */
546 while (*buf
&& *buf
!= '\n') {
547 eol
= strchrnul(buf
, '\n');
552 /* skip any empty lines */
556 /* parse signature first; we might not even have a subject line */
557 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
558 *siglen
= strlen(*sig
);
560 /* subject is first non-empty line */
562 /* subject goes to first empty line */
563 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
564 eol
= strchrnul(buf
, '\n');
569 *sublen
= buf
- *sub
;
570 /* drop trailing newline, if present */
571 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
574 /* skip any empty lines */
578 *bodylen
= strlen(buf
);
579 *nonsiglen
= *sig
- buf
;
582 /* See grab_values */
583 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
586 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
587 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
589 for (i
= 0; i
< used_atom_cnt
; i
++) {
590 const char *name
= used_atom
[i
];
591 struct atom_value
*v
= &val
[i
];
592 if (!!deref
!= (*name
== '*'))
596 if (strcmp(name
, "subject") &&
597 strcmp(name
, "body") &&
598 strcmp(name
, "contents") &&
599 strcmp(name
, "contents:subject") &&
600 strcmp(name
, "contents:body") &&
601 strcmp(name
, "contents:signature"))
606 &bodypos
, &bodylen
, &nonsiglen
,
609 if (!strcmp(name
, "subject"))
610 v
->s
= copy_subject(subpos
, sublen
);
611 else if (!strcmp(name
, "contents:subject"))
612 v
->s
= copy_subject(subpos
, sublen
);
613 else if (!strcmp(name
, "body"))
614 v
->s
= xmemdupz(bodypos
, bodylen
);
615 else if (!strcmp(name
, "contents:body"))
616 v
->s
= xmemdupz(bodypos
, nonsiglen
);
617 else if (!strcmp(name
, "contents:signature"))
618 v
->s
= xmemdupz(sigpos
, siglen
);
619 else if (!strcmp(name
, "contents"))
620 v
->s
= xstrdup(subpos
);
625 * We want to have empty print-string for field requests
626 * that do not apply (e.g. "authordate" for a tag object)
628 static void fill_missing_values(struct atom_value
*val
)
631 for (i
= 0; i
< used_atom_cnt
; i
++) {
632 struct atom_value
*v
= &val
[i
];
639 * val is a list of atom_value to hold returned values. Extract
640 * the values for atoms in used_atom array out of (obj, buf, sz).
641 * when deref is false, (obj, buf, sz) is the object that is
642 * pointed at by the ref itself; otherwise it is the object the
643 * ref (which is a tag) refers to.
645 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
647 grab_common_values(val
, deref
, obj
, buf
, sz
);
650 grab_tag_values(val
, deref
, obj
, buf
, sz
);
651 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
652 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
655 grab_commit_values(val
, deref
, obj
, buf
, sz
);
656 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
657 grab_person("author", val
, deref
, obj
, buf
, sz
);
658 grab_person("committer", val
, deref
, obj
, buf
, sz
);
661 /* grab_tree_values(val, deref, obj, buf, sz); */
664 /* grab_blob_values(val, deref, obj, buf, sz); */
667 die("Eh? Object of type %d?", obj
->type
);
671 static inline char *copy_advance(char *dst
, const char *src
)
679 * Parse the object referred by ref, and grab needed value.
681 static void populate_value(struct ref_array_item
*ref
)
687 const unsigned char *tagged
;
689 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
691 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
692 unsigned char unused1
[20];
693 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
699 /* Fill in specials first */
700 for (i
= 0; i
< used_atom_cnt
; i
++) {
701 const char *name
= used_atom
[i
];
702 struct atom_value
*v
= &ref
->value
[i
];
707 struct branch
*branch
= NULL
;
709 v
->handler
= append_atom
;
716 if (starts_with(name
, "refname"))
717 refname
= ref
->refname
;
718 else if (starts_with(name
, "symref"))
719 refname
= ref
->symref
? ref
->symref
: "";
720 else if (starts_with(name
, "upstream")) {
721 const char *branch_name
;
722 /* only local branches may have an upstream */
723 if (!skip_prefix(ref
->refname
, "refs/heads/",
726 branch
= branch_get(branch_name
);
728 refname
= branch_get_upstream(branch
, NULL
);
731 } else if (starts_with(name
, "push")) {
732 const char *branch_name
;
733 if (!skip_prefix(ref
->refname
, "refs/heads/",
736 branch
= branch_get(branch_name
);
738 refname
= branch_get_push(branch
, NULL
);
741 } else if (match_atom_name(name
, "color", &valp
)) {
742 char color
[COLOR_MAXLEN
] = "";
745 die(_("expected format: %%(color:<color>)"));
746 if (color_parse(valp
, color
) < 0)
747 die(_("unable to parse format"));
748 v
->s
= xstrdup(color
);
750 } else if (!strcmp(name
, "flag")) {
751 char buf
[256], *cp
= buf
;
752 if (ref
->flag
& REF_ISSYMREF
)
753 cp
= copy_advance(cp
, ",symref");
754 if (ref
->flag
& REF_ISPACKED
)
755 cp
= copy_advance(cp
, ",packed");
760 v
->s
= xstrdup(buf
+ 1);
763 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
765 } else if (!strcmp(name
, "HEAD")) {
767 unsigned char sha1
[20];
769 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
771 if (!strcmp(ref
->refname
, head
))
779 formatp
= strchr(name
, ':');
781 int num_ours
, num_theirs
;
784 if (!strcmp(formatp
, "short"))
785 refname
= shorten_unambiguous_ref(refname
,
786 warn_ambiguous_refs
);
787 else if (!strcmp(formatp
, "track") &&
788 (starts_with(name
, "upstream") ||
789 starts_with(name
, "push"))) {
792 if (stat_tracking_info(branch
, &num_ours
,
796 if (!num_ours
&& !num_theirs
)
798 else if (!num_ours
) {
799 sprintf(buf
, "[behind %d]", num_theirs
);
801 } else if (!num_theirs
) {
802 sprintf(buf
, "[ahead %d]", num_ours
);
805 sprintf(buf
, "[ahead %d, behind %d]",
806 num_ours
, num_theirs
);
810 } else if (!strcmp(formatp
, "trackshort") &&
811 (starts_with(name
, "upstream") ||
812 starts_with(name
, "push"))) {
815 if (stat_tracking_info(branch
, &num_ours
,
819 if (!num_ours
&& !num_theirs
)
823 else if (!num_theirs
)
829 die("unknown %.*s format %s",
830 (int)(formatp
- name
), name
, formatp
);
836 int len
= strlen(refname
);
837 char *s
= xmalloc(len
+ 4);
838 sprintf(s
, "%s^{}", refname
);
843 for (i
= 0; i
< used_atom_cnt
; i
++) {
844 struct atom_value
*v
= &ref
->value
[i
];
851 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
853 die("missing object %s for %s",
854 sha1_to_hex(ref
->objectname
), ref
->refname
);
856 die("parse_object_buffer failed on %s for %s",
857 sha1_to_hex(ref
->objectname
), ref
->refname
);
859 grab_values(ref
->value
, 0, obj
, buf
, size
);
864 * If there is no atom that wants to know about tagged
865 * object, we are done.
867 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
871 * If it is a tag object, see if we use a value that derefs
872 * the object, and if we do grab the object it refers to.
874 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
877 * NEEDSWORK: This derefs tag only once, which
878 * is good to deal with chains of trust, but
879 * is not consistent with what deref_tag() does
880 * which peels the onion to the core.
882 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
884 die("missing object %s for %s",
885 sha1_to_hex(tagged
), ref
->refname
);
887 die("parse_object_buffer failed on %s for %s",
888 sha1_to_hex(tagged
), ref
->refname
);
889 grab_values(ref
->value
, 1, obj
, buf
, size
);
895 * Given a ref, return the value for the atom. This lazily gets value
896 * out of the object by calling populate value.
898 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
902 fill_missing_values(ref
->value
);
904 *v
= &ref
->value
[atom
];
907 enum contains_result
{
908 CONTAINS_UNKNOWN
= -1,
914 * Mimicking the real stack, this stack lives on the heap, avoiding stack
917 * At each recursion step, the stack items points to the commits whose
918 * ancestors are to be inspected.
920 struct contains_stack
{
922 struct contains_stack_entry
{
923 struct commit
*commit
;
924 struct commit_list
*parents
;
928 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
930 for (; want
; want
= want
->next
)
931 if (!hashcmp(want
->item
->object
.sha1
, c
->object
.sha1
))
937 * Test whether the candidate or one of its parents is contained in the list.
938 * Do not recurse to find out, though, but return -1 if inconclusive.
940 static enum contains_result
contains_test(struct commit
*candidate
,
941 const struct commit_list
*want
)
943 /* was it previously marked as containing a want commit? */
944 if (candidate
->object
.flags
& TMP_MARK
)
946 /* or marked as not possibly containing a want commit? */
947 if (candidate
->object
.flags
& UNINTERESTING
)
950 if (in_commit_list(want
, candidate
)) {
951 candidate
->object
.flags
|= TMP_MARK
;
955 if (parse_commit(candidate
) < 0)
961 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
963 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
964 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
965 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
968 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
969 const struct commit_list
*want
)
971 struct contains_stack contains_stack
= { 0, 0, NULL
};
972 int result
= contains_test(candidate
, want
);
974 if (result
!= CONTAINS_UNKNOWN
)
977 push_to_contains_stack(candidate
, &contains_stack
);
978 while (contains_stack
.nr
) {
979 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
980 struct commit
*commit
= entry
->commit
;
981 struct commit_list
*parents
= entry
->parents
;
984 commit
->object
.flags
|= UNINTERESTING
;
988 * If we just popped the stack, parents->item has been marked,
989 * therefore contains_test will return a meaningful 0 or 1.
991 else switch (contains_test(parents
->item
, want
)) {
993 commit
->object
.flags
|= TMP_MARK
;
997 entry
->parents
= parents
->next
;
999 case CONTAINS_UNKNOWN
:
1000 push_to_contains_stack(parents
->item
, &contains_stack
);
1004 free(contains_stack
.contains_stack
);
1005 return contains_test(candidate
, want
);
1008 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1010 if (filter
->with_commit_tag_algo
)
1011 return contains_tag_algo(commit
, filter
->with_commit
);
1012 return is_descendant_of(commit
, filter
->with_commit
);
1016 * Return 1 if the refname matches one of the patterns, otherwise 0.
1017 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1018 * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
1019 * matches "refs/heads/m*",too).
1021 static int match_name_as_path(const char **pattern
, const char *refname
)
1023 int namelen
= strlen(refname
);
1024 for (; *pattern
; pattern
++) {
1025 const char *p
= *pattern
;
1026 int plen
= strlen(p
);
1028 if ((plen
<= namelen
) &&
1029 !strncmp(refname
, p
, plen
) &&
1030 (refname
[plen
] == '\0' ||
1031 refname
[plen
] == '/' ||
1034 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1041 * Given a ref (sha1, refname), check if the ref belongs to the array
1042 * of sha1s. If the given ref is a tag, check if the given tag points
1043 * at one of the sha1s in the given sha1 array.
1044 * the given sha1_array.
1046 * 1. Only a single level of inderection is obtained, we might want to
1047 * change this to account for multiple levels (e.g. annotated tags
1048 * pointing to annotated tags pointing to a commit.)
1049 * 2. As the refs are cached we might know what refname peels to without
1050 * the need to parse the object via parse_object(). peel_ref() might be a
1051 * more efficient alternative to obtain the pointee.
1053 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1054 const unsigned char *sha1
,
1055 const char *refname
)
1057 const unsigned char *tagged_sha1
= NULL
;
1060 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1062 obj
= parse_object(sha1
);
1064 die(_("malformed object at '%s'"), refname
);
1065 if (obj
->type
== OBJ_TAG
)
1066 tagged_sha1
= ((struct tag
*)obj
)->tagged
->sha1
;
1067 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1072 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1073 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1074 const unsigned char *objectname
,
1077 size_t len
= strlen(refname
);
1078 struct ref_array_item
*ref
= xcalloc(1, sizeof(struct ref_array_item
) + len
+ 1);
1079 memcpy(ref
->refname
, refname
, len
);
1080 ref
->refname
[len
] = '\0';
1081 hashcpy(ref
->objectname
, objectname
);
1088 * A call-back given to for_each_ref(). Filter refs and keep them for
1089 * later object processing.
1091 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1093 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1094 struct ref_filter
*filter
= ref_cbdata
->filter
;
1095 struct ref_array_item
*ref
;
1096 struct commit
*commit
= NULL
;
1098 if (flag
& REF_BAD_NAME
) {
1099 warning("ignoring ref with broken name %s", refname
);
1103 if (flag
& REF_ISBROKEN
) {
1104 warning("ignoring broken ref %s", refname
);
1108 if (*filter
->name_patterns
&& !match_name_as_path(filter
->name_patterns
, refname
))
1111 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1115 * A merge filter is applied on refs pointing to commits. Hence
1116 * obtain the commit using the 'oid' available and discard all
1117 * non-commits early. The actual filtering is done later.
1119 if (filter
->merge_commit
|| filter
->with_commit
) {
1120 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1123 /* We perform the filtering for the '--contains' option */
1124 if (filter
->with_commit
&&
1125 !commit_contains(filter
, commit
))
1130 * We do not open the object yet; sort may only need refname
1131 * to do its job and the resulting list may yet to be pruned
1132 * by maxcount logic.
1134 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1135 ref
->commit
= commit
;
1137 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1138 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1142 /* Free memory allocated for a ref_array_item */
1143 static void free_array_item(struct ref_array_item
*item
)
1145 free((char *)item
->symref
);
1149 /* Free all memory allocated for ref_array */
1150 void ref_array_clear(struct ref_array
*array
)
1154 for (i
= 0; i
< array
->nr
; i
++)
1155 free_array_item(array
->items
[i
]);
1157 array
->items
= NULL
;
1158 array
->nr
= array
->alloc
= 0;
1161 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1163 struct rev_info revs
;
1165 struct ref_filter
*filter
= ref_cbdata
->filter
;
1166 struct ref_array
*array
= ref_cbdata
->array
;
1167 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1169 init_revisions(&revs
, NULL
);
1171 for (i
= 0; i
< array
->nr
; i
++) {
1172 struct ref_array_item
*item
= array
->items
[i
];
1173 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1174 to_clear
[i
] = item
->commit
;
1177 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1178 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1181 if (prepare_revision_walk(&revs
))
1182 die(_("revision walk setup failed"));
1187 for (i
= 0; i
< old_nr
; i
++) {
1188 struct ref_array_item
*item
= array
->items
[i
];
1189 struct commit
*commit
= item
->commit
;
1191 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1193 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1194 array
->items
[array
->nr
++] = array
->items
[i
];
1196 free_array_item(item
);
1199 for (i
= 0; i
< old_nr
; i
++)
1200 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1201 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1206 * API for filtering a set of refs. Based on the type of refs the user
1207 * has requested, we iterate through those refs and apply filters
1208 * as per the given ref_filter structure and finally store the
1209 * filtered refs in the ref_array structure.
1211 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1213 struct ref_filter_cbdata ref_cbdata
;
1216 ref_cbdata
.array
= array
;
1217 ref_cbdata
.filter
= filter
;
1219 /* Simple per-ref filtering */
1220 if (type
& (FILTER_REFS_ALL
| FILTER_REFS_INCLUDE_BROKEN
))
1221 ret
= for_each_rawref(ref_filter_handler
, &ref_cbdata
);
1222 else if (type
& FILTER_REFS_ALL
)
1223 ret
= for_each_ref(ref_filter_handler
, &ref_cbdata
);
1225 die("filter_refs: invalid type");
1227 /* Filters that need revision walking */
1228 if (filter
->merge_commit
)
1229 do_merge_filter(&ref_cbdata
);
1234 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1236 struct atom_value
*va
, *vb
;
1238 cmp_type cmp_type
= used_atom_type
[s
->atom
];
1240 get_ref_atom_value(a
, s
->atom
, &va
);
1241 get_ref_atom_value(b
, s
->atom
, &vb
);
1244 cmp
= strcmp(va
->s
, vb
->s
);
1247 if (va
->ul
< vb
->ul
)
1249 else if (va
->ul
== vb
->ul
)
1255 return (s
->reverse
) ? -cmp
: cmp
;
1258 static struct ref_sorting
*ref_sorting
;
1259 static int compare_refs(const void *a_
, const void *b_
)
1261 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1262 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1263 struct ref_sorting
*s
;
1265 for (s
= ref_sorting
; s
; s
= s
->next
) {
1266 int cmp
= cmp_ref_sorting(s
, a
, b
);
1273 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1275 ref_sorting
= sorting
;
1276 qsort(array
->items
, array
->nr
, sizeof(struct ref_array_item
*), compare_refs
);
1279 static int hex1(char ch
)
1281 if ('0' <= ch
&& ch
<= '9')
1283 else if ('a' <= ch
&& ch
<= 'f')
1284 return ch
- 'a' + 10;
1285 else if ('A' <= ch
&& ch
<= 'F')
1286 return ch
- 'A' + 10;
1289 static int hex2(const char *cp
)
1292 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
1297 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1299 struct strbuf
*s
= &state
->stack
->output
;
1301 while (*cp
&& (!ep
|| cp
< ep
)) {
1306 int ch
= hex2(cp
+ 1);
1308 strbuf_addch(s
, ch
);
1314 strbuf_addch(s
, *cp
);
1319 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1321 const char *cp
, *sp
, *ep
;
1322 struct strbuf
*final_buf
;
1323 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1325 state
.quote_style
= quote_style
;
1326 push_stack_element(&state
.stack
);
1328 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1329 struct atom_value
*atomv
;
1331 ep
= strchr(sp
, ')');
1333 append_literal(cp
, sp
, &state
);
1334 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1335 atomv
->handler(atomv
, &state
);
1338 sp
= cp
+ strlen(cp
);
1339 append_literal(cp
, sp
, &state
);
1341 if (need_color_reset_at_eol
) {
1342 struct atom_value resetv
;
1343 char color
[COLOR_MAXLEN
] = "";
1345 if (color_parse("reset", color
) < 0)
1346 die("BUG: couldn't parse 'reset' as a color");
1348 append_atom(&resetv
, &state
);
1350 final_buf
= &state
.stack
->output
;
1351 fwrite(final_buf
->buf
, 1, final_buf
->len
, stdout
);
1352 pop_stack_element(&state
.stack
);
1356 /* If no sorting option is given, use refname to sort as default */
1357 struct ref_sorting
*ref_default_sorting(void)
1359 static const char cstr_name
[] = "refname";
1361 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1363 sorting
->next
= NULL
;
1364 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1368 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1370 struct ref_sorting
**sorting_tail
= opt
->value
;
1371 struct ref_sorting
*s
;
1374 if (!arg
) /* should --no-sort void the list ? */
1377 s
= xcalloc(1, sizeof(*s
));
1378 s
->next
= *sorting_tail
;
1386 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1390 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1392 struct ref_filter
*rf
= opt
->value
;
1393 unsigned char sha1
[20];
1395 rf
->merge
= starts_with(opt
->long_name
, "no")
1396 ? REF_FILTER_MERGED_OMIT
1397 : REF_FILTER_MERGED_INCLUDE
;
1399 if (get_sha1(arg
, sha1
))
1400 die(_("malformed object name %s"), arg
);
1402 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1403 if (!rf
->merge_commit
)
1404 return opterror(opt
, "must point to a commit", 0);