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