]> git.ipfire.org Git - thirdparty/git.git/blame - grep.h
The twentieth batch
[thirdparty/git.git] / grep.h
CommitLineData
83b5d2f5
JH
1#ifndef GREP_H
2#define GREP_H
7e8f59d5 3#include "color.h"
94da9193
ÆAB
4#ifdef USE_LIBPCRE2
5#define PCRE2_CODE_UNIT_WIDTH 8
6#include <pcre2.h>
797c3599
ÆAB
7#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 36) || PCRE2_MAJOR >= 11
8#define GIT_PCRE2_VERSION_10_36_OR_HIGHER
9#endif
14b9a044
MK
10#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 35) || PCRE2_MAJOR >= 11
11#define GIT_PCRE2_VERSION_10_35_OR_HIGHER
12#endif
b76bf27f
ÆAB
13#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 34) || PCRE2_MAJOR >= 11
14#define GIT_PCRE2_VERSION_10_34_OR_HIGHER
15#endif
94da9193
ÆAB
16#else
17typedef int pcre2_code;
18typedef int pcre2_match_data;
19typedef int pcre2_compile_context;
cbe81e65 20typedef int pcre2_general_context;
94da9193 21#endif
95ca1f98
ÆAB
22#ifndef PCRE2_MATCH_INVALID_UTF
23/* PCRE2_MATCH_* dummy also with !USE_LIBPCRE2, for test-pcre2-config.c */
24#define PCRE2_MATCH_INVALID_UTF 0
25#endif
0579f91d 26#include "thread-utils.h"
94ad9d9e 27#include "userdiff.h"
83b5d2f5 28
38bbc2ea
NTND
29struct repository;
30
83b5d2f5
JH
31enum grep_pat_token {
32 GREP_PATTERN,
480c1ca6
JH
33 GREP_PATTERN_HEAD,
34 GREP_PATTERN_BODY,
83b5d2f5
JH
35 GREP_AND,
36 GREP_OPEN_PAREN,
37 GREP_CLOSE_PAREN,
38 GREP_NOT,
4b05548f 39 GREP_OR
83b5d2f5
JH
40};
41
480c1ca6
JH
42enum grep_context {
43 GREP_CONTEXT_HEAD,
4b05548f 44 GREP_CONTEXT_BODY
480c1ca6
JH
45};
46
a4d7d2c6 47enum grep_header_field {
3ce3ffb8
AP
48 GREP_HEADER_FIELD_MIN = 0,
49 GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
ad4813b3 50 GREP_HEADER_COMMITTER,
72fd13f7 51 GREP_HEADER_REFLOG,
ad4813b3
NTND
52
53 /* Must be at the end of the enum */
54 GREP_HEADER_FIELD_MAX
a4d7d2c6
JH
55};
56
fa151dc5
NTND
57enum grep_color {
58 GREP_COLOR_CONTEXT,
59 GREP_COLOR_FILENAME,
60 GREP_COLOR_FUNCTION,
61 GREP_COLOR_LINENO,
d036d667 62 GREP_COLOR_COLUMNNO,
fa151dc5
NTND
63 GREP_COLOR_MATCH_CONTEXT,
64 GREP_COLOR_MATCH_SELECTED,
65 GREP_COLOR_SELECTED,
66 GREP_COLOR_SEP,
67 NR_GREP_COLORS
68};
69
83b5d2f5
JH
70struct grep_pat {
71 struct grep_pat *next;
72 const char *origin;
73 int no;
74 enum grep_pat_token token;
526a858a 75 char *pattern;
ed40a095 76 size_t patternlen;
a4d7d2c6 77 enum grep_header_field field;
83b5d2f5 78 regex_t regexp;
94da9193
ÆAB
79 pcre2_code *pcre2_pattern;
80 pcre2_match_data *pcre2_match_data;
81 pcre2_compile_context *pcre2_compile_context;
cbe81e65 82 pcre2_general_context *pcre2_general_context;
10da030a 83 const uint8_t *pcre2_tables;
94da9193 84 uint32_t pcre2_jit_on;
c822255c 85 unsigned fixed:1;
09872f64 86 unsigned is_fixed:1;
5183bf67 87 unsigned ignore_case:1;
d7eb527d 88 unsigned word_regexp:1;
83b5d2f5
JH
89};
90
91enum grep_expr_node {
92 GREP_NODE_ATOM,
93 GREP_NODE_NOT,
94 GREP_NODE_AND,
5aaeb733 95 GREP_NODE_TRUE,
4b05548f 96 GREP_NODE_OR
83b5d2f5
JH
97};
98
84befcd0
S
99enum grep_pattern_type {
100 GREP_PATTERN_TYPE_UNSPECIFIED = 0,
101 GREP_PATTERN_TYPE_BRE,
102 GREP_PATTERN_TYPE_ERE,
103 GREP_PATTERN_TYPE_FIXED,
104 GREP_PATTERN_TYPE_PCRE
105};
106
83b5d2f5
JH
107struct grep_expr {
108 enum grep_expr_node node;
0ab7befa 109 unsigned hit;
83b5d2f5
JH
110 union {
111 struct grep_pat *atom;
112 struct grep_expr *unary;
113 struct {
114 struct grep_expr *left;
115 struct grep_expr *right;
116 } binary;
117 } u;
118};
119
120struct grep_opt {
121 struct grep_pat *pattern_list;
122 struct grep_pat **pattern_tail;
80235ba7
JH
123 struct grep_pat *header_list;
124 struct grep_pat **header_tail;
83b5d2f5 125 struct grep_expr *pattern_expression;
0693806b
JT
126
127 /*
128 * NEEDSWORK: See if we can remove this field, because the repository
129 * should probably be per-source. That is, grep.c functions using this
130 * field should probably start using "repo" in "struct grep_source"
131 * instead.
132 *
133 * This is potentially the cause of at least one bug - "git grep"
45bde58e
MT
134 * using the textconv attributes from the superproject on the
135 * submodules. See the failing "git grep --textconv" tests in
136 * t7814-grep-recurse-submodules.sh for more information.
0693806b 137 */
38bbc2ea 138 struct repository *repo;
0693806b 139
3e230fa1 140 int linenum;
017c0fcf 141 int columnnum;
3e230fa1 142 int invert;
5183bf67 143 int ignore_case;
3e230fa1
RS
144 int status_only;
145 int name_only;
146 int unmatch_name_only;
147 int count;
148 int word_regexp;
3e230fa1 149 int all_match;
794c0002
RS
150 int no_body_match;
151 int body_hit;
83b5d2f5
JH
152#define GREP_BINARY_DEFAULT 0
153#define GREP_BINARY_NOMATCH 1
154#define GREP_BINARY_TEXT 2
3e230fa1 155 int binary;
335ec3bf 156 int allow_textconv;
baa6378f 157 int use_reflog_filter;
3e230fa1
RS
158 int relative;
159 int pathname;
160 int null_following_name;
9d8db06e 161 int only_matching;
7e8f59d5 162 int color;
a91f453f 163 int max_depth;
2944e4e6 164 int funcname;
ba8ea749 165 int funcbody;
84befcd0 166 int extended_regexp_option;
321ee436 167 enum grep_pattern_type pattern_type_option;
44570188 168 int ignore_locale;
fa151dc5 169 char colors[NR_GREP_COLORS][COLOR_MAXLEN];
83b5d2f5
JH
170 unsigned pre_context;
171 unsigned post_context;
5dd06d38 172 unsigned last_shown;
046802d0 173 int show_hunk_mark;
a8f0e764 174 int file_break;
1d84f72e 175 int heading;
68437ede 176 int max_count;
60ecac98 177 void *priv;
5b594f45
FK
178
179 void (*output)(struct grep_opt *opt, const void *data, size_t size);
180 void *output_priv;
83b5d2f5
JH
181};
182
72365bb4
ÆAB
183#define GREP_OPT_INIT { \
184 .relative = 1, \
185 .pathname = 1, \
186 .max_depth = -1, \
68437ede 187 .max_count = -1, \
72365bb4
ÆAB
188 .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, \
189 .colors = { \
190 [GREP_COLOR_CONTEXT] = "", \
191 [GREP_COLOR_FILENAME] = GIT_COLOR_MAGENTA, \
192 [GREP_COLOR_FUNCTION] = "", \
193 [GREP_COLOR_LINENO] = GIT_COLOR_GREEN, \
194 [GREP_COLOR_COLUMNNO] = GIT_COLOR_GREEN, \
195 [GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED, \
196 [GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED, \
197 [GREP_COLOR_SELECTED] = "", \
198 [GREP_COLOR_SEP] = GIT_COLOR_CYAN, \
199 }, \
200 .only_matching = 0, \
201 .color = -1, \
202 .output = std_output, \
203}
204
a4e7e317
GC
205struct config_context;
206int grep_config(const char *var, const char *value,
207 const struct config_context *ctx, void *data);
9725c8dd 208void grep_init(struct grep_opt *, struct repository *repo);
7687a054 209
55454427
DL
210void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
211void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
212void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
213void compile_grep_patterns(struct grep_opt *opt);
214void free_grep_patterns(struct grep_opt *opt);
1e668716 215int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size);
83b5d2f5 216
3f566c4e
HM
217/* The field parameter is only used to filter header patterns
218 * (where appropriate). If filtering isn't desirable
219 * GREP_HEADER_FIELD_MAX should be supplied.
220 */
221int grep_next_match(struct grep_opt *opt,
222 const char *bol, const char *eol,
223 enum grep_context ctx, regmatch_t *pmatch,
224 enum grep_header_field field, int eflags);
225
e1327023
JK
226struct grep_source {
227 char *name;
228
229 enum grep_source_type {
1c41c82b 230 GREP_SOURCE_OID,
e1327023
JK
231 GREP_SOURCE_FILE,
232 GREP_SOURCE_BUF,
233 } type;
234 void *identifier;
0693806b 235 struct repository *repo; /* if GREP_SOURCE_OID */
e1327023 236
1e668716 237 const char *buf;
e1327023 238 unsigned long size;
94ad9d9e 239
55c61688 240 char *path; /* for attribute lookups */
94ad9d9e 241 struct userdiff_driver *driver;
e1327023
JK
242};
243
50d92b5f
JT
244void grep_source_init_file(struct grep_source *gs, const char *name,
245 const char *path);
246void grep_source_init_oid(struct grep_source *gs, const char *name,
0693806b
JT
247 const char *path, const struct object_id *oid,
248 struct repository *repo);
e1327023
JK
249void grep_source_clear_data(struct grep_source *gs);
250void grep_source_clear(struct grep_source *gs);
acd00ea0
NTND
251void grep_source_load_driver(struct grep_source *gs,
252 struct index_state *istate);
07a7d656 253
e1327023
JK
254
255int grep_source(struct grep_opt *opt, struct grep_source *gs);
256
55454427 257struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
5b594f45 258
0579f91d
TR
259/*
260 * Mutex used around access to the attributes machinery if
261 * opt->use_threads. Must be initialized/destroyed by callers!
262 */
78db6ea9 263extern int grep_use_locks;
0579f91d 264extern pthread_mutex_t grep_attr_mutex;
b3aeb285 265
83b5d2f5 266#endif