]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/for-each-ref.c
Merge branch 'dl/test-must-fail-fixes-5'
[thirdparty/git.git] / builtin / for-each-ref.c
CommitLineData
baffc0e7 1#include "builtin.h"
9f613ddd 2#include "cache.h"
b2141fc1 3#include "config.h"
9f613ddd
JH
4#include "refs.h"
5#include "object.h"
c3428da8 6#include "parse-options.h"
69b1cf91 7#include "ref-filter.h"
9f613ddd 8
c3428da8 9static char const * const for_each_ref_usage[] = {
9c9b4f2f 10 N_("git for-each-ref [<options>] [<pattern>]"),
d325406e 11 N_("git for-each-ref [--points-at <object>]"),
7ac04f13 12 N_("git for-each-ref [(--merged | --no-merged) [<commit>]]"),
ac3f5a34 13 N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"),
c3428da8
PH
14 NULL
15};
16
17int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
9f613ddd 18{
215b5651 19 int i;
73079d21 20 struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
4a68e36d 21 int maxcount = 0, icase = 0;
14de7fba
KN
22 struct ref_array array;
23 struct ref_filter filter;
4a68e36d 24 struct ref_format format = REF_FORMAT_INIT;
9f613ddd 25
c3428da8 26 struct option opts[] = {
4a68e36d 27 OPT_BIT('s', "shell", &format.quote_style,
18913521 28 N_("quote placeholders suitably for shells"), QUOTE_SHELL),
4a68e36d 29 OPT_BIT('p', "perl", &format.quote_style,
18913521 30 N_("quote placeholders suitably for perl"), QUOTE_PERL),
4a68e36d 31 OPT_BIT(0 , "python", &format.quote_style,
18913521 32 N_("quote placeholders suitably for python"), QUOTE_PYTHON),
4a68e36d 33 OPT_BIT(0 , "tcl", &format.quote_style,
b799a696 34 N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
c3428da8
PH
35
36 OPT_GROUP(""),
18913521 37 OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
4a68e36d 38 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
0c88bf50 39 OPT__COLOR(&format.use_color, N_("respect format colors")),
95be717c 40 OPT_REF_SORT(sorting_tail),
d325406e
KN
41 OPT_CALLBACK(0, "points-at", &filter.points_at,
42 N_("object"), N_("print only refs which points at the given object"),
43 parse_opt_object_name),
7c328348
KN
44 OPT_MERGED(&filter, N_("print only refs that are merged")),
45 OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
4a71109a 46 OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
ac3f5a34 47 OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
3bb16a8b 48 OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
c3428da8
PH
49 OPT_END(),
50 };
51
d325406e
KN
52 memset(&array, 0, sizeof(array));
53 memset(&filter, 0, sizeof(filter));
54
4a68e36d
JK
55 format.format = "%(objectname) %(objecttype)\t%(refname)";
56
d8b68686
JK
57 git_config(git_default_config, NULL);
58
37782920 59 parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
c3428da8
PH
60 if (maxcount < 0) {
61 error("invalid --count argument: `%d'", maxcount);
62 usage_with_options(for_each_ref_usage, opts);
9f613ddd 63 }
4a68e36d 64 if (HAS_MULTI_BITS(format.quote_style)) {
c9ecf4f1 65 error("more than one quoting style?");
c3428da8
PH
66 usage_with_options(for_each_ref_usage, opts);
67 }
4a68e36d 68 if (verify_ref_format(&format))
c3428da8 69 usage_with_options(for_each_ref_usage, opts);
9f613ddd 70
73079d21
KN
71 if (!sorting)
72 sorting = ref_default_sorting();
76f9e569 73 ref_sorting_icase_all(sorting, icase);
3bb16a8b 74 filter.ignore_case = icase;
9f613ddd 75
14de7fba 76 filter.name_patterns = argv;
bef0e12b 77 filter.match_as_path = 1;
14de7fba
KN
78 filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
79 ref_array_sort(sorting, &array);
9f613ddd 80
14de7fba
KN
81 if (!maxcount || array.nr < maxcount)
82 maxcount = array.nr;
9f613ddd 83 for (i = 0; i < maxcount; i++)
4a68e36d 84 show_ref_array_item(array.items[i], &format);
14de7fba 85 ref_array_clear(&array);
9f613ddd
JH
86 return 0;
87}