The second batch
[git.git] / builtin / for-each-ref.c
blob919282e12a335a5a7491b905f27dcfface0120a3
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"
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
19 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
21 struct ref_sorting *sorting;
22 struct string_list sorting_options = STRING_LIST_INIT_DUP;
23 int icase = 0, include_root_refs = 0, from_stdin = 0;
24 struct ref_filter filter = REF_FILTER_INIT;
25 struct ref_format format = REF_FORMAT_INIT;
26 unsigned int flags = FILTER_REFS_REGULAR;
27 struct strvec vec = STRVEC_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),
38 OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty,
39 N_("do not output a newline after empty formatted refs")),
41 OPT_GROUP(""),
42 OPT_INTEGER( 0 , "count", &format.array_opts.max_count, N_("show only <n> matched refs")),
43 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
44 OPT__COLOR(&format.use_color, N_("respect format colors")),
45 OPT_REF_FILTER_EXCLUDE(&filter),
46 OPT_REF_SORT(&sorting_options),
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),
50 OPT_MERGED(&filter, N_("print only refs that are merged")),
51 OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
52 OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
53 OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
54 OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
55 OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")),
56 OPT_BOOL(0, "include-root-refs", &include_root_refs, N_("also include HEAD ref and pseudorefs")),
57 OPT_END(),
60 format.format = "%(objectname) %(objecttype)\t%(refname)";
62 git_config(git_default_config, NULL);
64 /* Set default (refname) sorting */
65 string_list_append(&sorting_options, "refname");
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);
72 if (HAS_MULTI_BITS(format.quote_style)) {
73 error("more than one quoting style?");
74 usage_with_options(for_each_ref_usage, opts);
76 if (verify_ref_format(&format))
77 usage_with_options(for_each_ref_usage, opts);
79 sorting = ref_sorting_options(&sorting_options);
80 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
81 filter.ignore_case = icase;
83 if (from_stdin) {
84 struct strbuf line = STRBUF_INIT;
86 if (argv[0])
87 die(_("unknown arguments supplied with --stdin"));
89 while (strbuf_getline(&line, stdin) != EOF)
90 strvec_push(&vec, line.buf);
92 strbuf_release(&line);
94 /* vec.v is NULL-terminated, just like 'argv'. */
95 filter.name_patterns = vec.v;
96 } else {
97 filter.name_patterns = argv;
100 if (include_root_refs)
101 flags |= FILTER_REFS_ROOT_REFS;
103 filter.match_as_path = 1;
104 filter_and_format_refs(&filter, flags, sorting, &format);
106 ref_filter_clear(&filter);
107 ref_sorting_release(sorting);
108 strvec_clear(&vec);
109 return 0;