1 #include "git-compat-util.h"
6 #include "object-store-ll.h"
9 #include "xdiff-interface.h"
16 static int grep_source_load(struct grep_source
*gs
);
17 static int grep_source_is_binary(struct grep_source
*gs
,
18 struct index_state
*istate
);
20 static void std_output(struct grep_opt
*opt UNUSED
, const void *buf
, size_t size
)
22 fwrite(buf
, size
, 1, stdout
);
25 static const char *color_grep_slots
[] = {
26 [GREP_COLOR_CONTEXT
] = "context",
27 [GREP_COLOR_FILENAME
] = "filename",
28 [GREP_COLOR_FUNCTION
] = "function",
29 [GREP_COLOR_LINENO
] = "lineNumber",
30 [GREP_COLOR_COLUMNNO
] = "column",
31 [GREP_COLOR_MATCH_CONTEXT
] = "matchContext",
32 [GREP_COLOR_MATCH_SELECTED
] = "matchSelected",
33 [GREP_COLOR_SELECTED
] = "selected",
34 [GREP_COLOR_SEP
] = "separator",
37 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
39 if (!strcmp(arg
, "default"))
40 return GREP_PATTERN_TYPE_UNSPECIFIED
;
41 else if (!strcmp(arg
, "basic"))
42 return GREP_PATTERN_TYPE_BRE
;
43 else if (!strcmp(arg
, "extended"))
44 return GREP_PATTERN_TYPE_ERE
;
45 else if (!strcmp(arg
, "fixed"))
46 return GREP_PATTERN_TYPE_FIXED
;
47 else if (!strcmp(arg
, "perl"))
48 return GREP_PATTERN_TYPE_PCRE
;
49 die("bad %s argument: %s", opt
, arg
);
52 define_list_config_array_extra(color_grep_slots
, {"match"});
55 * Read the configuration file once and store it in
56 * the grep_defaults template.
58 int grep_config(const char *var
, const char *value
,
59 const struct config_context
*ctx
, void *cb
)
61 struct grep_opt
*opt
= cb
;
64 if (userdiff_config(var
, value
) < 0)
67 if (!strcmp(var
, "grep.extendedregexp")) {
68 opt
->extended_regexp_option
= git_config_bool(var
, value
);
72 if (!strcmp(var
, "grep.patterntype")) {
73 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
77 if (!strcmp(var
, "grep.linenumber")) {
78 opt
->linenum
= git_config_bool(var
, value
);
81 if (!strcmp(var
, "grep.column")) {
82 opt
->columnnum
= git_config_bool(var
, value
);
86 if (!strcmp(var
, "grep.fullname")) {
87 opt
->relative
= !git_config_bool(var
, value
);
91 if (!strcmp(var
, "color.grep"))
92 opt
->color
= git_config_colorbool(var
, value
);
93 if (!strcmp(var
, "color.grep.match")) {
94 if (grep_config("color.grep.matchcontext", value
, ctx
, cb
) < 0)
96 if (grep_config("color.grep.matchselected", value
, ctx
, cb
) < 0)
98 } else if (skip_prefix(var
, "color.grep.", &slot
)) {
99 int i
= LOOKUP_CONFIG(color_grep_slots
, slot
);
104 color
= opt
->colors
[i
];
106 return config_error_nonbool(var
);
107 return color_parse(value
, color
);
112 void grep_init(struct grep_opt
*opt
, struct repository
*repo
)
114 struct grep_opt blank
= GREP_OPT_INIT
;
115 memcpy(opt
, &blank
, sizeof(*opt
));
118 opt
->pattern_tail
= &opt
->pattern_list
;
119 opt
->header_tail
= &opt
->header_list
;
122 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
123 const char *origin
, int no
,
124 enum grep_pat_token t
,
125 enum grep_header_field field
)
127 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
128 p
->pattern
= xmemdupz(pat
, patlen
);
129 p
->patternlen
= patlen
;
137 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
144 case GREP_PATTERN
: /* atom */
145 case GREP_PATTERN_HEAD
:
146 case GREP_PATTERN_BODY
:
148 struct grep_pat
*new_pat
;
150 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
151 while (++len
<= p
->patternlen
) {
152 if (*(--cp
) == '\n') {
159 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
160 p
->no
, p
->token
, p
->field
);
161 new_pat
->next
= p
->next
;
163 *tail
= &new_pat
->next
;
166 p
->patternlen
-= len
;
174 void append_header_grep_pattern(struct grep_opt
*opt
,
175 enum grep_header_field field
, const char *pat
)
177 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
178 GREP_PATTERN_HEAD
, field
);
179 if (field
== GREP_HEADER_REFLOG
)
180 opt
->use_reflog_filter
= 1;
181 do_append_grep_pat(&opt
->header_tail
, p
);
184 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
185 const char *origin
, int no
, enum grep_pat_token t
)
187 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
190 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
191 const char *origin
, int no
, enum grep_pat_token t
)
193 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
194 do_append_grep_pat(&opt
->pattern_tail
, p
);
197 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
199 struct grep_pat
*pat
;
200 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
203 ret
->pattern_list
= NULL
;
204 ret
->pattern_tail
= &ret
->pattern_list
;
206 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
208 if(pat
->token
== GREP_PATTERN_HEAD
)
209 append_header_grep_pattern(ret
, pat
->field
,
212 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
213 pat
->origin
, pat
->no
, pat
->token
);
219 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
225 xsnprintf(where
, sizeof(where
), "In '%s' at %d, ", p
->origin
, p
->no
);
227 xsnprintf(where
, sizeof(where
), "%s, ", p
->origin
);
231 die("%s'%s': %s", where
, p
->pattern
, error
);
234 static int is_fixed(const char *s
, size_t len
)
238 for (i
= 0; i
< len
; i
++) {
239 if (is_regex_special(s
[i
]))
247 #define GREP_PCRE2_DEBUG_MALLOC 0
249 static void *pcre2_malloc(PCRE2_SIZE size
, MAYBE_UNUSED
void *memory_data
)
251 void *pointer
= malloc(size
);
252 #if GREP_PCRE2_DEBUG_MALLOC
253 static int count
= 1;
254 fprintf(stderr
, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer
, count
++, size
);
259 static void pcre2_free(void *pointer
, MAYBE_UNUSED
void *memory_data
)
261 #if GREP_PCRE2_DEBUG_MALLOC
262 static int count
= 1;
264 fprintf(stderr
, "PCRE2:%p -> #%02d: free()\n", pointer
, count
++);
269 static int pcre2_jit_functional(void)
271 static int jit_working
= -1;
276 if (jit_working
!= -1)
280 * Try to JIT compile a simple pattern to probe if the JIT is
281 * working in general. It might fail for systems where creating
282 * memory mappings for runtime code generation is restricted.
284 code
= pcre2_compile((PCRE2_SPTR
)".", 1, 0, &err
, &off
, NULL
);
288 jit_working
= pcre2_jit_compile(code
, PCRE2_JIT_COMPLETE
) == 0;
289 pcre2_code_free(code
);
294 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
297 PCRE2_UCHAR errbuf
[256];
298 PCRE2_SIZE erroffset
;
299 int options
= PCRE2_MULTILINE
;
303 int literal
= !opt
->ignore_case
&& (p
->fixed
|| p
->is_fixed
);
306 * Call pcre2_general_context_create() before calling any
307 * other pcre2_*(). It sets up our malloc()/free() functions
308 * with which everything else is allocated.
310 p
->pcre2_general_context
= pcre2_general_context_create(
311 pcre2_malloc
, pcre2_free
, NULL
);
312 if (!p
->pcre2_general_context
)
313 die("Couldn't allocate PCRE2 general context");
315 if (opt
->ignore_case
) {
316 if (!opt
->ignore_locale
&& has_non_ascii(p
->pattern
)) {
317 p
->pcre2_tables
= pcre2_maketables(p
->pcre2_general_context
);
318 p
->pcre2_compile_context
= pcre2_compile_context_create(p
->pcre2_general_context
);
319 pcre2_set_character_tables(p
->pcre2_compile_context
,
322 options
|= PCRE2_CASELESS
;
324 if (!opt
->ignore_locale
&& is_utf8_locale() && !literal
)
325 options
|= (PCRE2_UTF
| PCRE2_UCP
| PCRE2_MATCH_INVALID_UTF
);
327 #ifndef GIT_PCRE2_VERSION_10_35_OR_HIGHER
329 * Work around a JIT bug related to invalid Unicode character handling
331 * https://github.com/PCRE2Project/pcre2/commit/c21bd977547d
333 options
&= ~PCRE2_UCP
;
336 #ifndef GIT_PCRE2_VERSION_10_36_OR_HIGHER
337 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
338 if (PCRE2_MATCH_INVALID_UTF
&& options
& (PCRE2_UTF
| PCRE2_CASELESS
))
339 options
|= PCRE2_NO_START_OPTIMIZE
;
342 p
->pcre2_pattern
= pcre2_compile((PCRE2_SPTR
)p
->pattern
,
343 p
->patternlen
, options
, &error
, &erroffset
,
344 p
->pcre2_compile_context
);
346 if (p
->pcre2_pattern
) {
347 p
->pcre2_match_data
= pcre2_match_data_create_from_pattern(p
->pcre2_pattern
, p
->pcre2_general_context
);
348 if (!p
->pcre2_match_data
)
349 die("Couldn't allocate PCRE2 match data");
351 pcre2_get_error_message(error
, errbuf
, sizeof(errbuf
));
352 compile_regexp_failed(p
, (const char *)&errbuf
);
355 pcre2_config(PCRE2_CONFIG_JIT
, &p
->pcre2_jit_on
);
356 if (p
->pcre2_jit_on
) {
357 jitret
= pcre2_jit_compile(p
->pcre2_pattern
, PCRE2_JIT_COMPLETE
);
358 if (jitret
== PCRE2_ERROR_NOMEMORY
&& !pcre2_jit_functional()) {
360 * Even though pcre2_config(PCRE2_CONFIG_JIT, ...)
361 * indicated JIT support, the library might still
362 * fail to generate JIT code for various reasons,
363 * e.g. when SELinux's 'deny_execmem' or PaX's
364 * MPROTECT prevent creating W|X memory mappings.
366 * Instead of faling hard, fall back to interpreter
367 * mode, just as if the pattern was prefixed with
373 int need_clip
= p
->patternlen
> 64;
374 int clip_len
= need_clip
? 64 : p
->patternlen
;
375 die("Couldn't JIT the PCRE2 pattern '%.*s'%s, got '%d'%s",
376 clip_len
, p
->pattern
, need_clip
? "..." : "", jitret
,
377 pcre2_jit_functional()
378 ? "\nPerhaps prefix (*NO_JIT) to your pattern?"
383 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
384 * tells us whether the library itself supports JIT,
385 * but to see whether we're going to be actually using
386 * JIT we need to extract PCRE2_INFO_JITSIZE from the
387 * pattern *after* we do pcre2_jit_compile() above.
389 * This is because if the pattern contains the
390 * (*NO_JIT) verb (see pcre2syntax(3))
391 * pcre2_jit_compile() will exit early with 0. If we
392 * then proceed to call pcre2_jit_match() further down
393 * the line instead of pcre2_match() we'll either
394 * segfault (pre PCRE 10.31) or run into a fatal error
397 patinforet
= pcre2_pattern_info(p
->pcre2_pattern
, PCRE2_INFO_JITSIZE
, &jitsizearg
);
399 BUG("pcre2_pattern_info() failed: %d", patinforet
);
400 if (jitsizearg
== 0) {
407 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
408 regmatch_t
*match
, int eflags
)
412 PCRE2_UCHAR errbuf
[256];
414 if (eflags
& REG_NOTBOL
)
415 flags
|= PCRE2_NOTBOL
;
418 ret
= pcre2_jit_match(p
->pcre2_pattern
, (unsigned char *)line
,
419 eol
- line
, 0, flags
, p
->pcre2_match_data
,
422 ret
= pcre2_match(p
->pcre2_pattern
, (unsigned char *)line
,
423 eol
- line
, 0, flags
, p
->pcre2_match_data
,
426 if (ret
< 0 && ret
!= PCRE2_ERROR_NOMATCH
) {
427 pcre2_get_error_message(ret
, errbuf
, sizeof(errbuf
));
428 die("%s failed with error code %d: %s",
429 (p
->pcre2_jit_on
? "pcre2_jit_match" : "pcre2_match"), ret
,
433 ovector
= pcre2_get_ovector_pointer(p
->pcre2_match_data
);
435 match
->rm_so
= (int)ovector
[0];
436 match
->rm_eo
= (int)ovector
[1];
442 static void free_pcre2_pattern(struct grep_pat
*p
)
444 pcre2_compile_context_free(p
->pcre2_compile_context
);
445 pcre2_code_free(p
->pcre2_pattern
);
446 pcre2_match_data_free(p
->pcre2_match_data
);
447 #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
448 pcre2_maketables_free(p
->pcre2_general_context
, p
->pcre2_tables
);
450 free((void *)p
->pcre2_tables
);
452 pcre2_general_context_free(p
->pcre2_general_context
);
454 #else /* !USE_LIBPCRE2 */
455 static void compile_pcre2_pattern(struct grep_pat
*p UNUSED
,
456 const struct grep_opt
*opt UNUSED
)
458 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
461 static int pcre2match(struct grep_pat
*p UNUSED
, const char *line UNUSED
,
462 const char *eol UNUSED
, regmatch_t
*match UNUSED
,
468 static void free_pcre2_pattern(struct grep_pat
*p UNUSED
)
472 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
474 struct strbuf sb
= STRBUF_INIT
;
478 basic_regex_quote_buf(&sb
, p
->pattern
);
479 if (opt
->ignore_case
)
480 regflags
|= REG_ICASE
;
481 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
485 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
486 compile_regexp_failed(p
, errbuf
);
489 #endif /* !USE_LIBPCRE2 */
491 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
494 int regflags
= REG_NEWLINE
;
496 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_UNSPECIFIED
)
497 opt
->pattern_type_option
= (opt
->extended_regexp_option
498 ? GREP_PATTERN_TYPE_ERE
499 : GREP_PATTERN_TYPE_BRE
);
501 p
->word_regexp
= opt
->word_regexp
;
502 p
->ignore_case
= opt
->ignore_case
;
503 p
->fixed
= opt
->pattern_type_option
== GREP_PATTERN_TYPE_FIXED
;
505 if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_PCRE
&&
506 memchr(p
->pattern
, 0, p
->patternlen
))
507 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
509 p
->is_fixed
= is_fixed(p
->pattern
, p
->patternlen
);
511 if (!p
->fixed
&& !p
->is_fixed
) {
512 const char *no_jit
= "(*NO_JIT)";
513 const int no_jit_len
= strlen(no_jit
);
514 if (starts_with(p
->pattern
, no_jit
) &&
515 is_fixed(p
->pattern
+ no_jit_len
,
516 p
->patternlen
- no_jit_len
))
520 if (p
->fixed
|| p
->is_fixed
) {
523 compile_pcre2_pattern(p
, opt
);
526 * E.g. t7811-grep-open.sh relies on the
527 * pattern being restored.
529 char *old_pattern
= p
->pattern
;
530 size_t old_patternlen
= p
->patternlen
;
531 struct strbuf sb
= STRBUF_INIT
;
534 * There is the PCRE2_LITERAL flag, but it's
535 * only in PCRE v2 10.30 and later. Needing to
536 * ifdef our way around that and dealing with
537 * it + PCRE2_MULTILINE being an error is more
538 * complex than just quoting this ourselves.
540 strbuf_add(&sb
, "\\Q", 2);
541 strbuf_add(&sb
, p
->pattern
, p
->patternlen
);
542 strbuf_add(&sb
, "\\E", 2);
545 p
->patternlen
= sb
.len
;
546 compile_pcre2_pattern(p
, opt
);
547 p
->pattern
= old_pattern
;
548 p
->patternlen
= old_patternlen
;
551 #else /* !USE_LIBPCRE2 */
552 compile_fixed_regexp(p
, opt
);
553 #endif /* !USE_LIBPCRE2 */
557 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_PCRE
) {
558 compile_pcre2_pattern(p
, opt
);
563 regflags
|= REG_ICASE
;
564 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_ERE
)
565 regflags
|= REG_EXTENDED
;
566 err
= regcomp(&p
->regexp
, p
->pattern
, regflags
);
569 regerror(err
, &p
->regexp
, errbuf
, 1024);
570 compile_regexp_failed(p
, errbuf
);
574 static struct grep_expr
*grep_not_expr(struct grep_expr
*expr
)
576 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
577 z
->node
= GREP_NODE_NOT
;
582 static struct grep_expr
*grep_binexp(enum grep_expr_node kind
,
583 struct grep_expr
*left
,
584 struct grep_expr
*right
)
586 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
588 z
->u
.binary
.left
= left
;
589 z
->u
.binary
.right
= right
;
593 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
595 return grep_binexp(GREP_NODE_OR
, left
, right
);
598 static struct grep_expr
*grep_and_expr(struct grep_expr
*left
, struct grep_expr
*right
)
600 return grep_binexp(GREP_NODE_AND
, left
, right
);
603 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
604 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
613 case GREP_PATTERN
: /* atom */
614 case GREP_PATTERN_HEAD
:
615 case GREP_PATTERN_BODY
:
617 x
->node
= GREP_NODE_ATOM
;
621 case GREP_OPEN_PAREN
:
623 x
= compile_pattern_or(list
);
624 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
625 die("unmatched parenthesis");
626 *list
= (*list
)->next
;
633 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
644 die("--not not followed by pattern expression");
646 x
= compile_pattern_not(list
);
648 die("--not followed by non pattern expression");
649 return grep_not_expr(x
);
651 return compile_pattern_atom(list
);
655 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
658 struct grep_expr
*x
, *y
;
660 x
= compile_pattern_not(list
);
662 if (p
&& p
->token
== GREP_AND
) {
664 die("--and not preceded by pattern expression");
666 die("--and not followed by pattern expression");
668 y
= compile_pattern_and(list
);
670 die("--and not followed by pattern expression");
671 return grep_and_expr(x
, y
);
676 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
679 struct grep_expr
*x
, *y
;
681 x
= compile_pattern_and(list
);
683 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
684 y
= compile_pattern_or(list
);
686 die("not a pattern expression %s", p
->pattern
);
687 return grep_or_expr(x
, y
);
692 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
694 return compile_pattern_or(list
);
697 static struct grep_expr
*grep_true_expr(void)
699 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
700 z
->node
= GREP_NODE_TRUE
;
704 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
707 struct grep_expr
*header_expr
;
708 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
709 enum grep_header_field fld
;
711 if (!opt
->header_list
)
714 for (p
= opt
->header_list
; p
; p
= p
->next
) {
715 if (p
->token
!= GREP_PATTERN_HEAD
)
716 BUG("a non-header pattern in grep header list.");
717 if (p
->field
< GREP_HEADER_FIELD_MIN
||
718 GREP_HEADER_FIELD_MAX
<= p
->field
)
719 BUG("unknown header field %d", p
->field
);
720 compile_regexp(p
, opt
);
723 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
724 header_group
[fld
] = NULL
;
726 for (p
= opt
->header_list
; p
; p
= p
->next
) {
728 struct grep_pat
*pp
= p
;
730 h
= compile_pattern_atom(&pp
);
731 if (!h
|| pp
!= p
->next
)
732 BUG("malformed header expr");
733 if (!header_group
[p
->field
]) {
734 header_group
[p
->field
] = h
;
737 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
742 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
743 if (!header_group
[fld
])
746 header_expr
= grep_true_expr();
747 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
752 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
754 struct grep_expr
*z
= x
;
757 assert(x
->node
== GREP_NODE_OR
);
758 if (x
->u
.binary
.right
&&
759 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
760 x
->u
.binary
.right
= y
;
763 x
= x
->u
.binary
.right
;
768 void compile_grep_patterns(struct grep_opt
*opt
)
771 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
774 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
776 case GREP_PATTERN
: /* atom */
777 case GREP_PATTERN_HEAD
:
778 case GREP_PATTERN_BODY
:
779 compile_regexp(p
, opt
);
787 if (opt
->all_match
|| opt
->no_body_match
|| header_expr
)
792 p
= opt
->pattern_list
;
794 opt
->pattern_expression
= compile_pattern_expr(&p
);
796 die("incomplete pattern expression: %s", p
->pattern
);
798 if (opt
->no_body_match
&& opt
->pattern_expression
)
799 opt
->pattern_expression
= grep_not_expr(opt
->pattern_expression
);
804 if (!opt
->pattern_expression
)
805 opt
->pattern_expression
= header_expr
;
806 else if (opt
->all_match
)
807 opt
->pattern_expression
= grep_splice_or(header_expr
,
808 opt
->pattern_expression
);
810 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
815 static void free_pattern_expr(struct grep_expr
*x
)
822 free_pattern_expr(x
->u
.unary
);
826 free_pattern_expr(x
->u
.binary
.left
);
827 free_pattern_expr(x
->u
.binary
.right
);
833 static void free_grep_pat(struct grep_pat
*pattern
)
835 struct grep_pat
*p
, *n
;
837 for (p
= pattern
; p
; p
= n
) {
840 case GREP_PATTERN
: /* atom */
841 case GREP_PATTERN_HEAD
:
842 case GREP_PATTERN_BODY
:
843 if (p
->pcre2_pattern
)
844 free_pcre2_pattern(p
);
856 void free_grep_patterns(struct grep_opt
*opt
)
858 free_grep_pat(opt
->pattern_list
);
859 free_grep_pat(opt
->header_list
);
861 if (opt
->pattern_expression
)
862 free_pattern_expr(opt
->pattern_expression
);
865 static const char *end_of_line(const char *cp
, unsigned long *left
)
867 unsigned long l
= *left
;
868 while (l
&& *cp
!= '\n') {
876 static int word_char(char ch
)
878 return isalnum(ch
) || ch
== '_';
881 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
884 if (want_color(opt
->color
) && color
&& color
[0]) {
885 opt
->output(opt
, color
, strlen(color
));
886 opt
->output(opt
, data
, size
);
887 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
889 opt
->output(opt
, data
, size
);
892 static void output_sep(struct grep_opt
*opt
, char sign
)
894 if (opt
->null_following_name
)
895 opt
->output(opt
, "\0", 1);
897 output_color(opt
, &sign
, 1, opt
->colors
[GREP_COLOR_SEP
]);
900 static void show_name(struct grep_opt
*opt
, const char *name
)
902 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
903 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
906 static int patmatch(struct grep_pat
*p
,
907 const char *line
, const char *eol
,
908 regmatch_t
*match
, int eflags
)
912 if (p
->pcre2_pattern
)
913 hit
= !pcre2match(p
, line
, eol
, match
, eflags
);
915 hit
= !regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
,
921 static void strip_timestamp(const char *bol
, const char **eol_p
)
923 const char *eol
= *eol_p
;
925 while (bol
< --eol
) {
938 { "committer ", 10 },
942 static int headerless_match_one_pattern(struct grep_pat
*p
,
943 const char *bol
, const char *eol
,
944 enum grep_context ctx
,
945 regmatch_t
*pmatch
, int eflags
)
948 const char *start
= bol
;
950 if ((p
->token
!= GREP_PATTERN
) &&
951 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
955 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
957 if (hit
&& p
->word_regexp
) {
958 if ((pmatch
[0].rm_so
< 0) ||
959 (eol
- bol
) < pmatch
[0].rm_so
||
960 (pmatch
[0].rm_eo
< 0) ||
961 (eol
- bol
) < pmatch
[0].rm_eo
)
962 die("regexp returned nonsense");
964 /* Match beginning must be either beginning of the
965 * line, or at word boundary (i.e. the last char must
966 * not be a word char). Similarly, match end must be
967 * either end of the line, or at word boundary
968 * (i.e. the next char must not be a word char).
970 if ( ((pmatch
[0].rm_so
== 0) ||
971 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
972 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
973 !word_char(bol
[pmatch
[0].rm_eo
])) )
978 /* Words consist of at least one character. */
979 if (pmatch
->rm_so
== pmatch
->rm_eo
)
982 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
983 /* There could be more than one match on the
984 * line, and the first match might not be
985 * strict word match. But later ones could be!
986 * Forward to the next possible start, i.e. the
987 * next position following a non-word char.
989 bol
= pmatch
[0].rm_so
+ bol
+ 1;
990 while (word_char(bol
[-1]) && bol
< eol
)
992 eflags
|= REG_NOTBOL
;
998 pmatch
[0].rm_so
+= bol
- start
;
999 pmatch
[0].rm_eo
+= bol
- start
;
1004 static int match_one_pattern(struct grep_pat
*p
,
1005 const char *bol
, const char *eol
,
1006 enum grep_context ctx
, regmatch_t
*pmatch
,
1012 if (p
->token
== GREP_PATTERN_HEAD
) {
1013 assert(p
->field
< ARRAY_SIZE(header_field
));
1014 field
= header_field
[p
->field
].field
;
1015 len
= header_field
[p
->field
].len
;
1016 if (strncmp(bol
, field
, len
))
1021 case GREP_HEADER_AUTHOR
:
1022 case GREP_HEADER_COMMITTER
:
1023 strip_timestamp(bol
, &eol
);
1030 return headerless_match_one_pattern(p
, bol
, eol
, ctx
, pmatch
, eflags
);
1034 static int match_expr_eval(struct grep_opt
*opt
, struct grep_expr
*x
,
1035 const char *bol
, const char *eol
,
1036 enum grep_context ctx
, ssize_t
*col
,
1037 ssize_t
*icol
, int collect_hits
)
1042 case GREP_NODE_TRUE
:
1045 case GREP_NODE_ATOM
:
1048 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
,
1050 if (h
&& (*col
< 0 || tmp
.rm_so
< *col
))
1053 if (x
->u
.atom
->token
== GREP_PATTERN_BODY
)
1058 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1060 h
= !match_expr_eval(opt
, x
->u
.unary
, bol
, eol
, ctx
, icol
, col
,
1064 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1066 if (h
|| opt
->columnnum
) {
1068 * Don't short-circuit AND when given --column, since a
1069 * NOT earlier in the tree may turn this into an OR. In
1070 * this case, see the below comment.
1072 h
&= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
,
1077 if (!(collect_hits
|| opt
->columnnum
)) {
1079 * Don't short-circuit OR when given --column (or
1080 * collecting hits) to ensure we don't skip a later
1081 * child that would produce an earlier match.
1083 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
,
1084 ctx
, col
, icol
, 0) ||
1085 match_expr_eval(opt
, x
->u
.binary
.right
, bol
,
1086 eol
, ctx
, col
, icol
, 0));
1088 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1091 x
->u
.binary
.left
->hit
|= h
;
1092 h
|= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
, col
,
1093 icol
, collect_hits
);
1096 die("Unexpected node type (internal error) %d", x
->node
);
1103 static int match_expr(struct grep_opt
*opt
,
1104 const char *bol
, const char *eol
,
1105 enum grep_context ctx
, ssize_t
*col
,
1106 ssize_t
*icol
, int collect_hits
)
1108 struct grep_expr
*x
= opt
->pattern_expression
;
1109 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, col
, icol
, collect_hits
);
1112 static int match_line(struct grep_opt
*opt
,
1113 const char *bol
, const char *eol
,
1114 ssize_t
*col
, ssize_t
*icol
,
1115 enum grep_context ctx
, int collect_hits
)
1120 if (opt
->pattern_expression
)
1121 return match_expr(opt
, bol
, eol
, ctx
, col
, icol
,
1124 /* we do not call with collect_hits without being extended */
1125 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1127 if (match_one_pattern(p
, bol
, eol
, ctx
, &tmp
, 0)) {
1129 if (!opt
->columnnum
) {
1131 * Without --column, any single match on a line
1132 * is enough to know that it needs to be
1133 * printed. With --column, scan _all_ patterns
1134 * to find the earliest.
1138 if (*col
< 0 || tmp
.rm_so
< *col
)
1145 static int match_next_pattern(struct grep_pat
*p
,
1146 const char *bol
, const char *eol
,
1147 enum grep_context ctx
,
1148 regmatch_t
*pmatch
, int eflags
)
1152 if (!headerless_match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1154 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1156 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1157 if (match
.rm_so
> pmatch
->rm_so
)
1159 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1162 pmatch
->rm_so
= match
.rm_so
;
1163 pmatch
->rm_eo
= match
.rm_eo
;
1167 int grep_next_match(struct grep_opt
*opt
,
1168 const char *bol
, const char *eol
,
1169 enum grep_context ctx
, regmatch_t
*pmatch
,
1170 enum grep_header_field field
, int eflags
)
1175 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1177 for (p
= ((ctx
== GREP_CONTEXT_HEAD
)
1178 ? opt
->header_list
: opt
->pattern_list
);
1181 case GREP_PATTERN_HEAD
:
1182 if ((field
!= GREP_HEADER_FIELD_MAX
) &&
1183 (p
->field
!= field
))
1186 case GREP_PATTERN
: /* atom */
1187 case GREP_PATTERN_BODY
:
1188 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1199 static void show_line_header(struct grep_opt
*opt
, const char *name
,
1200 unsigned lno
, ssize_t cno
, char sign
)
1202 if (opt
->heading
&& opt
->last_shown
== 0) {
1203 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1204 opt
->output(opt
, "\n", 1);
1206 opt
->last_shown
= lno
;
1208 if (!opt
->heading
&& opt
->pathname
) {
1209 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1210 output_sep(opt
, sign
);
1214 xsnprintf(buf
, sizeof(buf
), "%d", lno
);
1215 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_LINENO
]);
1216 output_sep(opt
, sign
);
1219 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1220 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1221 * being called with a context line.
1223 if (opt
->columnnum
&& cno
) {
1225 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, (uintmax_t)cno
);
1226 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_COLUMNNO
]);
1227 output_sep(opt
, sign
);
1231 static void show_line(struct grep_opt
*opt
,
1232 const char *bol
, const char *eol
,
1233 const char *name
, unsigned lno
, ssize_t cno
, char sign
)
1235 int rest
= eol
- bol
;
1236 const char *match_color
= NULL
;
1237 const char *line_color
= NULL
;
1239 if (opt
->file_break
&& opt
->last_shown
== 0) {
1240 if (opt
->show_hunk_mark
)
1241 opt
->output(opt
, "\n", 1);
1242 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1243 if (opt
->last_shown
== 0) {
1244 if (opt
->show_hunk_mark
) {
1245 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1246 opt
->output(opt
, "\n", 1);
1248 } else if (lno
> opt
->last_shown
+ 1) {
1249 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1250 opt
->output(opt
, "\n", 1);
1253 if (!opt
->only_matching
) {
1255 * In case the line we're being called with contains more than
1256 * one match, leave printing each header to the loop below.
1258 show_line_header(opt
, name
, lno
, cno
, sign
);
1260 if (opt
->color
|| opt
->only_matching
) {
1262 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1267 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
1269 match_color
= opt
->colors
[GREP_COLOR_MATCH_CONTEXT
];
1271 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
1272 else if (sign
== '-')
1273 line_color
= opt
->colors
[GREP_COLOR_CONTEXT
];
1274 else if (sign
== '=')
1275 line_color
= opt
->colors
[GREP_COLOR_FUNCTION
];
1277 while (grep_next_match(opt
, bol
, eol
, ctx
, &match
,
1278 GREP_HEADER_FIELD_MAX
, eflags
)) {
1279 if (match
.rm_so
== match
.rm_eo
)
1282 if (opt
->only_matching
)
1283 show_line_header(opt
, name
, lno
, cno
, sign
);
1285 output_color(opt
, bol
, match
.rm_so
, line_color
);
1286 output_color(opt
, bol
+ match
.rm_so
,
1287 match
.rm_eo
- match
.rm_so
, match_color
);
1288 if (opt
->only_matching
)
1289 opt
->output(opt
, "\n", 1);
1292 rest
-= match
.rm_eo
;
1293 eflags
= REG_NOTBOL
;
1296 if (!opt
->only_matching
) {
1297 output_color(opt
, bol
, rest
, line_color
);
1298 opt
->output(opt
, "\n", 1);
1305 * This lock protects access to the gitattributes machinery, which is
1308 pthread_mutex_t grep_attr_mutex
;
1310 static inline void grep_attr_lock(void)
1313 pthread_mutex_lock(&grep_attr_mutex
);
1316 static inline void grep_attr_unlock(void)
1319 pthread_mutex_unlock(&grep_attr_mutex
);
1322 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
,
1323 const char *bol
, const char *eol
)
1325 xdemitconf_t
*xecfg
= opt
->priv
;
1326 if (xecfg
&& !xecfg
->find_func
) {
1327 grep_source_load_driver(gs
, opt
->repo
->index
);
1328 if (gs
->driver
->funcname
.pattern
) {
1329 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1330 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1332 xecfg
= opt
->priv
= NULL
;
1338 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1339 xecfg
->find_func_priv
) >= 0;
1344 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1349 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1350 const char *bol
, unsigned lno
)
1352 while (bol
> gs
->buf
) {
1353 const char *eol
= --bol
;
1355 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1359 if (lno
<= opt
->last_shown
)
1362 if (match_funcname(opt
, gs
, bol
, eol
)) {
1363 show_line(opt
, bol
, eol
, gs
->name
, lno
, 0, '=');
1369 static int is_empty_line(const char *bol
, const char *eol
);
1371 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1372 const char *bol
, const char *end
, unsigned lno
)
1374 unsigned cur
= lno
, from
= 1, funcname_lno
= 0, orig_from
;
1375 int funcname_needed
= !!opt
->funcname
, comment_needed
= 0;
1377 if (opt
->pre_context
< lno
)
1378 from
= lno
- opt
->pre_context
;
1379 if (from
<= opt
->last_shown
)
1380 from
= opt
->last_shown
+ 1;
1382 if (opt
->funcbody
) {
1383 if (match_funcname(opt
, gs
, bol
, end
))
1386 funcname_needed
= 1;
1387 from
= opt
->last_shown
+ 1;
1391 while (bol
> gs
->buf
&& cur
> from
) {
1392 const char *next_bol
= bol
;
1393 const char *eol
= --bol
;
1395 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1398 if (comment_needed
&& (is_empty_line(bol
, eol
) ||
1399 match_funcname(opt
, gs
, bol
, eol
))) {
1408 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1410 funcname_needed
= 0;
1418 /* We need to look even further back to find a function signature. */
1419 if (opt
->funcname
&& funcname_needed
)
1420 show_funcname_line(opt
, gs
, bol
, cur
);
1424 const char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1426 while (*eol
!= '\n')
1428 show_line(opt
, bol
, eol
, gs
->name
, cur
, 0, sign
);
1434 static int should_lookahead(struct grep_opt
*opt
)
1438 if (opt
->pattern_expression
)
1439 return 0; /* punt for too complex stuff */
1442 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1443 if (p
->token
!= GREP_PATTERN
)
1444 return 0; /* punt for "header only" and stuff */
1449 static int look_ahead(struct grep_opt
*opt
,
1450 unsigned long *left_p
,
1454 unsigned lno
= *lno_p
;
1455 const char *bol
= *bol_p
;
1457 const char *sp
, *last_bol
;
1458 regoff_t earliest
= -1;
1460 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1464 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1465 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1467 if (earliest
< 0 || m
.rm_so
< earliest
)
1472 *bol_p
= bol
+ *left_p
;
1476 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1477 ; /* find the beginning of the line */
1480 for (sp
= bol
; sp
< last_bol
; sp
++) {
1484 *left_p
-= last_bol
- bol
;
1490 static int fill_textconv_grep(struct repository
*r
,
1491 struct userdiff_driver
*driver
,
1492 struct grep_source
*gs
)
1494 struct diff_filespec
*df
;
1498 if (!driver
|| !driver
->textconv
)
1499 return grep_source_load(gs
);
1502 * The textconv interface is intimately tied to diff_filespecs, so we
1503 * have to pretend to be one. If we could unify the grep_source
1504 * and diff_filespec structs, this mess could just go away.
1506 df
= alloc_filespec(gs
->path
);
1508 case GREP_SOURCE_OID
:
1509 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1511 case GREP_SOURCE_FILE
:
1512 fill_filespec(df
, null_oid(), 0, 0100644);
1515 BUG("attempt to textconv something without a path?");
1519 * fill_textconv is not remotely thread-safe; it modifies the global
1520 * diff tempfile structure, writes to the_repo's odb and might
1521 * internally call thread-unsafe functions such as the
1522 * prepare_packed_git() lazy-initializator. Because of the last two, we
1523 * must ensure mutual exclusion between this call and the object reading
1524 * API, thus we use obj_read_lock() here.
1526 * TODO: allowing text conversion to run in parallel with object
1527 * reading operations might increase performance in the multithreaded
1528 * non-worktreee git-grep with --textconv.
1531 size
= fill_textconv(r
, driver
, df
, &buf
);
1536 * The normal fill_textconv usage by the diff machinery would just keep
1537 * the textconv'd buf separate from the diff_filespec. But much of the
1538 * grep code passes around a grep_source and assumes that its "buf"
1539 * pointer is the beginning of the thing we are searching. So let's
1540 * install our textconv'd version into the grep_source, taking care not
1541 * to leak any existing buffer.
1543 grep_source_clear_data(gs
);
1550 static int is_empty_line(const char *bol
, const char *eol
)
1552 while (bol
< eol
&& isspace(*bol
))
1557 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1560 const char *peek_bol
= NULL
;
1563 unsigned last_hit
= 0;
1564 int binary_match_only
= 0;
1566 int try_lookahead
= 0;
1567 int show_function
= 0;
1568 struct userdiff_driver
*textconv
= NULL
;
1569 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1572 if (!opt
->status_only
&& gs
->name
== NULL
)
1573 BUG("grep call which could print a name requires "
1574 "grep_source.name be non-NULL");
1577 opt
->output
= std_output
;
1579 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1581 /* Show hunk marks, except for the first file. */
1582 if (opt
->last_shown
)
1583 opt
->show_hunk_mark
= 1;
1585 * If we're using threads then we can't easily identify
1586 * the first file. Always put hunk marks in that case
1587 * and skip the very first one later in work_done().
1589 if (opt
->output
!= std_output
)
1590 opt
->show_hunk_mark
= 1;
1592 opt
->last_shown
= 0;
1594 if (opt
->allow_textconv
) {
1595 grep_source_load_driver(gs
, opt
->repo
->index
);
1597 * We might set up the shared textconv cache data here, which
1598 * is not thread-safe. Also, get_oid_with_context() and
1599 * parse_object() might be internally called. As they are not
1600 * currently thread-safe and might be racy with object reading,
1601 * obj_read_lock() must be called.
1605 textconv
= userdiff_get_textconv(opt
->repo
, gs
->driver
);
1611 * We know the result of a textconv is text, so we only have to care
1612 * about binary handling if we are not using it.
1615 switch (opt
->binary
) {
1616 case GREP_BINARY_DEFAULT
:
1617 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1618 binary_match_only
= 1;
1620 case GREP_BINARY_NOMATCH
:
1621 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1622 return 0; /* Assume unmatch */
1624 case GREP_BINARY_TEXT
:
1627 BUG("unknown binary handling mode");
1631 memset(&xecfg
, 0, sizeof(xecfg
));
1634 try_lookahead
= should_lookahead(opt
);
1636 if (fill_textconv_grep(opt
->repo
, textconv
, gs
) < 0)
1645 ssize_t col
= -1, icol
= -1;
1648 * look_ahead() skips quickly to the line that possibly
1649 * has the next hit; don't call it if we need to do
1650 * something more than just skipping the current line
1651 * in response to an unmatch for the current line. E.g.
1652 * inside a post-context window, we will show the current
1653 * line as a context around the previous hit when it
1658 && (show_function
||
1659 lno
<= last_hit
+ opt
->post_context
))
1660 && look_ahead(opt
, &left
, &lno
, &bol
))
1662 eol
= end_of_line(bol
, &left
);
1664 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1665 ctx
= GREP_CONTEXT_BODY
;
1667 hit
= match_line(opt
, bol
, eol
, &col
, &icol
, ctx
, collect_hits
);
1672 /* "grep -v -e foo -e bla" should list lines
1673 * that do not have either, so inversion should
1678 if (opt
->unmatch_name_only
) {
1683 if (hit
&& (opt
->max_count
< 0 || count
< opt
->max_count
)) {
1685 if (opt
->status_only
)
1687 if (opt
->name_only
) {
1688 show_name(opt
, gs
->name
);
1693 if (binary_match_only
) {
1694 opt
->output(opt
, "Binary file ", 12);
1695 output_color(opt
, gs
->name
, strlen(gs
->name
),
1696 opt
->colors
[GREP_COLOR_FILENAME
]);
1697 opt
->output(opt
, " matches\n", 9);
1700 /* Hit at this line. If we haven't shown the
1701 * pre-context lines, we would need to show them.
1703 if (opt
->pre_context
|| opt
->funcbody
)
1704 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1705 else if (opt
->funcname
)
1706 show_funcname_line(opt
, gs
, bol
, lno
);
1707 cno
= opt
->invert
? icol
: col
;
1710 * A negative cno indicates that there was no
1711 * match on the line. We are thus inverted and
1712 * being asked to show all lines that _don't_
1713 * match a given expression. Therefore, set cno
1714 * to 0 to suggest the whole line matches.
1718 show_line(opt
, bol
, eol
, gs
->name
, lno
, cno
+ 1, ':');
1724 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1725 unsigned long peek_left
= left
;
1726 const char *peek_eol
= eol
;
1729 * Trailing empty lines are not interesting.
1730 * Peek past them to see if they belong to the
1731 * body of the current function.
1734 while (is_empty_line(peek_bol
, peek_eol
)) {
1735 peek_bol
= peek_eol
+ 1;
1736 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1739 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1742 if (show_function
||
1743 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1744 /* If the last hit is within the post context,
1745 * we need to show this line.
1747 show_line(opt
, bol
, eol
, gs
->name
, lno
, col
+ 1, '-');
1761 if (opt
->status_only
)
1762 return opt
->unmatch_name_only
;
1763 if (opt
->unmatch_name_only
) {
1764 /* We did not see any hit, so we want to show this */
1765 show_name(opt
, gs
->name
);
1769 xdiff_clear_find_func(&xecfg
);
1773 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1774 * which feels mostly useless but sometimes useful. Maybe
1775 * make it another option? For now suppress them.
1777 if (opt
->count
&& count
) {
1779 if (opt
->pathname
) {
1780 output_color(opt
, gs
->name
, strlen(gs
->name
),
1781 opt
->colors
[GREP_COLOR_FILENAME
]);
1782 output_sep(opt
, ':');
1784 xsnprintf(buf
, sizeof(buf
), "%u\n", count
);
1785 opt
->output(opt
, buf
, strlen(buf
));
1791 static void clr_hit_marker(struct grep_expr
*x
)
1793 /* All-hit markers are meaningful only at the very top level
1798 if (x
->node
!= GREP_NODE_OR
)
1800 x
->u
.binary
.left
->hit
= 0;
1801 x
= x
->u
.binary
.right
;
1805 static int chk_hit_marker(struct grep_expr
*x
)
1807 /* Top level nodes have hit markers. See if they all are hits */
1809 if (x
->node
!= GREP_NODE_OR
)
1811 if (!x
->u
.binary
.left
->hit
)
1813 x
= x
->u
.binary
.right
;
1817 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1820 * we do not have to do the two-pass grep when we do not check
1821 * buffer-wide "all-match".
1823 if (!opt
->all_match
&& !opt
->no_body_match
)
1824 return grep_source_1(opt
, gs
, 0);
1826 /* Otherwise the toplevel "or" terms hit a bit differently.
1827 * We first clear hit markers from them.
1829 clr_hit_marker(opt
->pattern_expression
);
1831 grep_source_1(opt
, gs
, 1);
1833 if (opt
->all_match
&& !chk_hit_marker(opt
->pattern_expression
))
1835 if (opt
->no_body_match
&& opt
->body_hit
)
1838 return grep_source_1(opt
, gs
, 0);
1841 static void grep_source_init_buf(struct grep_source
*gs
,
1845 gs
->type
= GREP_SOURCE_BUF
;
1851 gs
->identifier
= NULL
;
1854 int grep_buffer(struct grep_opt
*opt
, const char *buf
, unsigned long size
)
1856 struct grep_source gs
;
1859 grep_source_init_buf(&gs
, buf
, size
);
1861 r
= grep_source(opt
, &gs
);
1863 grep_source_clear(&gs
);
1867 void grep_source_init_file(struct grep_source
*gs
, const char *name
,
1870 gs
->type
= GREP_SOURCE_FILE
;
1871 gs
->name
= xstrdup_or_null(name
);
1872 gs
->path
= xstrdup_or_null(path
);
1876 gs
->identifier
= xstrdup(path
);
1879 void grep_source_init_oid(struct grep_source
*gs
, const char *name
,
1880 const char *path
, const struct object_id
*oid
,
1881 struct repository
*repo
)
1883 gs
->type
= GREP_SOURCE_OID
;
1884 gs
->name
= xstrdup_or_null(name
);
1885 gs
->path
= xstrdup_or_null(path
);
1889 gs
->identifier
= oiddup(oid
);
1893 void grep_source_clear(struct grep_source
*gs
)
1895 FREE_AND_NULL(gs
->name
);
1896 FREE_AND_NULL(gs
->path
);
1897 FREE_AND_NULL(gs
->identifier
);
1898 grep_source_clear_data(gs
);
1901 void grep_source_clear_data(struct grep_source
*gs
)
1904 case GREP_SOURCE_FILE
:
1905 case GREP_SOURCE_OID
:
1906 /* these types own the buffer */
1907 free((char *)gs
->buf
);
1911 case GREP_SOURCE_BUF
:
1912 /* leave user-provided buf intact */
1917 static int grep_source_load_oid(struct grep_source
*gs
)
1919 enum object_type type
;
1921 gs
->buf
= repo_read_object_file(gs
->repo
, gs
->identifier
, &type
,
1924 return error(_("'%s': unable to read %s"),
1926 oid_to_hex(gs
->identifier
));
1930 static int grep_source_load_file(struct grep_source
*gs
)
1932 const char *filename
= gs
->identifier
;
1938 if (lstat(filename
, &st
) < 0) {
1940 if (errno
!= ENOENT
)
1941 error_errno(_("failed to stat '%s'"), filename
);
1944 if (!S_ISREG(st
.st_mode
))
1946 size
= xsize_t(st
.st_size
);
1947 i
= open(filename
, O_RDONLY
);
1950 data
= xmallocz(size
);
1951 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1952 error_errno(_("'%s': short read"), filename
);
1964 static int grep_source_load(struct grep_source
*gs
)
1970 case GREP_SOURCE_FILE
:
1971 return grep_source_load_file(gs
);
1972 case GREP_SOURCE_OID
:
1973 return grep_source_load_oid(gs
);
1974 case GREP_SOURCE_BUF
:
1975 return gs
->buf
? 0 : -1;
1977 BUG("invalid grep_source type to load");
1980 void grep_source_load_driver(struct grep_source
*gs
,
1981 struct index_state
*istate
)
1988 gs
->driver
= userdiff_find_by_path(istate
, gs
->path
);
1990 gs
->driver
= userdiff_find_by_name("default");
1994 static int grep_source_is_binary(struct grep_source
*gs
,
1995 struct index_state
*istate
)
1997 grep_source_load_driver(gs
, istate
);
1998 if (gs
->driver
->binary
!= -1)
1999 return gs
->driver
->binary
;
2001 if (!grep_source_load(gs
))
2002 return buffer_is_binary(gs
->buf
, gs
->size
);