]>
Commit | Line | Data |
---|---|---|
baffc0e7 | 1 | #include "builtin.h" |
9f613ddd JH |
2 | #include "cache.h" |
3 | #include "refs.h" | |
4 | #include "object.h" | |
c3428da8 | 5 | #include "parse-options.h" |
69b1cf91 | 6 | #include "ref-filter.h" |
9f613ddd | 7 | |
c3428da8 | 8 | static char const * const for_each_ref_usage[] = { |
9c9b4f2f | 9 | N_("git for-each-ref [<options>] [<pattern>]"), |
d325406e | 10 | N_("git for-each-ref [--points-at <object>]"), |
7ac04f13 | 11 | N_("git for-each-ref [(--merged | --no-merged) [<commit>]]"), |
ac3f5a34 | 12 | N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"), |
c3428da8 PH |
13 | NULL |
14 | }; | |
15 | ||
16 | int cmd_for_each_ref(int argc, const char **argv, const char *prefix) | |
9f613ddd | 17 | { |
215b5651 | 18 | int i; |
c3428da8 | 19 | const char *format = "%(objectname) %(objecttype)\t%(refname)"; |
73079d21 | 20 | struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; |
3bb16a8b | 21 | int maxcount = 0, quote_style = 0, icase = 0; |
14de7fba KN |
22 | struct ref_array array; |
23 | struct ref_filter filter; | |
9f613ddd | 24 | |
c3428da8 | 25 | struct option opts[] = { |
9fac800c | 26 | OPT_BIT('s', "shell", "e_style, |
18913521 | 27 | N_("quote placeholders suitably for shells"), QUOTE_SHELL), |
9fac800c | 28 | OPT_BIT('p', "perl", "e_style, |
18913521 | 29 | N_("quote placeholders suitably for perl"), QUOTE_PERL), |
9fac800c | 30 | OPT_BIT(0 , "python", "e_style, |
18913521 | 31 | N_("quote placeholders suitably for python"), QUOTE_PYTHON), |
9fac800c | 32 | OPT_BIT(0 , "tcl", "e_style, |
b799a696 | 33 | N_("quote placeholders suitably for Tcl"), QUOTE_TCL), |
c3428da8 PH |
34 | |
35 | OPT_GROUP(""), | |
18913521 NTND |
36 | OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")), |
37 | OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")), | |
73079d21 | 38 | OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"), |
277b9157 | 39 | N_("field name to sort on"), &parse_opt_ref_sorting), |
d325406e KN |
40 | OPT_CALLBACK(0, "points-at", &filter.points_at, |
41 | N_("object"), N_("print only refs which points at the given object"), | |
42 | parse_opt_object_name), | |
7c328348 KN |
43 | OPT_MERGED(&filter, N_("print only refs that are merged")), |
44 | OPT_NO_MERGED(&filter, N_("print only refs that are not merged")), | |
4a71109a | 45 | OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")), |
ac3f5a34 | 46 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")), |
3bb16a8b | 47 | OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
c3428da8 PH |
48 | OPT_END(), |
49 | }; | |
50 | ||
d325406e KN |
51 | memset(&array, 0, sizeof(array)); |
52 | memset(&filter, 0, sizeof(filter)); | |
53 | ||
37782920 | 54 | parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0); |
c3428da8 PH |
55 | if (maxcount < 0) { |
56 | error("invalid --count argument: `%d'", maxcount); | |
57 | usage_with_options(for_each_ref_usage, opts); | |
9f613ddd | 58 | } |
9fac800c | 59 | if (HAS_MULTI_BITS(quote_style)) { |
c9ecf4f1 | 60 | error("more than one quoting style?"); |
c3428da8 PH |
61 | usage_with_options(for_each_ref_usage, opts); |
62 | } | |
277b9157 | 63 | if (verify_ref_format(format)) |
c3428da8 | 64 | usage_with_options(for_each_ref_usage, opts); |
9f613ddd | 65 | |
73079d21 KN |
66 | if (!sorting) |
67 | sorting = ref_default_sorting(); | |
3bb16a8b NTND |
68 | sorting->ignore_case = icase; |
69 | filter.ignore_case = icase; | |
9f613ddd | 70 | |
2bb98169 BW |
71 | /* for warn_ambiguous_refs */ |
72 | git_config(git_default_config, NULL); | |
73 | ||
14de7fba | 74 | filter.name_patterns = argv; |
bef0e12b | 75 | filter.match_as_path = 1; |
14de7fba KN |
76 | filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN); |
77 | ref_array_sort(sorting, &array); | |
9f613ddd | 78 | |
14de7fba KN |
79 | if (!maxcount || array.nr < maxcount) |
80 | maxcount = array.nr; | |
9f613ddd | 81 | for (i = 0; i < maxcount; i++) |
14de7fba KN |
82 | show_ref_array_item(array.items[i], format, quote_style); |
83 | ref_array_clear(&array); | |
9f613ddd JH |
84 | return 0; |
85 | } |