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;
633 p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL);
634 kwsincr(p->kws, p->pattern, p->patternlen);
637 } else if (opt->fixed) {
639 * We come here when the pattern has the non-ascii
640 * characters we cannot case-fold, and asked to
643 compile_fixed_regexp(p, opt);
648 compile_pcre2_pattern(p, opt);
653 compile_pcre1_regexp(p, opt);
657 err = regcomp(&p->regexp, p->pattern, opt->regflags);
660 regerror(err, &p->regexp, errbuf, 1024);
662 compile_regexp_failed(p, errbuf);
666 static struct grep_expr *compile_pattern_or(struct grep_pat **);
667 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
676 case GREP_PATTERN: /* atom */
677 case GREP_PATTERN_HEAD:
678 case GREP_PATTERN_BODY:
679 x = xcalloc(1, sizeof (struct grep_expr));
680 x->node = GREP_NODE_ATOM;
684 case GREP_OPEN_PAREN:
686 x = compile_pattern_or(list);
687 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
688 die("unmatched parenthesis");
689 *list = (*list)->next;
696 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
707 die("--not not followed by pattern expression");
709 x = xcalloc(1, sizeof (struct grep_expr));
710 x->node = GREP_NODE_NOT;
711 x->u.unary = compile_pattern_not(list);
713 die("--not followed by non pattern expression");
716 return compile_pattern_atom(list);
720 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
723 struct grep_expr *x, *y, *z;
725 x = compile_pattern_not(list);
727 if (p && p->token == GREP_AND) {
729 die("--and not followed by pattern expression");
731 y = compile_pattern_and(list);
733 die("--and not followed by pattern expression");
734 z = xcalloc(1, sizeof (struct grep_expr));
735 z->node = GREP_NODE_AND;
736 z->u.binary.left = x;
737 z->u.binary.right = y;
743 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
746 struct grep_expr *x, *y, *z;
748 x = compile_pattern_and(list);
750 if (x && p && p->token != GREP_CLOSE_PAREN) {
751 y = compile_pattern_or(list);
753 die("not a pattern expression %s", p->pattern);
754 z = xcalloc(1, sizeof (struct grep_expr));
755 z->node = GREP_NODE_OR;
756 z->u.binary.left = x;
757 z->u.binary.right = y;
763 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
765 return compile_pattern_or(list);
768 static void indent(int in)
774 static void dump_grep_pat(struct grep_pat *p)
777 case GREP_AND: fprintf(stderr, "*and*"); break;
778 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
779 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
780 case GREP_NOT: fprintf(stderr, "*not*"); break;
781 case GREP_OR: fprintf(stderr, "*or*"); break;
783 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
784 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
785 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
790 case GREP_PATTERN_HEAD:
791 fprintf(stderr, "<head %d>", p->field); break;
792 case GREP_PATTERN_BODY:
793 fprintf(stderr, "<body>"); break;
797 case GREP_PATTERN_HEAD:
798 case GREP_PATTERN_BODY:
800 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
806 static void dump_grep_expression_1(struct grep_expr *x, int in)
811 fprintf(stderr, "true\n");
814 dump_grep_pat(x->u.atom);
817 fprintf(stderr, "(not\n");
818 dump_grep_expression_1(x->u.unary, in+1);
820 fprintf(stderr, ")\n");
823 fprintf(stderr, "(and\n");
824 dump_grep_expression_1(x->u.binary.left, in+1);
825 dump_grep_expression_1(x->u.binary.right, in+1);
827 fprintf(stderr, ")\n");
830 fprintf(stderr, "(or\n");
831 dump_grep_expression_1(x->u.binary.left, in+1);
832 dump_grep_expression_1(x->u.binary.right, in+1);
834 fprintf(stderr, ")\n");
839 static void dump_grep_expression(struct grep_opt *opt)
841 struct grep_expr *x = opt->pattern_expression;
844 fprintf(stderr, "[all-match]\n");
845 dump_grep_expression_1(x, 0);
849 static struct grep_expr *grep_true_expr(void)
851 struct grep_expr *z = xcalloc(1, sizeof(*z));
852 z->node = GREP_NODE_TRUE;
856 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
858 struct grep_expr *z = xcalloc(1, sizeof(*z));
859 z->node = GREP_NODE_OR;
860 z->u.binary.left = left;
861 z->u.binary.right = right;
865 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
868 struct grep_expr *header_expr;
869 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
870 enum grep_header_field fld;
872 if (!opt->header_list)
875 for (p = opt->header_list; p; p = p->next) {
876 if (p->token != GREP_PATTERN_HEAD)
877 die("BUG: a non-header pattern in grep header list.");
878 if (p->field < GREP_HEADER_FIELD_MIN ||
879 GREP_HEADER_FIELD_MAX <= p->field)
880 die("BUG: unknown header field %d", p->field);
881 compile_regexp(p, opt);
884 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
885 header_group[fld] = NULL;
887 for (p = opt->header_list; p; p = p->next) {
889 struct grep_pat *pp = p;
891 h = compile_pattern_atom(&pp);
892 if (!h || pp != p->next)
893 die("BUG: malformed header expr");
894 if (!header_group[p->field]) {
895 header_group[p->field] = h;
898 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
903 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
904 if (!header_group[fld])
907 header_expr = grep_true_expr();
908 header_expr = grep_or_expr(header_group[fld], header_expr);
913 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
915 struct grep_expr *z = x;
918 assert(x->node == GREP_NODE_OR);
919 if (x->u.binary.right &&
920 x->u.binary.right->node == GREP_NODE_TRUE) {
921 x->u.binary.right = y;
924 x = x->u.binary.right;
929 static void compile_grep_patterns_real(struct grep_opt *opt)
932 struct grep_expr *header_expr = prep_header_patterns(opt);
934 for (p = opt->pattern_list; p; p = p->next) {
936 case GREP_PATTERN: /* atom */
937 case GREP_PATTERN_HEAD:
938 case GREP_PATTERN_BODY:
939 compile_regexp(p, opt);
947 if (opt->all_match || header_expr)
949 else if (!opt->extended && !opt->debug)
952 p = opt->pattern_list;
954 opt->pattern_expression = compile_pattern_expr(&p);
956 die("incomplete pattern expression: %s", p->pattern);
961 if (!opt->pattern_expression)
962 opt->pattern_expression = header_expr;
963 else if (opt->all_match)
964 opt->pattern_expression = grep_splice_or(header_expr,
965 opt->pattern_expression);
967 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
972 void compile_grep_patterns(struct grep_opt *opt)
974 compile_grep_patterns_real(opt);
976 dump_grep_expression(opt);
979 static void free_pattern_expr(struct grep_expr *x)
986 free_pattern_expr(x->u.unary);
990 free_pattern_expr(x->u.binary.left);
991 free_pattern_expr(x->u.binary.right);
997 void free_grep_patterns(struct grep_opt *opt)
999 struct grep_pat *p, *n;
1001 for (p = opt->pattern_list; p; p = n) {
1004 case GREP_PATTERN: /* atom */
1005 case GREP_PATTERN_HEAD:
1006 case GREP_PATTERN_BODY:
1009 else if (p->pcre1_regexp)
1010 free_pcre1_regexp(p);
1011 else if (p->pcre2_pattern)
1012 free_pcre2_pattern(p);
1014 regfree(&p->regexp);
1025 free_pattern_expr(opt->pattern_expression);
1028 static char *end_of_line(char *cp, unsigned long *left)
1030 unsigned long l = *left;
1031 while (l && *cp != '\n') {
1039 static int word_char(char ch)
1041 return isalnum(ch) || ch == '_';
1044 static void output_color(struct grep_opt *opt, const void *data, size_t size,
1047 if (want_color(opt->color) && color && color[0]) {
1048 opt->output(opt, color, strlen(color));
1049 opt->output(opt, data, size);
1050 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
1052 opt->output(opt, data, size);
1055 static void output_sep(struct grep_opt *opt, char sign)
1057 if (opt->null_following_name)
1058 opt->output(opt, "\0", 1);
1060 output_color(opt, &sign, 1, opt->color_sep);
1063 static void show_name(struct grep_opt *opt, const char *name)
1065 output_color(opt, name, strlen(name), opt->color_filename);
1066 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
1069 static int fixmatch(struct grep_pat *p, char *line, char *eol,
1072 struct kwsmatch kwsm;
1073 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
1075 match->rm_so = match->rm_eo = -1;
1078 match->rm_so = offset;
1079 match->rm_eo = match->rm_so + kwsm.size[0];
1084 static int patmatch(struct grep_pat *p, char *line, char *eol,
1085 regmatch_t *match, int eflags)
1090 hit = !fixmatch(p, line, eol, match);
1091 else if (p->pcre1_regexp)
1092 hit = !pcre1match(p, line, eol, match, eflags);
1093 else if (p->pcre2_pattern)
1094 hit = !pcre2match(p, line, eol, match, eflags);
1096 hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
1102 static int strip_timestamp(char *bol, char **eol_p)
1107 while (bol < --eol) {
1121 } header_field[] = {
1123 { "committer ", 10 },
1127 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
1128 enum grep_context ctx,
1129 regmatch_t *pmatch, int eflags)
1133 const char *start = bol;
1135 if ((p->token != GREP_PATTERN) &&
1136 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
1139 if (p->token == GREP_PATTERN_HEAD) {
1142 assert(p->field < ARRAY_SIZE(header_field));
1143 field = header_field[p->field].field;
1144 len = header_field[p->field].len;
1145 if (strncmp(bol, field, len))
1149 case GREP_HEADER_AUTHOR:
1150 case GREP_HEADER_COMMITTER:
1151 saved_ch = strip_timestamp(bol, &eol);
1159 hit = patmatch(p, bol, eol, pmatch, eflags);
1161 if (hit && p->word_regexp) {
1162 if ((pmatch[0].rm_so < 0) ||
1163 (eol - bol) < pmatch[0].rm_so ||
1164 (pmatch[0].rm_eo < 0) ||
1165 (eol - bol) < pmatch[0].rm_eo)
1166 die("regexp returned nonsense");
1168 /* Match beginning must be either beginning of the
1169 * line, or at word boundary (i.e. the last char must
1170 * not be a word char). Similarly, match end must be
1171 * either end of the line, or at word boundary
1172 * (i.e. the next char must not be a word char).
1174 if ( ((pmatch[0].rm_so == 0) ||
1175 !word_char(bol[pmatch[0].rm_so-1])) &&
1176 ((pmatch[0].rm_eo == (eol-bol)) ||
1177 !word_char(bol[pmatch[0].rm_eo])) )
1182 /* Words consist of at least one character. */
1183 if (pmatch->rm_so == pmatch->rm_eo)
1186 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
1187 /* There could be more than one match on the
1188 * line, and the first match might not be
1189 * strict word match. But later ones could be!
1190 * Forward to the next possible start, i.e. the
1191 * next position following a non-word char.
1193 bol = pmatch[0].rm_so + bol + 1;
1194 while (word_char(bol[-1]) && bol < eol)
1196 eflags |= REG_NOTBOL;
1201 if (p->token == GREP_PATTERN_HEAD && saved_ch)
1204 pmatch[0].rm_so += bol - start;
1205 pmatch[0].rm_eo += bol - start;
1210 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
1211 enum grep_context ctx, int collect_hits)
1217 die("Not a valid grep expression");
1219 case GREP_NODE_TRUE:
1222 case GREP_NODE_ATOM:
1223 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
1226 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
1229 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
1231 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
1235 return (match_expr_eval(x->u.binary.left,
1236 bol, eol, ctx, 0) ||
1237 match_expr_eval(x->u.binary.right,
1239 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
1240 x->u.binary.left->hit |= h;
1241 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
1244 die("Unexpected node type (internal error) %d", x->node);
1251 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1252 enum grep_context ctx, int collect_hits)
1254 struct grep_expr *x = opt->pattern_expression;
1255 return match_expr_eval(x, bol, eol, ctx, collect_hits);
1258 static int match_line(struct grep_opt *opt, char *bol, char *eol,
1259 enum grep_context ctx, int collect_hits)
1265 return match_expr(opt, bol, eol, ctx, collect_hits);
1267 /* we do not call with collect_hits without being extended */
1268 for (p = opt->pattern_list; p; p = p->next) {
1269 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
1275 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1276 enum grep_context ctx,
1277 regmatch_t *pmatch, int eflags)
1281 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
1283 if (match.rm_so < 0 || match.rm_eo < 0)
1285 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1286 if (match.rm_so > pmatch->rm_so)
1288 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1291 pmatch->rm_so = match.rm_so;
1292 pmatch->rm_eo = match.rm_eo;
1296 static int next_match(struct grep_opt *opt, char *bol, char *eol,
1297 enum grep_context ctx, regmatch_t *pmatch, int eflags)
1302 pmatch->rm_so = pmatch->rm_eo = -1;
1304 for (p = opt->pattern_list; p; p = p->next) {
1306 case GREP_PATTERN: /* atom */
1307 case GREP_PATTERN_HEAD:
1308 case GREP_PATTERN_BODY:
1309 hit |= match_next_pattern(p, bol, eol, ctx,
1320 static void show_line(struct grep_opt *opt, char *bol, char *eol,
1321 const char *name, unsigned lno, char sign)
1323 int rest = eol - bol;
1324 const char *match_color, *line_color = NULL;
1326 if (opt->file_break && opt->last_shown == 0) {
1327 if (opt->show_hunk_mark)
1328 opt->output(opt, "\n", 1);
1329 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1330 if (opt->last_shown == 0) {
1331 if (opt->show_hunk_mark) {
1332 output_color(opt, "--", 2, opt->color_sep);
1333 opt->output(opt, "\n", 1);
1335 } else if (lno > opt->last_shown + 1) {
1336 output_color(opt, "--", 2, opt->color_sep);
1337 opt->output(opt, "\n", 1);
1340 if (opt->heading && opt->last_shown == 0) {
1341 output_color(opt, name, strlen(name), opt->color_filename);
1342 opt->output(opt, "\n", 1);
1344 opt->last_shown = lno;
1346 if (!opt->heading && opt->pathname) {
1347 output_color(opt, name, strlen(name), opt->color_filename);
1348 output_sep(opt, sign);
1352 xsnprintf(buf, sizeof(buf), "%d", lno);
1353 output_color(opt, buf, strlen(buf), opt->color_lineno);
1354 output_sep(opt, sign);
1358 enum grep_context ctx = GREP_CONTEXT_BODY;
1363 match_color = opt->color_match_selected;
1365 match_color = opt->color_match_context;
1367 line_color = opt->color_selected;
1368 else if (sign == '-')
1369 line_color = opt->color_context;
1370 else if (sign == '=')
1371 line_color = opt->color_function;
1373 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1374 if (match.rm_so == match.rm_eo)
1377 output_color(opt, bol, match.rm_so, line_color);
1378 output_color(opt, bol + match.rm_so,
1379 match.rm_eo - match.rm_so, match_color);
1381 rest -= match.rm_eo;
1382 eflags = REG_NOTBOL;
1386 output_color(opt, bol, rest, line_color);
1387 opt->output(opt, "\n", 1);
1394 * This lock protects access to the gitattributes machinery, which is
1397 pthread_mutex_t grep_attr_mutex;
1399 static inline void grep_attr_lock(void)
1402 pthread_mutex_lock(&grep_attr_mutex);
1405 static inline void grep_attr_unlock(void)
1408 pthread_mutex_unlock(&grep_attr_mutex);
1412 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1414 pthread_mutex_t grep_read_mutex;
1417 #define grep_attr_lock()
1418 #define grep_attr_unlock()
1421 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1423 xdemitconf_t *xecfg = opt->priv;
1424 if (xecfg && !xecfg->find_func) {
1425 grep_source_load_driver(gs);
1426 if (gs->driver->funcname.pattern) {
1427 const struct userdiff_funcname *pe = &gs->driver->funcname;
1428 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1430 xecfg = opt->priv = NULL;
1436 return xecfg->find_func(bol, eol - bol, buf, 1,
1437 xecfg->find_func_priv) >= 0;
1442 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1447 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1448 char *bol, unsigned lno)
1450 while (bol > gs->buf) {
1453 while (bol > gs->buf && bol[-1] != '\n')
1457 if (lno <= opt->last_shown)
1460 if (match_funcname(opt, gs, bol, eol)) {
1461 show_line(opt, bol, eol, gs->name, lno, '=');
1467 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1468 char *bol, char *end, unsigned lno)
1470 unsigned cur = lno, from = 1, funcname_lno = 0;
1471 int funcname_needed = !!opt->funcname;
1473 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1474 funcname_needed = 2;
1476 if (opt->pre_context < lno)
1477 from = lno - opt->pre_context;
1478 if (from <= opt->last_shown)
1479 from = opt->last_shown + 1;
1482 while (bol > gs->buf &&
1483 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1486 while (bol > gs->buf && bol[-1] != '\n')
1489 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1491 funcname_needed = 0;
1495 /* We need to look even further back to find a function signature. */
1496 if (opt->funcname && funcname_needed)
1497 show_funcname_line(opt, gs, bol, cur);
1501 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1503 while (*eol != '\n')
1505 show_line(opt, bol, eol, gs->name, cur, sign);
1511 static int should_lookahead(struct grep_opt *opt)
1516 return 0; /* punt for too complex stuff */
1519 for (p = opt->pattern_list; p; p = p->next) {
1520 if (p->token != GREP_PATTERN)
1521 return 0; /* punt for "header only" and stuff */
1526 static int look_ahead(struct grep_opt *opt,
1527 unsigned long *left_p,
1531 unsigned lno = *lno_p;
1534 char *sp, *last_bol;
1535 regoff_t earliest = -1;
1537 for (p = opt->pattern_list; p; p = p->next) {
1541 hit = patmatch(p, bol, bol + *left_p, &m, 0);
1542 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1544 if (earliest < 0 || m.rm_so < earliest)
1549 *bol_p = bol + *left_p;
1553 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1554 ; /* find the beginning of the line */
1557 for (sp = bol; sp < last_bol; sp++) {
1561 *left_p -= last_bol - bol;
1567 static int fill_textconv_grep(struct userdiff_driver *driver,
1568 struct grep_source *gs)
1570 struct diff_filespec *df;
1574 if (!driver || !driver->textconv)
1575 return grep_source_load(gs);
1578 * The textconv interface is intimately tied to diff_filespecs, so we
1579 * have to pretend to be one. If we could unify the grep_source
1580 * and diff_filespec structs, this mess could just go away.
1582 df = alloc_filespec(gs->path);
1584 case GREP_SOURCE_OID:
1585 fill_filespec(df, gs->identifier, 1, 0100644);
1587 case GREP_SOURCE_FILE:
1588 fill_filespec(df, &null_oid, 0, 0100644);
1591 die("BUG: attempt to textconv something without a path?");
1595 * fill_textconv is not remotely thread-safe; it may load objects
1596 * behind the scenes, and it modifies the global diff tempfile
1600 size = fill_textconv(driver, df, &buf);
1605 * The normal fill_textconv usage by the diff machinery would just keep
1606 * the textconv'd buf separate from the diff_filespec. But much of the
1607 * grep code passes around a grep_source and assumes that its "buf"
1608 * pointer is the beginning of the thing we are searching. So let's
1609 * install our textconv'd version into the grep_source, taking care not
1610 * to leak any existing buffer.
1612 grep_source_clear_data(gs);
1619 static int is_empty_line(const char *bol, const char *eol)
1621 while (bol < eol && isspace(*bol))
1626 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1629 char *peek_bol = NULL;
1632 unsigned last_hit = 0;
1633 int binary_match_only = 0;
1635 int try_lookahead = 0;
1636 int show_function = 0;
1637 struct userdiff_driver *textconv = NULL;
1638 enum grep_context ctx = GREP_CONTEXT_HEAD;
1642 opt->output = std_output;
1644 if (opt->pre_context || opt->post_context || opt->file_break ||
1646 /* Show hunk marks, except for the first file. */
1647 if (opt->last_shown)
1648 opt->show_hunk_mark = 1;
1650 * If we're using threads then we can't easily identify
1651 * the first file. Always put hunk marks in that case
1652 * and skip the very first one later in work_done().
1654 if (opt->output != std_output)
1655 opt->show_hunk_mark = 1;
1657 opt->last_shown = 0;
1659 if (opt->allow_textconv) {
1660 grep_source_load_driver(gs);
1662 * We might set up the shared textconv cache data here, which
1663 * is not thread-safe.
1666 textconv = userdiff_get_textconv(gs->driver);
1671 * We know the result of a textconv is text, so we only have to care
1672 * about binary handling if we are not using it.
1675 switch (opt->binary) {
1676 case GREP_BINARY_DEFAULT:
1677 if (grep_source_is_binary(gs))
1678 binary_match_only = 1;
1680 case GREP_BINARY_NOMATCH:
1681 if (grep_source_is_binary(gs))
1682 return 0; /* Assume unmatch */
1684 case GREP_BINARY_TEXT:
1687 die("BUG: unknown binary handling mode");
1691 memset(&xecfg, 0, sizeof(xecfg));
1694 try_lookahead = should_lookahead(opt);
1696 if (fill_textconv_grep(textconv, gs) < 0)
1706 * look_ahead() skips quickly to the line that possibly
1707 * has the next hit; don't call it if we need to do
1708 * something more than just skipping the current line
1709 * in response to an unmatch for the current line. E.g.
1710 * inside a post-context window, we will show the current
1711 * line as a context around the previous hit when it
1716 && (show_function ||
1717 lno <= last_hit + opt->post_context))
1718 && look_ahead(opt, &left, &lno, &bol))
1720 eol = end_of_line(bol, &left);
1724 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1725 ctx = GREP_CONTEXT_BODY;
1727 hit = match_line(opt, bol, eol, ctx, collect_hits);
1733 /* "grep -v -e foo -e bla" should list lines
1734 * that do not have either, so inversion should
1739 if (opt->unmatch_name_only) {
1746 if (opt->status_only)
1748 if (opt->name_only) {
1749 show_name(opt, gs->name);
1754 if (binary_match_only) {
1755 opt->output(opt, "Binary file ", 12);
1756 output_color(opt, gs->name, strlen(gs->name),
1757 opt->color_filename);
1758 opt->output(opt, " matches\n", 9);
1761 /* Hit at this line. If we haven't shown the
1762 * pre-context lines, we would need to show them.
1764 if (opt->pre_context || opt->funcbody)
1765 show_pre_context(opt, gs, bol, eol, lno);
1766 else if (opt->funcname)
1767 show_funcname_line(opt, gs, bol, lno);
1768 show_line(opt, bol, eol, gs->name, lno, ':');
1774 if (show_function && (!peek_bol || peek_bol < bol)) {
1775 unsigned long peek_left = left;
1776 char *peek_eol = eol;
1779 * Trailing empty lines are not interesting.
1780 * Peek past them to see if they belong to the
1781 * body of the current function.
1784 while (is_empty_line(peek_bol, peek_eol)) {
1785 peek_bol = peek_eol + 1;
1786 peek_eol = end_of_line(peek_bol, &peek_left);
1789 if (match_funcname(opt, gs, peek_bol, peek_eol))
1792 if (show_function ||
1793 (last_hit && lno <= last_hit + opt->post_context)) {
1794 /* If the last hit is within the post context,
1795 * we need to show this line.
1797 show_line(opt, bol, eol, gs->name, lno, '-');
1811 if (opt->status_only)
1813 if (opt->unmatch_name_only) {
1814 /* We did not see any hit, so we want to show this */
1815 show_name(opt, gs->name);
1819 xdiff_clear_find_func(&xecfg);
1823 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1824 * which feels mostly useless but sometimes useful. Maybe
1825 * make it another option? For now suppress them.
1827 if (opt->count && count) {
1829 if (opt->pathname) {
1830 output_color(opt, gs->name, strlen(gs->name),
1831 opt->color_filename);
1832 output_sep(opt, ':');
1834 xsnprintf(buf, sizeof(buf), "%u\n", count);
1835 opt->output(opt, buf, strlen(buf));
1841 static void clr_hit_marker(struct grep_expr *x)
1843 /* All-hit markers are meaningful only at the very top level
1848 if (x->node != GREP_NODE_OR)
1850 x->u.binary.left->hit = 0;
1851 x = x->u.binary.right;
1855 static int chk_hit_marker(struct grep_expr *x)
1857 /* Top level nodes have hit markers. See if they all are hits */
1859 if (x->node != GREP_NODE_OR)
1861 if (!x->u.binary.left->hit)
1863 x = x->u.binary.right;
1867 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1870 * we do not have to do the two-pass grep when we do not check
1871 * buffer-wide "all-match".
1873 if (!opt->all_match)
1874 return grep_source_1(opt, gs, 0);
1876 /* Otherwise the toplevel "or" terms hit a bit differently.
1877 * We first clear hit markers from them.
1879 clr_hit_marker(opt->pattern_expression);
1880 grep_source_1(opt, gs, 1);
1882 if (!chk_hit_marker(opt->pattern_expression))
1885 return grep_source_1(opt, gs, 0);
1888 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1890 struct grep_source gs;
1893 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1897 r = grep_source(opt, &gs);
1899 grep_source_clear(&gs);
1903 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1904 const char *name, const char *path,
1905 const void *identifier)
1908 gs->name = xstrdup_or_null(name);
1909 gs->path = xstrdup_or_null(path);
1915 case GREP_SOURCE_FILE:
1916 gs->identifier = xstrdup(identifier);
1918 case GREP_SOURCE_SUBMODULE:
1920 gs->identifier = NULL;
1925 * If the identifier is non-NULL (in the submodule case) it
1926 * will be a SHA1 that needs to be copied.
1928 case GREP_SOURCE_OID:
1929 gs->identifier = oiddup(identifier);
1931 case GREP_SOURCE_BUF:
1932 gs->identifier = NULL;
1937 void grep_source_clear(struct grep_source *gs)
1939 FREE_AND_NULL(gs->name);
1940 FREE_AND_NULL(gs->path);
1941 FREE_AND_NULL(gs->identifier);
1942 grep_source_clear_data(gs);
1945 void grep_source_clear_data(struct grep_source *gs)
1948 case GREP_SOURCE_FILE:
1949 case GREP_SOURCE_OID:
1950 case GREP_SOURCE_SUBMODULE:
1951 FREE_AND_NULL(gs->buf);
1954 case GREP_SOURCE_BUF:
1955 /* leave user-provided buf intact */
1960 static int grep_source_load_oid(struct grep_source *gs)
1962 enum object_type type;
1965 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1969 return error(_("'%s': unable to read %s"),
1971 oid_to_hex(gs->identifier));
1975 static int grep_source_load_file(struct grep_source *gs)
1977 const char *filename = gs->identifier;
1983 if (lstat(filename, &st) < 0) {
1985 if (errno != ENOENT)
1986 error_errno(_("failed to stat '%s'"), filename);
1989 if (!S_ISREG(st.st_mode))
1991 size = xsize_t(st.st_size);
1992 i = open(filename, O_RDONLY);
1995 data = xmallocz(size);
1996 if (st.st_size != read_in_full(i, data, size)) {
1997 error_errno(_("'%s': short read"), filename);
2009 static int grep_source_load(struct grep_source *gs)
2015 case GREP_SOURCE_FILE:
2016 return grep_source_load_file(gs);
2017 case GREP_SOURCE_OID:
2018 return grep_source_load_oid(gs);
2019 case GREP_SOURCE_BUF:
2020 return gs->buf ? 0 : -1;
2021 case GREP_SOURCE_SUBMODULE:
2024 die("BUG: invalid grep_source type to load");
2027 void grep_source_load_driver(struct grep_source *gs)
2034 gs->driver = userdiff_find_by_path(gs->path);
2036 gs->driver = userdiff_find_by_name("default");
2040 static int grep_source_is_binary(struct grep_source *gs)
2042 grep_source_load_driver(gs);
2043 if (gs->driver->binary != -1)
2044 return gs->driver->binary;
2046 if (!grep_source_load(gs))
2047 return buffer_is_binary(gs->buf, gs->size);