ci: avoid brew for installing perforce
[git/debian.git] / builtin / for-each-ref.c
blob6f62f40d1263f44f8977125d9d54eca76f101685
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "refs.h"
5 #include "object.h"
6 #include "parse-options.h"
7 #include "ref-filter.h"
9 static char const * const for_each_ref_usage[] = {
10 N_("git for-each-ref [<options>] [<pattern>]"),
11 N_("git for-each-ref [--points-at <object>]"),
12 N_("git for-each-ref [--merged [<commit>]] [--no-merged [<commit>]]"),
13 N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"),
14 NULL
17 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
19 int i;
20 struct ref_sorting *sorting;
21 struct string_list sorting_options = STRING_LIST_INIT_DUP;
22 int maxcount = 0, icase = 0;
23 struct ref_array array;
24 struct ref_filter filter;
25 struct ref_format format = REF_FORMAT_INIT;
26 struct strbuf output = STRBUF_INIT;
27 struct strbuf err = STRBUF_INIT;
29 struct option opts[] = {
30 OPT_BIT('s', "shell", &format.quote_style,
31 N_("quote placeholders suitably for shells"), QUOTE_SHELL),
32 OPT_BIT('p', "perl", &format.quote_style,
33 N_("quote placeholders suitably for perl"), QUOTE_PERL),
34 OPT_BIT(0 , "python", &format.quote_style,
35 N_("quote placeholders suitably for python"), QUOTE_PYTHON),
36 OPT_BIT(0 , "tcl", &format.quote_style,
37 N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
39 OPT_GROUP(""),
40 OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
41 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
42 OPT__COLOR(&format.use_color, N_("respect format colors")),
43 OPT_REF_SORT(&sorting_options),
44 OPT_CALLBACK(0, "points-at", &filter.points_at,
45 N_("object"), N_("print only refs which points at the given object"),
46 parse_opt_object_name),
47 OPT_MERGED(&filter, N_("print only refs that are merged")),
48 OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
49 OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
50 OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
51 OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
52 OPT_END(),
55 memset(&array, 0, sizeof(array));
56 memset(&filter, 0, sizeof(filter));
58 format.format = "%(objectname) %(objecttype)\t%(refname)";
60 git_config(git_default_config, NULL);
62 parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
63 if (maxcount < 0) {
64 error("invalid --count argument: `%d'", maxcount);
65 usage_with_options(for_each_ref_usage, opts);
67 if (HAS_MULTI_BITS(format.quote_style)) {
68 error("more than one quoting style?");
69 usage_with_options(for_each_ref_usage, opts);
71 if (verify_ref_format(&format))
72 usage_with_options(for_each_ref_usage, opts);
74 sorting = ref_sorting_options(&sorting_options);
75 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
76 filter.ignore_case = icase;
78 filter.name_patterns = argv;
79 filter.match_as_path = 1;
80 filter_refs(&array, &filter, FILTER_REFS_ALL);
81 ref_array_sort(sorting, &array);
83 if (!maxcount || array.nr < maxcount)
84 maxcount = array.nr;
85 for (i = 0; i < maxcount; i++) {
86 strbuf_reset(&err);
87 strbuf_reset(&output);
88 if (format_ref_array_item(array.items[i], &format, &output, &err))
89 die("%s", err.buf);
90 fwrite(output.buf, 1, output.len, stdout);
91 putchar('\n');
94 strbuf_release(&err);
95 strbuf_release(&output);
96 ref_array_clear(&array);
97 free_commit_list(filter.with_commit);
98 free_commit_list(filter.no_commit);
99 ref_sorting_release(sorting);
100 return 0;