blob: 0ca28d2bba6f2aed4b8d084972739f3a88f44caa [file] [log] [blame]
Karthik Nayak69b1cf92015-06-14 01:07:26 +05301#ifndef REF_FILTER_H
2#define REF_FILTER_H
3
Elijah Newrenf394e092023-03-21 06:25:54 +00004#include "gettext.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -04005#include "oid-array.h"
Karthik Nayak69b1cf92015-06-14 01:07:26 +05306#include "commit.h"
Derrick Stolee49abcd22023-03-20 11:26:54 +00007#include "string-list.h"
Taylor Blau8255dd82023-07-10 17:12:19 -04008#include "strvec.h"
Victoria Dye6d6e5c52023-11-14 19:53:51 +00009#include "commit-reach.h"
Karthik Nayak69b1cf92015-06-14 01:07:26 +053010
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 Nayak5b4f2852015-09-10 21:18:23 +053018#define FILTER_REFS_TAGS 0x0002
19#define FILTER_REFS_BRANCHES 0x0004
20#define FILTER_REFS_REMOTES 0x0008
21#define FILTER_REFS_OTHERS 0x0010
Karthik Nayak810f7a12024-02-23 11:01:11 +010022#define FILTER_REFS_REGULAR (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
Karthik Nayak5b4f2852015-09-10 21:18:23 +053023 FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
24#define FILTER_REFS_DETACHED_HEAD 0x0020
Karthik Nayak33d15b52024-02-23 11:01:12 +010025#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 Nayak14de7fb2015-06-14 01:07:28 +053029
Karthik Nayak3a257612015-08-22 09:09:37 +053030struct atom_value;
Junio C Hamano98e7ab62021-10-20 12:23:53 -070031struct ref_sorting;
Derrick Stolee49abcd22023-03-20 11:26:54 +000032struct ahead_behind_count;
SZEDER Gáborc4d9c792023-03-19 17:27:12 +010033struct option;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053034
Junio C Hamano98e7ab62021-10-20 12:23:53 -070035enum 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 Nayak69b1cf92015-06-14 01:07:26 +053040};
41
42struct ref_array_item {
brian m. carlsoncedfc412017-05-06 22:10:21 +000043 struct object_id objectname;
ZheNing Hub9dee072021-07-26 03:26:50 +000044 const char *rest;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053045 int flag;
Karthik Nayak5b4f2852015-09-10 21:18:23 +053046 unsigned int kind;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053047 const char *symref;
Karthik Nayak35257aa2015-07-07 21:36:12 +053048 struct commit *commit;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053049 struct atom_value *value;
Derrick Stolee49abcd22023-03-20 11:26:54 +000050 struct ahead_behind_count **counts;
51
Karthik Nayak1958a6e2015-06-14 01:07:29 +053052 char refname[FLEX_ARRAY];
Karthik Nayak69b1cf92015-06-14 01:07:26 +053053};
54
55struct ref_array {
56 int nr, alloc;
57 struct ref_array_item **items;
Karthik Nayak1511b222015-09-23 23:41:11 +053058 struct rev_info *revs;
Derrick Stolee49abcd22023-03-20 11:26:54 +000059
60 struct ahead_behind_count *counts;
61 size_t counts_nr;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053062};
63
64struct ref_filter {
65 const char **name_patterns;
Taylor Blau8255dd82023-07-10 17:12:19 -040066 struct strvec exclude;
brian m. carlson910650d2017-03-31 01:40:00 +000067 struct oid_array points_at;
Karthik Nayakee2bd062015-07-07 21:36:16 +053068 struct commit_list *with_commit;
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +000069 struct commit_list *no_commit;
Aaron Lipman21bf9332020-09-15 22:08:40 -040070 struct commit_list *reachable_from;
71 struct commit_list *unreachable_from;
Karthik Nayakee2bd062015-07-07 21:36:16 +053072
Karthik Nayakbef0e122015-09-10 21:18:26 +053073 unsigned int with_commit_tag_algo : 1,
Karthik Nayak1511b222015-09-23 23:41:11 +053074 match_as_path : 1,
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +070075 ignore_case : 1,
Karthik Nayak1511b222015-09-23 23:41:11 +053076 detached : 1;
Karthik Nayak1bb38e52015-09-11 20:34:16 +053077 unsigned int kind,
78 lines;
Karthik Nayak1511b222015-09-23 23:41:11 +053079 int abbrev,
80 verbose;
Victoria Dye6d6e5c52023-11-14 19:53:51 +000081
82 struct {
83 struct contains_cache contains_cache;
84 struct contains_cache no_contains_cache;
85 } internal;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053086};
87
Jeff King4a68e362017-07-13 11:01:18 -040088struct 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 Hub9dee072021-07-26 03:26:50 +000094 const char *rest;
Jeff King4a68e362017-07-13 11:01:18 -040095 int quote_style;
Jeff King11b087a2017-07-13 11:09:32 -040096 int use_color;
Jeff Kingbf285ae2017-07-13 11:02:30 -040097
98 /* Internal state to ref-filter */
99 int need_color_reset_at_eol;
Derrick Stolee49abcd22023-03-20 11:26:54 +0000100
101 /* List of bases for ahead-behind counts. */
102 struct string_list bases;
Victoria Dye9d4fcfe2023-11-14 19:53:50 +0000103
104 struct {
105 int max_count;
106 int omit_empty;
107 } array_opts;
Jeff King4a68e362017-07-13 11:01:18 -0400108};
109
Jeff Kingb9f7daa2023-07-10 17:12:07 -0400110#define REF_FILTER_INIT { \
111 .points_at = OID_ARRAY_INIT, \
Taylor Blau8255dd82023-07-10 17:12:19 -0400112 .exclude = STRVEC_INIT, \
Jeff Kingb9f7daa2023-07-10 17:12:07 -0400113}
Derrick Stolee49abcd22023-03-20 11:26:54 +0000114#define REF_FORMAT_INIT { \
115 .use_color = -1, \
116 .bases = STRING_LIST_INIT_DUP, \
117}
Jeff King4a68e362017-07-13 11:01:18 -0400118
Karthik Nayak5afcb902015-07-07 21:36:11 +0530119/* 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 King95be7172019-03-20 16:22:15 -0400128#define OPT_REF_SORT(var) \
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700129 OPT_STRING_LIST(0, "sort", (var), \
130 N_("key"), N_("field name to sort on"))
Taylor Blau8255dd82023-07-10 17:12:19 -0400131#define OPT_REF_FILTER_EXCLUDE(var) \
132 OPT_STRVEC(0, "exclude", &(var)->exclude, \
133 N_("pattern"), N_("exclude refs which match pattern"))
Jeff King95be7172019-03-20 16:22:15 -0400134
Karthik Nayak14de7fb2015-06-14 01:07:28 +0530135/*
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 */
141int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
Victoria Dyee7574b02023-11-14 19:53:52 +0000142/*
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 */
147void filter_and_format_refs(struct ref_filter *filter, unsigned int type,
148 struct ref_sorting *sorting,
149 struct ref_format *format);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530150/* Clear all memory allocated to ref_array */
151void ref_array_clear(struct ref_array *array);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530152/* Used to verify if the given format is correct and to parse out the used atoms */
Jeff King4a68e362017-07-13 11:01:18 -0400153int verify_ref_format(struct ref_format *format);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530154/* Sort the given ref_array as per the ref_sorting provided */
155void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +0100156/* Set REF_SORTING_* sort_flags for all elements of a sorting list */
157void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on);
Karthik Nayak99c6a712017-01-10 14:19:39 +0530158/* Based on the given format and quote_style, fill the strbuf */
Olga Telezhnaya3019eca2018-03-29 12:49:45 +0000159int format_ref_array_item(struct ref_array_item *info,
ZheNing Hue85fcb32021-07-26 03:26:49 +0000160 struct ref_format *format,
Olga Telezhnaya3019eca2018-03-29 12:49:45 +0000161 struct strbuf *final_buf,
162 struct strbuf *error_buf);
Ævar Arnfjörð Bjarmasone5fb0282021-10-20 20:27:20 +0200163/* Release a "struct ref_sorting" */
164void ref_sorting_release(struct ref_sorting *);
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700165/* Convert list of sort options into ref_sorting */
166struct ref_sorting *ref_sorting_options(struct string_list *);
Karthik Nayak5afcb902015-07-07 21:36:11 +0530167/* Function to parse --merged and --no-merged options */
168int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
Karthik Nayakd4919bb2017-01-10 14:19:38 +0530169/* Get the current HEAD's description */
170char *get_head_description(void);
Karthik Nayak6eac70f2017-01-10 14:19:50 +0530171/* Set up translated strings in the output. */
172void setup_ref_filter_porcelain_msg(void);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530173
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500174/*
Victoria Dyee7574b02023-11-14 19:53:52 +0000175 * Print up to maxcount ref_array elements to stdout using the given
176 * ref_format.
177 */
178void print_formatted_ref_array(struct ref_array *array, struct ref_format *format);
179
180/*
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500181 * Print a single ref, outside of any ref-filter. Note that the
182 * name must be a fully qualified refname.
183 */
Jeff King53df97a2018-04-06 14:58:32 -0400184void pretty_print_ref(const char *name, const struct object_id *oid,
ZheNing Hue85fcb32021-07-26 03:26:49 +0000185 struct ref_format *format);
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500186
Jeff King427cbc92018-04-06 14:59:45 -0400187/*
188 * Push a single ref onto the array; this can be used to construct your own
189 * ref_array without using filter_refs().
190 */
191struct ref_array_item *ref_array_push(struct ref_array *array,
192 const char *refname,
193 const struct object_id *oid);
194
Derrick Stolee49abcd22023-03-20 11:26:54 +0000195/*
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 */
202void filter_ahead_behind(struct repository *r,
203 struct ref_format *format,
204 struct ref_array *array);
205
Jeff Kingb571fb92023-07-10 17:12:13 -0400206void ref_filter_init(struct ref_filter *filter);
207void ref_filter_clear(struct ref_filter *filter);
208
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530209#endif /* REF_FILTER_H */