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