help.c: split up list_all_cmds_help() function
[alt-git.git] / grep.c
blob5bec7fd7935dedc94bbde1e8efe67745e2022702
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] = GIT_COLOR_MAGENTA,
30 [GREP_COLOR_FUNCTION] = "",
31 [GREP_COLOR_LINENO] = GIT_COLOR_GREEN,
32 [GREP_COLOR_COLUMNNO] = GIT_COLOR_GREEN,
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;
365 int literal = !opt->ignore_case && (p->fixed || p->is_fixed);
368 * Call pcre2_general_context_create() before calling any
369 * other pcre2_*(). It sets up our malloc()/free() functions
370 * with which everything else is allocated.
372 p->pcre2_general_context = pcre2_general_context_create(
373 pcre2_malloc, pcre2_free, NULL);
374 if (!p->pcre2_general_context)
375 die("Couldn't allocate PCRE2 general context");
377 if (opt->ignore_case) {
378 if (!opt->ignore_locale && has_non_ascii(p->pattern)) {
379 p->pcre2_tables = pcre2_maketables(p->pcre2_general_context);
380 p->pcre2_compile_context = pcre2_compile_context_create(p->pcre2_general_context);
381 pcre2_set_character_tables(p->pcre2_compile_context,
382 p->pcre2_tables);
384 options |= PCRE2_CASELESS;
386 if (!opt->ignore_locale && is_utf8_locale() && !literal)
387 options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
389 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
390 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
391 if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS))
392 options |= PCRE2_NO_START_OPTIMIZE;
393 #endif
395 p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
396 p->patternlen, options, &error, &erroffset,
397 p->pcre2_compile_context);
399 if (p->pcre2_pattern) {
400 p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, p->pcre2_general_context);
401 if (!p->pcre2_match_data)
402 die("Couldn't allocate PCRE2 match data");
403 } else {
404 pcre2_get_error_message(error, errbuf, sizeof(errbuf));
405 compile_regexp_failed(p, (const char *)&errbuf);
408 pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on);
409 if (p->pcre2_jit_on) {
410 jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
411 if (jitret)
412 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret);
415 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
416 * tells us whether the library itself supports JIT,
417 * but to see whether we're going to be actually using
418 * JIT we need to extract PCRE2_INFO_JITSIZE from the
419 * pattern *after* we do pcre2_jit_compile() above.
421 * This is because if the pattern contains the
422 * (*NO_JIT) verb (see pcre2syntax(3))
423 * pcre2_jit_compile() will exit early with 0. If we
424 * then proceed to call pcre2_jit_match() further down
425 * the line instead of pcre2_match() we'll either
426 * segfault (pre PCRE 10.31) or run into a fatal error
427 * (post PCRE2 10.31)
429 patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg);
430 if (patinforet)
431 BUG("pcre2_pattern_info() failed: %d", patinforet);
432 if (jitsizearg == 0) {
433 p->pcre2_jit_on = 0;
434 return;
439 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
440 regmatch_t *match, int eflags)
442 int ret, flags = 0;
443 PCRE2_SIZE *ovector;
444 PCRE2_UCHAR errbuf[256];
446 if (eflags & REG_NOTBOL)
447 flags |= PCRE2_NOTBOL;
449 if (p->pcre2_jit_on)
450 ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line,
451 eol - line, 0, flags, p->pcre2_match_data,
452 NULL);
453 else
454 ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line,
455 eol - line, 0, flags, p->pcre2_match_data,
456 NULL);
458 if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) {
459 pcre2_get_error_message(ret, errbuf, sizeof(errbuf));
460 die("%s failed with error code %d: %s",
461 (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret,
462 errbuf);
464 if (ret > 0) {
465 ovector = pcre2_get_ovector_pointer(p->pcre2_match_data);
466 ret = 0;
467 match->rm_so = (int)ovector[0];
468 match->rm_eo = (int)ovector[1];
471 return ret;
474 static void free_pcre2_pattern(struct grep_pat *p)
476 pcre2_compile_context_free(p->pcre2_compile_context);
477 pcre2_code_free(p->pcre2_pattern);
478 pcre2_match_data_free(p->pcre2_match_data);
479 #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
480 pcre2_maketables_free(p->pcre2_general_context, p->pcre2_tables);
481 #else
482 free((void *)p->pcre2_tables);
483 #endif
484 pcre2_general_context_free(p->pcre2_general_context);
486 #else /* !USE_LIBPCRE2 */
487 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
489 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
492 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
493 regmatch_t *match, int eflags)
495 return 1;
498 static void free_pcre2_pattern(struct grep_pat *p)
502 static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
504 struct strbuf sb = STRBUF_INIT;
505 int err;
506 int regflags = 0;
508 basic_regex_quote_buf(&sb, p->pattern);
509 if (opt->ignore_case)
510 regflags |= REG_ICASE;
511 err = regcomp(&p->regexp, sb.buf, regflags);
512 strbuf_release(&sb);
513 if (err) {
514 char errbuf[1024];
515 regerror(err, &p->regexp, errbuf, sizeof(errbuf));
516 compile_regexp_failed(p, errbuf);
519 #endif /* !USE_LIBPCRE2 */
521 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
523 int err;
524 int regflags = REG_NEWLINE;
526 p->word_regexp = opt->word_regexp;
527 p->ignore_case = opt->ignore_case;
528 p->fixed = opt->fixed;
530 if (memchr(p->pattern, 0, p->patternlen) && !opt->pcre2)
531 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
533 p->is_fixed = is_fixed(p->pattern, p->patternlen);
534 #ifdef USE_LIBPCRE2
535 if (!p->fixed && !p->is_fixed) {
536 const char *no_jit = "(*NO_JIT)";
537 const int no_jit_len = strlen(no_jit);
538 if (starts_with(p->pattern, no_jit) &&
539 is_fixed(p->pattern + no_jit_len,
540 p->patternlen - no_jit_len))
541 p->is_fixed = 1;
543 #endif
544 if (p->fixed || p->is_fixed) {
545 #ifdef USE_LIBPCRE2
546 if (p->is_fixed) {
547 compile_pcre2_pattern(p, opt);
548 } else {
550 * E.g. t7811-grep-open.sh relies on the
551 * pattern being restored.
553 char *old_pattern = p->pattern;
554 size_t old_patternlen = p->patternlen;
555 struct strbuf sb = STRBUF_INIT;
558 * There is the PCRE2_LITERAL flag, but it's
559 * only in PCRE v2 10.30 and later. Needing to
560 * ifdef our way around that and dealing with
561 * it + PCRE2_MULTILINE being an error is more
562 * complex than just quoting this ourselves.
564 strbuf_add(&sb, "\\Q", 2);
565 strbuf_add(&sb, p->pattern, p->patternlen);
566 strbuf_add(&sb, "\\E", 2);
568 p->pattern = sb.buf;
569 p->patternlen = sb.len;
570 compile_pcre2_pattern(p, opt);
571 p->pattern = old_pattern;
572 p->patternlen = old_patternlen;
573 strbuf_release(&sb);
575 #else /* !USE_LIBPCRE2 */
576 compile_fixed_regexp(p, opt);
577 #endif /* !USE_LIBPCRE2 */
578 return;
581 if (opt->pcre2) {
582 compile_pcre2_pattern(p, opt);
583 return;
586 if (p->ignore_case)
587 regflags |= REG_ICASE;
588 if (opt->extended_regexp_option)
589 regflags |= REG_EXTENDED;
590 err = regcomp(&p->regexp, p->pattern, regflags);
591 if (err) {
592 char errbuf[1024];
593 regerror(err, &p->regexp, errbuf, 1024);
594 compile_regexp_failed(p, errbuf);
598 static struct grep_expr *grep_not_expr(struct grep_expr *expr)
600 struct grep_expr *z = xcalloc(1, sizeof(*z));
601 z->node = GREP_NODE_NOT;
602 z->u.unary = expr;
603 return z;
606 static struct grep_expr *grep_binexp(enum grep_expr_node kind,
607 struct grep_expr *left,
608 struct grep_expr *right)
610 struct grep_expr *z = xcalloc(1, sizeof(*z));
611 z->node = kind;
612 z->u.binary.left = left;
613 z->u.binary.right = right;
614 return z;
617 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
619 return grep_binexp(GREP_NODE_OR, left, right);
622 static struct grep_expr *grep_and_expr(struct grep_expr *left, struct grep_expr *right)
624 return grep_binexp(GREP_NODE_AND, left, right);
627 static struct grep_expr *compile_pattern_or(struct grep_pat **);
628 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
630 struct grep_pat *p;
631 struct grep_expr *x;
633 p = *list;
634 if (!p)
635 return NULL;
636 switch (p->token) {
637 case GREP_PATTERN: /* atom */
638 case GREP_PATTERN_HEAD:
639 case GREP_PATTERN_BODY:
640 CALLOC_ARRAY(x, 1);
641 x->node = GREP_NODE_ATOM;
642 x->u.atom = p;
643 *list = p->next;
644 return x;
645 case GREP_OPEN_PAREN:
646 *list = p->next;
647 x = compile_pattern_or(list);
648 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
649 die("unmatched parenthesis");
650 *list = (*list)->next;
651 return x;
652 default:
653 return NULL;
657 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
659 struct grep_pat *p;
660 struct grep_expr *x;
662 p = *list;
663 if (!p)
664 return NULL;
665 switch (p->token) {
666 case GREP_NOT:
667 if (!p->next)
668 die("--not not followed by pattern expression");
669 *list = p->next;
670 x = compile_pattern_not(list);
671 if (!x)
672 die("--not followed by non pattern expression");
673 return grep_not_expr(x);
674 default:
675 return compile_pattern_atom(list);
679 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
681 struct grep_pat *p;
682 struct grep_expr *x, *y;
684 x = compile_pattern_not(list);
685 p = *list;
686 if (p && p->token == GREP_AND) {
687 if (!x)
688 die("--and not preceded by pattern expression");
689 if (!p->next)
690 die("--and not followed by pattern expression");
691 *list = p->next;
692 y = compile_pattern_and(list);
693 if (!y)
694 die("--and not followed by pattern expression");
695 return grep_and_expr(x, y);
697 return x;
700 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
702 struct grep_pat *p;
703 struct grep_expr *x, *y;
705 x = compile_pattern_and(list);
706 p = *list;
707 if (x && p && p->token != GREP_CLOSE_PAREN) {
708 y = compile_pattern_or(list);
709 if (!y)
710 die("not a pattern expression %s", p->pattern);
711 return grep_or_expr(x, y);
713 return x;
716 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
718 return compile_pattern_or(list);
721 static struct grep_expr *grep_true_expr(void)
723 struct grep_expr *z = xcalloc(1, sizeof(*z));
724 z->node = GREP_NODE_TRUE;
725 return z;
728 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
730 struct grep_pat *p;
731 struct grep_expr *header_expr;
732 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
733 enum grep_header_field fld;
735 if (!opt->header_list)
736 return NULL;
738 for (p = opt->header_list; p; p = p->next) {
739 if (p->token != GREP_PATTERN_HEAD)
740 BUG("a non-header pattern in grep header list.");
741 if (p->field < GREP_HEADER_FIELD_MIN ||
742 GREP_HEADER_FIELD_MAX <= p->field)
743 BUG("unknown header field %d", p->field);
744 compile_regexp(p, opt);
747 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
748 header_group[fld] = NULL;
750 for (p = opt->header_list; p; p = p->next) {
751 struct grep_expr *h;
752 struct grep_pat *pp = p;
754 h = compile_pattern_atom(&pp);
755 if (!h || pp != p->next)
756 BUG("malformed header expr");
757 if (!header_group[p->field]) {
758 header_group[p->field] = h;
759 continue;
761 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
764 header_expr = NULL;
766 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
767 if (!header_group[fld])
768 continue;
769 if (!header_expr)
770 header_expr = grep_true_expr();
771 header_expr = grep_or_expr(header_group[fld], header_expr);
773 return header_expr;
776 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
778 struct grep_expr *z = x;
780 while (x) {
781 assert(x->node == GREP_NODE_OR);
782 if (x->u.binary.right &&
783 x->u.binary.right->node == GREP_NODE_TRUE) {
784 x->u.binary.right = y;
785 break;
787 x = x->u.binary.right;
789 return z;
792 void compile_grep_patterns(struct grep_opt *opt)
794 struct grep_pat *p;
795 struct grep_expr *header_expr = prep_header_patterns(opt);
797 for (p = opt->pattern_list; p; p = p->next) {
798 switch (p->token) {
799 case GREP_PATTERN: /* atom */
800 case GREP_PATTERN_HEAD:
801 case GREP_PATTERN_BODY:
802 compile_regexp(p, opt);
803 break;
804 default:
805 opt->extended = 1;
806 break;
810 if (opt->all_match || opt->no_body_match || header_expr)
811 opt->extended = 1;
812 else if (!opt->extended)
813 return;
815 p = opt->pattern_list;
816 if (p)
817 opt->pattern_expression = compile_pattern_expr(&p);
818 if (p)
819 die("incomplete pattern expression: %s", p->pattern);
821 if (opt->no_body_match && opt->pattern_expression)
822 opt->pattern_expression = grep_not_expr(opt->pattern_expression);
824 if (!header_expr)
825 return;
827 if (!opt->pattern_expression)
828 opt->pattern_expression = header_expr;
829 else if (opt->all_match)
830 opt->pattern_expression = grep_splice_or(header_expr,
831 opt->pattern_expression);
832 else
833 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
834 header_expr);
835 opt->all_match = 1;
838 static void free_pattern_expr(struct grep_expr *x)
840 switch (x->node) {
841 case GREP_NODE_TRUE:
842 case GREP_NODE_ATOM:
843 break;
844 case GREP_NODE_NOT:
845 free_pattern_expr(x->u.unary);
846 break;
847 case GREP_NODE_AND:
848 case GREP_NODE_OR:
849 free_pattern_expr(x->u.binary.left);
850 free_pattern_expr(x->u.binary.right);
851 break;
853 free(x);
856 void free_grep_patterns(struct grep_opt *opt)
858 struct grep_pat *p, *n;
860 for (p = opt->pattern_list; p; p = n) {
861 n = p->next;
862 switch (p->token) {
863 case GREP_PATTERN: /* atom */
864 case GREP_PATTERN_HEAD:
865 case GREP_PATTERN_BODY:
866 if (p->pcre2_pattern)
867 free_pcre2_pattern(p);
868 else
869 regfree(&p->regexp);
870 free(p->pattern);
871 break;
872 default:
873 break;
875 free(p);
878 if (!opt->extended)
879 return;
880 free_pattern_expr(opt->pattern_expression);
883 static const char *end_of_line(const char *cp, unsigned long *left)
885 unsigned long l = *left;
886 while (l && *cp != '\n') {
887 l--;
888 cp++;
890 *left = l;
891 return cp;
894 static int word_char(char ch)
896 return isalnum(ch) || ch == '_';
899 static void output_color(struct grep_opt *opt, const void *data, size_t size,
900 const char *color)
902 if (want_color(opt->color) && color && color[0]) {
903 opt->output(opt, color, strlen(color));
904 opt->output(opt, data, size);
905 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
906 } else
907 opt->output(opt, data, size);
910 static void output_sep(struct grep_opt *opt, char sign)
912 if (opt->null_following_name)
913 opt->output(opt, "\0", 1);
914 else
915 output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]);
918 static void show_name(struct grep_opt *opt, const char *name)
920 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
921 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
924 static int patmatch(struct grep_pat *p,
925 const char *line, const char *eol,
926 regmatch_t *match, int eflags)
928 int hit;
930 if (p->pcre2_pattern)
931 hit = !pcre2match(p, line, eol, match, eflags);
932 else
933 hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
934 eflags);
936 return hit;
939 static void strip_timestamp(const char *bol, const char **eol_p)
941 const char *eol = *eol_p;
943 while (bol < --eol) {
944 if (*eol != '>')
945 continue;
946 *eol_p = ++eol;
947 break;
951 static struct {
952 const char *field;
953 size_t len;
954 } header_field[] = {
955 { "author ", 7 },
956 { "committer ", 10 },
957 { "reflog ", 7 },
960 static int headerless_match_one_pattern(struct grep_pat *p,
961 const char *bol, const char *eol,
962 enum grep_context ctx,
963 regmatch_t *pmatch, int eflags)
965 int hit = 0;
966 const char *start = bol;
968 if ((p->token != GREP_PATTERN) &&
969 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
970 return 0;
972 again:
973 hit = patmatch(p, bol, eol, pmatch, eflags);
975 if (hit && p->word_regexp) {
976 if ((pmatch[0].rm_so < 0) ||
977 (eol - bol) < pmatch[0].rm_so ||
978 (pmatch[0].rm_eo < 0) ||
979 (eol - bol) < pmatch[0].rm_eo)
980 die("regexp returned nonsense");
982 /* Match beginning must be either beginning of the
983 * line, or at word boundary (i.e. the last char must
984 * not be a word char). Similarly, match end must be
985 * either end of the line, or at word boundary
986 * (i.e. the next char must not be a word char).
988 if ( ((pmatch[0].rm_so == 0) ||
989 !word_char(bol[pmatch[0].rm_so-1])) &&
990 ((pmatch[0].rm_eo == (eol-bol)) ||
991 !word_char(bol[pmatch[0].rm_eo])) )
993 else
994 hit = 0;
996 /* Words consist of at least one character. */
997 if (pmatch->rm_so == pmatch->rm_eo)
998 hit = 0;
1000 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
1001 /* There could be more than one match on the
1002 * line, and the first match might not be
1003 * strict word match. But later ones could be!
1004 * Forward to the next possible start, i.e. the
1005 * next position following a non-word char.
1007 bol = pmatch[0].rm_so + bol + 1;
1008 while (word_char(bol[-1]) && bol < eol)
1009 bol++;
1010 eflags |= REG_NOTBOL;
1011 if (bol < eol)
1012 goto again;
1015 if (hit) {
1016 pmatch[0].rm_so += bol - start;
1017 pmatch[0].rm_eo += bol - start;
1019 return hit;
1022 static int match_one_pattern(struct grep_pat *p,
1023 const char *bol, const char *eol,
1024 enum grep_context ctx, regmatch_t *pmatch,
1025 int eflags)
1027 const char *field;
1028 size_t len;
1030 if (p->token == GREP_PATTERN_HEAD) {
1031 assert(p->field < ARRAY_SIZE(header_field));
1032 field = header_field[p->field].field;
1033 len = header_field[p->field].len;
1034 if (strncmp(bol, field, len))
1035 return 0;
1036 bol += len;
1038 switch (p->field) {
1039 case GREP_HEADER_AUTHOR:
1040 case GREP_HEADER_COMMITTER:
1041 strip_timestamp(bol, &eol);
1042 break;
1043 default:
1044 break;
1048 return headerless_match_one_pattern(p, bol, eol, ctx, pmatch, eflags);
1052 static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x,
1053 const char *bol, const char *eol,
1054 enum grep_context ctx, ssize_t *col,
1055 ssize_t *icol, int collect_hits)
1057 int h = 0;
1059 if (!x)
1060 die("Not a valid grep expression");
1061 switch (x->node) {
1062 case GREP_NODE_TRUE:
1063 h = 1;
1064 break;
1065 case GREP_NODE_ATOM:
1067 regmatch_t tmp;
1068 h = match_one_pattern(x->u.atom, bol, eol, ctx,
1069 &tmp, 0);
1070 if (h && (*col < 0 || tmp.rm_so < *col))
1071 *col = tmp.rm_so;
1073 if (x->u.atom->token == GREP_PATTERN_BODY)
1074 opt->body_hit |= h;
1075 break;
1076 case GREP_NODE_NOT:
1078 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1080 h = !match_expr_eval(opt, x->u.unary, bol, eol, ctx, icol, col,
1082 break;
1083 case GREP_NODE_AND:
1084 h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
1085 icol, 0);
1086 if (h || opt->columnnum) {
1088 * Don't short-circuit AND when given --column, since a
1089 * NOT earlier in the tree may turn this into an OR. In
1090 * this case, see the below comment.
1092 h &= match_expr_eval(opt, x->u.binary.right, bol, eol,
1093 ctx, col, icol, 0);
1095 break;
1096 case GREP_NODE_OR:
1097 if (!(collect_hits || opt->columnnum)) {
1099 * Don't short-circuit OR when given --column (or
1100 * collecting hits) to ensure we don't skip a later
1101 * child that would produce an earlier match.
1103 return (match_expr_eval(opt, x->u.binary.left, bol, eol,
1104 ctx, col, icol, 0) ||
1105 match_expr_eval(opt, x->u.binary.right, bol,
1106 eol, ctx, col, icol, 0));
1108 h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
1109 icol, 0);
1110 if (collect_hits)
1111 x->u.binary.left->hit |= h;
1112 h |= match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col,
1113 icol, collect_hits);
1114 break;
1115 default:
1116 die("Unexpected node type (internal error) %d", x->node);
1118 if (collect_hits)
1119 x->hit |= h;
1120 return h;
1123 static int match_expr(struct grep_opt *opt,
1124 const char *bol, const char *eol,
1125 enum grep_context ctx, ssize_t *col,
1126 ssize_t *icol, int collect_hits)
1128 struct grep_expr *x = opt->pattern_expression;
1129 return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits);
1132 static int match_line(struct grep_opt *opt,
1133 const char *bol, const char *eol,
1134 ssize_t *col, ssize_t *icol,
1135 enum grep_context ctx, int collect_hits)
1137 struct grep_pat *p;
1138 int hit = 0;
1140 if (opt->extended)
1141 return match_expr(opt, bol, eol, ctx, col, icol,
1142 collect_hits);
1144 /* we do not call with collect_hits without being extended */
1145 for (p = opt->pattern_list; p; p = p->next) {
1146 regmatch_t tmp;
1147 if (match_one_pattern(p, bol, eol, ctx, &tmp, 0)) {
1148 hit |= 1;
1149 if (!opt->columnnum) {
1151 * Without --column, any single match on a line
1152 * is enough to know that it needs to be
1153 * printed. With --column, scan _all_ patterns
1154 * to find the earliest.
1156 break;
1158 if (*col < 0 || tmp.rm_so < *col)
1159 *col = tmp.rm_so;
1162 return hit;
1165 static int match_next_pattern(struct grep_pat *p,
1166 const char *bol, const char *eol,
1167 enum grep_context ctx,
1168 regmatch_t *pmatch, int eflags)
1170 regmatch_t match;
1172 if (!headerless_match_one_pattern(p, bol, eol, ctx, &match, eflags))
1173 return 0;
1174 if (match.rm_so < 0 || match.rm_eo < 0)
1175 return 0;
1176 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1177 if (match.rm_so > pmatch->rm_so)
1178 return 1;
1179 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1180 return 1;
1182 pmatch->rm_so = match.rm_so;
1183 pmatch->rm_eo = match.rm_eo;
1184 return 1;
1187 int grep_next_match(struct grep_opt *opt,
1188 const char *bol, const char *eol,
1189 enum grep_context ctx, regmatch_t *pmatch,
1190 enum grep_header_field field, int eflags)
1192 struct grep_pat *p;
1193 int hit = 0;
1195 pmatch->rm_so = pmatch->rm_eo = -1;
1196 if (bol < eol) {
1197 for (p = ((ctx == GREP_CONTEXT_HEAD)
1198 ? opt->header_list : opt->pattern_list);
1199 p; p = p->next) {
1200 switch (p->token) {
1201 case GREP_PATTERN_HEAD:
1202 if ((field != GREP_HEADER_FIELD_MAX) &&
1203 (p->field != field))
1204 continue;
1205 /* fall thru */
1206 case GREP_PATTERN: /* atom */
1207 case GREP_PATTERN_BODY:
1208 hit |= match_next_pattern(p, bol, eol, ctx,
1209 pmatch, eflags);
1210 break;
1211 default:
1212 break;
1216 return hit;
1219 static void show_line_header(struct grep_opt *opt, const char *name,
1220 unsigned lno, ssize_t cno, char sign)
1222 if (opt->heading && opt->last_shown == 0) {
1223 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1224 opt->output(opt, "\n", 1);
1226 opt->last_shown = lno;
1228 if (!opt->heading && opt->pathname) {
1229 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
1230 output_sep(opt, sign);
1232 if (opt->linenum) {
1233 char buf[32];
1234 xsnprintf(buf, sizeof(buf), "%d", lno);
1235 output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]);
1236 output_sep(opt, sign);
1239 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1240 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1241 * being called with a context line.
1243 if (opt->columnnum && cno) {
1244 char buf[32];
1245 xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno);
1246 output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_COLUMNNO]);
1247 output_sep(opt, sign);
1251 static void show_line(struct grep_opt *opt,
1252 const char *bol, const char *eol,
1253 const char *name, unsigned lno, ssize_t cno, char sign)
1255 int rest = eol - bol;
1256 const char *match_color = NULL;
1257 const char *line_color = NULL;
1259 if (opt->file_break && opt->last_shown == 0) {
1260 if (opt->show_hunk_mark)
1261 opt->output(opt, "\n", 1);
1262 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1263 if (opt->last_shown == 0) {
1264 if (opt->show_hunk_mark) {
1265 output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
1266 opt->output(opt, "\n", 1);
1268 } else if (lno > opt->last_shown + 1) {
1269 output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
1270 opt->output(opt, "\n", 1);
1273 if (!opt->only_matching) {
1275 * In case the line we're being called with contains more than
1276 * one match, leave printing each header to the loop below.
1278 show_line_header(opt, name, lno, cno, sign);
1280 if (opt->color || opt->only_matching) {
1281 regmatch_t match;
1282 enum grep_context ctx = GREP_CONTEXT_BODY;
1283 int eflags = 0;
1285 if (opt->color) {
1286 if (sign == ':')
1287 match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
1288 else
1289 match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT];
1290 if (sign == ':')
1291 line_color = opt->colors[GREP_COLOR_SELECTED];
1292 else if (sign == '-')
1293 line_color = opt->colors[GREP_COLOR_CONTEXT];
1294 else if (sign == '=')
1295 line_color = opt->colors[GREP_COLOR_FUNCTION];
1297 while (grep_next_match(opt, bol, eol, ctx, &match,
1298 GREP_HEADER_FIELD_MAX, eflags)) {
1299 if (match.rm_so == match.rm_eo)
1300 break;
1302 if (opt->only_matching)
1303 show_line_header(opt, name, lno, cno, sign);
1304 else
1305 output_color(opt, bol, match.rm_so, line_color);
1306 output_color(opt, bol + match.rm_so,
1307 match.rm_eo - match.rm_so, match_color);
1308 if (opt->only_matching)
1309 opt->output(opt, "\n", 1);
1310 bol += match.rm_eo;
1311 cno += match.rm_eo;
1312 rest -= match.rm_eo;
1313 eflags = REG_NOTBOL;
1316 if (!opt->only_matching) {
1317 output_color(opt, bol, rest, line_color);
1318 opt->output(opt, "\n", 1);
1322 int grep_use_locks;
1325 * This lock protects access to the gitattributes machinery, which is
1326 * not thread-safe.
1328 pthread_mutex_t grep_attr_mutex;
1330 static inline void grep_attr_lock(void)
1332 if (grep_use_locks)
1333 pthread_mutex_lock(&grep_attr_mutex);
1336 static inline void grep_attr_unlock(void)
1338 if (grep_use_locks)
1339 pthread_mutex_unlock(&grep_attr_mutex);
1342 static int match_funcname(struct grep_opt *opt, struct grep_source *gs,
1343 const char *bol, const char *eol)
1345 xdemitconf_t *xecfg = opt->priv;
1346 if (xecfg && !xecfg->find_func) {
1347 grep_source_load_driver(gs, opt->repo->index);
1348 if (gs->driver->funcname.pattern) {
1349 const struct userdiff_funcname *pe = &gs->driver->funcname;
1350 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1351 } else {
1352 xecfg = opt->priv = NULL;
1356 if (xecfg) {
1357 char buf[1];
1358 return xecfg->find_func(bol, eol - bol, buf, 1,
1359 xecfg->find_func_priv) >= 0;
1362 if (bol == eol)
1363 return 0;
1364 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1365 return 1;
1366 return 0;
1369 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1370 const char *bol, unsigned lno)
1372 while (bol > gs->buf) {
1373 const char *eol = --bol;
1375 while (bol > gs->buf && bol[-1] != '\n')
1376 bol--;
1377 lno--;
1379 if (lno <= opt->last_shown)
1380 break;
1382 if (match_funcname(opt, gs, bol, eol)) {
1383 show_line(opt, bol, eol, gs->name, lno, 0, '=');
1384 break;
1389 static int is_empty_line(const char *bol, const char *eol);
1391 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1392 const char *bol, const char *end, unsigned lno)
1394 unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
1395 int funcname_needed = !!opt->funcname, comment_needed = 0;
1397 if (opt->pre_context < lno)
1398 from = lno - opt->pre_context;
1399 if (from <= opt->last_shown)
1400 from = opt->last_shown + 1;
1401 orig_from = from;
1402 if (opt->funcbody) {
1403 if (match_funcname(opt, gs, bol, end))
1404 comment_needed = 1;
1405 else
1406 funcname_needed = 1;
1407 from = opt->last_shown + 1;
1410 /* Rewind. */
1411 while (bol > gs->buf && cur > from) {
1412 const char *next_bol = bol;
1413 const char *eol = --bol;
1415 while (bol > gs->buf && bol[-1] != '\n')
1416 bol--;
1417 cur--;
1418 if (comment_needed && (is_empty_line(bol, eol) ||
1419 match_funcname(opt, gs, bol, eol))) {
1420 comment_needed = 0;
1421 from = orig_from;
1422 if (cur < from) {
1423 cur++;
1424 bol = next_bol;
1425 break;
1428 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1429 funcname_lno = cur;
1430 funcname_needed = 0;
1431 if (opt->funcbody)
1432 comment_needed = 1;
1433 else
1434 from = orig_from;
1438 /* We need to look even further back to find a function signature. */
1439 if (opt->funcname && funcname_needed)
1440 show_funcname_line(opt, gs, bol, cur);
1442 /* Back forward. */
1443 while (cur < lno) {
1444 const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1446 while (*eol != '\n')
1447 eol++;
1448 show_line(opt, bol, eol, gs->name, cur, 0, sign);
1449 bol = eol + 1;
1450 cur++;
1454 static int should_lookahead(struct grep_opt *opt)
1456 struct grep_pat *p;
1458 if (opt->extended)
1459 return 0; /* punt for too complex stuff */
1460 if (opt->invert)
1461 return 0;
1462 for (p = opt->pattern_list; p; p = p->next) {
1463 if (p->token != GREP_PATTERN)
1464 return 0; /* punt for "header only" and stuff */
1466 return 1;
1469 static int look_ahead(struct grep_opt *opt,
1470 unsigned long *left_p,
1471 unsigned *lno_p,
1472 const char **bol_p)
1474 unsigned lno = *lno_p;
1475 const char *bol = *bol_p;
1476 struct grep_pat *p;
1477 const char *sp, *last_bol;
1478 regoff_t earliest = -1;
1480 for (p = opt->pattern_list; p; p = p->next) {
1481 int hit;
1482 regmatch_t m;
1484 hit = patmatch(p, bol, bol + *left_p, &m, 0);
1485 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1486 continue;
1487 if (earliest < 0 || m.rm_so < earliest)
1488 earliest = m.rm_so;
1491 if (earliest < 0) {
1492 *bol_p = bol + *left_p;
1493 *left_p = 0;
1494 return 1;
1496 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1497 ; /* find the beginning of the line */
1498 last_bol = sp;
1500 for (sp = bol; sp < last_bol; sp++) {
1501 if (*sp == '\n')
1502 lno++;
1504 *left_p -= last_bol - bol;
1505 *bol_p = last_bol;
1506 *lno_p = lno;
1507 return 0;
1510 static int fill_textconv_grep(struct repository *r,
1511 struct userdiff_driver *driver,
1512 struct grep_source *gs)
1514 struct diff_filespec *df;
1515 char *buf;
1516 size_t size;
1518 if (!driver || !driver->textconv)
1519 return grep_source_load(gs);
1522 * The textconv interface is intimately tied to diff_filespecs, so we
1523 * have to pretend to be one. If we could unify the grep_source
1524 * and diff_filespec structs, this mess could just go away.
1526 df = alloc_filespec(gs->path);
1527 switch (gs->type) {
1528 case GREP_SOURCE_OID:
1529 fill_filespec(df, gs->identifier, 1, 0100644);
1530 break;
1531 case GREP_SOURCE_FILE:
1532 fill_filespec(df, null_oid(), 0, 0100644);
1533 break;
1534 default:
1535 BUG("attempt to textconv something without a path?");
1539 * fill_textconv is not remotely thread-safe; it modifies the global
1540 * diff tempfile structure, writes to the_repo's odb and might
1541 * internally call thread-unsafe functions such as the
1542 * prepare_packed_git() lazy-initializator. Because of the last two, we
1543 * must ensure mutual exclusion between this call and the object reading
1544 * API, thus we use obj_read_lock() here.
1546 * TODO: allowing text conversion to run in parallel with object
1547 * reading operations might increase performance in the multithreaded
1548 * non-worktreee git-grep with --textconv.
1550 obj_read_lock();
1551 size = fill_textconv(r, driver, df, &buf);
1552 obj_read_unlock();
1553 free_filespec(df);
1556 * The normal fill_textconv usage by the diff machinery would just keep
1557 * the textconv'd buf separate from the diff_filespec. But much of the
1558 * grep code passes around a grep_source and assumes that its "buf"
1559 * pointer is the beginning of the thing we are searching. So let's
1560 * install our textconv'd version into the grep_source, taking care not
1561 * to leak any existing buffer.
1563 grep_source_clear_data(gs);
1564 gs->buf = buf;
1565 gs->size = size;
1567 return 0;
1570 static int is_empty_line(const char *bol, const char *eol)
1572 while (bol < eol && isspace(*bol))
1573 bol++;
1574 return bol == eol;
1577 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1579 const char *bol;
1580 const char *peek_bol = NULL;
1581 unsigned long left;
1582 unsigned lno = 1;
1583 unsigned last_hit = 0;
1584 int binary_match_only = 0;
1585 unsigned count = 0;
1586 int try_lookahead = 0;
1587 int show_function = 0;
1588 struct userdiff_driver *textconv = NULL;
1589 enum grep_context ctx = GREP_CONTEXT_HEAD;
1590 xdemitconf_t xecfg;
1592 if (!opt->status_only && gs->name == NULL)
1593 BUG("grep call which could print a name requires "
1594 "grep_source.name be non-NULL");
1596 if (!opt->output)
1597 opt->output = std_output;
1599 if (opt->pre_context || opt->post_context || opt->file_break ||
1600 opt->funcbody) {
1601 /* Show hunk marks, except for the first file. */
1602 if (opt->last_shown)
1603 opt->show_hunk_mark = 1;
1605 * If we're using threads then we can't easily identify
1606 * the first file. Always put hunk marks in that case
1607 * and skip the very first one later in work_done().
1609 if (opt->output != std_output)
1610 opt->show_hunk_mark = 1;
1612 opt->last_shown = 0;
1614 if (opt->allow_textconv) {
1615 grep_source_load_driver(gs, opt->repo->index);
1617 * We might set up the shared textconv cache data here, which
1618 * is not thread-safe. Also, get_oid_with_context() and
1619 * parse_object() might be internally called. As they are not
1620 * currently thread-safe and might be racy with object reading,
1621 * obj_read_lock() must be called.
1623 grep_attr_lock();
1624 obj_read_lock();
1625 textconv = userdiff_get_textconv(opt->repo, gs->driver);
1626 obj_read_unlock();
1627 grep_attr_unlock();
1631 * We know the result of a textconv is text, so we only have to care
1632 * about binary handling if we are not using it.
1634 if (!textconv) {
1635 switch (opt->binary) {
1636 case GREP_BINARY_DEFAULT:
1637 if (grep_source_is_binary(gs, opt->repo->index))
1638 binary_match_only = 1;
1639 break;
1640 case GREP_BINARY_NOMATCH:
1641 if (grep_source_is_binary(gs, opt->repo->index))
1642 return 0; /* Assume unmatch */
1643 break;
1644 case GREP_BINARY_TEXT:
1645 break;
1646 default:
1647 BUG("unknown binary handling mode");
1651 memset(&xecfg, 0, sizeof(xecfg));
1652 opt->priv = &xecfg;
1654 try_lookahead = should_lookahead(opt);
1656 if (fill_textconv_grep(opt->repo, textconv, gs) < 0)
1657 return 0;
1659 bol = gs->buf;
1660 left = gs->size;
1661 while (left) {
1662 const char *eol;
1663 int hit;
1664 ssize_t cno;
1665 ssize_t col = -1, icol = -1;
1668 * look_ahead() skips quickly to the line that possibly
1669 * has the next hit; don't call it if we need to do
1670 * something more than just skipping the current line
1671 * in response to an unmatch for the current line. E.g.
1672 * inside a post-context window, we will show the current
1673 * line as a context around the previous hit when it
1674 * doesn't hit.
1676 if (try_lookahead
1677 && !(last_hit
1678 && (show_function ||
1679 lno <= last_hit + opt->post_context))
1680 && look_ahead(opt, &left, &lno, &bol))
1681 break;
1682 eol = end_of_line(bol, &left);
1684 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1685 ctx = GREP_CONTEXT_BODY;
1687 hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits);
1689 if (collect_hits)
1690 goto next_line;
1692 /* "grep -v -e foo -e bla" should list lines
1693 * that do not have either, so inversion should
1694 * be done outside.
1696 if (opt->invert)
1697 hit = !hit;
1698 if (opt->unmatch_name_only) {
1699 if (hit)
1700 return 0;
1701 goto next_line;
1703 if (hit) {
1704 count++;
1705 if (opt->status_only)
1706 return 1;
1707 if (opt->name_only) {
1708 show_name(opt, gs->name);
1709 return 1;
1711 if (opt->count)
1712 goto next_line;
1713 if (binary_match_only) {
1714 opt->output(opt, "Binary file ", 12);
1715 output_color(opt, gs->name, strlen(gs->name),
1716 opt->colors[GREP_COLOR_FILENAME]);
1717 opt->output(opt, " matches\n", 9);
1718 return 1;
1720 /* Hit at this line. If we haven't shown the
1721 * pre-context lines, we would need to show them.
1723 if (opt->pre_context || opt->funcbody)
1724 show_pre_context(opt, gs, bol, eol, lno);
1725 else if (opt->funcname)
1726 show_funcname_line(opt, gs, bol, lno);
1727 cno = opt->invert ? icol : col;
1728 if (cno < 0) {
1730 * A negative cno indicates that there was no
1731 * match on the line. We are thus inverted and
1732 * being asked to show all lines that _don't_
1733 * match a given expression. Therefore, set cno
1734 * to 0 to suggest the whole line matches.
1736 cno = 0;
1738 show_line(opt, bol, eol, gs->name, lno, cno + 1, ':');
1739 last_hit = lno;
1740 if (opt->funcbody)
1741 show_function = 1;
1742 goto next_line;
1744 if (show_function && (!peek_bol || peek_bol < bol)) {
1745 unsigned long peek_left = left;
1746 const char *peek_eol = eol;
1749 * Trailing empty lines are not interesting.
1750 * Peek past them to see if they belong to the
1751 * body of the current function.
1753 peek_bol = bol;
1754 while (is_empty_line(peek_bol, peek_eol)) {
1755 peek_bol = peek_eol + 1;
1756 peek_eol = end_of_line(peek_bol, &peek_left);
1759 if (match_funcname(opt, gs, peek_bol, peek_eol))
1760 show_function = 0;
1762 if (show_function ||
1763 (last_hit && lno <= last_hit + opt->post_context)) {
1764 /* If the last hit is within the post context,
1765 * we need to show this line.
1767 show_line(opt, bol, eol, gs->name, lno, col + 1, '-');
1770 next_line:
1771 bol = eol + 1;
1772 if (!left)
1773 break;
1774 left--;
1775 lno++;
1778 if (collect_hits)
1779 return 0;
1781 if (opt->status_only)
1782 return opt->unmatch_name_only;
1783 if (opt->unmatch_name_only) {
1784 /* We did not see any hit, so we want to show this */
1785 show_name(opt, gs->name);
1786 return 1;
1789 xdiff_clear_find_func(&xecfg);
1790 opt->priv = NULL;
1792 /* NEEDSWORK:
1793 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1794 * which feels mostly useless but sometimes useful. Maybe
1795 * make it another option? For now suppress them.
1797 if (opt->count && count) {
1798 char buf[32];
1799 if (opt->pathname) {
1800 output_color(opt, gs->name, strlen(gs->name),
1801 opt->colors[GREP_COLOR_FILENAME]);
1802 output_sep(opt, ':');
1804 xsnprintf(buf, sizeof(buf), "%u\n", count);
1805 opt->output(opt, buf, strlen(buf));
1806 return 1;
1808 return !!last_hit;
1811 static void clr_hit_marker(struct grep_expr *x)
1813 /* All-hit markers are meaningful only at the very top level
1814 * OR node.
1816 while (1) {
1817 x->hit = 0;
1818 if (x->node != GREP_NODE_OR)
1819 return;
1820 x->u.binary.left->hit = 0;
1821 x = x->u.binary.right;
1825 static int chk_hit_marker(struct grep_expr *x)
1827 /* Top level nodes have hit markers. See if they all are hits */
1828 while (1) {
1829 if (x->node != GREP_NODE_OR)
1830 return x->hit;
1831 if (!x->u.binary.left->hit)
1832 return 0;
1833 x = x->u.binary.right;
1837 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1840 * we do not have to do the two-pass grep when we do not check
1841 * buffer-wide "all-match".
1843 if (!opt->all_match && !opt->no_body_match)
1844 return grep_source_1(opt, gs, 0);
1846 /* Otherwise the toplevel "or" terms hit a bit differently.
1847 * We first clear hit markers from them.
1849 clr_hit_marker(opt->pattern_expression);
1850 opt->body_hit = 0;
1851 grep_source_1(opt, gs, 1);
1853 if (opt->all_match && !chk_hit_marker(opt->pattern_expression))
1854 return 0;
1855 if (opt->no_body_match && opt->body_hit)
1856 return 0;
1858 return grep_source_1(opt, gs, 0);
1861 static void grep_source_init_buf(struct grep_source *gs,
1862 const char *buf,
1863 unsigned long size)
1865 gs->type = GREP_SOURCE_BUF;
1866 gs->name = NULL;
1867 gs->path = NULL;
1868 gs->buf = buf;
1869 gs->size = size;
1870 gs->driver = NULL;
1871 gs->identifier = NULL;
1874 int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size)
1876 struct grep_source gs;
1877 int r;
1879 grep_source_init_buf(&gs, buf, size);
1881 r = grep_source(opt, &gs);
1883 grep_source_clear(&gs);
1884 return r;
1887 void grep_source_init_file(struct grep_source *gs, const char *name,
1888 const char *path)
1890 gs->type = GREP_SOURCE_FILE;
1891 gs->name = xstrdup_or_null(name);
1892 gs->path = xstrdup_or_null(path);
1893 gs->buf = NULL;
1894 gs->size = 0;
1895 gs->driver = NULL;
1896 gs->identifier = xstrdup(path);
1899 void grep_source_init_oid(struct grep_source *gs, const char *name,
1900 const char *path, const struct object_id *oid,
1901 struct repository *repo)
1903 gs->type = GREP_SOURCE_OID;
1904 gs->name = xstrdup_or_null(name);
1905 gs->path = xstrdup_or_null(path);
1906 gs->buf = NULL;
1907 gs->size = 0;
1908 gs->driver = NULL;
1909 gs->identifier = oiddup(oid);
1910 gs->repo = repo;
1913 void grep_source_clear(struct grep_source *gs)
1915 FREE_AND_NULL(gs->name);
1916 FREE_AND_NULL(gs->path);
1917 FREE_AND_NULL(gs->identifier);
1918 grep_source_clear_data(gs);
1921 void grep_source_clear_data(struct grep_source *gs)
1923 switch (gs->type) {
1924 case GREP_SOURCE_FILE:
1925 case GREP_SOURCE_OID:
1926 /* these types own the buffer */
1927 free((char *)gs->buf);
1928 gs->buf = NULL;
1929 gs->size = 0;
1930 break;
1931 case GREP_SOURCE_BUF:
1932 /* leave user-provided buf intact */
1933 break;
1937 static int grep_source_load_oid(struct grep_source *gs)
1939 enum object_type type;
1941 gs->buf = repo_read_object_file(gs->repo, gs->identifier, &type,
1942 &gs->size);
1943 if (!gs->buf)
1944 return error(_("'%s': unable to read %s"),
1945 gs->name,
1946 oid_to_hex(gs->identifier));
1947 return 0;
1950 static int grep_source_load_file(struct grep_source *gs)
1952 const char *filename = gs->identifier;
1953 struct stat st;
1954 char *data;
1955 size_t size;
1956 int i;
1958 if (lstat(filename, &st) < 0) {
1959 err_ret:
1960 if (errno != ENOENT)
1961 error_errno(_("failed to stat '%s'"), filename);
1962 return -1;
1964 if (!S_ISREG(st.st_mode))
1965 return -1;
1966 size = xsize_t(st.st_size);
1967 i = open(filename, O_RDONLY);
1968 if (i < 0)
1969 goto err_ret;
1970 data = xmallocz(size);
1971 if (st.st_size != read_in_full(i, data, size)) {
1972 error_errno(_("'%s': short read"), filename);
1973 close(i);
1974 free(data);
1975 return -1;
1977 close(i);
1979 gs->buf = data;
1980 gs->size = size;
1981 return 0;
1984 static int grep_source_load(struct grep_source *gs)
1986 if (gs->buf)
1987 return 0;
1989 switch (gs->type) {
1990 case GREP_SOURCE_FILE:
1991 return grep_source_load_file(gs);
1992 case GREP_SOURCE_OID:
1993 return grep_source_load_oid(gs);
1994 case GREP_SOURCE_BUF:
1995 return gs->buf ? 0 : -1;
1997 BUG("invalid grep_source type to load");
2000 void grep_source_load_driver(struct grep_source *gs,
2001 struct index_state *istate)
2003 if (gs->driver)
2004 return;
2006 grep_attr_lock();
2007 if (gs->path)
2008 gs->driver = userdiff_find_by_path(istate, gs->path);
2009 if (!gs->driver)
2010 gs->driver = userdiff_find_by_name("default");
2011 grep_attr_unlock();
2014 static int grep_source_is_binary(struct grep_source *gs,
2015 struct index_state *istate)
2017 grep_source_load_driver(gs, istate);
2018 if (gs->driver->binary != -1)
2019 return gs->driver->binary;
2021 if (!grep_source_load(gs))
2022 return buffer_is_binary(gs->buf, gs->size);
2024 return 0;