]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-for-each-ref.c
10 #include "parse-options.h"
17 #define QUOTE_PYTHON 4
20 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
24 unsigned long ul
; /* used for sorting when not FIELD_STR */
28 struct ref_sort
*next
;
29 int atom
; /* index into used_atom array */
35 unsigned char objectname
[20];
36 struct atom_value
*value
;
45 { "objectsize", FIELD_ULONG
},
49 { "numparent", FIELD_ULONG
},
56 { "authordate", FIELD_TIME
},
60 { "committerdate", FIELD_TIME
},
64 { "taggerdate", FIELD_TIME
},
66 { "creatordate", FIELD_TIME
},
74 * An atom is a valid field atom listed above, possibly prefixed with
75 * a "*" to denote deref_tag().
77 * We parse given format string and sort specifiers, and make a list
78 * of properties that we need to extract out of objects. refinfo
79 * structure will hold an array of values extracted that can be
80 * indexed with the "atom number", which is an index into this
83 static const char **used_atom
;
84 static cmp_type
*used_atom_type
;
85 static int used_atom_cnt
, sort_atom_limit
, need_tagged
;
88 * Used to parse format string and sort specifiers
90 static int parse_atom(const char *atom
, const char *ep
)
96 if (*sp
== '*' && sp
< ep
)
99 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
101 /* Do we have the atom already used elsewhere? */
102 for (i
= 0; i
< used_atom_cnt
; i
++) {
103 int len
= strlen(used_atom
[i
]);
104 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
108 /* Is the atom a valid one? */
109 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
110 int len
= strlen(valid_atom
[i
].name
);
112 * If the atom name has a colon, strip it and everything after
113 * it off - it specifies the format for this entry, and
114 * shouldn't be used for checking against the valid_atom
117 const char *formatp
= strchr(sp
, ':');
118 if (!formatp
|| ep
< formatp
)
120 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
124 if (ARRAY_SIZE(valid_atom
) <= i
)
125 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
127 /* Add it in, including the deref prefix */
130 used_atom
= xrealloc(used_atom
,
131 (sizeof *used_atom
) * used_atom_cnt
);
132 used_atom_type
= xrealloc(used_atom_type
,
133 (sizeof(*used_atom_type
) * used_atom_cnt
));
134 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
135 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
142 * In a format string, find the next occurrence of %(atom).
144 static const char *find_next(const char *cp
)
149 * %( is the start of an atom;
150 * %% is a quoted per-cent.
154 else if (cp
[1] == '%')
155 cp
++; /* skip over two % */
156 /* otherwise this is a singleton, literal % */
164 * Make sure the format string is well formed, and parse out
167 static int verify_format(const char *format
)
170 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
171 const char *ep
= strchr(sp
, ')');
173 return error("malformed format string %s", sp
);
174 /* sp points at "%(" and ep points at the closing ")" */
175 parse_atom(sp
+ 2, ep
);
182 * Given an object name, read the object data and size, and return a
183 * "struct object". If the object data we are returning is also borrowed
184 * by the "struct object" representation, set *eaten as well---it is a
185 * signal from parse_object_buffer to us not to free the buffer.
187 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
189 enum object_type type
;
190 void *buf
= read_sha1_file(sha1
, &type
, sz
);
193 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
199 /* See grab_values */
200 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
204 for (i
= 0; i
< used_atom_cnt
; i
++) {
205 const char *name
= used_atom
[i
];
206 struct atom_value
*v
= &val
[i
];
207 if (!!deref
!= (*name
== '*'))
211 if (!strcmp(name
, "objecttype"))
212 v
->s
= typename(obj
->type
);
213 else if (!strcmp(name
, "objectsize")) {
214 char *s
= xmalloc(40);
215 sprintf(s
, "%lu", sz
);
219 else if (!strcmp(name
, "objectname")) {
220 char *s
= xmalloc(41);
221 strcpy(s
, sha1_to_hex(obj
->sha1
));
227 /* See grab_values */
228 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
231 struct tag
*tag
= (struct tag
*) obj
;
233 for (i
= 0; i
< used_atom_cnt
; i
++) {
234 const char *name
= used_atom
[i
];
235 struct atom_value
*v
= &val
[i
];
236 if (!!deref
!= (*name
== '*'))
240 if (!strcmp(name
, "tag"))
242 else if (!strcmp(name
, "type") && tag
->tagged
)
243 v
->s
= typename(tag
->tagged
->type
);
244 else if (!strcmp(name
, "object") && tag
->tagged
) {
245 char *s
= xmalloc(41);
246 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
252 static int num_parents(struct commit
*commit
)
254 struct commit_list
*parents
;
257 for (i
= 0, parents
= commit
->parents
;
259 parents
= parents
->next
)
264 /* See grab_values */
265 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
268 struct commit
*commit
= (struct commit
*) obj
;
270 for (i
= 0; i
< used_atom_cnt
; i
++) {
271 const char *name
= used_atom
[i
];
272 struct atom_value
*v
= &val
[i
];
273 if (!!deref
!= (*name
== '*'))
277 if (!strcmp(name
, "tree")) {
278 char *s
= xmalloc(41);
279 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
282 if (!strcmp(name
, "numparent")) {
283 char *s
= xmalloc(40);
284 v
->ul
= num_parents(commit
);
285 sprintf(s
, "%lu", v
->ul
);
288 else if (!strcmp(name
, "parent")) {
289 int num
= num_parents(commit
);
291 struct commit_list
*parents
;
292 char *s
= xmalloc(41 * num
+ 1);
294 for (i
= 0, parents
= commit
->parents
;
296 parents
= parents
->next
, i
= i
+ 41) {
297 struct commit
*parent
= parents
->item
;
298 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
308 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
312 if (!strncmp(buf
, who
, wholen
) &&
314 return buf
+ wholen
+ 1;
315 eol
= strchr(buf
, '\n');
320 return ""; /* end of header */
326 static const char *copy_line(const char *buf
)
328 const char *eol
= strchrnul(buf
, '\n');
329 return xmemdupz(buf
, eol
- buf
);
332 static const char *copy_name(const char *buf
)
335 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
336 if (!strncmp(cp
, " <", 2))
337 return xmemdupz(buf
, cp
- buf
);
342 static const char *copy_email(const char *buf
)
344 const char *email
= strchr(buf
, '<');
348 eoemail
= strchr(email
, '>');
351 return xmemdupz(email
, eoemail
+ 1 - email
);
354 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
356 const char *eoemail
= strstr(buf
, "> ");
358 unsigned long timestamp
;
360 enum date_mode date_mode
= DATE_NORMAL
;
364 * We got here because atomname ends in "date" or "date<something>";
365 * it's not possible that <something> is not ":<format>" because
366 * parse_atom() wouldn't have allowed it, so we can assume that no
367 * ":" means no format is specified, and use the default.
369 formatp
= strchr(atomname
, ':');
370 if (formatp
!= NULL
) {
372 date_mode
= parse_date_format(formatp
);
377 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
378 if (timestamp
== ULONG_MAX
)
380 tz
= strtol(zone
, NULL
, 10);
381 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
383 v
->s
= xstrdup(show_date(timestamp
, tz
, date_mode
));
391 /* See grab_values */
392 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
395 int wholen
= strlen(who
);
396 const char *wholine
= NULL
;
398 for (i
= 0; i
< used_atom_cnt
; i
++) {
399 const char *name
= used_atom
[i
];
400 struct atom_value
*v
= &val
[i
];
401 if (!!deref
!= (*name
== '*'))
405 if (strncmp(who
, name
, wholen
))
407 if (name
[wholen
] != 0 &&
408 strcmp(name
+ wholen
, "name") &&
409 strcmp(name
+ wholen
, "email") &&
410 prefixcmp(name
+ wholen
, "date"))
413 wholine
= find_wholine(who
, wholen
, buf
, sz
);
415 return; /* no point looking for it */
416 if (name
[wholen
] == 0)
417 v
->s
= copy_line(wholine
);
418 else if (!strcmp(name
+ wholen
, "name"))
419 v
->s
= copy_name(wholine
);
420 else if (!strcmp(name
+ wholen
, "email"))
421 v
->s
= copy_email(wholine
);
422 else if (!prefixcmp(name
+ wholen
, "date"))
423 grab_date(wholine
, v
, name
);
427 * For a tag or a commit object, if "creator" or "creatordate" is
428 * requested, do something special.
430 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
431 return; /* "author" for commit object is not wanted */
433 wholine
= find_wholine(who
, wholen
, buf
, sz
);
436 for (i
= 0; i
< used_atom_cnt
; i
++) {
437 const char *name
= used_atom
[i
];
438 struct atom_value
*v
= &val
[i
];
439 if (!!deref
!= (*name
== '*'))
444 if (!prefixcmp(name
, "creatordate"))
445 grab_date(wholine
, v
, name
);
446 else if (!strcmp(name
, "creator"))
447 v
->s
= copy_line(wholine
);
451 static void find_subpos(const char *buf
, unsigned long sz
, const char **sub
, const char **body
)
454 const char *eol
= strchr(buf
, '\n');
457 if (eol
[1] == '\n') {
459 break; /* found end of header */
467 *sub
= buf
; /* first non-empty line */
468 buf
= strchr(buf
, '\n');
471 return; /* no body */
474 buf
++; /* skip blank between subject and body */
478 /* See grab_values */
479 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
482 const char *subpos
= NULL
, *bodypos
= NULL
;
484 for (i
= 0; i
< used_atom_cnt
; i
++) {
485 const char *name
= used_atom
[i
];
486 struct atom_value
*v
= &val
[i
];
487 if (!!deref
!= (*name
== '*'))
491 if (strcmp(name
, "subject") &&
492 strcmp(name
, "body") &&
493 strcmp(name
, "contents"))
496 find_subpos(buf
, sz
, &subpos
, &bodypos
);
500 if (!strcmp(name
, "subject"))
501 v
->s
= copy_line(subpos
);
502 else if (!strcmp(name
, "body"))
503 v
->s
= xstrdup(bodypos
);
504 else if (!strcmp(name
, "contents"))
505 v
->s
= xstrdup(subpos
);
510 * We want to have empty print-string for field requests
511 * that do not apply (e.g. "authordate" for a tag object)
513 static void fill_missing_values(struct atom_value
*val
)
516 for (i
= 0; i
< used_atom_cnt
; i
++) {
517 struct atom_value
*v
= &val
[i
];
524 * val is a list of atom_value to hold returned values. Extract
525 * the values for atoms in used_atom array out of (obj, buf, sz).
526 * when deref is false, (obj, buf, sz) is the object that is
527 * pointed at by the ref itself; otherwise it is the object the
528 * ref (which is a tag) refers to.
530 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
532 grab_common_values(val
, deref
, obj
, buf
, sz
);
535 grab_tag_values(val
, deref
, obj
, buf
, sz
);
536 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
537 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
540 grab_commit_values(val
, deref
, obj
, buf
, sz
);
541 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
542 grab_person("author", val
, deref
, obj
, buf
, sz
);
543 grab_person("committer", val
, deref
, obj
, buf
, sz
);
546 // grab_tree_values(val, deref, obj, buf, sz);
549 // grab_blob_values(val, deref, obj, buf, sz);
552 die("Eh? Object of type %d?", obj
->type
);
557 * Parse the object referred by ref, and grab needed value.
559 static void populate_value(struct refinfo
*ref
)
565 const unsigned char *tagged
;
567 ref
->value
= xcalloc(sizeof(struct atom_value
), used_atom_cnt
);
569 /* Fill in specials first */
570 for (i
= 0; i
< used_atom_cnt
; i
++) {
571 const char *name
= used_atom
[i
];
572 struct atom_value
*v
= &ref
->value
[i
];
582 if (!prefixcmp(name
, "refname"))
583 refname
= ref
->refname
;
584 else if (!prefixcmp(name
, "upstream")) {
585 struct branch
*branch
;
586 /* only local branches may have an upstream */
587 if (prefixcmp(ref
->refname
, "refs/heads/"))
589 branch
= branch_get(ref
->refname
+ 11);
591 if (!branch
|| !branch
->merge
|| !branch
->merge
[0] ||
592 !branch
->merge
[0]->dst
)
594 refname
= branch
->merge
[0]->dst
;
599 formatp
= strchr(name
, ':');
600 /* look for "short" refname format */
603 if (!strcmp(formatp
, "short"))
604 refname
= shorten_unambiguous_ref(refname
,
605 warn_ambiguous_refs
);
607 die("unknown %.*s format %s",
608 (int)(formatp
- name
), name
, formatp
);
614 int len
= strlen(refname
);
615 char *s
= xmalloc(len
+ 4);
616 sprintf(s
, "%s^{}", refname
);
621 for (i
= 0; i
< used_atom_cnt
; i
++) {
622 struct atom_value
*v
= &ref
->value
[i
];
629 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
631 die("missing object %s for %s",
632 sha1_to_hex(ref
->objectname
), ref
->refname
);
634 die("parse_object_buffer failed on %s for %s",
635 sha1_to_hex(ref
->objectname
), ref
->refname
);
637 grab_values(ref
->value
, 0, obj
, buf
, size
);
642 * If there is no atom that wants to know about tagged
643 * object, we are done.
645 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
649 * If it is a tag object, see if we use a value that derefs
650 * the object, and if we do grab the object it refers to.
652 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
655 * NEEDSWORK: This derefs tag only once, which
656 * is good to deal with chains of trust, but
657 * is not consistent with what deref_tag() does
658 * which peels the onion to the core.
660 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
662 die("missing object %s for %s",
663 sha1_to_hex(tagged
), ref
->refname
);
665 die("parse_object_buffer failed on %s for %s",
666 sha1_to_hex(tagged
), ref
->refname
);
667 grab_values(ref
->value
, 1, obj
, buf
, size
);
673 * Given a ref, return the value for the atom. This lazily gets value
674 * out of the object by calling populate value.
676 static void get_value(struct refinfo
*ref
, int atom
, struct atom_value
**v
)
680 fill_missing_values(ref
->value
);
682 *v
= &ref
->value
[atom
];
685 struct grab_ref_cbdata
{
686 struct refinfo
**grab_array
;
687 const char **grab_pattern
;
692 * A call-back given to for_each_ref(). Filter refs and keep them for
693 * later object processing.
695 static int grab_single_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
697 struct grab_ref_cbdata
*cb
= cb_data
;
701 if (*cb
->grab_pattern
) {
702 const char **pattern
;
703 int namelen
= strlen(refname
);
704 for (pattern
= cb
->grab_pattern
; *pattern
; pattern
++) {
705 const char *p
= *pattern
;
706 int plen
= strlen(p
);
708 if ((plen
<= namelen
) &&
709 !strncmp(refname
, p
, plen
) &&
710 (refname
[plen
] == '\0' ||
711 refname
[plen
] == '/' ||
714 if (!fnmatch(p
, refname
, FNM_PATHNAME
))
722 * We do not open the object yet; sort may only need refname
723 * to do its job and the resulting list may yet to be pruned
726 ref
= xcalloc(1, sizeof(*ref
));
727 ref
->refname
= xstrdup(refname
);
728 hashcpy(ref
->objectname
, sha1
);
731 cb
->grab_array
= xrealloc(cb
->grab_array
,
732 sizeof(*cb
->grab_array
) * (cnt
+ 1));
733 cb
->grab_array
[cnt
++] = ref
;
738 static int cmp_ref_sort(struct ref_sort
*s
, struct refinfo
*a
, struct refinfo
*b
)
740 struct atom_value
*va
, *vb
;
742 cmp_type cmp_type
= used_atom_type
[s
->atom
];
744 get_value(a
, s
->atom
, &va
);
745 get_value(b
, s
->atom
, &vb
);
748 cmp
= strcmp(va
->s
, vb
->s
);
753 else if (va
->ul
== vb
->ul
)
759 return (s
->reverse
) ? -cmp
: cmp
;
762 static struct ref_sort
*ref_sort
;
763 static int compare_refs(const void *a_
, const void *b_
)
765 struct refinfo
*a
= *((struct refinfo
**)a_
);
766 struct refinfo
*b
= *((struct refinfo
**)b_
);
769 for (s
= ref_sort
; s
; s
= s
->next
) {
770 int cmp
= cmp_ref_sort(s
, a
, b
);
777 static void sort_refs(struct ref_sort
*sort
, struct refinfo
**refs
, int num_refs
)
780 qsort(refs
, num_refs
, sizeof(struct refinfo
*), compare_refs
);
783 static void print_value(struct refinfo
*ref
, int atom
, int quote_style
)
785 struct atom_value
*v
;
786 get_value(ref
, atom
, &v
);
787 switch (quote_style
) {
792 sq_quote_print(stdout
, v
->s
);
795 perl_quote_print(stdout
, v
->s
);
798 python_quote_print(stdout
, v
->s
);
801 tcl_quote_print(stdout
, v
->s
);
806 static int hex1(char ch
)
808 if ('0' <= ch
&& ch
<= '9')
810 else if ('a' <= ch
&& ch
<= 'f')
811 return ch
- 'a' + 10;
812 else if ('A' <= ch
&& ch
<= 'F')
813 return ch
- 'A' + 10;
816 static int hex2(const char *cp
)
819 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
824 static void emit(const char *cp
, const char *ep
)
826 while (*cp
&& (!ep
|| cp
< ep
)) {
831 int ch
= hex2(cp
+ 1);
844 static void show_ref(struct refinfo
*info
, const char *format
, int quote_style
)
846 const char *cp
, *sp
, *ep
;
848 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
849 ep
= strchr(sp
, ')');
852 print_value(info
, parse_atom(sp
+ 2, ep
), quote_style
);
855 sp
= cp
+ strlen(cp
);
861 static struct ref_sort
*default_sort(void)
863 static const char cstr_name
[] = "refname";
865 struct ref_sort
*sort
= xcalloc(1, sizeof(*sort
));
868 sort
->atom
= parse_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
872 static int opt_parse_sort(const struct option
*opt
, const char *arg
, int unset
)
874 struct ref_sort
**sort_tail
= opt
->value
;
878 if (!arg
) /* should --no-sort void the list ? */
881 *sort_tail
= s
= xcalloc(1, sizeof(*s
));
888 s
->atom
= parse_atom(arg
, arg
+len
);
892 static char const * const for_each_ref_usage
[] = {
893 "git for-each-ref [options] [<pattern>]",
897 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
900 const char *format
= "%(objectname) %(objecttype)\t%(refname)";
901 struct ref_sort
*sort
= NULL
, **sort_tail
= &sort
;
902 int maxcount
= 0, quote_style
= 0;
903 struct refinfo
**refs
;
904 struct grab_ref_cbdata cbdata
;
906 struct option opts
[] = {
907 OPT_BIT('s', "shell", "e_style
,
908 "quote placeholders suitably for shells", QUOTE_SHELL
),
909 OPT_BIT('p', "perl", "e_style
,
910 "quote placeholders suitably for perl", QUOTE_PERL
),
911 OPT_BIT(0 , "python", "e_style
,
912 "quote placeholders suitably for python", QUOTE_PYTHON
),
913 OPT_BIT(0 , "tcl", "e_style
,
914 "quote placeholders suitably for tcl", QUOTE_TCL
),
917 OPT_INTEGER( 0 , "count", &maxcount
, "show only <n> matched refs"),
918 OPT_STRING( 0 , "format", &format
, "format", "format to use for the output"),
919 OPT_CALLBACK(0 , "sort", sort_tail
, "key",
920 "field name to sort on", &opt_parse_sort
),
924 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
926 error("invalid --count argument: `%d'", maxcount
);
927 usage_with_options(for_each_ref_usage
, opts
);
929 if (HAS_MULTI_BITS(quote_style
)) {
930 error("more than one quoting style?");
931 usage_with_options(for_each_ref_usage
, opts
);
933 if (verify_format(format
))
934 usage_with_options(for_each_ref_usage
, opts
);
937 sort
= default_sort();
938 sort_atom_limit
= used_atom_cnt
;
940 /* for warn_ambiguous_refs */
941 git_config(git_default_config
, NULL
);
943 memset(&cbdata
, 0, sizeof(cbdata
));
944 cbdata
.grab_pattern
= argv
;
945 for_each_rawref(grab_single_ref
, &cbdata
);
946 refs
= cbdata
.grab_array
;
947 num_refs
= cbdata
.grab_cnt
;
949 sort_refs(sort
, refs
, num_refs
);
951 if (!maxcount
|| num_refs
< maxcount
)
953 for (i
= 0; i
< maxcount
; i
++)
954 show_ref(refs
[i
], format
, quote_style
);