for-each-ref.c 3.0 KB
Newer Older
1
#include "builtin.h"
2
#include "cache.h"
3
#include "config.h"
4 5
#include "refs.h"
#include "object.h"
6
#include "parse-options.h"
K
Karthik Nayak 已提交
7
#include "ref-filter.h"
8

9
static char const * const for_each_ref_usage[] = {
10
	N_("git for-each-ref [<options>] [<pattern>]"),
11
	N_("git for-each-ref [--points-at <object>]"),
12
	N_("git for-each-ref [(--merged | --no-merged) [<commit>]]"),
13
	N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"),
14 15 16 17
	NULL
};

int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
18
{
19
	int i;
20
	struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
21
	int maxcount = 0, icase = 0;
22 23
	struct ref_array array;
	struct ref_filter filter;
24
	struct ref_format format = REF_FORMAT_INIT;
25

26
	struct option opts[] = {
27
		OPT_BIT('s', "shell", &format.quote_style,
28
			N_("quote placeholders suitably for shells"), QUOTE_SHELL),
29
		OPT_BIT('p', "perl",  &format.quote_style,
30
			N_("quote placeholders suitably for perl"), QUOTE_PERL),
31
		OPT_BIT(0 , "python", &format.quote_style,
32
			N_("quote placeholders suitably for python"), QUOTE_PYTHON),
33
		OPT_BIT(0 , "tcl",  &format.quote_style,
34
			N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
35 36

		OPT_GROUP(""),
37
		OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
38
		OPT_STRING(  0 , "format", &format.format, N_("format"), N_("format to use for the output")),
39
		OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
40
			    N_("field name to sort on"), &parse_opt_ref_sorting),
41 42 43
		OPT_CALLBACK(0, "points-at", &filter.points_at,
			     N_("object"), N_("print only refs which points at the given object"),
			     parse_opt_object_name),
44 45
		OPT_MERGED(&filter, N_("print only refs that are merged")),
		OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
46
		OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
47
		OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
48
		OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
49 50 51
		OPT_END(),
	};

52 53 54
	memset(&array, 0, sizeof(array));
	memset(&filter, 0, sizeof(filter));

55 56
	format.format = "%(objectname) %(objecttype)\t%(refname)";

J
Jeff King 已提交
57 58
	git_config(git_default_config, NULL);

59
	parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
60 61 62
	if (maxcount < 0) {
		error("invalid --count argument: `%d'", maxcount);
		usage_with_options(for_each_ref_usage, opts);
63
	}
64
	if (HAS_MULTI_BITS(format.quote_style)) {
65
		error("more than one quoting style?");
66 67
		usage_with_options(for_each_ref_usage, opts);
	}
68
	if (verify_ref_format(&format))
69
		usage_with_options(for_each_ref_usage, opts);
70

71 72
	if (!sorting)
		sorting = ref_default_sorting();
73 74
	sorting->ignore_case = icase;
	filter.ignore_case = icase;
75

76
	filter.name_patterns = argv;
77
	filter.match_as_path = 1;
78 79
	filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
	ref_array_sort(sorting, &array);
80

81 82
	if (!maxcount || array.nr < maxcount)
		maxcount = array.nr;
83
	for (i = 0; i < maxcount; i++)
84
		show_ref_array_item(array.items[i], &format);
85
	ref_array_clear(&array);
86 87
	return 0;
}