4 #include "object-store.h"
6 #include "xdiff-interface.h"
13 static int grep_source_load(struct grep_source
*gs
);
14 static int grep_source_is_binary(struct grep_source
*gs
,
15 struct index_state
*istate
);
17 static void std_output(struct grep_opt
*opt
, const void *buf
, size_t size
)
19 fwrite(buf
, size
, 1, stdout
);
22 static struct grep_opt grep_defaults
= {
26 .pattern_type_option
= GREP_PATTERN_TYPE_UNSPECIFIED
,
28 [GREP_COLOR_CONTEXT
] = "",
29 [GREP_COLOR_FILENAME
] = "",
30 [GREP_COLOR_FUNCTION
] = "",
31 [GREP_COLOR_LINENO
] = "",
32 [GREP_COLOR_COLUMNNO
] = "",
33 [GREP_COLOR_MATCH_CONTEXT
] = GIT_COLOR_BOLD_RED
,
34 [GREP_COLOR_MATCH_SELECTED
] = GIT_COLOR_BOLD_RED
,
35 [GREP_COLOR_SELECTED
] = "",
36 [GREP_COLOR_SEP
] = GIT_COLOR_CYAN
,
44 static pcre2_general_context
*pcre2_global_context
;
46 static void *pcre2_malloc(PCRE2_SIZE size
, MAYBE_UNUSED
void *memory_data
)
51 static void pcre2_free(void *pointer
, MAYBE_UNUSED
void *memory_data
)
57 static const char *color_grep_slots
[] = {
58 [GREP_COLOR_CONTEXT
] = "context",
59 [GREP_COLOR_FILENAME
] = "filename",
60 [GREP_COLOR_FUNCTION
] = "function",
61 [GREP_COLOR_LINENO
] = "lineNumber",
62 [GREP_COLOR_COLUMNNO
] = "column",
63 [GREP_COLOR_MATCH_CONTEXT
] = "matchContext",
64 [GREP_COLOR_MATCH_SELECTED
] = "matchSelected",
65 [GREP_COLOR_SELECTED
] = "selected",
66 [GREP_COLOR_SEP
] = "separator",
69 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
71 if (!strcmp(arg
, "default"))
72 return GREP_PATTERN_TYPE_UNSPECIFIED
;
73 else if (!strcmp(arg
, "basic"))
74 return GREP_PATTERN_TYPE_BRE
;
75 else if (!strcmp(arg
, "extended"))
76 return GREP_PATTERN_TYPE_ERE
;
77 else if (!strcmp(arg
, "fixed"))
78 return GREP_PATTERN_TYPE_FIXED
;
79 else if (!strcmp(arg
, "perl"))
80 return GREP_PATTERN_TYPE_PCRE
;
81 die("bad %s argument: %s", opt
, arg
);
84 define_list_config_array_extra(color_grep_slots
, {"match"});
87 * Read the configuration file once and store it in
88 * the grep_defaults template.
90 int grep_config(const char *var
, const char *value
, void *cb
)
92 struct grep_opt
*opt
= &grep_defaults
;
95 if (userdiff_config(var
, value
) < 0)
99 * The instance of grep_opt that we set up here is copied by
100 * grep_init() to be used by each individual invocation.
101 * When populating a new field of this structure here, be
102 * sure to think about ownership -- e.g., you might need to
103 * override the shallow copy in grep_init() with a deep copy.
106 if (!strcmp(var
, "grep.extendedregexp")) {
107 opt
->extended_regexp_option
= git_config_bool(var
, value
);
111 if (!strcmp(var
, "grep.patterntype")) {
112 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
116 if (!strcmp(var
, "grep.linenumber")) {
117 opt
->linenum
= git_config_bool(var
, value
);
120 if (!strcmp(var
, "grep.column")) {
121 opt
->columnnum
= git_config_bool(var
, value
);
125 if (!strcmp(var
, "grep.fullname")) {
126 opt
->relative
= !git_config_bool(var
, value
);
130 if (!strcmp(var
, "color.grep"))
131 opt
->color
= git_config_colorbool(var
, value
);
132 if (!strcmp(var
, "color.grep.match")) {
133 if (grep_config("color.grep.matchcontext", value
, cb
) < 0)
135 if (grep_config("color.grep.matchselected", value
, cb
) < 0)
137 } else if (skip_prefix(var
, "color.grep.", &slot
)) {
138 int i
= LOOKUP_CONFIG(color_grep_slots
, slot
);
143 color
= opt
->colors
[i
];
145 return config_error_nonbool(var
);
146 return color_parse(value
, color
);
152 * Initialize one instance of grep_opt and copy the
153 * default values from the template we read the configuration
154 * information in an earlier call to git_config(grep_config).
156 * If using PCRE, make sure that the library is configured
157 * to use the same allocator as Git (e.g. nedmalloc on Windows).
159 * Any allocated memory needs to be released in grep_destroy().
161 void grep_init(struct grep_opt
*opt
, struct repository
*repo
, const char *prefix
)
163 #if defined(USE_LIBPCRE2)
164 if (!pcre2_global_context
)
165 pcre2_global_context
= pcre2_general_context_create(
166 pcre2_malloc
, pcre2_free
, NULL
);
169 *opt
= grep_defaults
;
172 opt
->prefix
= prefix
;
173 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
174 opt
->pattern_tail
= &opt
->pattern_list
;
175 opt
->header_tail
= &opt
->header_list
;
178 void grep_destroy(void)
181 pcre2_general_context_free(pcre2_global_context
);
185 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
188 * When committing to the pattern type by setting the relevant
189 * fields in grep_opt it's generally not necessary to zero out
190 * the fields we're not choosing, since they won't have been
191 * set by anything. The extended_regexp_option field is the
192 * only exception to this.
194 * This is because in the process of parsing grep.patternType
195 * & grep.extendedRegexp we set opt->pattern_type_option and
196 * opt->extended_regexp_option, respectively. We then
197 * internally use opt->extended_regexp_option to see if we're
198 * compiling an ERE. It must be unset if that's not actually
201 if (pattern_type
!= GREP_PATTERN_TYPE_ERE
&&
202 opt
->extended_regexp_option
)
203 opt
->extended_regexp_option
= 0;
205 switch (pattern_type
) {
206 case GREP_PATTERN_TYPE_UNSPECIFIED
:
209 case GREP_PATTERN_TYPE_BRE
:
212 case GREP_PATTERN_TYPE_ERE
:
213 opt
->extended_regexp_option
= 1;
216 case GREP_PATTERN_TYPE_FIXED
:
220 case GREP_PATTERN_TYPE_PCRE
:
226 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
228 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
229 grep_set_pattern_type_option(pattern_type
, opt
);
230 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
231 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
232 else if (opt
->extended_regexp_option
)
234 * This branch *must* happen after setting from the
235 * opt->pattern_type_option above, we don't want
236 * grep.extendedRegexp to override grep.patternType!
238 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
241 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
242 const char *origin
, int no
,
243 enum grep_pat_token t
,
244 enum grep_header_field field
)
246 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
247 p
->pattern
= xmemdupz(pat
, patlen
);
248 p
->patternlen
= patlen
;
256 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
263 case GREP_PATTERN
: /* atom */
264 case GREP_PATTERN_HEAD
:
265 case GREP_PATTERN_BODY
:
267 struct grep_pat
*new_pat
;
269 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
270 while (++len
<= p
->patternlen
) {
271 if (*(--cp
) == '\n') {
278 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
279 p
->no
, p
->token
, p
->field
);
280 new_pat
->next
= p
->next
;
282 *tail
= &new_pat
->next
;
285 p
->patternlen
-= len
;
293 void append_header_grep_pattern(struct grep_opt
*opt
,
294 enum grep_header_field field
, const char *pat
)
296 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
297 GREP_PATTERN_HEAD
, field
);
298 if (field
== GREP_HEADER_REFLOG
)
299 opt
->use_reflog_filter
= 1;
300 do_append_grep_pat(&opt
->header_tail
, p
);
303 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
304 const char *origin
, int no
, enum grep_pat_token t
)
306 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
309 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
310 const char *origin
, int no
, enum grep_pat_token t
)
312 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
313 do_append_grep_pat(&opt
->pattern_tail
, p
);
316 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
318 struct grep_pat
*pat
;
319 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
322 ret
->pattern_list
= NULL
;
323 ret
->pattern_tail
= &ret
->pattern_list
;
325 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
327 if(pat
->token
== GREP_PATTERN_HEAD
)
328 append_header_grep_pattern(ret
, pat
->field
,
331 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
332 pat
->origin
, pat
->no
, pat
->token
);
338 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
344 xsnprintf(where
, sizeof(where
), "In '%s' at %d, ", p
->origin
, p
->no
);
346 xsnprintf(where
, sizeof(where
), "%s, ", p
->origin
);
350 die("%s'%s': %s", where
, p
->pattern
, error
);
353 static int is_fixed(const char *s
, size_t len
)
357 for (i
= 0; i
< len
; i
++) {
358 if (is_regex_special(s
[i
]))
366 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
369 PCRE2_UCHAR errbuf
[256];
370 PCRE2_SIZE erroffset
;
371 int options
= PCRE2_MULTILINE
;
378 p
->pcre2_compile_context
= NULL
;
380 /* pcre2_global_context is initialized in append_grep_pattern */
381 if (opt
->ignore_case
) {
382 if (!opt
->ignore_locale
&& has_non_ascii(p
->pattern
)) {
383 if (!pcre2_global_context
)
384 BUG("pcre2_global_context uninitialized");
385 p
->pcre2_tables
= pcre2_maketables(pcre2_global_context
);
386 p
->pcre2_compile_context
= pcre2_compile_context_create(NULL
);
387 pcre2_set_character_tables(p
->pcre2_compile_context
,
390 options
|= PCRE2_CASELESS
;
392 if (!opt
->ignore_locale
&& is_utf8_locale() && has_non_ascii(p
->pattern
) &&
393 !(!opt
->ignore_case
&& (p
->fixed
|| p
->is_fixed
)))
394 options
|= PCRE2_UTF
;
396 p
->pcre2_pattern
= pcre2_compile((PCRE2_SPTR
)p
->pattern
,
397 p
->patternlen
, options
, &error
, &erroffset
,
398 p
->pcre2_compile_context
);
400 if (p
->pcre2_pattern
) {
401 p
->pcre2_match_data
= pcre2_match_data_create_from_pattern(p
->pcre2_pattern
, NULL
);
402 if (!p
->pcre2_match_data
)
403 die("Couldn't allocate PCRE2 match data");
405 pcre2_get_error_message(error
, errbuf
, sizeof(errbuf
));
406 compile_regexp_failed(p
, (const char *)&errbuf
);
409 pcre2_config(PCRE2_CONFIG_JIT
, &p
->pcre2_jit_on
);
411 fprintf(stderr
, "pcre2_jit_on=%d\n", p
->pcre2_jit_on
);
412 if (p
->pcre2_jit_on
) {
413 jitret
= pcre2_jit_compile(p
->pcre2_pattern
, PCRE2_JIT_COMPLETE
);
415 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p
->pattern
, jitret
);
418 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
419 * tells us whether the library itself supports JIT,
420 * but to see whether we're going to be actually using
421 * JIT we need to extract PCRE2_INFO_JITSIZE from the
422 * pattern *after* we do pcre2_jit_compile() above.
424 * This is because if the pattern contains the
425 * (*NO_JIT) verb (see pcre2syntax(3))
426 * pcre2_jit_compile() will exit early with 0. If we
427 * then proceed to call pcre2_jit_match() further down
428 * the line instead of pcre2_match() we'll either
429 * segfault (pre PCRE 10.31) or run into a fatal error
432 patinforet
= pcre2_pattern_info(p
->pcre2_pattern
, PCRE2_INFO_JITSIZE
, &jitsizearg
);
434 BUG("pcre2_pattern_info() failed: %d", patinforet
);
435 if (jitsizearg
== 0) {
438 fprintf(stderr
, "pcre2_jit_on=%d: (*NO_JIT) in regex\n",
445 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
446 regmatch_t
*match
, int eflags
)
450 PCRE2_UCHAR errbuf
[256];
452 if (eflags
& REG_NOTBOL
)
453 flags
|= PCRE2_NOTBOL
;
456 ret
= pcre2_jit_match(p
->pcre2_pattern
, (unsigned char *)line
,
457 eol
- line
, 0, flags
, p
->pcre2_match_data
,
460 ret
= pcre2_match(p
->pcre2_pattern
, (unsigned char *)line
,
461 eol
- line
, 0, flags
, p
->pcre2_match_data
,
464 if (ret
< 0 && ret
!= PCRE2_ERROR_NOMATCH
) {
465 pcre2_get_error_message(ret
, errbuf
, sizeof(errbuf
));
466 die("%s failed with error code %d: %s",
467 (p
->pcre2_jit_on
? "pcre2_jit_match" : "pcre2_match"), ret
,
471 ovector
= pcre2_get_ovector_pointer(p
->pcre2_match_data
);
473 match
->rm_so
= (int)ovector
[0];
474 match
->rm_eo
= (int)ovector
[1];
480 static void free_pcre2_pattern(struct grep_pat
*p
)
482 pcre2_compile_context_free(p
->pcre2_compile_context
);
483 pcre2_code_free(p
->pcre2_pattern
);
484 pcre2_match_data_free(p
->pcre2_match_data
);
485 free((void *)p
->pcre2_tables
);
487 #else /* !USE_LIBPCRE2 */
488 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
490 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
493 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
494 regmatch_t
*match
, int eflags
)
499 static void free_pcre2_pattern(struct grep_pat
*p
)
503 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
505 struct strbuf sb
= STRBUF_INIT
;
509 basic_regex_quote_buf(&sb
, p
->pattern
);
510 if (opt
->ignore_case
)
511 regflags
|= REG_ICASE
;
512 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
514 fprintf(stderr
, "fixed %s\n", sb
.buf
);
518 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
519 compile_regexp_failed(p
, errbuf
);
522 #endif /* !USE_LIBPCRE2 */
524 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
527 int regflags
= REG_NEWLINE
;
529 p
->word_regexp
= opt
->word_regexp
;
530 p
->ignore_case
= opt
->ignore_case
;
531 p
->fixed
= opt
->fixed
;
533 if (memchr(p
->pattern
, 0, p
->patternlen
) && !opt
->pcre2
)
534 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
536 p
->is_fixed
= is_fixed(p
->pattern
, p
->patternlen
);
538 if (!p
->fixed
&& !p
->is_fixed
) {
539 const char *no_jit
= "(*NO_JIT)";
540 const int no_jit_len
= strlen(no_jit
);
541 if (starts_with(p
->pattern
, no_jit
) &&
542 is_fixed(p
->pattern
+ no_jit_len
,
543 p
->patternlen
- no_jit_len
))
547 if (p
->fixed
|| p
->is_fixed
) {
551 compile_pcre2_pattern(p
, opt
);
554 * E.g. t7811-grep-open.sh relies on the
555 * pattern being restored.
557 char *old_pattern
= p
->pattern
;
558 size_t old_patternlen
= p
->patternlen
;
559 struct strbuf sb
= STRBUF_INIT
;
562 * There is the PCRE2_LITERAL flag, but it's
563 * only in PCRE v2 10.30 and later. Needing to
564 * ifdef our way around that and dealing with
565 * it + PCRE2_MULTILINE being an error is more
566 * complex than just quoting this ourselves.
568 strbuf_add(&sb
, "\\Q", 2);
569 strbuf_add(&sb
, p
->pattern
, p
->patternlen
);
570 strbuf_add(&sb
, "\\E", 2);
573 p
->patternlen
= sb
.len
;
574 compile_pcre2_pattern(p
, opt
);
575 p
->pattern
= old_pattern
;
576 p
->patternlen
= old_patternlen
;
579 #else /* !USE_LIBPCRE2 */
580 compile_fixed_regexp(p
, opt
);
581 #endif /* !USE_LIBPCRE2 */
586 compile_pcre2_pattern(p
, opt
);
591 regflags
|= REG_ICASE
;
592 if (opt
->extended_regexp_option
)
593 regflags
|= REG_EXTENDED
;
594 err
= regcomp(&p
->regexp
, p
->pattern
, regflags
);
597 regerror(err
, &p
->regexp
, errbuf
, 1024);
598 compile_regexp_failed(p
, errbuf
);
602 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
603 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
612 case GREP_PATTERN
: /* atom */
613 case GREP_PATTERN_HEAD
:
614 case GREP_PATTERN_BODY
:
615 x
= xcalloc(1, sizeof (struct grep_expr
));
616 x
->node
= GREP_NODE_ATOM
;
620 case GREP_OPEN_PAREN
:
622 x
= compile_pattern_or(list
);
623 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
624 die("unmatched parenthesis");
625 *list
= (*list
)->next
;
632 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
643 die("--not not followed by pattern expression");
645 x
= xcalloc(1, sizeof (struct grep_expr
));
646 x
->node
= GREP_NODE_NOT
;
647 x
->u
.unary
= compile_pattern_not(list
);
649 die("--not followed by non pattern expression");
652 return compile_pattern_atom(list
);
656 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
659 struct grep_expr
*x
, *y
, *z
;
661 x
= compile_pattern_not(list
);
663 if (p
&& p
->token
== GREP_AND
) {
665 die("--and not followed by pattern expression");
667 y
= compile_pattern_and(list
);
669 die("--and not followed by pattern expression");
670 z
= xcalloc(1, sizeof (struct grep_expr
));
671 z
->node
= GREP_NODE_AND
;
672 z
->u
.binary
.left
= x
;
673 z
->u
.binary
.right
= y
;
679 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
682 struct grep_expr
*x
, *y
, *z
;
684 x
= compile_pattern_and(list
);
686 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
687 y
= compile_pattern_or(list
);
689 die("not a pattern expression %s", p
->pattern
);
690 z
= xcalloc(1, sizeof (struct grep_expr
));
691 z
->node
= GREP_NODE_OR
;
692 z
->u
.binary
.left
= x
;
693 z
->u
.binary
.right
= y
;
699 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
701 return compile_pattern_or(list
);
704 static void indent(int in
)
710 static void dump_grep_pat(struct grep_pat
*p
)
713 case GREP_AND
: fprintf(stderr
, "*and*"); break;
714 case GREP_OPEN_PAREN
: fprintf(stderr
, "*(*"); break;
715 case GREP_CLOSE_PAREN
: fprintf(stderr
, "*)*"); break;
716 case GREP_NOT
: fprintf(stderr
, "*not*"); break;
717 case GREP_OR
: fprintf(stderr
, "*or*"); break;
719 case GREP_PATTERN
: fprintf(stderr
, "pattern"); break;
720 case GREP_PATTERN_HEAD
: fprintf(stderr
, "pattern_head"); break;
721 case GREP_PATTERN_BODY
: fprintf(stderr
, "pattern_body"); break;
726 case GREP_PATTERN_HEAD
:
727 fprintf(stderr
, "<head %d>", p
->field
); break;
728 case GREP_PATTERN_BODY
:
729 fprintf(stderr
, "<body>"); break;
733 case GREP_PATTERN_HEAD
:
734 case GREP_PATTERN_BODY
:
736 fprintf(stderr
, "%.*s", (int)p
->patternlen
, p
->pattern
);
742 static void dump_grep_expression_1(struct grep_expr
*x
, int in
)
747 fprintf(stderr
, "true\n");
750 dump_grep_pat(x
->u
.atom
);
753 fprintf(stderr
, "(not\n");
754 dump_grep_expression_1(x
->u
.unary
, in
+1);
756 fprintf(stderr
, ")\n");
759 fprintf(stderr
, "(and\n");
760 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
761 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
763 fprintf(stderr
, ")\n");
766 fprintf(stderr
, "(or\n");
767 dump_grep_expression_1(x
->u
.binary
.left
, in
+1);
768 dump_grep_expression_1(x
->u
.binary
.right
, in
+1);
770 fprintf(stderr
, ")\n");
775 static void dump_grep_expression(struct grep_opt
*opt
)
777 struct grep_expr
*x
= opt
->pattern_expression
;
780 fprintf(stderr
, "[all-match]\n");
781 dump_grep_expression_1(x
, 0);
785 static struct grep_expr
*grep_true_expr(void)
787 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
788 z
->node
= GREP_NODE_TRUE
;
792 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
794 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
795 z
->node
= GREP_NODE_OR
;
796 z
->u
.binary
.left
= left
;
797 z
->u
.binary
.right
= right
;
801 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
804 struct grep_expr
*header_expr
;
805 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
806 enum grep_header_field fld
;
808 if (!opt
->header_list
)
811 for (p
= opt
->header_list
; p
; p
= p
->next
) {
812 if (p
->token
!= GREP_PATTERN_HEAD
)
813 BUG("a non-header pattern in grep header list.");
814 if (p
->field
< GREP_HEADER_FIELD_MIN
||
815 GREP_HEADER_FIELD_MAX
<= p
->field
)
816 BUG("unknown header field %d", p
->field
);
817 compile_regexp(p
, opt
);
820 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
821 header_group
[fld
] = NULL
;
823 for (p
= opt
->header_list
; p
; p
= p
->next
) {
825 struct grep_pat
*pp
= p
;
827 h
= compile_pattern_atom(&pp
);
828 if (!h
|| pp
!= p
->next
)
829 BUG("malformed header expr");
830 if (!header_group
[p
->field
]) {
831 header_group
[p
->field
] = h
;
834 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
839 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
840 if (!header_group
[fld
])
843 header_expr
= grep_true_expr();
844 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
849 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
851 struct grep_expr
*z
= x
;
854 assert(x
->node
== GREP_NODE_OR
);
855 if (x
->u
.binary
.right
&&
856 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
857 x
->u
.binary
.right
= y
;
860 x
= x
->u
.binary
.right
;
865 static void compile_grep_patterns_real(struct grep_opt
*opt
)
868 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
870 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
872 case GREP_PATTERN
: /* atom */
873 case GREP_PATTERN_HEAD
:
874 case GREP_PATTERN_BODY
:
875 compile_regexp(p
, opt
);
883 if (opt
->all_match
|| header_expr
)
885 else if (!opt
->extended
&& !opt
->debug
)
888 p
= opt
->pattern_list
;
890 opt
->pattern_expression
= compile_pattern_expr(&p
);
892 die("incomplete pattern expression: %s", p
->pattern
);
897 if (!opt
->pattern_expression
)
898 opt
->pattern_expression
= header_expr
;
899 else if (opt
->all_match
)
900 opt
->pattern_expression
= grep_splice_or(header_expr
,
901 opt
->pattern_expression
);
903 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
908 void compile_grep_patterns(struct grep_opt
*opt
)
910 compile_grep_patterns_real(opt
);
912 dump_grep_expression(opt
);
915 static void free_pattern_expr(struct grep_expr
*x
)
922 free_pattern_expr(x
->u
.unary
);
926 free_pattern_expr(x
->u
.binary
.left
);
927 free_pattern_expr(x
->u
.binary
.right
);
933 void free_grep_patterns(struct grep_opt
*opt
)
935 struct grep_pat
*p
, *n
;
937 for (p
= opt
->pattern_list
; p
; p
= n
) {
940 case GREP_PATTERN
: /* atom */
941 case GREP_PATTERN_HEAD
:
942 case GREP_PATTERN_BODY
:
943 if (p
->pcre2_pattern
)
944 free_pcre2_pattern(p
);
957 free_pattern_expr(opt
->pattern_expression
);
960 static char *end_of_line(char *cp
, unsigned long *left
)
962 unsigned long l
= *left
;
963 while (l
&& *cp
!= '\n') {
971 static int word_char(char ch
)
973 return isalnum(ch
) || ch
== '_';
976 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
979 if (want_color(opt
->color
) && color
&& color
[0]) {
980 opt
->output(opt
, color
, strlen(color
));
981 opt
->output(opt
, data
, size
);
982 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
984 opt
->output(opt
, data
, size
);
987 static void output_sep(struct grep_opt
*opt
, char sign
)
989 if (opt
->null_following_name
)
990 opt
->output(opt
, "\0", 1);
992 output_color(opt
, &sign
, 1, opt
->colors
[GREP_COLOR_SEP
]);
995 static void show_name(struct grep_opt
*opt
, const char *name
)
997 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
998 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
1001 static int patmatch(struct grep_pat
*p
, char *line
, char *eol
,
1002 regmatch_t
*match
, int eflags
)
1006 if (p
->pcre2_pattern
)
1007 hit
= !pcre2match(p
, line
, eol
, match
, eflags
);
1009 hit
= !regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
,
1015 static int strip_timestamp(char *bol
, char **eol_p
)
1020 while (bol
< --eol
) {
1034 } header_field
[] = {
1036 { "committer ", 10 },
1040 static int match_one_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1041 enum grep_context ctx
,
1042 regmatch_t
*pmatch
, int eflags
)
1046 const char *start
= bol
;
1048 if ((p
->token
!= GREP_PATTERN
) &&
1049 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
1052 if (p
->token
== GREP_PATTERN_HEAD
) {
1055 assert(p
->field
< ARRAY_SIZE(header_field
));
1056 field
= header_field
[p
->field
].field
;
1057 len
= header_field
[p
->field
].len
;
1058 if (strncmp(bol
, field
, len
))
1062 case GREP_HEADER_AUTHOR
:
1063 case GREP_HEADER_COMMITTER
:
1064 saved_ch
= strip_timestamp(bol
, &eol
);
1072 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
1074 if (hit
&& p
->word_regexp
) {
1075 if ((pmatch
[0].rm_so
< 0) ||
1076 (eol
- bol
) < pmatch
[0].rm_so
||
1077 (pmatch
[0].rm_eo
< 0) ||
1078 (eol
- bol
) < pmatch
[0].rm_eo
)
1079 die("regexp returned nonsense");
1081 /* Match beginning must be either beginning of the
1082 * line, or at word boundary (i.e. the last char must
1083 * not be a word char). Similarly, match end must be
1084 * either end of the line, or at word boundary
1085 * (i.e. the next char must not be a word char).
1087 if ( ((pmatch
[0].rm_so
== 0) ||
1088 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
1089 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
1090 !word_char(bol
[pmatch
[0].rm_eo
])) )
1095 /* Words consist of at least one character. */
1096 if (pmatch
->rm_so
== pmatch
->rm_eo
)
1099 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
1100 /* There could be more than one match on the
1101 * line, and the first match might not be
1102 * strict word match. But later ones could be!
1103 * Forward to the next possible start, i.e. the
1104 * next position following a non-word char.
1106 bol
= pmatch
[0].rm_so
+ bol
+ 1;
1107 while (word_char(bol
[-1]) && bol
< eol
)
1109 eflags
|= REG_NOTBOL
;
1114 if (p
->token
== GREP_PATTERN_HEAD
&& saved_ch
)
1117 pmatch
[0].rm_so
+= bol
- start
;
1118 pmatch
[0].rm_eo
+= bol
- start
;
1123 static int match_expr_eval(struct grep_opt
*opt
, struct grep_expr
*x
, char *bol
,
1124 char *eol
, enum grep_context ctx
, ssize_t
*col
,
1125 ssize_t
*icol
, int collect_hits
)
1130 die("Not a valid grep expression");
1132 case GREP_NODE_TRUE
:
1135 case GREP_NODE_ATOM
:
1138 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
,
1140 if (h
&& (*col
< 0 || tmp
.rm_so
< *col
))
1146 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1148 h
= !match_expr_eval(opt
, x
->u
.unary
, bol
, eol
, ctx
, icol
, col
,
1152 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1154 if (h
|| opt
->columnnum
) {
1156 * Don't short-circuit AND when given --column, since a
1157 * NOT earlier in the tree may turn this into an OR. In
1158 * this case, see the below comment.
1160 h
&= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
,
1165 if (!(collect_hits
|| opt
->columnnum
)) {
1167 * Don't short-circuit OR when given --column (or
1168 * collecting hits) to ensure we don't skip a later
1169 * child that would produce an earlier match.
1171 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
,
1172 ctx
, col
, icol
, 0) ||
1173 match_expr_eval(opt
, x
->u
.binary
.right
, bol
,
1174 eol
, ctx
, col
, icol
, 0));
1176 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1179 x
->u
.binary
.left
->hit
|= h
;
1180 h
|= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
, col
,
1181 icol
, collect_hits
);
1184 die("Unexpected node type (internal error) %d", x
->node
);
1191 static int match_expr(struct grep_opt
*opt
, char *bol
, char *eol
,
1192 enum grep_context ctx
, ssize_t
*col
,
1193 ssize_t
*icol
, int collect_hits
)
1195 struct grep_expr
*x
= opt
->pattern_expression
;
1196 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, col
, icol
, collect_hits
);
1199 static int match_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1200 ssize_t
*col
, ssize_t
*icol
,
1201 enum grep_context ctx
, int collect_hits
)
1207 return match_expr(opt
, bol
, eol
, ctx
, col
, icol
,
1210 /* we do not call with collect_hits without being extended */
1211 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1213 if (match_one_pattern(p
, bol
, eol
, ctx
, &tmp
, 0)) {
1215 if (!opt
->columnnum
) {
1217 * Without --column, any single match on a line
1218 * is enough to know that it needs to be
1219 * printed. With --column, scan _all_ patterns
1220 * to find the earliest.
1224 if (*col
< 0 || tmp
.rm_so
< *col
)
1231 static int match_next_pattern(struct grep_pat
*p
, char *bol
, char *eol
,
1232 enum grep_context ctx
,
1233 regmatch_t
*pmatch
, int eflags
)
1237 if (!match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1239 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1241 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1242 if (match
.rm_so
> pmatch
->rm_so
)
1244 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1247 pmatch
->rm_so
= match
.rm_so
;
1248 pmatch
->rm_eo
= match
.rm_eo
;
1252 static int next_match(struct grep_opt
*opt
, char *bol
, char *eol
,
1253 enum grep_context ctx
, regmatch_t
*pmatch
, int eflags
)
1258 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1260 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1262 case GREP_PATTERN
: /* atom */
1263 case GREP_PATTERN_HEAD
:
1264 case GREP_PATTERN_BODY
:
1265 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1276 static void show_line_header(struct grep_opt
*opt
, const char *name
,
1277 unsigned lno
, ssize_t cno
, char sign
)
1279 if (opt
->heading
&& opt
->last_shown
== 0) {
1280 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1281 opt
->output(opt
, "\n", 1);
1283 opt
->last_shown
= lno
;
1285 if (!opt
->heading
&& opt
->pathname
) {
1286 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1287 output_sep(opt
, sign
);
1291 xsnprintf(buf
, sizeof(buf
), "%d", lno
);
1292 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_LINENO
]);
1293 output_sep(opt
, sign
);
1296 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1297 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1298 * being called with a context line.
1300 if (opt
->columnnum
&& cno
) {
1302 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, (uintmax_t)cno
);
1303 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_COLUMNNO
]);
1304 output_sep(opt
, sign
);
1308 static void show_line(struct grep_opt
*opt
, char *bol
, char *eol
,
1309 const char *name
, unsigned lno
, ssize_t cno
, char sign
)
1311 int rest
= eol
- bol
;
1312 const char *match_color
= NULL
;
1313 const char *line_color
= NULL
;
1315 if (opt
->file_break
&& opt
->last_shown
== 0) {
1316 if (opt
->show_hunk_mark
)
1317 opt
->output(opt
, "\n", 1);
1318 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1319 if (opt
->last_shown
== 0) {
1320 if (opt
->show_hunk_mark
) {
1321 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1322 opt
->output(opt
, "\n", 1);
1324 } else if (lno
> opt
->last_shown
+ 1) {
1325 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1326 opt
->output(opt
, "\n", 1);
1329 if (!opt
->only_matching
) {
1331 * In case the line we're being called with contains more than
1332 * one match, leave printing each header to the loop below.
1334 show_line_header(opt
, name
, lno
, cno
, sign
);
1336 if (opt
->color
|| opt
->only_matching
) {
1338 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1344 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
1346 match_color
= opt
->colors
[GREP_COLOR_MATCH_CONTEXT
];
1348 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
1349 else if (sign
== '-')
1350 line_color
= opt
->colors
[GREP_COLOR_CONTEXT
];
1351 else if (sign
== '=')
1352 line_color
= opt
->colors
[GREP_COLOR_FUNCTION
];
1355 while (next_match(opt
, bol
, eol
, ctx
, &match
, eflags
)) {
1356 if (match
.rm_so
== match
.rm_eo
)
1359 if (opt
->only_matching
)
1360 show_line_header(opt
, name
, lno
, cno
, sign
);
1362 output_color(opt
, bol
, match
.rm_so
, line_color
);
1363 output_color(opt
, bol
+ match
.rm_so
,
1364 match
.rm_eo
- match
.rm_so
, match_color
);
1365 if (opt
->only_matching
)
1366 opt
->output(opt
, "\n", 1);
1369 rest
-= match
.rm_eo
;
1370 eflags
= REG_NOTBOL
;
1374 if (!opt
->only_matching
) {
1375 output_color(opt
, bol
, rest
, line_color
);
1376 opt
->output(opt
, "\n", 1);
1383 * This lock protects access to the gitattributes machinery, which is
1386 pthread_mutex_t grep_attr_mutex
;
1388 static inline void grep_attr_lock(void)
1391 pthread_mutex_lock(&grep_attr_mutex
);
1394 static inline void grep_attr_unlock(void)
1397 pthread_mutex_unlock(&grep_attr_mutex
);
1400 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
, char *bol
, char *eol
)
1402 xdemitconf_t
*xecfg
= opt
->priv
;
1403 if (xecfg
&& !xecfg
->find_func
) {
1404 grep_source_load_driver(gs
, opt
->repo
->index
);
1405 if (gs
->driver
->funcname
.pattern
) {
1406 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1407 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1409 xecfg
= opt
->priv
= NULL
;
1415 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1416 xecfg
->find_func_priv
) >= 0;
1421 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1426 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1427 char *bol
, unsigned lno
)
1429 while (bol
> gs
->buf
) {
1432 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1436 if (lno
<= opt
->last_shown
)
1439 if (match_funcname(opt
, gs
, bol
, eol
)) {
1440 show_line(opt
, bol
, eol
, gs
->name
, lno
, 0, '=');
1446 static int is_empty_line(const char *bol
, const char *eol
);
1448 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1449 char *bol
, char *end
, unsigned lno
)
1451 unsigned cur
= lno
, from
= 1, funcname_lno
= 0, orig_from
;
1452 int funcname_needed
= !!opt
->funcname
, comment_needed
= 0;
1454 if (opt
->pre_context
< lno
)
1455 from
= lno
- opt
->pre_context
;
1456 if (from
<= opt
->last_shown
)
1457 from
= opt
->last_shown
+ 1;
1459 if (opt
->funcbody
) {
1460 if (match_funcname(opt
, gs
, bol
, end
))
1463 funcname_needed
= 1;
1464 from
= opt
->last_shown
+ 1;
1468 while (bol
> gs
->buf
&& cur
> from
) {
1469 char *next_bol
= bol
;
1472 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1475 if (comment_needed
&& (is_empty_line(bol
, eol
) ||
1476 match_funcname(opt
, gs
, bol
, eol
))) {
1485 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1487 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
, 0, 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 repository
*r
,
1568 struct userdiff_driver
*driver
,
1569 struct grep_source
*gs
)
1571 struct diff_filespec
*df
;
1575 if (!driver
|| !driver
->textconv
)
1576 return grep_source_load(gs
);
1579 * The textconv interface is intimately tied to diff_filespecs, so we
1580 * have to pretend to be one. If we could unify the grep_source
1581 * and diff_filespec structs, this mess could just go away.
1583 df
= alloc_filespec(gs
->path
);
1585 case GREP_SOURCE_OID
:
1586 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1588 case GREP_SOURCE_FILE
:
1589 fill_filespec(df
, &null_oid
, 0, 0100644);
1592 BUG("attempt to textconv something without a path?");
1596 * fill_textconv is not remotely thread-safe; it modifies the global
1597 * diff tempfile structure, writes to the_repo's odb and might
1598 * internally call thread-unsafe functions such as the
1599 * prepare_packed_git() lazy-initializator. Because of the last two, we
1600 * must ensure mutual exclusion between this call and the object reading
1601 * API, thus we use obj_read_lock() here.
1603 * TODO: allowing text conversion to run in parallel with object
1604 * reading operations might increase performance in the multithreaded
1605 * non-worktreee git-grep with --textconv.
1608 size
= fill_textconv(r
, driver
, df
, &buf
);
1613 * The normal fill_textconv usage by the diff machinery would just keep
1614 * the textconv'd buf separate from the diff_filespec. But much of the
1615 * grep code passes around a grep_source and assumes that its "buf"
1616 * pointer is the beginning of the thing we are searching. So let's
1617 * install our textconv'd version into the grep_source, taking care not
1618 * to leak any existing buffer.
1620 grep_source_clear_data(gs
);
1627 static int is_empty_line(const char *bol
, const char *eol
)
1629 while (bol
< eol
&& isspace(*bol
))
1634 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1637 char *peek_bol
= NULL
;
1640 unsigned last_hit
= 0;
1641 int binary_match_only
= 0;
1643 int try_lookahead
= 0;
1644 int show_function
= 0;
1645 struct userdiff_driver
*textconv
= NULL
;
1646 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1649 if (!opt
->status_only
&& gs
->name
== NULL
)
1650 BUG("grep call which could print a name requires "
1651 "grep_source.name be non-NULL");
1654 opt
->output
= std_output
;
1656 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1658 /* Show hunk marks, except for the first file. */
1659 if (opt
->last_shown
)
1660 opt
->show_hunk_mark
= 1;
1662 * If we're using threads then we can't easily identify
1663 * the first file. Always put hunk marks in that case
1664 * and skip the very first one later in work_done().
1666 if (opt
->output
!= std_output
)
1667 opt
->show_hunk_mark
= 1;
1669 opt
->last_shown
= 0;
1671 if (opt
->allow_textconv
) {
1672 grep_source_load_driver(gs
, opt
->repo
->index
);
1674 * We might set up the shared textconv cache data here, which
1675 * is not thread-safe. Also, get_oid_with_context() and
1676 * parse_object() might be internally called. As they are not
1677 * currently thread-safe and might be racy with object reading,
1678 * obj_read_lock() must be called.
1682 textconv
= userdiff_get_textconv(opt
->repo
, gs
->driver
);
1688 * We know the result of a textconv is text, so we only have to care
1689 * about binary handling if we are not using it.
1692 switch (opt
->binary
) {
1693 case GREP_BINARY_DEFAULT
:
1694 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1695 binary_match_only
= 1;
1697 case GREP_BINARY_NOMATCH
:
1698 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1699 return 0; /* Assume unmatch */
1701 case GREP_BINARY_TEXT
:
1704 BUG("unknown binary handling mode");
1708 memset(&xecfg
, 0, sizeof(xecfg
));
1711 try_lookahead
= should_lookahead(opt
);
1713 if (fill_textconv_grep(opt
->repo
, textconv
, gs
) < 0)
1722 ssize_t col
= -1, icol
= -1;
1725 * look_ahead() skips quickly to the line that possibly
1726 * has the next hit; don't call it if we need to do
1727 * something more than just skipping the current line
1728 * in response to an unmatch for the current line. E.g.
1729 * inside a post-context window, we will show the current
1730 * line as a context around the previous hit when it
1735 && (show_function
||
1736 lno
<= last_hit
+ opt
->post_context
))
1737 && look_ahead(opt
, &left
, &lno
, &bol
))
1739 eol
= end_of_line(bol
, &left
);
1743 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1744 ctx
= GREP_CONTEXT_BODY
;
1746 hit
= match_line(opt
, bol
, eol
, &col
, &icol
, ctx
, collect_hits
);
1752 /* "grep -v -e foo -e bla" should list lines
1753 * that do not have either, so inversion should
1758 if (opt
->unmatch_name_only
) {
1765 if (opt
->status_only
)
1767 if (opt
->name_only
) {
1768 show_name(opt
, gs
->name
);
1773 if (binary_match_only
) {
1774 opt
->output(opt
, "Binary file ", 12);
1775 output_color(opt
, gs
->name
, strlen(gs
->name
),
1776 opt
->colors
[GREP_COLOR_FILENAME
]);
1777 opt
->output(opt
, " matches\n", 9);
1780 /* Hit at this line. If we haven't shown the
1781 * pre-context lines, we would need to show them.
1783 if (opt
->pre_context
|| opt
->funcbody
)
1784 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1785 else if (opt
->funcname
)
1786 show_funcname_line(opt
, gs
, bol
, lno
);
1787 cno
= opt
->invert
? icol
: col
;
1790 * A negative cno indicates that there was no
1791 * match on the line. We are thus inverted and
1792 * being asked to show all lines that _don't_
1793 * match a given expression. Therefore, set cno
1794 * to 0 to suggest the whole line matches.
1798 show_line(opt
, bol
, eol
, gs
->name
, lno
, cno
+ 1, ':');
1804 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1805 unsigned long peek_left
= left
;
1806 char *peek_eol
= eol
;
1809 * Trailing empty lines are not interesting.
1810 * Peek past them to see if they belong to the
1811 * body of the current function.
1814 while (is_empty_line(peek_bol
, peek_eol
)) {
1815 peek_bol
= peek_eol
+ 1;
1816 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1819 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1822 if (show_function
||
1823 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1824 /* If the last hit is within the post context,
1825 * we need to show this line.
1827 show_line(opt
, bol
, eol
, gs
->name
, lno
, col
+ 1, '-');
1841 if (opt
->status_only
)
1842 return opt
->unmatch_name_only
;
1843 if (opt
->unmatch_name_only
) {
1844 /* We did not see any hit, so we want to show this */
1845 show_name(opt
, gs
->name
);
1849 xdiff_clear_find_func(&xecfg
);
1853 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1854 * which feels mostly useless but sometimes useful. Maybe
1855 * make it another option? For now suppress them.
1857 if (opt
->count
&& count
) {
1859 if (opt
->pathname
) {
1860 output_color(opt
, gs
->name
, strlen(gs
->name
),
1861 opt
->colors
[GREP_COLOR_FILENAME
]);
1862 output_sep(opt
, ':');
1864 xsnprintf(buf
, sizeof(buf
), "%u\n", count
);
1865 opt
->output(opt
, buf
, strlen(buf
));
1871 static void clr_hit_marker(struct grep_expr
*x
)
1873 /* All-hit markers are meaningful only at the very top level
1878 if (x
->node
!= GREP_NODE_OR
)
1880 x
->u
.binary
.left
->hit
= 0;
1881 x
= x
->u
.binary
.right
;
1885 static int chk_hit_marker(struct grep_expr
*x
)
1887 /* Top level nodes have hit markers. See if they all are hits */
1889 if (x
->node
!= GREP_NODE_OR
)
1891 if (!x
->u
.binary
.left
->hit
)
1893 x
= x
->u
.binary
.right
;
1897 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1900 * we do not have to do the two-pass grep when we do not check
1901 * buffer-wide "all-match".
1903 if (!opt
->all_match
)
1904 return grep_source_1(opt
, gs
, 0);
1906 /* Otherwise the toplevel "or" terms hit a bit differently.
1907 * We first clear hit markers from them.
1909 clr_hit_marker(opt
->pattern_expression
);
1910 grep_source_1(opt
, gs
, 1);
1912 if (!chk_hit_marker(opt
->pattern_expression
))
1915 return grep_source_1(opt
, gs
, 0);
1918 int grep_buffer(struct grep_opt
*opt
, char *buf
, unsigned long size
)
1920 struct grep_source gs
;
1923 grep_source_init(&gs
, GREP_SOURCE_BUF
, NULL
, NULL
, NULL
);
1927 r
= grep_source(opt
, &gs
);
1929 grep_source_clear(&gs
);
1933 void grep_source_init(struct grep_source
*gs
, enum grep_source_type type
,
1934 const char *name
, const char *path
,
1935 const void *identifier
)
1938 gs
->name
= xstrdup_or_null(name
);
1939 gs
->path
= xstrdup_or_null(path
);
1945 case GREP_SOURCE_FILE
:
1946 gs
->identifier
= xstrdup(identifier
);
1948 case GREP_SOURCE_OID
:
1949 gs
->identifier
= oiddup(identifier
);
1951 case GREP_SOURCE_BUF
:
1952 gs
->identifier
= NULL
;
1957 void grep_source_clear(struct grep_source
*gs
)
1959 FREE_AND_NULL(gs
->name
);
1960 FREE_AND_NULL(gs
->path
);
1961 FREE_AND_NULL(gs
->identifier
);
1962 grep_source_clear_data(gs
);
1965 void grep_source_clear_data(struct grep_source
*gs
)
1968 case GREP_SOURCE_FILE
:
1969 case GREP_SOURCE_OID
:
1970 FREE_AND_NULL(gs
->buf
);
1973 case GREP_SOURCE_BUF
:
1974 /* leave user-provided buf intact */
1979 static int grep_source_load_oid(struct grep_source
*gs
)
1981 enum object_type type
;
1983 gs
->buf
= read_object_file(gs
->identifier
, &type
, &gs
->size
);
1985 return error(_("'%s': unable to read %s"),
1987 oid_to_hex(gs
->identifier
));
1991 static int grep_source_load_file(struct grep_source
*gs
)
1993 const char *filename
= gs
->identifier
;
1999 if (lstat(filename
, &st
) < 0) {
2001 if (errno
!= ENOENT
)
2002 error_errno(_("failed to stat '%s'"), filename
);
2005 if (!S_ISREG(st
.st_mode
))
2007 size
= xsize_t(st
.st_size
);
2008 i
= open(filename
, O_RDONLY
);
2011 data
= xmallocz(size
);
2012 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
2013 error_errno(_("'%s': short read"), filename
);
2025 static int grep_source_load(struct grep_source
*gs
)
2031 case GREP_SOURCE_FILE
:
2032 return grep_source_load_file(gs
);
2033 case GREP_SOURCE_OID
:
2034 return grep_source_load_oid(gs
);
2035 case GREP_SOURCE_BUF
:
2036 return gs
->buf
? 0 : -1;
2038 BUG("invalid grep_source type to load");
2041 void grep_source_load_driver(struct grep_source
*gs
,
2042 struct index_state
*istate
)
2049 gs
->driver
= userdiff_find_by_path(istate
, gs
->path
);
2051 gs
->driver
= userdiff_find_by_name("default");
2055 static int grep_source_is_binary(struct grep_source
*gs
,
2056 struct index_state
*istate
)
2058 grep_source_load_driver(gs
, istate
);
2059 if (gs
->driver
->binary
!= -1)
2060 return gs
->driver
->binary
;
2062 if (!grep_source_load(gs
))
2063 return buffer_is_binary(gs
->buf
, gs
->size
);