1 #include "git-compat-util.h"
6 #include "object-store-ll.h"
9 #include "xdiff-interface.h"
15 static int grep_source_load(struct grep_source
*gs
);
16 static int grep_source_is_binary(struct grep_source
*gs
,
17 struct index_state
*istate
);
19 static void std_output(struct grep_opt
*opt UNUSED
, const void *buf
, size_t size
)
21 fwrite(buf
, size
, 1, stdout
);
24 static const char *color_grep_slots
[] = {
25 [GREP_COLOR_CONTEXT
] = "context",
26 [GREP_COLOR_FILENAME
] = "filename",
27 [GREP_COLOR_FUNCTION
] = "function",
28 [GREP_COLOR_LINENO
] = "lineNumber",
29 [GREP_COLOR_COLUMNNO
] = "column",
30 [GREP_COLOR_MATCH_CONTEXT
] = "matchContext",
31 [GREP_COLOR_MATCH_SELECTED
] = "matchSelected",
32 [GREP_COLOR_SELECTED
] = "selected",
33 [GREP_COLOR_SEP
] = "separator",
36 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
38 if (!strcmp(arg
, "default"))
39 return GREP_PATTERN_TYPE_UNSPECIFIED
;
40 else if (!strcmp(arg
, "basic"))
41 return GREP_PATTERN_TYPE_BRE
;
42 else if (!strcmp(arg
, "extended"))
43 return GREP_PATTERN_TYPE_ERE
;
44 else if (!strcmp(arg
, "fixed"))
45 return GREP_PATTERN_TYPE_FIXED
;
46 else if (!strcmp(arg
, "perl"))
47 return GREP_PATTERN_TYPE_PCRE
;
48 die("bad %s argument: %s", opt
, arg
);
51 define_list_config_array_extra(color_grep_slots
, {"match"});
54 * Read the configuration file once and store it in
55 * the grep_defaults template.
57 int grep_config(const char *var
, const char *value
,
58 const struct config_context
*ctx
, void *cb
)
60 struct grep_opt
*opt
= cb
;
63 if (userdiff_config(var
, value
) < 0)
66 if (!strcmp(var
, "grep.extendedregexp")) {
67 opt
->extended_regexp_option
= git_config_bool(var
, value
);
71 if (!strcmp(var
, "grep.patterntype")) {
72 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
76 if (!strcmp(var
, "grep.linenumber")) {
77 opt
->linenum
= git_config_bool(var
, value
);
80 if (!strcmp(var
, "grep.column")) {
81 opt
->columnnum
= git_config_bool(var
, value
);
85 if (!strcmp(var
, "grep.fullname")) {
86 opt
->relative
= !git_config_bool(var
, value
);
90 if (!strcmp(var
, "color.grep"))
91 opt
->color
= git_config_colorbool(var
, value
);
92 if (!strcmp(var
, "color.grep.match")) {
93 if (grep_config("color.grep.matchcontext", value
, ctx
, cb
) < 0)
95 if (grep_config("color.grep.matchselected", value
, ctx
, cb
) < 0)
97 } else if (skip_prefix(var
, "color.grep.", &slot
)) {
98 int i
= LOOKUP_CONFIG(color_grep_slots
, slot
);
103 color
= opt
->colors
[i
];
105 return config_error_nonbool(var
);
106 return color_parse(value
, color
);
111 void grep_init(struct grep_opt
*opt
, struct repository
*repo
)
113 struct grep_opt blank
= GREP_OPT_INIT
;
114 memcpy(opt
, &blank
, sizeof(*opt
));
117 opt
->pattern_tail
= &opt
->pattern_list
;
118 opt
->header_tail
= &opt
->header_list
;
121 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
122 const char *origin
, int no
,
123 enum grep_pat_token t
,
124 enum grep_header_field field
)
126 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
127 p
->pattern
= xmemdupz(pat
, patlen
);
128 p
->patternlen
= patlen
;
136 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
143 case GREP_PATTERN
: /* atom */
144 case GREP_PATTERN_HEAD
:
145 case GREP_PATTERN_BODY
:
147 struct grep_pat
*new_pat
;
149 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
150 while (++len
<= p
->patternlen
) {
151 if (*(--cp
) == '\n') {
158 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
159 p
->no
, p
->token
, p
->field
);
160 new_pat
->next
= p
->next
;
162 *tail
= &new_pat
->next
;
165 p
->patternlen
-= len
;
173 void append_header_grep_pattern(struct grep_opt
*opt
,
174 enum grep_header_field field
, const char *pat
)
176 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
177 GREP_PATTERN_HEAD
, field
);
178 if (field
== GREP_HEADER_REFLOG
)
179 opt
->use_reflog_filter
= 1;
180 do_append_grep_pat(&opt
->header_tail
, p
);
183 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
184 const char *origin
, int no
, enum grep_pat_token t
)
186 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
189 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
190 const char *origin
, int no
, enum grep_pat_token t
)
192 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
193 do_append_grep_pat(&opt
->pattern_tail
, p
);
196 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
198 struct grep_pat
*pat
;
199 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
202 ret
->pattern_list
= NULL
;
203 ret
->pattern_tail
= &ret
->pattern_list
;
205 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
207 if(pat
->token
== GREP_PATTERN_HEAD
)
208 append_header_grep_pattern(ret
, pat
->field
,
211 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
212 pat
->origin
, pat
->no
, pat
->token
);
218 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
224 xsnprintf(where
, sizeof(where
), "In '%s' at %d, ", p
->origin
, p
->no
);
226 xsnprintf(where
, sizeof(where
), "%s, ", p
->origin
);
230 die("%s'%s': %s", where
, p
->pattern
, error
);
233 static int is_fixed(const char *s
, size_t len
)
237 for (i
= 0; i
< len
; i
++) {
238 if (is_regex_special(s
[i
]))
246 #define GREP_PCRE2_DEBUG_MALLOC 0
248 static void *pcre2_malloc(PCRE2_SIZE size
, void *memory_data UNUSED
)
250 void *pointer
= malloc(size
);
251 #if GREP_PCRE2_DEBUG_MALLOC
252 static int count
= 1;
253 fprintf(stderr
, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer
, count
++, size
);
258 static void pcre2_free(void *pointer
, void *memory_data UNUSED
)
260 #if GREP_PCRE2_DEBUG_MALLOC
261 static int count
= 1;
263 fprintf(stderr
, "PCRE2:%p -> #%02d: free()\n", pointer
, count
++);
268 static int pcre2_jit_functional(void)
270 static int jit_working
= -1;
275 if (jit_working
!= -1)
279 * Try to JIT compile a simple pattern to probe if the JIT is
280 * working in general. It might fail for systems where creating
281 * memory mappings for runtime code generation is restricted.
283 code
= pcre2_compile((PCRE2_SPTR
)".", 1, 0, &err
, &off
, NULL
);
287 jit_working
= pcre2_jit_compile(code
, PCRE2_JIT_COMPLETE
) == 0;
288 pcre2_code_free(code
);
293 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
296 PCRE2_UCHAR errbuf
[256];
297 PCRE2_SIZE erroffset
;
298 int options
= PCRE2_MULTILINE
;
302 int literal
= !opt
->ignore_case
&& (p
->fixed
|| p
->is_fixed
);
305 * Call pcre2_general_context_create() before calling any
306 * other pcre2_*(). It sets up our malloc()/free() functions
307 * with which everything else is allocated.
309 p
->pcre2_general_context
= pcre2_general_context_create(
310 pcre2_malloc
, pcre2_free
, NULL
);
311 if (!p
->pcre2_general_context
)
312 die("Couldn't allocate PCRE2 general context");
314 if (opt
->ignore_case
) {
315 if (!opt
->ignore_locale
&& has_non_ascii(p
->pattern
)) {
316 p
->pcre2_tables
= pcre2_maketables(p
->pcre2_general_context
);
317 p
->pcre2_compile_context
= pcre2_compile_context_create(p
->pcre2_general_context
);
318 pcre2_set_character_tables(p
->pcre2_compile_context
,
321 options
|= PCRE2_CASELESS
;
323 if (!opt
->ignore_locale
&& is_utf8_locale() && !literal
)
324 options
|= (PCRE2_UTF
| PCRE2_UCP
| PCRE2_MATCH_INVALID_UTF
);
326 #ifndef GIT_PCRE2_VERSION_10_35_OR_HIGHER
328 * Work around a JIT bug related to invalid Unicode character handling
330 * https://github.com/PCRE2Project/pcre2/commit/c21bd977547d
332 options
&= ~PCRE2_UCP
;
335 #ifndef GIT_PCRE2_VERSION_10_36_OR_HIGHER
336 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
337 if (PCRE2_MATCH_INVALID_UTF
&& options
& (PCRE2_UTF
| PCRE2_CASELESS
))
338 options
|= PCRE2_NO_START_OPTIMIZE
;
341 p
->pcre2_pattern
= pcre2_compile((PCRE2_SPTR
)p
->pattern
,
342 p
->patternlen
, options
, &error
, &erroffset
,
343 p
->pcre2_compile_context
);
345 if (p
->pcre2_pattern
) {
346 p
->pcre2_match_data
= pcre2_match_data_create_from_pattern(p
->pcre2_pattern
, p
->pcre2_general_context
);
347 if (!p
->pcre2_match_data
)
348 die("Couldn't allocate PCRE2 match data");
350 pcre2_get_error_message(error
, errbuf
, sizeof(errbuf
));
351 compile_regexp_failed(p
, (const char *)&errbuf
);
354 pcre2_config(PCRE2_CONFIG_JIT
, &p
->pcre2_jit_on
);
355 if (p
->pcre2_jit_on
) {
356 jitret
= pcre2_jit_compile(p
->pcre2_pattern
, PCRE2_JIT_COMPLETE
);
357 if (jitret
== PCRE2_ERROR_NOMEMORY
&& !pcre2_jit_functional()) {
359 * Even though pcre2_config(PCRE2_CONFIG_JIT, ...)
360 * indicated JIT support, the library might still
361 * fail to generate JIT code for various reasons,
362 * e.g. when SELinux's 'deny_execmem' or PaX's
363 * MPROTECT prevent creating W|X memory mappings.
365 * Instead of faling hard, fall back to interpreter
366 * mode, just as if the pattern was prefixed with
372 int need_clip
= p
->patternlen
> 64;
373 int clip_len
= need_clip
? 64 : p
->patternlen
;
374 die("Couldn't JIT the PCRE2 pattern '%.*s'%s, got '%d'%s",
375 clip_len
, p
->pattern
, need_clip
? "..." : "", jitret
,
376 pcre2_jit_functional()
377 ? "\nPerhaps prefix (*NO_JIT) to your pattern?"
382 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
383 * tells us whether the library itself supports JIT,
384 * but to see whether we're going to be actually using
385 * JIT we need to extract PCRE2_INFO_JITSIZE from the
386 * pattern *after* we do pcre2_jit_compile() above.
388 * This is because if the pattern contains the
389 * (*NO_JIT) verb (see pcre2syntax(3))
390 * pcre2_jit_compile() will exit early with 0. If we
391 * then proceed to call pcre2_jit_match() further down
392 * the line instead of pcre2_match() we'll either
393 * segfault (pre PCRE 10.31) or run into a fatal error
396 patinforet
= pcre2_pattern_info(p
->pcre2_pattern
, PCRE2_INFO_JITSIZE
, &jitsizearg
);
398 BUG("pcre2_pattern_info() failed: %d", patinforet
);
399 if (jitsizearg
== 0) {
406 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
407 regmatch_t
*match
, int eflags
)
411 PCRE2_UCHAR errbuf
[256];
413 if (eflags
& REG_NOTBOL
)
414 flags
|= PCRE2_NOTBOL
;
417 ret
= pcre2_jit_match(p
->pcre2_pattern
, (unsigned char *)line
,
418 eol
- line
, 0, flags
, p
->pcre2_match_data
,
421 ret
= pcre2_match(p
->pcre2_pattern
, (unsigned char *)line
,
422 eol
- line
, 0, flags
, p
->pcre2_match_data
,
425 if (ret
< 0 && ret
!= PCRE2_ERROR_NOMATCH
) {
426 pcre2_get_error_message(ret
, errbuf
, sizeof(errbuf
));
427 die("%s failed with error code %d: %s",
428 (p
->pcre2_jit_on
? "pcre2_jit_match" : "pcre2_match"), ret
,
432 ovector
= pcre2_get_ovector_pointer(p
->pcre2_match_data
);
434 match
->rm_so
= (int)ovector
[0];
435 match
->rm_eo
= (int)ovector
[1];
441 static void free_pcre2_pattern(struct grep_pat
*p
)
443 pcre2_compile_context_free(p
->pcre2_compile_context
);
444 pcre2_code_free(p
->pcre2_pattern
);
445 pcre2_match_data_free(p
->pcre2_match_data
);
446 #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
447 pcre2_maketables_free(p
->pcre2_general_context
, p
->pcre2_tables
);
449 free((void *)p
->pcre2_tables
);
451 pcre2_general_context_free(p
->pcre2_general_context
);
453 #else /* !USE_LIBPCRE2 */
454 static void compile_pcre2_pattern(struct grep_pat
*p UNUSED
,
455 const struct grep_opt
*opt UNUSED
)
457 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
460 static int pcre2match(struct grep_pat
*p UNUSED
, const char *line UNUSED
,
461 const char *eol UNUSED
, regmatch_t
*match UNUSED
,
467 static void free_pcre2_pattern(struct grep_pat
*p UNUSED
)
471 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
473 struct strbuf sb
= STRBUF_INIT
;
477 basic_regex_quote_buf(&sb
, p
->pattern
);
478 if (opt
->ignore_case
)
479 regflags
|= REG_ICASE
;
480 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
484 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
485 compile_regexp_failed(p
, errbuf
);
488 #endif /* !USE_LIBPCRE2 */
490 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
493 int regflags
= REG_NEWLINE
;
495 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_UNSPECIFIED
)
496 opt
->pattern_type_option
= (opt
->extended_regexp_option
497 ? GREP_PATTERN_TYPE_ERE
498 : GREP_PATTERN_TYPE_BRE
);
500 p
->word_regexp
= opt
->word_regexp
;
501 p
->ignore_case
= opt
->ignore_case
;
502 p
->fixed
= opt
->pattern_type_option
== GREP_PATTERN_TYPE_FIXED
;
504 if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_PCRE
&&
505 memchr(p
->pattern
, 0, p
->patternlen
))
506 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
508 p
->is_fixed
= is_fixed(p
->pattern
, p
->patternlen
);
510 if (!p
->fixed
&& !p
->is_fixed
) {
511 const char *no_jit
= "(*NO_JIT)";
512 const int no_jit_len
= strlen(no_jit
);
513 if (starts_with(p
->pattern
, no_jit
) &&
514 is_fixed(p
->pattern
+ no_jit_len
,
515 p
->patternlen
- no_jit_len
))
519 if (p
->fixed
|| p
->is_fixed
) {
522 compile_pcre2_pattern(p
, opt
);
525 * E.g. t7811-grep-open.sh relies on the
526 * pattern being restored.
528 char *old_pattern
= p
->pattern
;
529 size_t old_patternlen
= p
->patternlen
;
530 struct strbuf sb
= STRBUF_INIT
;
533 * There is the PCRE2_LITERAL flag, but it's
534 * only in PCRE v2 10.30 and later. Needing to
535 * ifdef our way around that and dealing with
536 * it + PCRE2_MULTILINE being an error is more
537 * complex than just quoting this ourselves.
539 strbuf_add(&sb
, "\\Q", 2);
540 strbuf_add(&sb
, p
->pattern
, p
->patternlen
);
541 strbuf_add(&sb
, "\\E", 2);
544 p
->patternlen
= sb
.len
;
545 compile_pcre2_pattern(p
, opt
);
546 p
->pattern
= old_pattern
;
547 p
->patternlen
= old_patternlen
;
550 #else /* !USE_LIBPCRE2 */
551 compile_fixed_regexp(p
, opt
);
552 #endif /* !USE_LIBPCRE2 */
556 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_PCRE
) {
557 compile_pcre2_pattern(p
, opt
);
562 regflags
|= REG_ICASE
;
563 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_ERE
)
564 regflags
|= REG_EXTENDED
;
565 err
= regcomp(&p
->regexp
, p
->pattern
, regflags
);
568 regerror(err
, &p
->regexp
, errbuf
, 1024);
569 compile_regexp_failed(p
, errbuf
);
573 static struct grep_expr
*grep_not_expr(struct grep_expr
*expr
)
575 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
576 z
->node
= GREP_NODE_NOT
;
581 static struct grep_expr
*grep_binexp(enum grep_expr_node kind
,
582 struct grep_expr
*left
,
583 struct grep_expr
*right
)
585 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
587 z
->u
.binary
.left
= left
;
588 z
->u
.binary
.right
= right
;
592 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
594 return grep_binexp(GREP_NODE_OR
, left
, right
);
597 static struct grep_expr
*grep_and_expr(struct grep_expr
*left
, struct grep_expr
*right
)
599 return grep_binexp(GREP_NODE_AND
, left
, right
);
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
:
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 ( for expression group");
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
= compile_pattern_not(list
);
647 die("--not followed by non pattern expression");
648 return grep_not_expr(x
);
650 return compile_pattern_atom(list
);
654 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
657 struct grep_expr
*x
, *y
;
659 x
= compile_pattern_not(list
);
661 if (p
&& p
->token
== GREP_AND
) {
663 die("--and not preceded by pattern expression");
665 die("--and not followed by pattern expression");
667 y
= compile_pattern_and(list
);
669 die("--and not followed by pattern expression");
670 return grep_and_expr(x
, y
);
675 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
678 struct grep_expr
*x
, *y
;
680 x
= compile_pattern_and(list
);
682 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
683 y
= compile_pattern_or(list
);
685 die("not a pattern expression %s", p
->pattern
);
686 return grep_or_expr(x
, y
);
691 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
693 return compile_pattern_or(list
);
696 static struct grep_expr
*grep_true_expr(void)
698 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
699 z
->node
= GREP_NODE_TRUE
;
703 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
706 struct grep_expr
*header_expr
;
707 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
708 enum grep_header_field fld
;
710 if (!opt
->header_list
)
713 for (p
= opt
->header_list
; p
; p
= p
->next
) {
714 if (p
->token
!= GREP_PATTERN_HEAD
)
715 BUG("a non-header pattern in grep header list.");
716 if (p
->field
< GREP_HEADER_FIELD_MIN
||
717 GREP_HEADER_FIELD_MAX
<= p
->field
)
718 BUG("unknown header field %d", p
->field
);
719 compile_regexp(p
, opt
);
722 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
723 header_group
[fld
] = NULL
;
725 for (p
= opt
->header_list
; p
; p
= p
->next
) {
727 struct grep_pat
*pp
= p
;
729 h
= compile_pattern_atom(&pp
);
730 if (!h
|| pp
!= p
->next
)
731 BUG("malformed header expr");
732 if (!header_group
[p
->field
]) {
733 header_group
[p
->field
] = h
;
736 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
741 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
742 if (!header_group
[fld
])
745 header_expr
= grep_true_expr();
746 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
751 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
753 struct grep_expr
*z
= x
;
756 assert(x
->node
== GREP_NODE_OR
);
757 if (x
->u
.binary
.right
&&
758 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
759 x
->u
.binary
.right
= y
;
762 x
= x
->u
.binary
.right
;
767 void compile_grep_patterns(struct grep_opt
*opt
)
770 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
773 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
775 case GREP_PATTERN
: /* atom */
776 case GREP_PATTERN_HEAD
:
777 case GREP_PATTERN_BODY
:
778 compile_regexp(p
, opt
);
786 if (opt
->all_match
|| opt
->no_body_match
|| header_expr
)
791 p
= opt
->pattern_list
;
793 opt
->pattern_expression
= compile_pattern_expr(&p
);
795 die("incomplete pattern expression group: %s", p
->pattern
);
797 if (opt
->no_body_match
&& opt
->pattern_expression
)
798 opt
->pattern_expression
= grep_not_expr(opt
->pattern_expression
);
803 if (!opt
->pattern_expression
)
804 opt
->pattern_expression
= header_expr
;
805 else if (opt
->all_match
)
806 opt
->pattern_expression
= grep_splice_or(header_expr
,
807 opt
->pattern_expression
);
809 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
814 static void free_pattern_expr(struct grep_expr
*x
)
821 free_pattern_expr(x
->u
.unary
);
825 free_pattern_expr(x
->u
.binary
.left
);
826 free_pattern_expr(x
->u
.binary
.right
);
832 static void free_grep_pat(struct grep_pat
*pattern
)
834 struct grep_pat
*p
, *n
;
836 for (p
= pattern
; p
; p
= n
) {
839 case GREP_PATTERN
: /* atom */
840 case GREP_PATTERN_HEAD
:
841 case GREP_PATTERN_BODY
:
842 if (p
->pcre2_pattern
)
843 free_pcre2_pattern(p
);
855 void free_grep_patterns(struct grep_opt
*opt
)
857 free_grep_pat(opt
->pattern_list
);
858 free_grep_pat(opt
->header_list
);
860 if (opt
->pattern_expression
)
861 free_pattern_expr(opt
->pattern_expression
);
864 static const char *end_of_line(const char *cp
, unsigned long *left
)
866 unsigned long l
= *left
;
867 while (l
&& *cp
!= '\n') {
875 static int word_char(char ch
)
877 return isalnum(ch
) || ch
== '_';
880 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
883 if (want_color(opt
->color
) && color
&& color
[0]) {
884 opt
->output(opt
, color
, strlen(color
));
885 opt
->output(opt
, data
, size
);
886 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
888 opt
->output(opt
, data
, size
);
891 static void output_sep(struct grep_opt
*opt
, char sign
)
893 if (opt
->null_following_name
)
894 opt
->output(opt
, "\0", 1);
896 output_color(opt
, &sign
, 1, opt
->colors
[GREP_COLOR_SEP
]);
899 static void show_name(struct grep_opt
*opt
, const char *name
)
901 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
902 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
905 static int patmatch(struct grep_pat
*p
,
906 const char *line
, const char *eol
,
907 regmatch_t
*match
, int eflags
)
909 if (p
->pcre2_pattern
)
910 return !pcre2match(p
, line
, eol
, match
, eflags
);
912 switch (regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
, eflags
)) {
922 static void strip_timestamp(const char *bol
, const char **eol_p
)
924 const char *eol
= *eol_p
;
926 while (bol
< --eol
) {
939 { "committer ", 10 },
943 static int headerless_match_one_pattern(struct grep_pat
*p
,
944 const char *bol
, const char *eol
,
945 enum grep_context ctx
,
946 regmatch_t
*pmatch
, int eflags
)
949 const char *start
= bol
;
951 if ((p
->token
!= GREP_PATTERN
) &&
952 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
956 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
960 if (hit
&& p
->word_regexp
) {
961 if ((pmatch
[0].rm_so
< 0) ||
962 (eol
- bol
) < pmatch
[0].rm_so
||
963 (pmatch
[0].rm_eo
< 0) ||
964 (eol
- bol
) < pmatch
[0].rm_eo
)
965 die("regexp returned nonsense");
967 /* Match beginning must be either beginning of the
968 * line, or at word boundary (i.e. the last char must
969 * not be a word char). Similarly, match end must be
970 * either end of the line, or at word boundary
971 * (i.e. the next char must not be a word char).
973 if ( ((pmatch
[0].rm_so
== 0) ||
974 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
975 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
976 !word_char(bol
[pmatch
[0].rm_eo
])) )
981 /* Words consist of at least one character. */
982 if (pmatch
->rm_so
== pmatch
->rm_eo
)
985 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
986 /* There could be more than one match on the
987 * line, and the first match might not be
988 * strict word match. But later ones could be!
989 * Forward to the next possible start, i.e. the
990 * next position following a non-word char.
992 bol
= pmatch
[0].rm_so
+ bol
+ 1;
993 while (word_char(bol
[-1]) && bol
< eol
)
995 eflags
|= REG_NOTBOL
;
1001 pmatch
[0].rm_so
+= bol
- start
;
1002 pmatch
[0].rm_eo
+= bol
- start
;
1007 static int match_one_pattern(struct grep_pat
*p
,
1008 const char *bol
, const char *eol
,
1009 enum grep_context ctx
, regmatch_t
*pmatch
,
1015 if (p
->token
== GREP_PATTERN_HEAD
) {
1016 assert(p
->field
< ARRAY_SIZE(header_field
));
1017 field
= header_field
[p
->field
].field
;
1018 len
= header_field
[p
->field
].len
;
1019 if (strncmp(bol
, field
, len
))
1024 case GREP_HEADER_AUTHOR
:
1025 case GREP_HEADER_COMMITTER
:
1026 strip_timestamp(bol
, &eol
);
1033 return headerless_match_one_pattern(p
, bol
, eol
, ctx
, pmatch
, eflags
);
1037 static int match_expr_eval(struct grep_opt
*opt
, struct grep_expr
*x
,
1038 const char *bol
, const char *eol
,
1039 enum grep_context ctx
, ssize_t
*col
,
1040 ssize_t
*icol
, int collect_hits
)
1045 case GREP_NODE_TRUE
:
1048 case GREP_NODE_ATOM
:
1051 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
,
1053 if (h
&& (*col
< 0 || tmp
.rm_so
< *col
))
1056 if (x
->u
.atom
->token
== GREP_PATTERN_BODY
)
1061 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1063 h
= !match_expr_eval(opt
, x
->u
.unary
, bol
, eol
, ctx
, icol
, col
,
1067 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1069 if (h
|| opt
->columnnum
) {
1071 * Don't short-circuit AND when given --column, since a
1072 * NOT earlier in the tree may turn this into an OR. In
1073 * this case, see the below comment.
1075 h
&= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
,
1080 if (!(collect_hits
|| opt
->columnnum
)) {
1082 * Don't short-circuit OR when given --column (or
1083 * collecting hits) to ensure we don't skip a later
1084 * child that would produce an earlier match.
1086 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
,
1087 ctx
, col
, icol
, 0) ||
1088 match_expr_eval(opt
, x
->u
.binary
.right
, bol
,
1089 eol
, ctx
, col
, icol
, 0));
1091 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1094 x
->u
.binary
.left
->hit
|= h
;
1095 h
|= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
, col
,
1096 icol
, collect_hits
);
1099 die("Unexpected node type (internal error) %d", x
->node
);
1106 static int match_expr(struct grep_opt
*opt
,
1107 const char *bol
, const char *eol
,
1108 enum grep_context ctx
, ssize_t
*col
,
1109 ssize_t
*icol
, int collect_hits
)
1111 struct grep_expr
*x
= opt
->pattern_expression
;
1112 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, col
, icol
, collect_hits
);
1115 static int match_line(struct grep_opt
*opt
,
1116 const char *bol
, const char *eol
,
1117 ssize_t
*col
, ssize_t
*icol
,
1118 enum grep_context ctx
, int collect_hits
)
1123 if (opt
->pattern_expression
)
1124 return match_expr(opt
, bol
, eol
, ctx
, col
, icol
,
1127 /* we do not call with collect_hits without being extended */
1128 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1130 if (match_one_pattern(p
, bol
, eol
, ctx
, &tmp
, 0)) {
1132 if (!opt
->columnnum
) {
1134 * Without --column, any single match on a line
1135 * is enough to know that it needs to be
1136 * printed. With --column, scan _all_ patterns
1137 * to find the earliest.
1141 if (*col
< 0 || tmp
.rm_so
< *col
)
1148 static int match_next_pattern(struct grep_pat
*p
,
1149 const char *bol
, const char *eol
,
1150 enum grep_context ctx
,
1151 regmatch_t
*pmatch
, int eflags
)
1155 if (!headerless_match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1157 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1159 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1160 if (match
.rm_so
> pmatch
->rm_so
)
1162 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1165 pmatch
->rm_so
= match
.rm_so
;
1166 pmatch
->rm_eo
= match
.rm_eo
;
1170 int grep_next_match(struct grep_opt
*opt
,
1171 const char *bol
, const char *eol
,
1172 enum grep_context ctx
, regmatch_t
*pmatch
,
1173 enum grep_header_field field
, int eflags
)
1178 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1180 for (p
= ((ctx
== GREP_CONTEXT_HEAD
)
1181 ? opt
->header_list
: opt
->pattern_list
);
1184 case GREP_PATTERN_HEAD
:
1185 if ((field
!= GREP_HEADER_FIELD_MAX
) &&
1186 (p
->field
!= field
))
1189 case GREP_PATTERN
: /* atom */
1190 case GREP_PATTERN_BODY
:
1191 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1202 static void show_line_header(struct grep_opt
*opt
, const char *name
,
1203 unsigned lno
, ssize_t cno
, char sign
)
1205 if (opt
->heading
&& opt
->last_shown
== 0) {
1206 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1207 opt
->output(opt
, "\n", 1);
1209 opt
->last_shown
= lno
;
1211 if (!opt
->heading
&& opt
->pathname
) {
1212 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1213 output_sep(opt
, sign
);
1217 xsnprintf(buf
, sizeof(buf
), "%d", lno
);
1218 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_LINENO
]);
1219 output_sep(opt
, sign
);
1222 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1223 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1224 * being called with a context line.
1226 if (opt
->columnnum
&& cno
) {
1228 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, (uintmax_t)cno
);
1229 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_COLUMNNO
]);
1230 output_sep(opt
, sign
);
1234 static void show_line(struct grep_opt
*opt
,
1235 const char *bol
, const char *eol
,
1236 const char *name
, unsigned lno
, ssize_t cno
, char sign
)
1238 int rest
= eol
- bol
;
1239 const char *match_color
= NULL
;
1240 const char *line_color
= NULL
;
1242 if (opt
->file_break
&& opt
->last_shown
== 0) {
1243 if (opt
->show_hunk_mark
)
1244 opt
->output(opt
, "\n", 1);
1245 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1246 if (opt
->last_shown
== 0) {
1247 if (opt
->show_hunk_mark
) {
1248 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1249 opt
->output(opt
, "\n", 1);
1251 } else if (lno
> opt
->last_shown
+ 1) {
1252 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1253 opt
->output(opt
, "\n", 1);
1256 if (!opt
->only_matching
) {
1258 * In case the line we're being called with contains more than
1259 * one match, leave printing each header to the loop below.
1261 show_line_header(opt
, name
, lno
, cno
, sign
);
1263 if (opt
->color
|| opt
->only_matching
) {
1265 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1270 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
1272 match_color
= opt
->colors
[GREP_COLOR_MATCH_CONTEXT
];
1274 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
1275 else if (sign
== '-')
1276 line_color
= opt
->colors
[GREP_COLOR_CONTEXT
];
1277 else if (sign
== '=')
1278 line_color
= opt
->colors
[GREP_COLOR_FUNCTION
];
1280 while (grep_next_match(opt
, bol
, eol
, ctx
, &match
,
1281 GREP_HEADER_FIELD_MAX
, eflags
)) {
1282 if (match
.rm_so
== match
.rm_eo
)
1285 if (opt
->only_matching
)
1286 show_line_header(opt
, name
, lno
, cno
, sign
);
1288 output_color(opt
, bol
, match
.rm_so
, line_color
);
1289 output_color(opt
, bol
+ match
.rm_so
,
1290 match
.rm_eo
- match
.rm_so
, match_color
);
1291 if (opt
->only_matching
)
1292 opt
->output(opt
, "\n", 1);
1295 rest
-= match
.rm_eo
;
1296 eflags
= REG_NOTBOL
;
1299 if (!opt
->only_matching
) {
1300 output_color(opt
, bol
, rest
, line_color
);
1301 opt
->output(opt
, "\n", 1);
1308 * This lock protects access to the gitattributes machinery, which is
1311 pthread_mutex_t grep_attr_mutex
;
1313 static inline void grep_attr_lock(void)
1316 pthread_mutex_lock(&grep_attr_mutex
);
1319 static inline void grep_attr_unlock(void)
1322 pthread_mutex_unlock(&grep_attr_mutex
);
1325 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
,
1326 const char *bol
, const char *eol
)
1328 xdemitconf_t
*xecfg
= opt
->priv
;
1329 if (xecfg
&& !xecfg
->find_func
) {
1330 grep_source_load_driver(gs
, opt
->repo
->index
);
1331 if (gs
->driver
->funcname
.pattern
) {
1332 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1333 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1335 xecfg
= opt
->priv
= NULL
;
1341 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1342 xecfg
->find_func_priv
) >= 0;
1347 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1352 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1353 const char *bol
, unsigned lno
)
1355 while (bol
> gs
->buf
) {
1356 const char *eol
= --bol
;
1358 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1362 if (lno
<= opt
->last_shown
)
1365 if (match_funcname(opt
, gs
, bol
, eol
)) {
1366 show_line(opt
, bol
, eol
, gs
->name
, lno
, 0, '=');
1372 static int is_empty_line(const char *bol
, const char *eol
);
1374 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1375 const char *bol
, const char *end
, unsigned lno
)
1377 unsigned cur
= lno
, from
= 1, funcname_lno
= 0, orig_from
;
1378 int funcname_needed
= !!opt
->funcname
, comment_needed
= 0;
1380 if (opt
->pre_context
< lno
)
1381 from
= lno
- opt
->pre_context
;
1382 if (from
<= opt
->last_shown
)
1383 from
= opt
->last_shown
+ 1;
1385 if (opt
->funcbody
) {
1386 if (match_funcname(opt
, gs
, bol
, end
))
1389 funcname_needed
= 1;
1390 from
= opt
->last_shown
+ 1;
1394 while (bol
> gs
->buf
&& cur
> from
) {
1395 const char *next_bol
= bol
;
1396 const char *eol
= --bol
;
1398 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1401 if (comment_needed
&& (is_empty_line(bol
, eol
) ||
1402 match_funcname(opt
, gs
, bol
, eol
))) {
1411 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1413 funcname_needed
= 0;
1421 /* We need to look even further back to find a function signature. */
1422 if (opt
->funcname
&& funcname_needed
)
1423 show_funcname_line(opt
, gs
, bol
, cur
);
1427 const char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1429 while (*eol
!= '\n')
1431 show_line(opt
, bol
, eol
, gs
->name
, cur
, 0, sign
);
1437 static int should_lookahead(struct grep_opt
*opt
)
1441 if (opt
->pattern_expression
)
1442 return 0; /* punt for too complex stuff */
1445 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1446 if (p
->token
!= GREP_PATTERN
)
1447 return 0; /* punt for "header only" and stuff */
1452 static int look_ahead(struct grep_opt
*opt
,
1453 unsigned long *left_p
,
1457 unsigned lno
= *lno_p
;
1458 const char *bol
= *bol_p
;
1460 const char *sp
, *last_bol
;
1461 regoff_t earliest
= -1;
1463 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1467 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1470 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1472 if (earliest
< 0 || m
.rm_so
< earliest
)
1477 *bol_p
= bol
+ *left_p
;
1481 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1482 ; /* find the beginning of the line */
1485 for (sp
= bol
; sp
< last_bol
; sp
++) {
1489 *left_p
-= last_bol
- bol
;
1495 static int fill_textconv_grep(struct repository
*r
,
1496 struct userdiff_driver
*driver
,
1497 struct grep_source
*gs
)
1499 struct diff_filespec
*df
;
1503 if (!driver
|| !driver
->textconv
)
1504 return grep_source_load(gs
);
1507 * The textconv interface is intimately tied to diff_filespecs, so we
1508 * have to pretend to be one. If we could unify the grep_source
1509 * and diff_filespec structs, this mess could just go away.
1511 df
= alloc_filespec(gs
->path
);
1513 case GREP_SOURCE_OID
:
1514 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1516 case GREP_SOURCE_FILE
:
1517 fill_filespec(df
, null_oid(), 0, 0100644);
1520 BUG("attempt to textconv something without a path?");
1524 * fill_textconv is not remotely thread-safe; it modifies the global
1525 * diff tempfile structure, writes to the_repo's odb and might
1526 * internally call thread-unsafe functions such as the
1527 * prepare_packed_git() lazy-initializator. Because of the last two, we
1528 * must ensure mutual exclusion between this call and the object reading
1529 * API, thus we use obj_read_lock() here.
1531 * TODO: allowing text conversion to run in parallel with object
1532 * reading operations might increase performance in the multithreaded
1533 * non-worktreee git-grep with --textconv.
1536 size
= fill_textconv(r
, driver
, df
, &buf
);
1541 * The normal fill_textconv usage by the diff machinery would just keep
1542 * the textconv'd buf separate from the diff_filespec. But much of the
1543 * grep code passes around a grep_source and assumes that its "buf"
1544 * pointer is the beginning of the thing we are searching. So let's
1545 * install our textconv'd version into the grep_source, taking care not
1546 * to leak any existing buffer.
1548 grep_source_clear_data(gs
);
1555 static int is_empty_line(const char *bol
, const char *eol
)
1557 while (bol
< eol
&& isspace(*bol
))
1562 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1565 const char *peek_bol
= NULL
;
1568 unsigned last_hit
= 0;
1569 int binary_match_only
= 0;
1571 int try_lookahead
= 0;
1572 int show_function
= 0;
1573 struct userdiff_driver
*textconv
= NULL
;
1574 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1577 if (!opt
->status_only
&& gs
->name
== NULL
)
1578 BUG("grep call which could print a name requires "
1579 "grep_source.name be non-NULL");
1582 opt
->output
= std_output
;
1584 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1586 /* Show hunk marks, except for the first file. */
1587 if (opt
->last_shown
)
1588 opt
->show_hunk_mark
= 1;
1590 * If we're using threads then we can't easily identify
1591 * the first file. Always put hunk marks in that case
1592 * and skip the very first one later in work_done().
1594 if (opt
->output
!= std_output
)
1595 opt
->show_hunk_mark
= 1;
1597 opt
->last_shown
= 0;
1599 if (opt
->allow_textconv
) {
1600 grep_source_load_driver(gs
, opt
->repo
->index
);
1602 * We might set up the shared textconv cache data here, which
1603 * is not thread-safe. Also, get_oid_with_context() and
1604 * parse_object() might be internally called. As they are not
1605 * currently thread-safe and might be racy with object reading,
1606 * obj_read_lock() must be called.
1610 textconv
= userdiff_get_textconv(opt
->repo
, gs
->driver
);
1616 * We know the result of a textconv is text, so we only have to care
1617 * about binary handling if we are not using it.
1620 switch (opt
->binary
) {
1621 case GREP_BINARY_DEFAULT
:
1622 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1623 binary_match_only
= 1;
1625 case GREP_BINARY_NOMATCH
:
1626 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1627 return 0; /* Assume unmatch */
1629 case GREP_BINARY_TEXT
:
1632 BUG("unknown binary handling mode");
1636 memset(&xecfg
, 0, sizeof(xecfg
));
1639 try_lookahead
= should_lookahead(opt
);
1641 if (fill_textconv_grep(opt
->repo
, textconv
, gs
) < 0)
1650 ssize_t col
= -1, icol
= -1;
1653 * look_ahead() skips quickly to the line that possibly
1654 * has the next hit; don't call it if we need to do
1655 * something more than just skipping the current line
1656 * in response to an unmatch for the current line. E.g.
1657 * inside a post-context window, we will show the current
1658 * line as a context around the previous hit when it
1663 && (show_function
||
1664 lno
<= last_hit
+ opt
->post_context
))) {
1665 hit
= look_ahead(opt
, &left
, &lno
, &bol
);
1671 eol
= end_of_line(bol
, &left
);
1673 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1674 ctx
= GREP_CONTEXT_BODY
;
1676 hit
= match_line(opt
, bol
, eol
, &col
, &icol
, ctx
, collect_hits
);
1681 /* "grep -v -e foo -e bla" should list lines
1682 * that do not have either, so inversion should
1687 if (opt
->unmatch_name_only
) {
1692 if (hit
&& (opt
->max_count
< 0 || count
< opt
->max_count
)) {
1694 if (opt
->status_only
)
1696 if (opt
->name_only
) {
1697 show_name(opt
, gs
->name
);
1702 if (binary_match_only
) {
1703 opt
->output(opt
, "Binary file ", 12);
1704 output_color(opt
, gs
->name
, strlen(gs
->name
),
1705 opt
->colors
[GREP_COLOR_FILENAME
]);
1706 opt
->output(opt
, " matches\n", 9);
1709 /* Hit at this line. If we haven't shown the
1710 * pre-context lines, we would need to show them.
1712 if (opt
->pre_context
|| opt
->funcbody
)
1713 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1714 else if (opt
->funcname
)
1715 show_funcname_line(opt
, gs
, bol
, lno
);
1716 cno
= opt
->invert
? icol
: col
;
1719 * A negative cno indicates that there was no
1720 * match on the line. We are thus inverted and
1721 * being asked to show all lines that _don't_
1722 * match a given expression. Therefore, set cno
1723 * to 0 to suggest the whole line matches.
1727 show_line(opt
, bol
, eol
, gs
->name
, lno
, cno
+ 1, ':');
1733 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1734 unsigned long peek_left
= left
;
1735 const char *peek_eol
= eol
;
1738 * Trailing empty lines are not interesting.
1739 * Peek past them to see if they belong to the
1740 * body of the current function.
1743 while (is_empty_line(peek_bol
, peek_eol
)) {
1744 peek_bol
= peek_eol
+ 1;
1745 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1748 if (peek_bol
>= gs
->buf
+ gs
->size
||
1749 match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1752 if (show_function
||
1753 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1754 /* If the last hit is within the post context,
1755 * we need to show this line.
1757 show_line(opt
, bol
, eol
, gs
->name
, lno
, col
+ 1, '-');
1771 if (opt
->status_only
)
1772 return opt
->unmatch_name_only
;
1773 if (opt
->unmatch_name_only
) {
1774 /* We did not see any hit, so we want to show this */
1775 show_name(opt
, gs
->name
);
1779 xdiff_clear_find_func(&xecfg
);
1783 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1784 * which feels mostly useless but sometimes useful. Maybe
1785 * make it another option? For now suppress them.
1787 if (opt
->count
&& count
) {
1789 if (opt
->pathname
) {
1790 output_color(opt
, gs
->name
, strlen(gs
->name
),
1791 opt
->colors
[GREP_COLOR_FILENAME
]);
1792 output_sep(opt
, ':');
1794 xsnprintf(buf
, sizeof(buf
), "%u\n", count
);
1795 opt
->output(opt
, buf
, strlen(buf
));
1801 static void clr_hit_marker(struct grep_expr
*x
)
1803 /* All-hit markers are meaningful only at the very top level
1808 if (x
->node
!= GREP_NODE_OR
)
1810 x
->u
.binary
.left
->hit
= 0;
1811 x
= x
->u
.binary
.right
;
1815 static int chk_hit_marker(struct grep_expr
*x
)
1817 /* Top level nodes have hit markers. See if they all are hits */
1819 if (x
->node
!= GREP_NODE_OR
)
1821 if (!x
->u
.binary
.left
->hit
)
1823 x
= x
->u
.binary
.right
;
1827 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1830 * we do not have to do the two-pass grep when we do not check
1831 * buffer-wide "all-match".
1833 if (!opt
->all_match
&& !opt
->no_body_match
)
1834 return grep_source_1(opt
, gs
, 0);
1836 /* Otherwise the toplevel "or" terms hit a bit differently.
1837 * We first clear hit markers from them.
1839 clr_hit_marker(opt
->pattern_expression
);
1841 grep_source_1(opt
, gs
, 1);
1843 if (opt
->all_match
&& !chk_hit_marker(opt
->pattern_expression
))
1845 if (opt
->no_body_match
&& opt
->body_hit
)
1848 return grep_source_1(opt
, gs
, 0);
1851 static void grep_source_init_buf(struct grep_source
*gs
,
1855 gs
->type
= GREP_SOURCE_BUF
;
1861 gs
->identifier
= NULL
;
1864 int grep_buffer(struct grep_opt
*opt
, const char *buf
, unsigned long size
)
1866 struct grep_source gs
;
1869 grep_source_init_buf(&gs
, buf
, size
);
1871 r
= grep_source(opt
, &gs
);
1873 grep_source_clear(&gs
);
1877 void grep_source_init_file(struct grep_source
*gs
, const char *name
,
1880 gs
->type
= GREP_SOURCE_FILE
;
1881 gs
->name
= xstrdup_or_null(name
);
1882 gs
->path
= xstrdup_or_null(path
);
1886 gs
->identifier
= xstrdup(path
);
1889 void grep_source_init_oid(struct grep_source
*gs
, const char *name
,
1890 const char *path
, const struct object_id
*oid
,
1891 struct repository
*repo
)
1893 gs
->type
= GREP_SOURCE_OID
;
1894 gs
->name
= xstrdup_or_null(name
);
1895 gs
->path
= xstrdup_or_null(path
);
1899 gs
->identifier
= oiddup(oid
);
1903 void grep_source_clear(struct grep_source
*gs
)
1905 FREE_AND_NULL(gs
->name
);
1906 FREE_AND_NULL(gs
->path
);
1907 FREE_AND_NULL(gs
->identifier
);
1908 grep_source_clear_data(gs
);
1911 void grep_source_clear_data(struct grep_source
*gs
)
1914 case GREP_SOURCE_FILE
:
1915 case GREP_SOURCE_OID
:
1916 /* these types own the buffer */
1917 free((char *)gs
->buf
);
1921 case GREP_SOURCE_BUF
:
1922 /* leave user-provided buf intact */
1927 static int grep_source_load_oid(struct grep_source
*gs
)
1929 enum object_type type
;
1931 gs
->buf
= repo_read_object_file(gs
->repo
, gs
->identifier
, &type
,
1934 return error(_("'%s': unable to read %s"),
1936 oid_to_hex(gs
->identifier
));
1940 static int grep_source_load_file(struct grep_source
*gs
)
1942 const char *filename
= gs
->identifier
;
1948 if (lstat(filename
, &st
) < 0) {
1950 if (errno
!= ENOENT
)
1951 error_errno(_("failed to stat '%s'"), filename
);
1954 if (!S_ISREG(st
.st_mode
))
1956 size
= xsize_t(st
.st_size
);
1957 i
= open(filename
, O_RDONLY
);
1960 data
= xmallocz(size
);
1961 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1962 error_errno(_("'%s': short read"), filename
);
1974 static int grep_source_load(struct grep_source
*gs
)
1980 case GREP_SOURCE_FILE
:
1981 return grep_source_load_file(gs
);
1982 case GREP_SOURCE_OID
:
1983 return grep_source_load_oid(gs
);
1984 case GREP_SOURCE_BUF
:
1985 return gs
->buf
? 0 : -1;
1987 BUG("invalid grep_source type to load");
1990 void grep_source_load_driver(struct grep_source
*gs
,
1991 struct index_state
*istate
)
1998 gs
->driver
= userdiff_find_by_path(istate
, gs
->path
);
2000 gs
->driver
= userdiff_find_by_name("default");
2004 static int grep_source_is_binary(struct grep_source
*gs
,
2005 struct index_state
*istate
)
2007 grep_source_load_driver(gs
, istate
);
2008 if (gs
->driver
->binary
!= -1)
2009 return gs
->driver
->binary
;
2011 if (!grep_source_load(gs
))
2012 return buffer_is_binary(gs
->buf
, gs
->size
);