Merge branch 'dk/gc-idx-wo-pack' into maint
[git] / builtin / for-each-ref.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "object.h"
5 #include "parse-options.h"
6 #include "ref-filter.h"
7
8 static char const * const for_each_ref_usage[] = {
9         N_("git for-each-ref [<options>] [<pattern>]"),
10         NULL
11 };
12
13 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
14 {
15         int i;
16         const char *format = "%(objectname) %(objecttype)\t%(refname)";
17         struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
18         int maxcount = 0, quote_style = 0;
19         struct ref_array array;
20         struct ref_filter filter;
21
22         struct option opts[] = {
23                 OPT_BIT('s', "shell", &quote_style,
24                         N_("quote placeholders suitably for shells"), QUOTE_SHELL),
25                 OPT_BIT('p', "perl",  &quote_style,
26                         N_("quote placeholders suitably for perl"), QUOTE_PERL),
27                 OPT_BIT(0 , "python", &quote_style,
28                         N_("quote placeholders suitably for python"), QUOTE_PYTHON),
29                 OPT_BIT(0 , "tcl",  &quote_style,
30                         N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
31
32                 OPT_GROUP(""),
33                 OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
34                 OPT_STRING(  0 , "format", &format, N_("format"), N_("format to use for the output")),
35                 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
36                             N_("field name to sort on"), &parse_opt_ref_sorting),
37                 OPT_END(),
38         };
39
40         parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
41         if (maxcount < 0) {
42                 error("invalid --count argument: `%d'", maxcount);
43                 usage_with_options(for_each_ref_usage, opts);
44         }
45         if (HAS_MULTI_BITS(quote_style)) {
46                 error("more than one quoting style?");
47                 usage_with_options(for_each_ref_usage, opts);
48         }
49         if (verify_ref_format(format))
50                 usage_with_options(for_each_ref_usage, opts);
51
52         if (!sorting)
53                 sorting = ref_default_sorting();
54
55         /* for warn_ambiguous_refs */
56         git_config(git_default_config, NULL);
57
58         memset(&array, 0, sizeof(array));
59         memset(&filter, 0, sizeof(filter));
60         filter.name_patterns = argv;
61         filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
62         ref_array_sort(sorting, &array);
63
64         if (!maxcount || array.nr < maxcount)
65                 maxcount = array.nr;
66         for (i = 0; i < maxcount; i++)
67                 show_ref_array_item(array.items[i], format, quote_style);
68         ref_array_clear(&array);
69         return 0;
70 }