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