blob: 1524bc463a5701d475cb68e50caa884a6daf890a [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 "refs.h"
7#include "commit.h"
Derrick Stolee49abcd22023-03-20 11:26:54 +00008#include "string-list.h"
Taylor Blau8255dd82023-07-10 17:12:19 -04009#include "strvec.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
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)
Karthik Nayak14de7fb2015-06-14 01:07:28 +053026
Karthik Nayak3a257612015-08-22 09:09:37 +053027struct atom_value;
Junio C Hamano98e7ab62021-10-20 12:23:53 -070028struct ref_sorting;
Derrick Stolee49abcd22023-03-20 11:26:54 +000029struct ahead_behind_count;
SZEDER Gáborc4d9c792023-03-19 17:27:12 +010030struct option;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053031
Junio C Hamano98e7ab62021-10-20 12:23:53 -070032enum 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,
Karthik Nayak69b1cf92015-06-14 01:07:26 +053037};
38
39struct ref_array_item {
brian m. carlsoncedfc412017-05-06 22:10:21 +000040 struct object_id objectname;
ZheNing Hub9dee072021-07-26 03:26:50 +000041 const char *rest;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053042 int flag;
Karthik Nayak5b4f2852015-09-10 21:18:23 +053043 unsigned int kind;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053044 const char *symref;
Karthik Nayak35257aa2015-07-07 21:36:12 +053045 struct commit *commit;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053046 struct atom_value *value;
Derrick Stolee49abcd22023-03-20 11:26:54 +000047 struct ahead_behind_count **counts;
48
Karthik Nayak1958a6e2015-06-14 01:07:29 +053049 char refname[FLEX_ARRAY];
Karthik Nayak69b1cf92015-06-14 01:07:26 +053050};
51
52struct ref_array {
53 int nr, alloc;
54 struct ref_array_item **items;
Karthik Nayak1511b222015-09-23 23:41:11 +053055 struct rev_info *revs;
Derrick Stolee49abcd22023-03-20 11:26:54 +000056
57 struct ahead_behind_count *counts;
58 size_t counts_nr;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053059};
60
61struct ref_filter {
62 const char **name_patterns;
Taylor Blau8255dd82023-07-10 17:12:19 -040063 struct strvec exclude;
brian m. carlson910650d2017-03-31 01:40:00 +000064 struct oid_array points_at;
Karthik Nayakee2bd062015-07-07 21:36:16 +053065 struct commit_list *with_commit;
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +000066 struct commit_list *no_commit;
Aaron Lipman21bf9332020-09-15 22:08:40 -040067 struct commit_list *reachable_from;
68 struct commit_list *unreachable_from;
Karthik Nayakee2bd062015-07-07 21:36:16 +053069
Karthik Nayakbef0e122015-09-10 21:18:26 +053070 unsigned int with_commit_tag_algo : 1,
Karthik Nayak1511b222015-09-23 23:41:11 +053071 match_as_path : 1,
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +070072 ignore_case : 1,
Karthik Nayak1511b222015-09-23 23:41:11 +053073 detached : 1;
Karthik Nayak1bb38e52015-09-11 20:34:16 +053074 unsigned int kind,
75 lines;
Karthik Nayak1511b222015-09-23 23:41:11 +053076 int abbrev,
77 verbose;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053078};
79
Jeff King4a68e362017-07-13 11:01:18 -040080struct ref_format {
81 /*
82 * Set these to define the format; make sure you call
83 * verify_ref_format() afterwards to finalize.
84 */
85 const char *format;
ZheNing Hub9dee072021-07-26 03:26:50 +000086 const char *rest;
Jeff King4a68e362017-07-13 11:01:18 -040087 int quote_style;
Jeff King11b087a2017-07-13 11:09:32 -040088 int use_color;
Jeff Kingbf285ae2017-07-13 11:02:30 -040089
90 /* Internal state to ref-filter */
91 int need_color_reset_at_eol;
Derrick Stolee49abcd22023-03-20 11:26:54 +000092
93 /* List of bases for ahead-behind counts. */
94 struct string_list bases;
Jeff King4a68e362017-07-13 11:01:18 -040095};
96
Jeff Kingb9f7daa2023-07-10 17:12:07 -040097#define REF_FILTER_INIT { \
98 .points_at = OID_ARRAY_INIT, \
Taylor Blau8255dd82023-07-10 17:12:19 -040099 .exclude = STRVEC_INIT, \
Jeff Kingb9f7daa2023-07-10 17:12:07 -0400100}
Derrick Stolee49abcd22023-03-20 11:26:54 +0000101#define REF_FORMAT_INIT { \
102 .use_color = -1, \
103 .bases = STRING_LIST_INIT_DUP, \
104}
Jeff King4a68e362017-07-13 11:01:18 -0400105
Karthik Nayak5afcb902015-07-07 21:36:11 +0530106/* 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" \
111 }
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)
114
Jeff King95be7172019-03-20 16:22:15 -0400115#define OPT_REF_SORT(var) \
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700116 OPT_STRING_LIST(0, "sort", (var), \
117 N_("key"), N_("field name to sort on"))
Taylor Blau8255dd82023-07-10 17:12:19 -0400118#define OPT_REF_FILTER_EXCLUDE(var) \
119 OPT_STRVEC(0, "exclude", &(var)->exclude, \
120 N_("pattern"), N_("exclude refs which match pattern"))
Jeff King95be7172019-03-20 16:22:15 -0400121
Karthik Nayak14de7fb2015-06-14 01:07:28 +0530122/*
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.
127 */
128int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530129/* Clear all memory allocated to ref_array */
130void ref_array_clear(struct ref_array *array);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530131/* Used to verify if the given format is correct and to parse out the used atoms */
Jeff King4a68e362017-07-13 11:01:18 -0400132int verify_ref_format(struct ref_format *format);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530133/* Sort the given ref_array as per the ref_sorting provided */
134void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +0100135/* Set REF_SORTING_* sort_flags for all elements of a sorting list */
136void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on);
Karthik Nayak99c6a712017-01-10 14:19:39 +0530137/* Based on the given format and quote_style, fill the strbuf */
Olga Telezhnaya3019eca2018-03-29 12:49:45 +0000138int format_ref_array_item(struct ref_array_item *info,
ZheNing Hue85fcb32021-07-26 03:26:49 +0000139 struct ref_format *format,
Olga Telezhnaya3019eca2018-03-29 12:49:45 +0000140 struct strbuf *final_buf,
141 struct strbuf *error_buf);
Ævar Arnfjörð Bjarmasone5fb0282021-10-20 20:27:20 +0200142/* Release a "struct ref_sorting" */
143void ref_sorting_release(struct ref_sorting *);
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700144/* Convert list of sort options into ref_sorting */
145struct ref_sorting *ref_sorting_options(struct string_list *);
Karthik Nayak5afcb902015-07-07 21:36:11 +0530146/* Function to parse --merged and --no-merged options */
147int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
Karthik Nayakd4919bb2017-01-10 14:19:38 +0530148/* Get the current HEAD's description */
149char *get_head_description(void);
Karthik Nayak6eac70f2017-01-10 14:19:50 +0530150/* Set up translated strings in the output. */
151void setup_ref_filter_porcelain_msg(void);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530152
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500153/*
154 * Print a single ref, outside of any ref-filter. Note that the
155 * name must be a fully qualified refname.
156 */
Jeff King53df97a2018-04-06 14:58:32 -0400157void pretty_print_ref(const char *name, const struct object_id *oid,
ZheNing Hue85fcb32021-07-26 03:26:49 +0000158 struct ref_format *format);
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500159
Jeff King427cbc92018-04-06 14:59:45 -0400160/*
161 * Push a single ref onto the array; this can be used to construct your own
162 * ref_array without using filter_refs().
163 */
164struct ref_array_item *ref_array_push(struct ref_array *array,
165 const char *refname,
166 const struct object_id *oid);
167
Derrick Stolee49abcd22023-03-20 11:26:54 +0000168/*
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.
172 *
173 * If this is not called, then any ahead-behind atoms will be blank.
174 */
175void filter_ahead_behind(struct repository *r,
176 struct ref_format *format,
177 struct ref_array *array);
178
Jeff Kingb571fb92023-07-10 17:12:13 -0400179void ref_filter_init(struct ref_filter *filter);
180void ref_filter_clear(struct ref_filter *filter);
181
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530182#endif /* REF_FILTER_H */