]>
Commit | Line | Data |
---|---|---|
83b5d2f5 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
83b5d2f5 | 3 | #include "grep.h" |
cbd53a21 | 4 | #include "object-store.h" |
60ecac98 | 5 | #include "userdiff.h" |
6bfce93e | 6 | #include "xdiff-interface.h" |
335ec3bf JK |
7 | #include "diff.h" |
8 | #include "diffcore.h" | |
5c1ebcca | 9 | #include "commit.h" |
793dc676 | 10 | #include "quote.h" |
3ac68a93 | 11 | #include "help.h" |
83b5d2f5 | 12 | |
07a7d656 | 13 | static int grep_source_load(struct grep_source *gs); |
acd00ea0 NTND |
14 | static int grep_source_is_binary(struct grep_source *gs, |
15 | struct index_state *istate); | |
07a7d656 | 16 | |
96313423 MÅ |
17 | static void std_output(struct grep_opt *opt, const void *buf, size_t size) |
18 | { | |
19 | fwrite(buf, size, 1, stdout); | |
20 | } | |
21 | ||
22 | static struct grep_opt grep_defaults = { | |
23 | .relative = 1, | |
24 | .pathname = 1, | |
25 | .max_depth = -1, | |
26 | .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, | |
27 | .colors = { | |
28 | [GREP_COLOR_CONTEXT] = "", | |
29 | [GREP_COLOR_FILENAME] = "", | |
30 | [GREP_COLOR_FUNCTION] = "", | |
31 | [GREP_COLOR_LINENO] = "", | |
32 | [GREP_COLOR_COLUMNNO] = "", | |
33 | [GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED, | |
34 | [GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED, | |
35 | [GREP_COLOR_SELECTED] = "", | |
36 | [GREP_COLOR_SEP] = GIT_COLOR_CYAN, | |
37 | }, | |
38 | .only_matching = 0, | |
39 | .color = -1, | |
40 | .output = std_output, | |
41 | }; | |
7687a054 | 42 | |
fa151dc5 NTND |
43 | static const char *color_grep_slots[] = { |
44 | [GREP_COLOR_CONTEXT] = "context", | |
45 | [GREP_COLOR_FILENAME] = "filename", | |
46 | [GREP_COLOR_FUNCTION] = "function", | |
47 | [GREP_COLOR_LINENO] = "lineNumber", | |
d036d667 | 48 | [GREP_COLOR_COLUMNNO] = "column", |
fa151dc5 NTND |
49 | [GREP_COLOR_MATCH_CONTEXT] = "matchContext", |
50 | [GREP_COLOR_MATCH_SELECTED] = "matchSelected", | |
51 | [GREP_COLOR_SELECTED] = "selected", | |
52 | [GREP_COLOR_SEP] = "separator", | |
53 | }; | |
54 | ||
7687a054 JH |
55 | static int parse_pattern_type_arg(const char *opt, const char *arg) |
56 | { | |
57 | if (!strcmp(arg, "default")) | |
58 | return GREP_PATTERN_TYPE_UNSPECIFIED; | |
59 | else if (!strcmp(arg, "basic")) | |
60 | return GREP_PATTERN_TYPE_BRE; | |
61 | else if (!strcmp(arg, "extended")) | |
62 | return GREP_PATTERN_TYPE_ERE; | |
63 | else if (!strcmp(arg, "fixed")) | |
64 | return GREP_PATTERN_TYPE_FIXED; | |
65 | else if (!strcmp(arg, "perl")) | |
66 | return GREP_PATTERN_TYPE_PCRE; | |
67 | die("bad %s argument: %s", opt, arg); | |
68 | } | |
69 | ||
3ac68a93 NTND |
70 | define_list_config_array_extra(color_grep_slots, {"match"}); |
71 | ||
7687a054 JH |
72 | /* |
73 | * Read the configuration file once and store it in | |
74 | * the grep_defaults template. | |
75 | */ | |
76 | int grep_config(const char *var, const char *value, void *cb) | |
77 | { | |
78 | struct grep_opt *opt = &grep_defaults; | |
fa151dc5 | 79 | const char *slot; |
7687a054 JH |
80 | |
81 | if (userdiff_config(var, value) < 0) | |
82 | return -1; | |
83 | ||
6ba9bb76 MÅ |
84 | /* |
85 | * The instance of grep_opt that we set up here is copied by | |
86 | * grep_init() to be used by each individual invocation. | |
87 | * When populating a new field of this structure here, be | |
88 | * sure to think about ownership -- e.g., you might need to | |
89 | * override the shallow copy in grep_init() with a deep copy. | |
90 | */ | |
91 | ||
7687a054 | 92 | if (!strcmp(var, "grep.extendedregexp")) { |
c7e38551 | 93 | opt->extended_regexp_option = git_config_bool(var, value); |
7687a054 JH |
94 | return 0; |
95 | } | |
96 | ||
97 | if (!strcmp(var, "grep.patterntype")) { | |
98 | opt->pattern_type_option = parse_pattern_type_arg(var, value); | |
99 | return 0; | |
100 | } | |
101 | ||
102 | if (!strcmp(var, "grep.linenumber")) { | |
103 | opt->linenum = git_config_bool(var, value); | |
104 | return 0; | |
105 | } | |
6653fec3 TB |
106 | if (!strcmp(var, "grep.column")) { |
107 | opt->columnnum = git_config_bool(var, value); | |
108 | return 0; | |
109 | } | |
7687a054 | 110 | |
6453f7b3 AS |
111 | if (!strcmp(var, "grep.fullname")) { |
112 | opt->relative = !git_config_bool(var, value); | |
113 | return 0; | |
114 | } | |
115 | ||
7687a054 JH |
116 | if (!strcmp(var, "color.grep")) |
117 | opt->color = git_config_colorbool(var, value); | |
fa151dc5 NTND |
118 | if (!strcmp(var, "color.grep.match")) { |
119 | if (grep_config("color.grep.matchcontext", value, cb) < 0) | |
120 | return -1; | |
121 | if (grep_config("color.grep.matchselected", value, cb) < 0) | |
122 | return -1; | |
123 | } else if (skip_prefix(var, "color.grep.", &slot)) { | |
124 | int i = LOOKUP_CONFIG(color_grep_slots, slot); | |
125 | char *color; | |
126 | ||
127 | if (i < 0) | |
128 | return -1; | |
129 | color = opt->colors[i]; | |
7687a054 JH |
130 | if (!value) |
131 | return config_error_nonbool(var); | |
f6c5a296 | 132 | return color_parse(value, color); |
7687a054 JH |
133 | } |
134 | return 0; | |
135 | } | |
136 | ||
137 | /* | |
138 | * Initialize one instance of grep_opt and copy the | |
139 | * default values from the template we read the configuration | |
140 | * information in an earlier call to git_config(grep_config). | |
141 | */ | |
38bbc2ea | 142 | void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix) |
7687a054 | 143 | { |
6ba9bb76 MÅ |
144 | *opt = grep_defaults; |
145 | ||
38bbc2ea | 146 | opt->repo = repo; |
7687a054 JH |
147 | opt->prefix = prefix; |
148 | opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0; | |
149 | opt->pattern_tail = &opt->pattern_list; | |
150 | opt->header_tail = &opt->header_list; | |
7687a054 | 151 | } |
07a7d656 | 152 | |
8465541e | 153 | static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt) |
c5c31d33 | 154 | { |
07a3d411 ÆAB |
155 | /* |
156 | * When committing to the pattern type by setting the relevant | |
157 | * fields in grep_opt it's generally not necessary to zero out | |
158 | * the fields we're not choosing, since they won't have been | |
159 | * set by anything. The extended_regexp_option field is the | |
160 | * only exception to this. | |
161 | * | |
162 | * This is because in the process of parsing grep.patternType | |
163 | * & grep.extendedRegexp we set opt->pattern_type_option and | |
164 | * opt->extended_regexp_option, respectively. We then | |
165 | * internally use opt->extended_regexp_option to see if we're | |
166 | * compiling an ERE. It must be unset if that's not actually | |
167 | * the case. | |
168 | */ | |
169 | if (pattern_type != GREP_PATTERN_TYPE_ERE && | |
170 | opt->extended_regexp_option) | |
171 | opt->extended_regexp_option = 0; | |
172 | ||
c5c31d33 JH |
173 | switch (pattern_type) { |
174 | case GREP_PATTERN_TYPE_UNSPECIFIED: | |
175 | /* fall through */ | |
176 | ||
177 | case GREP_PATTERN_TYPE_BRE: | |
c5c31d33 JH |
178 | break; |
179 | ||
180 | case GREP_PATTERN_TYPE_ERE: | |
07a3d411 | 181 | opt->extended_regexp_option = 1; |
c5c31d33 JH |
182 | break; |
183 | ||
184 | case GREP_PATTERN_TYPE_FIXED: | |
185 | opt->fixed = 1; | |
c5c31d33 JH |
186 | break; |
187 | ||
188 | case GREP_PATTERN_TYPE_PCRE: | |
94da9193 | 189 | opt->pcre2 = 1; |
c5c31d33 JH |
190 | break; |
191 | } | |
192 | } | |
193 | ||
8465541e JH |
194 | void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt) |
195 | { | |
196 | if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED) | |
197 | grep_set_pattern_type_option(pattern_type, opt); | |
198 | else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED) | |
199 | grep_set_pattern_type_option(opt->pattern_type_option, opt); | |
200 | else if (opt->extended_regexp_option) | |
07a3d411 ÆAB |
201 | /* |
202 | * This branch *must* happen after setting from the | |
203 | * opt->pattern_type_option above, we don't want | |
204 | * grep.extendedRegexp to override grep.patternType! | |
205 | */ | |
8465541e JH |
206 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt); |
207 | } | |
208 | ||
fc456751 RS |
209 | static struct grep_pat *create_grep_pat(const char *pat, size_t patlen, |
210 | const char *origin, int no, | |
211 | enum grep_pat_token t, | |
212 | enum grep_header_field field) | |
a4d7d2c6 JH |
213 | { |
214 | struct grep_pat *p = xcalloc(1, sizeof(*p)); | |
526a858a | 215 | p->pattern = xmemdupz(pat, patlen); |
fc456751 RS |
216 | p->patternlen = patlen; |
217 | p->origin = origin; | |
218 | p->no = no; | |
219 | p->token = t; | |
a4d7d2c6 | 220 | p->field = field; |
fc456751 RS |
221 | return p; |
222 | } | |
223 | ||
2b3873ff RS |
224 | static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p) |
225 | { | |
226 | **tail = p; | |
227 | *tail = &p->next; | |
a4d7d2c6 | 228 | p->next = NULL; |
526a858a RS |
229 | |
230 | switch (p->token) { | |
231 | case GREP_PATTERN: /* atom */ | |
232 | case GREP_PATTERN_HEAD: | |
233 | case GREP_PATTERN_BODY: | |
234 | for (;;) { | |
235 | struct grep_pat *new_pat; | |
236 | size_t len = 0; | |
237 | char *cp = p->pattern + p->patternlen, *nl = NULL; | |
238 | while (++len <= p->patternlen) { | |
239 | if (*(--cp) == '\n') { | |
240 | nl = cp; | |
241 | break; | |
242 | } | |
243 | } | |
244 | if (!nl) | |
245 | break; | |
246 | new_pat = create_grep_pat(nl + 1, len - 1, p->origin, | |
247 | p->no, p->token, p->field); | |
248 | new_pat->next = p->next; | |
249 | if (!p->next) | |
250 | *tail = &new_pat->next; | |
251 | p->next = new_pat; | |
252 | *nl = '\0'; | |
253 | p->patternlen -= len; | |
254 | } | |
255 | break; | |
256 | default: | |
257 | break; | |
258 | } | |
2b3873ff RS |
259 | } |
260 | ||
fc456751 RS |
261 | void append_header_grep_pattern(struct grep_opt *opt, |
262 | enum grep_header_field field, const char *pat) | |
263 | { | |
264 | struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0, | |
265 | GREP_PATTERN_HEAD, field); | |
baa6378f JH |
266 | if (field == GREP_HEADER_REFLOG) |
267 | opt->use_reflog_filter = 1; | |
2b3873ff | 268 | do_append_grep_pat(&opt->header_tail, p); |
a4d7d2c6 JH |
269 | } |
270 | ||
83b5d2f5 JH |
271 | void append_grep_pattern(struct grep_opt *opt, const char *pat, |
272 | const char *origin, int no, enum grep_pat_token t) | |
ed40a095 RS |
273 | { |
274 | append_grep_pat(opt, pat, strlen(pat), origin, no, t); | |
275 | } | |
276 | ||
277 | void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, | |
278 | const char *origin, int no, enum grep_pat_token t) | |
83b5d2f5 | 279 | { |
fc456751 | 280 | struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0); |
2b3873ff | 281 | do_append_grep_pat(&opt->pattern_tail, p); |
83b5d2f5 JH |
282 | } |
283 | ||
5b594f45 FK |
284 | struct grep_opt *grep_opt_dup(const struct grep_opt *opt) |
285 | { | |
286 | struct grep_pat *pat; | |
287 | struct grep_opt *ret = xmalloc(sizeof(struct grep_opt)); | |
288 | *ret = *opt; | |
289 | ||
290 | ret->pattern_list = NULL; | |
291 | ret->pattern_tail = &ret->pattern_list; | |
292 | ||
293 | for(pat = opt->pattern_list; pat != NULL; pat = pat->next) | |
294 | { | |
295 | if(pat->token == GREP_PATTERN_HEAD) | |
296 | append_header_grep_pattern(ret, pat->field, | |
297 | pat->pattern); | |
298 | else | |
ed40a095 RS |
299 | append_grep_pat(ret, pat->pattern, pat->patternlen, |
300 | pat->origin, pat->no, pat->token); | |
5b594f45 FK |
301 | } |
302 | ||
303 | return ret; | |
304 | } | |
305 | ||
a30c148a MK |
306 | static NORETURN void compile_regexp_failed(const struct grep_pat *p, |
307 | const char *error) | |
308 | { | |
309 | char where[1024]; | |
310 | ||
311 | if (p->no) | |
19bdd3e7 | 312 | xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no); |
a30c148a | 313 | else if (p->origin) |
19bdd3e7 | 314 | xsnprintf(where, sizeof(where), "%s, ", p->origin); |
a30c148a MK |
315 | else |
316 | where[0] = 0; | |
317 | ||
318 | die("%s'%s': %s", where, p->pattern, error); | |
319 | } | |
320 | ||
543f1c0c ÆAB |
321 | static int is_fixed(const char *s, size_t len) |
322 | { | |
323 | size_t i; | |
324 | ||
325 | for (i = 0; i < len; i++) { | |
326 | if (is_regex_special(s[i])) | |
327 | return 0; | |
328 | } | |
329 | ||
330 | return 1; | |
331 | } | |
332 | ||
94da9193 | 333 | #ifdef USE_LIBPCRE2 |
c1760352 ÆAB |
334 | #define GREP_PCRE2_DEBUG_MALLOC 0 |
335 | ||
336 | static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data) | |
337 | { | |
338 | void *pointer = malloc(size); | |
339 | #if GREP_PCRE2_DEBUG_MALLOC | |
340 | static int count = 1; | |
341 | fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size); | |
342 | #endif | |
343 | return pointer; | |
344 | } | |
345 | ||
346 | static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data) | |
347 | { | |
348 | #if GREP_PCRE2_DEBUG_MALLOC | |
349 | static int count = 1; | |
350 | if (pointer) | |
351 | fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++); | |
352 | #endif | |
353 | free(pointer); | |
354 | } | |
355 | ||
94da9193 ÆAB |
356 | static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt) |
357 | { | |
358 | int error; | |
359 | PCRE2_UCHAR errbuf[256]; | |
360 | PCRE2_SIZE erroffset; | |
361 | int options = PCRE2_MULTILINE; | |
94da9193 | 362 | int jitret; |
a25b9085 ÆAB |
363 | int patinforet; |
364 | size_t jitsizearg; | |
94da9193 | 365 | |
cbe81e65 ÆAB |
366 | /* |
367 | * Call pcre2_general_context_create() before calling any | |
368 | * other pcre2_*(). It sets up our malloc()/free() functions | |
369 | * with which everything else is allocated. | |
370 | */ | |
371 | p->pcre2_general_context = pcre2_general_context_create( | |
372 | pcre2_malloc, pcre2_free, NULL); | |
373 | if (!p->pcre2_general_context) | |
374 | die("Couldn't allocate PCRE2 general context"); | |
94da9193 ÆAB |
375 | |
376 | if (opt->ignore_case) { | |
44570188 | 377 | if (!opt->ignore_locale && has_non_ascii(p->pattern)) { |
cbe81e65 ÆAB |
378 | p->pcre2_tables = pcre2_maketables(p->pcre2_general_context); |
379 | p->pcre2_compile_context = pcre2_compile_context_create(p->pcre2_general_context); | |
10da030a CMAB |
380 | pcre2_set_character_tables(p->pcre2_compile_context, |
381 | p->pcre2_tables); | |
94da9193 ÆAB |
382 | } |
383 | options |= PCRE2_CASELESS; | |
384 | } | |
e7f3925b JH |
385 | if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) && |
386 | !(!opt->ignore_case && (p->fixed || p->is_fixed))) | |
95ca1f98 ÆAB |
387 | options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF); |
388 | ||
797c3599 | 389 | #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER |
95ca1f98 | 390 | /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */ |
797c3599 ÆAB |
391 | if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS)) |
392 | options |= PCRE2_NO_START_OPTIMIZE; | |
393 | #endif | |
94da9193 ÆAB |
394 | |
395 | p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern, | |
396 | p->patternlen, options, &error, &erroffset, | |
397 | p->pcre2_compile_context); | |
398 | ||
399 | if (p->pcre2_pattern) { | |
cbe81e65 | 400 | p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, p->pcre2_general_context); |
94da9193 ÆAB |
401 | if (!p->pcre2_match_data) |
402 | die("Couldn't allocate PCRE2 match data"); | |
403 | } else { | |
404 | pcre2_get_error_message(error, errbuf, sizeof(errbuf)); | |
405 | compile_regexp_failed(p, (const char *)&errbuf); | |
406 | } | |
407 | ||
408 | pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on); | |
04bef50c | 409 | if (p->pcre2_jit_on) { |
94da9193 ÆAB |
410 | jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE); |
411 | if (jitret) | |
412 | die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret); | |
a25b9085 ÆAB |
413 | |
414 | /* | |
415 | * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just | |
416 | * tells us whether the library itself supports JIT, | |
417 | * but to see whether we're going to be actually using | |
418 | * JIT we need to extract PCRE2_INFO_JITSIZE from the | |
419 | * pattern *after* we do pcre2_jit_compile() above. | |
420 | * | |
421 | * This is because if the pattern contains the | |
422 | * (*NO_JIT) verb (see pcre2syntax(3)) | |
423 | * pcre2_jit_compile() will exit early with 0. If we | |
424 | * then proceed to call pcre2_jit_match() further down | |
425 | * the line instead of pcre2_match() we'll either | |
426 | * segfault (pre PCRE 10.31) or run into a fatal error | |
427 | * (post PCRE2 10.31) | |
428 | */ | |
429 | patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg); | |
430 | if (patinforet) | |
431 | BUG("pcre2_pattern_info() failed: %d", patinforet); | |
432 | if (jitsizearg == 0) { | |
433 | p->pcre2_jit_on = 0; | |
434 | return; | |
435 | } | |
94da9193 ÆAB |
436 | } |
437 | } | |
438 | ||
439 | static int pcre2match(struct grep_pat *p, const char *line, const char *eol, | |
440 | regmatch_t *match, int eflags) | |
441 | { | |
442 | int ret, flags = 0; | |
443 | PCRE2_SIZE *ovector; | |
444 | PCRE2_UCHAR errbuf[256]; | |
445 | ||
446 | if (eflags & REG_NOTBOL) | |
447 | flags |= PCRE2_NOTBOL; | |
448 | ||
449 | if (p->pcre2_jit_on) | |
450 | ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line, | |
451 | eol - line, 0, flags, p->pcre2_match_data, | |
452 | NULL); | |
453 | else | |
454 | ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line, | |
455 | eol - line, 0, flags, p->pcre2_match_data, | |
456 | NULL); | |
457 | ||
458 | if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) { | |
459 | pcre2_get_error_message(ret, errbuf, sizeof(errbuf)); | |
460 | die("%s failed with error code %d: %s", | |
461 | (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret, | |
462 | errbuf); | |
463 | } | |
464 | if (ret > 0) { | |
465 | ovector = pcre2_get_ovector_pointer(p->pcre2_match_data); | |
466 | ret = 0; | |
467 | match->rm_so = (int)ovector[0]; | |
468 | match->rm_eo = (int)ovector[1]; | |
469 | } | |
470 | ||
471 | return ret; | |
472 | } | |
473 | ||
474 | static void free_pcre2_pattern(struct grep_pat *p) | |
475 | { | |
476 | pcre2_compile_context_free(p->pcre2_compile_context); | |
477 | pcre2_code_free(p->pcre2_pattern); | |
478 | pcre2_match_data_free(p->pcre2_match_data); | |
b76bf27f | 479 | #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER |
cbe81e65 | 480 | pcre2_maketables_free(p->pcre2_general_context, p->pcre2_tables); |
b76bf27f | 481 | #else |
10da030a | 482 | free((void *)p->pcre2_tables); |
b76bf27f | 483 | #endif |
cbe81e65 | 484 | pcre2_general_context_free(p->pcre2_general_context); |
94da9193 ÆAB |
485 | } |
486 | #else /* !USE_LIBPCRE2 */ | |
487 | static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt) | |
488 | { | |
94da9193 ÆAB |
489 | die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE"); |
490 | } | |
491 | ||
492 | static int pcre2match(struct grep_pat *p, const char *line, const char *eol, | |
493 | regmatch_t *match, int eflags) | |
494 | { | |
495 | return 1; | |
496 | } | |
497 | ||
498 | static void free_pcre2_pattern(struct grep_pat *p) | |
499 | { | |
500 | } | |
94da9193 | 501 | |
793dc676 NTND |
502 | static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt) |
503 | { | |
504 | struct strbuf sb = STRBUF_INIT; | |
505 | int err; | |
1ceababc | 506 | int regflags = 0; |
793dc676 NTND |
507 | |
508 | basic_regex_quote_buf(&sb, p->pattern); | |
793dc676 NTND |
509 | if (opt->ignore_case) |
510 | regflags |= REG_ICASE; | |
511 | err = regcomp(&p->regexp, sb.buf, regflags); | |
793dc676 NTND |
512 | strbuf_release(&sb); |
513 | if (err) { | |
514 | char errbuf[1024]; | |
515 | regerror(err, &p->regexp, errbuf, sizeof(errbuf)); | |
793dc676 NTND |
516 | compile_regexp_failed(p, errbuf); |
517 | } | |
518 | } | |
b65abcaf | 519 | #endif /* !USE_LIBPCRE2 */ |
793dc676 | 520 | |
83b5d2f5 JH |
521 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) |
522 | { | |
c822255c | 523 | int err; |
07a3d411 | 524 | int regflags = REG_NEWLINE; |
c822255c | 525 | |
d7eb527d | 526 | p->word_regexp = opt->word_regexp; |
5183bf67 | 527 | p->ignore_case = opt->ignore_case; |
48de2a76 | 528 | p->fixed = opt->fixed; |
d7eb527d | 529 | |
45d1f37c ÆAB |
530 | if (memchr(p->pattern, 0, p->patternlen) && !opt->pcre2) |
531 | die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2")); | |
532 | ||
09872f64 | 533 | p->is_fixed = is_fixed(p->pattern, p->patternlen); |
8a599983 ÆAB |
534 | #ifdef USE_LIBPCRE2 |
535 | if (!p->fixed && !p->is_fixed) { | |
536 | const char *no_jit = "(*NO_JIT)"; | |
537 | const int no_jit_len = strlen(no_jit); | |
538 | if (starts_with(p->pattern, no_jit) && | |
539 | is_fixed(p->pattern + no_jit_len, | |
540 | p->patternlen - no_jit_len)) | |
541 | p->is_fixed = 1; | |
542 | } | |
543 | #endif | |
09872f64 | 544 | if (p->fixed || p->is_fixed) { |
b65abcaf | 545 | #ifdef USE_LIBPCRE2 |
09872f64 | 546 | if (p->is_fixed) { |
b65abcaf ÆAB |
547 | compile_pcre2_pattern(p, opt); |
548 | } else { | |
549 | /* | |
550 | * E.g. t7811-grep-open.sh relies on the | |
551 | * pattern being restored. | |
552 | */ | |
553 | char *old_pattern = p->pattern; | |
554 | size_t old_patternlen = p->patternlen; | |
555 | struct strbuf sb = STRBUF_INIT; | |
556 | ||
557 | /* | |
558 | * There is the PCRE2_LITERAL flag, but it's | |
559 | * only in PCRE v2 10.30 and later. Needing to | |
560 | * ifdef our way around that and dealing with | |
561 | * it + PCRE2_MULTILINE being an error is more | |
562 | * complex than just quoting this ourselves. | |
563 | */ | |
564 | strbuf_add(&sb, "\\Q", 2); | |
565 | strbuf_add(&sb, p->pattern, p->patternlen); | |
566 | strbuf_add(&sb, "\\E", 2); | |
567 | ||
568 | p->pattern = sb.buf; | |
569 | p->patternlen = sb.len; | |
570 | compile_pcre2_pattern(p, opt); | |
571 | p->pattern = old_pattern; | |
572 | p->patternlen = old_patternlen; | |
573 | strbuf_release(&sb); | |
574 | } | |
575 | #else /* !USE_LIBPCRE2 */ | |
793dc676 | 576 | compile_fixed_regexp(p, opt); |
b65abcaf | 577 | #endif /* !USE_LIBPCRE2 */ |
793dc676 | 578 | return; |
9eceddee | 579 | } |
c822255c | 580 | |
94da9193 ÆAB |
581 | if (opt->pcre2) { |
582 | compile_pcre2_pattern(p, opt); | |
583 | return; | |
584 | } | |
585 | ||
07a3d411 ÆAB |
586 | if (p->ignore_case) |
587 | regflags |= REG_ICASE; | |
588 | if (opt->extended_regexp_option) | |
589 | regflags |= REG_EXTENDED; | |
590 | err = regcomp(&p->regexp, p->pattern, regflags); | |
83b5d2f5 JH |
591 | if (err) { |
592 | char errbuf[1024]; | |
83b5d2f5 | 593 | regerror(err, &p->regexp, errbuf, 1024); |
a30c148a | 594 | compile_regexp_failed(p, errbuf); |
83b5d2f5 JH |
595 | } |
596 | } | |
597 | ||
0ab7befa | 598 | static struct grep_expr *compile_pattern_or(struct grep_pat **); |
83b5d2f5 JH |
599 | static struct grep_expr *compile_pattern_atom(struct grep_pat **list) |
600 | { | |
601 | struct grep_pat *p; | |
602 | struct grep_expr *x; | |
603 | ||
604 | p = *list; | |
c922b01f LT |
605 | if (!p) |
606 | return NULL; | |
83b5d2f5 JH |
607 | switch (p->token) { |
608 | case GREP_PATTERN: /* atom */ | |
480c1ca6 JH |
609 | case GREP_PATTERN_HEAD: |
610 | case GREP_PATTERN_BODY: | |
ca56dadb | 611 | CALLOC_ARRAY(x, 1); |
83b5d2f5 JH |
612 | x->node = GREP_NODE_ATOM; |
613 | x->u.atom = p; | |
614 | *list = p->next; | |
615 | return x; | |
616 | case GREP_OPEN_PAREN: | |
617 | *list = p->next; | |
0ab7befa | 618 | x = compile_pattern_or(list); |
83b5d2f5 JH |
619 | if (!*list || (*list)->token != GREP_CLOSE_PAREN) |
620 | die("unmatched parenthesis"); | |
621 | *list = (*list)->next; | |
622 | return x; | |
623 | default: | |
624 | return NULL; | |
625 | } | |
626 | } | |
627 | ||
628 | static struct grep_expr *compile_pattern_not(struct grep_pat **list) | |
629 | { | |
630 | struct grep_pat *p; | |
631 | struct grep_expr *x; | |
632 | ||
633 | p = *list; | |
c922b01f LT |
634 | if (!p) |
635 | return NULL; | |
83b5d2f5 JH |
636 | switch (p->token) { |
637 | case GREP_NOT: | |
638 | if (!p->next) | |
639 | die("--not not followed by pattern expression"); | |
640 | *list = p->next; | |
ca56dadb | 641 | CALLOC_ARRAY(x, 1); |
83b5d2f5 JH |
642 | x->node = GREP_NODE_NOT; |
643 | x->u.unary = compile_pattern_not(list); | |
644 | if (!x->u.unary) | |
645 | die("--not followed by non pattern expression"); | |
646 | return x; | |
647 | default: | |
648 | return compile_pattern_atom(list); | |
649 | } | |
650 | } | |
651 | ||
652 | static struct grep_expr *compile_pattern_and(struct grep_pat **list) | |
653 | { | |
654 | struct grep_pat *p; | |
655 | struct grep_expr *x, *y, *z; | |
656 | ||
657 | x = compile_pattern_not(list); | |
658 | p = *list; | |
659 | if (p && p->token == GREP_AND) { | |
fe7fe62d RS |
660 | if (!x) |
661 | die("--and not preceded by pattern expression"); | |
83b5d2f5 JH |
662 | if (!p->next) |
663 | die("--and not followed by pattern expression"); | |
664 | *list = p->next; | |
665 | y = compile_pattern_and(list); | |
666 | if (!y) | |
667 | die("--and not followed by pattern expression"); | |
ca56dadb | 668 | CALLOC_ARRAY(z, 1); |
83b5d2f5 JH |
669 | z->node = GREP_NODE_AND; |
670 | z->u.binary.left = x; | |
671 | z->u.binary.right = y; | |
672 | return z; | |
673 | } | |
674 | return x; | |
675 | } | |
676 | ||
677 | static struct grep_expr *compile_pattern_or(struct grep_pat **list) | |
678 | { | |
679 | struct grep_pat *p; | |
680 | struct grep_expr *x, *y, *z; | |
681 | ||
682 | x = compile_pattern_and(list); | |
683 | p = *list; | |
684 | if (x && p && p->token != GREP_CLOSE_PAREN) { | |
685 | y = compile_pattern_or(list); | |
686 | if (!y) | |
687 | die("not a pattern expression %s", p->pattern); | |
ca56dadb | 688 | CALLOC_ARRAY(z, 1); |
83b5d2f5 JH |
689 | z->node = GREP_NODE_OR; |
690 | z->u.binary.left = x; | |
691 | z->u.binary.right = y; | |
692 | return z; | |
693 | } | |
694 | return x; | |
695 | } | |
696 | ||
697 | static struct grep_expr *compile_pattern_expr(struct grep_pat **list) | |
698 | { | |
699 | return compile_pattern_or(list); | |
700 | } | |
701 | ||
5aaeb733 JH |
702 | static struct grep_expr *grep_true_expr(void) |
703 | { | |
704 | struct grep_expr *z = xcalloc(1, sizeof(*z)); | |
705 | z->node = GREP_NODE_TRUE; | |
706 | return z; | |
707 | } | |
708 | ||
709 | static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right) | |
710 | { | |
711 | struct grep_expr *z = xcalloc(1, sizeof(*z)); | |
712 | z->node = GREP_NODE_OR; | |
713 | z->u.binary.left = left; | |
714 | z->u.binary.right = right; | |
715 | return z; | |
716 | } | |
717 | ||
95ce9ce2 | 718 | static struct grep_expr *prep_header_patterns(struct grep_opt *opt) |
83b5d2f5 JH |
719 | { |
720 | struct grep_pat *p; | |
95ce9ce2 | 721 | struct grep_expr *header_expr; |
5aaeb733 JH |
722 | struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]); |
723 | enum grep_header_field fld; | |
83b5d2f5 | 724 | |
95ce9ce2 JH |
725 | if (!opt->header_list) |
726 | return NULL; | |
2385f246 | 727 | |
95ce9ce2 JH |
728 | for (p = opt->header_list; p; p = p->next) { |
729 | if (p->token != GREP_PATTERN_HEAD) | |
033abf97 | 730 | BUG("a non-header pattern in grep header list."); |
3ce3ffb8 AP |
731 | if (p->field < GREP_HEADER_FIELD_MIN || |
732 | GREP_HEADER_FIELD_MAX <= p->field) | |
033abf97 | 733 | BUG("unknown header field %d", p->field); |
95ce9ce2 | 734 | compile_regexp(p, opt); |
80235ba7 | 735 | } |
5aaeb733 JH |
736 | |
737 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) | |
738 | header_group[fld] = NULL; | |
739 | ||
740 | for (p = opt->header_list; p; p = p->next) { | |
741 | struct grep_expr *h; | |
742 | struct grep_pat *pp = p; | |
743 | ||
744 | h = compile_pattern_atom(&pp); | |
745 | if (!h || pp != p->next) | |
033abf97 | 746 | BUG("malformed header expr"); |
5aaeb733 JH |
747 | if (!header_group[p->field]) { |
748 | header_group[p->field] = h; | |
749 | continue; | |
750 | } | |
751 | header_group[p->field] = grep_or_expr(h, header_group[p->field]); | |
752 | } | |
753 | ||
754 | header_expr = NULL; | |
755 | ||
756 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) { | |
757 | if (!header_group[fld]) | |
758 | continue; | |
759 | if (!header_expr) | |
760 | header_expr = grep_true_expr(); | |
761 | header_expr = grep_or_expr(header_group[fld], header_expr); | |
762 | } | |
95ce9ce2 JH |
763 | return header_expr; |
764 | } | |
765 | ||
13e4fc7e JH |
766 | static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y) |
767 | { | |
768 | struct grep_expr *z = x; | |
769 | ||
770 | while (x) { | |
771 | assert(x->node == GREP_NODE_OR); | |
772 | if (x->u.binary.right && | |
773 | x->u.binary.right->node == GREP_NODE_TRUE) { | |
774 | x->u.binary.right = y; | |
775 | break; | |
776 | } | |
777 | x = x->u.binary.right; | |
778 | } | |
779 | return z; | |
780 | } | |
781 | ||
15c96497 | 782 | void compile_grep_patterns(struct grep_opt *opt) |
95ce9ce2 JH |
783 | { |
784 | struct grep_pat *p; | |
785 | struct grep_expr *header_expr = prep_header_patterns(opt); | |
0ab7befa | 786 | |
83b5d2f5 | 787 | for (p = opt->pattern_list; p; p = p->next) { |
480c1ca6 JH |
788 | switch (p->token) { |
789 | case GREP_PATTERN: /* atom */ | |
790 | case GREP_PATTERN_HEAD: | |
791 | case GREP_PATTERN_BODY: | |
c822255c | 792 | compile_regexp(p, opt); |
480c1ca6 JH |
793 | break; |
794 | default: | |
83b5d2f5 | 795 | opt->extended = 1; |
480c1ca6 JH |
796 | break; |
797 | } | |
83b5d2f5 JH |
798 | } |
799 | ||
80235ba7 JH |
800 | if (opt->all_match || header_expr) |
801 | opt->extended = 1; | |
15c96497 | 802 | else if (!opt->extended) |
83b5d2f5 JH |
803 | return; |
804 | ||
83b5d2f5 | 805 | p = opt->pattern_list; |
ba150a3f MB |
806 | if (p) |
807 | opt->pattern_expression = compile_pattern_expr(&p); | |
83b5d2f5 JH |
808 | if (p) |
809 | die("incomplete pattern expression: %s", p->pattern); | |
80235ba7 JH |
810 | |
811 | if (!header_expr) | |
812 | return; | |
813 | ||
5aaeb733 | 814 | if (!opt->pattern_expression) |
80235ba7 | 815 | opt->pattern_expression = header_expr; |
13e4fc7e JH |
816 | else if (opt->all_match) |
817 | opt->pattern_expression = grep_splice_or(header_expr, | |
818 | opt->pattern_expression); | |
5aaeb733 JH |
819 | else |
820 | opt->pattern_expression = grep_or_expr(opt->pattern_expression, | |
821 | header_expr); | |
80235ba7 | 822 | opt->all_match = 1; |
83b5d2f5 JH |
823 | } |
824 | ||
b48fb5b6 JH |
825 | static void free_pattern_expr(struct grep_expr *x) |
826 | { | |
827 | switch (x->node) { | |
5aaeb733 | 828 | case GREP_NODE_TRUE: |
b48fb5b6 JH |
829 | case GREP_NODE_ATOM: |
830 | break; | |
831 | case GREP_NODE_NOT: | |
832 | free_pattern_expr(x->u.unary); | |
833 | break; | |
834 | case GREP_NODE_AND: | |
835 | case GREP_NODE_OR: | |
836 | free_pattern_expr(x->u.binary.left); | |
837 | free_pattern_expr(x->u.binary.right); | |
838 | break; | |
839 | } | |
840 | free(x); | |
841 | } | |
842 | ||
843 | void free_grep_patterns(struct grep_opt *opt) | |
844 | { | |
845 | struct grep_pat *p, *n; | |
846 | ||
847 | for (p = opt->pattern_list; p; p = n) { | |
848 | n = p->next; | |
849 | switch (p->token) { | |
850 | case GREP_PATTERN: /* atom */ | |
851 | case GREP_PATTERN_HEAD: | |
852 | case GREP_PATTERN_BODY: | |
7599730b | 853 | if (p->pcre2_pattern) |
94da9193 | 854 | free_pcre2_pattern(p); |
63e7e9d8 MK |
855 | else |
856 | regfree(&p->regexp); | |
526a858a | 857 | free(p->pattern); |
b48fb5b6 JH |
858 | break; |
859 | default: | |
860 | break; | |
861 | } | |
862 | free(p); | |
863 | } | |
864 | ||
865 | if (!opt->extended) | |
866 | return; | |
867 | free_pattern_expr(opt->pattern_expression); | |
868 | } | |
869 | ||
1a845fbc | 870 | static const char *end_of_line(const char *cp, unsigned long *left) |
83b5d2f5 JH |
871 | { |
872 | unsigned long l = *left; | |
873 | while (l && *cp != '\n') { | |
874 | l--; | |
875 | cp++; | |
876 | } | |
877 | *left = l; | |
878 | return cp; | |
879 | } | |
880 | ||
881 | static int word_char(char ch) | |
882 | { | |
883 | return isalnum(ch) || ch == '_'; | |
884 | } | |
885 | ||
55f638bd ML |
886 | static void output_color(struct grep_opt *opt, const void *data, size_t size, |
887 | const char *color) | |
888 | { | |
daa0c3d9 | 889 | if (want_color(opt->color) && color && color[0]) { |
55f638bd ML |
890 | opt->output(opt, color, strlen(color)); |
891 | opt->output(opt, data, size); | |
892 | opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET)); | |
893 | } else | |
894 | opt->output(opt, data, size); | |
895 | } | |
896 | ||
897 | static void output_sep(struct grep_opt *opt, char sign) | |
898 | { | |
899 | if (opt->null_following_name) | |
900 | opt->output(opt, "\0", 1); | |
901 | else | |
fa151dc5 | 902 | output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]); |
55f638bd ML |
903 | } |
904 | ||
83caecca RZ |
905 | static void show_name(struct grep_opt *opt, const char *name) |
906 | { | |
fa151dc5 | 907 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
5b594f45 | 908 | opt->output(opt, opt->null_following_name ? "\0" : "\n", 1); |
83caecca RZ |
909 | } |
910 | ||
1a845fbc JK |
911 | static int patmatch(struct grep_pat *p, |
912 | const char *line, const char *eol, | |
97e77784 MK |
913 | regmatch_t *match, int eflags) |
914 | { | |
915 | int hit; | |
916 | ||
7599730b | 917 | if (p->pcre2_pattern) |
94da9193 | 918 | hit = !pcre2match(p, line, eol, match, eflags); |
97e77784 | 919 | else |
b7d36ffc JS |
920 | hit = !regexec_buf(&p->regexp, line, eol - line, 1, match, |
921 | eflags); | |
97e77784 MK |
922 | |
923 | return hit; | |
924 | } | |
925 | ||
1a845fbc | 926 | static void strip_timestamp(const char *bol, const char **eol_p) |
a4d7d2c6 | 927 | { |
1a845fbc | 928 | const char *eol = *eol_p; |
a4d7d2c6 JH |
929 | |
930 | while (bol < --eol) { | |
931 | if (*eol != '>') | |
932 | continue; | |
933 | *eol_p = ++eol; | |
cc8e26ee | 934 | break; |
a4d7d2c6 | 935 | } |
a4d7d2c6 JH |
936 | } |
937 | ||
938 | static struct { | |
939 | const char *field; | |
940 | size_t len; | |
941 | } header_field[] = { | |
942 | { "author ", 7 }, | |
943 | { "committer ", 10 }, | |
72fd13f7 | 944 | { "reflog ", 7 }, |
a4d7d2c6 JH |
945 | }; |
946 | ||
3f566c4e HM |
947 | static int headerless_match_one_pattern(struct grep_pat *p, |
948 | const char *bol, const char *eol, | |
949 | enum grep_context ctx, | |
950 | regmatch_t *pmatch, int eflags) | |
83b5d2f5 JH |
951 | { |
952 | int hit = 0; | |
e701fadb | 953 | const char *start = bol; |
83b5d2f5 | 954 | |
480c1ca6 JH |
955 | if ((p->token != GREP_PATTERN) && |
956 | ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD))) | |
957 | return 0; | |
958 | ||
83b5d2f5 | 959 | again: |
97e77784 | 960 | hit = patmatch(p, bol, eol, pmatch, eflags); |
83b5d2f5 | 961 | |
d7eb527d | 962 | if (hit && p->word_regexp) { |
83b5d2f5 | 963 | if ((pmatch[0].rm_so < 0) || |
84201eae | 964 | (eol - bol) < pmatch[0].rm_so || |
83b5d2f5 JH |
965 | (pmatch[0].rm_eo < 0) || |
966 | (eol - bol) < pmatch[0].rm_eo) | |
967 | die("regexp returned nonsense"); | |
968 | ||
969 | /* Match beginning must be either beginning of the | |
970 | * line, or at word boundary (i.e. the last char must | |
971 | * not be a word char). Similarly, match end must be | |
972 | * either end of the line, or at word boundary | |
973 | * (i.e. the next char must not be a word char). | |
974 | */ | |
fb62eb7f | 975 | if ( ((pmatch[0].rm_so == 0) || |
83b5d2f5 JH |
976 | !word_char(bol[pmatch[0].rm_so-1])) && |
977 | ((pmatch[0].rm_eo == (eol-bol)) || | |
978 | !word_char(bol[pmatch[0].rm_eo])) ) | |
979 | ; | |
980 | else | |
981 | hit = 0; | |
982 | ||
84201eae RS |
983 | /* Words consist of at least one character. */ |
984 | if (pmatch->rm_so == pmatch->rm_eo) | |
985 | hit = 0; | |
986 | ||
83b5d2f5 JH |
987 | if (!hit && pmatch[0].rm_so + bol + 1 < eol) { |
988 | /* There could be more than one match on the | |
989 | * line, and the first match might not be | |
990 | * strict word match. But later ones could be! | |
fb62eb7f RS |
991 | * Forward to the next possible start, i.e. the |
992 | * next position following a non-word char. | |
83b5d2f5 JH |
993 | */ |
994 | bol = pmatch[0].rm_so + bol + 1; | |
fb62eb7f RS |
995 | while (word_char(bol[-1]) && bol < eol) |
996 | bol++; | |
dbb6a4ad | 997 | eflags |= REG_NOTBOL; |
fb62eb7f RS |
998 | if (bol < eol) |
999 | goto again; | |
83b5d2f5 JH |
1000 | } |
1001 | } | |
e701fadb RS |
1002 | if (hit) { |
1003 | pmatch[0].rm_so += bol - start; | |
1004 | pmatch[0].rm_eo += bol - start; | |
1005 | } | |
83b5d2f5 JH |
1006 | return hit; |
1007 | } | |
1008 | ||
3f566c4e HM |
1009 | static int match_one_pattern(struct grep_pat *p, |
1010 | const char *bol, const char *eol, | |
1011 | enum grep_context ctx, regmatch_t *pmatch, | |
1012 | int eflags) | |
1013 | { | |
1014 | const char *field; | |
1015 | size_t len; | |
1016 | ||
1017 | if (p->token == GREP_PATTERN_HEAD) { | |
1018 | assert(p->field < ARRAY_SIZE(header_field)); | |
1019 | field = header_field[p->field].field; | |
1020 | len = header_field[p->field].len; | |
1021 | if (strncmp(bol, field, len)) | |
1022 | return 0; | |
1023 | bol += len; | |
1024 | ||
1025 | switch (p->field) { | |
1026 | case GREP_HEADER_AUTHOR: | |
1027 | case GREP_HEADER_COMMITTER: | |
1028 | strip_timestamp(bol, &eol); | |
1029 | break; | |
1030 | default: | |
1031 | break; | |
1032 | } | |
1033 | } | |
1034 | ||
1035 | return headerless_match_one_pattern(p, bol, eol, ctx, pmatch, eflags); | |
1036 | } | |
1037 | ||
1038 | ||
1a845fbc JK |
1039 | static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, |
1040 | const char *bol, const char *eol, | |
1041 | enum grep_context ctx, ssize_t *col, | |
68d686e6 | 1042 | ssize_t *icol, int collect_hits) |
83b5d2f5 | 1043 | { |
0ab7befa JH |
1044 | int h = 0; |
1045 | ||
c922b01f LT |
1046 | if (!x) |
1047 | die("Not a valid grep expression"); | |
83b5d2f5 | 1048 | switch (x->node) { |
5aaeb733 JH |
1049 | case GREP_NODE_TRUE: |
1050 | h = 1; | |
1051 | break; | |
83b5d2f5 | 1052 | case GREP_NODE_ATOM: |
68d686e6 TB |
1053 | { |
1054 | regmatch_t tmp; | |
1055 | h = match_one_pattern(x->u.atom, bol, eol, ctx, | |
1056 | &tmp, 0); | |
1057 | if (h && (*col < 0 || tmp.rm_so < *col)) | |
1058 | *col = tmp.rm_so; | |
1059 | } | |
83b5d2f5 JH |
1060 | break; |
1061 | case GREP_NODE_NOT: | |
68d686e6 TB |
1062 | /* |
1063 | * Upon visiting a GREP_NODE_NOT, col and icol become swapped. | |
1064 | */ | |
1065 | h = !match_expr_eval(opt, x->u.unary, bol, eol, ctx, icol, col, | |
1066 | 0); | |
0ab7befa | 1067 | break; |
83b5d2f5 | 1068 | case GREP_NODE_AND: |
017c0fcf | 1069 | h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col, |
68d686e6 | 1070 | icol, 0); |
017c0fcf TB |
1071 | if (h || opt->columnnum) { |
1072 | /* | |
1073 | * Don't short-circuit AND when given --column, since a | |
1074 | * NOT earlier in the tree may turn this into an OR. In | |
1075 | * this case, see the below comment. | |
1076 | */ | |
1077 | h &= match_expr_eval(opt, x->u.binary.right, bol, eol, | |
1078 | ctx, col, icol, 0); | |
1079 | } | |
0ab7befa | 1080 | break; |
83b5d2f5 | 1081 | case GREP_NODE_OR: |
017c0fcf TB |
1082 | if (!(collect_hits || opt->columnnum)) { |
1083 | /* | |
1084 | * Don't short-circuit OR when given --column (or | |
1085 | * collecting hits) to ensure we don't skip a later | |
1086 | * child that would produce an earlier match. | |
1087 | */ | |
68d686e6 TB |
1088 | return (match_expr_eval(opt, x->u.binary.left, bol, eol, |
1089 | ctx, col, icol, 0) || | |
1090 | match_expr_eval(opt, x->u.binary.right, bol, | |
1091 | eol, ctx, col, icol, 0)); | |
017c0fcf | 1092 | } |
68d686e6 TB |
1093 | h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col, |
1094 | icol, 0); | |
017c0fcf TB |
1095 | if (collect_hits) |
1096 | x->u.binary.left->hit |= h; | |
68d686e6 | 1097 | h |= match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col, |
017c0fcf | 1098 | icol, collect_hits); |
0ab7befa JH |
1099 | break; |
1100 | default: | |
d7530708 | 1101 | die("Unexpected node type (internal error) %d", x->node); |
83b5d2f5 | 1102 | } |
0ab7befa JH |
1103 | if (collect_hits) |
1104 | x->hit |= h; | |
1105 | return h; | |
83b5d2f5 JH |
1106 | } |
1107 | ||
1a845fbc JK |
1108 | static int match_expr(struct grep_opt *opt, |
1109 | const char *bol, const char *eol, | |
68d686e6 TB |
1110 | enum grep_context ctx, ssize_t *col, |
1111 | ssize_t *icol, int collect_hits) | |
83b5d2f5 JH |
1112 | { |
1113 | struct grep_expr *x = opt->pattern_expression; | |
68d686e6 | 1114 | return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits); |
83b5d2f5 JH |
1115 | } |
1116 | ||
1a845fbc JK |
1117 | static int match_line(struct grep_opt *opt, |
1118 | const char *bol, const char *eol, | |
68d686e6 | 1119 | ssize_t *col, ssize_t *icol, |
0ab7befa | 1120 | enum grep_context ctx, int collect_hits) |
83b5d2f5 JH |
1121 | { |
1122 | struct grep_pat *p; | |
017c0fcf | 1123 | int hit = 0; |
79212772 | 1124 | |
83b5d2f5 | 1125 | if (opt->extended) |
68d686e6 TB |
1126 | return match_expr(opt, bol, eol, ctx, col, icol, |
1127 | collect_hits); | |
0ab7befa JH |
1128 | |
1129 | /* we do not call with collect_hits without being extended */ | |
83b5d2f5 | 1130 | for (p = opt->pattern_list; p; p = p->next) { |
68d686e6 TB |
1131 | regmatch_t tmp; |
1132 | if (match_one_pattern(p, bol, eol, ctx, &tmp, 0)) { | |
017c0fcf TB |
1133 | hit |= 1; |
1134 | if (!opt->columnnum) { | |
1135 | /* | |
1136 | * Without --column, any single match on a line | |
1137 | * is enough to know that it needs to be | |
1138 | * printed. With --column, scan _all_ patterns | |
1139 | * to find the earliest. | |
1140 | */ | |
1141 | break; | |
1142 | } | |
1143 | if (*col < 0 || tmp.rm_so < *col) | |
1144 | *col = tmp.rm_so; | |
68d686e6 | 1145 | } |
83b5d2f5 | 1146 | } |
017c0fcf | 1147 | return hit; |
83b5d2f5 JH |
1148 | } |
1149 | ||
1a845fbc JK |
1150 | static int match_next_pattern(struct grep_pat *p, |
1151 | const char *bol, const char *eol, | |
7e8f59d5 RS |
1152 | enum grep_context ctx, |
1153 | regmatch_t *pmatch, int eflags) | |
1154 | { | |
1155 | regmatch_t match; | |
1156 | ||
3f566c4e | 1157 | if (!headerless_match_one_pattern(p, bol, eol, ctx, &match, eflags)) |
7e8f59d5 RS |
1158 | return 0; |
1159 | if (match.rm_so < 0 || match.rm_eo < 0) | |
1160 | return 0; | |
1161 | if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) { | |
1162 | if (match.rm_so > pmatch->rm_so) | |
1163 | return 1; | |
1164 | if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo) | |
1165 | return 1; | |
1166 | } | |
1167 | pmatch->rm_so = match.rm_so; | |
1168 | pmatch->rm_eo = match.rm_eo; | |
1169 | return 1; | |
1170 | } | |
1171 | ||
3f566c4e HM |
1172 | int grep_next_match(struct grep_opt *opt, |
1173 | const char *bol, const char *eol, | |
1174 | enum grep_context ctx, regmatch_t *pmatch, | |
1175 | enum grep_header_field field, int eflags) | |
7e8f59d5 RS |
1176 | { |
1177 | struct grep_pat *p; | |
1178 | int hit = 0; | |
1179 | ||
1180 | pmatch->rm_so = pmatch->rm_eo = -1; | |
1181 | if (bol < eol) { | |
3f566c4e HM |
1182 | for (p = ((ctx == GREP_CONTEXT_HEAD) |
1183 | ? opt->header_list : opt->pattern_list); | |
1184 | p; p = p->next) { | |
7e8f59d5 | 1185 | switch (p->token) { |
7e8f59d5 | 1186 | case GREP_PATTERN_HEAD: |
3f566c4e HM |
1187 | if ((field != GREP_HEADER_FIELD_MAX) && |
1188 | (p->field != field)) | |
1189 | continue; | |
1190 | /* fall thru */ | |
1191 | case GREP_PATTERN: /* atom */ | |
7e8f59d5 RS |
1192 | case GREP_PATTERN_BODY: |
1193 | hit |= match_next_pattern(p, bol, eol, ctx, | |
1194 | pmatch, eflags); | |
1195 | break; | |
1196 | default: | |
1197 | break; | |
1198 | } | |
1199 | } | |
1200 | } | |
1201 | return hit; | |
1202 | } | |
1203 | ||
c707ded3 TB |
1204 | static void show_line_header(struct grep_opt *opt, const char *name, |
1205 | unsigned lno, ssize_t cno, char sign) | |
7e8f59d5 | 1206 | { |
1d84f72e | 1207 | if (opt->heading && opt->last_shown == 0) { |
fa151dc5 | 1208 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
1d84f72e RS |
1209 | opt->output(opt, "\n", 1); |
1210 | } | |
5dd06d38 RS |
1211 | opt->last_shown = lno; |
1212 | ||
1d84f72e | 1213 | if (!opt->heading && opt->pathname) { |
fa151dc5 | 1214 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
55f638bd | 1215 | output_sep(opt, sign); |
5b594f45 FK |
1216 | } |
1217 | if (opt->linenum) { | |
1218 | char buf[32]; | |
1a168e5c | 1219 | xsnprintf(buf, sizeof(buf), "%d", lno); |
fa151dc5 | 1220 | output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]); |
55f638bd | 1221 | output_sep(opt, sign); |
5b594f45 | 1222 | } |
89252cd0 TB |
1223 | /* |
1224 | * Treat 'cno' as the 1-indexed offset from the start of a non-context | |
1225 | * line to its first match. Otherwise, 'cno' is 0 indicating that we are | |
1226 | * being called with a context line. | |
1227 | */ | |
1228 | if (opt->columnnum && cno) { | |
1229 | char buf[32]; | |
1230 | xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno); | |
d036d667 | 1231 | output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_COLUMNNO]); |
89252cd0 TB |
1232 | output_sep(opt, sign); |
1233 | } | |
c707ded3 TB |
1234 | } |
1235 | ||
1a845fbc JK |
1236 | static void show_line(struct grep_opt *opt, |
1237 | const char *bol, const char *eol, | |
c707ded3 TB |
1238 | const char *name, unsigned lno, ssize_t cno, char sign) |
1239 | { | |
1240 | int rest = eol - bol; | |
9d8db06e TB |
1241 | const char *match_color = NULL; |
1242 | const char *line_color = NULL; | |
c707ded3 TB |
1243 | |
1244 | if (opt->file_break && opt->last_shown == 0) { | |
1245 | if (opt->show_hunk_mark) | |
1246 | opt->output(opt, "\n", 1); | |
1247 | } else if (opt->pre_context || opt->post_context || opt->funcbody) { | |
1248 | if (opt->last_shown == 0) { | |
1249 | if (opt->show_hunk_mark) { | |
87ece7ce | 1250 | output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); |
c707ded3 TB |
1251 | opt->output(opt, "\n", 1); |
1252 | } | |
1253 | } else if (lno > opt->last_shown + 1) { | |
87ece7ce | 1254 | output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); |
c707ded3 TB |
1255 | opt->output(opt, "\n", 1); |
1256 | } | |
1257 | } | |
9d8db06e TB |
1258 | if (!opt->only_matching) { |
1259 | /* | |
1260 | * In case the line we're being called with contains more than | |
1261 | * one match, leave printing each header to the loop below. | |
1262 | */ | |
1263 | show_line_header(opt, name, lno, cno, sign); | |
1264 | } | |
1265 | if (opt->color || opt->only_matching) { | |
7e8f59d5 RS |
1266 | regmatch_t match; |
1267 | enum grep_context ctx = GREP_CONTEXT_BODY; | |
7e8f59d5 RS |
1268 | int eflags = 0; |
1269 | ||
9d8db06e TB |
1270 | if (opt->color) { |
1271 | if (sign == ':') | |
87ece7ce | 1272 | match_color = opt->colors[GREP_COLOR_MATCH_SELECTED]; |
9d8db06e | 1273 | else |
87ece7ce | 1274 | match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT]; |
9d8db06e | 1275 | if (sign == ':') |
87ece7ce | 1276 | line_color = opt->colors[GREP_COLOR_SELECTED]; |
9d8db06e | 1277 | else if (sign == '-') |
87ece7ce | 1278 | line_color = opt->colors[GREP_COLOR_CONTEXT]; |
9d8db06e | 1279 | else if (sign == '=') |
87ece7ce | 1280 | line_color = opt->colors[GREP_COLOR_FUNCTION]; |
9d8db06e | 1281 | } |
3f566c4e HM |
1282 | while (grep_next_match(opt, bol, eol, ctx, &match, |
1283 | GREP_HEADER_FIELD_MAX, eflags)) { | |
1f5b9cc4 RS |
1284 | if (match.rm_so == match.rm_eo) |
1285 | break; | |
5b594f45 | 1286 | |
9d8db06e TB |
1287 | if (opt->only_matching) |
1288 | show_line_header(opt, name, lno, cno, sign); | |
1289 | else | |
1290 | output_color(opt, bol, match.rm_so, line_color); | |
55f638bd | 1291 | output_color(opt, bol + match.rm_so, |
79a77109 | 1292 | match.rm_eo - match.rm_so, match_color); |
9d8db06e TB |
1293 | if (opt->only_matching) |
1294 | opt->output(opt, "\n", 1); | |
7e8f59d5 | 1295 | bol += match.rm_eo; |
9d8db06e | 1296 | cno += match.rm_eo; |
7e8f59d5 RS |
1297 | rest -= match.rm_eo; |
1298 | eflags = REG_NOTBOL; | |
1299 | } | |
7e8f59d5 | 1300 | } |
9d8db06e TB |
1301 | if (!opt->only_matching) { |
1302 | output_color(opt, bol, rest, line_color); | |
1303 | opt->output(opt, "\n", 1); | |
1304 | } | |
7e8f59d5 RS |
1305 | } |
1306 | ||
78db6ea9 JK |
1307 | int grep_use_locks; |
1308 | ||
0579f91d TR |
1309 | /* |
1310 | * This lock protects access to the gitattributes machinery, which is | |
1311 | * not thread-safe. | |
1312 | */ | |
1313 | pthread_mutex_t grep_attr_mutex; | |
1314 | ||
78db6ea9 | 1315 | static inline void grep_attr_lock(void) |
0579f91d | 1316 | { |
78db6ea9 | 1317 | if (grep_use_locks) |
0579f91d TR |
1318 | pthread_mutex_lock(&grep_attr_mutex); |
1319 | } | |
1320 | ||
78db6ea9 | 1321 | static inline void grep_attr_unlock(void) |
0579f91d | 1322 | { |
78db6ea9 | 1323 | if (grep_use_locks) |
0579f91d TR |
1324 | pthread_mutex_unlock(&grep_attr_mutex); |
1325 | } | |
b3aeb285 | 1326 | |
1a845fbc JK |
1327 | static int match_funcname(struct grep_opt *opt, struct grep_source *gs, |
1328 | const char *bol, const char *eol) | |
2944e4e6 | 1329 | { |
60ecac98 | 1330 | xdemitconf_t *xecfg = opt->priv; |
0579f91d | 1331 | if (xecfg && !xecfg->find_func) { |
acd00ea0 | 1332 | grep_source_load_driver(gs, opt->repo->index); |
94ad9d9e JK |
1333 | if (gs->driver->funcname.pattern) { |
1334 | const struct userdiff_funcname *pe = &gs->driver->funcname; | |
0579f91d TR |
1335 | xdiff_set_find_func(xecfg, pe->pattern, pe->cflags); |
1336 | } else { | |
1337 | xecfg = opt->priv = NULL; | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | if (xecfg) { | |
60ecac98 RS |
1342 | char buf[1]; |
1343 | return xecfg->find_func(bol, eol - bol, buf, 1, | |
1344 | xecfg->find_func_priv) >= 0; | |
1345 | } | |
1346 | ||
2944e4e6 RS |
1347 | if (bol == eol) |
1348 | return 0; | |
1349 | if (isalpha(*bol) || *bol == '_' || *bol == '$') | |
1350 | return 1; | |
1351 | return 0; | |
1352 | } | |
1353 | ||
e1327023 | 1354 | static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs, |
1a845fbc | 1355 | const char *bol, unsigned lno) |
2944e4e6 | 1356 | { |
e1327023 | 1357 | while (bol > gs->buf) { |
1a845fbc | 1358 | const char *eol = --bol; |
2944e4e6 | 1359 | |
e1327023 | 1360 | while (bol > gs->buf && bol[-1] != '\n') |
2944e4e6 RS |
1361 | bol--; |
1362 | lno--; | |
1363 | ||
1364 | if (lno <= opt->last_shown) | |
1365 | break; | |
1366 | ||
e1327023 | 1367 | if (match_funcname(opt, gs, bol, eol)) { |
89252cd0 | 1368 | show_line(opt, bol, eol, gs->name, lno, 0, '='); |
2944e4e6 RS |
1369 | break; |
1370 | } | |
1371 | } | |
1372 | } | |
1373 | ||
a5dc20b0 RS |
1374 | static int is_empty_line(const char *bol, const char *eol); |
1375 | ||
e1327023 | 1376 | static void show_pre_context(struct grep_opt *opt, struct grep_source *gs, |
1a845fbc | 1377 | const char *bol, const char *end, unsigned lno) |
49de3216 | 1378 | { |
6653a01b | 1379 | unsigned cur = lno, from = 1, funcname_lno = 0, orig_from; |
a5dc20b0 | 1380 | int funcname_needed = !!opt->funcname, comment_needed = 0; |
ba8ea749 | 1381 | |
49de3216 RS |
1382 | if (opt->pre_context < lno) |
1383 | from = lno - opt->pre_context; | |
1384 | if (from <= opt->last_shown) | |
1385 | from = opt->last_shown + 1; | |
6653a01b | 1386 | orig_from = from; |
a5dc20b0 RS |
1387 | if (opt->funcbody) { |
1388 | if (match_funcname(opt, gs, bol, end)) | |
1389 | comment_needed = 1; | |
1390 | else | |
1391 | funcname_needed = 1; | |
6653a01b RS |
1392 | from = opt->last_shown + 1; |
1393 | } | |
49de3216 RS |
1394 | |
1395 | /* Rewind. */ | |
6653a01b | 1396 | while (bol > gs->buf && cur > from) { |
1a845fbc JK |
1397 | const char *next_bol = bol; |
1398 | const char *eol = --bol; | |
2944e4e6 | 1399 | |
e1327023 | 1400 | while (bol > gs->buf && bol[-1] != '\n') |
49de3216 RS |
1401 | bol--; |
1402 | cur--; | |
a5dc20b0 RS |
1403 | if (comment_needed && (is_empty_line(bol, eol) || |
1404 | match_funcname(opt, gs, bol, eol))) { | |
1405 | comment_needed = 0; | |
1406 | from = orig_from; | |
1407 | if (cur < from) { | |
1408 | cur++; | |
1409 | bol = next_bol; | |
1410 | break; | |
1411 | } | |
1412 | } | |
e1327023 | 1413 | if (funcname_needed && match_funcname(opt, gs, bol, eol)) { |
2944e4e6 RS |
1414 | funcname_lno = cur; |
1415 | funcname_needed = 0; | |
a5dc20b0 RS |
1416 | if (opt->funcbody) |
1417 | comment_needed = 1; | |
1418 | else | |
1419 | from = orig_from; | |
2944e4e6 | 1420 | } |
49de3216 RS |
1421 | } |
1422 | ||
2944e4e6 RS |
1423 | /* We need to look even further back to find a function signature. */ |
1424 | if (opt->funcname && funcname_needed) | |
e1327023 | 1425 | show_funcname_line(opt, gs, bol, cur); |
2944e4e6 | 1426 | |
49de3216 RS |
1427 | /* Back forward. */ |
1428 | while (cur < lno) { | |
1a845fbc | 1429 | const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-'; |
49de3216 RS |
1430 | |
1431 | while (*eol != '\n') | |
1432 | eol++; | |
89252cd0 | 1433 | show_line(opt, bol, eol, gs->name, cur, 0, sign); |
49de3216 RS |
1434 | bol = eol + 1; |
1435 | cur++; | |
1436 | } | |
1437 | } | |
1438 | ||
a26345b6 JH |
1439 | static int should_lookahead(struct grep_opt *opt) |
1440 | { | |
1441 | struct grep_pat *p; | |
1442 | ||
1443 | if (opt->extended) | |
1444 | return 0; /* punt for too complex stuff */ | |
1445 | if (opt->invert) | |
1446 | return 0; | |
1447 | for (p = opt->pattern_list; p; p = p->next) { | |
1448 | if (p->token != GREP_PATTERN) | |
1449 | return 0; /* punt for "header only" and stuff */ | |
1450 | } | |
1451 | return 1; | |
1452 | } | |
1453 | ||
1454 | static int look_ahead(struct grep_opt *opt, | |
1455 | unsigned long *left_p, | |
1456 | unsigned *lno_p, | |
1a845fbc | 1457 | const char **bol_p) |
a26345b6 JH |
1458 | { |
1459 | unsigned lno = *lno_p; | |
1a845fbc | 1460 | const char *bol = *bol_p; |
a26345b6 | 1461 | struct grep_pat *p; |
1a845fbc | 1462 | const char *sp, *last_bol; |
a26345b6 JH |
1463 | regoff_t earliest = -1; |
1464 | ||
1465 | for (p = opt->pattern_list; p; p = p->next) { | |
1466 | int hit; | |
1467 | regmatch_t m; | |
1468 | ||
97e77784 | 1469 | hit = patmatch(p, bol, bol + *left_p, &m, 0); |
a26345b6 JH |
1470 | if (!hit || m.rm_so < 0 || m.rm_eo < 0) |
1471 | continue; | |
1472 | if (earliest < 0 || m.rm_so < earliest) | |
1473 | earliest = m.rm_so; | |
1474 | } | |
1475 | ||
1476 | if (earliest < 0) { | |
1477 | *bol_p = bol + *left_p; | |
1478 | *left_p = 0; | |
1479 | return 1; | |
1480 | } | |
1481 | for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--) | |
1482 | ; /* find the beginning of the line */ | |
1483 | last_bol = sp; | |
1484 | ||
1485 | for (sp = bol; sp < last_bol; sp++) { | |
1486 | if (*sp == '\n') | |
1487 | lno++; | |
1488 | } | |
1489 | *left_p -= last_bol - bol; | |
1490 | *bol_p = last_bol; | |
1491 | *lno_p = lno; | |
1492 | return 0; | |
1493 | } | |
1494 | ||
38bbc2ea NTND |
1495 | static int fill_textconv_grep(struct repository *r, |
1496 | struct userdiff_driver *driver, | |
335ec3bf JK |
1497 | struct grep_source *gs) |
1498 | { | |
1499 | struct diff_filespec *df; | |
1500 | char *buf; | |
1501 | size_t size; | |
1502 | ||
1503 | if (!driver || !driver->textconv) | |
1504 | return grep_source_load(gs); | |
1505 | ||
1506 | /* | |
1507 | * The textconv interface is intimately tied to diff_filespecs, so we | |
1508 | * have to pretend to be one. If we could unify the grep_source | |
1509 | * and diff_filespec structs, this mess could just go away. | |
1510 | */ | |
1511 | df = alloc_filespec(gs->path); | |
1512 | switch (gs->type) { | |
1c41c82b | 1513 | case GREP_SOURCE_OID: |
335ec3bf JK |
1514 | fill_filespec(df, gs->identifier, 1, 0100644); |
1515 | break; | |
1516 | case GREP_SOURCE_FILE: | |
14228447 | 1517 | fill_filespec(df, null_oid(), 0, 0100644); |
335ec3bf JK |
1518 | break; |
1519 | default: | |
033abf97 | 1520 | BUG("attempt to textconv something without a path?"); |
335ec3bf JK |
1521 | } |
1522 | ||
1523 | /* | |
1d1729ca MT |
1524 | * fill_textconv is not remotely thread-safe; it modifies the global |
1525 | * diff tempfile structure, writes to the_repo's odb and might | |
1526 | * internally call thread-unsafe functions such as the | |
1527 | * prepare_packed_git() lazy-initializator. Because of the last two, we | |
1528 | * must ensure mutual exclusion between this call and the object reading | |
1529 | * API, thus we use obj_read_lock() here. | |
1530 | * | |
1531 | * TODO: allowing text conversion to run in parallel with object | |
1532 | * reading operations might increase performance in the multithreaded | |
1533 | * non-worktreee git-grep with --textconv. | |
335ec3bf | 1534 | */ |
1d1729ca | 1535 | obj_read_lock(); |
38bbc2ea | 1536 | size = fill_textconv(r, driver, df, &buf); |
1d1729ca | 1537 | obj_read_unlock(); |
335ec3bf JK |
1538 | free_filespec(df); |
1539 | ||
1540 | /* | |
1541 | * The normal fill_textconv usage by the diff machinery would just keep | |
1542 | * the textconv'd buf separate from the diff_filespec. But much of the | |
1543 | * grep code passes around a grep_source and assumes that its "buf" | |
1544 | * pointer is the beginning of the thing we are searching. So let's | |
1545 | * install our textconv'd version into the grep_source, taking care not | |
1546 | * to leak any existing buffer. | |
1547 | */ | |
1548 | grep_source_clear_data(gs); | |
1549 | gs->buf = buf; | |
1550 | gs->size = size; | |
1551 | ||
1552 | return 0; | |
1553 | } | |
1554 | ||
4aa2c475 RS |
1555 | static int is_empty_line(const char *bol, const char *eol) |
1556 | { | |
1557 | while (bol < eol && isspace(*bol)) | |
1558 | bol++; | |
1559 | return bol == eol; | |
1560 | } | |
1561 | ||
e1327023 | 1562 | static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits) |
83b5d2f5 | 1563 | { |
1a845fbc JK |
1564 | const char *bol; |
1565 | const char *peek_bol = NULL; | |
e1327023 | 1566 | unsigned long left; |
83b5d2f5 | 1567 | unsigned lno = 1; |
83b5d2f5 | 1568 | unsigned last_hit = 0; |
83b5d2f5 | 1569 | int binary_match_only = 0; |
83b5d2f5 | 1570 | unsigned count = 0; |
a26345b6 | 1571 | int try_lookahead = 0; |
ba8ea749 | 1572 | int show_function = 0; |
335ec3bf | 1573 | struct userdiff_driver *textconv = NULL; |
480c1ca6 | 1574 | enum grep_context ctx = GREP_CONTEXT_HEAD; |
60ecac98 | 1575 | xdemitconf_t xecfg; |
83b5d2f5 | 1576 | |
de99eb0c ES |
1577 | if (!opt->status_only && gs->name == NULL) |
1578 | BUG("grep call which could print a name requires " | |
1579 | "grep_source.name be non-NULL"); | |
1580 | ||
5b594f45 FK |
1581 | if (!opt->output) |
1582 | opt->output = std_output; | |
1583 | ||
ba8ea749 RS |
1584 | if (opt->pre_context || opt->post_context || opt->file_break || |
1585 | opt->funcbody) { | |
08303c36 RS |
1586 | /* Show hunk marks, except for the first file. */ |
1587 | if (opt->last_shown) | |
1588 | opt->show_hunk_mark = 1; | |
1589 | /* | |
1590 | * If we're using threads then we can't easily identify | |
1591 | * the first file. Always put hunk marks in that case | |
1592 | * and skip the very first one later in work_done(). | |
1593 | */ | |
1594 | if (opt->output != std_output) | |
1595 | opt->show_hunk_mark = 1; | |
1596 | } | |
431d6e7b RS |
1597 | opt->last_shown = 0; |
1598 | ||
335ec3bf | 1599 | if (opt->allow_textconv) { |
acd00ea0 | 1600 | grep_source_load_driver(gs, opt->repo->index); |
335ec3bf JK |
1601 | /* |
1602 | * We might set up the shared textconv cache data here, which | |
1d1729ca MT |
1603 | * is not thread-safe. Also, get_oid_with_context() and |
1604 | * parse_object() might be internally called. As they are not | |
84544f2e | 1605 | * currently thread-safe and might be racy with object reading, |
1d1729ca | 1606 | * obj_read_lock() must be called. |
335ec3bf JK |
1607 | */ |
1608 | grep_attr_lock(); | |
1d1729ca | 1609 | obj_read_lock(); |
bd7ad45b | 1610 | textconv = userdiff_get_textconv(opt->repo, gs->driver); |
1d1729ca | 1611 | obj_read_unlock(); |
335ec3bf JK |
1612 | grep_attr_unlock(); |
1613 | } | |
1614 | ||
1615 | /* | |
1616 | * We know the result of a textconv is text, so we only have to care | |
1617 | * about binary handling if we are not using it. | |
1618 | */ | |
1619 | if (!textconv) { | |
1620 | switch (opt->binary) { | |
1621 | case GREP_BINARY_DEFAULT: | |
acd00ea0 | 1622 | if (grep_source_is_binary(gs, opt->repo->index)) |
335ec3bf JK |
1623 | binary_match_only = 1; |
1624 | break; | |
1625 | case GREP_BINARY_NOMATCH: | |
acd00ea0 | 1626 | if (grep_source_is_binary(gs, opt->repo->index)) |
335ec3bf JK |
1627 | return 0; /* Assume unmatch */ |
1628 | break; | |
1629 | case GREP_BINARY_TEXT: | |
1630 | break; | |
1631 | default: | |
033abf97 | 1632 | BUG("unknown binary handling mode"); |
335ec3bf | 1633 | } |
83b5d2f5 JH |
1634 | } |
1635 | ||
60ecac98 | 1636 | memset(&xecfg, 0, sizeof(xecfg)); |
0579f91d TR |
1637 | opt->priv = &xecfg; |
1638 | ||
a26345b6 | 1639 | try_lookahead = should_lookahead(opt); |
60ecac98 | 1640 | |
38bbc2ea | 1641 | if (fill_textconv_grep(opt->repo, textconv, gs) < 0) |
08265798 JK |
1642 | return 0; |
1643 | ||
e1327023 JK |
1644 | bol = gs->buf; |
1645 | left = gs->size; | |
83b5d2f5 | 1646 | while (left) { |
1a845fbc | 1647 | const char *eol; |
0ab7befa | 1648 | int hit; |
89252cd0 | 1649 | ssize_t cno; |
68d686e6 | 1650 | ssize_t col = -1, icol = -1; |
83b5d2f5 | 1651 | |
a26345b6 | 1652 | /* |
8997da38 | 1653 | * look_ahead() skips quickly to the line that possibly |
a26345b6 JH |
1654 | * has the next hit; don't call it if we need to do |
1655 | * something more than just skipping the current line | |
1656 | * in response to an unmatch for the current line. E.g. | |
1657 | * inside a post-context window, we will show the current | |
1658 | * line as a context around the previous hit when it | |
1659 | * doesn't hit. | |
1660 | */ | |
1661 | if (try_lookahead | |
1662 | && !(last_hit | |
ba8ea749 RS |
1663 | && (show_function || |
1664 | lno <= last_hit + opt->post_context)) | |
a26345b6 JH |
1665 | && look_ahead(opt, &left, &lno, &bol)) |
1666 | break; | |
83b5d2f5 | 1667 | eol = end_of_line(bol, &left); |
83b5d2f5 | 1668 | |
480c1ca6 JH |
1669 | if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) |
1670 | ctx = GREP_CONTEXT_BODY; | |
1671 | ||
68d686e6 | 1672 | hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits); |
83b5d2f5 | 1673 | |
0ab7befa JH |
1674 | if (collect_hits) |
1675 | goto next_line; | |
1676 | ||
83b5d2f5 JH |
1677 | /* "grep -v -e foo -e bla" should list lines |
1678 | * that do not have either, so inversion should | |
1679 | * be done outside. | |
1680 | */ | |
1681 | if (opt->invert) | |
1682 | hit = !hit; | |
1683 | if (opt->unmatch_name_only) { | |
1684 | if (hit) | |
1685 | return 0; | |
1686 | goto next_line; | |
1687 | } | |
1688 | if (hit) { | |
1689 | count++; | |
1690 | if (opt->status_only) | |
1691 | return 1; | |
321ffcc0 | 1692 | if (opt->name_only) { |
e1327023 | 1693 | show_name(opt, gs->name); |
321ffcc0 RS |
1694 | return 1; |
1695 | } | |
c30c10cf RS |
1696 | if (opt->count) |
1697 | goto next_line; | |
83b5d2f5 | 1698 | if (binary_match_only) { |
5b594f45 | 1699 | opt->output(opt, "Binary file ", 12); |
e1327023 | 1700 | output_color(opt, gs->name, strlen(gs->name), |
fa151dc5 | 1701 | opt->colors[GREP_COLOR_FILENAME]); |
5b594f45 | 1702 | opt->output(opt, " matches\n", 9); |
83b5d2f5 JH |
1703 | return 1; |
1704 | } | |
83b5d2f5 JH |
1705 | /* Hit at this line. If we haven't shown the |
1706 | * pre-context lines, we would need to show them. | |
83b5d2f5 | 1707 | */ |
ba8ea749 | 1708 | if (opt->pre_context || opt->funcbody) |
e1327023 | 1709 | show_pre_context(opt, gs, bol, eol, lno); |
2944e4e6 | 1710 | else if (opt->funcname) |
e1327023 | 1711 | show_funcname_line(opt, gs, bol, lno); |
89252cd0 TB |
1712 | cno = opt->invert ? icol : col; |
1713 | if (cno < 0) { | |
1714 | /* | |
1715 | * A negative cno indicates that there was no | |
1716 | * match on the line. We are thus inverted and | |
1717 | * being asked to show all lines that _don't_ | |
1718 | * match a given expression. Therefore, set cno | |
1719 | * to 0 to suggest the whole line matches. | |
1720 | */ | |
1721 | cno = 0; | |
1722 | } | |
1723 | show_line(opt, bol, eol, gs->name, lno, cno + 1, ':'); | |
5dd06d38 | 1724 | last_hit = lno; |
ba8ea749 RS |
1725 | if (opt->funcbody) |
1726 | show_function = 1; | |
1727 | goto next_line; | |
83b5d2f5 | 1728 | } |
4aa2c475 RS |
1729 | if (show_function && (!peek_bol || peek_bol < bol)) { |
1730 | unsigned long peek_left = left; | |
1a845fbc | 1731 | const char *peek_eol = eol; |
4aa2c475 RS |
1732 | |
1733 | /* | |
1734 | * Trailing empty lines are not interesting. | |
1735 | * Peek past them to see if they belong to the | |
1736 | * body of the current function. | |
1737 | */ | |
1738 | peek_bol = bol; | |
1739 | while (is_empty_line(peek_bol, peek_eol)) { | |
1740 | peek_bol = peek_eol + 1; | |
1741 | peek_eol = end_of_line(peek_bol, &peek_left); | |
1742 | } | |
1743 | ||
1744 | if (match_funcname(opt, gs, peek_bol, peek_eol)) | |
1745 | show_function = 0; | |
1746 | } | |
ba8ea749 RS |
1747 | if (show_function || |
1748 | (last_hit && lno <= last_hit + opt->post_context)) { | |
83b5d2f5 JH |
1749 | /* If the last hit is within the post context, |
1750 | * we need to show this line. | |
1751 | */ | |
89252cd0 | 1752 | show_line(opt, bol, eol, gs->name, lno, col + 1, '-'); |
83b5d2f5 | 1753 | } |
83b5d2f5 JH |
1754 | |
1755 | next_line: | |
1756 | bol = eol + 1; | |
1757 | if (!left) | |
1758 | break; | |
1759 | left--; | |
1760 | lno++; | |
1761 | } | |
1762 | ||
0ab7befa JH |
1763 | if (collect_hits) |
1764 | return 0; | |
b48fb5b6 | 1765 | |
83b5d2f5 | 1766 | if (opt->status_only) |
e1f68c66 | 1767 | return opt->unmatch_name_only; |
83b5d2f5 JH |
1768 | if (opt->unmatch_name_only) { |
1769 | /* We did not see any hit, so we want to show this */ | |
e1327023 | 1770 | show_name(opt, gs->name); |
83b5d2f5 JH |
1771 | return 1; |
1772 | } | |
1773 | ||
60ecac98 RS |
1774 | xdiff_clear_find_func(&xecfg); |
1775 | opt->priv = NULL; | |
1776 | ||
83b5d2f5 JH |
1777 | /* NEEDSWORK: |
1778 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, | |
1779 | * which feels mostly useless but sometimes useful. Maybe | |
1780 | * make it another option? For now suppress them. | |
1781 | */ | |
5b594f45 FK |
1782 | if (opt->count && count) { |
1783 | char buf[32]; | |
f76d947a RS |
1784 | if (opt->pathname) { |
1785 | output_color(opt, gs->name, strlen(gs->name), | |
fa151dc5 | 1786 | opt->colors[GREP_COLOR_FILENAME]); |
f76d947a RS |
1787 | output_sep(opt, ':'); |
1788 | } | |
1a168e5c | 1789 | xsnprintf(buf, sizeof(buf), "%u\n", count); |
5b594f45 | 1790 | opt->output(opt, buf, strlen(buf)); |
c30c10cf | 1791 | return 1; |
5b594f45 | 1792 | } |
83b5d2f5 JH |
1793 | return !!last_hit; |
1794 | } | |
1795 | ||
0ab7befa JH |
1796 | static void clr_hit_marker(struct grep_expr *x) |
1797 | { | |
1798 | /* All-hit markers are meaningful only at the very top level | |
1799 | * OR node. | |
1800 | */ | |
1801 | while (1) { | |
1802 | x->hit = 0; | |
1803 | if (x->node != GREP_NODE_OR) | |
1804 | return; | |
1805 | x->u.binary.left->hit = 0; | |
1806 | x = x->u.binary.right; | |
1807 | } | |
1808 | } | |
1809 | ||
1810 | static int chk_hit_marker(struct grep_expr *x) | |
1811 | { | |
1812 | /* Top level nodes have hit markers. See if they all are hits */ | |
1813 | while (1) { | |
1814 | if (x->node != GREP_NODE_OR) | |
1815 | return x->hit; | |
1816 | if (!x->u.binary.left->hit) | |
1817 | return 0; | |
1818 | x = x->u.binary.right; | |
1819 | } | |
1820 | } | |
1821 | ||
e1327023 | 1822 | int grep_source(struct grep_opt *opt, struct grep_source *gs) |
0ab7befa JH |
1823 | { |
1824 | /* | |
1825 | * we do not have to do the two-pass grep when we do not check | |
1826 | * buffer-wide "all-match". | |
1827 | */ | |
1828 | if (!opt->all_match) | |
e1327023 | 1829 | return grep_source_1(opt, gs, 0); |
0ab7befa JH |
1830 | |
1831 | /* Otherwise the toplevel "or" terms hit a bit differently. | |
1832 | * We first clear hit markers from them. | |
1833 | */ | |
1834 | clr_hit_marker(opt->pattern_expression); | |
e1327023 | 1835 | grep_source_1(opt, gs, 1); |
0ab7befa JH |
1836 | |
1837 | if (!chk_hit_marker(opt->pattern_expression)) | |
1838 | return 0; | |
1839 | ||
e1327023 JK |
1840 | return grep_source_1(opt, gs, 0); |
1841 | } | |
1842 | ||
1e668716 JK |
1843 | static void grep_source_init_buf(struct grep_source *gs, |
1844 | const char *buf, | |
50d92b5f JT |
1845 | unsigned long size) |
1846 | { | |
1847 | gs->type = GREP_SOURCE_BUF; | |
1848 | gs->name = NULL; | |
1849 | gs->path = NULL; | |
1850 | gs->buf = buf; | |
1851 | gs->size = size; | |
1852 | gs->driver = NULL; | |
1853 | gs->identifier = NULL; | |
1854 | } | |
1855 | ||
1e668716 | 1856 | int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size) |
e1327023 JK |
1857 | { |
1858 | struct grep_source gs; | |
1859 | int r; | |
1860 | ||
50d92b5f | 1861 | grep_source_init_buf(&gs, buf, size); |
e1327023 JK |
1862 | |
1863 | r = grep_source(opt, &gs); | |
1864 | ||
1865 | grep_source_clear(&gs); | |
1866 | return r; | |
1867 | } | |
1868 | ||
50d92b5f JT |
1869 | void grep_source_init_file(struct grep_source *gs, const char *name, |
1870 | const char *path) | |
e1327023 | 1871 | { |
50d92b5f | 1872 | gs->type = GREP_SOURCE_FILE; |
8c53f071 JK |
1873 | gs->name = xstrdup_or_null(name); |
1874 | gs->path = xstrdup_or_null(path); | |
e1327023 JK |
1875 | gs->buf = NULL; |
1876 | gs->size = 0; | |
94ad9d9e | 1877 | gs->driver = NULL; |
50d92b5f JT |
1878 | gs->identifier = xstrdup(path); |
1879 | } | |
e1327023 | 1880 | |
50d92b5f | 1881 | void grep_source_init_oid(struct grep_source *gs, const char *name, |
0693806b JT |
1882 | const char *path, const struct object_id *oid, |
1883 | struct repository *repo) | |
50d92b5f JT |
1884 | { |
1885 | gs->type = GREP_SOURCE_OID; | |
1886 | gs->name = xstrdup_or_null(name); | |
1887 | gs->path = xstrdup_or_null(path); | |
1888 | gs->buf = NULL; | |
1889 | gs->size = 0; | |
1890 | gs->driver = NULL; | |
1891 | gs->identifier = oiddup(oid); | |
0693806b | 1892 | gs->repo = repo; |
e1327023 JK |
1893 | } |
1894 | ||
1895 | void grep_source_clear(struct grep_source *gs) | |
1896 | { | |
88ce3ef6 ÆAB |
1897 | FREE_AND_NULL(gs->name); |
1898 | FREE_AND_NULL(gs->path); | |
1899 | FREE_AND_NULL(gs->identifier); | |
e1327023 JK |
1900 | grep_source_clear_data(gs); |
1901 | } | |
1902 | ||
1903 | void grep_source_clear_data(struct grep_source *gs) | |
1904 | { | |
1905 | switch (gs->type) { | |
1906 | case GREP_SOURCE_FILE: | |
1c41c82b | 1907 | case GREP_SOURCE_OID: |
1e668716 JK |
1908 | /* these types own the buffer */ |
1909 | free((char *)gs->buf); | |
1910 | gs->buf = NULL; | |
e1327023 JK |
1911 | gs->size = 0; |
1912 | break; | |
1913 | case GREP_SOURCE_BUF: | |
1914 | /* leave user-provided buf intact */ | |
1915 | break; | |
1916 | } | |
1917 | } | |
1918 | ||
1c41c82b | 1919 | static int grep_source_load_oid(struct grep_source *gs) |
e1327023 JK |
1920 | { |
1921 | enum object_type type; | |
1922 | ||
0693806b JT |
1923 | gs->buf = repo_read_object_file(gs->repo, gs->identifier, &type, |
1924 | &gs->size); | |
e1327023 JK |
1925 | if (!gs->buf) |
1926 | return error(_("'%s': unable to read %s"), | |
1927 | gs->name, | |
1c41c82b | 1928 | oid_to_hex(gs->identifier)); |
e1327023 JK |
1929 | return 0; |
1930 | } | |
1931 | ||
1932 | static int grep_source_load_file(struct grep_source *gs) | |
1933 | { | |
1934 | const char *filename = gs->identifier; | |
1935 | struct stat st; | |
1936 | char *data; | |
1937 | size_t size; | |
1938 | int i; | |
1939 | ||
1940 | if (lstat(filename, &st) < 0) { | |
1941 | err_ret: | |
1942 | if (errno != ENOENT) | |
7645d8f1 | 1943 | error_errno(_("failed to stat '%s'"), filename); |
e1327023 JK |
1944 | return -1; |
1945 | } | |
1946 | if (!S_ISREG(st.st_mode)) | |
1947 | return -1; | |
1948 | size = xsize_t(st.st_size); | |
1949 | i = open(filename, O_RDONLY); | |
1950 | if (i < 0) | |
1951 | goto err_ret; | |
3733e694 | 1952 | data = xmallocz(size); |
e1327023 | 1953 | if (st.st_size != read_in_full(i, data, size)) { |
7645d8f1 | 1954 | error_errno(_("'%s': short read"), filename); |
e1327023 JK |
1955 | close(i); |
1956 | free(data); | |
1957 | return -1; | |
1958 | } | |
1959 | close(i); | |
e1327023 JK |
1960 | |
1961 | gs->buf = data; | |
1962 | gs->size = size; | |
1963 | return 0; | |
1964 | } | |
1965 | ||
3083301e | 1966 | static int grep_source_load(struct grep_source *gs) |
e1327023 JK |
1967 | { |
1968 | if (gs->buf) | |
1969 | return 0; | |
1970 | ||
1971 | switch (gs->type) { | |
1972 | case GREP_SOURCE_FILE: | |
1973 | return grep_source_load_file(gs); | |
1c41c82b BW |
1974 | case GREP_SOURCE_OID: |
1975 | return grep_source_load_oid(gs); | |
e1327023 JK |
1976 | case GREP_SOURCE_BUF: |
1977 | return gs->buf ? 0 : -1; | |
1978 | } | |
033abf97 | 1979 | BUG("invalid grep_source type to load"); |
0ab7befa | 1980 | } |
94ad9d9e | 1981 | |
acd00ea0 NTND |
1982 | void grep_source_load_driver(struct grep_source *gs, |
1983 | struct index_state *istate) | |
94ad9d9e JK |
1984 | { |
1985 | if (gs->driver) | |
1986 | return; | |
1987 | ||
1988 | grep_attr_lock(); | |
1d1729ca | 1989 | if (gs->path) |
acd00ea0 | 1990 | gs->driver = userdiff_find_by_path(istate, gs->path); |
94ad9d9e JK |
1991 | if (!gs->driver) |
1992 | gs->driver = userdiff_find_by_name("default"); | |
1993 | grep_attr_unlock(); | |
1994 | } | |
41b59bfc | 1995 | |
acd00ea0 NTND |
1996 | static int grep_source_is_binary(struct grep_source *gs, |
1997 | struct index_state *istate) | |
41b59bfc | 1998 | { |
acd00ea0 | 1999 | grep_source_load_driver(gs, istate); |
41b59bfc JK |
2000 | if (gs->driver->binary != -1) |
2001 | return gs->driver->binary; | |
2002 | ||
2003 | if (!grep_source_load(gs)) | |
2004 | return buffer_is_binary(gs->buf, gs->size); | |
2005 | ||
2006 | return 0; | |
2007 | } |