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" },
59 * An atom is a valid field atom listed above, possibly prefixed with
60 * a "*" to denote deref_tag().
62 * We parse given format string and sort specifiers, and make a list
63 * of properties that we need to extract out of objects. ref_array_item
64 * structure will hold an array of values extracted that can be
65 * indexed with the "atom number", which is an index into this
68 static const char **used_atom
;
69 static cmp_type
*used_atom_type
;
70 static int used_atom_cnt
, need_tagged
, need_symref
;
71 static int need_color_reset_at_eol
;
74 * Used to parse format string and sort specifiers
76 int parse_ref_filter_atom(const char *atom
, const char *ep
)
82 if (*sp
== '*' && sp
< ep
)
85 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
87 /* Do we have the atom already used elsewhere? */
88 for (i
= 0; i
< used_atom_cnt
; i
++) {
89 int len
= strlen(used_atom
[i
]);
90 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
94 /* Is the atom a valid one? */
95 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
96 int len
= strlen(valid_atom
[i
].name
);
98 * If the atom name has a colon, strip it and everything after
99 * it off - it specifies the format for this entry, and
100 * shouldn't be used for checking against the valid_atom
103 const char *formatp
= strchr(sp
, ':');
104 if (!formatp
|| ep
< formatp
)
106 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
110 if (ARRAY_SIZE(valid_atom
) <= i
)
111 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
113 /* Add it in, including the deref prefix */
116 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
117 REALLOC_ARRAY(used_atom_type
, used_atom_cnt
);
118 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
119 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
122 if (!strcmp(used_atom
[at
], "symref"))
128 * In a format string, find the next occurrence of %(atom).
130 static const char *find_next(const char *cp
)
135 * %( is the start of an atom;
136 * %% is a quoted per-cent.
140 else if (cp
[1] == '%')
141 cp
++; /* skip over two % */
142 /* otherwise this is a singleton, literal % */
150 * Make sure the format string is well formed, and parse out
153 int verify_ref_format(const char *format
)
157 need_color_reset_at_eol
= 0;
158 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
159 const char *color
, *ep
= strchr(sp
, ')');
163 return error("malformed format string %s", sp
);
164 /* sp points at "%(" and ep points at the closing ")" */
165 at
= parse_ref_filter_atom(sp
+ 2, ep
);
168 if (skip_prefix(used_atom
[at
], "color:", &color
))
169 need_color_reset_at_eol
= !!strcmp(color
, "reset");
175 * Given an object name, read the object data and size, and return a
176 * "struct object". If the object data we are returning is also borrowed
177 * by the "struct object" representation, set *eaten as well---it is a
178 * signal from parse_object_buffer to us not to free the buffer.
180 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
182 enum object_type type
;
183 void *buf
= read_sha1_file(sha1
, &type
, sz
);
186 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
192 static int grab_objectname(const char *name
, const unsigned char *sha1
,
193 struct atom_value
*v
)
195 if (!strcmp(name
, "objectname")) {
196 char *s
= xmalloc(41);
197 strcpy(s
, sha1_to_hex(sha1
));
201 if (!strcmp(name
, "objectname:short")) {
202 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
208 /* See grab_values */
209 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
213 for (i
= 0; i
< used_atom_cnt
; i
++) {
214 const char *name
= used_atom
[i
];
215 struct atom_value
*v
= &val
[i
];
216 if (!!deref
!= (*name
== '*'))
220 if (!strcmp(name
, "objecttype"))
221 v
->s
= typename(obj
->type
);
222 else if (!strcmp(name
, "objectsize")) {
223 char *s
= xmalloc(40);
224 sprintf(s
, "%lu", sz
);
229 grab_objectname(name
, obj
->sha1
, v
);
233 /* See grab_values */
234 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
237 struct tag
*tag
= (struct tag
*) obj
;
239 for (i
= 0; i
< used_atom_cnt
; i
++) {
240 const char *name
= used_atom
[i
];
241 struct atom_value
*v
= &val
[i
];
242 if (!!deref
!= (*name
== '*'))
246 if (!strcmp(name
, "tag"))
248 else if (!strcmp(name
, "type") && tag
->tagged
)
249 v
->s
= typename(tag
->tagged
->type
);
250 else if (!strcmp(name
, "object") && tag
->tagged
) {
251 char *s
= xmalloc(41);
252 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
258 /* See grab_values */
259 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
262 struct commit
*commit
= (struct commit
*) obj
;
264 for (i
= 0; i
< used_atom_cnt
; i
++) {
265 const char *name
= used_atom
[i
];
266 struct atom_value
*v
= &val
[i
];
267 if (!!deref
!= (*name
== '*'))
271 if (!strcmp(name
, "tree")) {
272 char *s
= xmalloc(41);
273 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
276 if (!strcmp(name
, "numparent")) {
277 char *s
= xmalloc(40);
278 v
->ul
= commit_list_count(commit
->parents
);
279 sprintf(s
, "%lu", v
->ul
);
282 else if (!strcmp(name
, "parent")) {
283 int num
= commit_list_count(commit
->parents
);
285 struct commit_list
*parents
;
286 char *s
= xmalloc(41 * num
+ 1);
288 for (i
= 0, parents
= commit
->parents
;
290 parents
= parents
->next
, i
= i
+ 41) {
291 struct commit
*parent
= parents
->item
;
292 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
302 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
306 if (!strncmp(buf
, who
, wholen
) &&
308 return buf
+ wholen
+ 1;
309 eol
= strchr(buf
, '\n');
314 return ""; /* end of header */
320 static const char *copy_line(const char *buf
)
322 const char *eol
= strchrnul(buf
, '\n');
323 return xmemdupz(buf
, eol
- buf
);
326 static const char *copy_name(const char *buf
)
329 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
330 if (!strncmp(cp
, " <", 2))
331 return xmemdupz(buf
, cp
- buf
);
336 static const char *copy_email(const char *buf
)
338 const char *email
= strchr(buf
, '<');
342 eoemail
= strchr(email
, '>');
345 return xmemdupz(email
, eoemail
+ 1 - email
);
348 static char *copy_subject(const char *buf
, unsigned long len
)
350 char *r
= xmemdupz(buf
, len
);
353 for (i
= 0; i
< len
; i
++)
360 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
362 const char *eoemail
= strstr(buf
, "> ");
364 unsigned long timestamp
;
366 enum date_mode date_mode
= DATE_NORMAL
;
370 * We got here because atomname ends in "date" or "date<something>";
371 * it's not possible that <something> is not ":<format>" because
372 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
373 * ":" means no format is specified, and use the default.
375 formatp
= strchr(atomname
, ':');
376 if (formatp
!= NULL
) {
378 date_mode
= parse_date_format(formatp
);
383 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
384 if (timestamp
== ULONG_MAX
)
386 tz
= strtol(zone
, NULL
, 10);
387 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
389 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
397 /* See grab_values */
398 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
401 int wholen
= strlen(who
);
402 const char *wholine
= NULL
;
404 for (i
= 0; i
< used_atom_cnt
; i
++) {
405 const char *name
= used_atom
[i
];
406 struct atom_value
*v
= &val
[i
];
407 if (!!deref
!= (*name
== '*'))
411 if (strncmp(who
, name
, wholen
))
413 if (name
[wholen
] != 0 &&
414 strcmp(name
+ wholen
, "name") &&
415 strcmp(name
+ wholen
, "email") &&
416 !starts_with(name
+ wholen
, "date"))
419 wholine
= find_wholine(who
, wholen
, buf
, sz
);
421 return; /* no point looking for it */
422 if (name
[wholen
] == 0)
423 v
->s
= copy_line(wholine
);
424 else if (!strcmp(name
+ wholen
, "name"))
425 v
->s
= copy_name(wholine
);
426 else if (!strcmp(name
+ wholen
, "email"))
427 v
->s
= copy_email(wholine
);
428 else if (starts_with(name
+ wholen
, "date"))
429 grab_date(wholine
, v
, name
);
433 * For a tag or a commit object, if "creator" or "creatordate" is
434 * requested, do something special.
436 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
437 return; /* "author" for commit object is not wanted */
439 wholine
= find_wholine(who
, wholen
, buf
, sz
);
442 for (i
= 0; i
< used_atom_cnt
; i
++) {
443 const char *name
= used_atom
[i
];
444 struct atom_value
*v
= &val
[i
];
445 if (!!deref
!= (*name
== '*'))
450 if (starts_with(name
, "creatordate"))
451 grab_date(wholine
, v
, name
);
452 else if (!strcmp(name
, "creator"))
453 v
->s
= copy_line(wholine
);
457 static void find_subpos(const char *buf
, unsigned long sz
,
458 const char **sub
, unsigned long *sublen
,
459 const char **body
, unsigned long *bodylen
,
460 unsigned long *nonsiglen
,
461 const char **sig
, unsigned long *siglen
)
464 /* skip past header until we hit empty line */
465 while (*buf
&& *buf
!= '\n') {
466 eol
= strchrnul(buf
, '\n');
471 /* skip any empty lines */
475 /* parse signature first; we might not even have a subject line */
476 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
477 *siglen
= strlen(*sig
);
479 /* subject is first non-empty line */
481 /* subject goes to first empty line */
482 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
483 eol
= strchrnul(buf
, '\n');
488 *sublen
= buf
- *sub
;
489 /* drop trailing newline, if present */
490 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
493 /* skip any empty lines */
497 *bodylen
= strlen(buf
);
498 *nonsiglen
= *sig
- buf
;
501 /* See grab_values */
502 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
505 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
506 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
508 for (i
= 0; i
< used_atom_cnt
; i
++) {
509 const char *name
= used_atom
[i
];
510 struct atom_value
*v
= &val
[i
];
511 if (!!deref
!= (*name
== '*'))
515 if (strcmp(name
, "subject") &&
516 strcmp(name
, "body") &&
517 strcmp(name
, "contents") &&
518 strcmp(name
, "contents:subject") &&
519 strcmp(name
, "contents:body") &&
520 strcmp(name
, "contents:signature"))
525 &bodypos
, &bodylen
, &nonsiglen
,
528 if (!strcmp(name
, "subject"))
529 v
->s
= copy_subject(subpos
, sublen
);
530 else if (!strcmp(name
, "contents:subject"))
531 v
->s
= copy_subject(subpos
, sublen
);
532 else if (!strcmp(name
, "body"))
533 v
->s
= xmemdupz(bodypos
, bodylen
);
534 else if (!strcmp(name
, "contents:body"))
535 v
->s
= xmemdupz(bodypos
, nonsiglen
);
536 else if (!strcmp(name
, "contents:signature"))
537 v
->s
= xmemdupz(sigpos
, siglen
);
538 else if (!strcmp(name
, "contents"))
539 v
->s
= xstrdup(subpos
);
544 * We want to have empty print-string for field requests
545 * that do not apply (e.g. "authordate" for a tag object)
547 static void fill_missing_values(struct atom_value
*val
)
550 for (i
= 0; i
< used_atom_cnt
; i
++) {
551 struct atom_value
*v
= &val
[i
];
558 * val is a list of atom_value to hold returned values. Extract
559 * the values for atoms in used_atom array out of (obj, buf, sz).
560 * when deref is false, (obj, buf, sz) is the object that is
561 * pointed at by the ref itself; otherwise it is the object the
562 * ref (which is a tag) refers to.
564 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
566 grab_common_values(val
, deref
, obj
, buf
, sz
);
569 grab_tag_values(val
, deref
, obj
, buf
, sz
);
570 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
571 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
574 grab_commit_values(val
, deref
, obj
, buf
, sz
);
575 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
576 grab_person("author", val
, deref
, obj
, buf
, sz
);
577 grab_person("committer", val
, deref
, obj
, buf
, sz
);
580 /* grab_tree_values(val, deref, obj, buf, sz); */
583 /* grab_blob_values(val, deref, obj, buf, sz); */
586 die("Eh? Object of type %d?", obj
->type
);
590 static inline char *copy_advance(char *dst
, const char *src
)
598 * Parse the object referred by ref, and grab needed value.
600 static void populate_value(struct ref_array_item
*ref
)
606 const unsigned char *tagged
;
608 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
610 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
611 unsigned char unused1
[20];
612 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
618 /* Fill in specials first */
619 for (i
= 0; i
< used_atom_cnt
; i
++) {
620 const char *name
= used_atom
[i
];
621 struct atom_value
*v
= &ref
->value
[i
];
625 struct branch
*branch
= NULL
;
632 if (starts_with(name
, "refname"))
633 refname
= ref
->refname
;
634 else if (starts_with(name
, "symref"))
635 refname
= ref
->symref
? ref
->symref
: "";
636 else if (starts_with(name
, "upstream")) {
637 const char *branch_name
;
638 /* only local branches may have an upstream */
639 if (!skip_prefix(ref
->refname
, "refs/heads/",
642 branch
= branch_get(branch_name
);
644 refname
= branch_get_upstream(branch
, NULL
);
647 } else if (starts_with(name
, "push")) {
648 const char *branch_name
;
649 if (!skip_prefix(ref
->refname
, "refs/heads/",
652 branch
= branch_get(branch_name
);
654 refname
= branch_get_push(branch
, NULL
);
657 } else if (starts_with(name
, "color:")) {
658 char color
[COLOR_MAXLEN
] = "";
660 if (color_parse(name
+ 6, color
) < 0)
661 die(_("unable to parse format"));
662 v
->s
= xstrdup(color
);
664 } else if (!strcmp(name
, "flag")) {
665 char buf
[256], *cp
= buf
;
666 if (ref
->flag
& REF_ISSYMREF
)
667 cp
= copy_advance(cp
, ",symref");
668 if (ref
->flag
& REF_ISPACKED
)
669 cp
= copy_advance(cp
, ",packed");
674 v
->s
= xstrdup(buf
+ 1);
677 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
679 } else if (!strcmp(name
, "HEAD")) {
681 unsigned char sha1
[20];
683 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
685 if (!strcmp(ref
->refname
, head
))
693 formatp
= strchr(name
, ':');
695 int num_ours
, num_theirs
;
698 if (!strcmp(formatp
, "short"))
699 refname
= shorten_unambiguous_ref(refname
,
700 warn_ambiguous_refs
);
701 else if (!strcmp(formatp
, "track") &&
702 (starts_with(name
, "upstream") ||
703 starts_with(name
, "push"))) {
706 if (stat_tracking_info(branch
, &num_ours
,
710 if (!num_ours
&& !num_theirs
)
712 else if (!num_ours
) {
713 sprintf(buf
, "[behind %d]", num_theirs
);
715 } else if (!num_theirs
) {
716 sprintf(buf
, "[ahead %d]", num_ours
);
719 sprintf(buf
, "[ahead %d, behind %d]",
720 num_ours
, num_theirs
);
724 } else if (!strcmp(formatp
, "trackshort") &&
725 (starts_with(name
, "upstream") ||
726 starts_with(name
, "push"))) {
729 if (stat_tracking_info(branch
, &num_ours
,
733 if (!num_ours
&& !num_theirs
)
737 else if (!num_theirs
)
743 die("unknown %.*s format %s",
744 (int)(formatp
- name
), name
, formatp
);
750 int len
= strlen(refname
);
751 char *s
= xmalloc(len
+ 4);
752 sprintf(s
, "%s^{}", refname
);
757 for (i
= 0; i
< used_atom_cnt
; i
++) {
758 struct atom_value
*v
= &ref
->value
[i
];
765 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
767 die("missing object %s for %s",
768 sha1_to_hex(ref
->objectname
), ref
->refname
);
770 die("parse_object_buffer failed on %s for %s",
771 sha1_to_hex(ref
->objectname
), ref
->refname
);
773 grab_values(ref
->value
, 0, obj
, buf
, size
);
778 * If there is no atom that wants to know about tagged
779 * object, we are done.
781 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
785 * If it is a tag object, see if we use a value that derefs
786 * the object, and if we do grab the object it refers to.
788 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
791 * NEEDSWORK: This derefs tag only once, which
792 * is good to deal with chains of trust, but
793 * is not consistent with what deref_tag() does
794 * which peels the onion to the core.
796 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
798 die("missing object %s for %s",
799 sha1_to_hex(tagged
), ref
->refname
);
801 die("parse_object_buffer failed on %s for %s",
802 sha1_to_hex(tagged
), ref
->refname
);
803 grab_values(ref
->value
, 1, obj
, buf
, size
);
809 * Given a ref, return the value for the atom. This lazily gets value
810 * out of the object by calling populate value.
812 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
816 fill_missing_values(ref
->value
);
818 *v
= &ref
->value
[atom
];
821 enum contains_result
{
822 CONTAINS_UNKNOWN
= -1,
828 * Mimicking the real stack, this stack lives on the heap, avoiding stack
831 * At each recursion step, the stack items points to the commits whose
832 * ancestors are to be inspected.
834 struct contains_stack
{
836 struct contains_stack_entry
{
837 struct commit
*commit
;
838 struct commit_list
*parents
;
842 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
844 for (; want
; want
= want
->next
)
845 if (!hashcmp(want
->item
->object
.sha1
, c
->object
.sha1
))
851 * Test whether the candidate or one of its parents is contained in the list.
852 * Do not recurse to find out, though, but return -1 if inconclusive.
854 static enum contains_result
contains_test(struct commit
*candidate
,
855 const struct commit_list
*want
)
857 /* was it previously marked as containing a want commit? */
858 if (candidate
->object
.flags
& TMP_MARK
)
860 /* or marked as not possibly containing a want commit? */
861 if (candidate
->object
.flags
& UNINTERESTING
)
864 if (in_commit_list(want
, candidate
)) {
865 candidate
->object
.flags
|= TMP_MARK
;
869 if (parse_commit(candidate
) < 0)
875 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
877 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
878 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
879 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
882 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
883 const struct commit_list
*want
)
885 struct contains_stack contains_stack
= { 0, 0, NULL
};
886 int result
= contains_test(candidate
, want
);
888 if (result
!= CONTAINS_UNKNOWN
)
891 push_to_contains_stack(candidate
, &contains_stack
);
892 while (contains_stack
.nr
) {
893 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
894 struct commit
*commit
= entry
->commit
;
895 struct commit_list
*parents
= entry
->parents
;
898 commit
->object
.flags
|= UNINTERESTING
;
902 * If we just popped the stack, parents->item has been marked,
903 * therefore contains_test will return a meaningful 0 or 1.
905 else switch (contains_test(parents
->item
, want
)) {
907 commit
->object
.flags
|= TMP_MARK
;
911 entry
->parents
= parents
->next
;
913 case CONTAINS_UNKNOWN
:
914 push_to_contains_stack(parents
->item
, &contains_stack
);
918 free(contains_stack
.contains_stack
);
919 return contains_test(candidate
, want
);
922 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
924 if (filter
->with_commit_tag_algo
)
925 return contains_tag_algo(commit
, filter
->with_commit
);
926 return is_descendant_of(commit
, filter
->with_commit
);
930 * Return 1 if the refname matches one of the patterns, otherwise 0.
931 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
932 * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
933 * matches "refs/heads/m*",too).
935 static int match_name_as_path(const char **pattern
, const char *refname
)
937 int namelen
= strlen(refname
);
938 for (; *pattern
; pattern
++) {
939 const char *p
= *pattern
;
940 int plen
= strlen(p
);
942 if ((plen
<= namelen
) &&
943 !strncmp(refname
, p
, plen
) &&
944 (refname
[plen
] == '\0' ||
945 refname
[plen
] == '/' ||
948 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
955 * Given a ref (sha1, refname), check if the ref belongs to the array
956 * of sha1s. If the given ref is a tag, check if the given tag points
957 * at one of the sha1s in the given sha1 array.
958 * the given sha1_array.
960 * 1. Only a single level of inderection is obtained, we might want to
961 * change this to account for multiple levels (e.g. annotated tags
962 * pointing to annotated tags pointing to a commit.)
963 * 2. As the refs are cached we might know what refname peels to without
964 * the need to parse the object via parse_object(). peel_ref() might be a
965 * more efficient alternative to obtain the pointee.
967 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
968 const unsigned char *sha1
,
971 const unsigned char *tagged_sha1
= NULL
;
974 if (sha1_array_lookup(points_at
, sha1
) >= 0)
976 obj
= parse_object(sha1
);
978 die(_("malformed object at '%s'"), refname
);
979 if (obj
->type
== OBJ_TAG
)
980 tagged_sha1
= ((struct tag
*)obj
)->tagged
->sha1
;
981 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
986 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
987 static struct ref_array_item
*new_ref_array_item(const char *refname
,
988 const unsigned char *objectname
,
991 size_t len
= strlen(refname
);
992 struct ref_array_item
*ref
= xcalloc(1, sizeof(struct ref_array_item
) + len
+ 1);
993 memcpy(ref
->refname
, refname
, len
);
994 ref
->refname
[len
] = '\0';
995 hashcpy(ref
->objectname
, objectname
);
1002 * A call-back given to for_each_ref(). Filter refs and keep them for
1003 * later object processing.
1005 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1007 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1008 struct ref_filter
*filter
= ref_cbdata
->filter
;
1009 struct ref_array_item
*ref
;
1010 struct commit
*commit
= NULL
;
1012 if (flag
& REF_BAD_NAME
) {
1013 warning("ignoring ref with broken name %s", refname
);
1017 if (*filter
->name_patterns
&& !match_name_as_path(filter
->name_patterns
, refname
))
1020 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1024 * A merge filter is applied on refs pointing to commits. Hence
1025 * obtain the commit using the 'oid' available and discard all
1026 * non-commits early. The actual filtering is done later.
1028 if (filter
->merge_commit
|| filter
->with_commit
) {
1029 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1032 /* We perform the filtering for the '--contains' option */
1033 if (filter
->with_commit
&&
1034 !commit_contains(filter
, commit
))
1039 * We do not open the object yet; sort may only need refname
1040 * to do its job and the resulting list may yet to be pruned
1041 * by maxcount logic.
1043 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1044 ref
->commit
= commit
;
1046 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1047 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1051 /* Free memory allocated for a ref_array_item */
1052 static void free_array_item(struct ref_array_item
*item
)
1054 free((char *)item
->symref
);
1058 /* Free all memory allocated for ref_array */
1059 void ref_array_clear(struct ref_array
*array
)
1063 for (i
= 0; i
< array
->nr
; i
++)
1064 free_array_item(array
->items
[i
]);
1066 array
->items
= NULL
;
1067 array
->nr
= array
->alloc
= 0;
1070 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1072 struct rev_info revs
;
1074 struct ref_filter
*filter
= ref_cbdata
->filter
;
1075 struct ref_array
*array
= ref_cbdata
->array
;
1076 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1078 init_revisions(&revs
, NULL
);
1080 for (i
= 0; i
< array
->nr
; i
++) {
1081 struct ref_array_item
*item
= array
->items
[i
];
1082 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1083 to_clear
[i
] = item
->commit
;
1086 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1087 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1090 if (prepare_revision_walk(&revs
))
1091 die(_("revision walk setup failed"));
1096 for (i
= 0; i
< old_nr
; i
++) {
1097 struct ref_array_item
*item
= array
->items
[i
];
1098 struct commit
*commit
= item
->commit
;
1100 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1102 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1103 array
->items
[array
->nr
++] = array
->items
[i
];
1105 free_array_item(item
);
1108 for (i
= 0; i
< old_nr
; i
++)
1109 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1110 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1115 * API for filtering a set of refs. Based on the type of refs the user
1116 * has requested, we iterate through those refs and apply filters
1117 * as per the given ref_filter structure and finally store the
1118 * filtered refs in the ref_array structure.
1120 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1122 struct ref_filter_cbdata ref_cbdata
;
1125 ref_cbdata
.array
= array
;
1126 ref_cbdata
.filter
= filter
;
1128 /* Simple per-ref filtering */
1129 if (type
& (FILTER_REFS_ALL
| FILTER_REFS_INCLUDE_BROKEN
))
1130 ret
= for_each_rawref(ref_filter_handler
, &ref_cbdata
);
1131 else if (type
& FILTER_REFS_ALL
)
1132 ret
= for_each_ref(ref_filter_handler
, &ref_cbdata
);
1134 die("filter_refs: invalid type");
1136 /* Filters that need revision walking */
1137 if (filter
->merge_commit
)
1138 do_merge_filter(&ref_cbdata
);
1143 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1145 struct atom_value
*va
, *vb
;
1147 cmp_type cmp_type
= used_atom_type
[s
->atom
];
1149 get_ref_atom_value(a
, s
->atom
, &va
);
1150 get_ref_atom_value(b
, s
->atom
, &vb
);
1153 cmp
= strcmp(va
->s
, vb
->s
);
1156 if (va
->ul
< vb
->ul
)
1158 else if (va
->ul
== vb
->ul
)
1164 return (s
->reverse
) ? -cmp
: cmp
;
1167 static struct ref_sorting
*ref_sorting
;
1168 static int compare_refs(const void *a_
, const void *b_
)
1170 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1171 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1172 struct ref_sorting
*s
;
1174 for (s
= ref_sorting
; s
; s
= s
->next
) {
1175 int cmp
= cmp_ref_sorting(s
, a
, b
);
1182 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1184 ref_sorting
= sorting
;
1185 qsort(array
->items
, array
->nr
, sizeof(struct ref_array_item
*), compare_refs
);
1188 static void print_value(struct atom_value
*v
, int quote_style
)
1190 struct strbuf sb
= STRBUF_INIT
;
1191 switch (quote_style
) {
1193 fputs(v
->s
, stdout
);
1196 sq_quote_buf(&sb
, v
->s
);
1199 perl_quote_buf(&sb
, v
->s
);
1202 python_quote_buf(&sb
, v
->s
);
1205 tcl_quote_buf(&sb
, v
->s
);
1208 if (quote_style
!= QUOTE_NONE
) {
1209 fputs(sb
.buf
, stdout
);
1210 strbuf_release(&sb
);
1214 static int hex1(char ch
)
1216 if ('0' <= ch
&& ch
<= '9')
1218 else if ('a' <= ch
&& ch
<= 'f')
1219 return ch
- 'a' + 10;
1220 else if ('A' <= ch
&& ch
<= 'F')
1221 return ch
- 'A' + 10;
1224 static int hex2(const char *cp
)
1227 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
1232 static void emit(const char *cp
, const char *ep
)
1234 while (*cp
&& (!ep
|| cp
< ep
)) {
1239 int ch
= hex2(cp
+ 1);
1252 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1254 const char *cp
, *sp
, *ep
;
1256 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1257 struct atom_value
*atomv
;
1259 ep
= strchr(sp
, ')');
1262 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1263 print_value(atomv
, quote_style
);
1266 sp
= cp
+ strlen(cp
);
1269 if (need_color_reset_at_eol
) {
1270 struct atom_value resetv
;
1271 char color
[COLOR_MAXLEN
] = "";
1273 if (color_parse("reset", color
) < 0)
1274 die("BUG: couldn't parse 'reset' as a color");
1276 print_value(&resetv
, quote_style
);
1281 /* If no sorting option is given, use refname to sort as default */
1282 struct ref_sorting
*ref_default_sorting(void)
1284 static const char cstr_name
[] = "refname";
1286 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1288 sorting
->next
= NULL
;
1289 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1293 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1295 struct ref_sorting
**sorting_tail
= opt
->value
;
1296 struct ref_sorting
*s
;
1299 if (!arg
) /* should --no-sort void the list ? */
1302 s
= xcalloc(1, sizeof(*s
));
1303 s
->next
= *sorting_tail
;
1311 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1315 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1317 struct ref_filter
*rf
= opt
->value
;
1318 unsigned char sha1
[20];
1320 rf
->merge
= starts_with(opt
->long_name
, "no")
1321 ? REF_FILTER_MERGED_OMIT
1322 : REF_FILTER_MERGED_INCLUDE
;
1324 if (get_sha1(arg
, sha1
))
1325 die(_("malformed object name %s"), arg
);
1327 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1328 if (!rf
->merge_commit
)
1329 return opterror(opt
, "must point to a commit", 0);