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_35_OR_HIGHER
300 * Work around a JIT bug related to invalid Unicode character handling
302 * https://github.com/PCRE2Project/pcre2/commit/c21bd977547d
304 options
&= ~PCRE2_UCP
;
307 #ifndef GIT_PCRE2_VERSION_10_36_OR_HIGHER
308 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
309 if (PCRE2_MATCH_INVALID_UTF
&& options
& (PCRE2_UTF
| PCRE2_CASELESS
))
310 options
|= PCRE2_NO_START_OPTIMIZE
;
313 p
->pcre2_pattern
= pcre2_compile((PCRE2_SPTR
)p
->pattern
,
314 p
->patternlen
, options
, &error
, &erroffset
,
315 p
->pcre2_compile_context
);
317 if (p
->pcre2_pattern
) {
318 p
->pcre2_match_data
= pcre2_match_data_create_from_pattern(p
->pcre2_pattern
, p
->pcre2_general_context
);
319 if (!p
->pcre2_match_data
)
320 die("Couldn't allocate PCRE2 match data");
322 pcre2_get_error_message(error
, errbuf
, sizeof(errbuf
));
323 compile_regexp_failed(p
, (const char *)&errbuf
);
326 pcre2_config(PCRE2_CONFIG_JIT
, &p
->pcre2_jit_on
);
327 if (p
->pcre2_jit_on
) {
328 jitret
= pcre2_jit_compile(p
->pcre2_pattern
, PCRE2_JIT_COMPLETE
);
330 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p
->pattern
, jitret
);
333 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
334 * tells us whether the library itself supports JIT,
335 * but to see whether we're going to be actually using
336 * JIT we need to extract PCRE2_INFO_JITSIZE from the
337 * pattern *after* we do pcre2_jit_compile() above.
339 * This is because if the pattern contains the
340 * (*NO_JIT) verb (see pcre2syntax(3))
341 * pcre2_jit_compile() will exit early with 0. If we
342 * then proceed to call pcre2_jit_match() further down
343 * the line instead of pcre2_match() we'll either
344 * segfault (pre PCRE 10.31) or run into a fatal error
347 patinforet
= pcre2_pattern_info(p
->pcre2_pattern
, PCRE2_INFO_JITSIZE
, &jitsizearg
);
349 BUG("pcre2_pattern_info() failed: %d", patinforet
);
350 if (jitsizearg
== 0) {
357 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
358 regmatch_t
*match
, int eflags
)
362 PCRE2_UCHAR errbuf
[256];
364 if (eflags
& REG_NOTBOL
)
365 flags
|= PCRE2_NOTBOL
;
368 ret
= pcre2_jit_match(p
->pcre2_pattern
, (unsigned char *)line
,
369 eol
- line
, 0, flags
, p
->pcre2_match_data
,
372 ret
= pcre2_match(p
->pcre2_pattern
, (unsigned char *)line
,
373 eol
- line
, 0, flags
, p
->pcre2_match_data
,
376 if (ret
< 0 && ret
!= PCRE2_ERROR_NOMATCH
) {
377 pcre2_get_error_message(ret
, errbuf
, sizeof(errbuf
));
378 die("%s failed with error code %d: %s",
379 (p
->pcre2_jit_on
? "pcre2_jit_match" : "pcre2_match"), ret
,
383 ovector
= pcre2_get_ovector_pointer(p
->pcre2_match_data
);
385 match
->rm_so
= (int)ovector
[0];
386 match
->rm_eo
= (int)ovector
[1];
392 static void free_pcre2_pattern(struct grep_pat
*p
)
394 pcre2_compile_context_free(p
->pcre2_compile_context
);
395 pcre2_code_free(p
->pcre2_pattern
);
396 pcre2_match_data_free(p
->pcre2_match_data
);
397 #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
398 pcre2_maketables_free(p
->pcre2_general_context
, p
->pcre2_tables
);
400 free((void *)p
->pcre2_tables
);
402 pcre2_general_context_free(p
->pcre2_general_context
);
404 #else /* !USE_LIBPCRE2 */
405 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
407 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
410 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
411 regmatch_t
*match
, int eflags
)
416 static void free_pcre2_pattern(struct grep_pat
*p
)
420 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
422 struct strbuf sb
= STRBUF_INIT
;
426 basic_regex_quote_buf(&sb
, p
->pattern
);
427 if (opt
->ignore_case
)
428 regflags
|= REG_ICASE
;
429 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
433 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
434 compile_regexp_failed(p
, errbuf
);
437 #endif /* !USE_LIBPCRE2 */
439 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
442 int regflags
= REG_NEWLINE
;
444 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_UNSPECIFIED
)
445 opt
->pattern_type_option
= (opt
->extended_regexp_option
446 ? GREP_PATTERN_TYPE_ERE
447 : GREP_PATTERN_TYPE_BRE
);
449 p
->word_regexp
= opt
->word_regexp
;
450 p
->ignore_case
= opt
->ignore_case
;
451 p
->fixed
= opt
->pattern_type_option
== GREP_PATTERN_TYPE_FIXED
;
453 if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_PCRE
&&
454 memchr(p
->pattern
, 0, p
->patternlen
))
455 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
457 p
->is_fixed
= is_fixed(p
->pattern
, p
->patternlen
);
459 if (!p
->fixed
&& !p
->is_fixed
) {
460 const char *no_jit
= "(*NO_JIT)";
461 const int no_jit_len
= strlen(no_jit
);
462 if (starts_with(p
->pattern
, no_jit
) &&
463 is_fixed(p
->pattern
+ no_jit_len
,
464 p
->patternlen
- no_jit_len
))
468 if (p
->fixed
|| p
->is_fixed
) {
471 compile_pcre2_pattern(p
, opt
);
474 * E.g. t7811-grep-open.sh relies on the
475 * pattern being restored.
477 char *old_pattern
= p
->pattern
;
478 size_t old_patternlen
= p
->patternlen
;
479 struct strbuf sb
= STRBUF_INIT
;
482 * There is the PCRE2_LITERAL flag, but it's
483 * only in PCRE v2 10.30 and later. Needing to
484 * ifdef our way around that and dealing with
485 * it + PCRE2_MULTILINE being an error is more
486 * complex than just quoting this ourselves.
488 strbuf_add(&sb
, "\\Q", 2);
489 strbuf_add(&sb
, p
->pattern
, p
->patternlen
);
490 strbuf_add(&sb
, "\\E", 2);
493 p
->patternlen
= sb
.len
;
494 compile_pcre2_pattern(p
, opt
);
495 p
->pattern
= old_pattern
;
496 p
->patternlen
= old_patternlen
;
499 #else /* !USE_LIBPCRE2 */
500 compile_fixed_regexp(p
, opt
);
501 #endif /* !USE_LIBPCRE2 */
505 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_PCRE
) {
506 compile_pcre2_pattern(p
, opt
);
511 regflags
|= REG_ICASE
;
512 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_ERE
)
513 regflags
|= REG_EXTENDED
;
514 err
= regcomp(&p
->regexp
, p
->pattern
, regflags
);
517 regerror(err
, &p
->regexp
, errbuf
, 1024);
518 compile_regexp_failed(p
, errbuf
);
522 static struct grep_expr
*grep_not_expr(struct grep_expr
*expr
)
524 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
525 z
->node
= GREP_NODE_NOT
;
530 static struct grep_expr
*grep_binexp(enum grep_expr_node kind
,
531 struct grep_expr
*left
,
532 struct grep_expr
*right
)
534 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
536 z
->u
.binary
.left
= left
;
537 z
->u
.binary
.right
= right
;
541 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
543 return grep_binexp(GREP_NODE_OR
, left
, right
);
546 static struct grep_expr
*grep_and_expr(struct grep_expr
*left
, struct grep_expr
*right
)
548 return grep_binexp(GREP_NODE_AND
, left
, right
);
551 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
552 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
561 case GREP_PATTERN
: /* atom */
562 case GREP_PATTERN_HEAD
:
563 case GREP_PATTERN_BODY
:
565 x
->node
= GREP_NODE_ATOM
;
569 case GREP_OPEN_PAREN
:
571 x
= compile_pattern_or(list
);
572 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
573 die("unmatched parenthesis");
574 *list
= (*list
)->next
;
581 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
592 die("--not not followed by pattern expression");
594 x
= compile_pattern_not(list
);
596 die("--not followed by non pattern expression");
597 return grep_not_expr(x
);
599 return compile_pattern_atom(list
);
603 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
606 struct grep_expr
*x
, *y
;
608 x
= compile_pattern_not(list
);
610 if (p
&& p
->token
== GREP_AND
) {
612 die("--and not preceded by pattern expression");
614 die("--and not followed by pattern expression");
616 y
= compile_pattern_and(list
);
618 die("--and not followed by pattern expression");
619 return grep_and_expr(x
, y
);
624 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
627 struct grep_expr
*x
, *y
;
629 x
= compile_pattern_and(list
);
631 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
632 y
= compile_pattern_or(list
);
634 die("not a pattern expression %s", p
->pattern
);
635 return grep_or_expr(x
, y
);
640 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
642 return compile_pattern_or(list
);
645 static struct grep_expr
*grep_true_expr(void)
647 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
648 z
->node
= GREP_NODE_TRUE
;
652 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
655 struct grep_expr
*header_expr
;
656 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
657 enum grep_header_field fld
;
659 if (!opt
->header_list
)
662 for (p
= opt
->header_list
; p
; p
= p
->next
) {
663 if (p
->token
!= GREP_PATTERN_HEAD
)
664 BUG("a non-header pattern in grep header list.");
665 if (p
->field
< GREP_HEADER_FIELD_MIN
||
666 GREP_HEADER_FIELD_MAX
<= p
->field
)
667 BUG("unknown header field %d", p
->field
);
668 compile_regexp(p
, opt
);
671 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
672 header_group
[fld
] = NULL
;
674 for (p
= opt
->header_list
; p
; p
= p
->next
) {
676 struct grep_pat
*pp
= p
;
678 h
= compile_pattern_atom(&pp
);
679 if (!h
|| pp
!= p
->next
)
680 BUG("malformed header expr");
681 if (!header_group
[p
->field
]) {
682 header_group
[p
->field
] = h
;
685 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
690 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
691 if (!header_group
[fld
])
694 header_expr
= grep_true_expr();
695 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
700 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
702 struct grep_expr
*z
= x
;
705 assert(x
->node
== GREP_NODE_OR
);
706 if (x
->u
.binary
.right
&&
707 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
708 x
->u
.binary
.right
= y
;
711 x
= x
->u
.binary
.right
;
716 void compile_grep_patterns(struct grep_opt
*opt
)
719 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
722 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
724 case GREP_PATTERN
: /* atom */
725 case GREP_PATTERN_HEAD
:
726 case GREP_PATTERN_BODY
:
727 compile_regexp(p
, opt
);
735 if (opt
->all_match
|| opt
->no_body_match
|| header_expr
)
740 p
= opt
->pattern_list
;
742 opt
->pattern_expression
= compile_pattern_expr(&p
);
744 die("incomplete pattern expression: %s", p
->pattern
);
746 if (opt
->no_body_match
&& opt
->pattern_expression
)
747 opt
->pattern_expression
= grep_not_expr(opt
->pattern_expression
);
752 if (!opt
->pattern_expression
)
753 opt
->pattern_expression
= header_expr
;
754 else if (opt
->all_match
)
755 opt
->pattern_expression
= grep_splice_or(header_expr
,
756 opt
->pattern_expression
);
758 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
763 static void free_pattern_expr(struct grep_expr
*x
)
770 free_pattern_expr(x
->u
.unary
);
774 free_pattern_expr(x
->u
.binary
.left
);
775 free_pattern_expr(x
->u
.binary
.right
);
781 void free_grep_patterns(struct grep_opt
*opt
)
783 struct grep_pat
*p
, *n
;
785 for (p
= opt
->pattern_list
; p
; p
= n
) {
788 case GREP_PATTERN
: /* atom */
789 case GREP_PATTERN_HEAD
:
790 case GREP_PATTERN_BODY
:
791 if (p
->pcre2_pattern
)
792 free_pcre2_pattern(p
);
803 if (!opt
->pattern_expression
)
805 free_pattern_expr(opt
->pattern_expression
);
808 static const char *end_of_line(const char *cp
, unsigned long *left
)
810 unsigned long l
= *left
;
811 while (l
&& *cp
!= '\n') {
819 static int word_char(char ch
)
821 return isalnum(ch
) || ch
== '_';
824 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
827 if (want_color(opt
->color
) && color
&& color
[0]) {
828 opt
->output(opt
, color
, strlen(color
));
829 opt
->output(opt
, data
, size
);
830 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
832 opt
->output(opt
, data
, size
);
835 static void output_sep(struct grep_opt
*opt
, char sign
)
837 if (opt
->null_following_name
)
838 opt
->output(opt
, "\0", 1);
840 output_color(opt
, &sign
, 1, opt
->colors
[GREP_COLOR_SEP
]);
843 static void show_name(struct grep_opt
*opt
, const char *name
)
845 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
846 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
849 static int patmatch(struct grep_pat
*p
,
850 const char *line
, const char *eol
,
851 regmatch_t
*match
, int eflags
)
855 if (p
->pcre2_pattern
)
856 hit
= !pcre2match(p
, line
, eol
, match
, eflags
);
858 hit
= !regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
,
864 static void strip_timestamp(const char *bol
, const char **eol_p
)
866 const char *eol
= *eol_p
;
868 while (bol
< --eol
) {
881 { "committer ", 10 },
885 static int headerless_match_one_pattern(struct grep_pat
*p
,
886 const char *bol
, const char *eol
,
887 enum grep_context ctx
,
888 regmatch_t
*pmatch
, int eflags
)
891 const char *start
= bol
;
893 if ((p
->token
!= GREP_PATTERN
) &&
894 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
898 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
900 if (hit
&& p
->word_regexp
) {
901 if ((pmatch
[0].rm_so
< 0) ||
902 (eol
- bol
) < pmatch
[0].rm_so
||
903 (pmatch
[0].rm_eo
< 0) ||
904 (eol
- bol
) < pmatch
[0].rm_eo
)
905 die("regexp returned nonsense");
907 /* Match beginning must be either beginning of the
908 * line, or at word boundary (i.e. the last char must
909 * not be a word char). Similarly, match end must be
910 * either end of the line, or at word boundary
911 * (i.e. the next char must not be a word char).
913 if ( ((pmatch
[0].rm_so
== 0) ||
914 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
915 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
916 !word_char(bol
[pmatch
[0].rm_eo
])) )
921 /* Words consist of at least one character. */
922 if (pmatch
->rm_so
== pmatch
->rm_eo
)
925 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
926 /* There could be more than one match on the
927 * line, and the first match might not be
928 * strict word match. But later ones could be!
929 * Forward to the next possible start, i.e. the
930 * next position following a non-word char.
932 bol
= pmatch
[0].rm_so
+ bol
+ 1;
933 while (word_char(bol
[-1]) && bol
< eol
)
935 eflags
|= REG_NOTBOL
;
941 pmatch
[0].rm_so
+= bol
- start
;
942 pmatch
[0].rm_eo
+= bol
- start
;
947 static int match_one_pattern(struct grep_pat
*p
,
948 const char *bol
, const char *eol
,
949 enum grep_context ctx
, regmatch_t
*pmatch
,
955 if (p
->token
== GREP_PATTERN_HEAD
) {
956 assert(p
->field
< ARRAY_SIZE(header_field
));
957 field
= header_field
[p
->field
].field
;
958 len
= header_field
[p
->field
].len
;
959 if (strncmp(bol
, field
, len
))
964 case GREP_HEADER_AUTHOR
:
965 case GREP_HEADER_COMMITTER
:
966 strip_timestamp(bol
, &eol
);
973 return headerless_match_one_pattern(p
, bol
, eol
, ctx
, pmatch
, eflags
);
977 static int match_expr_eval(struct grep_opt
*opt
, struct grep_expr
*x
,
978 const char *bol
, const char *eol
,
979 enum grep_context ctx
, ssize_t
*col
,
980 ssize_t
*icol
, int collect_hits
)
991 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
,
993 if (h
&& (*col
< 0 || tmp
.rm_so
< *col
))
996 if (x
->u
.atom
->token
== GREP_PATTERN_BODY
)
1001 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1003 h
= !match_expr_eval(opt
, x
->u
.unary
, bol
, eol
, ctx
, icol
, col
,
1007 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1009 if (h
|| opt
->columnnum
) {
1011 * Don't short-circuit AND when given --column, since a
1012 * NOT earlier in the tree may turn this into an OR. In
1013 * this case, see the below comment.
1015 h
&= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
,
1020 if (!(collect_hits
|| opt
->columnnum
)) {
1022 * Don't short-circuit OR when given --column (or
1023 * collecting hits) to ensure we don't skip a later
1024 * child that would produce an earlier match.
1026 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
,
1027 ctx
, col
, icol
, 0) ||
1028 match_expr_eval(opt
, x
->u
.binary
.right
, bol
,
1029 eol
, ctx
, col
, icol
, 0));
1031 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1034 x
->u
.binary
.left
->hit
|= h
;
1035 h
|= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
, col
,
1036 icol
, collect_hits
);
1039 die("Unexpected node type (internal error) %d", x
->node
);
1046 static int match_expr(struct grep_opt
*opt
,
1047 const char *bol
, const char *eol
,
1048 enum grep_context ctx
, ssize_t
*col
,
1049 ssize_t
*icol
, int collect_hits
)
1051 struct grep_expr
*x
= opt
->pattern_expression
;
1052 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, col
, icol
, collect_hits
);
1055 static int match_line(struct grep_opt
*opt
,
1056 const char *bol
, const char *eol
,
1057 ssize_t
*col
, ssize_t
*icol
,
1058 enum grep_context ctx
, int collect_hits
)
1063 if (opt
->pattern_expression
)
1064 return match_expr(opt
, bol
, eol
, ctx
, col
, icol
,
1067 /* we do not call with collect_hits without being extended */
1068 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1070 if (match_one_pattern(p
, bol
, eol
, ctx
, &tmp
, 0)) {
1072 if (!opt
->columnnum
) {
1074 * Without --column, any single match on a line
1075 * is enough to know that it needs to be
1076 * printed. With --column, scan _all_ patterns
1077 * to find the earliest.
1081 if (*col
< 0 || tmp
.rm_so
< *col
)
1088 static int match_next_pattern(struct grep_pat
*p
,
1089 const char *bol
, const char *eol
,
1090 enum grep_context ctx
,
1091 regmatch_t
*pmatch
, int eflags
)
1095 if (!headerless_match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1097 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1099 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1100 if (match
.rm_so
> pmatch
->rm_so
)
1102 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1105 pmatch
->rm_so
= match
.rm_so
;
1106 pmatch
->rm_eo
= match
.rm_eo
;
1110 int grep_next_match(struct grep_opt
*opt
,
1111 const char *bol
, const char *eol
,
1112 enum grep_context ctx
, regmatch_t
*pmatch
,
1113 enum grep_header_field field
, int eflags
)
1118 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1120 for (p
= ((ctx
== GREP_CONTEXT_HEAD
)
1121 ? opt
->header_list
: opt
->pattern_list
);
1124 case GREP_PATTERN_HEAD
:
1125 if ((field
!= GREP_HEADER_FIELD_MAX
) &&
1126 (p
->field
!= field
))
1129 case GREP_PATTERN
: /* atom */
1130 case GREP_PATTERN_BODY
:
1131 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1142 static void show_line_header(struct grep_opt
*opt
, const char *name
,
1143 unsigned lno
, ssize_t cno
, char sign
)
1145 if (opt
->heading
&& opt
->last_shown
== 0) {
1146 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1147 opt
->output(opt
, "\n", 1);
1149 opt
->last_shown
= lno
;
1151 if (!opt
->heading
&& opt
->pathname
) {
1152 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1153 output_sep(opt
, sign
);
1157 xsnprintf(buf
, sizeof(buf
), "%d", lno
);
1158 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_LINENO
]);
1159 output_sep(opt
, sign
);
1162 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1163 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1164 * being called with a context line.
1166 if (opt
->columnnum
&& cno
) {
1168 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, (uintmax_t)cno
);
1169 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_COLUMNNO
]);
1170 output_sep(opt
, sign
);
1174 static void show_line(struct grep_opt
*opt
,
1175 const char *bol
, const char *eol
,
1176 const char *name
, unsigned lno
, ssize_t cno
, char sign
)
1178 int rest
= eol
- bol
;
1179 const char *match_color
= NULL
;
1180 const char *line_color
= NULL
;
1182 if (opt
->file_break
&& opt
->last_shown
== 0) {
1183 if (opt
->show_hunk_mark
)
1184 opt
->output(opt
, "\n", 1);
1185 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1186 if (opt
->last_shown
== 0) {
1187 if (opt
->show_hunk_mark
) {
1188 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1189 opt
->output(opt
, "\n", 1);
1191 } else if (lno
> opt
->last_shown
+ 1) {
1192 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1193 opt
->output(opt
, "\n", 1);
1196 if (!opt
->only_matching
) {
1198 * In case the line we're being called with contains more than
1199 * one match, leave printing each header to the loop below.
1201 show_line_header(opt
, name
, lno
, cno
, sign
);
1203 if (opt
->color
|| opt
->only_matching
) {
1205 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1210 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
1212 match_color
= opt
->colors
[GREP_COLOR_MATCH_CONTEXT
];
1214 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
1215 else if (sign
== '-')
1216 line_color
= opt
->colors
[GREP_COLOR_CONTEXT
];
1217 else if (sign
== '=')
1218 line_color
= opt
->colors
[GREP_COLOR_FUNCTION
];
1220 while (grep_next_match(opt
, bol
, eol
, ctx
, &match
,
1221 GREP_HEADER_FIELD_MAX
, eflags
)) {
1222 if (match
.rm_so
== match
.rm_eo
)
1225 if (opt
->only_matching
)
1226 show_line_header(opt
, name
, lno
, cno
, sign
);
1228 output_color(opt
, bol
, match
.rm_so
, line_color
);
1229 output_color(opt
, bol
+ match
.rm_so
,
1230 match
.rm_eo
- match
.rm_so
, match_color
);
1231 if (opt
->only_matching
)
1232 opt
->output(opt
, "\n", 1);
1235 rest
-= match
.rm_eo
;
1236 eflags
= REG_NOTBOL
;
1239 if (!opt
->only_matching
) {
1240 output_color(opt
, bol
, rest
, line_color
);
1241 opt
->output(opt
, "\n", 1);
1248 * This lock protects access to the gitattributes machinery, which is
1251 pthread_mutex_t grep_attr_mutex
;
1253 static inline void grep_attr_lock(void)
1256 pthread_mutex_lock(&grep_attr_mutex
);
1259 static inline void grep_attr_unlock(void)
1262 pthread_mutex_unlock(&grep_attr_mutex
);
1265 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
,
1266 const char *bol
, const char *eol
)
1268 xdemitconf_t
*xecfg
= opt
->priv
;
1269 if (xecfg
&& !xecfg
->find_func
) {
1270 grep_source_load_driver(gs
, opt
->repo
->index
);
1271 if (gs
->driver
->funcname
.pattern
) {
1272 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1273 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1275 xecfg
= opt
->priv
= NULL
;
1281 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1282 xecfg
->find_func_priv
) >= 0;
1287 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1292 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1293 const char *bol
, unsigned lno
)
1295 while (bol
> gs
->buf
) {
1296 const char *eol
= --bol
;
1298 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1302 if (lno
<= opt
->last_shown
)
1305 if (match_funcname(opt
, gs
, bol
, eol
)) {
1306 show_line(opt
, bol
, eol
, gs
->name
, lno
, 0, '=');
1312 static int is_empty_line(const char *bol
, const char *eol
);
1314 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1315 const char *bol
, const char *end
, unsigned lno
)
1317 unsigned cur
= lno
, from
= 1, funcname_lno
= 0, orig_from
;
1318 int funcname_needed
= !!opt
->funcname
, comment_needed
= 0;
1320 if (opt
->pre_context
< lno
)
1321 from
= lno
- opt
->pre_context
;
1322 if (from
<= opt
->last_shown
)
1323 from
= opt
->last_shown
+ 1;
1325 if (opt
->funcbody
) {
1326 if (match_funcname(opt
, gs
, bol
, end
))
1329 funcname_needed
= 1;
1330 from
= opt
->last_shown
+ 1;
1334 while (bol
> gs
->buf
&& cur
> from
) {
1335 const char *next_bol
= bol
;
1336 const char *eol
= --bol
;
1338 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1341 if (comment_needed
&& (is_empty_line(bol
, eol
) ||
1342 match_funcname(opt
, gs
, bol
, eol
))) {
1351 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1353 funcname_needed
= 0;
1361 /* We need to look even further back to find a function signature. */
1362 if (opt
->funcname
&& funcname_needed
)
1363 show_funcname_line(opt
, gs
, bol
, cur
);
1367 const char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1369 while (*eol
!= '\n')
1371 show_line(opt
, bol
, eol
, gs
->name
, cur
, 0, sign
);
1377 static int should_lookahead(struct grep_opt
*opt
)
1381 if (opt
->pattern_expression
)
1382 return 0; /* punt for too complex stuff */
1385 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1386 if (p
->token
!= GREP_PATTERN
)
1387 return 0; /* punt for "header only" and stuff */
1392 static int look_ahead(struct grep_opt
*opt
,
1393 unsigned long *left_p
,
1397 unsigned lno
= *lno_p
;
1398 const char *bol
= *bol_p
;
1400 const char *sp
, *last_bol
;
1401 regoff_t earliest
= -1;
1403 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1407 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1408 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1410 if (earliest
< 0 || m
.rm_so
< earliest
)
1415 *bol_p
= bol
+ *left_p
;
1419 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1420 ; /* find the beginning of the line */
1423 for (sp
= bol
; sp
< last_bol
; sp
++) {
1427 *left_p
-= last_bol
- bol
;
1433 static int fill_textconv_grep(struct repository
*r
,
1434 struct userdiff_driver
*driver
,
1435 struct grep_source
*gs
)
1437 struct diff_filespec
*df
;
1441 if (!driver
|| !driver
->textconv
)
1442 return grep_source_load(gs
);
1445 * The textconv interface is intimately tied to diff_filespecs, so we
1446 * have to pretend to be one. If we could unify the grep_source
1447 * and diff_filespec structs, this mess could just go away.
1449 df
= alloc_filespec(gs
->path
);
1451 case GREP_SOURCE_OID
:
1452 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1454 case GREP_SOURCE_FILE
:
1455 fill_filespec(df
, null_oid(), 0, 0100644);
1458 BUG("attempt to textconv something without a path?");
1462 * fill_textconv is not remotely thread-safe; it modifies the global
1463 * diff tempfile structure, writes to the_repo's odb and might
1464 * internally call thread-unsafe functions such as the
1465 * prepare_packed_git() lazy-initializator. Because of the last two, we
1466 * must ensure mutual exclusion between this call and the object reading
1467 * API, thus we use obj_read_lock() here.
1469 * TODO: allowing text conversion to run in parallel with object
1470 * reading operations might increase performance in the multithreaded
1471 * non-worktreee git-grep with --textconv.
1474 size
= fill_textconv(r
, driver
, df
, &buf
);
1479 * The normal fill_textconv usage by the diff machinery would just keep
1480 * the textconv'd buf separate from the diff_filespec. But much of the
1481 * grep code passes around a grep_source and assumes that its "buf"
1482 * pointer is the beginning of the thing we are searching. So let's
1483 * install our textconv'd version into the grep_source, taking care not
1484 * to leak any existing buffer.
1486 grep_source_clear_data(gs
);
1493 static int is_empty_line(const char *bol
, const char *eol
)
1495 while (bol
< eol
&& isspace(*bol
))
1500 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1503 const char *peek_bol
= NULL
;
1506 unsigned last_hit
= 0;
1507 int binary_match_only
= 0;
1509 int try_lookahead
= 0;
1510 int show_function
= 0;
1511 struct userdiff_driver
*textconv
= NULL
;
1512 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1515 if (!opt
->status_only
&& gs
->name
== NULL
)
1516 BUG("grep call which could print a name requires "
1517 "grep_source.name be non-NULL");
1520 opt
->output
= std_output
;
1522 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1524 /* Show hunk marks, except for the first file. */
1525 if (opt
->last_shown
)
1526 opt
->show_hunk_mark
= 1;
1528 * If we're using threads then we can't easily identify
1529 * the first file. Always put hunk marks in that case
1530 * and skip the very first one later in work_done().
1532 if (opt
->output
!= std_output
)
1533 opt
->show_hunk_mark
= 1;
1535 opt
->last_shown
= 0;
1537 if (opt
->allow_textconv
) {
1538 grep_source_load_driver(gs
, opt
->repo
->index
);
1540 * We might set up the shared textconv cache data here, which
1541 * is not thread-safe. Also, get_oid_with_context() and
1542 * parse_object() might be internally called. As they are not
1543 * currently thread-safe and might be racy with object reading,
1544 * obj_read_lock() must be called.
1548 textconv
= userdiff_get_textconv(opt
->repo
, gs
->driver
);
1554 * We know the result of a textconv is text, so we only have to care
1555 * about binary handling if we are not using it.
1558 switch (opt
->binary
) {
1559 case GREP_BINARY_DEFAULT
:
1560 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1561 binary_match_only
= 1;
1563 case GREP_BINARY_NOMATCH
:
1564 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1565 return 0; /* Assume unmatch */
1567 case GREP_BINARY_TEXT
:
1570 BUG("unknown binary handling mode");
1574 memset(&xecfg
, 0, sizeof(xecfg
));
1577 try_lookahead
= should_lookahead(opt
);
1579 if (fill_textconv_grep(opt
->repo
, textconv
, gs
) < 0)
1588 ssize_t col
= -1, icol
= -1;
1591 * look_ahead() skips quickly to the line that possibly
1592 * has the next hit; don't call it if we need to do
1593 * something more than just skipping the current line
1594 * in response to an unmatch for the current line. E.g.
1595 * inside a post-context window, we will show the current
1596 * line as a context around the previous hit when it
1601 && (show_function
||
1602 lno
<= last_hit
+ opt
->post_context
))
1603 && look_ahead(opt
, &left
, &lno
, &bol
))
1605 eol
= end_of_line(bol
, &left
);
1607 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1608 ctx
= GREP_CONTEXT_BODY
;
1610 hit
= match_line(opt
, bol
, eol
, &col
, &icol
, ctx
, collect_hits
);
1615 /* "grep -v -e foo -e bla" should list lines
1616 * that do not have either, so inversion should
1621 if (opt
->unmatch_name_only
) {
1626 if (hit
&& (opt
->max_count
< 0 || count
< opt
->max_count
)) {
1628 if (opt
->status_only
)
1630 if (opt
->name_only
) {
1631 show_name(opt
, gs
->name
);
1636 if (binary_match_only
) {
1637 opt
->output(opt
, "Binary file ", 12);
1638 output_color(opt
, gs
->name
, strlen(gs
->name
),
1639 opt
->colors
[GREP_COLOR_FILENAME
]);
1640 opt
->output(opt
, " matches\n", 9);
1643 /* Hit at this line. If we haven't shown the
1644 * pre-context lines, we would need to show them.
1646 if (opt
->pre_context
|| opt
->funcbody
)
1647 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1648 else if (opt
->funcname
)
1649 show_funcname_line(opt
, gs
, bol
, lno
);
1650 cno
= opt
->invert
? icol
: col
;
1653 * A negative cno indicates that there was no
1654 * match on the line. We are thus inverted and
1655 * being asked to show all lines that _don't_
1656 * match a given expression. Therefore, set cno
1657 * to 0 to suggest the whole line matches.
1661 show_line(opt
, bol
, eol
, gs
->name
, lno
, cno
+ 1, ':');
1667 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1668 unsigned long peek_left
= left
;
1669 const char *peek_eol
= eol
;
1672 * Trailing empty lines are not interesting.
1673 * Peek past them to see if they belong to the
1674 * body of the current function.
1677 while (is_empty_line(peek_bol
, peek_eol
)) {
1678 peek_bol
= peek_eol
+ 1;
1679 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1682 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1685 if (show_function
||
1686 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1687 /* If the last hit is within the post context,
1688 * we need to show this line.
1690 show_line(opt
, bol
, eol
, gs
->name
, lno
, col
+ 1, '-');
1704 if (opt
->status_only
)
1705 return opt
->unmatch_name_only
;
1706 if (opt
->unmatch_name_only
) {
1707 /* We did not see any hit, so we want to show this */
1708 show_name(opt
, gs
->name
);
1712 xdiff_clear_find_func(&xecfg
);
1716 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1717 * which feels mostly useless but sometimes useful. Maybe
1718 * make it another option? For now suppress them.
1720 if (opt
->count
&& count
) {
1722 if (opt
->pathname
) {
1723 output_color(opt
, gs
->name
, strlen(gs
->name
),
1724 opt
->colors
[GREP_COLOR_FILENAME
]);
1725 output_sep(opt
, ':');
1727 xsnprintf(buf
, sizeof(buf
), "%u\n", count
);
1728 opt
->output(opt
, buf
, strlen(buf
));
1734 static void clr_hit_marker(struct grep_expr
*x
)
1736 /* All-hit markers are meaningful only at the very top level
1741 if (x
->node
!= GREP_NODE_OR
)
1743 x
->u
.binary
.left
->hit
= 0;
1744 x
= x
->u
.binary
.right
;
1748 static int chk_hit_marker(struct grep_expr
*x
)
1750 /* Top level nodes have hit markers. See if they all are hits */
1752 if (x
->node
!= GREP_NODE_OR
)
1754 if (!x
->u
.binary
.left
->hit
)
1756 x
= x
->u
.binary
.right
;
1760 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1763 * we do not have to do the two-pass grep when we do not check
1764 * buffer-wide "all-match".
1766 if (!opt
->all_match
&& !opt
->no_body_match
)
1767 return grep_source_1(opt
, gs
, 0);
1769 /* Otherwise the toplevel "or" terms hit a bit differently.
1770 * We first clear hit markers from them.
1772 clr_hit_marker(opt
->pattern_expression
);
1774 grep_source_1(opt
, gs
, 1);
1776 if (opt
->all_match
&& !chk_hit_marker(opt
->pattern_expression
))
1778 if (opt
->no_body_match
&& opt
->body_hit
)
1781 return grep_source_1(opt
, gs
, 0);
1784 static void grep_source_init_buf(struct grep_source
*gs
,
1788 gs
->type
= GREP_SOURCE_BUF
;
1794 gs
->identifier
= NULL
;
1797 int grep_buffer(struct grep_opt
*opt
, const char *buf
, unsigned long size
)
1799 struct grep_source gs
;
1802 grep_source_init_buf(&gs
, buf
, size
);
1804 r
= grep_source(opt
, &gs
);
1806 grep_source_clear(&gs
);
1810 void grep_source_init_file(struct grep_source
*gs
, const char *name
,
1813 gs
->type
= GREP_SOURCE_FILE
;
1814 gs
->name
= xstrdup_or_null(name
);
1815 gs
->path
= xstrdup_or_null(path
);
1819 gs
->identifier
= xstrdup(path
);
1822 void grep_source_init_oid(struct grep_source
*gs
, const char *name
,
1823 const char *path
, const struct object_id
*oid
,
1824 struct repository
*repo
)
1826 gs
->type
= GREP_SOURCE_OID
;
1827 gs
->name
= xstrdup_or_null(name
);
1828 gs
->path
= xstrdup_or_null(path
);
1832 gs
->identifier
= oiddup(oid
);
1836 void grep_source_clear(struct grep_source
*gs
)
1838 FREE_AND_NULL(gs
->name
);
1839 FREE_AND_NULL(gs
->path
);
1840 FREE_AND_NULL(gs
->identifier
);
1841 grep_source_clear_data(gs
);
1844 void grep_source_clear_data(struct grep_source
*gs
)
1847 case GREP_SOURCE_FILE
:
1848 case GREP_SOURCE_OID
:
1849 /* these types own the buffer */
1850 free((char *)gs
->buf
);
1854 case GREP_SOURCE_BUF
:
1855 /* leave user-provided buf intact */
1860 static int grep_source_load_oid(struct grep_source
*gs
)
1862 enum object_type type
;
1864 gs
->buf
= repo_read_object_file(gs
->repo
, gs
->identifier
, &type
,
1867 return error(_("'%s': unable to read %s"),
1869 oid_to_hex(gs
->identifier
));
1873 static int grep_source_load_file(struct grep_source
*gs
)
1875 const char *filename
= gs
->identifier
;
1881 if (lstat(filename
, &st
) < 0) {
1883 if (errno
!= ENOENT
)
1884 error_errno(_("failed to stat '%s'"), filename
);
1887 if (!S_ISREG(st
.st_mode
))
1889 size
= xsize_t(st
.st_size
);
1890 i
= open(filename
, O_RDONLY
);
1893 data
= xmallocz(size
);
1894 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1895 error_errno(_("'%s': short read"), filename
);
1907 static int grep_source_load(struct grep_source
*gs
)
1913 case GREP_SOURCE_FILE
:
1914 return grep_source_load_file(gs
);
1915 case GREP_SOURCE_OID
:
1916 return grep_source_load_oid(gs
);
1917 case GREP_SOURCE_BUF
:
1918 return gs
->buf
? 0 : -1;
1920 BUG("invalid grep_source type to load");
1923 void grep_source_load_driver(struct grep_source
*gs
,
1924 struct index_state
*istate
)
1931 gs
->driver
= userdiff_find_by_path(istate
, gs
->path
);
1933 gs
->driver
= userdiff_find_by_name("default");
1937 static int grep_source_is_binary(struct grep_source
*gs
,
1938 struct index_state
*istate
)
1940 grep_source_load_driver(gs
, istate
);
1941 if (gs
->driver
->binary
!= -1)
1942 return gs
->driver
->binary
;
1944 if (!grep_source_load(gs
))
1945 return buffer_is_binary(gs
->buf
, gs
->size
);