]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/for-each-ref.c
Merge branch 'jc/bisect-doc' into maint-2.43
[thirdparty/git.git] / builtin / for-each-ref.c
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 {
21 int i;
22 struct ref_sorting *sorting;
23 struct string_list sorting_options = STRING_LIST_INIT_DUP;
24 int maxcount = 0, icase = 0, omit_empty = 0;
25 struct ref_array array;
26 struct ref_filter filter = REF_FILTER_INIT;
27 struct ref_format format = REF_FORMAT_INIT;
28 struct strbuf output = STRBUF_INIT;
29 struct strbuf err = STRBUF_INIT;
30 int from_stdin = 0;
31 struct strvec vec = STRVEC_INIT;
32
33 struct option opts[] = {
34 OPT_BIT('s', "shell", &format.quote_style,
35 N_("quote placeholders suitably for shells"), QUOTE_SHELL),
36 OPT_BIT('p', "perl", &format.quote_style,
37 N_("quote placeholders suitably for perl"), QUOTE_PERL),
38 OPT_BIT(0 , "python", &format.quote_style,
39 N_("quote placeholders suitably for python"), QUOTE_PYTHON),
40 OPT_BIT(0 , "tcl", &format.quote_style,
41 N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
42 OPT_BOOL(0, "omit-empty", &omit_empty,
43 N_("do not output a newline after empty formatted refs")),
44
45 OPT_GROUP(""),
46 OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
47 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
48 OPT__COLOR(&format.use_color, N_("respect format colors")),
49 OPT_REF_FILTER_EXCLUDE(&filter),
50 OPT_REF_SORT(&sorting_options),
51 OPT_CALLBACK(0, "points-at", &filter.points_at,
52 N_("object"), N_("print only refs which points at the given object"),
53 parse_opt_object_name),
54 OPT_MERGED(&filter, N_("print only refs that are merged")),
55 OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
56 OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
57 OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
58 OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
59 OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")),
60 OPT_END(),
61 };
62
63 memset(&array, 0, sizeof(array));
64
65 format.format = "%(objectname) %(objecttype)\t%(refname)";
66
67 git_config(git_default_config, NULL);
68
69 parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
70 if (maxcount < 0) {
71 error("invalid --count argument: `%d'", maxcount);
72 usage_with_options(for_each_ref_usage, opts);
73 }
74 if (HAS_MULTI_BITS(format.quote_style)) {
75 error("more than one quoting style?");
76 usage_with_options(for_each_ref_usage, opts);
77 }
78 if (verify_ref_format(&format))
79 usage_with_options(for_each_ref_usage, opts);
80
81 sorting = ref_sorting_options(&sorting_options);
82 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
83 filter.ignore_case = icase;
84
85 if (from_stdin) {
86 struct strbuf line = STRBUF_INIT;
87
88 if (argv[0])
89 die(_("unknown arguments supplied with --stdin"));
90
91 while (strbuf_getline(&line, stdin) != EOF)
92 strvec_push(&vec, line.buf);
93
94 strbuf_release(&line);
95
96 /* vec.v is NULL-terminated, just like 'argv'. */
97 filter.name_patterns = vec.v;
98 } else {
99 filter.name_patterns = argv;
100 }
101
102 filter.match_as_path = 1;
103 filter_refs(&array, &filter, FILTER_REFS_ALL);
104 filter_ahead_behind(the_repository, &format, &array);
105
106 ref_array_sort(sorting, &array);
107
108 if (!maxcount || array.nr < maxcount)
109 maxcount = array.nr;
110 for (i = 0; i < maxcount; i++) {
111 strbuf_reset(&err);
112 strbuf_reset(&output);
113 if (format_ref_array_item(array.items[i], &format, &output, &err))
114 die("%s", err.buf);
115 fwrite(output.buf, 1, output.len, stdout);
116 if (output.len || !omit_empty)
117 putchar('\n');
118 }
119
120 strbuf_release(&err);
121 strbuf_release(&output);
122 ref_array_clear(&array);
123 ref_filter_clear(&filter);
124 ref_sorting_release(sorting);
125 strvec_clear(&vec);
126 return 0;
127 }