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:
179 case GREP_PATTERN_TYPE_ERE:
180 opt->regflags |= REG_EXTENDED;
183 case GREP_PATTERN_TYPE_FIXED:
187 case GREP_PATTERN_TYPE_PCRE:
192 * It's important that pcre1 always be assigned to
193 * even when there's no USE_LIBPCRE* defined. We still
194 * call the PCRE stub function, it just dies with
195 * "cannot use Perl-compatible regexes[...]".
203 void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
205 if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED)
206 grep_set_pattern_type_option(pattern_type, opt);
207 else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
208 grep_set_pattern_type_option(opt->pattern_type_option, opt);
209 else if (opt->extended_regexp_option)
210 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
213 static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
214 const char *origin, int no,
215 enum grep_pat_token t,
216 enum grep_header_field field)
218 struct grep_pat *p = xcalloc(1, sizeof(*p));
219 p->pattern = xmemdupz(pat, patlen);
220 p->patternlen = patlen;
228 static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
235 case GREP_PATTERN: /* atom */
236 case GREP_PATTERN_HEAD:
237 case GREP_PATTERN_BODY:
239 struct grep_pat *new_pat;
241 char *cp = p->pattern + p->patternlen, *nl = NULL;
242 while (++len <= p->patternlen) {
243 if (*(--cp) == '\n') {
250 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
251 p->no, p->token, p->field);
252 new_pat->next = p->next;
254 *tail = &new_pat->next;
257 p->patternlen -= len;
265 void append_header_grep_pattern(struct grep_opt *opt,
266 enum grep_header_field field, const char *pat)
268 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
269 GREP_PATTERN_HEAD, field);
270 if (field == GREP_HEADER_REFLOG)
271 opt->use_reflog_filter = 1;
272 do_append_grep_pat(&opt->header_tail, p);
275 void append_grep_pattern(struct grep_opt *opt, const char *pat,
276 const char *origin, int no, enum grep_pat_token t)
278 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
281 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
282 const char *origin, int no, enum grep_pat_token t)
284 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
285 do_append_grep_pat(&opt->pattern_tail, p);
288 struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
290 struct grep_pat *pat;
291 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
294 ret->pattern_list = NULL;
295 ret->pattern_tail = &ret->pattern_list;
297 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
299 if(pat->token == GREP_PATTERN_HEAD)
300 append_header_grep_pattern(ret, pat->field,
303 append_grep_pat(ret, pat->pattern, pat->patternlen,
304 pat->origin, pat->no, pat->token);
310 static NORETURN void compile_regexp_failed(const struct grep_pat *p,
316 xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no);
318 xsnprintf(where, sizeof(where), "%s, ", p->origin);
322 die("%s'%s': %s", where, p->pattern, error);
325 static int is_fixed(const char *s, size_t len)
329 for (i = 0; i < len; i++) {
330 if (is_regex_special(s[i]))
337 static int has_null(const char *s, size_t len)
340 * regcomp cannot accept patterns with NULs so when using it
341 * we consider any pattern containing a NUL fixed.
343 if (memchr(s, 0, len))
350 static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
354 int options = PCRE_MULTILINE;
356 if (opt->ignore_case) {
357 if (has_non_ascii(p->pattern))
358 p->pcre1_tables = pcre_maketables();
359 options |= PCRE_CASELESS;
361 if (is_utf8_locale() && has_non_ascii(p->pattern))
362 options |= PCRE_UTF8;
364 p->pcre1_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
366 if (!p->pcre1_regexp)
367 compile_regexp_failed(p, error);
369 p->pcre1_extra_info = pcre_study(p->pcre1_regexp, PCRE_STUDY_JIT_COMPILE, &error);
370 if (!p->pcre1_extra_info && error)
373 #ifdef GIT_PCRE1_USE_JIT
374 pcre_config(PCRE_CONFIG_JIT, &p->pcre1_jit_on);
375 if (p->pcre1_jit_on == 1) {
376 p->pcre1_jit_stack = pcre_jit_stack_alloc(1, 1024 * 1024);
377 if (!p->pcre1_jit_stack)
378 die("Couldn't allocate PCRE JIT stack");
379 pcre_assign_jit_stack(p->pcre1_extra_info, NULL, p->pcre1_jit_stack);
380 } else if (p->pcre1_jit_on != 0) {
381 die("BUG: The pcre1_jit_on variable should be 0 or 1, not %d",
387 static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
388 regmatch_t *match, int eflags)
390 int ovector[30], ret, flags = 0;
392 if (eflags & REG_NOTBOL)
393 flags |= PCRE_NOTBOL;
395 #ifdef GIT_PCRE1_USE_JIT
396 if (p->pcre1_jit_on) {
397 ret = pcre_jit_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
398 eol - line, 0, flags, ovector,
399 ARRAY_SIZE(ovector), p->pcre1_jit_stack);
403 ret = pcre_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
404 eol - line, 0, flags, ovector,
405 ARRAY_SIZE(ovector));
408 if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
409 die("pcre_exec failed with error code %d", ret);
412 match->rm_so = ovector[0];
413 match->rm_eo = ovector[1];
419 static void free_pcre1_regexp(struct grep_pat *p)
421 pcre_free(p->pcre1_regexp);
422 #ifdef GIT_PCRE1_USE_JIT
423 if (p->pcre1_jit_on) {
424 pcre_free_study(p->pcre1_extra_info);
425 pcre_jit_stack_free(p->pcre1_jit_stack);
429 pcre_free(p->pcre1_extra_info);
431 pcre_free((void *)p->pcre1_tables);
433 #else /* !USE_LIBPCRE1 */
434 static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
436 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
439 static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
440 regmatch_t *match, int eflags)
445 static void free_pcre1_regexp(struct grep_pat *p)
448 #endif /* !USE_LIBPCRE1 */
451 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
454 PCRE2_UCHAR errbuf[256];
455 PCRE2_SIZE erroffset;
456 int options = PCRE2_MULTILINE;
457 const uint8_t *character_tables = NULL;
462 p->pcre2_compile_context = NULL;
464 if (opt->ignore_case) {
465 if (has_non_ascii(p->pattern)) {
466 character_tables = pcre2_maketables(NULL);
467 p->pcre2_compile_context = pcre2_compile_context_create(NULL);
468 pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
470 options |= PCRE2_CASELESS;
472 if (is_utf8_locale() && has_non_ascii(p->pattern))
473 options |= PCRE2_UTF;
475 p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
476 p->patternlen, options, &error, &erroffset,
477 p->pcre2_compile_context);
479 if (p->pcre2_pattern) {
480 p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL);
481 if (!p->pcre2_match_data)
482 die("Couldn't allocate PCRE2 match data");
484 pcre2_get_error_message(error, errbuf, sizeof(errbuf));
485 compile_regexp_failed(p, (const char *)&errbuf);
488 pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on);
489 if (p->pcre2_jit_on == 1) {
490 jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
492 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret);
493 p->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, NULL);
494 if (!p->pcre2_jit_stack)
495 die("Couldn't allocate PCRE2 JIT stack");
496 p->pcre2_match_context = pcre2_match_context_create(NULL);
497 if (!p->pcre2_match_context)
498 die("Couldn't allocate PCRE2 match context");
499 pcre2_jit_stack_assign(p->pcre2_match_context, NULL, p->pcre2_jit_stack);
500 } else if (p->pcre2_jit_on != 0) {
501 die("BUG: The pcre2_jit_on variable should be 0 or 1, not %d",
506 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
507 regmatch_t *match, int eflags)
511 PCRE2_UCHAR errbuf[256];
513 if (eflags & REG_NOTBOL)
514 flags |= PCRE2_NOTBOL;
517 ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line,
518 eol - line, 0, flags, p->pcre2_match_data,
521 ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line,
522 eol - line, 0, flags, p->pcre2_match_data,
525 if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) {
526 pcre2_get_error_message(ret, errbuf, sizeof(errbuf));
527 die("%s failed with error code %d: %s",
528 (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret,
532 ovector = pcre2_get_ovector_pointer(p->pcre2_match_data);
534 match->rm_so = (int)ovector[0];
535 match->rm_eo = (int)ovector[1];
541 static void free_pcre2_pattern(struct grep_pat *p)
543 pcre2_compile_context_free(p->pcre2_compile_context);
544 pcre2_code_free(p->pcre2_pattern);
545 pcre2_match_data_free(p->pcre2_match_data);
546 pcre2_jit_stack_free(p->pcre2_jit_stack);
547 pcre2_match_context_free(p->pcre2_match_context);
549 #else /* !USE_LIBPCRE2 */
550 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
553 * Unreachable until USE_LIBPCRE2 becomes synonymous with
554 * USE_LIBPCRE. See the sibling comment in
555 * grep_set_pattern_type_option().
557 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
560 static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
561 regmatch_t *match, int eflags)
566 static void free_pcre2_pattern(struct grep_pat *p)
569 #endif /* !USE_LIBPCRE2 */
571 static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
573 struct strbuf sb = STRBUF_INIT;
575 int regflags = opt->regflags;
577 basic_regex_quote_buf(&sb, p->pattern);
578 if (opt->ignore_case)
579 regflags |= REG_ICASE;
580 err = regcomp(&p->regexp, sb.buf, regflags);
582 fprintf(stderr, "fixed %s\n", sb.buf);
586 regerror(err, &p->regexp, errbuf, sizeof(errbuf));
588 compile_regexp_failed(p, errbuf);
592 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
594 int icase, ascii_only;
597 p->word_regexp = opt->word_regexp;
598 p->ignore_case = opt->ignore_case;
599 icase = opt->regflags & REG_ICASE || p->ignore_case;
600 ascii_only = !has_non_ascii(p->pattern);
603 * Even when -F (fixed) asks us to do a non-regexp search, we
604 * may not be able to correctly case-fold when -i
605 * (ignore-case) is asked (in which case, we'll synthesize a
606 * regexp to match the pattern that matches regexp special
607 * characters literally, while ignoring case differences). On
608 * the other hand, even without -F, if the pattern does not
609 * have any regexp special characters and there is no need for
610 * case-folding search, we can internally turn it into a
611 * simple string match using kws. p->fixed tells us if we
615 has_null(p->pattern, p->patternlen) ||
616 is_fixed(p->pattern, p->patternlen))
617 p->fixed = !icase || ascii_only;
620 p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL);
621 kwsincr(p->kws, p->pattern, p->patternlen);
624 } else if (opt->fixed) {
626 * We come here when the pattern has the non-ascii
627 * characters we cannot case-fold, and asked to
630 compile_fixed_regexp(p, opt);
635 compile_pcre2_pattern(p, opt);
640 compile_pcre1_regexp(p, opt);
644 err = regcomp(&p->regexp, p->pattern, opt->regflags);
647 regerror(err, &p->regexp, errbuf, 1024);
649 compile_regexp_failed(p, errbuf);
653 static struct grep_expr *compile_pattern_or(struct grep_pat **);
654 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
663 case GREP_PATTERN: /* atom */
664 case GREP_PATTERN_HEAD:
665 case GREP_PATTERN_BODY:
666 x = xcalloc(1, sizeof (struct grep_expr));
667 x->node = GREP_NODE_ATOM;
671 case GREP_OPEN_PAREN:
673 x = compile_pattern_or(list);
674 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
675 die("unmatched parenthesis");
676 *list = (*list)->next;
683 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
694 die("--not not followed by pattern expression");
696 x = xcalloc(1, sizeof (struct grep_expr));
697 x->node = GREP_NODE_NOT;
698 x->u.unary = compile_pattern_not(list);
700 die("--not followed by non pattern expression");
703 return compile_pattern_atom(list);
707 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
710 struct grep_expr *x, *y, *z;
712 x = compile_pattern_not(list);
714 if (p && p->token == GREP_AND) {
716 die("--and not followed by pattern expression");
718 y = compile_pattern_and(list);
720 die("--and not followed by pattern expression");
721 z = xcalloc(1, sizeof (struct grep_expr));
722 z->node = GREP_NODE_AND;
723 z->u.binary.left = x;
724 z->u.binary.right = y;
730 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
733 struct grep_expr *x, *y, *z;
735 x = compile_pattern_and(list);
737 if (x && p && p->token != GREP_CLOSE_PAREN) {
738 y = compile_pattern_or(list);
740 die("not a pattern expression %s", p->pattern);
741 z = xcalloc(1, sizeof (struct grep_expr));
742 z->node = GREP_NODE_OR;
743 z->u.binary.left = x;
744 z->u.binary.right = y;
750 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
752 return compile_pattern_or(list);
755 static void indent(int in)
761 static void dump_grep_pat(struct grep_pat *p)
764 case GREP_AND: fprintf(stderr, "*and*"); break;
765 case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break;
766 case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break;
767 case GREP_NOT: fprintf(stderr, "*not*"); break;
768 case GREP_OR: fprintf(stderr, "*or*"); break;
770 case GREP_PATTERN: fprintf(stderr, "pattern"); break;
771 case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break;
772 case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break;
777 case GREP_PATTERN_HEAD:
778 fprintf(stderr, "<head %d>", p->field); break;
779 case GREP_PATTERN_BODY:
780 fprintf(stderr, "<body>"); break;
784 case GREP_PATTERN_HEAD:
785 case GREP_PATTERN_BODY:
787 fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern);
793 static void dump_grep_expression_1(struct grep_expr *x, int in)
798 fprintf(stderr, "true\n");
801 dump_grep_pat(x->u.atom);
804 fprintf(stderr, "(not\n");
805 dump_grep_expression_1(x->u.unary, in+1);
807 fprintf(stderr, ")\n");
810 fprintf(stderr, "(and\n");
811 dump_grep_expression_1(x->u.binary.left, in+1);
812 dump_grep_expression_1(x->u.binary.right, in+1);
814 fprintf(stderr, ")\n");
817 fprintf(stderr, "(or\n");
818 dump_grep_expression_1(x->u.binary.left, in+1);
819 dump_grep_expression_1(x->u.binary.right, in+1);
821 fprintf(stderr, ")\n");
826 static void dump_grep_expression(struct grep_opt *opt)
828 struct grep_expr *x = opt->pattern_expression;
831 fprintf(stderr, "[all-match]\n");
832 dump_grep_expression_1(x, 0);
836 static struct grep_expr *grep_true_expr(void)
838 struct grep_expr *z = xcalloc(1, sizeof(*z));
839 z->node = GREP_NODE_TRUE;
843 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
845 struct grep_expr *z = xcalloc(1, sizeof(*z));
846 z->node = GREP_NODE_OR;
847 z->u.binary.left = left;
848 z->u.binary.right = right;
852 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
855 struct grep_expr *header_expr;
856 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
857 enum grep_header_field fld;
859 if (!opt->header_list)
862 for (p = opt->header_list; p; p = p->next) {
863 if (p->token != GREP_PATTERN_HEAD)
864 die("BUG: a non-header pattern in grep header list.");
865 if (p->field < GREP_HEADER_FIELD_MIN ||
866 GREP_HEADER_FIELD_MAX <= p->field)
867 die("BUG: unknown header field %d", p->field);
868 compile_regexp(p, opt);
871 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
872 header_group[fld] = NULL;
874 for (p = opt->header_list; p; p = p->next) {
876 struct grep_pat *pp = p;
878 h = compile_pattern_atom(&pp);
879 if (!h || pp != p->next)
880 die("BUG: malformed header expr");
881 if (!header_group[p->field]) {
882 header_group[p->field] = h;
885 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
890 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
891 if (!header_group[fld])
894 header_expr = grep_true_expr();
895 header_expr = grep_or_expr(header_group[fld], header_expr);
900 static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
902 struct grep_expr *z = x;
905 assert(x->node == GREP_NODE_OR);
906 if (x->u.binary.right &&
907 x->u.binary.right->node == GREP_NODE_TRUE) {
908 x->u.binary.right = y;
911 x = x->u.binary.right;
916 static void compile_grep_patterns_real(struct grep_opt *opt)
919 struct grep_expr *header_expr = prep_header_patterns(opt);
921 for (p = opt->pattern_list; p; p = p->next) {
923 case GREP_PATTERN: /* atom */
924 case GREP_PATTERN_HEAD:
925 case GREP_PATTERN_BODY:
926 compile_regexp(p, opt);
934 if (opt->all_match || header_expr)
936 else if (!opt->extended && !opt->debug)
939 p = opt->pattern_list;
941 opt->pattern_expression = compile_pattern_expr(&p);
943 die("incomplete pattern expression: %s", p->pattern);
948 if (!opt->pattern_expression)
949 opt->pattern_expression = header_expr;
950 else if (opt->all_match)
951 opt->pattern_expression = grep_splice_or(header_expr,
952 opt->pattern_expression);
954 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
959 void compile_grep_patterns(struct grep_opt *opt)
961 compile_grep_patterns_real(opt);
963 dump_grep_expression(opt);
966 static void free_pattern_expr(struct grep_expr *x)
973 free_pattern_expr(x->u.unary);
977 free_pattern_expr(x->u.binary.left);
978 free_pattern_expr(x->u.binary.right);
984 void free_grep_patterns(struct grep_opt *opt)
986 struct grep_pat *p, *n;
988 for (p = opt->pattern_list; p; p = n) {
991 case GREP_PATTERN: /* atom */
992 case GREP_PATTERN_HEAD:
993 case GREP_PATTERN_BODY:
996 else if (p->pcre1_regexp)
997 free_pcre1_regexp(p);
998 else if (p->pcre2_pattern)
999 free_pcre2_pattern(p);
1001 regfree(&p->regexp);
1012 free_pattern_expr(opt->pattern_expression);
1015 static char *end_of_line(char *cp, unsigned long *left)
1017 unsigned long l = *left;
1018 while (l && *cp != '\n') {
1026 static int word_char(char ch)
1028 return isalnum(ch) || ch == '_';
1031 static void output_color(struct grep_opt *opt, const void *data, size_t size,
1034 if (want_color(opt->color) && color && color[0]) {
1035 opt->output(opt, color, strlen(color));
1036 opt->output(opt, data, size);
1037 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
1039 opt->output(opt, data, size);
1042 static void output_sep(struct grep_opt *opt, char sign)
1044 if (opt->null_following_name)
1045 opt->output(opt, "\0", 1);
1047 output_color(opt, &sign, 1, opt->color_sep);
1050 static void show_name(struct grep_opt *opt, const char *name)
1052 output_color(opt, name, strlen(name), opt->color_filename);
1053 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
1056 static int fixmatch(struct grep_pat *p, char *line, char *eol,
1059 struct kwsmatch kwsm;
1060 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
1062 match->rm_so = match->rm_eo = -1;
1065 match->rm_so = offset;
1066 match->rm_eo = match->rm_so + kwsm.size[0];
1071 static int patmatch(struct grep_pat *p, char *line, char *eol,
1072 regmatch_t *match, int eflags)
1077 hit = !fixmatch(p, line, eol, match);
1078 else if (p->pcre1_regexp)
1079 hit = !pcre1match(p, line, eol, match, eflags);
1080 else if (p->pcre2_pattern)
1081 hit = !pcre2match(p, line, eol, match, eflags);
1083 hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
1089 static int strip_timestamp(char *bol, char **eol_p)
1094 while (bol < --eol) {
1108 } header_field[] = {
1110 { "committer ", 10 },
1114 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
1115 enum grep_context ctx,
1116 regmatch_t *pmatch, int eflags)
1120 const char *start = bol;
1122 if ((p->token != GREP_PATTERN) &&
1123 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
1126 if (p->token == GREP_PATTERN_HEAD) {
1129 assert(p->field < ARRAY_SIZE(header_field));
1130 field = header_field[p->field].field;
1131 len = header_field[p->field].len;
1132 if (strncmp(bol, field, len))
1136 case GREP_HEADER_AUTHOR:
1137 case GREP_HEADER_COMMITTER:
1138 saved_ch = strip_timestamp(bol, &eol);
1146 hit = patmatch(p, bol, eol, pmatch, eflags);
1148 if (hit && p->word_regexp) {
1149 if ((pmatch[0].rm_so < 0) ||
1150 (eol - bol) < pmatch[0].rm_so ||
1151 (pmatch[0].rm_eo < 0) ||
1152 (eol - bol) < pmatch[0].rm_eo)
1153 die("regexp returned nonsense");
1155 /* Match beginning must be either beginning of the
1156 * line, or at word boundary (i.e. the last char must
1157 * not be a word char). Similarly, match end must be
1158 * either end of the line, or at word boundary
1159 * (i.e. the next char must not be a word char).
1161 if ( ((pmatch[0].rm_so == 0) ||
1162 !word_char(bol[pmatch[0].rm_so-1])) &&
1163 ((pmatch[0].rm_eo == (eol-bol)) ||
1164 !word_char(bol[pmatch[0].rm_eo])) )
1169 /* Words consist of at least one character. */
1170 if (pmatch->rm_so == pmatch->rm_eo)
1173 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
1174 /* There could be more than one match on the
1175 * line, and the first match might not be
1176 * strict word match. But later ones could be!
1177 * Forward to the next possible start, i.e. the
1178 * next position following a non-word char.
1180 bol = pmatch[0].rm_so + bol + 1;
1181 while (word_char(bol[-1]) && bol < eol)
1183 eflags |= REG_NOTBOL;
1188 if (p->token == GREP_PATTERN_HEAD && saved_ch)
1191 pmatch[0].rm_so += bol - start;
1192 pmatch[0].rm_eo += bol - start;
1197 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
1198 enum grep_context ctx, int collect_hits)
1204 die("Not a valid grep expression");
1206 case GREP_NODE_TRUE:
1209 case GREP_NODE_ATOM:
1210 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
1213 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
1216 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
1218 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
1222 return (match_expr_eval(x->u.binary.left,
1223 bol, eol, ctx, 0) ||
1224 match_expr_eval(x->u.binary.right,
1226 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
1227 x->u.binary.left->hit |= h;
1228 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
1231 die("Unexpected node type (internal error) %d", x->node);
1238 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1239 enum grep_context ctx, int collect_hits)
1241 struct grep_expr *x = opt->pattern_expression;
1242 return match_expr_eval(x, bol, eol, ctx, collect_hits);
1245 static int match_line(struct grep_opt *opt, char *bol, char *eol,
1246 enum grep_context ctx, int collect_hits)
1252 return match_expr(opt, bol, eol, ctx, collect_hits);
1254 /* we do not call with collect_hits without being extended */
1255 for (p = opt->pattern_list; p; p = p->next) {
1256 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
1262 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1263 enum grep_context ctx,
1264 regmatch_t *pmatch, int eflags)
1268 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
1270 if (match.rm_so < 0 || match.rm_eo < 0)
1272 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1273 if (match.rm_so > pmatch->rm_so)
1275 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1278 pmatch->rm_so = match.rm_so;
1279 pmatch->rm_eo = match.rm_eo;
1283 static int next_match(struct grep_opt *opt, char *bol, char *eol,
1284 enum grep_context ctx, regmatch_t *pmatch, int eflags)
1289 pmatch->rm_so = pmatch->rm_eo = -1;
1291 for (p = opt->pattern_list; p; p = p->next) {
1293 case GREP_PATTERN: /* atom */
1294 case GREP_PATTERN_HEAD:
1295 case GREP_PATTERN_BODY:
1296 hit |= match_next_pattern(p, bol, eol, ctx,
1307 static void show_line(struct grep_opt *opt, char *bol, char *eol,
1308 const char *name, unsigned lno, char sign)
1310 int rest = eol - bol;
1311 const char *match_color, *line_color = NULL;
1313 if (opt->file_break && opt->last_shown == 0) {
1314 if (opt->show_hunk_mark)
1315 opt->output(opt, "\n", 1);
1316 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1317 if (opt->last_shown == 0) {
1318 if (opt->show_hunk_mark) {
1319 output_color(opt, "--", 2, opt->color_sep);
1320 opt->output(opt, "\n", 1);
1322 } else if (lno > opt->last_shown + 1) {
1323 output_color(opt, "--", 2, opt->color_sep);
1324 opt->output(opt, "\n", 1);
1327 if (opt->heading && opt->last_shown == 0) {
1328 output_color(opt, name, strlen(name), opt->color_filename);
1329 opt->output(opt, "\n", 1);
1331 opt->last_shown = lno;
1333 if (!opt->heading && opt->pathname) {
1334 output_color(opt, name, strlen(name), opt->color_filename);
1335 output_sep(opt, sign);
1339 xsnprintf(buf, sizeof(buf), "%d", lno);
1340 output_color(opt, buf, strlen(buf), opt->color_lineno);
1341 output_sep(opt, sign);
1345 enum grep_context ctx = GREP_CONTEXT_BODY;
1350 match_color = opt->color_match_selected;
1352 match_color = opt->color_match_context;
1354 line_color = opt->color_selected;
1355 else if (sign == '-')
1356 line_color = opt->color_context;
1357 else if (sign == '=')
1358 line_color = opt->color_function;
1360 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1361 if (match.rm_so == match.rm_eo)
1364 output_color(opt, bol, match.rm_so, line_color);
1365 output_color(opt, bol + match.rm_so,
1366 match.rm_eo - match.rm_so, match_color);
1368 rest -= match.rm_eo;
1369 eflags = REG_NOTBOL;
1373 output_color(opt, bol, rest, line_color);
1374 opt->output(opt, "\n", 1);
1381 * This lock protects access to the gitattributes machinery, which is
1384 pthread_mutex_t grep_attr_mutex;
1386 static inline void grep_attr_lock(void)
1389 pthread_mutex_lock(&grep_attr_mutex);
1392 static inline void grep_attr_unlock(void)
1395 pthread_mutex_unlock(&grep_attr_mutex);
1399 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
1401 pthread_mutex_t grep_read_mutex;
1404 #define grep_attr_lock()
1405 #define grep_attr_unlock()
1408 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1410 xdemitconf_t *xecfg = opt->priv;
1411 if (xecfg && !xecfg->find_func) {
1412 grep_source_load_driver(gs);
1413 if (gs->driver->funcname.pattern) {
1414 const struct userdiff_funcname *pe = &gs->driver->funcname;
1415 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1417 xecfg = opt->priv = NULL;
1423 return xecfg->find_func(bol, eol - bol, buf, 1,
1424 xecfg->find_func_priv) >= 0;
1429 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1434 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1435 char *bol, unsigned lno)
1437 while (bol > gs->buf) {
1440 while (bol > gs->buf && bol[-1] != '\n')
1444 if (lno <= opt->last_shown)
1447 if (match_funcname(opt, gs, bol, eol)) {
1448 show_line(opt, bol, eol, gs->name, lno, '=');
1454 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1455 char *bol, char *end, unsigned lno)
1457 unsigned cur = lno, from = 1, funcname_lno = 0;
1458 int funcname_needed = !!opt->funcname;
1460 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1461 funcname_needed = 2;
1463 if (opt->pre_context < lno)
1464 from = lno - opt->pre_context;
1465 if (from <= opt->last_shown)
1466 from = opt->last_shown + 1;
1469 while (bol > gs->buf &&
1470 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1473 while (bol > gs->buf && bol[-1] != '\n')
1476 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1478 funcname_needed = 0;
1482 /* We need to look even further back to find a function signature. */
1483 if (opt->funcname && funcname_needed)
1484 show_funcname_line(opt, gs, bol, cur);
1488 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1490 while (*eol != '\n')
1492 show_line(opt, bol, eol, gs->name, cur, sign);
1498 static int should_lookahead(struct grep_opt *opt)
1503 return 0; /* punt for too complex stuff */
1506 for (p = opt->pattern_list; p; p = p->next) {
1507 if (p->token != GREP_PATTERN)
1508 return 0; /* punt for "header only" and stuff */
1513 static int look_ahead(struct grep_opt *opt,
1514 unsigned long *left_p,
1518 unsigned lno = *lno_p;
1521 char *sp, *last_bol;
1522 regoff_t earliest = -1;
1524 for (p = opt->pattern_list; p; p = p->next) {
1528 hit = patmatch(p, bol, bol + *left_p, &m, 0);
1529 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1531 if (earliest < 0 || m.rm_so < earliest)
1536 *bol_p = bol + *left_p;
1540 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1541 ; /* find the beginning of the line */
1544 for (sp = bol; sp < last_bol; sp++) {
1548 *left_p -= last_bol - bol;
1554 static int fill_textconv_grep(struct userdiff_driver *driver,
1555 struct grep_source *gs)
1557 struct diff_filespec *df;
1561 if (!driver || !driver->textconv)
1562 return grep_source_load(gs);
1565 * The textconv interface is intimately tied to diff_filespecs, so we
1566 * have to pretend to be one. If we could unify the grep_source
1567 * and diff_filespec structs, this mess could just go away.
1569 df = alloc_filespec(gs->path);
1571 case GREP_SOURCE_OID:
1572 fill_filespec(df, gs->identifier, 1, 0100644);
1574 case GREP_SOURCE_FILE:
1575 fill_filespec(df, &null_oid, 0, 0100644);
1578 die("BUG: attempt to textconv something without a path?");
1582 * fill_textconv is not remotely thread-safe; it may load objects
1583 * behind the scenes, and it modifies the global diff tempfile
1587 size = fill_textconv(driver, df, &buf);
1592 * The normal fill_textconv usage by the diff machinery would just keep
1593 * the textconv'd buf separate from the diff_filespec. But much of the
1594 * grep code passes around a grep_source and assumes that its "buf"
1595 * pointer is the beginning of the thing we are searching. So let's
1596 * install our textconv'd version into the grep_source, taking care not
1597 * to leak any existing buffer.
1599 grep_source_clear_data(gs);
1606 static int is_empty_line(const char *bol, const char *eol)
1608 while (bol < eol && isspace(*bol))
1613 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1616 char *peek_bol = NULL;
1619 unsigned last_hit = 0;
1620 int binary_match_only = 0;
1622 int try_lookahead = 0;
1623 int show_function = 0;
1624 struct userdiff_driver *textconv = NULL;
1625 enum grep_context ctx = GREP_CONTEXT_HEAD;
1629 opt->output = std_output;
1631 if (opt->pre_context || opt->post_context || opt->file_break ||
1633 /* Show hunk marks, except for the first file. */
1634 if (opt->last_shown)
1635 opt->show_hunk_mark = 1;
1637 * If we're using threads then we can't easily identify
1638 * the first file. Always put hunk marks in that case
1639 * and skip the very first one later in work_done().
1641 if (opt->output != std_output)
1642 opt->show_hunk_mark = 1;
1644 opt->last_shown = 0;
1646 if (opt->allow_textconv) {
1647 grep_source_load_driver(gs);
1649 * We might set up the shared textconv cache data here, which
1650 * is not thread-safe.
1653 textconv = userdiff_get_textconv(gs->driver);
1658 * We know the result of a textconv is text, so we only have to care
1659 * about binary handling if we are not using it.
1662 switch (opt->binary) {
1663 case GREP_BINARY_DEFAULT:
1664 if (grep_source_is_binary(gs))
1665 binary_match_only = 1;
1667 case GREP_BINARY_NOMATCH:
1668 if (grep_source_is_binary(gs))
1669 return 0; /* Assume unmatch */
1671 case GREP_BINARY_TEXT:
1674 die("BUG: unknown binary handling mode");
1678 memset(&xecfg, 0, sizeof(xecfg));
1681 try_lookahead = should_lookahead(opt);
1683 if (fill_textconv_grep(textconv, gs) < 0)
1693 * look_ahead() skips quickly to the line that possibly
1694 * has the next hit; don't call it if we need to do
1695 * something more than just skipping the current line
1696 * in response to an unmatch for the current line. E.g.
1697 * inside a post-context window, we will show the current
1698 * line as a context around the previous hit when it
1703 && (show_function ||
1704 lno <= last_hit + opt->post_context))
1705 && look_ahead(opt, &left, &lno, &bol))
1707 eol = end_of_line(bol, &left);
1711 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1712 ctx = GREP_CONTEXT_BODY;
1714 hit = match_line(opt, bol, eol, ctx, collect_hits);
1720 /* "grep -v -e foo -e bla" should list lines
1721 * that do not have either, so inversion should
1726 if (opt->unmatch_name_only) {
1733 if (opt->status_only)
1735 if (opt->name_only) {
1736 show_name(opt, gs->name);
1741 if (binary_match_only) {
1742 opt->output(opt, "Binary file ", 12);
1743 output_color(opt, gs->name, strlen(gs->name),
1744 opt->color_filename);
1745 opt->output(opt, " matches\n", 9);
1748 /* Hit at this line. If we haven't shown the
1749 * pre-context lines, we would need to show them.
1751 if (opt->pre_context || opt->funcbody)
1752 show_pre_context(opt, gs, bol, eol, lno);
1753 else if (opt->funcname)
1754 show_funcname_line(opt, gs, bol, lno);
1755 show_line(opt, bol, eol, gs->name, lno, ':');
1761 if (show_function && (!peek_bol || peek_bol < bol)) {
1762 unsigned long peek_left = left;
1763 char *peek_eol = eol;
1766 * Trailing empty lines are not interesting.
1767 * Peek past them to see if they belong to the
1768 * body of the current function.
1771 while (is_empty_line(peek_bol, peek_eol)) {
1772 peek_bol = peek_eol + 1;
1773 peek_eol = end_of_line(peek_bol, &peek_left);
1776 if (match_funcname(opt, gs, peek_bol, peek_eol))
1779 if (show_function ||
1780 (last_hit && lno <= last_hit + opt->post_context)) {
1781 /* If the last hit is within the post context,
1782 * we need to show this line.
1784 show_line(opt, bol, eol, gs->name, lno, '-');
1798 if (opt->status_only)
1800 if (opt->unmatch_name_only) {
1801 /* We did not see any hit, so we want to show this */
1802 show_name(opt, gs->name);
1806 xdiff_clear_find_func(&xecfg);
1810 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1811 * which feels mostly useless but sometimes useful. Maybe
1812 * make it another option? For now suppress them.
1814 if (opt->count && count) {
1816 if (opt->pathname) {
1817 output_color(opt, gs->name, strlen(gs->name),
1818 opt->color_filename);
1819 output_sep(opt, ':');
1821 xsnprintf(buf, sizeof(buf), "%u\n", count);
1822 opt->output(opt, buf, strlen(buf));
1828 static void clr_hit_marker(struct grep_expr *x)
1830 /* All-hit markers are meaningful only at the very top level
1835 if (x->node != GREP_NODE_OR)
1837 x->u.binary.left->hit = 0;
1838 x = x->u.binary.right;
1842 static int chk_hit_marker(struct grep_expr *x)
1844 /* Top level nodes have hit markers. See if they all are hits */
1846 if (x->node != GREP_NODE_OR)
1848 if (!x->u.binary.left->hit)
1850 x = x->u.binary.right;
1854 int grep_source(struct grep_opt *opt, struct grep_source *gs)
1857 * we do not have to do the two-pass grep when we do not check
1858 * buffer-wide "all-match".
1860 if (!opt->all_match)
1861 return grep_source_1(opt, gs, 0);
1863 /* Otherwise the toplevel "or" terms hit a bit differently.
1864 * We first clear hit markers from them.
1866 clr_hit_marker(opt->pattern_expression);
1867 grep_source_1(opt, gs, 1);
1869 if (!chk_hit_marker(opt->pattern_expression))
1872 return grep_source_1(opt, gs, 0);
1875 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1877 struct grep_source gs;
1880 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1884 r = grep_source(opt, &gs);
1886 grep_source_clear(&gs);
1890 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1891 const char *name, const char *path,
1892 const void *identifier)
1895 gs->name = xstrdup_or_null(name);
1896 gs->path = xstrdup_or_null(path);
1902 case GREP_SOURCE_FILE:
1903 gs->identifier = xstrdup(identifier);
1905 case GREP_SOURCE_SUBMODULE:
1907 gs->identifier = NULL;
1912 * If the identifier is non-NULL (in the submodule case) it
1913 * will be a SHA1 that needs to be copied.
1915 case GREP_SOURCE_OID:
1916 gs->identifier = oiddup(identifier);
1918 case GREP_SOURCE_BUF:
1919 gs->identifier = NULL;
1924 void grep_source_clear(struct grep_source *gs)
1926 FREE_AND_NULL(gs->name);
1927 FREE_AND_NULL(gs->path);
1928 FREE_AND_NULL(gs->identifier);
1929 grep_source_clear_data(gs);
1932 void grep_source_clear_data(struct grep_source *gs)
1935 case GREP_SOURCE_FILE:
1936 case GREP_SOURCE_OID:
1937 case GREP_SOURCE_SUBMODULE:
1938 FREE_AND_NULL(gs->buf);
1941 case GREP_SOURCE_BUF:
1942 /* leave user-provided buf intact */
1947 static int grep_source_load_oid(struct grep_source *gs)
1949 enum object_type type;
1952 gs->buf = read_sha1_file(gs->identifier, &type, &gs->size);
1956 return error(_("'%s': unable to read %s"),
1958 oid_to_hex(gs->identifier));
1962 static int grep_source_load_file(struct grep_source *gs)
1964 const char *filename = gs->identifier;
1970 if (lstat(filename, &st) < 0) {
1972 if (errno != ENOENT)
1973 error_errno(_("failed to stat '%s'"), filename);
1976 if (!S_ISREG(st.st_mode))
1978 size = xsize_t(st.st_size);
1979 i = open(filename, O_RDONLY);
1982 data = xmallocz(size);
1983 if (st.st_size != read_in_full(i, data, size)) {
1984 error_errno(_("'%s': short read"), filename);
1996 static int grep_source_load(struct grep_source *gs)
2002 case GREP_SOURCE_FILE:
2003 return grep_source_load_file(gs);
2004 case GREP_SOURCE_OID:
2005 return grep_source_load_oid(gs);
2006 case GREP_SOURCE_BUF:
2007 return gs->buf ? 0 : -1;
2008 case GREP_SOURCE_SUBMODULE:
2011 die("BUG: invalid grep_source type to load");
2014 void grep_source_load_driver(struct grep_source *gs)
2021 gs->driver = userdiff_find_by_path(gs->path);
2023 gs->driver = userdiff_find_by_name("default");
2027 static int grep_source_is_binary(struct grep_source *gs)
2029 grep_source_load_driver(gs);
2030 if (gs->driver->binary != -1)
2031 return gs->driver->binary;
2033 if (!grep_source_load(gs))
2034 return buffer_is_binary(gs->buf, gs->size);