]>
Commit | Line | Data |
---|---|---|
baffc0e7 | 1 | #include "builtin.h" |
9f613ddd | 2 | #include "cache.h" |
b2141fc1 | 3 | #include "config.h" |
f394e093 | 4 | #include "gettext.h" |
9f613ddd JH |
5 | #include "refs.h" |
6 | #include "object.h" | |
c3428da8 | 7 | #include "parse-options.h" |
69b1cf91 | 8 | #include "ref-filter.h" |
9f613ddd | 9 | |
c3428da8 | 10 | static char const * const for_each_ref_usage[] = { |
9c9b4f2f | 11 | N_("git for-each-ref [<options>] [<pattern>]"), |
d325406e | 12 | N_("git for-each-ref [--points-at <object>]"), |
21bf9339 | 13 | N_("git for-each-ref [--merged [<commit>]] [--no-merged [<commit>]]"), |
ac3f5a34 | 14 | N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"), |
c3428da8 PH |
15 | NULL |
16 | }; | |
17 | ||
18 | int cmd_for_each_ref(int argc, const char **argv, const char *prefix) | |
9f613ddd | 19 | { |
215b5651 | 20 | int i; |
98e7ab6d JH |
21 | struct ref_sorting *sorting; |
22 | struct string_list sorting_options = STRING_LIST_INIT_DUP; | |
4a68e36d | 23 | int maxcount = 0, icase = 0; |
14de7fba KN |
24 | struct ref_array array; |
25 | struct ref_filter filter; | |
4a68e36d | 26 | struct ref_format format = REF_FORMAT_INIT; |
844c3f0b ZH |
27 | struct strbuf output = STRBUF_INIT; |
28 | struct strbuf err = STRBUF_INIT; | |
9f613ddd | 29 | |
c3428da8 | 30 | struct option opts[] = { |
4a68e36d | 31 | OPT_BIT('s', "shell", &format.quote_style, |
18913521 | 32 | N_("quote placeholders suitably for shells"), QUOTE_SHELL), |
4a68e36d | 33 | OPT_BIT('p', "perl", &format.quote_style, |
18913521 | 34 | N_("quote placeholders suitably for perl"), QUOTE_PERL), |
4a68e36d | 35 | OPT_BIT(0 , "python", &format.quote_style, |
18913521 | 36 | N_("quote placeholders suitably for python"), QUOTE_PYTHON), |
4a68e36d | 37 | OPT_BIT(0 , "tcl", &format.quote_style, |
b799a696 | 38 | N_("quote placeholders suitably for Tcl"), QUOTE_TCL), |
c3428da8 PH |
39 | |
40 | OPT_GROUP(""), | |
18913521 | 41 | OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")), |
4a68e36d | 42 | OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), |
0c88bf50 | 43 | OPT__COLOR(&format.use_color, N_("respect format colors")), |
98e7ab6d | 44 | OPT_REF_SORT(&sorting_options), |
d325406e KN |
45 | OPT_CALLBACK(0, "points-at", &filter.points_at, |
46 | N_("object"), N_("print only refs which points at the given object"), | |
47 | parse_opt_object_name), | |
7c328348 KN |
48 | OPT_MERGED(&filter, N_("print only refs that are merged")), |
49 | OPT_NO_MERGED(&filter, N_("print only refs that are not merged")), | |
4a71109a | 50 | OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")), |
ac3f5a34 | 51 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")), |
3bb16a8b | 52 | OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
c3428da8 PH |
53 | OPT_END(), |
54 | }; | |
55 | ||
d325406e KN |
56 | memset(&array, 0, sizeof(array)); |
57 | memset(&filter, 0, sizeof(filter)); | |
58 | ||
4a68e36d JK |
59 | format.format = "%(objectname) %(objecttype)\t%(refname)"; |
60 | ||
d8b68686 JK |
61 | git_config(git_default_config, NULL); |
62 | ||
37782920 | 63 | parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0); |
c3428da8 PH |
64 | if (maxcount < 0) { |
65 | error("invalid --count argument: `%d'", maxcount); | |
66 | usage_with_options(for_each_ref_usage, opts); | |
9f613ddd | 67 | } |
4a68e36d | 68 | if (HAS_MULTI_BITS(format.quote_style)) { |
c9ecf4f1 | 69 | error("more than one quoting style?"); |
c3428da8 PH |
70 | usage_with_options(for_each_ref_usage, opts); |
71 | } | |
4a68e36d | 72 | if (verify_ref_format(&format)) |
c3428da8 | 73 | usage_with_options(for_each_ref_usage, opts); |
9f613ddd | 74 | |
98e7ab6d | 75 | sorting = ref_sorting_options(&sorting_options); |
7c269a7b | 76 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); |
3bb16a8b | 77 | filter.ignore_case = icase; |
9f613ddd | 78 | |
14de7fba | 79 | filter.name_patterns = argv; |
bef0e12b | 80 | filter.match_as_path = 1; |
1763334c | 81 | filter_refs(&array, &filter, FILTER_REFS_ALL); |
14de7fba | 82 | ref_array_sort(sorting, &array); |
9f613ddd | 83 | |
14de7fba KN |
84 | if (!maxcount || array.nr < maxcount) |
85 | maxcount = array.nr; | |
22f69a85 | 86 | for (i = 0; i < maxcount; i++) { |
844c3f0b ZH |
87 | strbuf_reset(&err); |
88 | strbuf_reset(&output); | |
22f69a85 ZH |
89 | if (format_ref_array_item(array.items[i], &format, &output, &err)) |
90 | die("%s", err.buf); | |
91 | fwrite(output.buf, 1, output.len, stdout); | |
92 | putchar('\n'); | |
22f69a85 | 93 | } |
844c3f0b ZH |
94 | |
95 | strbuf_release(&err); | |
96 | strbuf_release(&output); | |
14de7fba | 97 | ref_array_clear(&array); |
a317a553 AH |
98 | free_commit_list(filter.with_commit); |
99 | free_commit_list(filter.no_commit); | |
e5fb0286 | 100 | ref_sorting_release(sorting); |
9f613ddd JH |
101 | return 0; |
102 | } |