7 #include "parse-options.h"
8 #include "ref-filter.h"
10 #include "commit-reach.h"
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>]]"),
20 int cmd_for_each_ref(int argc
, const char **argv
, const char *prefix
)
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
;
28 struct ref_format format
= REF_FORMAT_INIT
;
29 struct strbuf output
= STRBUF_INIT
;
30 struct strbuf err
= STRBUF_INIT
;
32 struct strvec vec
= STRVEC_INIT
;
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")),
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_SORT(&sorting_options
),
51 OPT_CALLBACK(0, "points-at", &filter
.points_at
,
52 N_("object"), N_("print only refs which points at the given object"),
53 parse_opt_object_name
),
54 OPT_MERGED(&filter
, N_("print only refs that are merged")),
55 OPT_NO_MERGED(&filter
, N_("print only refs that are not merged")),
56 OPT_CONTAINS(&filter
.with_commit
, N_("print only refs which contain the commit")),
57 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only refs which don't contain the commit")),
58 OPT_BOOL(0, "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
59 OPT_BOOL(0, "stdin", &from_stdin
, N_("read reference patterns from stdin")),
63 memset(&array
, 0, sizeof(array
));
64 memset(&filter
, 0, sizeof(filter
));
66 format
.format
= "%(objectname) %(objecttype)\t%(refname)";
68 git_config(git_default_config
, NULL
);
70 parse_options(argc
, argv
, prefix
, opts
, for_each_ref_usage
, 0);
72 error("invalid --count argument: `%d'", maxcount
);
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 filter
.match_as_path
= 1;
104 filter_refs(&array
, &filter
, FILTER_REFS_ALL
);
105 filter_ahead_behind(the_repository
, &format
, &array
);
107 ref_array_sort(sorting
, &array
);
109 if (!maxcount
|| array
.nr
< maxcount
)
111 for (i
= 0; i
< maxcount
; i
++) {
113 strbuf_reset(&output
);
114 if (format_ref_array_item(array
.items
[i
], &format
, &output
, &err
))
116 fwrite(output
.buf
, 1, output
.len
, stdout
);
117 if (output
.len
|| !omit_empty
)
121 strbuf_release(&err
);
122 strbuf_release(&output
);
123 ref_array_clear(&array
);
124 free_commit_list(filter
.with_commit
);
125 free_commit_list(filter
.no_commit
);
126 ref_sorting_release(sorting
);