]>
Commit | Line | Data |
---|---|---|
baffc0e7 | 1 | #include "builtin.h" |
b2141fc1 | 2 | #include "config.h" |
f394e093 | 3 | #include "gettext.h" |
9f613ddd JH |
4 | #include "refs.h" |
5 | #include "object.h" | |
c3428da8 | 6 | #include "parse-options.h" |
69b1cf91 | 7 | #include "ref-filter.h" |
88e4e183 | 8 | #include "strbuf.h" |
b73dec55 | 9 | #include "strvec.h" |
49abcd21 | 10 | #include "commit-reach.h" |
9f613ddd | 11 | |
c3428da8 | 12 | static char const * const for_each_ref_usage[] = { |
9c9b4f2f | 13 | N_("git for-each-ref [<options>] [<pattern>]"), |
d325406e | 14 | N_("git for-each-ref [--points-at <object>]"), |
21bf9339 | 15 | N_("git for-each-ref [--merged [<commit>]] [--no-merged [<commit>]]"), |
ac3f5a34 | 16 | N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"), |
c3428da8 PH |
17 | NULL |
18 | }; | |
19 | ||
20 | int cmd_for_each_ref(int argc, const char **argv, const char *prefix) | |
9f613ddd | 21 | { |
215b5651 | 22 | int i; |
98e7ab6d JH |
23 | struct ref_sorting *sorting; |
24 | struct string_list sorting_options = STRING_LIST_INIT_DUP; | |
aabfdc95 | 25 | int maxcount = 0, icase = 0, omit_empty = 0; |
14de7fba | 26 | struct ref_array array; |
b9f7daa6 | 27 | struct ref_filter filter = REF_FILTER_INIT; |
4a68e36d | 28 | struct ref_format format = REF_FORMAT_INIT; |
844c3f0b ZH |
29 | struct strbuf output = STRBUF_INIT; |
30 | struct strbuf err = STRBUF_INIT; | |
b73dec55 DS |
31 | int from_stdin = 0; |
32 | struct strvec vec = STRVEC_INIT; | |
9f613ddd | 33 | |
c3428da8 | 34 | struct option opts[] = { |
4a68e36d | 35 | OPT_BIT('s', "shell", &format.quote_style, |
18913521 | 36 | N_("quote placeholders suitably for shells"), QUOTE_SHELL), |
4a68e36d | 37 | OPT_BIT('p', "perl", &format.quote_style, |
18913521 | 38 | N_("quote placeholders suitably for perl"), QUOTE_PERL), |
4a68e36d | 39 | OPT_BIT(0 , "python", &format.quote_style, |
18913521 | 40 | N_("quote placeholders suitably for python"), QUOTE_PYTHON), |
4a68e36d | 41 | OPT_BIT(0 , "tcl", &format.quote_style, |
b799a696 | 42 | N_("quote placeholders suitably for Tcl"), QUOTE_TCL), |
aabfdc95 ØW |
43 | OPT_BOOL(0, "omit-empty", &omit_empty, |
44 | N_("do not output a newline after empty formatted refs")), | |
c3428da8 PH |
45 | |
46 | OPT_GROUP(""), | |
18913521 | 47 | OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")), |
4a68e36d | 48 | OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), |
0c88bf50 | 49 | OPT__COLOR(&format.use_color, N_("respect format colors")), |
8255dd8a | 50 | OPT_REF_FILTER_EXCLUDE(&filter), |
98e7ab6d | 51 | OPT_REF_SORT(&sorting_options), |
d325406e KN |
52 | OPT_CALLBACK(0, "points-at", &filter.points_at, |
53 | N_("object"), N_("print only refs which points at the given object"), | |
54 | parse_opt_object_name), | |
7c328348 KN |
55 | OPT_MERGED(&filter, N_("print only refs that are merged")), |
56 | OPT_NO_MERGED(&filter, N_("print only refs that are not merged")), | |
4a71109a | 57 | OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")), |
ac3f5a34 | 58 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")), |
3bb16a8b | 59 | OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
b73dec55 | 60 | OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")), |
c3428da8 PH |
61 | OPT_END(), |
62 | }; | |
63 | ||
d325406e | 64 | memset(&array, 0, sizeof(array)); |
d325406e | 65 | |
4a68e36d JK |
66 | format.format = "%(objectname) %(objecttype)\t%(refname)"; |
67 | ||
d8b68686 JK |
68 | git_config(git_default_config, NULL); |
69 | ||
37782920 | 70 | parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0); |
c3428da8 PH |
71 | if (maxcount < 0) { |
72 | error("invalid --count argument: `%d'", maxcount); | |
73 | usage_with_options(for_each_ref_usage, opts); | |
9f613ddd | 74 | } |
4a68e36d | 75 | if (HAS_MULTI_BITS(format.quote_style)) { |
c9ecf4f1 | 76 | error("more than one quoting style?"); |
c3428da8 PH |
77 | usage_with_options(for_each_ref_usage, opts); |
78 | } | |
4a68e36d | 79 | if (verify_ref_format(&format)) |
c3428da8 | 80 | usage_with_options(for_each_ref_usage, opts); |
9f613ddd | 81 | |
98e7ab6d | 82 | sorting = ref_sorting_options(&sorting_options); |
7c269a7b | 83 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); |
3bb16a8b | 84 | filter.ignore_case = icase; |
9f613ddd | 85 | |
b73dec55 DS |
86 | if (from_stdin) { |
87 | struct strbuf line = STRBUF_INIT; | |
88 | ||
89 | if (argv[0]) | |
90 | die(_("unknown arguments supplied with --stdin")); | |
91 | ||
92 | while (strbuf_getline(&line, stdin) != EOF) | |
93 | strvec_push(&vec, line.buf); | |
94 | ||
95 | strbuf_release(&line); | |
96 | ||
97 | /* vec.v is NULL-terminated, just like 'argv'. */ | |
98 | filter.name_patterns = vec.v; | |
99 | } else { | |
100 | filter.name_patterns = argv; | |
101 | } | |
102 | ||
bef0e12b | 103 | filter.match_as_path = 1; |
1763334c | 104 | filter_refs(&array, &filter, FILTER_REFS_ALL); |
49abcd21 DS |
105 | filter_ahead_behind(the_repository, &format, &array); |
106 | ||
14de7fba | 107 | ref_array_sort(sorting, &array); |
9f613ddd | 108 | |
14de7fba KN |
109 | if (!maxcount || array.nr < maxcount) |
110 | maxcount = array.nr; | |
22f69a85 | 111 | for (i = 0; i < maxcount; i++) { |
844c3f0b ZH |
112 | strbuf_reset(&err); |
113 | strbuf_reset(&output); | |
22f69a85 ZH |
114 | if (format_ref_array_item(array.items[i], &format, &output, &err)) |
115 | die("%s", err.buf); | |
116 | fwrite(output.buf, 1, output.len, stdout); | |
aabfdc95 ØW |
117 | if (output.len || !omit_empty) |
118 | putchar('\n'); | |
22f69a85 | 119 | } |
844c3f0b ZH |
120 | |
121 | strbuf_release(&err); | |
122 | strbuf_release(&output); | |
14de7fba | 123 | ref_array_clear(&array); |
b571fb98 | 124 | ref_filter_clear(&filter); |
e5fb0286 | 125 | ref_sorting_release(sorting); |
b73dec55 | 126 | strvec_clear(&vec); |
9f613ddd JH |
127 | return 0; |
128 | } |