Merge branch 'rs/archive-with-attr-pathspec-fix'
[git/gitster.git] / ref-filter.h
blob754038ab078669d82e4fff10514e46803cc265b7
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 0x0080
27 #define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD | \
28 FILTER_REFS_PSEUDOREFS | FILTER_REFS_ROOT_REFS)
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;
51 char **is_base;
53 char refname[FLEX_ARRAY];
56 struct ref_array {
57 int nr, alloc;
58 struct ref_array_item **items;
59 struct rev_info *revs;
61 struct ahead_behind_count *counts;
62 size_t counts_nr;
65 struct ref_filter {
66 const char **name_patterns;
67 struct strvec exclude;
68 struct oid_array points_at;
69 struct commit_list *with_commit;
70 struct commit_list *no_commit;
71 struct commit_list *reachable_from;
72 struct commit_list *unreachable_from;
74 unsigned int with_commit_tag_algo : 1,
75 match_as_path : 1,
76 ignore_case : 1,
77 detached : 1;
78 unsigned int kind,
79 lines;
80 int abbrev,
81 verbose;
83 struct {
84 struct contains_cache contains_cache;
85 struct contains_cache no_contains_cache;
86 } internal;
89 struct ref_format {
91 * Set these to define the format; make sure you call
92 * verify_ref_format() afterwards to finalize.
94 const char *format;
95 const char *rest;
96 int quote_style;
97 int use_color;
99 /* Internal state to ref-filter */
100 int need_color_reset_at_eol;
102 /* List of bases for ahead-behind counts. */
103 struct string_list bases;
105 /* List of bases for is-base indicators. */
106 struct string_list is_base_tips;
108 struct {
109 int max_count;
110 int omit_empty;
111 } array_opts;
114 #define REF_FILTER_INIT { \
115 .points_at = OID_ARRAY_INIT, \
116 .exclude = STRVEC_INIT, \
118 #define REF_FORMAT_INIT { \
119 .use_color = -1, \
120 .bases = STRING_LIST_INIT_DUP, \
121 .is_base_tips = STRING_LIST_INIT_DUP, \
124 /* Macros for checking --merged and --no-merged options */
125 #define _OPT_MERGED_NO_MERGED(option, filter, h) \
126 { OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
127 PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \
128 parse_opt_merge_filter, (intptr_t) "HEAD" \
130 #define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h)
131 #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
133 #define OPT_REF_SORT(var) \
134 OPT_STRING_LIST(0, "sort", (var), \
135 N_("key"), N_("field name to sort on"))
136 #define OPT_REF_FILTER_EXCLUDE(var) \
137 OPT_STRVEC(0, "exclude", &(var)->exclude, \
138 N_("pattern"), N_("exclude refs which match pattern"))
141 * API for filtering a set of refs. Based on the type of refs the user
142 * has requested, we iterate through those refs and apply filters
143 * as per the given ref_filter structure and finally store the
144 * filtered refs in the ref_array structure.
146 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
148 * Filter refs using the given ref_filter and type, sort the contents
149 * according to the given ref_sorting, format the filtered refs with the
150 * given ref_format, and print them to stdout.
152 void filter_and_format_refs(struct ref_filter *filter, unsigned int type,
153 struct ref_sorting *sorting,
154 struct ref_format *format);
155 /* Clear all memory allocated to ref_array */
156 void ref_array_clear(struct ref_array *array);
157 /* Used to verify if the given format is correct and to parse out the used atoms */
158 int verify_ref_format(struct ref_format *format);
159 /* Sort the given ref_array as per the ref_sorting provided */
160 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
161 /* Set REF_SORTING_* sort_flags for all elements of a sorting list */
162 void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on);
163 /* Based on the given format and quote_style, fill the strbuf */
164 int format_ref_array_item(struct ref_array_item *info,
165 struct ref_format *format,
166 struct strbuf *final_buf,
167 struct strbuf *error_buf);
168 /* Release a "struct ref_sorting" */
169 void ref_sorting_release(struct ref_sorting *);
170 /* Convert list of sort options into ref_sorting */
171 struct ref_sorting *ref_sorting_options(struct string_list *);
172 /* Function to parse --merged and --no-merged options */
173 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
174 /* Get the current HEAD's description */
175 char *get_head_description(void);
176 /* Set up translated strings in the output. */
177 void setup_ref_filter_porcelain_msg(void);
180 * Print up to maxcount ref_array elements to stdout using the given
181 * ref_format.
183 void print_formatted_ref_array(struct ref_array *array, struct ref_format *format);
186 * Print a single ref, outside of any ref-filter. Note that the
187 * name must be a fully qualified refname.
189 void pretty_print_ref(const char *name, const struct object_id *oid,
190 struct ref_format *format);
193 * Push a single ref onto the array; this can be used to construct your own
194 * ref_array without using filter_refs().
196 struct ref_array_item *ref_array_push(struct ref_array *array,
197 const char *refname,
198 const struct object_id *oid);
201 * If the provided format includes ahead-behind atoms, then compute the
202 * ahead-behind values for the array of filtered references. Must be
203 * called after filter_refs() but before outputting the formatted refs.
205 * If this is not called, then any ahead-behind atoms will be blank.
207 void filter_ahead_behind(struct repository *r,
208 struct ref_format *format,
209 struct ref_array *array);
212 * If the provided format includes is-base atoms, then compute the base checks
213 * for those tips against all refs.
215 * If this is not called, then any is-base atoms will be blank.
217 void filter_is_base(struct repository *r,
218 struct ref_format *format,
219 struct ref_array *array);
221 void ref_filter_init(struct ref_filter *filter);
222 void ref_filter_clear(struct ref_filter *filter);
224 void ref_format_init(struct ref_format *format);
225 void ref_format_clear(struct ref_format *format);
227 #endif /* REF_FILTER_H */