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