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