5 #include "xdiff-interface.h"
11 static int grep_source_load(struct grep_source *gs);
12 static int grep_source_is_binary(struct grep_source *gs);
14 static struct grep_opt grep_defaults;
16 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
18 fwrite(buf, size, 1, stdout);
22 * Initialize the grep_defaults template with hardcoded defaults.
23 * We could let the compiler do this, but without C99 initializers
24 * the code gets unwieldy and unreadable, so...
26 void init_grep_defaults(void)
28 struct grep_opt *opt = &grep_defaults;
35 memset(opt, 0, sizeof(*opt));
38 opt->regflags = REG_NEWLINE;
40 opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
41 color_set(opt->color_context, "");
42 color_set(opt->color_filename, "");
43 color_set(opt->color_function, "");
44 color_set(opt->color_lineno, "");
45 color_set(opt->color_match_context, GIT_COLOR_BOLD_RED);
46 color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
47 color_set(opt->color_selected, "");
48 color_set(opt->color_sep, GIT_COLOR_CYAN);
50 opt->output = std_output;
53 static int parse_pattern_type_arg(const char *opt, const char *arg)
55 if (!strcmp(arg, "default"))
56 return GREP_PATTERN_TYPE_UNSPECIFIED;
57 else if (!strcmp(arg, "basic"))
58 return GREP_PATTERN_TYPE_BRE;
59 else if (!strcmp(arg, "extended"))
60 return GREP_PATTERN_TYPE_ERE;
61 else if (!strcmp(arg, "fixed"))
62 return GREP_PATTERN_TYPE_FIXED;
63 else if (!strcmp(arg, "perl"))
64 return GREP_PATTERN_TYPE_PCRE;
65 die("bad %s argument: %s", opt, arg);
69 * Read the configuration file once and store it in
70 * the grep_defaults template.
72 int grep_config(const char *var, const char *value, void *cb)
74 struct grep_opt *opt = &grep_defaults;
77 if (userdiff_config(var, value) < 0)
80 if (!strcmp(var, "grep.extendedregexp")) {
81 opt->extended_regexp_option = git_config_bool(var, value);
85 if (!strcmp(var, "grep.patterntype")) {
86 opt->pattern_type_option = parse_pattern_type_arg(var, value);
90 if (!strcmp(var, "grep.linenumber")) {
91 opt->linenum = git_config_bool(var, value);
95 if (!strcmp(var, "grep.fullname")) {
96 opt->relative = !git_config_bool(var, value);
100 if (!strcmp(var, "color.grep"))
101 opt->color = git_config_colorbool(var, value);
102 else if (!strcmp(var, "color.grep.context"))
103 color = opt->color_context;
104 else if (!strcmp(var, "color.grep.filename"))
105 color = opt->color_filename;
106 else if (!strcmp(var, "color.grep.function"))
107 color = opt->color_function;
108 else if (!strcmp(var, "color.grep.linenumber"))
109 color = opt->color_lineno;
110 else if (!strcmp(var, "color.grep.matchcontext"))
111 color = opt->color_match_context;
112 else if (!strcmp(var, "color.grep.matchselected"))
113 color = opt->color_match_selected;
114 else if (!strcmp(var, "color.grep.selected"))
115 color = opt->color_selected;
116 else if (!strcmp(var, "color.grep.separator"))
117 color = opt->color_sep;
118 else if (!strcmp(var, "color.grep.match")) {
121 return config_error_nonbool(var);
122 rc |= color_parse(value, opt->color_match_context);
123 rc |= color_parse(value, opt->color_match_selected);
129 return config_error_nonbool(var);
130 return color_parse(value, color);
136 * Initialize one instance of grep_opt and copy the
137 * default values from the template we read the configuration
138 * information in an earlier call to git_config(grep_config).
140 void grep_init(struct grep_opt *opt, const char *prefix)
142 struct grep_opt *def = &grep_defaults;
144 memset(opt, 0, sizeof(*opt));
145 opt->prefix = prefix;
146 opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
147 opt->pattern_tail = &opt->pattern_list;
148 opt->header_tail = &opt->header_list;
150 opt->color = def->color;
151 opt->extended_regexp_option = def->extended_regexp_option;
152 opt->pattern_type_option = def->pattern_type_option;
153 opt->linenum = def->linenum;
154 opt->max_depth = def->max_depth;
155 opt->pathname = def->pathname;
156 opt->regflags = def->regflags;
157 opt->relative = def->relative;
158 opt->output = def->output;
160 color_set(opt->color_context, def->color_context);
161 color_set(opt->color_filename, def->color_filename);
162 color_set(opt->color_function, def->color_function);
163 color_set(opt->color_lineno, def->color_lineno);
164 color_set(opt->color_match_context, def->color_match_context);
165 color_set(opt->color_match_selected, def->color_match_selected);
166 color_set(opt->color_selected, def->color_selected);
167 color_set(opt->color_sep, def->color_sep);
170 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
172 switch (pattern_type) {
173 case GREP_PATTERN_TYPE_UNSPECIFIED:
176 case GREP_PATTERN_TYPE_BRE:
182 case GREP_PATTERN_TYPE_ERE:
186 opt->regflags |= REG_EXTENDED;
189 case GREP_PATTERN_TYPE_FIXED:
195 case GREP_PATTERN_TYPE_PCRE:
202 * It's important that pcre1 always be assigned to
203 * even when there's no USE_LIBPCRE* defined. We still
204 * call the PCRE stub function, it just dies with
205 * "cannot use Perl-compatible regexes[...]".
214 void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
216 if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED)
217 grep_set_pattern_type_option(pattern_type, opt);
218 else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
219 grep_set_pattern_type_option(opt->pattern_type_option, opt);
220 else if (opt->extended_regexp_option)
221 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
224 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
225 const char *origin, int no,
226 enum grep_pat_token t,
227 enum grep_header_field field)
229 struct grep_pat *p = xcalloc(1, sizeof(*p));
230 p->pattern = xmemdupz(pat, patlen);
231 p->patternlen = patlen;
239 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
246 case GREP_PATTERN: /* atom */
247 case GREP_PATTERN_HEAD:
248 case GREP_PATTERN_BODY:
250 struct grep_pat *new_pat;
252 char *cp = p->pattern + p->patternlen, *nl = NULL;
253 while (++len <= p->patternlen) {
254 if (*(--cp) == '\n') {
261 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
262 p->no, p->token, p->field);
263 new_pat->next = p->next;
265 *tail = &new_pat->next;
268 p->patternlen -= len;
276 void append_header_grep_pattern(struct grep_opt *opt,
277 enum grep_header_field field, const char *pat)
279 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
280 GREP_PATTERN_HEAD, field);
281 if (field == GREP_HEADER_REFLOG)
282 opt->use_reflog_filter = 1;
283 do_append_grep_pat(&opt->header_tail, p);
286 void append_grep_pattern(struct grep_opt *opt, const char *pat,
287 const char *origin, int no, enum grep_pat_token t)
289 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
292 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
293 const char *origin, int no, enum grep_pat_token t)
295 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
296 do_append_grep_pat(&opt->pattern_tail, p);
299 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
301 struct grep_pat *pat;
302 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
305 ret->pattern_list = NULL;
306 ret->pattern_tail = &ret->pattern_list;
308 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
310 if(pat->token == GREP_PATTERN_HEAD)
311 append_header_grep_pattern(ret, pat->field,
314 append_grep_pat(ret, pat->pattern, pat->patternlen,
315 pat->origin, pat->no, pat->token);
321 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
327 xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no);
329 xsnprintf(where, sizeof(where), "%s, ", p->origin);
333 die("%s'%s': %s", where, p->pattern, error);
336 static int is_fixed(const char *s, size_t len)
340 for (i = 0; i < len; i++) {
341 if (is_regex_special(s[i]))
348 static int has_null(const char *s, size_t len)
351 * regcomp cannot accept patterns with NULs so when using it
352 * we consider any pattern containing a NUL fixed.
354 if (memchr(s, 0, len))
361 static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
365 int options = PCRE_MULTILINE;
367 if (opt->ignore_case) {
368 if (has_non_ascii(p->pattern))
369 p->pcre1_tables = pcre_maketables();
370 options |= PCRE_CASELESS;
372 if (is_utf8_locale() && has_non_ascii(p->pattern))
373 options |= PCRE_UTF8;
375 p->pcre1_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
377 if (!p->pcre1_regexp)
378 compile_regexp_failed(p, error);
380 p->pcre1_extra_info = pcre_study(p->pcre1_regexp, PCRE_STUDY_JIT_COMPILE, &error);
381 if (!p->pcre1_extra_info && error)
384 #ifdef GIT_PCRE1_USE_JIT
385 pcre_config(PCRE_CONFIG_JIT, &p->pcre1_jit_on);
386 if (p->pcre1_jit_on == 1) {
387 p->pcre1_jit_stack = pcre_jit_stack_alloc(1, 1024 * 1024);
388 if (!p->pcre1_jit_stack)
389 die("Couldn't allocate PCRE JIT stack");
390 pcre_assign_jit_stack(p->pcre1_extra_info, NULL, p->pcre1_jit_stack);
391 } else if (p->pcre1_jit_on != 0) {
392 die("BUG: The pcre1_jit_on variable should be 0 or 1, not %d",
398 static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
399 regmatch_t *match, int eflags)
401 int ovector[30], ret, flags = 0;
403 if (eflags & REG_NOTBOL)
404 flags |= PCRE_NOTBOL;
406 #ifdef GIT_PCRE1_USE_JIT
407 if (p->pcre1_jit_on) {
408 ret = pcre_jit_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
409 eol - line, 0, flags, ovector,
410 ARRAY_SIZE(ovector), p->pcre1_jit_stack);
414 ret = pcre_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
415 eol - line, 0, flags, ovector,
416 ARRAY_SIZE(ovector));
419 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
420 die("pcre_exec failed with error code %d", ret);
423 match->rm_so = ovector[0];
424 match->rm_eo = ovector[1];
430 static void free_pcre1_regexp(struct grep_pat *p)
432 pcre_free(p->pcre1_regexp);
433 #ifdef GIT_PCRE1_USE_JIT
434 if (p->pcre1_jit_on) {
435 pcre_free_study(p->pcre1_extra_info);
436 pcre_jit_stack_free(p->pcre1_jit_stack);
440 pcre_free(p->pcre1_extra_info);
442 pcre_free((void *)p->pcre1_tables);
444 #else /* !USE_LIBPCRE1 */
445 static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
447 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
450 static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
451 regmatch_t *match, int eflags)
456 static void free_pcre1_regexp(struct grep_pat *p)
459 #endif /* !USE_LIBPCRE1 */
462 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
465 PCRE2_UCHAR errbuf[256];
466 PCRE2_SIZE erroffset;
467 int options = PCRE2_MULTILINE;
468 const uint8_t *character_tables = NULL;
473 p->pcre2_compile_context = NULL;
475 if (opt->ignore_case) {
476 if (has_non_ascii(p->pattern)) {
477 character_tables = pcre2_maketables(NULL);
478 p->pcre2_compile_context = pcre2_compile_context_create(NULL);
479 pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
481 options |= PCRE2_CASELESS;
483 if (is_utf8_locale() && has_non_ascii(p->pattern))
484 options |= PCRE2_UTF;
486 p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
487 p->patternlen, options, &error, &erroffset,
488 p->pcre2_compile_context);
490 if (p->pcre2_pattern) {
491 p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL);
492 if (!p->pcre2_match_data)
493 die("Couldn't allocate PCRE2 match data");
495 pcre2_get_error_message(error, errbuf, sizeof(errbuf));
496 compile_regexp_failed(p, (const char *)&errbuf);
499 pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on);
500 if (p->pcre2_jit_on == 1) {
501 jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
503 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret);
504 p->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, NULL);
505 if (!p->pcre2_jit_stack)
506 die("Couldn't allocate PCRE2 JIT stack");
507 p->pcre2_match_context = pcre2_match_context_create(NULL);
508 if (!p->pcre2_match_context)
509 die("Couldn't allocate PCRE2 match context");
510 pcre2_jit_stack_assign(p->pcre2_match_context, NULL, p->pcre2_jit_stack);
511 } else if (p->pcre2_jit_on != 0) {
512 die("BUG: The pcre2_jit_on variable should be 0 or 1, not %d",
517 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
518 regmatch_t *match, int eflags)
522 PCRE2_UCHAR errbuf[256];
524 if (eflags & REG_NOTBOL)
525 flags |= PCRE2_NOTBOL;
528 ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line,
529 eol - line, 0, flags, p->pcre2_match_data,
532 ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line,
533 eol - line, 0, flags, p->pcre2_match_data,
536 if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) {
537 pcre2_get_error_message(ret, errbuf, sizeof(errbuf));
538 die("%s failed with error code %d: %s",
539 (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret,
543 ovector = pcre2_get_ovector_pointer(p->pcre2_match_data);
545 match->rm_so = (int)ovector[0];
546 match->rm_eo = (int)ovector[1];
552 static void free_pcre2_pattern(struct grep_pat *p)
554 pcre2_compile_context_free(p->pcre2_compile_context);
555 pcre2_code_free(p->pcre2_pattern);
556 pcre2_match_data_free(p->pcre2_match_data);
557 pcre2_jit_stack_free(p->pcre2_jit_stack);
558 pcre2_match_context_free(p->pcre2_match_context);
560 #else /* !USE_LIBPCRE2 */
561 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
564 * Unreachable until USE_LIBPCRE2 becomes synonymous with
565 * USE_LIBPCRE. See the sibling comment in
566 * grep_set_pattern_type_option().
568 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
571 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
572 regmatch_t *match, int eflags)
577 static void free_pcre2_pattern(struct grep_pat *p)
580 #endif /* !USE_LIBPCRE2 */
582 static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
584 struct strbuf sb = STRBUF_INIT;
586 int regflags = opt->regflags;
588 basic_regex_quote_buf(&sb, p->pattern);
589 if (opt->ignore_case)
590 regflags |= REG_ICASE;
591 err = regcomp(&p->regexp, sb.buf, regflags);
593 fprintf(stderr, "fixed %s\n", sb.buf);
597 regerror(err, &p->regexp, errbuf, sizeof(errbuf));
599 compile_regexp_failed(p, errbuf);
603 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
605 int icase, ascii_only;
608 p->word_regexp = opt->word_regexp;
609 p->ignore_case = opt->ignore_case;
610 icase = opt->regflags & REG_ICASE || p->ignore_case;
611 ascii_only = !has_non_ascii(p->pattern);
614 * Even when -F (fixed) asks us to do a non-regexp search, we
615 * may not be able to correctly case-fold when -i
616 * (ignore-case) is asked (in which case, we'll synthesize a
617 * regexp to match the pattern that matches regexp special
618 * characters literally, while ignoring case differences). On
619 * the other hand, even without -F, if the pattern does not
620 * have any regexp special characters and there is no need for
621 * case-folding search, we can internally turn it into a
622 * simple string match using kws. p->fixed tells us if we
626 has_null(p->pattern, p->patternlen) ||
627 is_fixed(p->pattern, p->patternlen))
628 p->fixed = !icase || ascii_only;
631 p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL);
632 kwsincr(p->kws, p->pattern, p->patternlen);
635 } else if (opt->fixed) {
637 * We come here when the pattern has the non-ascii
638 * characters we cannot case-fold, and asked to
641 compile_fixed_regexp(p, opt);
646 compile_pcre2_pattern(p, opt);
651 compile_pcre1_regexp(p, opt);
655 err = regcomp(&p->regexp, p->pattern, opt->regflags);
658 regerror(err, &p->regexp, errbuf, 1024);
660 compile_regexp_failed(p, errbuf);
664 static struct grep_expr *compile_pattern_or(struct grep_pat **);
665 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
674 case GREP_PATTERN: /* atom */
675 case GREP_PATTERN_HEAD:
676 case GREP_PATTERN_BODY:
677 x = xcalloc(1, sizeof (struct grep_expr));
678 x->node = GREP_NODE_ATOM;
682 case GREP_OPEN_PAREN:
684 x = compile_pattern_or(list);
685 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
686 die("unmatched parenthesis");
687 *list = (*list)->next;
694 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
705 die("--not not followed by pattern expression");
707 x = xcalloc(1, sizeof (struct grep_expr));
708 x->node = GREP_NODE_NOT;
709 x->u.unary = compile_pattern_not(list);
711 die("--not followed by non pattern expression");
714 return compile_pattern_atom(list);
718 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
721 struct grep_expr *x, *y, *z;
723 x = compile_pattern_not(list);
725 if (p && p->token == GREP_AND) {
727 die("--and not followed by pattern expression");
729 y = compile_pattern_and(list);
731 die("--and not followed by pattern expression");
732 z = xcalloc(1, sizeof (struct grep_expr));
733 z->node = GREP_NODE_AND;
734 z->u.binary.left = x;
735 z->u.binary.right = y;
741 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
744 struct grep_expr *x, *y, *z;
746 x = compile_pattern_and(list);
748 if (x && p && p->token != GREP_CLOSE_PAREN) {
749 y = compile_pattern_or(list);
751 die("not a pattern expression %s", p->pattern);
752 z = xcalloc(1, sizeof (struct grep_expr));
753 z->node = GREP_NODE_OR;
754 z->u.binary.left = x;
755 z->u.binary.right = y;
761 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
763 return compile_pattern_or(list);
766 static void indent(int in)
772 static void dump_grep_pat(struct grep_pat *p)
775 case GREP_AND: fprintf(stderr, "*and*"); break;
776 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
777 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
778 case GREP_NOT: fprintf(stderr, "*not*"); break;
779 case GREP_OR: fprintf(stderr, "*or*"); break;
781 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
782 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
783 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
788 case GREP_PATTERN_HEAD:
789 fprintf(stderr, "<head %d>", p->field); break;
790 case GREP_PATTERN_BODY:
791 fprintf(stderr, "<body>"); break;
795 case GREP_PATTERN_HEAD:
796 case GREP_PATTERN_BODY:
798 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
804 static void dump_grep_expression_1(struct grep_expr *x, int in)
809 fprintf(stderr, "true\n");
812 dump_grep_pat(x->u.atom);
815 fprintf(stderr, "(not\n");
816 dump_grep_expression_1(x->u.unary, in+1);
818 fprintf(stderr, ")\n");
821 fprintf(stderr, "(and\n");
822 dump_grep_expression_1(x->u.binary.left, in+1);
823 dump_grep_expression_1(x->u.binary.right, in+1);
825 fprintf(stderr, ")\n");
828 fprintf(stderr, "(or\n");
829 dump_grep_expression_1(x->u.binary.left, in+1);
830 dump_grep_expression_1(x->u.binary.right, in+1);
832 fprintf(stderr, ")\n");
837 static void dump_grep_expression(struct grep_opt *opt)
839 struct grep_expr *x = opt->pattern_expression;
842 fprintf(stderr, "[all-match]\n");
843 dump_grep_expression_1(x, 0);
847 static struct grep_expr *grep_true_expr(void)
849 struct grep_expr *z = xcalloc(1, sizeof(*z));
850 z->node = GREP_NODE_TRUE;
854 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
856 struct grep_expr *z = xcalloc(1, sizeof(*z));
857 z->node = GREP_NODE_OR;
858 z->u.binary.left = left;
859 z->u.binary.right = right;
863 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
866 struct grep_expr *header_expr;
867 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
868 enum grep_header_field fld;
870 if (!opt->header_list)
873 for (p = opt->header_list; p; p = p->next) {
874 if (p->token != GREP_PATTERN_HEAD)
875 die("BUG: a non-header pattern in grep header list.");
876 if (p->field < GREP_HEADER_FIELD_MIN ||
877 GREP_HEADER_FIELD_MAX <= p->field)
878 die("BUG: unknown header field %d", p->field);
879 compile_regexp(p, opt);
882 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
883 header_group[fld] = NULL;
885 for (p = opt->header_list; p; p = p->next) {
887 struct grep_pat *pp = p;
889 h = compile_pattern_atom(&pp);
890 if (!h || pp != p->next)
891 die("BUG: malformed header expr");
892 if (!header_group[p->field]) {
893 header_group[p->field] = h;
896 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
901 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
902 if (!header_group[fld])
905 header_expr = grep_true_expr();
906 header_expr = grep_or_expr(header_group[fld], header_expr);
911 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
913 struct grep_expr *z = x;
916 assert(x->node == GREP_NODE_OR);
917 if (x->u.binary.right &&
918 x->u.binary.right->node == GREP_NODE_TRUE) {
919 x->u.binary.right = y;
922 x = x->u.binary.right;
927 static void compile_grep_patterns_real(struct grep_opt *opt)
930 struct grep_expr *header_expr = prep_header_patterns(opt);
932 for (p = opt->pattern_list; p; p = p->next) {
934 case GREP_PATTERN: /* atom */
935 case GREP_PATTERN_HEAD:
936 case GREP_PATTERN_BODY:
937 compile_regexp(p, opt);
945 if (opt->all_match || header_expr)
947 else if (!opt->extended && !opt->debug)
950 p = opt->pattern_list;
952 opt->pattern_expression = compile_pattern_expr(&p);
954 die("incomplete pattern expression: %s", p->pattern);
959 if (!opt->pattern_expression)
960 opt->pattern_expression = header_expr;
961 else if (opt->all_match)
962 opt->pattern_expression = grep_splice_or(header_expr,
963 opt->pattern_expression);
965 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
970 void compile_grep_patterns(struct grep_opt *opt)
972 compile_grep_patterns_real(opt);
974 dump_grep_expression(opt);
977 static void free_pattern_expr(struct grep_expr *x)
984 free_pattern_expr(x->u.unary);
988 free_pattern_expr(x->u.binary.left);
989 free_pattern_expr(x->u.binary.right);
995 void free_grep_patterns(struct grep_opt *opt)
997 struct grep_pat *p, *n;
999 for (p = opt->pattern_list; p; p = n) {
1002 case GREP_PATTERN: /* atom */
1003 case GREP_PATTERN_HEAD:
1004 case GREP_PATTERN_BODY:
1007 else if (p->pcre1_regexp)
1008 free_pcre1_regexp(p);
1009 else if (p->pcre2_pattern)
1010 free_pcre2_pattern(p);
1012 regfree(&p->regexp);
1023 free_pattern_expr(opt->pattern_expression);
1026 static char *end_of_line(char *cp, unsigned long *left)
1028 unsigned long l = *left;
1029 while (l && *cp != '\n') {
1037 static int word_char(char ch)
1039 return isalnum(ch) || ch == '_';
1042 static void output_color(struct grep_opt *opt, const void *data, size_t size,
1045 if (want_color(opt->color) && color && color[0]) {
1046 opt->output(opt, color, strlen(color));
1047 opt->output(opt, data, size);
1048 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
1050 opt->output(opt, data, size);
1053 static void output_sep(struct grep_opt *opt, char sign)
1055 if (opt->null_following_name)
1056 opt->output(opt, "\0", 1);
1058 output_color(opt, &sign, 1, opt->color_sep);
1061 static void show_name(struct grep_opt *opt, const char *name)
1063 output_color(opt, name, strlen(name), opt->color_filename);
1064 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
1067 static int fixmatch(struct grep_pat *p, char *line, char *eol,
1070 struct kwsmatch kwsm;
1071 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
1073 match->rm_so = match->rm_eo = -1;
1076 match->rm_so = offset;
1077 match->rm_eo = match->rm_so + kwsm.size[0];
1082 static int patmatch(struct grep_pat *p, char *line, char *eol,
1083 regmatch_t *match, int eflags)
1088 hit = !fixmatch(p, line, eol, match);
1089 else if (p->pcre1_regexp)
1090 hit = !pcre1match(p, line, eol, match, eflags);
1091 else if (p->pcre2_pattern)
1092 hit = !pcre2match(p, line, eol, match, eflags);
1094 hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
1100 static int strip_timestamp(char *bol, char **eol_p)
1105 while (bol < --eol) {
1119 } header_field[] = {
1121 { "committer ", 10 },
1125 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
1126 enum grep_context ctx,
1127 regmatch_t *pmatch, int eflags)
1131 const char *start = bol;
1133 if ((p->token != GREP_PATTERN) &&
1134 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
1137 if (p->token == GREP_PATTERN_HEAD) {
1140 assert(p->field < ARRAY_SIZE(header_field));
1141 field = header_field[p->field].field;
1142 len = header_field[p->field].len;
1143 if (strncmp(bol, field, len))
1147 case GREP_HEADER_AUTHOR:
1148 case GREP_HEADER_COMMITTER:
1149 saved_ch = strip_timestamp(bol, &eol);
1157 hit = patmatch(p, bol, eol, pmatch, eflags);
1159 if (hit && p->word_regexp) {
1160 if ((pmatch[0].rm_so < 0) ||
1161 (eol - bol) < pmatch[0].rm_so ||
1162 (pmatch[0].rm_eo < 0) ||
1163 (eol - bol) < pmatch[0].rm_eo)
1164 die("regexp returned nonsense");
1166 /* Match beginning must be either beginning of the
1167 * line, or at word boundary (i.e. the last char must
1168 * not be a word char). Similarly, match end must be
1169 * either end of the line, or at word boundary
1170 * (i.e. the next char must not be a word char).
1172 if ( ((pmatch[0].rm_so == 0) ||
1173 !word_char(bol[pmatch[0].rm_so-1])) &&
1174 ((pmatch[0].rm_eo == (eol-bol)) ||
1175 !word_char(bol[pmatch[0].rm_eo])) )
1180 /* Words consist of at least one character. */
1181 if (pmatch->rm_so == pmatch->rm_eo)
1184 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
1185 /* There could be more than one match on the
1186 * line, and the first match might not be
1187 * strict word match. But later ones could be!
1188 * Forward to the next possible start, i.e. the
1189 * next position following a non-word char.
1191 bol = pmatch[0].rm_so + bol + 1;
1192 while (word_char(bol[-1]) && bol < eol)
1194 eflags |= REG_NOTBOL;
1199 if (p->token == GREP_PATTERN_HEAD && saved_ch)
1202 pmatch[0].rm_so += bol - start;
1203 pmatch[0].rm_eo += bol - start;
1208 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
1209 enum grep_context ctx, int collect_hits)
1215 die("Not a valid grep expression");
1217 case GREP_NODE_TRUE:
1220 case GREP_NODE_ATOM:
1221 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
1224 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
1227 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
1229 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
1233 return (match_expr_eval(x->u.binary.left,
1234 bol, eol, ctx, 0) ||
1235 match_expr_eval(x->u.binary.right,
1237 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
1238 x->u.binary.left->hit |= h;
1239 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
1242 die("Unexpected node type (internal error) %d", x->node);
1249 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1250 enum grep_context ctx, int collect_hits)
1252 struct grep_expr *x = opt->pattern_expression;
1253 return match_expr_eval(x, bol, eol, ctx, collect_hits);
1256 static int match_line(struct grep_opt *opt, char *bol, char *eol,
1257 enum grep_context ctx, int collect_hits)
1263 return match_expr(opt, bol, eol, ctx, collect_hits);
1265 /* we do not call with collect_hits without being extended */
1266 for (p = opt->pattern_list; p; p = p->next) {
1267 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
1273 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1274 enum grep_context ctx,
1275 regmatch_t *pmatch, int eflags)
1279 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
1281 if (match.rm_so < 0 || match.rm_eo < 0)
1283 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1284 if (match.rm_so > pmatch->rm_so)
1286 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1289 pmatch->rm_so = match.rm_so;
1290 pmatch->rm_eo = match.rm_eo;
1294 static int next_match(struct grep_opt *opt, char *bol, char *eol,
1295 enum grep_context ctx, regmatch_t *pmatch, int eflags)
1300 pmatch->rm_so = pmatch->rm_eo = -1;
1302 for (p = opt->pattern_list; p; p = p->next) {
1304 case GREP_PATTERN: /* atom */
1305 case GREP_PATTERN_HEAD:
1306 case GREP_PATTERN_BODY:
1307 hit |= match_next_pattern(p, bol, eol, ctx,
1318 static void show_line(struct grep_opt *opt, char *bol, char *eol,
1319 const char *name, unsigned lno, char sign)
1321 int rest = eol - bol;
1322 const char *match_color, *line_color = NULL;
1324 if (opt->file_break && opt->last_shown == 0) {
1325 if (opt->show_hunk_mark)
1326 opt->output(opt, "\n", 1);
1327 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1328 if (opt->last_shown == 0) {
1329 if (opt->show_hunk_mark) {
1330 output_color(opt, "--", 2, opt->color_sep);
1331 opt->output(opt, "\n", 1);
1333 } else if (lno > opt->last_shown + 1) {
1334 output_color(opt, "--", 2, opt->color_sep);
1335 opt->output(opt, "\n", 1);
1338 if (opt->heading && opt->last_shown == 0) {
1339 output_color(opt, name, strlen(name), opt->color_filename);
1340 opt->output(opt, "\n", 1);
1342 opt->last_shown = lno;
1344 if (!opt->heading && opt->pathname) {
1345 output_color(opt, name, strlen(name), opt->color_filename);
1346 output_sep(opt, sign);
1350 xsnprintf(buf, sizeof(buf), "%d", lno);
1351 output_color(opt, buf, strlen(buf), opt->color_lineno);
1352 output_sep(opt, sign);
1356 enum grep_context ctx = GREP_CONTEXT_BODY;
1361 match_color = opt->color_match_selected;
1363 match_color = opt->color_match_context;
1365 line_color = opt->color_selected;
1366 else if (sign == '-')
1367 line_color = opt->color_context;
1368 else if (sign == '=')
1369 line_color = opt->color_function;
1371 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1372 if (match.rm_so == match.rm_eo)
1375 output_color(opt, bol, match.rm_so, line_color);
1376 output_color(opt, bol + match.rm_so,
1377 match.rm_eo - match.rm_so, match_color);
1379 rest -= match.rm_eo;
1380 eflags = REG_NOTBOL;
1384 output_color(opt, bol, rest, line_color);
1385 opt->output(opt, "\n", 1);
1392 * This lock protects access to the gitattributes machinery, which is
1395 pthread_mutex_t grep_attr_mutex;
1397 static inline void grep_attr_lock(void)
1400 pthread_mutex_lock(&grep_attr_mutex);
1403 static inline void grep_attr_unlock(void)
1406 pthread_mutex_unlock(&grep_attr_mutex);
1410 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1412 pthread_mutex_t grep_read_mutex;
1415 #define grep_attr_lock()
1416 #define grep_attr_unlock()
1419 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1421 xdemitconf_t *xecfg = opt->priv;
1422 if (xecfg && !xecfg->find_func) {
1423 grep_source_load_driver(gs);
1424 if (gs->driver->funcname.pattern) {
1425 const struct userdiff_funcname *pe = &gs->driver->funcname;
1426 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1428 xecfg = opt->priv = NULL;
1434 return xecfg->find_func(bol, eol - bol, buf, 1,
1435 xecfg->find_func_priv) >= 0;
1440 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1445 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1446 char *bol, unsigned lno)
1448 while (bol > gs->buf) {
1451 while (bol > gs->buf && bol[-1] != '\n')
1455 if (lno <= opt->last_shown)
1458 if (match_funcname(opt, gs, bol, eol)) {
1459 show_line(opt, bol, eol, gs->name, lno, '=');
1465 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1466 char *bol, char *end, unsigned lno)
1468 unsigned cur = lno, from = 1, funcname_lno = 0;
1469 int funcname_needed = !!opt->funcname;
1471 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1472 funcname_needed = 2;
1474 if (opt->pre_context < lno)
1475 from = lno - opt->pre_context;
1476 if (from <= opt->last_shown)
1477 from = opt->last_shown + 1;
1480 while (bol > gs->buf &&
1481 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1484 while (bol > gs->buf && bol[-1] != '\n')
1487 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1489 funcname_needed = 0;
1493 /* We need to look even further back to find a function signature. */
1494 if (opt->funcname && funcname_needed)
1495 show_funcname_line(opt, gs, bol, cur);
1499 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1501 while (*eol != '\n')
1503 show_line(opt, bol, eol, gs->name, cur, sign);
1509 static int should_lookahead(struct grep_opt *opt)
1514 return 0; /* punt for too complex stuff */
1517 for (p = opt->pattern_list; p; p = p->next) {
1518 if (p->token != GREP_PATTERN)
1519 return 0; /* punt for "header only" and stuff */
1524 static int look_ahead(struct grep_opt *opt,
1525 unsigned long *left_p,
1529 unsigned lno = *lno_p;
1532 char *sp, *last_bol;
1533 regoff_t earliest = -1;
1535 for (p = opt->pattern_list; p; p = p->next) {
1539 hit = patmatch(p, bol, bol + *left_p, &m, 0);
1540 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1542 if (earliest < 0 || m.rm_so < earliest)
1547 *bol_p = bol + *left_p;
1551 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1552 ; /* find the beginning of the line */
1555 for (sp = bol; sp < last_bol; sp++) {
1559 *left_p -= last_bol - bol;
1565 static int fill_textconv_grep(struct userdiff_driver *driver,
1566 struct grep_source *gs)
1568 struct diff_filespec *df;
1572 if (!driver || !driver->textconv)
1573 return grep_source_load(gs);
1576 * The textconv interface is intimately tied to diff_filespecs, so we
1577 * have to pretend to be one. If we could unify the grep_source
1578 * and diff_filespec structs, this mess could just go away.
1580 df = alloc_filespec(gs->path);
1582 case GREP_SOURCE_OID:
1583 fill_filespec(df, gs->identifier, 1, 0100644);
1585 case GREP_SOURCE_FILE:
1586 fill_filespec(df, &null_oid, 0, 0100644);
1589 die("BUG: attempt to textconv something without a path?");
1593 * fill_textconv is not remotely thread-safe; it may load objects
1594 * behind the scenes, and it modifies the global diff tempfile
1598 size = fill_textconv(driver, df, &buf);
1603 * The normal fill_textconv usage by the diff machinery would just keep
1604 * the textconv'd buf separate from the diff_filespec. But much of the
1605 * grep code passes around a grep_source and assumes that its "buf"
1606 * pointer is the beginning of the thing we are searching. So let's
1607 * install our textconv'd version into the grep_source, taking care not
1608 * to leak any existing buffer.
1610 grep_source_clear_data(gs);
1617 static int is_empty_line(const char *bol, const char *eol)
1619 while (bol < eol && isspace(*bol))
1624 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1627 char *peek_bol = NULL;
1630 unsigned last_hit = 0;
1631 int binary_match_only = 0;
1633 int try_lookahead = 0;
1634 int show_function = 0;
1635 struct userdiff_driver *textconv = NULL;
1636 enum grep_context ctx = GREP_CONTEXT_HEAD;
1640 opt->output = std_output;
1642 if (opt->pre_context || opt->post_context || opt->file_break ||
1644 /* Show hunk marks, except for the first file. */
1645 if (opt->last_shown)
1646 opt->show_hunk_mark = 1;
1648 * If we're using threads then we can't easily identify
1649 * the first file. Always put hunk marks in that case
1650 * and skip the very first one later in work_done().
1652 if (opt->output != std_output)
1653 opt->show_hunk_mark = 1;
1655 opt->last_shown = 0;
1657 if (opt->allow_textconv) {
1658 grep_source_load_driver(gs);
1660 * We might set up the shared textconv cache data here, which
1661 * is not thread-safe.
1664 textconv = userdiff_get_textconv(gs->driver);
1669 * We know the result of a textconv is text, so we only have to care
1670 * about binary handling if we are not using it.
1673 switch (opt->binary) {
1674 case GREP_BINARY_DEFAULT:
1675 if (grep_source_is_binary(gs))
1676 binary_match_only = 1;
1678 case GREP_BINARY_NOMATCH:
1679 if (grep_source_is_binary(gs))
1680 return 0; /* Assume unmatch */
1682 case GREP_BINARY_TEXT:
1685 die("BUG: unknown binary handling mode");
1689 memset(&xecfg, 0, sizeof(xecfg));
1692 try_lookahead = should_lookahead(opt);
1694 if (fill_textconv_grep(textconv, gs) < 0)
1704 * look_ahead() skips quickly to the line that possibly
1705 * has the next hit; don't call it if we need to do
1706 * something more than just skipping the current line
1707 * in response to an unmatch for the current line. E.g.
1708 * inside a post-context window, we will show the current
1709 * line as a context around the previous hit when it
1714 && (show_function ||
1715 lno <= last_hit + opt->post_context))
1716 && look_ahead(opt, &left, &lno, &bol))
1718 eol = end_of_line(bol, &left);
1722 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1723 ctx = GREP_CONTEXT_BODY;
1725 hit = match_line(opt, bol, eol, ctx, collect_hits);
1731 /* "grep -v -e foo -e bla" should list lines
1732 * that do not have either, so inversion should
1737 if (opt->unmatch_name_only) {
1744 if (opt->status_only)
1746 if (opt->name_only) {
1747 show_name(opt, gs->name);
1752 if (binary_match_only) {
1753 opt->output(opt, "Binary file ", 12);
1754 output_color(opt, gs->name, strlen(gs->name),
1755 opt->color_filename);
1756 opt->output(opt, " matches\n", 9);
1759 /* Hit at this line. If we haven't shown the
1760 * pre-context lines, we would need to show them.
1762 if (opt->pre_context || opt->funcbody)
1763 show_pre_context(opt, gs, bol, eol, lno);
1764 else if (opt->funcname)
1765 show_funcname_line(opt, gs, bol, lno);
1766 show_line(opt, bol, eol, gs->name, lno, ':');
1772 if (show_function && (!peek_bol || peek_bol < bol)) {
1773 unsigned long peek_left = left;
1774 char *peek_eol = eol;
1777 * Trailing empty lines are not interesting.
1778 * Peek past them to see if they belong to the
1779 * body of the current function.
1782 while (is_empty_line(peek_bol, peek_eol)) {
1783 peek_bol = peek_eol + 1;
1784 peek_eol = end_of_line(peek_bol, &peek_left);
1787 if (match_funcname(opt, gs, peek_bol, peek_eol))
1790 if (show_function ||
1791 (last_hit && lno <= last_hit + opt->post_context)) {
1792 /* If the last hit is within the post context,
1793 * we need to show this line.
1795 show_line(opt, bol, eol, gs->name, lno, '-');
1809 if (opt->status_only)
1811 if (opt->unmatch_name_only) {
1812 /* We did not see any hit, so we want to show this */
1813 show_name(opt, gs->name);
1817 xdiff_clear_find_func(&xecfg);
1821 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1822 * which feels mostly useless but sometimes useful. Maybe
1823 * make it another option? For now suppress them.
1825 if (opt->count && count) {
1827 if (opt->pathname) {
1828 output_color(opt, gs->name, strlen(gs->name),
1829 opt->color_filename);
1830 output_sep(opt, ':');
1832 xsnprintf(buf, sizeof(buf), "%u\n", count);
1833 opt->output(opt, buf, strlen(buf));
1839 static void clr_hit_marker(struct grep_expr *x)
1841 /* All-hit markers are meaningful only at the very top level
1846 if (x->node != GREP_NODE_OR)
1848 x->u.binary.left->hit = 0;
1849 x = x->u.binary.right;
1853 static int chk_hit_marker(struct grep_expr *x)
1855 /* Top level nodes have hit markers. See if they all are hits */
1857 if (x->node != GREP_NODE_OR)
1859 if (!x->u.binary.left->hit)
1861 x = x->u.binary.right;
1865 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1868 * we do not have to do the two-pass grep when we do not check
1869 * buffer-wide "all-match".
1871 if (!opt->all_match)
1872 return grep_source_1(opt, gs, 0);
1874 /* Otherwise the toplevel "or" terms hit a bit differently.
1875 * We first clear hit markers from them.
1877 clr_hit_marker(opt->pattern_expression);
1878 grep_source_1(opt, gs, 1);
1880 if (!chk_hit_marker(opt->pattern_expression))
1883 return grep_source_1(opt, gs, 0);
1886 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1888 struct grep_source gs;
1891 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1895 r = grep_source(opt, &gs);
1897 grep_source_clear(&gs);
1901 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1902 const char *name, const char *path,
1903 const void *identifier)
1906 gs->name = xstrdup_or_null(name);
1907 gs->path = xstrdup_or_null(path);
1913 case GREP_SOURCE_FILE:
1914 gs->identifier = xstrdup(identifier);
1916 case GREP_SOURCE_SUBMODULE:
1918 gs->identifier = NULL;
1923 * If the identifier is non-NULL (in the submodule case) it
1924 * will be a SHA1 that needs to be copied.
1926 case GREP_SOURCE_OID:
1927 gs->identifier = oiddup(identifier);
1929 case GREP_SOURCE_BUF:
1930 gs->identifier = NULL;
1935 void grep_source_clear(struct grep_source *gs)
1937 FREE_AND_NULL(gs->name);
1938 FREE_AND_NULL(gs->path);
1939 FREE_AND_NULL(gs->identifier);
1940 grep_source_clear_data(gs);
1943 void grep_source_clear_data(struct grep_source *gs)
1946 case GREP_SOURCE_FILE:
1947 case GREP_SOURCE_OID:
1948 case GREP_SOURCE_SUBMODULE:
1949 FREE_AND_NULL(gs->buf);
1952 case GREP_SOURCE_BUF:
1953 /* leave user-provided buf intact */
1958 static int grep_source_load_oid(struct grep_source *gs)
1960 enum object_type type;
1963 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1967 return error(_("'%s': unable to read %s"),
1969 oid_to_hex(gs->identifier));
1973 static int grep_source_load_file(struct grep_source *gs)
1975 const char *filename = gs->identifier;
1981 if (lstat(filename, &st) < 0) {
1983 if (errno != ENOENT)
1984 error_errno(_("failed to stat '%s'"), filename);
1987 if (!S_ISREG(st.st_mode))
1989 size = xsize_t(st.st_size);
1990 i = open(filename, O_RDONLY);
1993 data = xmallocz(size);
1994 if (st.st_size != read_in_full(i, data, size)) {
1995 error_errno(_("'%s': short read"), filename);
2007 static int grep_source_load(struct grep_source *gs)
2013 case GREP_SOURCE_FILE:
2014 return grep_source_load_file(gs);
2015 case GREP_SOURCE_OID:
2016 return grep_source_load_oid(gs);
2017 case GREP_SOURCE_BUF:
2018 return gs->buf ? 0 : -1;
2019 case GREP_SOURCE_SUBMODULE:
2022 die("BUG: invalid grep_source type to load");
2025 void grep_source_load_driver(struct grep_source *gs)
2032 gs->driver = userdiff_find_by_path(gs->path);
2034 gs->driver = userdiff_find_by_name("default");
2038 static int grep_source_is_binary(struct grep_source *gs)
2040 grep_source_load_driver(gs);
2041 if (gs->driver->binary != -1)
2042 return gs->driver->binary;
2044 if (!grep_source_load(gs))
2045 return buffer_is_binary(gs->buf, gs->size);