blob: 6a1f0ab01729b8a11eb873a93829f7ee57d07dd4 [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
Ævar Arnfjörð Bjarmasonb76bf272021-02-18 01:07:25 +010010#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 34) || PCRE2_MAJOR >= 11
11#define GIT_PCRE2_VERSION_10_34_OR_HIGHER
12#endif
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +000013#else
14typedef int pcre2_code;
15typedef int pcre2_match_data;
16typedef int pcre2_compile_context;
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 01:07:27 +010017typedef int pcre2_general_context;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +000018#endif
Ævar Arnfjörð Bjarmason95ca1f92021-01-24 18:28:13 +010019#ifndef PCRE2_MATCH_INVALID_UTF
20/* PCRE2_MATCH_* dummy also with !USE_LIBPCRE2, for test-pcre2-config.c */
21#define PCRE2_MATCH_INVALID_UTF 0
22#endif
Thomas Rast0579f912011-12-12 22:16:07 +010023#include "thread-utils.h"
Jeff King94ad9d92012-02-02 03:20:43 -050024#include "userdiff.h"
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070025
Nguyễn Thái Ngọc Duy38bbc2e2018-09-21 17:57:23 +020026struct repository;
27
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070028enum grep_pat_token {
29 GREP_PATTERN,
Junio C Hamano480c1ca2006-09-20 12:39:46 -070030 GREP_PATTERN_HEAD,
31 GREP_PATTERN_BODY,
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070032 GREP_AND,
33 GREP_OPEN_PAREN,
34 GREP_CLOSE_PAREN,
35 GREP_NOT,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000036 GREP_OR
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070037};
38
Junio C Hamano480c1ca2006-09-20 12:39:46 -070039enum grep_context {
40 GREP_CONTEXT_HEAD,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000041 GREP_CONTEXT_BODY
Junio C Hamano480c1ca2006-09-20 12:39:46 -070042};
43
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -070044enum grep_header_field {
Antoine Pelisse3ce3ffb2013-02-03 14:37:09 +000045 GREP_HEADER_FIELD_MIN = 0,
46 GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
Nguyễn Thái Ngọc Duyad4813b2012-09-29 11:41:27 +070047 GREP_HEADER_COMMITTER,
Nguyễn Thái Ngọc Duy72fd13f2012-09-29 11:41:28 +070048 GREP_HEADER_REFLOG,
Nguyễn Thái Ngọc Duyad4813b2012-09-29 11:41:27 +070049
50 /* Must be at the end of the enum */
51 GREP_HEADER_FIELD_MAX
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -070052};
53
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 15:55:22 +020054enum grep_color {
55 GREP_COLOR_CONTEXT,
56 GREP_COLOR_FILENAME,
57 GREP_COLOR_FUNCTION,
58 GREP_COLOR_LINENO,
Junio C Hamanod036d662018-07-18 12:20:31 -070059 GREP_COLOR_COLUMNNO,
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 15:55:22 +020060 GREP_COLOR_MATCH_CONTEXT,
61 GREP_COLOR_MATCH_SELECTED,
62 GREP_COLOR_SELECTED,
63 GREP_COLOR_SEP,
64 NR_GREP_COLORS
65};
66
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070067struct grep_pat {
68 struct grep_pat *next;
69 const char *origin;
70 int no;
71 enum grep_pat_token token;
René Scharfe526a8582012-05-20 16:33:07 +020072 char *pattern;
René Scharfeed40a092010-05-22 23:43:43 +020073 size_t patternlen;
Junio C Hamanoa4d7d2c2008-09-04 22:15:02 -070074 enum grep_header_field field;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070075 regex_t regexp;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +000076 pcre2_code *pcre2_pattern;
77 pcre2_match_data *pcre2_match_data;
78 pcre2_compile_context *pcre2_compile_context;
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 01:07:27 +010079 pcre2_general_context *pcre2_general_context;
Carlo Marcelo Arenas Belón10da0302019-10-16 12:10:24 +000080 const uint8_t *pcre2_tables;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +000081 uint32_t pcre2_jit_on;
René Scharfec8222552009-01-10 00:18:34 +010082 unsigned fixed:1;
Ævar Arnfjörð Bjarmason09872f62019-07-26 17:08:15 +020083 unsigned is_fixed:1;
Brian Collins5183bf62009-11-06 01:22:35 -080084 unsigned ignore_case:1;
René Scharfed7eb5272009-03-07 13:28:40 +010085 unsigned word_regexp:1;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070086};
87
88enum grep_expr_node {
89 GREP_NODE_ATOM,
90 GREP_NODE_NOT,
91 GREP_NODE_AND,
Junio C Hamano5aaeb732010-09-12 22:15:35 -070092 GREP_NODE_TRUE,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000093 GREP_NODE_OR
Junio C Hamano83b5d2f2006-09-17 16:02:52 -070094};
95
J Smith84befcd2012-08-03 10:53:50 -040096enum grep_pattern_type {
97 GREP_PATTERN_TYPE_UNSPECIFIED = 0,
98 GREP_PATTERN_TYPE_BRE,
99 GREP_PATTERN_TYPE_ERE,
100 GREP_PATTERN_TYPE_FIXED,
101 GREP_PATTERN_TYPE_PCRE
102};
103
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700104struct grep_expr {
105 enum grep_expr_node node;
Junio C Hamano0ab7bef2006-09-27 17:50:52 -0700106 unsigned hit;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700107 union {
108 struct grep_pat *atom;
109 struct grep_expr *unary;
110 struct {
111 struct grep_expr *left;
112 struct grep_expr *right;
113 } binary;
114 } u;
115};
116
117struct grep_opt {
118 struct grep_pat *pattern_list;
119 struct grep_pat **pattern_tail;
Junio C Hamano80235ba2010-01-17 20:09:06 -0800120 struct grep_pat *header_list;
121 struct grep_pat **header_tail;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700122 struct grep_expr *pattern_expression;
Jonathan Tan06938062021-08-16 14:09:56 -0700123
124 /*
125 * NEEDSWORK: See if we can remove this field, because the repository
126 * should probably be per-source. That is, grep.c functions using this
127 * field should probably start using "repo" in "struct grep_source"
128 * instead.
129 *
130 * This is potentially the cause of at least one bug - "git grep"
Matheus Tavares45bde582021-09-29 09:24:25 -0300131 * using the textconv attributes from the superproject on the
132 * submodules. See the failing "git grep --textconv" tests in
133 * t7814-grep-recurse-submodules.sh for more information.
Jonathan Tan06938062021-08-16 14:09:56 -0700134 */
Nguyễn Thái Ngọc Duy38bbc2e2018-09-21 17:57:23 +0200135 struct repository *repo;
Jonathan Tan06938062021-08-16 14:09:56 -0700136
Clemens Buchacher493b7a02009-09-05 14:31:17 +0200137 const char *prefix;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700138 int prefix_length;
139 regex_t regexp;
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;
149 int fixed;
150 int all_match;
René Scharfe794c0002021-12-17 17:48:49 +0100151 int no_body_match;
152 int body_hit;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700153#define GREP_BINARY_DEFAULT 0
154#define GREP_BINARY_NOMATCH 1
155#define GREP_BINARY_TEXT 2
René Scharfe3e230fa2009-05-07 21:46:48 +0200156 int binary;
Jeff King335ec3b2013-05-10 17:10:15 +0200157 int allow_textconv;
René Scharfe3e230fa2009-05-07 21:46:48 +0200158 int extended;
Junio C Hamanobaa63782012-09-29 11:59:52 -0700159 int use_reflog_filter;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +0000160 int pcre2;
René Scharfe3e230fa2009-05-07 21:46:48 +0200161 int relative;
162 int pathname;
163 int null_following_name;
Taylor Blau9d8db062018-07-09 15:33:47 -0500164 int only_matching;
René Scharfe7e8f59d2009-03-07 13:32:32 +0100165 int color;
Michał Kiedrowicza91f4532009-07-22 19:52:15 +0200166 int max_depth;
René Scharfe2944e4e2009-07-02 00:06:34 +0200167 int funcname;
René Scharfeba8ea742011-08-01 19:20:53 +0200168 int funcbody;
J Smith84befcd2012-08-03 10:53:50 -0400169 int extended_regexp_option;
170 int pattern_type_option;
Ævar Arnfjörð Bjarmason44570182019-06-28 01:39:05 +0200171 int ignore_locale;
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 15:55:22 +0200172 char colors[NR_GREP_COLORS][COLOR_MAXLEN];
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700173 unsigned pre_context;
174 unsigned post_context;
René Scharfe5dd06d32009-07-02 00:02:38 +0200175 unsigned last_shown;
René Scharfe046802d2009-07-02 00:03:44 +0200176 int show_hunk_mark;
René Scharfea8f0e762011-06-05 17:24:25 +0200177 int file_break;
René Scharfe1d84f722011-06-05 17:24:36 +0200178 int heading;
René Scharfe60ecac92009-07-02 00:07:24 +0200179 void *priv;
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +0100180
181 void (*output)(struct grep_opt *opt, const void *data, size_t size);
182 void *output_priv;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700183};
184
Denton Liu55454422019-04-29 04:28:14 -0400185int grep_config(const char *var, const char *value, void *);
186void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
Junio C Hamanoc5c31d32012-10-03 14:47:48 -0700187void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
Junio C Hamano7687a052012-10-09 16:17:50 -0700188
Denton Liu55454422019-04-29 04:28:14 -0400189void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
190void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
191void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
192void compile_grep_patterns(struct grep_opt *opt);
193void free_grep_patterns(struct grep_opt *opt);
Jeff King1e668712021-09-20 23:51:28 -0400194int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700195
Hamza Mahfooz3f566c42021-09-29 07:57:15 -0400196/* The field parameter is only used to filter header patterns
197 * (where appropriate). If filtering isn't desirable
198 * GREP_HEADER_FIELD_MAX should be supplied.
199 */
200int grep_next_match(struct grep_opt *opt,
201 const char *bol, const char *eol,
202 enum grep_context ctx, regmatch_t *pmatch,
203 enum grep_header_field field, int eflags);
204
Jeff Kinge1327022012-02-02 03:19:28 -0500205struct grep_source {
206 char *name;
207
208 enum grep_source_type {
Brandon Williams1c41c822017-05-30 10:30:44 -0700209 GREP_SOURCE_OID,
Jeff Kinge1327022012-02-02 03:19:28 -0500210 GREP_SOURCE_FILE,
211 GREP_SOURCE_BUF,
212 } type;
213 void *identifier;
Jonathan Tan06938062021-08-16 14:09:56 -0700214 struct repository *repo; /* if GREP_SOURCE_OID */
Jeff Kinge1327022012-02-02 03:19:28 -0500215
Jeff King1e668712021-09-20 23:51:28 -0400216 const char *buf;
Jeff Kinge1327022012-02-02 03:19:28 -0500217 unsigned long size;
Jeff King94ad9d92012-02-02 03:20:43 -0500218
Nguyễn Thái Ngọc Duy55c61682012-10-12 17:49:38 +0700219 char *path; /* for attribute lookups */
Jeff King94ad9d92012-02-02 03:20:43 -0500220 struct userdiff_driver *driver;
Jeff Kinge1327022012-02-02 03:19:28 -0500221};
222
Jonathan Tan50d92b52021-08-16 14:09:53 -0700223void grep_source_init_file(struct grep_source *gs, const char *name,
224 const char *path);
225void grep_source_init_oid(struct grep_source *gs, const char *name,
Jonathan Tan06938062021-08-16 14:09:56 -0700226 const char *path, const struct object_id *oid,
227 struct repository *repo);
Jeff Kinge1327022012-02-02 03:19:28 -0500228void grep_source_clear_data(struct grep_source *gs);
229void grep_source_clear(struct grep_source *gs);
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200230void grep_source_load_driver(struct grep_source *gs,
231 struct index_state *istate);
Junio C Hamano07a7d652012-09-15 14:04:36 -0700232
Jeff Kinge1327022012-02-02 03:19:28 -0500233
234int grep_source(struct grep_opt *opt, struct grep_source *gs);
235
Denton Liu55454422019-04-29 04:28:14 -0400236struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +0100237
Thomas Rast0579f912011-12-12 22:16:07 +0100238/*
239 * Mutex used around access to the attributes machinery if
240 * opt->use_threads. Must be initialized/destroyed by callers!
241 */
Jeff King78db6ea2012-02-02 03:18:29 -0500242extern int grep_use_locks;
Thomas Rast0579f912011-12-12 22:16:07 +0100243extern pthread_mutex_t grep_attr_mutex;
Jeff Kingb3aeb282012-02-02 03:18:41 -0500244
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700245#endif