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 int pcre2_jit_functional(void)
267 static int jit_working
= -1;
272 if (jit_working
!= -1)
276 * Try to JIT compile a simple pattern to probe if the JIT is
277 * working in general. It might fail for systems where creating
278 * memory mappings for runtime code generation is restricted.
280 code
= pcre2_compile((PCRE2_SPTR
)".", 1, 0, &err
, &off
, NULL
);
284 jit_working
= pcre2_jit_compile(code
, PCRE2_JIT_COMPLETE
) == 0;
285 pcre2_code_free(code
);
290 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
293 PCRE2_UCHAR errbuf
[256];
294 PCRE2_SIZE erroffset
;
295 int options
= PCRE2_MULTILINE
;
299 int literal
= !opt
->ignore_case
&& (p
->fixed
|| p
->is_fixed
);
302 * Call pcre2_general_context_create() before calling any
303 * other pcre2_*(). It sets up our malloc()/free() functions
304 * with which everything else is allocated.
306 p
->pcre2_general_context
= pcre2_general_context_create(
307 pcre2_malloc
, pcre2_free
, NULL
);
308 if (!p
->pcre2_general_context
)
309 die("Couldn't allocate PCRE2 general context");
311 if (opt
->ignore_case
) {
312 if (!opt
->ignore_locale
&& has_non_ascii(p
->pattern
)) {
313 p
->pcre2_tables
= pcre2_maketables(p
->pcre2_general_context
);
314 p
->pcre2_compile_context
= pcre2_compile_context_create(p
->pcre2_general_context
);
315 pcre2_set_character_tables(p
->pcre2_compile_context
,
318 options
|= PCRE2_CASELESS
;
320 if (!opt
->ignore_locale
&& is_utf8_locale() && !literal
)
321 options
|= (PCRE2_UTF
| PCRE2_UCP
| PCRE2_MATCH_INVALID_UTF
);
323 #ifndef GIT_PCRE2_VERSION_10_36_OR_HIGHER
324 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
325 if (PCRE2_MATCH_INVALID_UTF
&& options
& (PCRE2_UTF
| PCRE2_CASELESS
))
326 options
|= PCRE2_NO_START_OPTIMIZE
;
329 p
->pcre2_pattern
= pcre2_compile((PCRE2_SPTR
)p
->pattern
,
330 p
->patternlen
, options
, &error
, &erroffset
,
331 p
->pcre2_compile_context
);
333 if (p
->pcre2_pattern
) {
334 p
->pcre2_match_data
= pcre2_match_data_create_from_pattern(p
->pcre2_pattern
, p
->pcre2_general_context
);
335 if (!p
->pcre2_match_data
)
336 die("Couldn't allocate PCRE2 match data");
338 pcre2_get_error_message(error
, errbuf
, sizeof(errbuf
));
339 compile_regexp_failed(p
, (const char *)&errbuf
);
342 pcre2_config(PCRE2_CONFIG_JIT
, &p
->pcre2_jit_on
);
343 if (p
->pcre2_jit_on
) {
344 jitret
= pcre2_jit_compile(p
->pcre2_pattern
, PCRE2_JIT_COMPLETE
);
345 if (jitret
== PCRE2_ERROR_NOMEMORY
&& !pcre2_jit_functional()) {
347 * Even though pcre2_config(PCRE2_CONFIG_JIT, ...)
348 * indicated JIT support, the library might still
349 * fail to generate JIT code for various reasons,
350 * e.g. when SELinux's 'deny_execmem' or PaX's
351 * MPROTECT prevent creating W|X memory mappings.
353 * Instead of faling hard, fall back to interpreter
354 * mode, just as if the pattern was prefixed with
360 int need_clip
= p
->patternlen
> 64;
361 int clip_len
= need_clip
? 64 : p
->patternlen
;
362 die("Couldn't JIT the PCRE2 pattern '%.*s'%s, got '%d'%s",
363 clip_len
, p
->pattern
, need_clip
? "..." : "", jitret
,
364 pcre2_jit_functional()
365 ? "\nPerhaps prefix (*NO_JIT) to your pattern?"
370 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
371 * tells us whether the library itself supports JIT,
372 * but to see whether we're going to be actually using
373 * JIT we need to extract PCRE2_INFO_JITSIZE from the
374 * pattern *after* we do pcre2_jit_compile() above.
376 * This is because if the pattern contains the
377 * (*NO_JIT) verb (see pcre2syntax(3))
378 * pcre2_jit_compile() will exit early with 0. If we
379 * then proceed to call pcre2_jit_match() further down
380 * the line instead of pcre2_match() we'll either
381 * segfault (pre PCRE 10.31) or run into a fatal error
384 patinforet
= pcre2_pattern_info(p
->pcre2_pattern
, PCRE2_INFO_JITSIZE
, &jitsizearg
);
386 BUG("pcre2_pattern_info() failed: %d", patinforet
);
387 if (jitsizearg
== 0) {
394 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
395 regmatch_t
*match
, int eflags
)
399 PCRE2_UCHAR errbuf
[256];
401 if (eflags
& REG_NOTBOL
)
402 flags
|= PCRE2_NOTBOL
;
405 ret
= pcre2_jit_match(p
->pcre2_pattern
, (unsigned char *)line
,
406 eol
- line
, 0, flags
, p
->pcre2_match_data
,
409 ret
= pcre2_match(p
->pcre2_pattern
, (unsigned char *)line
,
410 eol
- line
, 0, flags
, p
->pcre2_match_data
,
413 if (ret
< 0 && ret
!= PCRE2_ERROR_NOMATCH
) {
414 pcre2_get_error_message(ret
, errbuf
, sizeof(errbuf
));
415 die("%s failed with error code %d: %s",
416 (p
->pcre2_jit_on
? "pcre2_jit_match" : "pcre2_match"), ret
,
420 ovector
= pcre2_get_ovector_pointer(p
->pcre2_match_data
);
422 match
->rm_so
= (int)ovector
[0];
423 match
->rm_eo
= (int)ovector
[1];
429 static void free_pcre2_pattern(struct grep_pat
*p
)
431 pcre2_compile_context_free(p
->pcre2_compile_context
);
432 pcre2_code_free(p
->pcre2_pattern
);
433 pcre2_match_data_free(p
->pcre2_match_data
);
434 #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
435 pcre2_maketables_free(p
->pcre2_general_context
, p
->pcre2_tables
);
437 free((void *)p
->pcre2_tables
);
439 pcre2_general_context_free(p
->pcre2_general_context
);
441 #else /* !USE_LIBPCRE2 */
442 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
444 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
447 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
448 regmatch_t
*match
, int eflags
)
453 static void free_pcre2_pattern(struct grep_pat
*p
)
457 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
459 struct strbuf sb
= STRBUF_INIT
;
463 basic_regex_quote_buf(&sb
, p
->pattern
);
464 if (opt
->ignore_case
)
465 regflags
|= REG_ICASE
;
466 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
470 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
471 compile_regexp_failed(p
, errbuf
);
474 #endif /* !USE_LIBPCRE2 */
476 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
479 int regflags
= REG_NEWLINE
;
481 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_UNSPECIFIED
)
482 opt
->pattern_type_option
= (opt
->extended_regexp_option
483 ? GREP_PATTERN_TYPE_ERE
484 : GREP_PATTERN_TYPE_BRE
);
486 p
->word_regexp
= opt
->word_regexp
;
487 p
->ignore_case
= opt
->ignore_case
;
488 p
->fixed
= opt
->pattern_type_option
== GREP_PATTERN_TYPE_FIXED
;
490 if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_PCRE
&&
491 memchr(p
->pattern
, 0, p
->patternlen
))
492 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
494 p
->is_fixed
= is_fixed(p
->pattern
, p
->patternlen
);
496 if (!p
->fixed
&& !p
->is_fixed
) {
497 const char *no_jit
= "(*NO_JIT)";
498 const int no_jit_len
= strlen(no_jit
);
499 if (starts_with(p
->pattern
, no_jit
) &&
500 is_fixed(p
->pattern
+ no_jit_len
,
501 p
->patternlen
- no_jit_len
))
505 if (p
->fixed
|| p
->is_fixed
) {
508 compile_pcre2_pattern(p
, opt
);
511 * E.g. t7811-grep-open.sh relies on the
512 * pattern being restored.
514 char *old_pattern
= p
->pattern
;
515 size_t old_patternlen
= p
->patternlen
;
516 struct strbuf sb
= STRBUF_INIT
;
519 * There is the PCRE2_LITERAL flag, but it's
520 * only in PCRE v2 10.30 and later. Needing to
521 * ifdef our way around that and dealing with
522 * it + PCRE2_MULTILINE being an error is more
523 * complex than just quoting this ourselves.
525 strbuf_add(&sb
, "\\Q", 2);
526 strbuf_add(&sb
, p
->pattern
, p
->patternlen
);
527 strbuf_add(&sb
, "\\E", 2);
530 p
->patternlen
= sb
.len
;
531 compile_pcre2_pattern(p
, opt
);
532 p
->pattern
= old_pattern
;
533 p
->patternlen
= old_patternlen
;
536 #else /* !USE_LIBPCRE2 */
537 compile_fixed_regexp(p
, opt
);
538 #endif /* !USE_LIBPCRE2 */
542 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_PCRE
) {
543 compile_pcre2_pattern(p
, opt
);
548 regflags
|= REG_ICASE
;
549 if (opt
->pattern_type_option
== GREP_PATTERN_TYPE_ERE
)
550 regflags
|= REG_EXTENDED
;
551 err
= regcomp(&p
->regexp
, p
->pattern
, regflags
);
554 regerror(err
, &p
->regexp
, errbuf
, 1024);
555 compile_regexp_failed(p
, errbuf
);
559 static struct grep_expr
*grep_not_expr(struct grep_expr
*expr
)
561 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
562 z
->node
= GREP_NODE_NOT
;
567 static struct grep_expr
*grep_binexp(enum grep_expr_node kind
,
568 struct grep_expr
*left
,
569 struct grep_expr
*right
)
571 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
573 z
->u
.binary
.left
= left
;
574 z
->u
.binary
.right
= right
;
578 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
580 return grep_binexp(GREP_NODE_OR
, left
, right
);
583 static struct grep_expr
*grep_and_expr(struct grep_expr
*left
, struct grep_expr
*right
)
585 return grep_binexp(GREP_NODE_AND
, left
, right
);
588 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
589 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
598 case GREP_PATTERN
: /* atom */
599 case GREP_PATTERN_HEAD
:
600 case GREP_PATTERN_BODY
:
602 x
->node
= GREP_NODE_ATOM
;
606 case GREP_OPEN_PAREN
:
608 x
= compile_pattern_or(list
);
609 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
610 die("unmatched parenthesis");
611 *list
= (*list
)->next
;
618 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
629 die("--not not followed by pattern expression");
631 x
= compile_pattern_not(list
);
633 die("--not followed by non pattern expression");
634 return grep_not_expr(x
);
636 return compile_pattern_atom(list
);
640 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
643 struct grep_expr
*x
, *y
;
645 x
= compile_pattern_not(list
);
647 if (p
&& p
->token
== GREP_AND
) {
649 die("--and not preceded by pattern expression");
651 die("--and not followed by pattern expression");
653 y
= compile_pattern_and(list
);
655 die("--and not followed by pattern expression");
656 return grep_and_expr(x
, y
);
661 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
664 struct grep_expr
*x
, *y
;
666 x
= compile_pattern_and(list
);
668 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
669 y
= compile_pattern_or(list
);
671 die("not a pattern expression %s", p
->pattern
);
672 return grep_or_expr(x
, y
);
677 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
679 return compile_pattern_or(list
);
682 static struct grep_expr
*grep_true_expr(void)
684 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
685 z
->node
= GREP_NODE_TRUE
;
689 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
692 struct grep_expr
*header_expr
;
693 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
694 enum grep_header_field fld
;
696 if (!opt
->header_list
)
699 for (p
= opt
->header_list
; p
; p
= p
->next
) {
700 if (p
->token
!= GREP_PATTERN_HEAD
)
701 BUG("a non-header pattern in grep header list.");
702 if (p
->field
< GREP_HEADER_FIELD_MIN
||
703 GREP_HEADER_FIELD_MAX
<= p
->field
)
704 BUG("unknown header field %d", p
->field
);
705 compile_regexp(p
, opt
);
708 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
709 header_group
[fld
] = NULL
;
711 for (p
= opt
->header_list
; p
; p
= p
->next
) {
713 struct grep_pat
*pp
= p
;
715 h
= compile_pattern_atom(&pp
);
716 if (!h
|| pp
!= p
->next
)
717 BUG("malformed header expr");
718 if (!header_group
[p
->field
]) {
719 header_group
[p
->field
] = h
;
722 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
727 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
728 if (!header_group
[fld
])
731 header_expr
= grep_true_expr();
732 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
737 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
739 struct grep_expr
*z
= x
;
742 assert(x
->node
== GREP_NODE_OR
);
743 if (x
->u
.binary
.right
&&
744 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
745 x
->u
.binary
.right
= y
;
748 x
= x
->u
.binary
.right
;
753 void compile_grep_patterns(struct grep_opt
*opt
)
756 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
759 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
761 case GREP_PATTERN
: /* atom */
762 case GREP_PATTERN_HEAD
:
763 case GREP_PATTERN_BODY
:
764 compile_regexp(p
, opt
);
772 if (opt
->all_match
|| opt
->no_body_match
|| header_expr
)
777 p
= opt
->pattern_list
;
779 opt
->pattern_expression
= compile_pattern_expr(&p
);
781 die("incomplete pattern expression: %s", p
->pattern
);
783 if (opt
->no_body_match
&& opt
->pattern_expression
)
784 opt
->pattern_expression
= grep_not_expr(opt
->pattern_expression
);
789 if (!opt
->pattern_expression
)
790 opt
->pattern_expression
= header_expr
;
791 else if (opt
->all_match
)
792 opt
->pattern_expression
= grep_splice_or(header_expr
,
793 opt
->pattern_expression
);
795 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
800 static void free_pattern_expr(struct grep_expr
*x
)
807 free_pattern_expr(x
->u
.unary
);
811 free_pattern_expr(x
->u
.binary
.left
);
812 free_pattern_expr(x
->u
.binary
.right
);
818 void free_grep_patterns(struct grep_opt
*opt
)
820 struct grep_pat
*p
, *n
;
822 for (p
= opt
->pattern_list
; p
; p
= n
) {
825 case GREP_PATTERN
: /* atom */
826 case GREP_PATTERN_HEAD
:
827 case GREP_PATTERN_BODY
:
828 if (p
->pcre2_pattern
)
829 free_pcre2_pattern(p
);
840 if (!opt
->pattern_expression
)
842 free_pattern_expr(opt
->pattern_expression
);
845 static const char *end_of_line(const char *cp
, unsigned long *left
)
847 unsigned long l
= *left
;
848 while (l
&& *cp
!= '\n') {
856 static int word_char(char ch
)
858 return isalnum(ch
) || ch
== '_';
861 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
864 if (want_color(opt
->color
) && color
&& color
[0]) {
865 opt
->output(opt
, color
, strlen(color
));
866 opt
->output(opt
, data
, size
);
867 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
869 opt
->output(opt
, data
, size
);
872 static void output_sep(struct grep_opt
*opt
, char sign
)
874 if (opt
->null_following_name
)
875 opt
->output(opt
, "\0", 1);
877 output_color(opt
, &sign
, 1, opt
->colors
[GREP_COLOR_SEP
]);
880 static void show_name(struct grep_opt
*opt
, const char *name
)
882 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
883 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
886 static int patmatch(struct grep_pat
*p
,
887 const char *line
, const char *eol
,
888 regmatch_t
*match
, int eflags
)
892 if (p
->pcre2_pattern
)
893 hit
= !pcre2match(p
, line
, eol
, match
, eflags
);
895 hit
= !regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
,
901 static void strip_timestamp(const char *bol
, const char **eol_p
)
903 const char *eol
= *eol_p
;
905 while (bol
< --eol
) {
918 { "committer ", 10 },
922 static int headerless_match_one_pattern(struct grep_pat
*p
,
923 const char *bol
, const char *eol
,
924 enum grep_context ctx
,
925 regmatch_t
*pmatch
, int eflags
)
928 const char *start
= bol
;
930 if ((p
->token
!= GREP_PATTERN
) &&
931 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
935 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
937 if (hit
&& p
->word_regexp
) {
938 if ((pmatch
[0].rm_so
< 0) ||
939 (eol
- bol
) < pmatch
[0].rm_so
||
940 (pmatch
[0].rm_eo
< 0) ||
941 (eol
- bol
) < pmatch
[0].rm_eo
)
942 die("regexp returned nonsense");
944 /* Match beginning must be either beginning of the
945 * line, or at word boundary (i.e. the last char must
946 * not be a word char). Similarly, match end must be
947 * either end of the line, or at word boundary
948 * (i.e. the next char must not be a word char).
950 if ( ((pmatch
[0].rm_so
== 0) ||
951 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
952 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
953 !word_char(bol
[pmatch
[0].rm_eo
])) )
958 /* Words consist of at least one character. */
959 if (pmatch
->rm_so
== pmatch
->rm_eo
)
962 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
963 /* There could be more than one match on the
964 * line, and the first match might not be
965 * strict word match. But later ones could be!
966 * Forward to the next possible start, i.e. the
967 * next position following a non-word char.
969 bol
= pmatch
[0].rm_so
+ bol
+ 1;
970 while (word_char(bol
[-1]) && bol
< eol
)
972 eflags
|= REG_NOTBOL
;
978 pmatch
[0].rm_so
+= bol
- start
;
979 pmatch
[0].rm_eo
+= bol
- start
;
984 static int match_one_pattern(struct grep_pat
*p
,
985 const char *bol
, const char *eol
,
986 enum grep_context ctx
, regmatch_t
*pmatch
,
992 if (p
->token
== GREP_PATTERN_HEAD
) {
993 assert(p
->field
< ARRAY_SIZE(header_field
));
994 field
= header_field
[p
->field
].field
;
995 len
= header_field
[p
->field
].len
;
996 if (strncmp(bol
, field
, len
))
1001 case GREP_HEADER_AUTHOR
:
1002 case GREP_HEADER_COMMITTER
:
1003 strip_timestamp(bol
, &eol
);
1010 return headerless_match_one_pattern(p
, bol
, eol
, ctx
, pmatch
, eflags
);
1014 static int match_expr_eval(struct grep_opt
*opt
, struct grep_expr
*x
,
1015 const char *bol
, const char *eol
,
1016 enum grep_context ctx
, ssize_t
*col
,
1017 ssize_t
*icol
, int collect_hits
)
1022 case GREP_NODE_TRUE
:
1025 case GREP_NODE_ATOM
:
1028 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
,
1030 if (h
&& (*col
< 0 || tmp
.rm_so
< *col
))
1033 if (x
->u
.atom
->token
== GREP_PATTERN_BODY
)
1038 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1040 h
= !match_expr_eval(opt
, x
->u
.unary
, bol
, eol
, ctx
, icol
, col
,
1044 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1046 if (h
|| opt
->columnnum
) {
1048 * Don't short-circuit AND when given --column, since a
1049 * NOT earlier in the tree may turn this into an OR. In
1050 * this case, see the below comment.
1052 h
&= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
,
1057 if (!(collect_hits
|| opt
->columnnum
)) {
1059 * Don't short-circuit OR when given --column (or
1060 * collecting hits) to ensure we don't skip a later
1061 * child that would produce an earlier match.
1063 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
,
1064 ctx
, col
, icol
, 0) ||
1065 match_expr_eval(opt
, x
->u
.binary
.right
, bol
,
1066 eol
, ctx
, col
, icol
, 0));
1068 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1071 x
->u
.binary
.left
->hit
|= h
;
1072 h
|= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
, col
,
1073 icol
, collect_hits
);
1076 die("Unexpected node type (internal error) %d", x
->node
);
1083 static int match_expr(struct grep_opt
*opt
,
1084 const char *bol
, const char *eol
,
1085 enum grep_context ctx
, ssize_t
*col
,
1086 ssize_t
*icol
, int collect_hits
)
1088 struct grep_expr
*x
= opt
->pattern_expression
;
1089 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, col
, icol
, collect_hits
);
1092 static int match_line(struct grep_opt
*opt
,
1093 const char *bol
, const char *eol
,
1094 ssize_t
*col
, ssize_t
*icol
,
1095 enum grep_context ctx
, int collect_hits
)
1100 if (opt
->pattern_expression
)
1101 return match_expr(opt
, bol
, eol
, ctx
, col
, icol
,
1104 /* we do not call with collect_hits without being extended */
1105 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1107 if (match_one_pattern(p
, bol
, eol
, ctx
, &tmp
, 0)) {
1109 if (!opt
->columnnum
) {
1111 * Without --column, any single match on a line
1112 * is enough to know that it needs to be
1113 * printed. With --column, scan _all_ patterns
1114 * to find the earliest.
1118 if (*col
< 0 || tmp
.rm_so
< *col
)
1125 static int match_next_pattern(struct grep_pat
*p
,
1126 const char *bol
, const char *eol
,
1127 enum grep_context ctx
,
1128 regmatch_t
*pmatch
, int eflags
)
1132 if (!headerless_match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1134 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1136 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1137 if (match
.rm_so
> pmatch
->rm_so
)
1139 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1142 pmatch
->rm_so
= match
.rm_so
;
1143 pmatch
->rm_eo
= match
.rm_eo
;
1147 int grep_next_match(struct grep_opt
*opt
,
1148 const char *bol
, const char *eol
,
1149 enum grep_context ctx
, regmatch_t
*pmatch
,
1150 enum grep_header_field field
, int eflags
)
1155 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1157 for (p
= ((ctx
== GREP_CONTEXT_HEAD
)
1158 ? opt
->header_list
: opt
->pattern_list
);
1161 case GREP_PATTERN_HEAD
:
1162 if ((field
!= GREP_HEADER_FIELD_MAX
) &&
1163 (p
->field
!= field
))
1166 case GREP_PATTERN
: /* atom */
1167 case GREP_PATTERN_BODY
:
1168 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1179 static void show_line_header(struct grep_opt
*opt
, const char *name
,
1180 unsigned lno
, ssize_t cno
, char sign
)
1182 if (opt
->heading
&& opt
->last_shown
== 0) {
1183 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1184 opt
->output(opt
, "\n", 1);
1186 opt
->last_shown
= lno
;
1188 if (!opt
->heading
&& opt
->pathname
) {
1189 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1190 output_sep(opt
, sign
);
1194 xsnprintf(buf
, sizeof(buf
), "%d", lno
);
1195 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_LINENO
]);
1196 output_sep(opt
, sign
);
1199 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1200 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1201 * being called with a context line.
1203 if (opt
->columnnum
&& cno
) {
1205 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, (uintmax_t)cno
);
1206 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_COLUMNNO
]);
1207 output_sep(opt
, sign
);
1211 static void show_line(struct grep_opt
*opt
,
1212 const char *bol
, const char *eol
,
1213 const char *name
, unsigned lno
, ssize_t cno
, char sign
)
1215 int rest
= eol
- bol
;
1216 const char *match_color
= NULL
;
1217 const char *line_color
= NULL
;
1219 if (opt
->file_break
&& opt
->last_shown
== 0) {
1220 if (opt
->show_hunk_mark
)
1221 opt
->output(opt
, "\n", 1);
1222 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1223 if (opt
->last_shown
== 0) {
1224 if (opt
->show_hunk_mark
) {
1225 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1226 opt
->output(opt
, "\n", 1);
1228 } else if (lno
> opt
->last_shown
+ 1) {
1229 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1230 opt
->output(opt
, "\n", 1);
1233 if (!opt
->only_matching
) {
1235 * In case the line we're being called with contains more than
1236 * one match, leave printing each header to the loop below.
1238 show_line_header(opt
, name
, lno
, cno
, sign
);
1240 if (opt
->color
|| opt
->only_matching
) {
1242 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1247 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
1249 match_color
= opt
->colors
[GREP_COLOR_MATCH_CONTEXT
];
1251 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
1252 else if (sign
== '-')
1253 line_color
= opt
->colors
[GREP_COLOR_CONTEXT
];
1254 else if (sign
== '=')
1255 line_color
= opt
->colors
[GREP_COLOR_FUNCTION
];
1257 while (grep_next_match(opt
, bol
, eol
, ctx
, &match
,
1258 GREP_HEADER_FIELD_MAX
, eflags
)) {
1259 if (match
.rm_so
== match
.rm_eo
)
1262 if (opt
->only_matching
)
1263 show_line_header(opt
, name
, lno
, cno
, sign
);
1265 output_color(opt
, bol
, match
.rm_so
, line_color
);
1266 output_color(opt
, bol
+ match
.rm_so
,
1267 match
.rm_eo
- match
.rm_so
, match_color
);
1268 if (opt
->only_matching
)
1269 opt
->output(opt
, "\n", 1);
1272 rest
-= match
.rm_eo
;
1273 eflags
= REG_NOTBOL
;
1276 if (!opt
->only_matching
) {
1277 output_color(opt
, bol
, rest
, line_color
);
1278 opt
->output(opt
, "\n", 1);
1285 * This lock protects access to the gitattributes machinery, which is
1288 pthread_mutex_t grep_attr_mutex
;
1290 static inline void grep_attr_lock(void)
1293 pthread_mutex_lock(&grep_attr_mutex
);
1296 static inline void grep_attr_unlock(void)
1299 pthread_mutex_unlock(&grep_attr_mutex
);
1302 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
,
1303 const char *bol
, const char *eol
)
1305 xdemitconf_t
*xecfg
= opt
->priv
;
1306 if (xecfg
&& !xecfg
->find_func
) {
1307 grep_source_load_driver(gs
, opt
->repo
->index
);
1308 if (gs
->driver
->funcname
.pattern
) {
1309 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1310 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1312 xecfg
= opt
->priv
= NULL
;
1318 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1319 xecfg
->find_func_priv
) >= 0;
1324 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1329 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1330 const char *bol
, unsigned lno
)
1332 while (bol
> gs
->buf
) {
1333 const char *eol
= --bol
;
1335 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1339 if (lno
<= opt
->last_shown
)
1342 if (match_funcname(opt
, gs
, bol
, eol
)) {
1343 show_line(opt
, bol
, eol
, gs
->name
, lno
, 0, '=');
1349 static int is_empty_line(const char *bol
, const char *eol
);
1351 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1352 const char *bol
, const char *end
, unsigned lno
)
1354 unsigned cur
= lno
, from
= 1, funcname_lno
= 0, orig_from
;
1355 int funcname_needed
= !!opt
->funcname
, comment_needed
= 0;
1357 if (opt
->pre_context
< lno
)
1358 from
= lno
- opt
->pre_context
;
1359 if (from
<= opt
->last_shown
)
1360 from
= opt
->last_shown
+ 1;
1362 if (opt
->funcbody
) {
1363 if (match_funcname(opt
, gs
, bol
, end
))
1366 funcname_needed
= 1;
1367 from
= opt
->last_shown
+ 1;
1371 while (bol
> gs
->buf
&& cur
> from
) {
1372 const char *next_bol
= bol
;
1373 const char *eol
= --bol
;
1375 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1378 if (comment_needed
&& (is_empty_line(bol
, eol
) ||
1379 match_funcname(opt
, gs
, bol
, eol
))) {
1388 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1390 funcname_needed
= 0;
1398 /* We need to look even further back to find a function signature. */
1399 if (opt
->funcname
&& funcname_needed
)
1400 show_funcname_line(opt
, gs
, bol
, cur
);
1404 const char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1406 while (*eol
!= '\n')
1408 show_line(opt
, bol
, eol
, gs
->name
, cur
, 0, sign
);
1414 static int should_lookahead(struct grep_opt
*opt
)
1418 if (opt
->pattern_expression
)
1419 return 0; /* punt for too complex stuff */
1422 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1423 if (p
->token
!= GREP_PATTERN
)
1424 return 0; /* punt for "header only" and stuff */
1429 static int look_ahead(struct grep_opt
*opt
,
1430 unsigned long *left_p
,
1434 unsigned lno
= *lno_p
;
1435 const char *bol
= *bol_p
;
1437 const char *sp
, *last_bol
;
1438 regoff_t earliest
= -1;
1440 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1444 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1445 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1447 if (earliest
< 0 || m
.rm_so
< earliest
)
1452 *bol_p
= bol
+ *left_p
;
1456 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1457 ; /* find the beginning of the line */
1460 for (sp
= bol
; sp
< last_bol
; sp
++) {
1464 *left_p
-= last_bol
- bol
;
1470 static int fill_textconv_grep(struct repository
*r
,
1471 struct userdiff_driver
*driver
,
1472 struct grep_source
*gs
)
1474 struct diff_filespec
*df
;
1478 if (!driver
|| !driver
->textconv
)
1479 return grep_source_load(gs
);
1482 * The textconv interface is intimately tied to diff_filespecs, so we
1483 * have to pretend to be one. If we could unify the grep_source
1484 * and diff_filespec structs, this mess could just go away.
1486 df
= alloc_filespec(gs
->path
);
1488 case GREP_SOURCE_OID
:
1489 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1491 case GREP_SOURCE_FILE
:
1492 fill_filespec(df
, null_oid(), 0, 0100644);
1495 BUG("attempt to textconv something without a path?");
1499 * fill_textconv is not remotely thread-safe; it modifies the global
1500 * diff tempfile structure, writes to the_repo's odb and might
1501 * internally call thread-unsafe functions such as the
1502 * prepare_packed_git() lazy-initializator. Because of the last two, we
1503 * must ensure mutual exclusion between this call and the object reading
1504 * API, thus we use obj_read_lock() here.
1506 * TODO: allowing text conversion to run in parallel with object
1507 * reading operations might increase performance in the multithreaded
1508 * non-worktreee git-grep with --textconv.
1511 size
= fill_textconv(r
, driver
, df
, &buf
);
1516 * The normal fill_textconv usage by the diff machinery would just keep
1517 * the textconv'd buf separate from the diff_filespec. But much of the
1518 * grep code passes around a grep_source and assumes that its "buf"
1519 * pointer is the beginning of the thing we are searching. So let's
1520 * install our textconv'd version into the grep_source, taking care not
1521 * to leak any existing buffer.
1523 grep_source_clear_data(gs
);
1530 static int is_empty_line(const char *bol
, const char *eol
)
1532 while (bol
< eol
&& isspace(*bol
))
1537 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1540 const char *peek_bol
= NULL
;
1543 unsigned last_hit
= 0;
1544 int binary_match_only
= 0;
1546 int try_lookahead
= 0;
1547 int show_function
= 0;
1548 struct userdiff_driver
*textconv
= NULL
;
1549 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1552 if (!opt
->status_only
&& gs
->name
== NULL
)
1553 BUG("grep call which could print a name requires "
1554 "grep_source.name be non-NULL");
1557 opt
->output
= std_output
;
1559 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1561 /* Show hunk marks, except for the first file. */
1562 if (opt
->last_shown
)
1563 opt
->show_hunk_mark
= 1;
1565 * If we're using threads then we can't easily identify
1566 * the first file. Always put hunk marks in that case
1567 * and skip the very first one later in work_done().
1569 if (opt
->output
!= std_output
)
1570 opt
->show_hunk_mark
= 1;
1572 opt
->last_shown
= 0;
1574 if (opt
->allow_textconv
) {
1575 grep_source_load_driver(gs
, opt
->repo
->index
);
1577 * We might set up the shared textconv cache data here, which
1578 * is not thread-safe. Also, get_oid_with_context() and
1579 * parse_object() might be internally called. As they are not
1580 * currently thread-safe and might be racy with object reading,
1581 * obj_read_lock() must be called.
1585 textconv
= userdiff_get_textconv(opt
->repo
, gs
->driver
);
1591 * We know the result of a textconv is text, so we only have to care
1592 * about binary handling if we are not using it.
1595 switch (opt
->binary
) {
1596 case GREP_BINARY_DEFAULT
:
1597 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1598 binary_match_only
= 1;
1600 case GREP_BINARY_NOMATCH
:
1601 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1602 return 0; /* Assume unmatch */
1604 case GREP_BINARY_TEXT
:
1607 BUG("unknown binary handling mode");
1611 memset(&xecfg
, 0, sizeof(xecfg
));
1614 try_lookahead
= should_lookahead(opt
);
1616 if (fill_textconv_grep(opt
->repo
, textconv
, gs
) < 0)
1625 ssize_t col
= -1, icol
= -1;
1628 * look_ahead() skips quickly to the line that possibly
1629 * has the next hit; don't call it if we need to do
1630 * something more than just skipping the current line
1631 * in response to an unmatch for the current line. E.g.
1632 * inside a post-context window, we will show the current
1633 * line as a context around the previous hit when it
1638 && (show_function
||
1639 lno
<= last_hit
+ opt
->post_context
))
1640 && look_ahead(opt
, &left
, &lno
, &bol
))
1642 eol
= end_of_line(bol
, &left
);
1644 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1645 ctx
= GREP_CONTEXT_BODY
;
1647 hit
= match_line(opt
, bol
, eol
, &col
, &icol
, ctx
, collect_hits
);
1652 /* "grep -v -e foo -e bla" should list lines
1653 * that do not have either, so inversion should
1658 if (opt
->unmatch_name_only
) {
1663 if (hit
&& (opt
->max_count
< 0 || count
< opt
->max_count
)) {
1665 if (opt
->status_only
)
1667 if (opt
->name_only
) {
1668 show_name(opt
, gs
->name
);
1673 if (binary_match_only
) {
1674 opt
->output(opt
, "Binary file ", 12);
1675 output_color(opt
, gs
->name
, strlen(gs
->name
),
1676 opt
->colors
[GREP_COLOR_FILENAME
]);
1677 opt
->output(opt
, " matches\n", 9);
1680 /* Hit at this line. If we haven't shown the
1681 * pre-context lines, we would need to show them.
1683 if (opt
->pre_context
|| opt
->funcbody
)
1684 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1685 else if (opt
->funcname
)
1686 show_funcname_line(opt
, gs
, bol
, lno
);
1687 cno
= opt
->invert
? icol
: col
;
1690 * A negative cno indicates that there was no
1691 * match on the line. We are thus inverted and
1692 * being asked to show all lines that _don't_
1693 * match a given expression. Therefore, set cno
1694 * to 0 to suggest the whole line matches.
1698 show_line(opt
, bol
, eol
, gs
->name
, lno
, cno
+ 1, ':');
1704 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1705 unsigned long peek_left
= left
;
1706 const char *peek_eol
= eol
;
1709 * Trailing empty lines are not interesting.
1710 * Peek past them to see if they belong to the
1711 * body of the current function.
1714 while (is_empty_line(peek_bol
, peek_eol
)) {
1715 peek_bol
= peek_eol
+ 1;
1716 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1719 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1722 if (show_function
||
1723 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1724 /* If the last hit is within the post context,
1725 * we need to show this line.
1727 show_line(opt
, bol
, eol
, gs
->name
, lno
, col
+ 1, '-');
1741 if (opt
->status_only
)
1742 return opt
->unmatch_name_only
;
1743 if (opt
->unmatch_name_only
) {
1744 /* We did not see any hit, so we want to show this */
1745 show_name(opt
, gs
->name
);
1749 xdiff_clear_find_func(&xecfg
);
1753 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1754 * which feels mostly useless but sometimes useful. Maybe
1755 * make it another option? For now suppress them.
1757 if (opt
->count
&& count
) {
1759 if (opt
->pathname
) {
1760 output_color(opt
, gs
->name
, strlen(gs
->name
),
1761 opt
->colors
[GREP_COLOR_FILENAME
]);
1762 output_sep(opt
, ':');
1764 xsnprintf(buf
, sizeof(buf
), "%u\n", count
);
1765 opt
->output(opt
, buf
, strlen(buf
));
1771 static void clr_hit_marker(struct grep_expr
*x
)
1773 /* All-hit markers are meaningful only at the very top level
1778 if (x
->node
!= GREP_NODE_OR
)
1780 x
->u
.binary
.left
->hit
= 0;
1781 x
= x
->u
.binary
.right
;
1785 static int chk_hit_marker(struct grep_expr
*x
)
1787 /* Top level nodes have hit markers. See if they all are hits */
1789 if (x
->node
!= GREP_NODE_OR
)
1791 if (!x
->u
.binary
.left
->hit
)
1793 x
= x
->u
.binary
.right
;
1797 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1800 * we do not have to do the two-pass grep when we do not check
1801 * buffer-wide "all-match".
1803 if (!opt
->all_match
&& !opt
->no_body_match
)
1804 return grep_source_1(opt
, gs
, 0);
1806 /* Otherwise the toplevel "or" terms hit a bit differently.
1807 * We first clear hit markers from them.
1809 clr_hit_marker(opt
->pattern_expression
);
1811 grep_source_1(opt
, gs
, 1);
1813 if (opt
->all_match
&& !chk_hit_marker(opt
->pattern_expression
))
1815 if (opt
->no_body_match
&& opt
->body_hit
)
1818 return grep_source_1(opt
, gs
, 0);
1821 static void grep_source_init_buf(struct grep_source
*gs
,
1825 gs
->type
= GREP_SOURCE_BUF
;
1831 gs
->identifier
= NULL
;
1834 int grep_buffer(struct grep_opt
*opt
, const char *buf
, unsigned long size
)
1836 struct grep_source gs
;
1839 grep_source_init_buf(&gs
, buf
, size
);
1841 r
= grep_source(opt
, &gs
);
1843 grep_source_clear(&gs
);
1847 void grep_source_init_file(struct grep_source
*gs
, const char *name
,
1850 gs
->type
= GREP_SOURCE_FILE
;
1851 gs
->name
= xstrdup_or_null(name
);
1852 gs
->path
= xstrdup_or_null(path
);
1856 gs
->identifier
= xstrdup(path
);
1859 void grep_source_init_oid(struct grep_source
*gs
, const char *name
,
1860 const char *path
, const struct object_id
*oid
,
1861 struct repository
*repo
)
1863 gs
->type
= GREP_SOURCE_OID
;
1864 gs
->name
= xstrdup_or_null(name
);
1865 gs
->path
= xstrdup_or_null(path
);
1869 gs
->identifier
= oiddup(oid
);
1873 void grep_source_clear(struct grep_source
*gs
)
1875 FREE_AND_NULL(gs
->name
);
1876 FREE_AND_NULL(gs
->path
);
1877 FREE_AND_NULL(gs
->identifier
);
1878 grep_source_clear_data(gs
);
1881 void grep_source_clear_data(struct grep_source
*gs
)
1884 case GREP_SOURCE_FILE
:
1885 case GREP_SOURCE_OID
:
1886 /* these types own the buffer */
1887 free((char *)gs
->buf
);
1891 case GREP_SOURCE_BUF
:
1892 /* leave user-provided buf intact */
1897 static int grep_source_load_oid(struct grep_source
*gs
)
1899 enum object_type type
;
1901 gs
->buf
= repo_read_object_file(gs
->repo
, gs
->identifier
, &type
,
1904 return error(_("'%s': unable to read %s"),
1906 oid_to_hex(gs
->identifier
));
1910 static int grep_source_load_file(struct grep_source
*gs
)
1912 const char *filename
= gs
->identifier
;
1918 if (lstat(filename
, &st
) < 0) {
1920 if (errno
!= ENOENT
)
1921 error_errno(_("failed to stat '%s'"), filename
);
1924 if (!S_ISREG(st
.st_mode
))
1926 size
= xsize_t(st
.st_size
);
1927 i
= open(filename
, O_RDONLY
);
1930 data
= xmallocz(size
);
1931 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1932 error_errno(_("'%s': short read"), filename
);
1944 static int grep_source_load(struct grep_source
*gs
)
1950 case GREP_SOURCE_FILE
:
1951 return grep_source_load_file(gs
);
1952 case GREP_SOURCE_OID
:
1953 return grep_source_load_oid(gs
);
1954 case GREP_SOURCE_BUF
:
1955 return gs
->buf
? 0 : -1;
1957 BUG("invalid grep_source type to load");
1960 void grep_source_load_driver(struct grep_source
*gs
,
1961 struct index_state
*istate
)
1968 gs
->driver
= userdiff_find_by_path(istate
, gs
->path
);
1970 gs
->driver
= userdiff_find_by_name("default");
1974 static int grep_source_is_binary(struct grep_source
*gs
,
1975 struct index_state
*istate
)
1977 grep_source_load_driver(gs
, istate
);
1978 if (gs
->driver
->binary
!= -1)
1979 return gs
->driver
->binary
;
1981 if (!grep_source_load(gs
))
1982 return buffer_is_binary(gs
->buf
, gs
->size
);