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