Raw
1 #ifndef REF_FILTER_H
2 #define REF_FILTER_H
3
4 #include "gettext.h"
5 #include "oid-array.h"
6 #include "commit.h"
7 #include "string-list.h"
8 #include "strvec.h"
9 #include "commit-reach.h"
10
11 /* Quoting styles */
12 #define QUOTE_NONE 0
13 #define QUOTE_SHELL 1
14 #define QUOTE_PERL 2
15 #define QUOTE_PYTHON 4
16 #define QUOTE_TCL 8
17
18 #define FILTER_REFS_TAGS 0x0002
19 #define FILTER_REFS_BRANCHES 0x0004
20 #define FILTER_REFS_REMOTES 0x0008
21 #define FILTER_REFS_OTHERS 0x0010
22 #define FILTER_REFS_REGULAR (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
23 FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
24 #define FILTER_REFS_DETACHED_HEAD 0x0020
25 #define FILTER_REFS_PSEUDOREFS 0x0040
26 #define FILTER_REFS_ROOT_REFS 0x0080
27 #define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD | \
28 FILTER_REFS_PSEUDOREFS | FILTER_REFS_ROOT_REFS)
29
30 struct atom_value;
31 struct ref_sorting;
32 struct ahead_behind_count;
33 struct option;
34
35 enum ref_sorting_order {
36 REF_SORTING_REVERSE = 1<<0,
37 REF_SORTING_ICASE = 1<<1,
38 REF_SORTING_VERSION = 1<<2,
39 REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
40 };
41
42 struct ref_array_item {
43 struct object_id objectname;
44 struct object_id peeled_oid;
45 const char *rest;
46 int flag;
47 unsigned int kind;
48 const char *symref;
49 struct commit *commit;
50 struct atom_value *value;
51 struct ahead_behind_count **counts;
52 char **is_base;
53
54 char refname[FLEX_ARRAY];
55 };
56
57 struct ref_array {
58 int nr, alloc;
59 struct ref_array_item **items;
60 struct rev_info *revs;
61
62 struct ahead_behind_count *counts;
63 size_t counts_nr;
64 };
65
66 struct ref_filter {
67 const char **name_patterns;
68 const char *start_after;
69 struct strvec exclude;
70 struct oid_array points_at;
71 struct commit_list *with_commit;
72 struct commit_list *no_commit;
73 struct commit_list *reachable_from;
74 struct commit_list *unreachable_from;
75
76 unsigned int with_commit_tag_algo : 1,
77 match_as_path : 1,
78 ignore_case : 1,
79 detached : 1;
80 unsigned int kind,
81 lines;
82 int abbrev,
83 verbose;
84
85 struct {
86 struct contains_cache contains_cache;
87 struct contains_cache no_contains_cache;
88 } internal;
89 };
90
91 struct ref_format {
92 /*
93 * Set these to define the format; make sure you call
94 * verify_ref_format() afterwards to finalize.
95 */
96 const char *format;
97 const char *rest;
98 int quote_style;
99 enum git_colorbool use_color;
100
101 /* Internal state to ref-filter */
102 int need_color_reset_at_eol;
103
104 struct {
105 int max_count;
106 int omit_empty;
107 } array_opts;
108 };
109
110 #define REF_FILTER_INIT { \
111 .points_at = OID_ARRAY_INIT, \
112 .exclude = STRVEC_INIT, \
113 }
114 #define REF_FORMAT_INIT { \
115 .use_color = GIT_COLOR_UNKNOWN, \
116 }
117
118 /* Macros for checking --merged and --no-merged options */
119 #define _OPT_MERGED_NO_MERGED(option, filter, h) { \
120 .type = OPTION_CALLBACK, \
121 .long_name = option, \
122 .value = (filter), \
123 .argh = N_("commit"), \
124 .help = (h), \
125 .flags = PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \
126 .callback = parse_opt_merge_filter, \
127 .defval = (intptr_t) "HEAD", \
128 }
129 #define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h)
130 #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
131
132 #define OPT_REF_SORT(var) \
133 OPT_STRING_LIST(0, "sort", (var), \
134 N_("key"), N_("field name to sort on"))
135 #define OPT_REF_FILTER_EXCLUDE(var) \
136 OPT_STRVEC(0, "exclude", &(var)->exclude, \
137 N_("pattern"), N_("exclude refs which match pattern"))
138
139 /* Get the reference kind from the provided reference name. */
140 int ref_kind_from_refname(const char *refname);
141 /*
142 * API for filtering a set of refs. Based on the type of refs the user
143 * has requested, we iterate through those refs and apply filters
144 * as per the given ref_filter structure and finally store the
145 * filtered refs in the ref_array structure.
146 */
147 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
148 /*
149 * Filter refs using the given ref_filter and type, sort the contents
150 * according to the given ref_sorting, format the filtered refs with the
151 * given ref_format, and print them to stdout.
152 */
153 void filter_and_format_refs(struct ref_filter *filter, unsigned int type,
154 struct ref_sorting *sorting,
155 struct ref_format *format);
156 /* Clear all memory allocated to ref_array */
157 void ref_array_clear(struct ref_array *array);
158 /* Used to verify if the given format is correct and to parse out the used atoms */
159 int verify_ref_format(struct ref_format *format);
160 /* Sort the given ref_array as per the ref_sorting provided */
161 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
162 /* Set REF_SORTING_* sort_flags for all elements of a sorting list */
163 void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on);
164 /* Based on the given format and quote_style, fill the strbuf */
165 int format_ref_array_item(struct ref_array_item *info,
166 struct ref_format *format,
167 struct strbuf *final_buf,
168 struct strbuf *error_buf);
169 /* Release a "struct ref_sorting" */
170 void ref_sorting_release(struct ref_sorting *);
171 /* Convert list of sort options into ref_sorting */
172 struct ref_sorting *ref_sorting_options(struct string_list *);
173 /* Function to parse --merged and --no-merged options */
174 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
175 /* Get the current HEAD's description */
176 char *get_head_description(void);
177 /* Set up translated strings in the output. */
178 void setup_ref_filter_porcelain_msg(void);
179
180 /*
181 * Print up to maxcount ref_array elements to stdout using the given
182 * ref_format.
183 */
184 void print_formatted_ref_array(struct ref_array *array, struct ref_format *format);
185
186 /*
187 * Print a single ref, outside of any ref-filter. Note that the
188 * name must be a fully qualified refname.
189 */
190 void pretty_print_ref(const char *name, const struct object_id *oid,
191 const struct object_id *peeled_oid,
192 struct ref_format *format);
193
194 /*
195 * Push a single ref onto the array; this can be used to construct your own
196 * ref_array without using filter_refs().
197 */
198 struct ref_array_item *ref_array_push(struct ref_array *array,
199 const char *refname,
200 const struct object_id *oid,
201 const struct object_id *peeled_oid);
202
203 /*
204 * If the provided format includes ahead-behind atoms, then compute the
205 * ahead-behind values for the array of filtered references. Must be
206 * called after filter_refs() but before outputting the formatted refs.
207 *
208 * If this is not called, then any ahead-behind atoms will be blank.
209 */
210 void filter_ahead_behind(struct repository *r,
211 struct ref_array *array);
212
213 /*
214 * If the provided format includes is-base atoms, then compute the base checks
215 * for those tips against all refs.
216 *
217 * If this is not called, then any is-base atoms will be blank.
218 */
219 void filter_is_base(struct repository *r,
220 struct ref_array *array);
221
222 void ref_filter_init(struct ref_filter *filter);
223 void ref_filter_clear(struct ref_filter *filter);
224
225 #endif /* REF_FILTER_H */