Merge branch 'ah/sequencer-rewrite-todo-fix'
[git.git] / ref-filter.h
blob1524bc463a5701d475cb68e50caa884a6daf890a
1 #ifndef REF_FILTER_H
2 #define REF_FILTER_H
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"
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_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)
27 struct atom_value;
28 struct ref_sorting;
29 struct ahead_behind_count;
30 struct option;
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,
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;
49 char refname[FLEX_ARRAY];
52 struct ref_array {
53 int nr, alloc;
54 struct ref_array_item **items;
55 struct rev_info *revs;
57 struct ahead_behind_count *counts;
58 size_t counts_nr;
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;
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;
80 struct ref_format {
82 * Set these to define the format; make sure you call
83 * verify_ref_format() afterwards to finalize.
85 const char *format;
86 const char *rest;
87 int quote_style;
88 int use_color;
90 /* Internal state to ref-filter */
91 int need_color_reset_at_eol;
93 /* List of bases for ahead-behind counts. */
94 struct string_list bases;
97 #define REF_FILTER_INIT { \
98 .points_at = OID_ARRAY_INIT, \
99 .exclude = STRVEC_INIT, \
101 #define REF_FORMAT_INIT { \
102 .use_color = -1, \
103 .bases = STRING_LIST_INIT_DUP, \
106 /* Macros for checking --merged and --no-merged options */
107 #define _OPT_MERGED_NO_MERGED(option, filter, h) \
108 { OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
109 PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \
110 parse_opt_merge_filter, (intptr_t) "HEAD" \
112 #define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h)
113 #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
115 #define OPT_REF_SORT(var) \
116 OPT_STRING_LIST(0, "sort", (var), \
117 N_("key"), N_("field name to sort on"))
118 #define OPT_REF_FILTER_EXCLUDE(var) \
119 OPT_STRVEC(0, "exclude", &(var)->exclude, \
120 N_("pattern"), N_("exclude refs which match pattern"))
123 * API for filtering a set of refs. Based on the type of refs the user
124 * has requested, we iterate through those refs and apply filters
125 * as per the given ref_filter structure and finally store the
126 * filtered refs in the ref_array structure.
128 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
129 /* Clear all memory allocated to ref_array */
130 void ref_array_clear(struct ref_array *array);
131 /* Used to verify if the given format is correct and to parse out the used atoms */
132 int verify_ref_format(struct ref_format *format);
133 /* Sort the given ref_array as per the ref_sorting provided */
134 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
135 /* Set REF_SORTING_* sort_flags for all elements of a sorting list */
136 void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on);
137 /* Based on the given format and quote_style, fill the strbuf */
138 int format_ref_array_item(struct ref_array_item *info,
139 struct ref_format *format,
140 struct strbuf *final_buf,
141 struct strbuf *error_buf);
142 /* Release a "struct ref_sorting" */
143 void ref_sorting_release(struct ref_sorting *);
144 /* Convert list of sort options into ref_sorting */
145 struct ref_sorting *ref_sorting_options(struct string_list *);
146 /* Function to parse --merged and --no-merged options */
147 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
148 /* Get the current HEAD's description */
149 char *get_head_description(void);
150 /* Set up translated strings in the output. */
151 void setup_ref_filter_porcelain_msg(void);
154 * Print a single ref, outside of any ref-filter. Note that the
155 * name must be a fully qualified refname.
157 void pretty_print_ref(const char *name, const struct object_id *oid,
158 struct ref_format *format);
161 * Push a single ref onto the array; this can be used to construct your own
162 * ref_array without using filter_refs().
164 struct ref_array_item *ref_array_push(struct ref_array *array,
165 const char *refname,
166 const struct object_id *oid);
169 * If the provided format includes ahead-behind atoms, then compute the
170 * ahead-behind values for the array of filtered references. Must be
171 * called after filter_refs() but before outputting the formatted refs.
173 * If this is not called, then any ahead-behind atoms will be blank.
175 void filter_ahead_behind(struct repository *r,
176 struct ref_format *format,
177 struct ref_array *array);
179 void ref_filter_init(struct ref_filter *filter);
180 void ref_filter_clear(struct ref_filter *filter);
182 #endif /* REF_FILTER_H */