Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1 | #ifndef GREP_H |
| 2 | #define GREP_H |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 3 | #include "color.h" |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 4 | #ifdef USE_LIBPCRE2 |
| 5 | #define PCRE2_CODE_UNIT_WIDTH 8 |
| 6 | #include <pcre2.h> |
Ævar Arnfjörð Bjarmason | 797c359 | 2021-02-18 01:07:24 +0100 | [diff] [blame] | 7 | #if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 36) || PCRE2_MAJOR >= 11 |
| 8 | #define GIT_PCRE2_VERSION_10_36_OR_HIGHER |
| 9 | #endif |
Ævar Arnfjörð Bjarmason | b76bf27 | 2021-02-18 01:07:25 +0100 | [diff] [blame] | 10 | #if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 34) || PCRE2_MAJOR >= 11 |
| 11 | #define GIT_PCRE2_VERSION_10_34_OR_HIGHER |
| 12 | #endif |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 13 | #else |
| 14 | typedef int pcre2_code; |
| 15 | typedef int pcre2_match_data; |
| 16 | typedef int pcre2_compile_context; |
Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 01:07:27 +0100 | [diff] [blame] | 17 | typedef int pcre2_general_context; |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 18 | #endif |
Ævar Arnfjörð Bjarmason | 95ca1f9 | 2021-01-24 18:28:13 +0100 | [diff] [blame] | 19 | #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 Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 23 | #include "thread-utils.h" |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 24 | #include "userdiff.h" |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 25 | |
Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 17:57:23 +0200 | [diff] [blame] | 26 | struct repository; |
| 27 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 28 | enum grep_pat_token { |
| 29 | GREP_PATTERN, |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 30 | GREP_PATTERN_HEAD, |
| 31 | GREP_PATTERN_BODY, |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 32 | GREP_AND, |
| 33 | GREP_OPEN_PAREN, |
| 34 | GREP_CLOSE_PAREN, |
| 35 | GREP_NOT, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 36 | GREP_OR |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 39 | enum grep_context { |
| 40 | GREP_CONTEXT_HEAD, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 41 | GREP_CONTEXT_BODY |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 44 | enum grep_header_field { |
Antoine Pelisse | 3ce3ffb | 2013-02-03 14:37:09 +0000 | [diff] [blame] | 45 | GREP_HEADER_FIELD_MIN = 0, |
| 46 | GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN, |
Nguyễn Thái Ngọc Duy | ad4813b | 2012-09-29 11:41:27 +0700 | [diff] [blame] | 47 | GREP_HEADER_COMMITTER, |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 48 | GREP_HEADER_REFLOG, |
Nguyễn Thái Ngọc Duy | ad4813b | 2012-09-29 11:41:27 +0700 | [diff] [blame] | 49 | |
| 50 | /* Must be at the end of the enum */ |
| 51 | GREP_HEADER_FIELD_MAX |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 54 | enum grep_color { |
| 55 | GREP_COLOR_CONTEXT, |
| 56 | GREP_COLOR_FILENAME, |
| 57 | GREP_COLOR_FUNCTION, |
| 58 | GREP_COLOR_LINENO, |
Junio C Hamano | d036d66 | 2018-07-18 12:20:31 -0700 | [diff] [blame] | 59 | GREP_COLOR_COLUMNNO, |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 60 | 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 Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 67 | struct grep_pat { |
| 68 | struct grep_pat *next; |
| 69 | const char *origin; |
| 70 | int no; |
| 71 | enum grep_pat_token token; |
René Scharfe | 526a858 | 2012-05-20 16:33:07 +0200 | [diff] [blame] | 72 | char *pattern; |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 73 | size_t patternlen; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 74 | enum grep_header_field field; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 75 | regex_t regexp; |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 76 | pcre2_code *pcre2_pattern; |
| 77 | pcre2_match_data *pcre2_match_data; |
| 78 | pcre2_compile_context *pcre2_compile_context; |
Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 01:07:27 +0100 | [diff] [blame] | 79 | pcre2_general_context *pcre2_general_context; |
Carlo Marcelo Arenas Belón | 10da030 | 2019-10-16 12:10:24 +0000 | [diff] [blame] | 80 | const uint8_t *pcre2_tables; |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 81 | uint32_t pcre2_jit_on; |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 82 | unsigned fixed:1; |
Ævar Arnfjörð Bjarmason | 09872f6 | 2019-07-26 17:08:15 +0200 | [diff] [blame] | 83 | unsigned is_fixed:1; |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 84 | unsigned ignore_case:1; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 85 | unsigned word_regexp:1; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | enum grep_expr_node { |
| 89 | GREP_NODE_ATOM, |
| 90 | GREP_NODE_NOT, |
| 91 | GREP_NODE_AND, |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 92 | GREP_NODE_TRUE, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 93 | GREP_NODE_OR |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
J Smith | 84befcd | 2012-08-03 10:53:50 -0400 | [diff] [blame] | 96 | enum 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 Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 104 | struct grep_expr { |
| 105 | enum grep_expr_node node; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 106 | unsigned hit; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 107 | 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 | |
| 117 | struct grep_opt { |
| 118 | struct grep_pat *pattern_list; |
| 119 | struct grep_pat **pattern_tail; |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 120 | struct grep_pat *header_list; |
| 121 | struct grep_pat **header_tail; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 122 | struct grep_expr *pattern_expression; |
Jonathan Tan | 0693806 | 2021-08-16 14:09:56 -0700 | [diff] [blame] | 123 | |
| 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 Duy | 38bbc2e | 2018-09-21 17:57:23 +0200 | [diff] [blame] | 135 | struct repository *repo; |
Jonathan Tan | 0693806 | 2021-08-16 14:09:56 -0700 | [diff] [blame] | 136 | |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 137 | const char *prefix; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 138 | int prefix_length; |
| 139 | regex_t regexp; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 140 | int linenum; |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 141 | int columnnum; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 142 | int invert; |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 143 | int ignore_case; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 144 | 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 Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 151 | #define GREP_BINARY_DEFAULT 0 |
| 152 | #define GREP_BINARY_NOMATCH 1 |
| 153 | #define GREP_BINARY_TEXT 2 |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 154 | int binary; |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 155 | int allow_textconv; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 156 | int extended; |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 157 | int use_reflog_filter; |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 158 | int pcre2; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 159 | int relative; |
| 160 | int pathname; |
| 161 | int null_following_name; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 162 | int only_matching; |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 163 | int color; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 164 | int max_depth; |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 165 | int funcname; |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 166 | int funcbody; |
J Smith | 84befcd | 2012-08-03 10:53:50 -0400 | [diff] [blame] | 167 | int extended_regexp_option; |
| 168 | int pattern_type_option; |
Ævar Arnfjörð Bjarmason | 4457018 | 2019-06-28 01:39:05 +0200 | [diff] [blame] | 169 | int ignore_locale; |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 170 | char colors[NR_GREP_COLORS][COLOR_MAXLEN]; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 171 | unsigned pre_context; |
| 172 | unsigned post_context; |
René Scharfe | 5dd06d3 | 2009-07-02 00:02:38 +0200 | [diff] [blame] | 173 | unsigned last_shown; |
René Scharfe | 046802d | 2009-07-02 00:03:44 +0200 | [diff] [blame] | 174 | int show_hunk_mark; |
René Scharfe | a8f0e76 | 2011-06-05 17:24:25 +0200 | [diff] [blame] | 175 | int file_break; |
René Scharfe | 1d84f72 | 2011-06-05 17:24:36 +0200 | [diff] [blame] | 176 | int heading; |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 177 | void *priv; |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 178 | |
| 179 | void (*output)(struct grep_opt *opt, const void *data, size_t size); |
| 180 | void *output_priv; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 183 | int grep_config(const char *var, const char *value, void *); |
| 184 | void grep_init(struct grep_opt *, struct repository *repo, const char *prefix); |
Junio C Hamano | c5c31d3 | 2012-10-03 14:47:48 -0700 | [diff] [blame] | 185 | void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt); |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 186 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 187 | void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t); |
| 188 | void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t); |
| 189 | void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *); |
| 190 | void compile_grep_patterns(struct grep_opt *opt); |
| 191 | void free_grep_patterns(struct grep_opt *opt); |
Jeff King | 1e66871 | 2021-09-20 23:51:28 -0400 | [diff] [blame] | 192 | int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 193 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 194 | struct grep_source { |
| 195 | char *name; |
| 196 | |
| 197 | enum grep_source_type { |
Brandon Williams | 1c41c82 | 2017-05-30 10:30:44 -0700 | [diff] [blame] | 198 | GREP_SOURCE_OID, |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 199 | GREP_SOURCE_FILE, |
| 200 | GREP_SOURCE_BUF, |
| 201 | } type; |
| 202 | void *identifier; |
Jonathan Tan | 0693806 | 2021-08-16 14:09:56 -0700 | [diff] [blame] | 203 | struct repository *repo; /* if GREP_SOURCE_OID */ |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 204 | |
Jeff King | 1e66871 | 2021-09-20 23:51:28 -0400 | [diff] [blame] | 205 | const char *buf; |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 206 | unsigned long size; |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 207 | |
Nguyễn Thái Ngọc Duy | 55c6168 | 2012-10-12 17:49:38 +0700 | [diff] [blame] | 208 | char *path; /* for attribute lookups */ |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 209 | struct userdiff_driver *driver; |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 210 | }; |
| 211 | |
Jonathan Tan | 50d92b5 | 2021-08-16 14:09:53 -0700 | [diff] [blame] | 212 | void grep_source_init_file(struct grep_source *gs, const char *name, |
| 213 | const char *path); |
| 214 | void grep_source_init_oid(struct grep_source *gs, const char *name, |
Jonathan Tan | 0693806 | 2021-08-16 14:09:56 -0700 | [diff] [blame] | 215 | const char *path, const struct object_id *oid, |
| 216 | struct repository *repo); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 217 | void grep_source_clear_data(struct grep_source *gs); |
| 218 | void grep_source_clear(struct grep_source *gs); |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 219 | void grep_source_load_driver(struct grep_source *gs, |
| 220 | struct index_state *istate); |
Junio C Hamano | 07a7d65 | 2012-09-15 14:04:36 -0700 | [diff] [blame] | 221 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 222 | |
| 223 | int grep_source(struct grep_opt *opt, struct grep_source *gs); |
| 224 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 225 | struct grep_opt *grep_opt_dup(const struct grep_opt *opt); |
| 226 | int grep_threads_ok(const struct grep_opt *opt); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 227 | |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 228 | /* |
| 229 | * Mutex used around access to the attributes machinery if |
| 230 | * opt->use_threads. Must be initialized/destroyed by callers! |
| 231 | */ |
Jeff King | 78db6ea | 2012-02-02 03:18:29 -0500 | [diff] [blame] | 232 | extern int grep_use_locks; |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 233 | extern pthread_mutex_t grep_attr_mutex; |
Jeff King | b3aeb28 | 2012-02-02 03:18:41 -0500 | [diff] [blame] | 234 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 235 | #endif |