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