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