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