| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 1 | #ifndef GREP_H |
| 2 | #define GREP_H |
| René Scharfe | 7e8f59d | 2009-03-07 12:32:32 | [diff] [blame] | 3 | #include "color.h" |
| Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 | [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 00:07:24 | [diff] [blame] | 7 | #if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 36) || PCRE2_MAJOR >= 11 |
| 8 | #define GIT_PCRE2_VERSION_10_36_OR_HIGHER |
| 9 | #endif |
| Mathias Krause | 14b9a04 | 2023-03-23 17:25:39 | [diff] [blame] | 10 | #if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 35) || PCRE2_MAJOR >= 11 |
| 11 | #define GIT_PCRE2_VERSION_10_35_OR_HIGHER |
| 12 | #endif |
| Ævar Arnfjörð Bjarmason | b76bf27 | 2021-02-18 00:07:25 | [diff] [blame] | 13 | #if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 34) || PCRE2_MAJOR >= 11 |
| 14 | #define GIT_PCRE2_VERSION_10_34_OR_HIGHER |
| 15 | #endif |
| Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 | [diff] [blame] | 16 | #else |
| 17 | typedef int pcre2_code; |
| 18 | typedef int pcre2_match_data; |
| 19 | typedef int pcre2_compile_context; |
| Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 00:07:27 | [diff] [blame] | 20 | typedef int pcre2_general_context; |
| Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 | [diff] [blame] | 21 | #endif |
| Ævar Arnfjörð Bjarmason | 95ca1f9 | 2021-01-24 17:28:13 | [diff] [blame] | 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 |
| Thomas Rast | 0579f91 | 2011-12-12 21:16:07 | [diff] [blame] | 26 | #include "thread-utils.h" |
| Jeff King | 94ad9d9 | 2012-02-02 08:20:43 | [diff] [blame] | 27 | #include "userdiff.h" |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 28 | |
| Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 15:57:23 | [diff] [blame] | 29 | struct repository; |
| 30 | |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 31 | enum grep_pat_token { |
| 32 | GREP_PATTERN, |
| Junio C Hamano | 480c1ca | 2006-09-20 19:39:46 | [diff] [blame] | 33 | GREP_PATTERN_HEAD, |
| 34 | GREP_PATTERN_BODY, |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 35 | GREP_AND, |
| 36 | GREP_OPEN_PAREN, |
| 37 | GREP_CLOSE_PAREN, |
| 38 | GREP_NOT, |
| Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 | [diff] [blame] | 39 | GREP_OR |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 40 | }; |
| 41 | |
| Junio C Hamano | 480c1ca | 2006-09-20 19:39:46 | [diff] [blame] | 42 | enum grep_context { |
| 43 | GREP_CONTEXT_HEAD, |
| Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 | [diff] [blame] | 44 | GREP_CONTEXT_BODY |
| Junio C Hamano | 480c1ca | 2006-09-20 19:39:46 | [diff] [blame] | 45 | }; |
| 46 | |
| Junio C Hamano | a4d7d2c | 2008-09-05 05:15:02 | [diff] [blame] | 47 | enum grep_header_field { |
| Antoine Pelisse | 3ce3ffb | 2013-02-03 14:37:09 | [diff] [blame] | 48 | GREP_HEADER_FIELD_MIN = 0, |
| 49 | GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN, |
| Nguyễn Thái Ngọc Duy | ad4813b | 2012-09-29 04:41:27 | [diff] [blame] | 50 | GREP_HEADER_COMMITTER, |
| Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 04:41:28 | [diff] [blame] | 51 | GREP_HEADER_REFLOG, |
| Nguyễn Thái Ngọc Duy | ad4813b | 2012-09-29 04:41:27 | [diff] [blame] | 52 | |
| 53 | /* Must be at the end of the enum */ |
| 54 | GREP_HEADER_FIELD_MAX |
| Junio C Hamano | a4d7d2c | 2008-09-05 05:15:02 | [diff] [blame] | 55 | }; |
| 56 | |
| Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 13:55:22 | [diff] [blame] | 57 | enum grep_color { |
| 58 | GREP_COLOR_CONTEXT, |
| 59 | GREP_COLOR_FILENAME, |
| 60 | GREP_COLOR_FUNCTION, |
| 61 | GREP_COLOR_LINENO, |
| Junio C Hamano | d036d66 | 2018-07-18 19:20:31 | [diff] [blame] | 62 | GREP_COLOR_COLUMNNO, |
| Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 13:55:22 | [diff] [blame] | 63 | 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 Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 70 | struct grep_pat { |
| 71 | struct grep_pat *next; |
| 72 | const char *origin; |
| 73 | int no; |
| 74 | enum grep_pat_token token; |
| René Scharfe | 526a858 | 2012-05-20 14:33:07 | [diff] [blame] | 75 | char *pattern; |
| René Scharfe | ed40a09 | 2010-05-22 21:43:43 | [diff] [blame] | 76 | size_t patternlen; |
| Junio C Hamano | a4d7d2c | 2008-09-05 05:15:02 | [diff] [blame] | 77 | enum grep_header_field field; |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 78 | regex_t regexp; |
| Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 | [diff] [blame] | 79 | pcre2_code *pcre2_pattern; |
| 80 | pcre2_match_data *pcre2_match_data; |
| 81 | pcre2_compile_context *pcre2_compile_context; |
| Ævar Arnfjörð Bjarmason | cbe81e6 | 2021-02-18 00:07:27 | [diff] [blame] | 82 | pcre2_general_context *pcre2_general_context; |
| Carlo Marcelo Arenas Belón | 10da030 | 2019-10-16 12:10:24 | [diff] [blame] | 83 | const uint8_t *pcre2_tables; |
| Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 | [diff] [blame] | 84 | uint32_t pcre2_jit_on; |
| René Scharfe | c822255 | 2009-01-09 23:18:34 | [diff] [blame] | 85 | unsigned fixed:1; |
| Ævar Arnfjörð Bjarmason | 09872f6 | 2019-07-26 15:08:15 | [diff] [blame] | 86 | unsigned is_fixed:1; |
| Brian Collins | 5183bf6 | 2009-11-06 09:22:35 | [diff] [blame] | 87 | unsigned ignore_case:1; |
| René Scharfe | d7eb527 | 2009-03-07 12:28:40 | [diff] [blame] | 88 | unsigned word_regexp:1; |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | enum grep_expr_node { |
| 92 | GREP_NODE_ATOM, |
| 93 | GREP_NODE_NOT, |
| 94 | GREP_NODE_AND, |
| Junio C Hamano | 5aaeb73 | 2010-09-13 05:15:35 | [diff] [blame] | 95 | GREP_NODE_TRUE, |
| Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 | [diff] [blame] | 96 | GREP_NODE_OR |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 97 | }; |
| 98 | |
| J Smith | 84befcd | 2012-08-03 14:53:50 | [diff] [blame] | 99 | enum 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 Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 107 | struct grep_expr { |
| 108 | enum grep_expr_node node; |
| Junio C Hamano | 0ab7bef | 2006-09-28 00:50:52 | [diff] [blame] | 109 | unsigned hit; |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 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 | |
| 120 | struct grep_opt { |
| 121 | struct grep_pat *pattern_list; |
| 122 | struct grep_pat **pattern_tail; |
| Junio C Hamano | 80235ba | 2010-01-18 04:09:06 | [diff] [blame] | 123 | struct grep_pat *header_list; |
| 124 | struct grep_pat **header_tail; |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 125 | struct grep_expr *pattern_expression; |
| Jonathan Tan | 0693806 | 2021-08-16 21:09:56 | [diff] [blame] | 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" |
| Matheus Tavares | 45bde58 | 2021-09-29 12:24:25 | [diff] [blame] | 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. |
| Jonathan Tan | 0693806 | 2021-08-16 21:09:56 | [diff] [blame] | 137 | */ |
| Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 15:57:23 | [diff] [blame] | 138 | struct repository *repo; |
| Jonathan Tan | 0693806 | 2021-08-16 21:09:56 | [diff] [blame] | 139 | |
| René Scharfe | 3e230fa | 2009-05-07 19:46:48 | [diff] [blame] | 140 | int linenum; |
| Taylor Blau | 017c0fc | 2018-06-22 15:49:39 | [diff] [blame] | 141 | int columnnum; |
| René Scharfe | 3e230fa | 2009-05-07 19:46:48 | [diff] [blame] | 142 | int invert; |
| Brian Collins | 5183bf6 | 2009-11-06 09:22:35 | [diff] [blame] | 143 | int ignore_case; |
| René Scharfe | 3e230fa | 2009-05-07 19:46:48 | [diff] [blame] | 144 | int status_only; |
| 145 | int name_only; |
| 146 | int unmatch_name_only; |
| 147 | int count; |
| 148 | int word_regexp; |
| René Scharfe | 3e230fa | 2009-05-07 19:46:48 | [diff] [blame] | 149 | int all_match; |
| René Scharfe | 794c000 | 2021-12-17 16:48:49 | [diff] [blame] | 150 | int no_body_match; |
| 151 | int body_hit; |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 152 | #define GREP_BINARY_DEFAULT 0 |
| 153 | #define GREP_BINARY_NOMATCH 1 |
| 154 | #define GREP_BINARY_TEXT 2 |
| René Scharfe | 3e230fa | 2009-05-07 19:46:48 | [diff] [blame] | 155 | int binary; |
| Jeff King | 335ec3b | 2013-05-10 15:10:15 | [diff] [blame] | 156 | int allow_textconv; |
| Junio C Hamano | baa6378 | 2012-09-29 18:59:52 | [diff] [blame] | 157 | int use_reflog_filter; |
| René Scharfe | 3e230fa | 2009-05-07 19:46:48 | [diff] [blame] | 158 | int relative; |
| 159 | int pathname; |
| 160 | int null_following_name; |
| Taylor Blau | 9d8db06 | 2018-07-09 20:33:47 | [diff] [blame] | 161 | int only_matching; |
| René Scharfe | 7e8f59d | 2009-03-07 12:32:32 | [diff] [blame] | 162 | int color; |
| Michał Kiedrowicz | a91f453 | 2009-07-22 17:52:15 | [diff] [blame] | 163 | int max_depth; |
| René Scharfe | 2944e4e | 2009-07-01 22:06:34 | [diff] [blame] | 164 | int funcname; |
| René Scharfe | ba8ea74 | 2011-08-01 17:20:53 | [diff] [blame] | 165 | int funcbody; |
| J Smith | 84befcd | 2012-08-03 14:53:50 | [diff] [blame] | 166 | int extended_regexp_option; |
| Ævar Arnfjörð Bjarmason | 321ee43 | 2022-02-16 00:00:37 | [diff] [blame] | 167 | enum grep_pattern_type pattern_type_option; |
| Ævar Arnfjörð Bjarmason | 4457018 | 2019-06-27 23:39:05 | [diff] [blame] | 168 | int ignore_locale; |
| Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 13:55:22 | [diff] [blame] | 169 | char colors[NR_GREP_COLORS][COLOR_MAXLEN]; |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 170 | unsigned pre_context; |
| 171 | unsigned post_context; |
| René Scharfe | 5dd06d3 | 2009-07-01 22:02:38 | [diff] [blame] | 172 | unsigned last_shown; |
| René Scharfe | 046802d | 2009-07-01 22:03:44 | [diff] [blame] | 173 | int show_hunk_mark; |
| René Scharfe | a8f0e76 | 2011-06-05 15:24:25 | [diff] [blame] | 174 | int file_break; |
| René Scharfe | 1d84f72 | 2011-06-05 15:24:36 | [diff] [blame] | 175 | int heading; |
| Carlos López | 68437ed | 2022-06-22 19:47:32 | [diff] [blame] | 176 | int max_count; |
| René Scharfe | 60ecac9 | 2009-07-01 22:07:24 | [diff] [blame] | 177 | void *priv; |
| Fredrik Kuivinen | 5b594f4 | 2010-01-25 22:51:39 | [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 23:02:52 | [diff] [blame] | 181 | }; |
| 182 | |
| Ævar Arnfjörð Bjarmason | 72365bb | 2022-02-16 00:00:36 | [diff] [blame] | 183 | #define GREP_OPT_INIT { \ |
| 184 | .relative = 1, \ |
| 185 | .pathname = 1, \ |
| 186 | .max_depth = -1, \ |
| Carlos López | 68437ed | 2022-06-22 19:47:32 | [diff] [blame] | 187 | .max_count = -1, \ |
| Ævar Arnfjörð Bjarmason | 72365bb | 2022-02-16 00:00:36 | [diff] [blame] | 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 | |
| Glen Choo | a4e7e31 | 2023-06-28 19:26:22 | [diff] [blame] | 205 | struct config_context; |
| 206 | int grep_config(const char *var, const char *value, |
| 207 | const struct config_context *ctx, void *data); |
| Ævar Arnfjörð Bjarmason | 9725c8d | 2022-02-16 00:00:34 | [diff] [blame] | 208 | void grep_init(struct grep_opt *, struct repository *repo); |
| Junio C Hamano | 7687a05 | 2012-10-09 23:17:50 | [diff] [blame] | 209 | |
| Denton Liu | 5545442 | 2019-04-29 08:28:14 | [diff] [blame] | 210 | void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t); |
| 211 | void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t); |
| 212 | void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *); |
| 213 | void compile_grep_patterns(struct grep_opt *opt); |
| 214 | void free_grep_patterns(struct grep_opt *opt); |
| Jeff King | 1e66871 | 2021-09-21 03:51:28 | [diff] [blame] | 215 | int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size); |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 216 | |
| Hamza Mahfooz | 3f566c4 | 2021-09-29 11:57:15 | [diff] [blame] | 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 | */ |
| 221 | int 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 King | e132702 | 2012-02-02 08:19:28 | [diff] [blame] | 226 | struct grep_source { |
| 227 | char *name; |
| 228 | |
| 229 | enum grep_source_type { |
| Brandon Williams | 1c41c82 | 2017-05-30 17:30:44 | [diff] [blame] | 230 | GREP_SOURCE_OID, |
| Jeff King | e132702 | 2012-02-02 08:19:28 | [diff] [blame] | 231 | GREP_SOURCE_FILE, |
| 232 | GREP_SOURCE_BUF, |
| Jeff King | e132702 | 2012-02-02 08:19:28 | [diff] [blame] | 233 | } type; |
| 234 | void *identifier; |
| Jonathan Tan | 0693806 | 2021-08-16 21:09:56 | [diff] [blame] | 235 | struct repository *repo; /* if GREP_SOURCE_OID */ |
| Jeff King | e132702 | 2012-02-02 08:19:28 | [diff] [blame] | 236 | |
| Jeff King | 1e66871 | 2021-09-21 03:51:28 | [diff] [blame] | 237 | const char *buf; |
| Jeff King | e132702 | 2012-02-02 08:19:28 | [diff] [blame] | 238 | unsigned long size; |
| Jeff King | 94ad9d9 | 2012-02-02 08:20:43 | [diff] [blame] | 239 | |
| Nguyễn Thái Ngọc Duy | 55c6168 | 2012-10-12 10:49:38 | [diff] [blame] | 240 | char *path; /* for attribute lookups */ |
| Jeff King | 94ad9d9 | 2012-02-02 08:20:43 | [diff] [blame] | 241 | struct userdiff_driver *driver; |
| Jeff King | e132702 | 2012-02-02 08:19:28 | [diff] [blame] | 242 | }; |
| 243 | |
| Jonathan Tan | 50d92b5 | 2021-08-16 21:09:53 | [diff] [blame] | 244 | void grep_source_init_file(struct grep_source *gs, const char *name, |
| 245 | const char *path); |
| 246 | void grep_source_init_oid(struct grep_source *gs, const char *name, |
| Jonathan Tan | 0693806 | 2021-08-16 21:09:56 | [diff] [blame] | 247 | const char *path, const struct object_id *oid, |
| 248 | struct repository *repo); |
| Jeff King | e132702 | 2012-02-02 08:19:28 | [diff] [blame] | 249 | void grep_source_clear_data(struct grep_source *gs); |
| 250 | void grep_source_clear(struct grep_source *gs); |
| Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 15:57:33 | [diff] [blame] | 251 | void grep_source_load_driver(struct grep_source *gs, |
| 252 | struct index_state *istate); |
| Junio C Hamano | 07a7d65 | 2012-09-15 21:04:36 | [diff] [blame] | 253 | |
| Jeff King | e132702 | 2012-02-02 08:19:28 | [diff] [blame] | 254 | |
| 255 | int grep_source(struct grep_opt *opt, struct grep_source *gs); |
| 256 | |
| Denton Liu | 5545442 | 2019-04-29 08:28:14 | [diff] [blame] | 257 | struct grep_opt *grep_opt_dup(const struct grep_opt *opt); |
| Fredrik Kuivinen | 5b594f4 | 2010-01-25 22:51:39 | [diff] [blame] | 258 | |
| Thomas Rast | 0579f91 | 2011-12-12 21:16:07 | [diff] [blame] | 259 | /* |
| 260 | * Mutex used around access to the attributes machinery if |
| 261 | * opt->use_threads. Must be initialized/destroyed by callers! |
| 262 | */ |
| Jeff King | 78db6ea | 2012-02-02 08:18:29 | [diff] [blame] | 263 | extern int grep_use_locks; |
| Thomas Rast | 0579f91 | 2011-12-12 21:16:07 | [diff] [blame] | 264 | extern pthread_mutex_t grep_attr_mutex; |
| Jeff King | b3aeb28 | 2012-02-02 08:18:41 | [diff] [blame] | 265 | |
| Junio C Hamano | 83b5d2f | 2006-09-17 23:02:52 | [diff] [blame] | 266 | #endif |