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 const char *color_grep_slots
[] = {
23 [GREP_COLOR_CONTEXT
] = "context",
24 [GREP_COLOR_FILENAME
] = "filename",
25 [GREP_COLOR_FUNCTION
] = "function",
26 [GREP_COLOR_LINENO
] = "lineNumber",
27 [GREP_COLOR_COLUMNNO
] = "column",
28 [GREP_COLOR_MATCH_CONTEXT
] = "matchContext",
29 [GREP_COLOR_MATCH_SELECTED
] = "matchSelected",
30 [GREP_COLOR_SELECTED
] = "selected",
31 [GREP_COLOR_SEP
] = "separator",
34 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
36 if (!strcmp(arg
, "default"))
37 return GREP_PATTERN_TYPE_UNSPECIFIED
;
38 else if (!strcmp(arg
, "basic"))
39 return GREP_PATTERN_TYPE_BRE
;
40 else if (!strcmp(arg
, "extended"))
41 return GREP_PATTERN_TYPE_ERE
;
42 else if (!strcmp(arg
, "fixed"))
43 return GREP_PATTERN_TYPE_FIXED
;
44 else if (!strcmp(arg
, "perl"))
45 return GREP_PATTERN_TYPE_PCRE
;
46 die("bad %s argument: %s", opt
, arg
);
49 define_list_config_array_extra(color_grep_slots
, {"match"});
52 * Read the configuration file once and store it in
53 * the grep_defaults template.
55 int grep_config(const char *var
, const char *value
, void *cb
)
57 struct grep_opt
*opt
= cb
;
60 if (userdiff_config(var
, value
) < 0)
63 if (!strcmp(var
, "grep.extendedregexp")) {
64 opt
->extended_regexp_option
= git_config_bool(var
, value
);
68 if (!strcmp(var
, "grep.patterntype")) {
69 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
73 if (!strcmp(var
, "grep.linenumber")) {
74 opt
->linenum
= git_config_bool(var
, value
);
77 if (!strcmp(var
, "grep.column")) {
78 opt
->columnnum
= git_config_bool(var
, value
);
82 if (!strcmp(var
, "grep.fullname")) {
83 opt
->relative
= !git_config_bool(var
, value
);
87 if (!strcmp(var
, "color.grep"))
88 opt
->color
= git_config_colorbool(var
, value
);
89 if (!strcmp(var
, "color.grep.match")) {
90 if (grep_config("color.grep.matchcontext", value
, cb
) < 0)
92 if (grep_config("color.grep.matchselected", value
, cb
) < 0)
94 } else if (skip_prefix(var
, "color.grep.", &slot
)) {
95 int i
= LOOKUP_CONFIG(color_grep_slots
, slot
);
100 color
= opt
->colors
[i
];
102 return config_error_nonbool(var
);
103 return color_parse(value
, color
);
108 void grep_init(struct grep_opt
*opt
, struct repository
*repo
)
110 struct grep_opt blank
= GREP_OPT_INIT
;
111 memcpy(opt
, &blank
, sizeof(*opt
));
114 opt
->pattern_tail
= &opt
->pattern_list
;
115 opt
->header_tail
= &opt
->header_list
;
118 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
119 const char *origin
, int no
,
120 enum grep_pat_token t
,
121 enum grep_header_field field
)
123 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
124 p
->pattern
= xmemdupz(pat
, patlen
);
125 p
->patternlen
= patlen
;
133 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
140 case GREP_PATTERN
: /* atom */
141 case GREP_PATTERN_HEAD
:
142 case GREP_PATTERN_BODY
:
144 struct grep_pat
*new_pat
;
146 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
147 while (++len
<= p
->patternlen
) {
148 if (*(--cp
) == '\n') {
155 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
156 p
->no
, p
->token
, p
->field
);
157 new_pat
->next
= p
->next
;
159 *tail
= &new_pat
->next
;
162 p
->patternlen
-= len
;
170 void append_header_grep_pattern(struct grep_opt
*opt
,
171 enum grep_header_field field
, const char *pat
)
173 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
174 GREP_PATTERN_HEAD
, field
);
175 if (field
== GREP_HEADER_REFLOG
)
176 opt
->use_reflog_filter
= 1;
177 do_append_grep_pat(&opt
->header_tail
, p
);
180 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
181 const char *origin
, int no
, enum grep_pat_token t
)
183 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
186 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
187 const char *origin
, int no
, enum grep_pat_token t
)
189 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
190 do_append_grep_pat(&opt
->pattern_tail
, p
);
193 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
195 struct grep_pat
*pat
;
196 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
199 ret
->pattern_list
= NULL
;
200 ret
->pattern_tail
= &ret
->pattern_list
;
202 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
204 if(pat
->token
== GREP_PATTERN_HEAD
)
205 append_header_grep_pattern(ret
, pat
->field
,
208 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
209 pat
->origin
, pat
->no
, pat
->token
);
215 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
221 xsnprintf(where
, sizeof(where
), "In '%s' at %d, ", p
->origin
, p
->no
);
223 xsnprintf(where
, sizeof(where
), "%s, ", p
->origin
);
227 die("%s'%s': %s", where
, p
->pattern
, error
);
230 static int is_fixed(const char *s
, size_t len
)
234 for (i
= 0; i
< len
; i
++) {
235 if (is_regex_special(s
[i
]))
243 #define GREP_PCRE2_DEBUG_MALLOC 0
245 static void *pcre2_malloc(PCRE2_SIZE size
, MAYBE_UNUSED
void *memory_data
)
247 void *pointer
= malloc(size
);
248 #if GREP_PCRE2_DEBUG_MALLOC
249 static int count
= 1;
250 fprintf(stderr
, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer
, count
++, size
);
255 static void pcre2_free(void *pointer
, MAYBE_UNUSED
void *memory_data
)
257 #if GREP_PCRE2_DEBUG_MALLOC
258 static int count
= 1;
260 fprintf(stderr
, "PCRE2:%p -> #%02d: free()\n", pointer
, count
++);
265 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
268 PCRE2_UCHAR errbuf
[256];
269 PCRE2_SIZE erroffset
;
270 int options
= PCRE2_MULTILINE
;
274 int literal
= !opt
->ignore_case
&& (p
->fixed
|| p
->is_fixed
);
277 * Call pcre2_general_context_create() before calling any
278 * other pcre2_*(). It sets up our malloc()/free() functions
279 * with which everything else is allocated.
281 p
->pcre2_general_context
= pcre2_general_context_create(
282 pcre2_malloc
, pcre2_free
, NULL
);
283 if (!p
->pcre2_general_context
)
284 die("Couldn't allocate PCRE2 general context");
286 if (opt
->ignore_case
) {
287 if (!opt
->ignore_locale
&& has_non_ascii(p
->pattern
)) {
288 p
->pcre2_tables
= pcre2_maketables(p
->pcre2_general_context
);
289 p
->pcre2_compile_context
= pcre2_compile_context_create(p
->pcre2_general_context
);
290 pcre2_set_character_tables(p
->pcre2_compile_context
,
293 options
|= PCRE2_CASELESS
;
295 if (!opt
->ignore_locale
&& is_utf8_locale() && !literal
)
296 options
|= (PCRE2_UTF
| PCRE2_UCP
| PCRE2_MATCH_INVALID_UTF
);
298 #ifndef GIT_PCRE2_VERSION_10_36_OR_HIGHER
299 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
300 if (PCRE2_MATCH_INVALID_UTF
&& options
& (PCRE2_UTF
| PCRE2_CASELESS
))
301 options
|= PCRE2_NO_START_OPTIMIZE
;
304 p
->pcre2_pattern
= pcre2_compile((PCRE2_SPTR
)p
->pattern
,
305 p
->patternlen
, options
, &error
, &erroffset
,
306 p
->pcre2_compile_context
);
308 if (p
->pcre2_pattern
) {
309 p
->pcre2_match_data
= pcre2_match_data_create_from_pattern(p
->pcre2_pattern
, p
->pcre2_general_context
);
310 if (!p
->pcre2_match_data
)
311 die("Couldn't allocate PCRE2 match data");
313 pcre2_get_error_message(error
, errbuf
, sizeof(errbuf
));
314 compile_regexp_failed(p
, (const char *)&errbuf
);
317 pcre2_config(PCRE2_CONFIG_JIT
, &p
->pcre2_jit_on
);
318 if (p
->pcre2_jit_on
) {
319 jitret
= pcre2_jit_compile(p
->pcre2_pattern
, PCRE2_JIT_COMPLETE
);
321 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p
->pattern
, jitret
);
324 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
325 * tells us whether the library itself supports JIT,
326 * but to see whether we're going to be actually using
327 * JIT we need to extract PCRE2_INFO_JITSIZE from the
328 * pattern *after* we do pcre2_jit_compile() above.
330 * This is because if the pattern contains the
331 * (*NO_JIT) verb (see pcre2syntax(3))
332 * pcre2_jit_compile() will exit early with 0. If we
333 * then proceed to call pcre2_jit_match() further down
334 * the line instead of pcre2_match() we'll either
335 * segfault (pre PCRE 10.31) or run into a fatal error
338 patinforet
= pcre2_pattern_info(p
->pcre2_pattern
, PCRE2_INFO_JITSIZE
, &jitsizearg
);
340 BUG("pcre2_pattern_info() failed: %d", patinforet
);
341 if (jitsizearg
== 0) {
348 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
349 regmatch_t
*match
, int eflags
)
353 PCRE2_UCHAR errbuf
[256];
355 if (eflags
& REG_NOTBOL
)
356 flags
|= PCRE2_NOTBOL
;
359 ret
= pcre2_jit_match(p
->pcre2_pattern
, (unsigned char *)line
,
360 eol
- line
, 0, flags
, p
->pcre2_match_data
,
363 ret
= pcre2_match(p
->pcre2_pattern
, (unsigned char *)line
,
364 eol
- line
, 0, flags
, p
->pcre2_match_data
,
367 if (ret
< 0 && ret
!= PCRE2_ERROR_NOMATCH
) {
368 pcre2_get_error_message(ret
, errbuf
, sizeof(errbuf
));
369 die("%s failed with error code %d: %s",
370 (p
->pcre2_jit_on
? "pcre2_jit_match" : "pcre2_match"), ret
,
374 ovector
= pcre2_get_ovector_pointer(p
->pcre2_match_data
);
376 match
->rm_so
= (int)ovector
[0];
377 match
->rm_eo
= (int)ovector
[1];
383 static void free_pcre2_pattern(struct grep_pat
*p
)
385 pcre2_compile_context_free(p
->pcre2_compile_context
);
386 pcre2_code_free(p
->pcre2_pattern
);
387 pcre2_match_data_free(p
->pcre2_match_data
);
388 #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
389 pcre2_maketables_free(p
->pcre2_general_context
, p
->pcre2_tables
);
391 free((void *)p
->pcre2_tables
);
393 pcre2_general_context_free(p
->pcre2_general_context
);
395 #else /* !USE_LIBPCRE2 */
396 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
398 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
401 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
402 regmatch_t
*match
, int eflags
)
407 static void free_pcre2_pattern(struct grep_pat
*p
)
411 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
413 struct strbuf sb
= STRBUF_INIT
;
417 basic_regex_quote_buf(&sb
, p
->pattern
);
418 if (opt
->ignore_case
)
419 regflags
|= REG_ICASE
;
420 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
424 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
425 compile_regexp_failed(p
, errbuf
);
428 #endif /* !USE_LIBPCRE2 */
430 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
433 int regflags
= REG_NEWLINE
;
435 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_UNSPECIFIED
)
436 opt
->pattern_type_option
= (opt
->extended_regexp_option
437 ? GREP_PATTERN_TYPE_ERE
438 : GREP_PATTERN_TYPE_BRE
);
440 p
->word_regexp
= opt
->word_regexp
;
441 p
->ignore_case
= opt
->ignore_case
;
442 p
->fixed
= opt
->pattern_type_option
== GREP_PATTERN_TYPE_FIXED
;
444 if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_PCRE
&&
445 memchr(p
->pattern
, 0, p
->patternlen
))
446 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
448 p
->is_fixed
= is_fixed(p
->pattern
, p
->patternlen
);
450 if (!p
->fixed
&& !p
->is_fixed
) {
451 const char *no_jit
= "(*NO_JIT)";
452 const int no_jit_len
= strlen(no_jit
);
453 if (starts_with(p
->pattern
, no_jit
) &&
454 is_fixed(p
->pattern
+ no_jit_len
,
455 p
->patternlen
- no_jit_len
))
459 if (p
->fixed
|| p
->is_fixed
) {
462 compile_pcre2_pattern(p
, opt
);
465 * E.g. t7811-grep-open.sh relies on the
466 * pattern being restored.
468 char *old_pattern
= p
->pattern
;
469 size_t old_patternlen
= p
->patternlen
;
470 struct strbuf sb
= STRBUF_INIT
;
473 * There is the PCRE2_LITERAL flag, but it's
474 * only in PCRE v2 10.30 and later. Needing to
475 * ifdef our way around that and dealing with
476 * it + PCRE2_MULTILINE being an error is more
477 * complex than just quoting this ourselves.
479 strbuf_add(&sb
, "\\Q", 2);
480 strbuf_add(&sb
, p
->pattern
, p
->patternlen
);
481 strbuf_add(&sb
, "\\E", 2);
484 p
->patternlen
= sb
.len
;
485 compile_pcre2_pattern(p
, opt
);
486 p
->pattern
= old_pattern
;
487 p
->patternlen
= old_patternlen
;
490 #else /* !USE_LIBPCRE2 */
491 compile_fixed_regexp(p
, opt
);
492 #endif /* !USE_LIBPCRE2 */
496 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_PCRE
) {
497 compile_pcre2_pattern(p
, opt
);
502 regflags
|= REG_ICASE
;
503 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_ERE
)
504 regflags
|= REG_EXTENDED
;
505 err
= regcomp(&p
->regexp
, p
->pattern
, regflags
);
508 regerror(err
, &p
->regexp
, errbuf
, 1024);
509 compile_regexp_failed(p
, errbuf
);
513 static struct grep_expr
*grep_not_expr(struct grep_expr
*expr
)
515 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
516 z
->node
= GREP_NODE_NOT
;
521 static struct grep_expr
*grep_binexp(enum grep_expr_node kind
,
522 struct grep_expr
*left
,
523 struct grep_expr
*right
)
525 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
527 z
->u
.binary
.left
= left
;
528 z
->u
.binary
.right
= right
;
532 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
534 return grep_binexp(GREP_NODE_OR
, left
, right
);
537 static struct grep_expr
*grep_and_expr(struct grep_expr
*left
, struct grep_expr
*right
)
539 return grep_binexp(GREP_NODE_AND
, left
, right
);
542 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
543 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
552 case GREP_PATTERN
: /* atom */
553 case GREP_PATTERN_HEAD
:
554 case GREP_PATTERN_BODY
:
556 x
->node
= GREP_NODE_ATOM
;
560 case GREP_OPEN_PAREN
:
562 x
= compile_pattern_or(list
);
563 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
564 die("unmatched parenthesis");
565 *list
= (*list
)->next
;
572 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
583 die("--not not followed by pattern expression");
585 x
= compile_pattern_not(list
);
587 die("--not followed by non pattern expression");
588 return grep_not_expr(x
);
590 return compile_pattern_atom(list
);
594 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
597 struct grep_expr
*x
, *y
;
599 x
= compile_pattern_not(list
);
601 if (p
&& p
->token
== GREP_AND
) {
603 die("--and not preceded by pattern expression");
605 die("--and not followed by pattern expression");
607 y
= compile_pattern_and(list
);
609 die("--and not followed by pattern expression");
610 return grep_and_expr(x
, y
);
615 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
618 struct grep_expr
*x
, *y
;
620 x
= compile_pattern_and(list
);
622 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
623 y
= compile_pattern_or(list
);
625 die("not a pattern expression %s", p
->pattern
);
626 return grep_or_expr(x
, y
);
631 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
633 return compile_pattern_or(list
);
636 static struct grep_expr
*grep_true_expr(void)
638 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
639 z
->node
= GREP_NODE_TRUE
;
643 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
646 struct grep_expr
*header_expr
;
647 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
648 enum grep_header_field fld
;
650 if (!opt
->header_list
)
653 for (p
= opt
->header_list
; p
; p
= p
->next
) {
654 if (p
->token
!= GREP_PATTERN_HEAD
)
655 BUG("a non-header pattern in grep header list.");
656 if (p
->field
< GREP_HEADER_FIELD_MIN
||
657 GREP_HEADER_FIELD_MAX
<= p
->field
)
658 BUG("unknown header field %d", p
->field
);
659 compile_regexp(p
, opt
);
662 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
663 header_group
[fld
] = NULL
;
665 for (p
= opt
->header_list
; p
; p
= p
->next
) {
667 struct grep_pat
*pp
= p
;
669 h
= compile_pattern_atom(&pp
);
670 if (!h
|| pp
!= p
->next
)
671 BUG("malformed header expr");
672 if (!header_group
[p
->field
]) {
673 header_group
[p
->field
] = h
;
676 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
681 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
682 if (!header_group
[fld
])
685 header_expr
= grep_true_expr();
686 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
691 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
693 struct grep_expr
*z
= x
;
696 assert(x
->node
== GREP_NODE_OR
);
697 if (x
->u
.binary
.right
&&
698 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
699 x
->u
.binary
.right
= y
;
702 x
= x
->u
.binary
.right
;
707 void compile_grep_patterns(struct grep_opt
*opt
)
710 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
713 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
715 case GREP_PATTERN
: /* atom */
716 case GREP_PATTERN_HEAD
:
717 case GREP_PATTERN_BODY
:
718 compile_regexp(p
, opt
);
726 if (opt
->all_match
|| opt
->no_body_match
|| header_expr
)
731 p
= opt
->pattern_list
;
733 opt
->pattern_expression
= compile_pattern_expr(&p
);
735 die("incomplete pattern expression: %s", p
->pattern
);
737 if (opt
->no_body_match
&& opt
->pattern_expression
)
738 opt
->pattern_expression
= grep_not_expr(opt
->pattern_expression
);
743 if (!opt
->pattern_expression
)
744 opt
->pattern_expression
= header_expr
;
745 else if (opt
->all_match
)
746 opt
->pattern_expression
= grep_splice_or(header_expr
,
747 opt
->pattern_expression
);
749 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
754 static void free_pattern_expr(struct grep_expr
*x
)
761 free_pattern_expr(x
->u
.unary
);
765 free_pattern_expr(x
->u
.binary
.left
);
766 free_pattern_expr(x
->u
.binary
.right
);
772 static void free_grep_pat(struct grep_pat
*pattern
)
774 struct grep_pat
*p
, *n
;
776 for (p
= pattern
; p
; p
= n
) {
779 case GREP_PATTERN
: /* atom */
780 case GREP_PATTERN_HEAD
:
781 case GREP_PATTERN_BODY
:
782 if (p
->pcre2_pattern
)
783 free_pcre2_pattern(p
);
795 void free_grep_patterns(struct grep_opt
*opt
)
797 free_grep_pat(opt
->pattern_list
);
798 free_grep_pat(opt
->header_list
);
800 if (opt
->pattern_expression
)
801 free_pattern_expr(opt
->pattern_expression
);
804 static const char *end_of_line(const char *cp
, unsigned long *left
)
806 unsigned long l
= *left
;
807 while (l
&& *cp
!= '\n') {
815 static int word_char(char ch
)
817 return isalnum(ch
) || ch
== '_';
820 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
823 if (want_color(opt
->color
) && color
&& color
[0]) {
824 opt
->output(opt
, color
, strlen(color
));
825 opt
->output(opt
, data
, size
);
826 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
828 opt
->output(opt
, data
, size
);
831 static void output_sep(struct grep_opt
*opt
, char sign
)
833 if (opt
->null_following_name
)
834 opt
->output(opt
, "\0", 1);
836 output_color(opt
, &sign
, 1, opt
->colors
[GREP_COLOR_SEP
]);
839 static void show_name(struct grep_opt
*opt
, const char *name
)
841 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
842 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
845 static int patmatch(struct grep_pat
*p
,
846 const char *line
, const char *eol
,
847 regmatch_t
*match
, int eflags
)
851 if (p
->pcre2_pattern
)
852 hit
= !pcre2match(p
, line
, eol
, match
, eflags
);
854 hit
= !regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
,
860 static void strip_timestamp(const char *bol
, const char **eol_p
)
862 const char *eol
= *eol_p
;
864 while (bol
< --eol
) {
877 { "committer ", 10 },
881 static int headerless_match_one_pattern(struct grep_pat
*p
,
882 const char *bol
, const char *eol
,
883 enum grep_context ctx
,
884 regmatch_t
*pmatch
, int eflags
)
887 const char *start
= bol
;
889 if ((p
->token
!= GREP_PATTERN
) &&
890 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
894 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
896 if (hit
&& p
->word_regexp
) {
897 if ((pmatch
[0].rm_so
< 0) ||
898 (eol
- bol
) < pmatch
[0].rm_so
||
899 (pmatch
[0].rm_eo
< 0) ||
900 (eol
- bol
) < pmatch
[0].rm_eo
)
901 die("regexp returned nonsense");
903 /* Match beginning must be either beginning of the
904 * line, or at word boundary (i.e. the last char must
905 * not be a word char). Similarly, match end must be
906 * either end of the line, or at word boundary
907 * (i.e. the next char must not be a word char).
909 if ( ((pmatch
[0].rm_so
== 0) ||
910 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
911 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
912 !word_char(bol
[pmatch
[0].rm_eo
])) )
917 /* Words consist of at least one character. */
918 if (pmatch
->rm_so
== pmatch
->rm_eo
)
921 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
922 /* There could be more than one match on the
923 * line, and the first match might not be
924 * strict word match. But later ones could be!
925 * Forward to the next possible start, i.e. the
926 * next position following a non-word char.
928 bol
= pmatch
[0].rm_so
+ bol
+ 1;
929 while (word_char(bol
[-1]) && bol
< eol
)
931 eflags
|= REG_NOTBOL
;
937 pmatch
[0].rm_so
+= bol
- start
;
938 pmatch
[0].rm_eo
+= bol
- start
;
943 static int match_one_pattern(struct grep_pat
*p
,
944 const char *bol
, const char *eol
,
945 enum grep_context ctx
, regmatch_t
*pmatch
,
951 if (p
->token
== GREP_PATTERN_HEAD
) {
952 assert(p
->field
< ARRAY_SIZE(header_field
));
953 field
= header_field
[p
->field
].field
;
954 len
= header_field
[p
->field
].len
;
955 if (strncmp(bol
, field
, len
))
960 case GREP_HEADER_AUTHOR
:
961 case GREP_HEADER_COMMITTER
:
962 strip_timestamp(bol
, &eol
);
969 return headerless_match_one_pattern(p
, bol
, eol
, ctx
, pmatch
, eflags
);
973 static int match_expr_eval(struct grep_opt
*opt
, struct grep_expr
*x
,
974 const char *bol
, const char *eol
,
975 enum grep_context ctx
, ssize_t
*col
,
976 ssize_t
*icol
, int collect_hits
)
987 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
,
989 if (h
&& (*col
< 0 || tmp
.rm_so
< *col
))
992 if (x
->u
.atom
->token
== GREP_PATTERN_BODY
)
997 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
999 h
= !match_expr_eval(opt
, x
->u
.unary
, bol
, eol
, ctx
, icol
, col
,
1003 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1005 if (h
|| opt
->columnnum
) {
1007 * Don't short-circuit AND when given --column, since a
1008 * NOT earlier in the tree may turn this into an OR. In
1009 * this case, see the below comment.
1011 h
&= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
,
1016 if (!(collect_hits
|| opt
->columnnum
)) {
1018 * Don't short-circuit OR when given --column (or
1019 * collecting hits) to ensure we don't skip a later
1020 * child that would produce an earlier match.
1022 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
,
1023 ctx
, col
, icol
, 0) ||
1024 match_expr_eval(opt
, x
->u
.binary
.right
, bol
,
1025 eol
, ctx
, col
, icol
, 0));
1027 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1030 x
->u
.binary
.left
->hit
|= h
;
1031 h
|= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
, col
,
1032 icol
, collect_hits
);
1035 die("Unexpected node type (internal error) %d", x
->node
);
1042 static int match_expr(struct grep_opt
*opt
,
1043 const char *bol
, const char *eol
,
1044 enum grep_context ctx
, ssize_t
*col
,
1045 ssize_t
*icol
, int collect_hits
)
1047 struct grep_expr
*x
= opt
->pattern_expression
;
1048 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, col
, icol
, collect_hits
);
1051 static int match_line(struct grep_opt
*opt
,
1052 const char *bol
, const char *eol
,
1053 ssize_t
*col
, ssize_t
*icol
,
1054 enum grep_context ctx
, int collect_hits
)
1059 if (opt
->pattern_expression
)
1060 return match_expr(opt
, bol
, eol
, ctx
, col
, icol
,
1063 /* we do not call with collect_hits without being extended */
1064 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1066 if (match_one_pattern(p
, bol
, eol
, ctx
, &tmp
, 0)) {
1068 if (!opt
->columnnum
) {
1070 * Without --column, any single match on a line
1071 * is enough to know that it needs to be
1072 * printed. With --column, scan _all_ patterns
1073 * to find the earliest.
1077 if (*col
< 0 || tmp
.rm_so
< *col
)
1084 static int match_next_pattern(struct grep_pat
*p
,
1085 const char *bol
, const char *eol
,
1086 enum grep_context ctx
,
1087 regmatch_t
*pmatch
, int eflags
)
1091 if (!headerless_match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1093 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1095 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1096 if (match
.rm_so
> pmatch
->rm_so
)
1098 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1101 pmatch
->rm_so
= match
.rm_so
;
1102 pmatch
->rm_eo
= match
.rm_eo
;
1106 int grep_next_match(struct grep_opt
*opt
,
1107 const char *bol
, const char *eol
,
1108 enum grep_context ctx
, regmatch_t
*pmatch
,
1109 enum grep_header_field field
, int eflags
)
1114 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1116 for (p
= ((ctx
== GREP_CONTEXT_HEAD
)
1117 ? opt
->header_list
: opt
->pattern_list
);
1120 case GREP_PATTERN_HEAD
:
1121 if ((field
!= GREP_HEADER_FIELD_MAX
) &&
1122 (p
->field
!= field
))
1125 case GREP_PATTERN
: /* atom */
1126 case GREP_PATTERN_BODY
:
1127 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1138 static void show_line_header(struct grep_opt
*opt
, const char *name
,
1139 unsigned lno
, ssize_t cno
, char sign
)
1141 if (opt
->heading
&& opt
->last_shown
== 0) {
1142 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1143 opt
->output(opt
, "\n", 1);
1145 opt
->last_shown
= lno
;
1147 if (!opt
->heading
&& opt
->pathname
) {
1148 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1149 output_sep(opt
, sign
);
1153 xsnprintf(buf
, sizeof(buf
), "%d", lno
);
1154 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_LINENO
]);
1155 output_sep(opt
, sign
);
1158 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1159 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1160 * being called with a context line.
1162 if (opt
->columnnum
&& cno
) {
1164 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, (uintmax_t)cno
);
1165 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_COLUMNNO
]);
1166 output_sep(opt
, sign
);
1170 static void show_line(struct grep_opt
*opt
,
1171 const char *bol
, const char *eol
,
1172 const char *name
, unsigned lno
, ssize_t cno
, char sign
)
1174 int rest
= eol
- bol
;
1175 const char *match_color
= NULL
;
1176 const char *line_color
= NULL
;
1178 if (opt
->file_break
&& opt
->last_shown
== 0) {
1179 if (opt
->show_hunk_mark
)
1180 opt
->output(opt
, "\n", 1);
1181 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1182 if (opt
->last_shown
== 0) {
1183 if (opt
->show_hunk_mark
) {
1184 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1185 opt
->output(opt
, "\n", 1);
1187 } else if (lno
> opt
->last_shown
+ 1) {
1188 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1189 opt
->output(opt
, "\n", 1);
1192 if (!opt
->only_matching
) {
1194 * In case the line we're being called with contains more than
1195 * one match, leave printing each header to the loop below.
1197 show_line_header(opt
, name
, lno
, cno
, sign
);
1199 if (opt
->color
|| opt
->only_matching
) {
1201 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1206 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
1208 match_color
= opt
->colors
[GREP_COLOR_MATCH_CONTEXT
];
1210 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
1211 else if (sign
== '-')
1212 line_color
= opt
->colors
[GREP_COLOR_CONTEXT
];
1213 else if (sign
== '=')
1214 line_color
= opt
->colors
[GREP_COLOR_FUNCTION
];
1216 while (grep_next_match(opt
, bol
, eol
, ctx
, &match
,
1217 GREP_HEADER_FIELD_MAX
, eflags
)) {
1218 if (match
.rm_so
== match
.rm_eo
)
1221 if (opt
->only_matching
)
1222 show_line_header(opt
, name
, lno
, cno
, sign
);
1224 output_color(opt
, bol
, match
.rm_so
, line_color
);
1225 output_color(opt
, bol
+ match
.rm_so
,
1226 match
.rm_eo
- match
.rm_so
, match_color
);
1227 if (opt
->only_matching
)
1228 opt
->output(opt
, "\n", 1);
1231 rest
-= match
.rm_eo
;
1232 eflags
= REG_NOTBOL
;
1235 if (!opt
->only_matching
) {
1236 output_color(opt
, bol
, rest
, line_color
);
1237 opt
->output(opt
, "\n", 1);
1244 * This lock protects access to the gitattributes machinery, which is
1247 pthread_mutex_t grep_attr_mutex
;
1249 static inline void grep_attr_lock(void)
1252 pthread_mutex_lock(&grep_attr_mutex
);
1255 static inline void grep_attr_unlock(void)
1258 pthread_mutex_unlock(&grep_attr_mutex
);
1261 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
,
1262 const char *bol
, const char *eol
)
1264 xdemitconf_t
*xecfg
= opt
->priv
;
1265 if (xecfg
&& !xecfg
->find_func
) {
1266 grep_source_load_driver(gs
, opt
->repo
->index
);
1267 if (gs
->driver
->funcname
.pattern
) {
1268 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1269 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1271 xecfg
= opt
->priv
= NULL
;
1277 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1278 xecfg
->find_func_priv
) >= 0;
1283 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1288 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1289 const char *bol
, unsigned lno
)
1291 while (bol
> gs
->buf
) {
1292 const char *eol
= --bol
;
1294 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1298 if (lno
<= opt
->last_shown
)
1301 if (match_funcname(opt
, gs
, bol
, eol
)) {
1302 show_line(opt
, bol
, eol
, gs
->name
, lno
, 0, '=');
1308 static int is_empty_line(const char *bol
, const char *eol
);
1310 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1311 const char *bol
, const char *end
, unsigned lno
)
1313 unsigned cur
= lno
, from
= 1, funcname_lno
= 0, orig_from
;
1314 int funcname_needed
= !!opt
->funcname
, comment_needed
= 0;
1316 if (opt
->pre_context
< lno
)
1317 from
= lno
- opt
->pre_context
;
1318 if (from
<= opt
->last_shown
)
1319 from
= opt
->last_shown
+ 1;
1321 if (opt
->funcbody
) {
1322 if (match_funcname(opt
, gs
, bol
, end
))
1325 funcname_needed
= 1;
1326 from
= opt
->last_shown
+ 1;
1330 while (bol
> gs
->buf
&& cur
> from
) {
1331 const char *next_bol
= bol
;
1332 const char *eol
= --bol
;
1334 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1337 if (comment_needed
&& (is_empty_line(bol
, eol
) ||
1338 match_funcname(opt
, gs
, bol
, eol
))) {
1347 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1349 funcname_needed
= 0;
1357 /* We need to look even further back to find a function signature. */
1358 if (opt
->funcname
&& funcname_needed
)
1359 show_funcname_line(opt
, gs
, bol
, cur
);
1363 const char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1365 while (*eol
!= '\n')
1367 show_line(opt
, bol
, eol
, gs
->name
, cur
, 0, sign
);
1373 static int should_lookahead(struct grep_opt
*opt
)
1377 if (opt
->pattern_expression
)
1378 return 0; /* punt for too complex stuff */
1381 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1382 if (p
->token
!= GREP_PATTERN
)
1383 return 0; /* punt for "header only" and stuff */
1388 static int look_ahead(struct grep_opt
*opt
,
1389 unsigned long *left_p
,
1393 unsigned lno
= *lno_p
;
1394 const char *bol
= *bol_p
;
1396 const char *sp
, *last_bol
;
1397 regoff_t earliest
= -1;
1399 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1403 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1404 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1406 if (earliest
< 0 || m
.rm_so
< earliest
)
1411 *bol_p
= bol
+ *left_p
;
1415 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1416 ; /* find the beginning of the line */
1419 for (sp
= bol
; sp
< last_bol
; sp
++) {
1423 *left_p
-= last_bol
- bol
;
1429 static int fill_textconv_grep(struct repository
*r
,
1430 struct userdiff_driver
*driver
,
1431 struct grep_source
*gs
)
1433 struct diff_filespec
*df
;
1437 if (!driver
|| !driver
->textconv
)
1438 return grep_source_load(gs
);
1441 * The textconv interface is intimately tied to diff_filespecs, so we
1442 * have to pretend to be one. If we could unify the grep_source
1443 * and diff_filespec structs, this mess could just go away.
1445 df
= alloc_filespec(gs
->path
);
1447 case GREP_SOURCE_OID
:
1448 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1450 case GREP_SOURCE_FILE
:
1451 fill_filespec(df
, null_oid(), 0, 0100644);
1454 BUG("attempt to textconv something without a path?");
1458 * fill_textconv is not remotely thread-safe; it modifies the global
1459 * diff tempfile structure, writes to the_repo's odb and might
1460 * internally call thread-unsafe functions such as the
1461 * prepare_packed_git() lazy-initializator. Because of the last two, we
1462 * must ensure mutual exclusion between this call and the object reading
1463 * API, thus we use obj_read_lock() here.
1465 * TODO: allowing text conversion to run in parallel with object
1466 * reading operations might increase performance in the multithreaded
1467 * non-worktreee git-grep with --textconv.
1470 size
= fill_textconv(r
, driver
, df
, &buf
);
1475 * The normal fill_textconv usage by the diff machinery would just keep
1476 * the textconv'd buf separate from the diff_filespec. But much of the
1477 * grep code passes around a grep_source and assumes that its "buf"
1478 * pointer is the beginning of the thing we are searching. So let's
1479 * install our textconv'd version into the grep_source, taking care not
1480 * to leak any existing buffer.
1482 grep_source_clear_data(gs
);
1489 static int is_empty_line(const char *bol
, const char *eol
)
1491 while (bol
< eol
&& isspace(*bol
))
1496 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1499 const char *peek_bol
= NULL
;
1502 unsigned last_hit
= 0;
1503 int binary_match_only
= 0;
1505 int try_lookahead
= 0;
1506 int show_function
= 0;
1507 struct userdiff_driver
*textconv
= NULL
;
1508 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1511 if (!opt
->status_only
&& gs
->name
== NULL
)
1512 BUG("grep call which could print a name requires "
1513 "grep_source.name be non-NULL");
1516 opt
->output
= std_output
;
1518 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1520 /* Show hunk marks, except for the first file. */
1521 if (opt
->last_shown
)
1522 opt
->show_hunk_mark
= 1;
1524 * If we're using threads then we can't easily identify
1525 * the first file. Always put hunk marks in that case
1526 * and skip the very first one later in work_done().
1528 if (opt
->output
!= std_output
)
1529 opt
->show_hunk_mark
= 1;
1531 opt
->last_shown
= 0;
1533 if (opt
->allow_textconv
) {
1534 grep_source_load_driver(gs
, opt
->repo
->index
);
1536 * We might set up the shared textconv cache data here, which
1537 * is not thread-safe. Also, get_oid_with_context() and
1538 * parse_object() might be internally called. As they are not
1539 * currently thread-safe and might be racy with object reading,
1540 * obj_read_lock() must be called.
1544 textconv
= userdiff_get_textconv(opt
->repo
, gs
->driver
);
1550 * We know the result of a textconv is text, so we only have to care
1551 * about binary handling if we are not using it.
1554 switch (opt
->binary
) {
1555 case GREP_BINARY_DEFAULT
:
1556 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1557 binary_match_only
= 1;
1559 case GREP_BINARY_NOMATCH
:
1560 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1561 return 0; /* Assume unmatch */
1563 case GREP_BINARY_TEXT
:
1566 BUG("unknown binary handling mode");
1570 memset(&xecfg
, 0, sizeof(xecfg
));
1573 try_lookahead
= should_lookahead(opt
);
1575 if (fill_textconv_grep(opt
->repo
, textconv
, gs
) < 0)
1584 ssize_t col
= -1, icol
= -1;
1587 * look_ahead() skips quickly to the line that possibly
1588 * has the next hit; don't call it if we need to do
1589 * something more than just skipping the current line
1590 * in response to an unmatch for the current line. E.g.
1591 * inside a post-context window, we will show the current
1592 * line as a context around the previous hit when it
1597 && (show_function
||
1598 lno
<= last_hit
+ opt
->post_context
))
1599 && look_ahead(opt
, &left
, &lno
, &bol
))
1601 eol
= end_of_line(bol
, &left
);
1603 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1604 ctx
= GREP_CONTEXT_BODY
;
1606 hit
= match_line(opt
, bol
, eol
, &col
, &icol
, ctx
, collect_hits
);
1611 /* "grep -v -e foo -e bla" should list lines
1612 * that do not have either, so inversion should
1617 if (opt
->unmatch_name_only
) {
1622 if (hit
&& (opt
->max_count
< 0 || count
< opt
->max_count
)) {
1624 if (opt
->status_only
)
1626 if (opt
->name_only
) {
1627 show_name(opt
, gs
->name
);
1632 if (binary_match_only
) {
1633 opt
->output(opt
, "Binary file ", 12);
1634 output_color(opt
, gs
->name
, strlen(gs
->name
),
1635 opt
->colors
[GREP_COLOR_FILENAME
]);
1636 opt
->output(opt
, " matches\n", 9);
1639 /* Hit at this line. If we haven't shown the
1640 * pre-context lines, we would need to show them.
1642 if (opt
->pre_context
|| opt
->funcbody
)
1643 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1644 else if (opt
->funcname
)
1645 show_funcname_line(opt
, gs
, bol
, lno
);
1646 cno
= opt
->invert
? icol
: col
;
1649 * A negative cno indicates that there was no
1650 * match on the line. We are thus inverted and
1651 * being asked to show all lines that _don't_
1652 * match a given expression. Therefore, set cno
1653 * to 0 to suggest the whole line matches.
1657 show_line(opt
, bol
, eol
, gs
->name
, lno
, cno
+ 1, ':');
1663 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1664 unsigned long peek_left
= left
;
1665 const char *peek_eol
= eol
;
1668 * Trailing empty lines are not interesting.
1669 * Peek past them to see if they belong to the
1670 * body of the current function.
1673 while (is_empty_line(peek_bol
, peek_eol
)) {
1674 peek_bol
= peek_eol
+ 1;
1675 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1678 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1681 if (show_function
||
1682 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1683 /* If the last hit is within the post context,
1684 * we need to show this line.
1686 show_line(opt
, bol
, eol
, gs
->name
, lno
, col
+ 1, '-');
1700 if (opt
->status_only
)
1701 return opt
->unmatch_name_only
;
1702 if (opt
->unmatch_name_only
) {
1703 /* We did not see any hit, so we want to show this */
1704 show_name(opt
, gs
->name
);
1708 xdiff_clear_find_func(&xecfg
);
1712 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1713 * which feels mostly useless but sometimes useful. Maybe
1714 * make it another option? For now suppress them.
1716 if (opt
->count
&& count
) {
1718 if (opt
->pathname
) {
1719 output_color(opt
, gs
->name
, strlen(gs
->name
),
1720 opt
->colors
[GREP_COLOR_FILENAME
]);
1721 output_sep(opt
, ':');
1723 xsnprintf(buf
, sizeof(buf
), "%u\n", count
);
1724 opt
->output(opt
, buf
, strlen(buf
));
1730 static void clr_hit_marker(struct grep_expr
*x
)
1732 /* All-hit markers are meaningful only at the very top level
1737 if (x
->node
!= GREP_NODE_OR
)
1739 x
->u
.binary
.left
->hit
= 0;
1740 x
= x
->u
.binary
.right
;
1744 static int chk_hit_marker(struct grep_expr
*x
)
1746 /* Top level nodes have hit markers. See if they all are hits */
1748 if (x
->node
!= GREP_NODE_OR
)
1750 if (!x
->u
.binary
.left
->hit
)
1752 x
= x
->u
.binary
.right
;
1756 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1759 * we do not have to do the two-pass grep when we do not check
1760 * buffer-wide "all-match".
1762 if (!opt
->all_match
&& !opt
->no_body_match
)
1763 return grep_source_1(opt
, gs
, 0);
1765 /* Otherwise the toplevel "or" terms hit a bit differently.
1766 * We first clear hit markers from them.
1768 clr_hit_marker(opt
->pattern_expression
);
1770 grep_source_1(opt
, gs
, 1);
1772 if (opt
->all_match
&& !chk_hit_marker(opt
->pattern_expression
))
1774 if (opt
->no_body_match
&& opt
->body_hit
)
1777 return grep_source_1(opt
, gs
, 0);
1780 static void grep_source_init_buf(struct grep_source
*gs
,
1784 gs
->type
= GREP_SOURCE_BUF
;
1790 gs
->identifier
= NULL
;
1793 int grep_buffer(struct grep_opt
*opt
, const char *buf
, unsigned long size
)
1795 struct grep_source gs
;
1798 grep_source_init_buf(&gs
, buf
, size
);
1800 r
= grep_source(opt
, &gs
);
1802 grep_source_clear(&gs
);
1806 void grep_source_init_file(struct grep_source
*gs
, const char *name
,
1809 gs
->type
= GREP_SOURCE_FILE
;
1810 gs
->name
= xstrdup_or_null(name
);
1811 gs
->path
= xstrdup_or_null(path
);
1815 gs
->identifier
= xstrdup(path
);
1818 void grep_source_init_oid(struct grep_source
*gs
, const char *name
,
1819 const char *path
, const struct object_id
*oid
,
1820 struct repository
*repo
)
1822 gs
->type
= GREP_SOURCE_OID
;
1823 gs
->name
= xstrdup_or_null(name
);
1824 gs
->path
= xstrdup_or_null(path
);
1828 gs
->identifier
= oiddup(oid
);
1832 void grep_source_clear(struct grep_source
*gs
)
1834 FREE_AND_NULL(gs
->name
);
1835 FREE_AND_NULL(gs
->path
);
1836 FREE_AND_NULL(gs
->identifier
);
1837 grep_source_clear_data(gs
);
1840 void grep_source_clear_data(struct grep_source
*gs
)
1843 case GREP_SOURCE_FILE
:
1844 case GREP_SOURCE_OID
:
1845 /* these types own the buffer */
1846 free((char *)gs
->buf
);
1850 case GREP_SOURCE_BUF
:
1851 /* leave user-provided buf intact */
1856 static int grep_source_load_oid(struct grep_source
*gs
)
1858 enum object_type type
;
1860 gs
->buf
= repo_read_object_file(gs
->repo
, gs
->identifier
, &type
,
1863 return error(_("'%s': unable to read %s"),
1865 oid_to_hex(gs
->identifier
));
1869 static int grep_source_load_file(struct grep_source
*gs
)
1871 const char *filename
= gs
->identifier
;
1877 if (lstat(filename
, &st
) < 0) {
1879 if (errno
!= ENOENT
)
1880 error_errno(_("failed to stat '%s'"), filename
);
1883 if (!S_ISREG(st
.st_mode
))
1885 size
= xsize_t(st
.st_size
);
1886 i
= open(filename
, O_RDONLY
);
1889 data
= xmallocz(size
);
1890 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1891 error_errno(_("'%s': short read"), filename
);
1903 static int grep_source_load(struct grep_source
*gs
)
1909 case GREP_SOURCE_FILE
:
1910 return grep_source_load_file(gs
);
1911 case GREP_SOURCE_OID
:
1912 return grep_source_load_oid(gs
);
1913 case GREP_SOURCE_BUF
:
1914 return gs
->buf
? 0 : -1;
1916 BUG("invalid grep_source type to load");
1919 void grep_source_load_driver(struct grep_source
*gs
,
1920 struct index_state
*istate
)
1927 gs
->driver
= userdiff_find_by_path(istate
, gs
->path
);
1929 gs
->driver
= userdiff_find_by_name("default");
1933 static int grep_source_is_binary(struct grep_source
*gs
,
1934 struct index_state
*istate
)
1936 grep_source_load_driver(gs
, istate
);
1937 if (gs
->driver
->binary
!= -1)
1938 return gs
->driver
->binary
;
1940 if (!grep_source_load(gs
))
1941 return buffer_is_binary(gs
->buf
, gs
->size
);