Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 1 | #ifndef REF_FILTER_H |
| 2 | #define REF_FILTER_H |
| 3 | |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 4 | #include "gettext.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 5 | #include "oid-array.h" |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 6 | #include "commit.h" |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 7 | #include "string-list.h" |
Taylor Blau | 8255dd8 | 2023-07-10 17:12:19 -0400 | [diff] [blame] | 8 | #include "strvec.h" |
Victoria Dye | 6d6e5c5 | 2023-11-14 19:53:51 +0000 | [diff] [blame] | 9 | #include "commit-reach.h" |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 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 | |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 18 | #define FILTER_REFS_TAGS 0x0002 |
| 19 | #define FILTER_REFS_BRANCHES 0x0004 |
| 20 | #define FILTER_REFS_REMOTES 0x0008 |
| 21 | #define FILTER_REFS_OTHERS 0x0010 |
Karthik Nayak | 810f7a1 | 2024-02-23 11:01:11 +0100 | [diff] [blame] | 22 | #define FILTER_REFS_REGULAR (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \ |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 23 | FILTER_REFS_REMOTES | FILTER_REFS_OTHERS) |
| 24 | #define FILTER_REFS_DETACHED_HEAD 0x0020 |
Karthik Nayak | 33d15b5 | 2024-02-23 11:01:12 +0100 | [diff] [blame] | 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) |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 29 | |
Karthik Nayak | 3a25761 | 2015-08-22 09:09:37 +0530 | [diff] [blame] | 30 | struct atom_value; |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 31 | struct ref_sorting; |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 32 | struct ahead_behind_count; |
SZEDER Gábor | c4d9c79 | 2023-03-19 17:27:12 +0100 | [diff] [blame] | 33 | struct option; |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 34 | |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 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, |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct ref_array_item { |
brian m. carlson | cedfc41 | 2017-05-06 22:10:21 +0000 | [diff] [blame] | 43 | struct object_id objectname; |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 44 | const char *rest; |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 45 | int flag; |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 46 | unsigned int kind; |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 47 | const char *symref; |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 48 | struct commit *commit; |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 49 | struct atom_value *value; |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 50 | struct ahead_behind_count **counts; |
| 51 | |
Karthik Nayak | 1958a6e | 2015-06-14 01:07:29 +0530 | [diff] [blame] | 52 | char refname[FLEX_ARRAY]; |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct ref_array { |
| 56 | int nr, alloc; |
| 57 | struct ref_array_item **items; |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 58 | struct rev_info *revs; |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 59 | |
| 60 | struct ahead_behind_count *counts; |
| 61 | size_t counts_nr; |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct ref_filter { |
| 65 | const char **name_patterns; |
Taylor Blau | 8255dd8 | 2023-07-10 17:12:19 -0400 | [diff] [blame] | 66 | struct strvec exclude; |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 67 | struct oid_array points_at; |
Karthik Nayak | ee2bd06 | 2015-07-07 21:36:16 +0530 | [diff] [blame] | 68 | struct commit_list *with_commit; |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 69 | struct commit_list *no_commit; |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 70 | struct commit_list *reachable_from; |
| 71 | struct commit_list *unreachable_from; |
Karthik Nayak | ee2bd06 | 2015-07-07 21:36:16 +0530 | [diff] [blame] | 72 | |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 73 | unsigned int with_commit_tag_algo : 1, |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 74 | match_as_path : 1, |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 75 | ignore_case : 1, |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 76 | detached : 1; |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 77 | unsigned int kind, |
| 78 | lines; |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 79 | int abbrev, |
| 80 | verbose; |
Victoria Dye | 6d6e5c5 | 2023-11-14 19:53:51 +0000 | [diff] [blame] | 81 | |
| 82 | struct { |
| 83 | struct contains_cache contains_cache; |
| 84 | struct contains_cache no_contains_cache; |
| 85 | } internal; |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 86 | }; |
| 87 | |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 88 | struct ref_format { |
| 89 | /* |
| 90 | * Set these to define the format; make sure you call |
| 91 | * verify_ref_format() afterwards to finalize. |
| 92 | */ |
| 93 | const char *format; |
ZheNing Hu | b9dee07 | 2021-07-26 03:26:50 +0000 | [diff] [blame] | 94 | const char *rest; |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 95 | int quote_style; |
Jeff King | 11b087a | 2017-07-13 11:09:32 -0400 | [diff] [blame] | 96 | int use_color; |
Jeff King | bf285ae | 2017-07-13 11:02:30 -0400 | [diff] [blame] | 97 | |
| 98 | /* Internal state to ref-filter */ |
| 99 | int need_color_reset_at_eol; |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 100 | |
| 101 | /* List of bases for ahead-behind counts. */ |
| 102 | struct string_list bases; |
Victoria Dye | 9d4fcfe | 2023-11-14 19:53:50 +0000 | [diff] [blame] | 103 | |
| 104 | struct { |
| 105 | int max_count; |
| 106 | int omit_empty; |
| 107 | } array_opts; |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 108 | }; |
| 109 | |
Jeff King | b9f7daa | 2023-07-10 17:12:07 -0400 | [diff] [blame] | 110 | #define REF_FILTER_INIT { \ |
| 111 | .points_at = OID_ARRAY_INIT, \ |
Taylor Blau | 8255dd8 | 2023-07-10 17:12:19 -0400 | [diff] [blame] | 112 | .exclude = STRVEC_INIT, \ |
Jeff King | b9f7daa | 2023-07-10 17:12:07 -0400 | [diff] [blame] | 113 | } |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 114 | #define REF_FORMAT_INIT { \ |
| 115 | .use_color = -1, \ |
| 116 | .bases = STRING_LIST_INIT_DUP, \ |
| 117 | } |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 118 | |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 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" \ |
| 124 | } |
| 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) |
| 127 | |
Jeff King | 95be717 | 2019-03-20 16:22:15 -0400 | [diff] [blame] | 128 | #define OPT_REF_SORT(var) \ |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 129 | OPT_STRING_LIST(0, "sort", (var), \ |
| 130 | N_("key"), N_("field name to sort on")) |
Taylor Blau | 8255dd8 | 2023-07-10 17:12:19 -0400 | [diff] [blame] | 131 | #define OPT_REF_FILTER_EXCLUDE(var) \ |
| 132 | OPT_STRVEC(0, "exclude", &(var)->exclude, \ |
| 133 | N_("pattern"), N_("exclude refs which match pattern")) |
Jeff King | 95be717 | 2019-03-20 16:22:15 -0400 | [diff] [blame] | 134 | |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 135 | /* |
| 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. |
| 140 | */ |
| 141 | int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type); |
Victoria Dye | e7574b0 | 2023-11-14 19:53:52 +0000 | [diff] [blame] | 142 | /* |
| 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. |
| 146 | */ |
| 147 | void filter_and_format_refs(struct ref_filter *filter, unsigned int type, |
| 148 | struct ref_sorting *sorting, |
| 149 | struct ref_format *format); |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 150 | /* Clear all memory allocated to ref_array */ |
| 151 | void ref_array_clear(struct ref_array *array); |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 152 | /* Used to verify if the given format is correct and to parse out the used atoms */ |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 153 | int verify_ref_format(struct ref_format *format); |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 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); |
Ævar Arnfjörð Bjarmason | 7c269a7 | 2021-01-07 10:51:51 +0100 | [diff] [blame] | 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); |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 158 | /* Based on the given format and quote_style, fill the strbuf */ |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 159 | int format_ref_array_item(struct ref_array_item *info, |
ZheNing Hu | e85fcb3 | 2021-07-26 03:26:49 +0000 | [diff] [blame] | 160 | struct ref_format *format, |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 161 | struct strbuf *final_buf, |
| 162 | struct strbuf *error_buf); |
Ævar Arnfjörð Bjarmason | e5fb028 | 2021-10-20 20:27:20 +0200 | [diff] [blame] | 163 | /* Release a "struct ref_sorting" */ |
| 164 | void ref_sorting_release(struct ref_sorting *); |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 165 | /* Convert list of sort options into ref_sorting */ |
| 166 | struct ref_sorting *ref_sorting_options(struct string_list *); |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 167 | /* Function to parse --merged and --no-merged options */ |
| 168 | int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset); |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 169 | /* Get the current HEAD's description */ |
| 170 | char *get_head_description(void); |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 171 | /* Set up translated strings in the output. */ |
| 172 | void setup_ref_filter_porcelain_msg(void); |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 173 | |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 174 | /* |
Victoria Dye | e7574b0 | 2023-11-14 19:53:52 +0000 | [diff] [blame] | 175 | * Print up to maxcount ref_array elements to stdout using the given |
| 176 | * ref_format. |
| 177 | */ |
| 178 | void print_formatted_ref_array(struct ref_array *array, struct ref_format *format); |
| 179 | |
| 180 | /* |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 181 | * Print a single ref, outside of any ref-filter. Note that the |
| 182 | * name must be a fully qualified refname. |
| 183 | */ |
Jeff King | 53df97a | 2018-04-06 14:58:32 -0400 | [diff] [blame] | 184 | void pretty_print_ref(const char *name, const struct object_id *oid, |
ZheNing Hu | e85fcb3 | 2021-07-26 03:26:49 +0000 | [diff] [blame] | 185 | struct ref_format *format); |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 186 | |
Jeff King | 427cbc9 | 2018-04-06 14:59:45 -0400 | [diff] [blame] | 187 | /* |
| 188 | * Push a single ref onto the array; this can be used to construct your own |
| 189 | * ref_array without using filter_refs(). |
| 190 | */ |
| 191 | struct ref_array_item *ref_array_push(struct ref_array *array, |
| 192 | const char *refname, |
| 193 | const struct object_id *oid); |
| 194 | |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 195 | /* |
| 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. |
| 199 | * |
| 200 | * If this is not called, then any ahead-behind atoms will be blank. |
| 201 | */ |
| 202 | void filter_ahead_behind(struct repository *r, |
| 203 | struct ref_format *format, |
| 204 | struct ref_array *array); |
| 205 | |
Jeff King | b571fb9 | 2023-07-10 17:12:13 -0400 | [diff] [blame] | 206 | void ref_filter_init(struct ref_filter *filter); |
| 207 | void ref_filter_clear(struct ref_filter *filter); |
| 208 | |
Karthik Nayak | 69b1cf9 | 2015-06-14 01:07:26 +0530 | [diff] [blame] | 209 | #endif /* REF_FILTER_H */ |