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
,
22 struct repository
*repo
)
24 struct ref_sorting
*sorting
;
25 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
26 int icase
= 0, include_root_refs
= 0, from_stdin
= 0;
27 struct ref_filter filter
= REF_FILTER_INIT
;
28 struct ref_format format
= REF_FORMAT_INIT
;
29 unsigned int flags
= FILTER_REFS_REGULAR
;
30 struct strvec vec
= STRVEC_INIT
;
32 struct option opts
[] = {
33 OPT_BIT('s', "shell", &format
.quote_style
,
34 N_("quote placeholders suitably for shells"), QUOTE_SHELL
),
35 OPT_BIT('p', "perl", &format
.quote_style
,
36 N_("quote placeholders suitably for perl"), QUOTE_PERL
),
37 OPT_BIT(0 , "python", &format
.quote_style
,
38 N_("quote placeholders suitably for python"), QUOTE_PYTHON
),
39 OPT_BIT(0 , "tcl", &format
.quote_style
,
40 N_("quote placeholders suitably for Tcl"), QUOTE_TCL
),
41 OPT_BOOL(0, "omit-empty", &format
.array_opts
.omit_empty
,
42 N_("do not output a newline after empty formatted refs")),
45 OPT_INTEGER( 0 , "count", &format
.array_opts
.max_count
, N_("show only <n> matched refs")),
46 OPT_STRING( 0 , "format", &format
.format
, N_("format"), N_("format to use for the output")),
47 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
48 OPT_REF_FILTER_EXCLUDE(&filter
),
49 OPT_REF_SORT(&sorting_options
),
50 OPT_CALLBACK(0, "points-at", &filter
.points_at
,
51 N_("object"), N_("print only refs which points at the given object"),
52 parse_opt_object_name
),
53 OPT_MERGED(&filter
, N_("print only refs that are merged")),
54 OPT_NO_MERGED(&filter
, N_("print only refs that are not merged")),
55 OPT_CONTAINS(&filter
.with_commit
, N_("print only refs which contain the commit")),
56 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only refs which don't contain the commit")),
57 OPT_BOOL(0, "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
58 OPT_BOOL(0, "stdin", &from_stdin
, N_("read reference patterns from stdin")),
59 OPT_BOOL(0, "include-root-refs", &include_root_refs
, N_("also include HEAD ref and pseudorefs")),
63 format
.format
= "%(objectname) %(objecttype)\t%(refname)";
65 repo_config(repo
, git_default_config
, NULL
);
67 /* Set default (refname) sorting */
68 string_list_append(&sorting_options
, "refname");
70 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
71 if (format
.array_opts
.max_count
< 0) {
72 error("invalid --count argument: `%d'", format
.array_opts
.max_count
);
73 usage_with_options(for_each_ref_usage
, opts
);
75 if (HAS_MULTI_BITS(format
.quote_style
)) {
76 error("more than one quoting style?");
77 usage_with_options(for_each_ref_usage
, opts
);
79 if (verify_ref_format(&format
))
80 usage_with_options(for_each_ref_usage
, opts
);
82 sorting
= ref_sorting_options(&sorting_options
);
83 ref_sorting_set_sort_flags_all(sorting
, REF_SORTING_ICASE
, icase
);
84 filter
.ignore_case
= icase
;
87 struct strbuf line
= STRBUF_INIT
;
90 die(_("unknown arguments supplied with --stdin"));
92 while (strbuf_getline(&line
, stdin
) != EOF
)
93 strvec_push(&vec
, line
.buf
);
95 strbuf_release(&line
);
97 /* vec.v is NULL-terminated, just like 'argv'. */
98 filter
.name_patterns
= vec
.v
;
100 filter
.name_patterns
= argv
;
103 if (include_root_refs
)
104 flags
|= FILTER_REFS_ROOT_REFS
| FILTER_REFS_DETACHED_HEAD
;
106 filter
.match_as_path
= 1;
107 filter_and_format_refs(&filter
, flags
, sorting
, &format
);
109 ref_filter_clear(&filter
);
110 ref_sorting_release(sorting
);