blob: 85c8ebc3b904e9b44bed8b164b9cdf62839d6dae [file] [log] [blame]
Karthik Nayak69b1cf92015-06-14 01:07:26 +05301#ifndef REF_FILTER_H
2#define REF_FILTER_H
3
4#include "sha1-array.h"
5#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_INCLUDE_BROKEN 0x0001
17#define FILTER_REFS_TAGS 0x0002
18#define FILTER_REFS_BRANCHES 0x0004
19#define FILTER_REFS_REMOTES 0x0008
20#define FILTER_REFS_OTHERS 0x0010
21#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
22 FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
23#define FILTER_REFS_DETACHED_HEAD 0x0020
24#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
Karthik Nayak14de7fb2015-06-14 01:07:28 +053025
Karthik Nayak3a257612015-08-22 09:09:37 +053026struct atom_value;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053027
28struct ref_sorting {
29 struct ref_sorting *next;
30 int atom; /* index into used_atom array (internal) */
Karthik Nayak90c00402015-09-10 21:18:25 +053031 unsigned reverse : 1,
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +070032 ignore_case : 1,
Karthik Nayak90c00402015-09-10 21:18:25 +053033 version : 1;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053034};
35
36struct ref_array_item {
brian m. carlsoncedfc412017-05-06 22:10:21 +000037 struct object_id objectname;
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;
Karthik Nayak35257aa2015-07-07 21:36:12 +053057
58 enum {
59 REF_FILTER_MERGED_NONE = 0,
60 REF_FILTER_MERGED_INCLUDE,
61 REF_FILTER_MERGED_OMIT
62 } merge;
63 struct commit *merge_commit;
Karthik Nayakee2bd062015-07-07 21:36:16 +053064
Karthik Nayakbef0e122015-09-10 21:18:26 +053065 unsigned int with_commit_tag_algo : 1,
Karthik Nayak1511b222015-09-23 23:41:11 +053066 match_as_path : 1,
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +070067 ignore_case : 1,
Karthik Nayak1511b222015-09-23 23:41:11 +053068 detached : 1;
Karthik Nayak1bb38e52015-09-11 20:34:16 +053069 unsigned int kind,
70 lines;
Karthik Nayak1511b222015-09-23 23:41:11 +053071 int abbrev,
72 verbose;
Karthik Nayak69b1cf92015-06-14 01:07:26 +053073};
74
Jeff King4a68e362017-07-13 11:01:18 -040075struct ref_format {
76 /*
77 * Set these to define the format; make sure you call
78 * verify_ref_format() afterwards to finalize.
79 */
80 const char *format;
81 int quote_style;
Jeff King11b087a2017-07-13 11:09:32 -040082 int use_color;
Jeff Kingbf285ae2017-07-13 11:02:30 -040083
84 /* Internal state to ref-filter */
85 int need_color_reset_at_eol;
Jeff King4a68e362017-07-13 11:01:18 -040086};
87
Jeff King11b087a2017-07-13 11:09:32 -040088#define REF_FORMAT_INIT { NULL, 0, -1 }
Jeff King4a68e362017-07-13 11:01:18 -040089
Karthik Nayak5afcb902015-07-07 21:36:11 +053090/* Macros for checking --merged and --no-merged options */
91#define _OPT_MERGED_NO_MERGED(option, filter, h) \
92 { OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
93 PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \
94 parse_opt_merge_filter, (intptr_t) "HEAD" \
95 }
96#define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h)
97#define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
98
Karthik Nayak14de7fb2015-06-14 01:07:28 +053099/*
100 * API for filtering a set of refs. Based on the type of refs the user
101 * has requested, we iterate through those refs and apply filters
102 * as per the given ref_filter structure and finally store the
103 * filtered refs in the ref_array structure.
104 */
105int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530106/* Clear all memory allocated to ref_array */
107void ref_array_clear(struct ref_array *array);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530108/* Used to verify if the given format is correct and to parse out the used atoms */
Jeff King4a68e362017-07-13 11:01:18 -0400109int verify_ref_format(struct ref_format *format);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530110/* Sort the given ref_array as per the ref_sorting provided */
111void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
Karthik Nayak99c6a712017-01-10 14:19:39 +0530112/* Based on the given format and quote_style, fill the strbuf */
Olga Telezhnaya3019eca2018-03-29 12:49:45 +0000113int format_ref_array_item(struct ref_array_item *info,
114 const struct ref_format *format,
115 struct strbuf *final_buf,
116 struct strbuf *error_buf);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530117/* Print the ref using the given format and quote_style */
Jeff King4a68e362017-07-13 11:01:18 -0400118void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
Jeff King18a25652017-07-13 11:02:44 -0400119/* Parse a single sort specifier and add it to the list */
120void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530121/* Callback function for parsing the sort option */
122int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
123/* Default sort option based on refname */
124struct ref_sorting *ref_default_sorting(void);
Karthik Nayak5afcb902015-07-07 21:36:11 +0530125/* Function to parse --merged and --no-merged options */
126int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
Karthik Nayakd4919bb2017-01-10 14:19:38 +0530127/* Get the current HEAD's description */
128char *get_head_description(void);
Karthik Nayak6eac70f2017-01-10 14:19:50 +0530129/* Set up translated strings in the output. */
130void setup_ref_filter_porcelain_msg(void);
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530131
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500132/*
133 * Print a single ref, outside of any ref-filter. Note that the
134 * name must be a fully qualified refname.
135 */
Jeff King53df97a2018-04-06 14:58:32 -0400136void pretty_print_ref(const char *name, const struct object_id *oid,
Jeff King4a68e362017-07-13 11:01:18 -0400137 const struct ref_format *format);
Lukas Puehringer2111aa72017-01-17 18:37:19 -0500138
Jeff King427cbc92018-04-06 14:59:45 -0400139/*
140 * Push a single ref onto the array; this can be used to construct your own
141 * ref_array without using filter_refs().
142 */
143struct ref_array_item *ref_array_push(struct ref_array *array,
144 const char *refname,
145 const struct object_id *oid);
146
Karthik Nayak69b1cf92015-06-14 01:07:26 +0530147#endif /* REF_FILTER_H */