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