l10n: po-id for 2.34 (round 2)
[git/debian.git] / grep.c
blobf6e113e9f0fd52bc40c08613919eade24020d8da
1 #include "cache.h"
2 #include "config.h"
3 #include "grep.h"
4 #include "object-store.h"
5 #include "userdiff.h"
6 #include "xdiff-interface.h"
7 #include "diff.h"
8 #include "diffcore.h"
9 #include "commit.h"
10 #include "quote.h"
11 #include "help.h"
13 static int grep_source_load(struct grep_source *gs);
14 static int grep_source_is_binary(struct grep_source *gs,
15 struct index_state *istate);
17 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
19 fwrite(buf, size, 1, stdout);
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,
38 .only_matching = 0,
39 .color = -1,
40 .output = std_output,
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",
48 [GREP_COLOR_COLUMNNO] = "column",
49 [GREP_COLOR_MATCH_CONTEXT] = "matchContext",
50 [GREP_COLOR_MATCH_SELECTED] = "matchSelected",
51 [GREP_COLOR_SELECTED] = "selected",
52 [GREP_COLOR_SEP] = "separator",
55 static int parse_pattern_type_arg(const char *opt, const char *arg)
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);
70 define_list_config_array_extra(color_grep_slots, {"match"});
73 * Read the configuration file once and store it in
74 * the grep_defaults template.
76 int grep_config(const char *var, const char *value, void *cb)
78 struct grep_opt *opt = &grep_defaults;
79 const char *slot;
81 if (userdiff_config(var, value) < 0)
82 return -1;
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.
92 if (!strcmp(var, "grep.extendedregexp")) {
93 opt->extended_regexp_option = git_config_bool(var, value);
94 return 0;
97 if (!strcmp(var, "grep.patterntype")) {
98 opt->pattern_type_option = parse_pattern_type_arg(var, value);
99 return 0;
102 if (!strcmp(var, "grep.linenumber")) {
103 opt->linenum = git_config_bool(var, value);
104 return 0;
106 if (!strcmp(var, "grep.column")) {
107 opt->columnnum = git_config_bool(var, value);
108 return 0;
111 if (!strcmp(var, "grep.fullname")) {
112 opt->relative = !git_config_bool(var, value);
113 return 0;
116 if (!strcmp(var, "color.grep"))
117 opt->color = git_config_colorbool(var, value);
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;
127 if (i < 0)
128 return -1;
129 color = opt->colors[i];
130 if (!value)
131 return config_error_nonbool(var);
132 return color_parse(value, color);
134 return 0;
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).
142 void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
144 *opt = grep_defaults;
146 opt->repo = repo;
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;
153 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
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.
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.
169 if (pattern_type != GREP_PATTERN_TYPE_ERE &&
170 opt->extended_regexp_option)
171 opt->extended_regexp_option = 0;
173 switch (pattern_type) {
174 case GREP_PATTERN_TYPE_UNSPECIFIED:
175 /* fall through */
177 case GREP_PATTERN_TYPE_BRE:
178 break;
180 case GREP_PATTERN_TYPE_ERE:
181 opt->extended_regexp_option = 1;
182 break;
184 case GREP_PATTERN_TYPE_FIXED:
185 opt->fixed = 1;
186 break;
188 case GREP_PATTERN_TYPE_PCRE:
189 opt->pcre2 = 1;
190 break;
194 void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
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)
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!
206 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
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)
214 struct grep_pat *p = xcalloc(1, sizeof(*p));
215 p->pattern = xmemdupz(pat, patlen);
216 p->patternlen = patlen;
217 p->origin = origin;
218 p->no = no;
219 p->token = t;
220 p->field = field;
221 return p;
224 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
226 **tail = p;
227 *tail = &p->next;
228 p->next = NULL;
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;
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;
255 break;
256 default:
257 break;
261 void append_header_grep_pattern(struct grep_opt *opt,
262 enum grep_header_field field, const char *pat)
264 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
265 GREP_PATTERN_HEAD, field);
266 if (field == GREP_HEADER_REFLOG)
267 opt->use_reflog_filter = 1;
268 do_append_grep_pat(&opt->header_tail, p);
271 void append_grep_pattern(struct grep_opt *opt, const char *pat,
272 const char *origin, int no, enum grep_pat_token t)
274 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
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)
280 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
281 do_append_grep_pat(&opt->pattern_tail, p);
284 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
286 struct grep_pat *pat;
287 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
288 *ret = *opt;
290 ret->pattern_list = NULL;
291 ret->pattern_tail = &ret->pattern_list;
293 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
295 if(pat->token == GREP_PATTERN_HEAD)
296 append_header_grep_pattern(ret, pat->field,
297 pat->pattern);
298 else
299 append_grep_pat(ret, pat->pattern, pat->patternlen,
300 pat->origin, pat->no, pat->token);
303 return ret;
306 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
307 const char *error)
309 char where[1024];
311 if (p->no)
312 xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no);
313 else if (p->origin)
314 xsnprintf(where, sizeof(where), "%s, ", p->origin);
315 else
316 where[0] = 0;
318 die("%s'%s': %s", where, p->pattern, error);
321 static int is_fixed(const char *s, size_t len)
323 size_t i;
325 for (i = 0; i < len; i++) {
326 if (is_regex_special(s[i]))
327 return 0;
330 return 1;
333 #ifdef USE_LIBPCRE2
334 #define GREP_PCRE2_DEBUG_MALLOC 0
336 static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
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;
346 static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
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);
356 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
358 int error;
359 PCRE2_UCHAR errbuf[256];
360 PCRE2_SIZE erroffset;
361 int options = PCRE2_MULTILINE;
362 int jitret;
363 int patinforet;
364 size_t jitsizearg;
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.
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");
376 if (opt->ignore_case) {
377 if (!opt->ignore_locale && has_non_ascii(p->pattern)) {
378 p->pcre2_tables = pcre2_maketables(p->pcre2_general_context);
379 p->pcre2_compile_context = pcre2_compile_context_create(p->pcre2_general_context);
380 pcre2_set_character_tables(p->pcre2_compile_context,
381 p->pcre2_tables);
383 options |= PCRE2_CASELESS;
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))))
389 options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
391 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
392 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
393 if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS))
394 options |= PCRE2_NO_START_OPTIMIZE;
395 #endif
397 p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
398 p->patternlen, options, &error, &erroffset,
399 p->pcre2_compile_context);
401 if (p->pcre2_pattern) {
402 p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, p->pcre2_general_context);
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);
410 pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on);
411 if (p->pcre2_jit_on) {
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);
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.
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)
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;
441 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
442 regmatch_t *match, int eflags)
444 int ret, flags = 0;
445 PCRE2_SIZE *ovector;
446 PCRE2_UCHAR errbuf[256];
448 if (eflags & REG_NOTBOL)
449 flags |= PCRE2_NOTBOL;
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);
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);
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];
473 return ret;
476 static void free_pcre2_pattern(struct grep_pat *p)
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);
481 #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
482 pcre2_maketables_free(p->pcre2_general_context, p->pcre2_tables);
483 #else
484 free((void *)p->pcre2_tables);
485 #endif
486 pcre2_general_context_free(p->pcre2_general_context);
488 #else /* !USE_LIBPCRE2 */
489 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
491 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
494 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
495 regmatch_t *match, int eflags)
497 return 1;
500 static void free_pcre2_pattern(struct grep_pat *p)
504 static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
506 struct strbuf sb = STRBUF_INIT;
507 int err;
508 int regflags = 0;
510 basic_regex_quote_buf(&sb, p->pattern);
511 if (opt->ignore_case)
512 regflags |= REG_ICASE;
513 err = regcomp(&p->regexp, sb.buf, regflags);
514 strbuf_release(&sb);
515 if (err) {
516 char errbuf[1024];
517 regerror(err, &p->regexp, errbuf, sizeof(errbuf));
518 compile_regexp_failed(p, errbuf);
521 #endif /* !USE_LIBPCRE2 */
523 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
525 int err;
526 int regflags = REG_NEWLINE;
528 p->word_regexp = opt->word_regexp;
529 p->ignore_case = opt->ignore_case;
530 p->fixed = opt->fixed;
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"));
535 p->is_fixed = is_fixed(p->pattern, p->patternlen);
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;
545 #endif
546 if (p->fixed || p->is_fixed) {
547 #ifdef USE_LIBPCRE2
548 if (p->is_fixed) {
549 compile_pcre2_pattern(p, opt);
550 } else {
552 * E.g. t7811-grep-open.sh relies on the
553 * pattern being restored.
555 char *old_pattern = p->pattern;
556 size_t old_patternlen = p->patternlen;
557 struct strbuf sb = STRBUF_INIT;
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.
566 strbuf_add(&sb, "\\Q", 2);
567 strbuf_add(&sb, p->pattern, p->patternlen);
568 strbuf_add(&sb, "\\E", 2);
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);
577 #else /* !USE_LIBPCRE2 */
578 compile_fixed_regexp(p, opt);
579 #endif /* !USE_LIBPCRE2 */
580 return;
583 if (opt->pcre2) {
584 compile_pcre2_pattern(p, opt);
585 return;
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);
593 if (err) {
594 char errbuf[1024];
595 regerror(err, &p->regexp, errbuf, 1024);
596 compile_regexp_failed(p, errbuf);
600 static struct grep_expr *compile_pattern_or(struct grep_pat **);
601 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
603 struct grep_pat *p;
604 struct grep_expr *x;
606 p = *list;
607 if (!p)
608 return NULL;
609 switch (p->token) {
610 case GREP_PATTERN: /* atom */
611 case GREP_PATTERN_HEAD:
612 case GREP_PATTERN_BODY:
613 CALLOC_ARRAY(x, 1);
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;
620 x = compile_pattern_or(list);
621 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
622 die("unmatched parenthesis");
623 *list = (*list)->next;
624 return x;
625 default:
626 return NULL;
630 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
632 struct grep_pat *p;
633 struct grep_expr *x;
635 p = *list;
636 if (!p)
637 return NULL;
638 switch (p->token) {
639 case GREP_NOT:
640 if (!p->next)
641 die("--not not followed by pattern expression");
642 *list = p->next;
643 CALLOC_ARRAY(x, 1);
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);
654 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
656 struct grep_pat *p;
657 struct grep_expr *x, *y, *z;
659 x = compile_pattern_not(list);
660 p = *list;
661 if (p && p->token == GREP_AND) {
662 if (!x)
663 die("--and not preceded by pattern expression");
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");
670 CALLOC_ARRAY(z, 1);
671 z->node = GREP_NODE_AND;
672 z->u.binary.left = x;
673 z->u.binary.right = y;
674 return z;
676 return x;
679 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
681 struct grep_pat *p;
682 struct grep_expr *x, *y, *z;
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);
690 CALLOC_ARRAY(z, 1);
691 z->node = GREP_NODE_OR;
692 z->u.binary.left = x;
693 z->u.binary.right = y;
694 return z;
696 return x;
699 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
701 return compile_pattern_or(list);
704 static struct grep_expr *grep_true_expr(void)
706 struct grep_expr *z = xcalloc(1, sizeof(*z));
707 z->node = GREP_NODE_TRUE;
708 return z;
711 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
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;
720 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
722 struct grep_pat *p;
723 struct grep_expr *header_expr;
724 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
725 enum grep_header_field fld;
727 if (!opt->header_list)
728 return NULL;
730 for (p = opt->header_list; p; p = p->next) {
731 if (p->token != GREP_PATTERN_HEAD)
732 BUG("a non-header pattern in grep header list.");
733 if (p->field < GREP_HEADER_FIELD_MIN ||
734 GREP_HEADER_FIELD_MAX <= p->field)
735 BUG("unknown header field %d", p->field);
736 compile_regexp(p, opt);
739 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
740 header_group[fld] = NULL;
742 for (p = opt->header_list; p; p = p->next) {
743 struct grep_expr *h;
744 struct grep_pat *pp = p;
746 h = compile_pattern_atom(&pp);
747 if (!h || pp != p->next)
748 BUG("malformed header expr");
749 if (!header_group[p->field]) {
750 header_group[p->field] = h;
751 continue;
753 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
756 header_expr = NULL;
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);
765 return header_expr;
768 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
770 struct grep_expr *z = x;
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;
779 x = x->u.binary.right;
781 return z;
784 void compile_grep_patterns(struct grep_opt *opt)
786 struct grep_pat *p;
787 struct grep_expr *header_expr = prep_header_patterns(opt);
789 for (p = opt->pattern_list; p; p = p->next) {
790 switch (p->token) {
791 case GREP_PATTERN: /* atom */
792 case GREP_PATTERN_HEAD:
793 case GREP_PATTERN_BODY:
794 compile_regexp(p, opt);
795 break;
796 default:
797 opt->extended = 1;
798 break;
802 if (opt->all_match || header_expr)
803 opt->extended = 1;
804 else if (!opt->extended)
805 return;
807 p = opt->pattern_list;
808 if (p)
809 opt->pattern_expression = compile_pattern_expr(&p);
810 if (p)
811 die("incomplete pattern expression: %s", p->pattern);
813 if (!header_expr)
814 return;
816 if (!opt->pattern_expression)
817 opt->pattern_expression = header_expr;
818 else if (opt->all_match)
819 opt->pattern_expression = grep_splice_or(header_expr,
820 opt->pattern_expression);
821 else
822 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
823 header_expr);
824 opt->all_match = 1;
827 static void free_pattern_expr(struct grep_expr *x)
829 switch (x->node) {
830 case GREP_NODE_TRUE:
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;
842 free(x);
845 void free_grep_patterns(struct grep_opt *opt)
847 struct grep_pat *p, *n;
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:
855 if (p->pcre2_pattern)
856 free_pcre2_pattern(p);
857 else
858 regfree(&p->regexp);
859 free(p->pattern);
860 break;
861 default:
862 break;
864 free(p);
867 if (!opt->extended)
868 return;
869 free_pattern_expr(opt->pattern_expression);
872 static const char *end_of_line(const char *cp, unsigned long *left)
874 unsigned long l = *left;
875 while (l && *cp != '\n') {
876 l--;
877 cp++;
879 *left = l;
880 return cp;
883 static int word_char(char ch)
885 return isalnum(ch) || ch == '_';
888 static void output_color(struct grep_opt *opt, const void *data, size_t size,
889 const char *color)
891 if (want_color(opt->color) && color && color[0]) {
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);
899 static void output_sep(struct grep_opt *opt, char sign)
901 if (opt->null_following_name)
902 opt->output(opt, "\0", 1);
903 else
904 output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]);
907 static void show_name(struct grep_opt *opt, const char *name)
909 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
910 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
913 static int patmatch(struct grep_pat *p,
914 const char *line, const char *eol,
915 regmatch_t *match, int eflags)
917 int hit;
919 if (p->pcre2_pattern)
920 hit = !pcre2match(p, line, eol, match, eflags);
921 else
922 hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
923 eflags);
925 return hit;
928 static void strip_timestamp(const char *bol, const char **eol_p)
930 const char *eol = *eol_p;
932 while (bol < --eol) {
933 if (*eol != '>')
934 continue;
935 *eol_p = ++eol;
936 break;
940 static struct {
941 const char *field;
942 size_t len;
943 } header_field[] = {
944 { "author ", 7 },
945 { "committer ", 10 },
946 { "reflog ", 7 },
949 static 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)
954 int hit = 0;
955 const char *start = bol;
957 if ((p->token != GREP_PATTERN) &&
958 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
959 return 0;
961 again:
962 hit = patmatch(p, bol, eol, pmatch, eflags);
964 if (hit && p->word_regexp) {
965 if ((pmatch[0].rm_so < 0) ||
966 (eol - bol) < pmatch[0].rm_so ||
967 (pmatch[0].rm_eo < 0) ||
968 (eol - bol) < pmatch[0].rm_eo)
969 die("regexp returned nonsense");
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).
977 if ( ((pmatch[0].rm_so == 0) ||
978 !word_char(bol[pmatch[0].rm_so-1])) &&
979 ((pmatch[0].rm_eo == (eol-bol)) ||
980 !word_char(bol[pmatch[0].rm_eo])) )
982 else
983 hit = 0;
985 /* Words consist of at least one character. */
986 if (pmatch->rm_so == pmatch->rm_eo)
987 hit = 0;
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!
993 * Forward to the next possible start, i.e. the
994 * next position following a non-word char.
996 bol = pmatch[0].rm_so + bol + 1;
997 while (word_char(bol[-1]) && bol < eol)
998 bol++;
999 eflags |= REG_NOTBOL;
1000 if (bol < eol)
1001 goto again;
1004 if (hit) {
1005 pmatch[0].rm_so += bol - start;
1006 pmatch[0].rm_eo += bol - start;
1008 return hit;
1011 static 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)
1016 const char *field;
1017 size_t len;
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;
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;
1037 return headerless_match_one_pattern(p, bol, eol, ctx, pmatch, eflags);
1041 static 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,
1044 ssize_t *icol, int collect_hits)
1046 int h = 0;
1048 if (!x)
1049 die("Not a valid grep expression");
1050 switch (x->node) {
1051 case GREP_NODE_TRUE:
1052 h = 1;
1053 break;
1054 case GREP_NODE_ATOM:
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;
1062 break;
1063 case GREP_NODE_NOT:
1065 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1067 h = !match_expr_eval(opt, x->u.unary, bol, eol, ctx, icol, col,
1069 break;
1070 case GREP_NODE_AND:
1071 h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
1072 icol, 0);
1073 if (h || opt->columnnum) {
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.
1079 h &= match_expr_eval(opt, x->u.binary.right, bol, eol,
1080 ctx, col, icol, 0);
1082 break;
1083 case GREP_NODE_OR:
1084 if (!(collect_hits || opt->columnnum)) {
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.
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));
1095 h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
1096 icol, 0);
1097 if (collect_hits)
1098 x->u.binary.left->hit |= h;
1099 h |= match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col,
1100 icol, collect_hits);
1101 break;
1102 default:
1103 die("Unexpected node type (internal error) %d", x->node);
1105 if (collect_hits)
1106 x->hit |= h;
1107 return h;
1110 static int match_expr(struct grep_opt *opt,
1111 const char *bol, const char *eol,
1112 enum grep_context ctx, ssize_t *col,
1113 ssize_t *icol, int collect_hits)
1115 struct grep_expr *x = opt->pattern_expression;
1116 return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits);
1119 static int match_line(struct grep_opt *opt,
1120 const char *bol, const char *eol,
1121 ssize_t *col, ssize_t *icol,
1122 enum grep_context ctx, int collect_hits)
1124 struct grep_pat *p;
1125 int hit = 0;
1127 if (opt->extended)
1128 return match_expr(opt, bol, eol, ctx, col, icol,
1129 collect_hits);
1131 /* we do not call with collect_hits without being extended */
1132 for (p = opt->pattern_list; p; p = p->next) {
1133 regmatch_t tmp;
1134 if (match_one_pattern(p, bol, eol, ctx, &tmp, 0)) {
1135 hit |= 1;
1136 if (!opt->columnnum) {
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.
1143 break;
1145 if (*col < 0 || tmp.rm_so < *col)
1146 *col = tmp.rm_so;
1149 return hit;
1152 static int match_next_pattern(struct grep_pat *p,
1153 const char *bol, const char *eol,
1154 enum grep_context ctx,
1155 regmatch_t *pmatch, int eflags)
1157 regmatch_t match;
1159 if (!headerless_match_one_pattern(p, bol, eol, ctx, &match, eflags))
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;
1169 pmatch->rm_so = match.rm_so;
1170 pmatch->rm_eo = match.rm_eo;
1171 return 1;
1174 int 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)
1179 struct grep_pat *p;
1180 int hit = 0;
1182 pmatch->rm_so = pmatch->rm_eo = -1;
1183 if (bol < eol) {
1184 for (p = ((ctx == GREP_CONTEXT_HEAD)
1185 ? opt->header_list : opt->pattern_list);
1186 p; p = p->next) {
1187 switch (p->token) {
1188 case GREP_PATTERN_HEAD:
1189 if ((field != GREP_HEADER_FIELD_MAX) &&
1190 (p->field != field))
1191 continue;
1192 /* fall thru */
1193 case GREP_PATTERN: /* atom */
1194 case GREP_PATTERN_BODY:
1195 hit |= match_next_pattern(p, bol, eol, ctx,
1196 pmatch, eflags);
1197 break;
1198 default:
1199 break;
1203 return hit;
1206 static void show_line_header(struct grep_opt *opt, const char *name,
1207 unsigned lno, ssize_t cno, char sign)
1209 if (opt->heading && opt->last_shown == 0) {
1210 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1211 opt->output(opt, "\n", 1);
1213 opt->last_shown = lno;
1215 if (!opt->heading && opt->pathname) {
1216 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1217 output_sep(opt, sign);
1219 if (opt->linenum) {
1220 char buf[32];
1221 xsnprintf(buf, sizeof(buf), "%d", lno);
1222 output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]);
1223 output_sep(opt, sign);
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.
1230 if (opt->columnnum && cno) {
1231 char buf[32];
1232 xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno);
1233 output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_COLUMNNO]);
1234 output_sep(opt, sign);
1238 static void show_line(struct grep_opt *opt,
1239 const char *bol, const char *eol,
1240 const char *name, unsigned lno, ssize_t cno, char sign)
1242 int rest = eol - bol;
1243 const char *match_color = NULL;
1244 const char *line_color = NULL;
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) {
1252 output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
1253 opt->output(opt, "\n", 1);
1255 } else if (lno > opt->last_shown + 1) {
1256 output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
1257 opt->output(opt, "\n", 1);
1260 if (!opt->only_matching) {
1262 * In case the line we're being called with contains more than
1263 * one match, leave printing each header to the loop below.
1265 show_line_header(opt, name, lno, cno, sign);
1267 if (opt->color || opt->only_matching) {
1268 regmatch_t match;
1269 enum grep_context ctx = GREP_CONTEXT_BODY;
1270 int eflags = 0;
1272 if (opt->color) {
1273 if (sign == ':')
1274 match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
1275 else
1276 match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT];
1277 if (sign == ':')
1278 line_color = opt->colors[GREP_COLOR_SELECTED];
1279 else if (sign == '-')
1280 line_color = opt->colors[GREP_COLOR_CONTEXT];
1281 else if (sign == '=')
1282 line_color = opt->colors[GREP_COLOR_FUNCTION];
1284 while (grep_next_match(opt, bol, eol, ctx, &match,
1285 GREP_HEADER_FIELD_MAX, eflags)) {
1286 if (match.rm_so == match.rm_eo)
1287 break;
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);
1293 output_color(opt, bol + match.rm_so,
1294 match.rm_eo - match.rm_so, match_color);
1295 if (opt->only_matching)
1296 opt->output(opt, "\n", 1);
1297 bol += match.rm_eo;
1298 cno += match.rm_eo;
1299 rest -= match.rm_eo;
1300 eflags = REG_NOTBOL;
1303 if (!opt->only_matching) {
1304 output_color(opt, bol, rest, line_color);
1305 opt->output(opt, "\n", 1);
1309 int grep_use_locks;
1312 * This lock protects access to the gitattributes machinery, which is
1313 * not thread-safe.
1315 pthread_mutex_t grep_attr_mutex;
1317 static inline void grep_attr_lock(void)
1319 if (grep_use_locks)
1320 pthread_mutex_lock(&grep_attr_mutex);
1323 static inline void grep_attr_unlock(void)
1325 if (grep_use_locks)
1326 pthread_mutex_unlock(&grep_attr_mutex);
1329 static int match_funcname(struct grep_opt *opt, struct grep_source *gs,
1330 const char *bol, const char *eol)
1332 xdemitconf_t *xecfg = opt->priv;
1333 if (xecfg && !xecfg->find_func) {
1334 grep_source_load_driver(gs, opt->repo->index);
1335 if (gs->driver->funcname.pattern) {
1336 const struct userdiff_funcname *pe = &gs->driver->funcname;
1337 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1338 } else {
1339 xecfg = opt->priv = NULL;
1343 if (xecfg) {
1344 char buf[1];
1345 return xecfg->find_func(bol, eol - bol, buf, 1,
1346 xecfg->find_func_priv) >= 0;
1349 if (bol == eol)
1350 return 0;
1351 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1352 return 1;
1353 return 0;
1356 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1357 const char *bol, unsigned lno)
1359 while (bol > gs->buf) {
1360 const char *eol = --bol;
1362 while (bol > gs->buf && bol[-1] != '\n')
1363 bol--;
1364 lno--;
1366 if (lno <= opt->last_shown)
1367 break;
1369 if (match_funcname(opt, gs, bol, eol)) {
1370 show_line(opt, bol, eol, gs->name, lno, 0, '=');
1371 break;
1376 static int is_empty_line(const char *bol, const char *eol);
1378 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1379 const char *bol, const char *end, unsigned lno)
1381 unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
1382 int funcname_needed = !!opt->funcname, comment_needed = 0;
1384 if (opt->pre_context < lno)
1385 from = lno - opt->pre_context;
1386 if (from <= opt->last_shown)
1387 from = opt->last_shown + 1;
1388 orig_from = from;
1389 if (opt->funcbody) {
1390 if (match_funcname(opt, gs, bol, end))
1391 comment_needed = 1;
1392 else
1393 funcname_needed = 1;
1394 from = opt->last_shown + 1;
1397 /* Rewind. */
1398 while (bol > gs->buf && cur > from) {
1399 const char *next_bol = bol;
1400 const char *eol = --bol;
1402 while (bol > gs->buf && bol[-1] != '\n')
1403 bol--;
1404 cur--;
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;
1415 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1416 funcname_lno = cur;
1417 funcname_needed = 0;
1418 if (opt->funcbody)
1419 comment_needed = 1;
1420 else
1421 from = orig_from;
1425 /* We need to look even further back to find a function signature. */
1426 if (opt->funcname && funcname_needed)
1427 show_funcname_line(opt, gs, bol, cur);
1429 /* Back forward. */
1430 while (cur < lno) {
1431 const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1433 while (*eol != '\n')
1434 eol++;
1435 show_line(opt, bol, eol, gs->name, cur, 0, sign);
1436 bol = eol + 1;
1437 cur++;
1441 static int should_lookahead(struct grep_opt *opt)
1443 struct grep_pat *p;
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 */
1453 return 1;
1456 static int look_ahead(struct grep_opt *opt,
1457 unsigned long *left_p,
1458 unsigned *lno_p,
1459 const char **bol_p)
1461 unsigned lno = *lno_p;
1462 const char *bol = *bol_p;
1463 struct grep_pat *p;
1464 const char *sp, *last_bol;
1465 regoff_t earliest = -1;
1467 for (p = opt->pattern_list; p; p = p->next) {
1468 int hit;
1469 regmatch_t m;
1471 hit = patmatch(p, bol, bol + *left_p, &m, 0);
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;
1478 if (earliest < 0) {
1479 *bol_p = bol + *left_p;
1480 *left_p = 0;
1481 return 1;
1483 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1484 ; /* find the beginning of the line */
1485 last_bol = sp;
1487 for (sp = bol; sp < last_bol; sp++) {
1488 if (*sp == '\n')
1489 lno++;
1491 *left_p -= last_bol - bol;
1492 *bol_p = last_bol;
1493 *lno_p = lno;
1494 return 0;
1497 static int fill_textconv_grep(struct repository *r,
1498 struct userdiff_driver *driver,
1499 struct grep_source *gs)
1501 struct diff_filespec *df;
1502 char *buf;
1503 size_t size;
1505 if (!driver || !driver->textconv)
1506 return grep_source_load(gs);
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.
1513 df = alloc_filespec(gs->path);
1514 switch (gs->type) {
1515 case GREP_SOURCE_OID:
1516 fill_filespec(df, gs->identifier, 1, 0100644);
1517 break;
1518 case GREP_SOURCE_FILE:
1519 fill_filespec(df, null_oid(), 0, 0100644);
1520 break;
1521 default:
1522 BUG("attempt to textconv something without a path?");
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.
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.
1537 obj_read_lock();
1538 size = fill_textconv(r, driver, df, &buf);
1539 obj_read_unlock();
1540 free_filespec(df);
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.
1550 grep_source_clear_data(gs);
1551 gs->buf = buf;
1552 gs->size = size;
1554 return 0;
1557 static int is_empty_line(const char *bol, const char *eol)
1559 while (bol < eol && isspace(*bol))
1560 bol++;
1561 return bol == eol;
1564 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1566 const char *bol;
1567 const char *peek_bol = NULL;
1568 unsigned long left;
1569 unsigned lno = 1;
1570 unsigned last_hit = 0;
1571 int binary_match_only = 0;
1572 unsigned count = 0;
1573 int try_lookahead = 0;
1574 int show_function = 0;
1575 struct userdiff_driver *textconv = NULL;
1576 enum grep_context ctx = GREP_CONTEXT_HEAD;
1577 xdemitconf_t xecfg;
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");
1583 if (!opt->output)
1584 opt->output = std_output;
1586 if (opt->pre_context || opt->post_context || opt->file_break ||
1587 opt->funcbody) {
1588 /* Show hunk marks, except for the first file. */
1589 if (opt->last_shown)
1590 opt->show_hunk_mark = 1;
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().
1596 if (opt->output != std_output)
1597 opt->show_hunk_mark = 1;
1599 opt->last_shown = 0;
1601 if (opt->allow_textconv) {
1602 grep_source_load_driver(gs, opt->repo->index);
1604 * We might set up the shared textconv cache data here, which
1605 * is not thread-safe. Also, get_oid_with_context() and
1606 * parse_object() might be internally called. As they are not
1607 * currently thread-safe and might be racy with object reading,
1608 * obj_read_lock() must be called.
1610 grep_attr_lock();
1611 obj_read_lock();
1612 textconv = userdiff_get_textconv(opt->repo, gs->driver);
1613 obj_read_unlock();
1614 grep_attr_unlock();
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.
1621 if (!textconv) {
1622 switch (opt->binary) {
1623 case GREP_BINARY_DEFAULT:
1624 if (grep_source_is_binary(gs, opt->repo->index))
1625 binary_match_only = 1;
1626 break;
1627 case GREP_BINARY_NOMATCH:
1628 if (grep_source_is_binary(gs, opt->repo->index))
1629 return 0; /* Assume unmatch */
1630 break;
1631 case GREP_BINARY_TEXT:
1632 break;
1633 default:
1634 BUG("unknown binary handling mode");
1638 memset(&xecfg, 0, sizeof(xecfg));
1639 opt->priv = &xecfg;
1641 try_lookahead = should_lookahead(opt);
1643 if (fill_textconv_grep(opt->repo, textconv, gs) < 0)
1644 return 0;
1646 bol = gs->buf;
1647 left = gs->size;
1648 while (left) {
1649 const char *eol;
1650 int hit;
1651 ssize_t cno;
1652 ssize_t col = -1, icol = -1;
1655 * look_ahead() skips quickly to the line that possibly
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.
1663 if (try_lookahead
1664 && !(last_hit
1665 && (show_function ||
1666 lno <= last_hit + opt->post_context))
1667 && look_ahead(opt, &left, &lno, &bol))
1668 break;
1669 eol = end_of_line(bol, &left);
1671 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1672 ctx = GREP_CONTEXT_BODY;
1674 hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits);
1676 if (collect_hits)
1677 goto next_line;
1679 /* "grep -v -e foo -e bla" should list lines
1680 * that do not have either, so inversion should
1681 * be done outside.
1683 if (opt->invert)
1684 hit = !hit;
1685 if (opt->unmatch_name_only) {
1686 if (hit)
1687 return 0;
1688 goto next_line;
1690 if (hit) {
1691 count++;
1692 if (opt->status_only)
1693 return 1;
1694 if (opt->name_only) {
1695 show_name(opt, gs->name);
1696 return 1;
1698 if (opt->count)
1699 goto next_line;
1700 if (binary_match_only) {
1701 opt->output(opt, "Binary file ", 12);
1702 output_color(opt, gs->name, strlen(gs->name),
1703 opt->colors[GREP_COLOR_FILENAME]);
1704 opt->output(opt, " matches\n", 9);
1705 return 1;
1707 /* Hit at this line. If we haven't shown the
1708 * pre-context lines, we would need to show them.
1710 if (opt->pre_context || opt->funcbody)
1711 show_pre_context(opt, gs, bol, eol, lno);
1712 else if (opt->funcname)
1713 show_funcname_line(opt, gs, bol, lno);
1714 cno = opt->invert ? icol : col;
1715 if (cno < 0) {
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.
1723 cno = 0;
1725 show_line(opt, bol, eol, gs->name, lno, cno + 1, ':');
1726 last_hit = lno;
1727 if (opt->funcbody)
1728 show_function = 1;
1729 goto next_line;
1731 if (show_function && (!peek_bol || peek_bol < bol)) {
1732 unsigned long peek_left = left;
1733 const char *peek_eol = eol;
1736 * Trailing empty lines are not interesting.
1737 * Peek past them to see if they belong to the
1738 * body of the current function.
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);
1746 if (match_funcname(opt, gs, peek_bol, peek_eol))
1747 show_function = 0;
1749 if (show_function ||
1750 (last_hit && lno <= last_hit + opt->post_context)) {
1751 /* If the last hit is within the post context,
1752 * we need to show this line.
1754 show_line(opt, bol, eol, gs->name, lno, col + 1, '-');
1757 next_line:
1758 bol = eol + 1;
1759 if (!left)
1760 break;
1761 left--;
1762 lno++;
1765 if (collect_hits)
1766 return 0;
1768 if (opt->status_only)
1769 return opt->unmatch_name_only;
1770 if (opt->unmatch_name_only) {
1771 /* We did not see any hit, so we want to show this */
1772 show_name(opt, gs->name);
1773 return 1;
1776 xdiff_clear_find_func(&xecfg);
1777 opt->priv = NULL;
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.
1784 if (opt->count && count) {
1785 char buf[32];
1786 if (opt->pathname) {
1787 output_color(opt, gs->name, strlen(gs->name),
1788 opt->colors[GREP_COLOR_FILENAME]);
1789 output_sep(opt, ':');
1791 xsnprintf(buf, sizeof(buf), "%u\n", count);
1792 opt->output(opt, buf, strlen(buf));
1793 return 1;
1795 return !!last_hit;
1798 static void clr_hit_marker(struct grep_expr *x)
1800 /* All-hit markers are meaningful only at the very top level
1801 * OR node.
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;
1812 static int chk_hit_marker(struct grep_expr *x)
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;
1824 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1827 * we do not have to do the two-pass grep when we do not check
1828 * buffer-wide "all-match".
1830 if (!opt->all_match)
1831 return grep_source_1(opt, gs, 0);
1833 /* Otherwise the toplevel "or" terms hit a bit differently.
1834 * We first clear hit markers from them.
1836 clr_hit_marker(opt->pattern_expression);
1837 grep_source_1(opt, gs, 1);
1839 if (!chk_hit_marker(opt->pattern_expression))
1840 return 0;
1842 return grep_source_1(opt, gs, 0);
1845 static void grep_source_init_buf(struct grep_source *gs,
1846 const char *buf,
1847 unsigned long size)
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;
1858 int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size)
1860 struct grep_source gs;
1861 int r;
1863 grep_source_init_buf(&gs, buf, size);
1865 r = grep_source(opt, &gs);
1867 grep_source_clear(&gs);
1868 return r;
1871 void grep_source_init_file(struct grep_source *gs, const char *name,
1872 const char *path)
1874 gs->type = GREP_SOURCE_FILE;
1875 gs->name = xstrdup_or_null(name);
1876 gs->path = xstrdup_or_null(path);
1877 gs->buf = NULL;
1878 gs->size = 0;
1879 gs->driver = NULL;
1880 gs->identifier = xstrdup(path);
1883 void grep_source_init_oid(struct grep_source *gs, const char *name,
1884 const char *path, const struct object_id *oid,
1885 struct repository *repo)
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);
1894 gs->repo = repo;
1897 void grep_source_clear(struct grep_source *gs)
1899 FREE_AND_NULL(gs->name);
1900 FREE_AND_NULL(gs->path);
1901 FREE_AND_NULL(gs->identifier);
1902 grep_source_clear_data(gs);
1905 void grep_source_clear_data(struct grep_source *gs)
1907 switch (gs->type) {
1908 case GREP_SOURCE_FILE:
1909 case GREP_SOURCE_OID:
1910 /* these types own the buffer */
1911 free((char *)gs->buf);
1912 gs->buf = NULL;
1913 gs->size = 0;
1914 break;
1915 case GREP_SOURCE_BUF:
1916 /* leave user-provided buf intact */
1917 break;
1921 static int grep_source_load_oid(struct grep_source *gs)
1923 enum object_type type;
1925 gs->buf = repo_read_object_file(gs->repo, gs->identifier, &type,
1926 &gs->size);
1927 if (!gs->buf)
1928 return error(_("'%s': unable to read %s"),
1929 gs->name,
1930 oid_to_hex(gs->identifier));
1931 return 0;
1934 static int grep_source_load_file(struct grep_source *gs)
1936 const char *filename = gs->identifier;
1937 struct stat st;
1938 char *data;
1939 size_t size;
1940 int i;
1942 if (lstat(filename, &st) < 0) {
1943 err_ret:
1944 if (errno != ENOENT)
1945 error_errno(_("failed to stat '%s'"), filename);
1946 return -1;
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;
1954 data = xmallocz(size);
1955 if (st.st_size != read_in_full(i, data, size)) {
1956 error_errno(_("'%s': short read"), filename);
1957 close(i);
1958 free(data);
1959 return -1;
1961 close(i);
1963 gs->buf = data;
1964 gs->size = size;
1965 return 0;
1968 static int grep_source_load(struct grep_source *gs)
1970 if (gs->buf)
1971 return 0;
1973 switch (gs->type) {
1974 case GREP_SOURCE_FILE:
1975 return grep_source_load_file(gs);
1976 case GREP_SOURCE_OID:
1977 return grep_source_load_oid(gs);
1978 case GREP_SOURCE_BUF:
1979 return gs->buf ? 0 : -1;
1981 BUG("invalid grep_source type to load");
1984 void grep_source_load_driver(struct grep_source *gs,
1985 struct index_state *istate)
1987 if (gs->driver)
1988 return;
1990 grep_attr_lock();
1991 if (gs->path)
1992 gs->driver = userdiff_find_by_path(istate, gs->path);
1993 if (!gs->driver)
1994 gs->driver = userdiff_find_by_name("default");
1995 grep_attr_unlock();
1998 static int grep_source_is_binary(struct grep_source *gs,
1999 struct index_state *istate)
2001 grep_source_load_driver(gs, istate);
2002 if (gs->driver->binary != -1)
2003 return gs->driver->binary;
2005 if (!grep_source_load(gs))
2006 return buffer_is_binary(gs->buf, gs->size);
2008 return 0;