blob: 926c0875c42f63f961447a30f95c39d7ae031999 [file] [log] [blame]
Junio C Hamano83b5d2f2006-09-17 16:02:52 -07001#ifndef GREP_H
2#define GREP_H
René Scharfe7e8f59d2009-03-07 13:32:32 +01003#include "color.h"
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +00004#ifdef USE_LIBPCRE2
5#define PCRE2_CODE_UNIT_WIDTH 8
6#include <pcre2.h>
Ævar Arnfjörð Bjarmason797c3592021-02-18 01:07:24 +01007#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 36) || PCRE2_MAJOR >= 11
8#define GIT_PCRE2_VERSION_10_36_OR_HIGHER
9#endif
Mathias Krause14b9a042023-03-23 18:25:39 +010010#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 35) || PCRE2_MAJOR >= 11
11#define GIT_PCRE2_VERSION_10_35_OR_HIGHER
12#endif
Ævar Arnfjörð Bjarmasonb76bf272021-02-18 01:07:25 +010013#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 34) || PCRE2_MAJOR >= 11
14#define GIT_PCRE2_VERSION_10_34_OR_HIGHER
15#endif
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +000016#else
17typedef int pcre2_code;
18typedef int pcre2_match_data;
19typedef int pcre2_compile_context;
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 01:07:27 +010020typedef int pcre2_general_context;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +000021#endif
Ævar Arnfjörð Bjarmason95ca1f92021-01-24 18:28:13 +010022#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
Thomas Rast0579f912011-12-12 22:16:07 +010026#include "thread-utils.h"
Jeff King94ad9d92012-02-02 03:20:43 -050027#include "userdiff.h"
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070028
Nguyễn Thái Ngọc Duy38bbc2e2018-09-21 17:57:23 +020029struct repository;
30
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070031enum grep_pat_token {
32 GREP_PATTERN,
Junio C Hamano480c1ca2006-09-20 12:39:46 -070033 GREP_PATTERN_HEAD,
34 GREP_PATTERN_BODY,
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070035 GREP_AND,
36 GREP_OPEN_PAREN,
37 GREP_CLOSE_PAREN,
38 GREP_NOT,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000039 GREP_OR
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070040};
41
Junio C Hamano480c1ca2006-09-20 12:39:46 -070042enum grep_context {
43 GREP_CONTEXT_HEAD,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000044 GREP_CONTEXT_BODY
Junio C Hamano480c1ca2006-09-20 12:39:46 -070045};
46
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -070047enum grep_header_field {
Antoine Pelisse3ce3ffb2013-02-03 14:37:09 +000048 GREP_HEADER_FIELD_MIN = 0,
49 GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
Nguyễn Thái Ngọc Duyad4813b2012-09-29 11:41:27 +070050 GREP_HEADER_COMMITTER,
Nguyễn Thái Ngọc Duy72fd13f2012-09-29 11:41:28 +070051 GREP_HEADER_REFLOG,
Nguyễn Thái Ngọc Duyad4813b2012-09-29 11:41:27 +070052
53 /* Must be at the end of the enum */
54 GREP_HEADER_FIELD_MAX
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -070055};
56
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 15:55:22 +020057enum grep_color {
58 GREP_COLOR_CONTEXT,
59 GREP_COLOR_FILENAME,
60 GREP_COLOR_FUNCTION,
61 GREP_COLOR_LINENO,
Junio C Hamanod036d662018-07-18 12:20:31 -070062 GREP_COLOR_COLUMNNO,
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 15:55:22 +020063 GREP_COLOR_MATCH_CONTEXT,
64 GREP_COLOR_MATCH_SELECTED,
65 GREP_COLOR_SELECTED,
66 GREP_COLOR_SEP,
67 NR_GREP_COLORS
68};
69
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070070struct grep_pat {
71 struct grep_pat *next;
72 const char *origin;
73 int no;
74 enum grep_pat_token token;
René Scharfe526a8582012-05-20 16:33:07 +020075 char *pattern;
René Scharfeed40a092010-05-22 23:43:43 +020076 size_t patternlen;
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -070077 enum grep_header_field field;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070078 regex_t regexp;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +000079 pcre2_code *pcre2_pattern;
80 pcre2_match_data *pcre2_match_data;
81 pcre2_compile_context *pcre2_compile_context;
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 01:07:27 +010082 pcre2_general_context *pcre2_general_context;
Carlo Marcelo Arenas Belón10da0302019-10-16 12:10:24 +000083 const uint8_t *pcre2_tables;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +000084 uint32_t pcre2_jit_on;
René Scharfec8222552009-01-10 00:18:34 +010085 unsigned fixed:1;
Ævar Arnfjörð Bjarmason09872f62019-07-26 17:08:15 +020086 unsigned is_fixed:1;
Brian Collins5183bf62009-11-06 01:22:35 -080087 unsigned ignore_case:1;
René Scharfed7eb5272009-03-07 13:28:40 +010088 unsigned word_regexp:1;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070089};
90
91enum grep_expr_node {
92 GREP_NODE_ATOM,
93 GREP_NODE_NOT,
94 GREP_NODE_AND,
Junio C Hamano5aaeb732010-09-12 22:15:35 -070095 GREP_NODE_TRUE,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000096 GREP_NODE_OR
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070097};
98
J Smith84befcd2012-08-03 10:53:50 -040099enum 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
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700107struct grep_expr {
108 enum grep_expr_node node;
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700109 unsigned hit;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700110 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;
Junio C Hamano80235ba2010-01-17 20:09:06 -0800123 struct grep_pat *header_list;
124 struct grep_pat **header_tail;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700125 struct grep_expr *pattern_expression;
Jonathan Tan06938062021-08-16 14:09:56 -0700126
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"
Matheus Tavares45bde582021-09-29 09:24:25 -0300134 * 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.
Jonathan Tan06938062021-08-16 14:09:56 -0700137 */
Nguyễn Thái Ngọc Duy38bbc2e2018-09-21 17:57:23 +0200138 struct repository *repo;
Jonathan Tan06938062021-08-16 14:09:56 -0700139
René Scharfe3e230fa2009-05-07 21:46:48 +0200140 int linenum;
Taylor Blau017c0fc2018-06-22 10:49:39 -0500141 int columnnum;
René Scharfe3e230fa2009-05-07 21:46:48 +0200142 int invert;
Brian Collins5183bf62009-11-06 01:22:35 -0800143 int ignore_case;
René Scharfe3e230fa2009-05-07 21:46:48 +0200144 int status_only;
145 int name_only;
146 int unmatch_name_only;
147 int count;
148 int word_regexp;
René Scharfe3e230fa2009-05-07 21:46:48 +0200149 int all_match;
René Scharfe794c0002021-12-17 17:48:49 +0100150 int no_body_match;
151 int body_hit;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700152#define GREP_BINARY_DEFAULT 0
153#define GREP_BINARY_NOMATCH 1
154#define GREP_BINARY_TEXT 2
René Scharfe3e230fa2009-05-07 21:46:48 +0200155 int binary;
Jeff King335ec3b2013-05-10 17:10:15 +0200156 int allow_textconv;
Junio C Hamanobaa63782012-09-29 11:59:52 -0700157 int use_reflog_filter;
René Scharfe3e230fa2009-05-07 21:46:48 +0200158 int relative;
159 int pathname;
160 int null_following_name;
Taylor Blau9d8db062018-07-09 15:33:47 -0500161 int only_matching;
René Scharfe7e8f59d2009-03-07 13:32:32 +0100162 int color;
Michał Kiedrowicza91f4532009-07-22 19:52:15 +0200163 int max_depth;
René Scharfe2944e4e2009-07-02 00:06:34 +0200164 int funcname;
René Scharfeba8ea742011-08-01 19:20:53 +0200165 int funcbody;
J Smith84befcd2012-08-03 10:53:50 -0400166 int extended_regexp_option;
Ævar Arnfjörð Bjarmason321ee432022-02-16 01:00:37 +0100167 enum grep_pattern_type pattern_type_option;
Ævar Arnfjörð Bjarmason44570182019-06-28 01:39:05 +0200168 int ignore_locale;
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 15:55:22 +0200169 char colors[NR_GREP_COLORS][COLOR_MAXLEN];
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700170 unsigned pre_context;
171 unsigned post_context;
René Scharfe5dd06d32009-07-02 00:02:38 +0200172 unsigned last_shown;
René Scharfe046802d2009-07-02 00:03:44 +0200173 int show_hunk_mark;
René Scharfea8f0e762011-06-05 17:24:25 +0200174 int file_break;
René Scharfe1d84f722011-06-05 17:24:36 +0200175 int heading;
Carlos López68437ed2022-06-22 19:47:32 +0000176 int max_count;
René Scharfe60ecac92009-07-02 00:07:24 +0200177 void *priv;
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +0100178
179 void (*output)(struct grep_opt *opt, const void *data, size_t size);
180 void *output_priv;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700181};
182
Ævar Arnfjörð Bjarmason72365bb2022-02-16 01:00:36 +0100183#define GREP_OPT_INIT { \
184 .relative = 1, \
185 .pathname = 1, \
186 .max_depth = -1, \
Carlos López68437ed2022-06-22 19:47:32 +0000187 .max_count = -1, \
Ævar Arnfjörð Bjarmason72365bb2022-02-16 01:00:36 +0100188 .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
Glen Chooa4e7e312023-06-28 19:26:22 +0000205struct config_context;
206int grep_config(const char *var, const char *value,
207 const struct config_context *ctx, void *data);
Ævar Arnfjörð Bjarmason9725c8d2022-02-16 01:00:34 +0100208void grep_init(struct grep_opt *, struct repository *repo);
Junio C Hamano7687a052012-10-09 16:17:50 -0700209
Denton Liu55454422019-04-29 04:28:14 -0400210void 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);
Jeff King1e668712021-09-20 23:51:28 -0400215int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700216
Hamza Mahfooz3f566c42021-09-29 07:57:15 -0400217/* 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
Jeff Kinge1327022012-02-02 03:19:28 -0500226struct grep_source {
227 char *name;
228
229 enum grep_source_type {
Brandon Williams1c41c822017-05-30 10:30:44 -0700230 GREP_SOURCE_OID,
Jeff Kinge1327022012-02-02 03:19:28 -0500231 GREP_SOURCE_FILE,
232 GREP_SOURCE_BUF,
233 } type;
234 void *identifier;
Jonathan Tan06938062021-08-16 14:09:56 -0700235 struct repository *repo; /* if GREP_SOURCE_OID */
Jeff Kinge1327022012-02-02 03:19:28 -0500236
Jeff King1e668712021-09-20 23:51:28 -0400237 const char *buf;
Jeff Kinge1327022012-02-02 03:19:28 -0500238 unsigned long size;
Jeff King94ad9d92012-02-02 03:20:43 -0500239
Nguyễn Thái Ngọc Duy55c61682012-10-12 17:49:38 +0700240 char *path; /* for attribute lookups */
Jeff King94ad9d92012-02-02 03:20:43 -0500241 struct userdiff_driver *driver;
Jeff Kinge1327022012-02-02 03:19:28 -0500242};
243
Jonathan Tan50d92b52021-08-16 14:09:53 -0700244void 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,
Jonathan Tan06938062021-08-16 14:09:56 -0700247 const char *path, const struct object_id *oid,
248 struct repository *repo);
Jeff Kinge1327022012-02-02 03:19:28 -0500249void grep_source_clear_data(struct grep_source *gs);
250void grep_source_clear(struct grep_source *gs);
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200251void grep_source_load_driver(struct grep_source *gs,
252 struct index_state *istate);
Junio C Hamano07a7d652012-09-15 14:04:36 -0700253
Jeff Kinge1327022012-02-02 03:19:28 -0500254
255int grep_source(struct grep_opt *opt, struct grep_source *gs);
256
Denton Liu55454422019-04-29 04:28:14 -0400257struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +0100258
Thomas Rast0579f912011-12-12 22:16:07 +0100259/*
260 * Mutex used around access to the attributes machinery if
261 * opt->use_threads. Must be initialized/destroyed by callers!
262 */
Jeff King78db6ea2012-02-02 03:18:29 -0500263extern int grep_use_locks;
Thomas Rast0579f912011-12-12 22:16:07 +0100264extern pthread_mutex_t grep_attr_mutex;
Jeff Kingb3aeb282012-02-02 03:18:41 -0500265
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700266#endif