t0410: make test description clearer
[git/gitster.git] / builtin / for-each-ref.c
blob715745a262aa351d94cabc084bda64d7e05f8848
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "commit.h"
4 #include "config.h"
5 #include "gettext.h"
6 #include "object.h"
7 #include "parse-options.h"
8 #include "ref-filter.h"
9 #include "strbuf.h"
10 #include "strvec.h"
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
20 int cmd_for_each_ref(int argc,
21 const char **argv,
22 const char *prefix,
23 struct repository *repo UNUSED)
25 struct ref_sorting *sorting;
26 struct string_list sorting_options = STRING_LIST_INIT_DUP;
27 int icase = 0, include_root_refs = 0, from_stdin = 0;
28 struct ref_filter filter = REF_FILTER_INIT;
29 struct ref_format format = REF_FORMAT_INIT;
30 unsigned int flags = FILTER_REFS_REGULAR;
31 struct strvec vec = STRVEC_INIT;
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", &format.array_opts.omit_empty,
43 N_("do not output a newline after empty formatted refs")),
45 OPT_GROUP(""),
46 OPT_INTEGER( 0 , "count", &format.array_opts.max_count, 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_BOOL(0, "include-root-refs", &include_root_refs, N_("also include HEAD ref and pseudorefs")),
61 OPT_END(),
64 format.format = "%(objectname) %(objecttype)\t%(refname)";
66 git_config(git_default_config, NULL);
68 /* Set default (refname) sorting */
69 string_list_append(&sorting_options, "refname");
71 parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
72 if (format.array_opts.max_count < 0) {
73 error("invalid --count argument: `%d'", format.array_opts.max_count);
74 usage_with_options(for_each_ref_usage, opts);
76 if (HAS_MULTI_BITS(format.quote_style)) {
77 error("more than one quoting style?");
78 usage_with_options(for_each_ref_usage, opts);
80 if (verify_ref_format(&format))
81 usage_with_options(for_each_ref_usage, opts);
83 sorting = ref_sorting_options(&sorting_options);
84 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
85 filter.ignore_case = icase;
87 if (from_stdin) {
88 struct strbuf line = STRBUF_INIT;
90 if (argv[0])
91 die(_("unknown arguments supplied with --stdin"));
93 while (strbuf_getline(&line, stdin) != EOF)
94 strvec_push(&vec, line.buf);
96 strbuf_release(&line);
98 /* vec.v is NULL-terminated, just like 'argv'. */
99 filter.name_patterns = vec.v;
100 } else {
101 filter.name_patterns = argv;
104 if (include_root_refs)
105 flags |= FILTER_REFS_ROOT_REFS | FILTER_REFS_DETACHED_HEAD;
107 filter.match_as_path = 1;
108 filter_and_format_refs(&filter, flags, sorting, &format);
110 ref_filter_clear(&filter);
111 ref_format_clear(&format);
112 ref_sorting_release(sorting);
113 strvec_clear(&vec);
114 return 0;