blob: 3cb8a83ae8ca584bc1b3f7a5d0e9dd6b92381777 [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"
131 * ignoring the textconv attributes from submodules. See [1] for more
132 * information.
133 * [1] https://lore.kernel.org/git/CAHd-oW5iEQarYVxEXoTG-ua2zdoybTrSjCBKtO0YT292fm0NQQ@mail.gmail.com/
134 */
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;
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700151#define GREP_BINARY_DEFAULT 0
152#define GREP_BINARY_NOMATCH 1
153#define GREP_BINARY_TEXT 2
René Scharfe3e230fa2009-05-07 21:46:48 +0200154 int binary;
Jeff King335ec3b2013-05-10 17:10:15 +0200155 int allow_textconv;
René Scharfe3e230fa2009-05-07 21:46:48 +0200156 int extended;
Junio C Hamanobaa63782012-09-29 11:59:52 -0700157 int use_reflog_filter;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56 +0000158 int pcre2;
René Scharfe3e230fa2009-05-07 21:46:48 +0200159 int relative;
160 int pathname;
161 int null_following_name;
Taylor Blau9d8db062018-07-09 15:33:47 -0500162 int only_matching;
René Scharfe7e8f59d2009-03-07 13:32:32 +0100163 int color;
Michał Kiedrowicza91f4532009-07-22 19:52:15 +0200164 int max_depth;
René Scharfe2944e4e2009-07-02 00:06:34 +0200165 int funcname;
René Scharfeba8ea742011-08-01 19:20:53 +0200166 int funcbody;
J Smith84befcd2012-08-03 10:53:50 -0400167 int extended_regexp_option;
168 int pattern_type_option;
Ævar Arnfjörð Bjarmason44570182019-06-28 01:39:05 +0200169 int ignore_locale;
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 15:55:22 +0200170 char colors[NR_GREP_COLORS][COLOR_MAXLEN];
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700171 unsigned pre_context;
172 unsigned post_context;
René Scharfe5dd06d32009-07-02 00:02:38 +0200173 unsigned last_shown;
René Scharfe046802d2009-07-02 00:03:44 +0200174 int show_hunk_mark;
René Scharfea8f0e762011-06-05 17:24:25 +0200175 int file_break;
René Scharfe1d84f722011-06-05 17:24:36 +0200176 int heading;
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
Denton Liu55454422019-04-29 04:28:14 -0400183int grep_config(const char *var, const char *value, void *);
184void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
Junio C Hamanoc5c31d32012-10-03 14:47:48 -0700185void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
Junio C Hamano7687a052012-10-09 16:17:50 -0700186
Denton Liu55454422019-04-29 04:28:14 -0400187void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
188void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
189void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
190void compile_grep_patterns(struct grep_opt *opt);
191void free_grep_patterns(struct grep_opt *opt);
Jeff King1e668712021-09-20 23:51:28 -0400192int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size);
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700193
Jeff Kinge1327022012-02-02 03:19:28 -0500194struct grep_source {
195 char *name;
196
197 enum grep_source_type {
Brandon Williams1c41c822017-05-30 10:30:44 -0700198 GREP_SOURCE_OID,
Jeff Kinge1327022012-02-02 03:19:28 -0500199 GREP_SOURCE_FILE,
200 GREP_SOURCE_BUF,
201 } type;
202 void *identifier;
Jonathan Tan06938062021-08-16 14:09:56 -0700203 struct repository *repo; /* if GREP_SOURCE_OID */
Jeff Kinge1327022012-02-02 03:19:28 -0500204
Jeff King1e668712021-09-20 23:51:28 -0400205 const char *buf;
Jeff Kinge1327022012-02-02 03:19:28 -0500206 unsigned long size;
Jeff King94ad9d92012-02-02 03:20:43 -0500207
Nguyễn Thái Ngọc Duy55c61682012-10-12 17:49:38 +0700208 char *path; /* for attribute lookups */
Jeff King94ad9d92012-02-02 03:20:43 -0500209 struct userdiff_driver *driver;
Jeff Kinge1327022012-02-02 03:19:28 -0500210};
211
Jonathan Tan50d92b52021-08-16 14:09:53 -0700212void grep_source_init_file(struct grep_source *gs, const char *name,
213 const char *path);
214void grep_source_init_oid(struct grep_source *gs, const char *name,
Jonathan Tan06938062021-08-16 14:09:56 -0700215 const char *path, const struct object_id *oid,
216 struct repository *repo);
Jeff Kinge1327022012-02-02 03:19:28 -0500217void grep_source_clear_data(struct grep_source *gs);
218void grep_source_clear(struct grep_source *gs);
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200219void grep_source_load_driver(struct grep_source *gs,
220 struct index_state *istate);
Junio C Hamano07a7d652012-09-15 14:04:36 -0700221
Jeff Kinge1327022012-02-02 03:19:28 -0500222
223int grep_source(struct grep_opt *opt, struct grep_source *gs);
224
Denton Liu55454422019-04-29 04:28:14 -0400225struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
226int grep_threads_ok(const struct grep_opt *opt);
Fredrik Kuivinen5b594f42010-01-25 23:51:39 +0100227
Thomas Rast0579f912011-12-12 22:16:07 +0100228/*
229 * Mutex used around access to the attributes machinery if
230 * opt->use_threads. Must be initialized/destroyed by callers!
231 */
Jeff King78db6ea2012-02-02 03:18:29 -0500232extern int grep_use_locks;
Thomas Rast0579f912011-12-12 22:16:07 +0100233extern pthread_mutex_t grep_attr_mutex;
Jeff Kingb3aeb282012-02-02 03:18:41 -0500234
Junio C Hamano83b5d2f2006-09-17 16:02:52 -0700235#endif