6 #include "parse-options.h"
7 #include "ref-filter.h"
11 static char const * const for_each_ref_usage
[] = {
12 N_("git for-each-ref [<options>] [<pattern>]"),
13 N_("git for-each-ref [--points-at <object>]"),
14 N_("git for-each-ref [--merged [<commit>]] [--no-merged [<commit>]]"),
15 N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"),
19 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
21 struct ref_sorting
*sorting
;
22 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
24 struct ref_filter filter
= REF_FILTER_INIT
;
25 struct ref_format format
= REF_FORMAT_INIT
;
27 struct strvec vec
= STRVEC_INIT
;
29 struct option opts
[] = {
30 OPT_BIT('s', "shell", &format
.quote_style
,
31 N_("quote placeholders suitably for shells"), QUOTE_SHELL
),
32 OPT_BIT('p', "perl", &format
.quote_style
,
33 N_("quote placeholders suitably for perl"), QUOTE_PERL
),
34 OPT_BIT(0 , "python", &format
.quote_style
,
35 N_("quote placeholders suitably for python"), QUOTE_PYTHON
),
36 OPT_BIT(0 , "tcl", &format
.quote_style
,
37 N_("quote placeholders suitably for Tcl"), QUOTE_TCL
),
38 OPT_BOOL(0, "omit-empty", &format
.array_opts
.omit_empty
,
39 N_("do not output a newline after empty formatted refs")),
42 OPT_INTEGER( 0 , "count", &format
.array_opts
.max_count
, N_("show only <n> matched refs")),
43 OPT_STRING( 0 , "format", &format
.format
, N_("format"), N_("format to use for the output")),
44 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
45 OPT_REF_FILTER_EXCLUDE(&filter
),
46 OPT_REF_SORT(&sorting_options
),
47 OPT_CALLBACK(0, "points-at", &filter
.points_at
,
48 N_("object"), N_("print only refs which points at the given object"),
49 parse_opt_object_name
),
50 OPT_MERGED(&filter
, N_("print only refs that are merged")),
51 OPT_NO_MERGED(&filter
, N_("print only refs that are not merged")),
52 OPT_CONTAINS(&filter
.with_commit
, N_("print only refs which contain the commit")),
53 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only refs which don't contain the commit")),
54 OPT_BOOL(0, "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
55 OPT_BOOL(0, "stdin", &from_stdin
, N_("read reference patterns from stdin")),
59 format
.format
= "%(objectname) %(objecttype)\t%(refname)";
61 git_config(git_default_config
, NULL
);
63 /* Set default (refname) sorting */
64 string_list_append(&sorting_options
, "refname");
66 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
67 if (format
.array_opts
.max_count
< 0) {
68 error("invalid --count argument: `%d'", format
.array_opts
.max_count
);
69 usage_with_options(for_each_ref_usage
, opts
);
71 if (HAS_MULTI_BITS(format
.quote_style
)) {
72 error("more than one quoting style?");
73 usage_with_options(for_each_ref_usage
, opts
);
75 if (verify_ref_format(&format
))
76 usage_with_options(for_each_ref_usage
, opts
);
78 sorting
= ref_sorting_options(&sorting_options
);
79 ref_sorting_set_sort_flags_all(sorting
, REF_SORTING_ICASE
, icase
);
80 filter
.ignore_case
= icase
;
83 struct strbuf line
= STRBUF_INIT
;
86 die(_("unknown arguments supplied with --stdin"));
88 while (strbuf_getline(&line
, stdin
) != EOF
)
89 strvec_push(&vec
, line
.buf
);
91 strbuf_release(&line
);
93 /* vec.v is NULL-terminated, just like 'argv'. */
94 filter
.name_patterns
= vec
.v
;
96 filter
.name_patterns
= argv
;
99 filter
.match_as_path
= 1;
100 filter_and_format_refs(&filter
, FILTER_REFS_ALL
, sorting
, &format
);
102 ref_filter_clear(&filter
);
103 ref_sorting_release(sorting
);