blob: aa0eea4ecf591edd0620dc47db0e0bfc3e5c4208 [file] [log] [blame]
Karthik Nayak69b1cf92015-06-14 01:07:26 +05301#ifndef REF_FILTER_H
2#define REF_FILTER_H
3
Jeff Kingfe299ec2020-03-30 10:03:46 -04004#include "oid-array.h"
Karthik Nayak69b1cf92015-06-14 01:07:26 +05305#include "refs.h"
6#include "commit.h"
7#include "parse-options.h"
8
9/* Quoting styles */
10#define QUOTE_NONE 0
11#define QUOTE_SHELL 1
12#define QUOTE_PERL 2
13#define QUOTE_PYTHON 4
14#define QUOTE_TCL 8
15
Karthik Nayak5b4f2852015-09-10 21:18:23 +053016#define FILTER_REFS_TAGS 0x0002
17#define FILTER_REFS_BRANCHES 0x0004
18#define FILTER_REFS_REMOTES 0x0008
19#define FILTER_REFS_OTHERS 0x0010
20#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
21 FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
22#define FILTER_REFS_DETACHED_HEAD 0x0020
23#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
Karthik Nayak14de7fb2015-06-14 01:07:28 +053024
Karthik Nayak3a257612015-08-22 09:09:37 +053025struct atom_value;
Junio C Hamano98e7ab62021-10-20 12:23:53 -070026struct ref_sorting;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053027
Junio C Hamano98e7ab62021-10-20 12:23:53 -070028enum ref_sorting_order {
29 REF_SORTING_REVERSE = 1<<0,
30 REF_SORTING_ICASE = 1<<1,
31 REF_SORTING_VERSION = 1<<2,
32 REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
Karthik Nayak69b1cf92015-06-14 01:07:26 +053033};
34
35struct ref_array_item {
brian m. carlsoncedfc412017-05-06 22:10:21 +000036 struct object_id objectname;
ZheNing Hub9dee072021-07-26 03:26:50 +000037 const char *rest;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053038 int flag;
Karthik Nayak5b4f2852015-09-10 21:18:23 +053039 unsigned int kind;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053040 const char *symref;
Karthik Nayak35257aa2015-07-07 21:36:12 +053041 struct commit *commit;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053042 struct atom_value *value;
Karthik Nayak1958a6e2015-06-14 01:07:29 +053043 char refname[FLEX_ARRAY];
Karthik Nayak69b1cf92015-06-14 01:07:26 +053044};
45
46struct ref_array {
47 int nr, alloc;
48 struct ref_array_item **items;
Karthik Nayak1511b222015-09-23 23:41:11 +053049 struct rev_info *revs;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053050};
51
52struct ref_filter {
53 const char **name_patterns;
brian m. carlson910650d2017-03-31 01:40:00 +000054 struct oid_array points_at;
Karthik Nayakee2bd062015-07-07 21:36:16 +053055 struct commit_list *with_commit;
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +000056 struct commit_list *no_commit;
Aaron Lipman21bf9332020-09-15 22:08:40 -040057 struct commit_list *reachable_from;
58 struct commit_list *unreachable_from;
Karthik Nayakee2bd062015-07-07 21:36:16 +053059
Karthik Nayakbef0e122015-09-10 21:18:26 +053060 unsigned int with_commit_tag_algo : 1,
Karthik Nayak1511b222015-09-23 23:41:11 +053061 match_as_path : 1,
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +070062 ignore_case : 1,
Karthik Nayak1511b222015-09-23 23:41:11 +053063 detached : 1;
Karthik Nayak1bb38e52015-09-11 20:34:16 +053064 unsigned int kind,
65 lines;
Karthik Nayak1511b222015-09-23 23:41:11 +053066 int abbrev,
67 verbose;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053068};
69
Jeff King4a68e362017-07-13 11:01:18 -040070struct ref_format {
71 /*
72 * Set these to define the format; make sure you call
73 * verify_ref_format() afterwards to finalize.
74 */
75 const char *format;
ZheNing Hub9dee072021-07-26 03:26:50 +000076 const char *rest;
Jeff King4a68e362017-07-13 11:01:18 -040077 int quote_style;
ZheNing Hub9dee072021-07-26 03:26:50 +000078 int use_rest;
Jeff King11b087a2017-07-13 11:09:32 -040079 int use_color;
Jeff Kingbf285ae2017-07-13 11:02:30 -040080
81 /* Internal state to ref-filter */
82 int need_color_reset_at_eol;
Jeff King4a68e362017-07-13 11:01:18 -040083};
84
ZheNing Hub9dee072021-07-26 03:26:50 +000085#define REF_FORMAT_INIT { .use_color = -1 }
Jeff King4a68e362017-07-13 11:01:18 -040086
Karthik Nayak5afcb902015-07-07 21:36:11 +053087/* Macros for checking --merged and --no-merged options */
88#define _OPT_MERGED_NO_MERGED(option, filter, h) \
89 { OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
90 PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \
91 parse_opt_merge_filter, (intptr_t) "HEAD" \
92 }
93#define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h)
94#define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
95
Jeff King95be7172019-03-20 16:22:15 -040096#define OPT_REF_SORT(var) \
Junio C Hamano98e7ab62021-10-20 12:23:53 -070097 OPT_STRING_LIST(0, "sort", (var), \
98 N_("key"), N_("field name to sort on"))
Jeff King95be7172019-03-20 16:22:15 -040099
Karthik Nayak14de7fb2015-06-14 01:07:28 +0530100/*
101 * API for filtering a set of refs. Based on the type of refs the user
102 * has requested, we iterate through those refs and apply filters
103 * as per the given ref_filter structure and finally store the
104 * filtered refs in the ref_array structure.
105 */
106int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530107/* Clear all memory allocated to ref_array */
108void ref_array_clear(struct ref_array *array);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530109/* Used to verify if the given format is correct and to parse out the used atoms */
Jeff King4a68e362017-07-13 11:01:18 -0400110int verify_ref_format(struct ref_format *format);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530111/* Sort the given ref_array as per the ref_sorting provided */
112void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +0100113/* Set REF_SORTING_* sort_flags for all elements of a sorting list */
114void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on);
Karthik Nayak99c6a712017-01-10 14:19:39 +0530115/* Based on the given format and quote_style, fill the strbuf */
Olga Telezhnaya3019eca2018-03-29 12:49:45 +0000116int format_ref_array_item(struct ref_array_item *info,
ZheNing Hue85fcb32021-07-26 03:26:49 +0000117 struct ref_format *format,
Olga Telezhnaya3019eca2018-03-29 12:49:45 +0000118 struct strbuf *final_buf,
119 struct strbuf *error_buf);
Ævar Arnfjörð Bjarmasone5fb0282021-10-20 20:27:20 +0200120/* Release a "struct ref_sorting" */
121void ref_sorting_release(struct ref_sorting *);
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700122/* Convert list of sort options into ref_sorting */
123struct ref_sorting *ref_sorting_options(struct string_list *);
Karthik Nayak5afcb902015-07-07 21:36:11 +0530124/* Function to parse --merged and --no-merged options */
125int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
Karthik Nayakd4919bb2017-01-10 14:19:38 +0530126/* Get the current HEAD's description */
127char *get_head_description(void);
Karthik Nayak6eac70f2017-01-10 14:19:50 +0530128/* Set up translated strings in the output. */
129void setup_ref_filter_porcelain_msg(void);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530130
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500131/*
132 * Print a single ref, outside of any ref-filter. Note that the
133 * name must be a fully qualified refname.
134 */
Jeff King53df97a2018-04-06 14:58:32 -0400135void pretty_print_ref(const char *name, const struct object_id *oid,
ZheNing Hue85fcb32021-07-26 03:26:49 +0000136 struct ref_format *format);
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500137
Jeff King427cbc92018-04-06 14:59:45 -0400138/*
139 * Push a single ref onto the array; this can be used to construct your own
140 * ref_array without using filter_refs().
141 */
142struct ref_array_item *ref_array_push(struct ref_array *array,
143 const char *refname,
144 const struct object_id *oid);
145
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530146#endif /* REF_FILTER_H */