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 struct grep_opt grep_defaults
= {
26 .pattern_type_option
= GREP_PATTERN_TYPE_UNSPECIFIED
,
28 [GREP_COLOR_CONTEXT
] = "",
29 [GREP_COLOR_FILENAME
] = "",
30 [GREP_COLOR_FUNCTION
] = "",
31 [GREP_COLOR_LINENO
] = "",
32 [GREP_COLOR_COLUMNNO
] = "",
33 [GREP_COLOR_MATCH_CONTEXT
] = GIT_COLOR_BOLD_RED
,
34 [GREP_COLOR_MATCH_SELECTED
] = GIT_COLOR_BOLD_RED
,
35 [GREP_COLOR_SELECTED
] = "",
36 [GREP_COLOR_SEP
] = GIT_COLOR_CYAN
,
43 static const char *color_grep_slots
[] = {
44 [GREP_COLOR_CONTEXT
] = "context",
45 [GREP_COLOR_FILENAME
] = "filename",
46 [GREP_COLOR_FUNCTION
] = "function",
47 [GREP_COLOR_LINENO
] = "lineNumber",
48 [GREP_COLOR_COLUMNNO
] = "column",
49 [GREP_COLOR_MATCH_CONTEXT
] = "matchContext",
50 [GREP_COLOR_MATCH_SELECTED
] = "matchSelected",
51 [GREP_COLOR_SELECTED
] = "selected",
52 [GREP_COLOR_SEP
] = "separator",
55 static int parse_pattern_type_arg(const char *opt
, const char *arg
)
57 if (!strcmp(arg
, "default"))
58 return GREP_PATTERN_TYPE_UNSPECIFIED
;
59 else if (!strcmp(arg
, "basic"))
60 return GREP_PATTERN_TYPE_BRE
;
61 else if (!strcmp(arg
, "extended"))
62 return GREP_PATTERN_TYPE_ERE
;
63 else if (!strcmp(arg
, "fixed"))
64 return GREP_PATTERN_TYPE_FIXED
;
65 else if (!strcmp(arg
, "perl"))
66 return GREP_PATTERN_TYPE_PCRE
;
67 die("bad %s argument: %s", opt
, arg
);
70 define_list_config_array_extra(color_grep_slots
, {"match"});
73 * Read the configuration file once and store it in
74 * the grep_defaults template.
76 int grep_config(const char *var
, const char *value
, void *cb
)
78 struct grep_opt
*opt
= &grep_defaults
;
81 if (userdiff_config(var
, value
) < 0)
85 * The instance of grep_opt that we set up here is copied by
86 * grep_init() to be used by each individual invocation.
87 * When populating a new field of this structure here, be
88 * sure to think about ownership -- e.g., you might need to
89 * override the shallow copy in grep_init() with a deep copy.
92 if (!strcmp(var
, "grep.extendedregexp")) {
93 opt
->extended_regexp_option
= git_config_bool(var
, value
);
97 if (!strcmp(var
, "grep.patterntype")) {
98 opt
->pattern_type_option
= parse_pattern_type_arg(var
, value
);
102 if (!strcmp(var
, "grep.linenumber")) {
103 opt
->linenum
= git_config_bool(var
, value
);
106 if (!strcmp(var
, "grep.column")) {
107 opt
->columnnum
= git_config_bool(var
, value
);
111 if (!strcmp(var
, "grep.fullname")) {
112 opt
->relative
= !git_config_bool(var
, value
);
116 if (!strcmp(var
, "color.grep"))
117 opt
->color
= git_config_colorbool(var
, value
);
118 if (!strcmp(var
, "color.grep.match")) {
119 if (grep_config("color.grep.matchcontext", value
, cb
) < 0)
121 if (grep_config("color.grep.matchselected", value
, cb
) < 0)
123 } else if (skip_prefix(var
, "color.grep.", &slot
)) {
124 int i
= LOOKUP_CONFIG(color_grep_slots
, slot
);
129 color
= opt
->colors
[i
];
131 return config_error_nonbool(var
);
132 return color_parse(value
, color
);
138 * Initialize one instance of grep_opt and copy the
139 * default values from the template we read the configuration
140 * information in an earlier call to git_config(grep_config).
142 void grep_init(struct grep_opt
*opt
, struct repository
*repo
, const char *prefix
)
144 *opt
= grep_defaults
;
147 opt
->prefix
= prefix
;
148 opt
->prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
149 opt
->pattern_tail
= &opt
->pattern_list
;
150 opt
->header_tail
= &opt
->header_list
;
153 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
156 * When committing to the pattern type by setting the relevant
157 * fields in grep_opt it's generally not necessary to zero out
158 * the fields we're not choosing, since they won't have been
159 * set by anything. The extended_regexp_option field is the
160 * only exception to this.
162 * This is because in the process of parsing grep.patternType
163 * & grep.extendedRegexp we set opt->pattern_type_option and
164 * opt->extended_regexp_option, respectively. We then
165 * internally use opt->extended_regexp_option to see if we're
166 * compiling an ERE. It must be unset if that's not actually
169 if (pattern_type
!= GREP_PATTERN_TYPE_ERE
&&
170 opt
->extended_regexp_option
)
171 opt
->extended_regexp_option
= 0;
173 switch (pattern_type
) {
174 case GREP_PATTERN_TYPE_UNSPECIFIED
:
177 case GREP_PATTERN_TYPE_BRE
:
180 case GREP_PATTERN_TYPE_ERE
:
181 opt
->extended_regexp_option
= 1;
184 case GREP_PATTERN_TYPE_FIXED
:
188 case GREP_PATTERN_TYPE_PCRE
:
194 void grep_commit_pattern_type(enum grep_pattern_type pattern_type
, struct grep_opt
*opt
)
196 if (pattern_type
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
197 grep_set_pattern_type_option(pattern_type
, opt
);
198 else if (opt
->pattern_type_option
!= GREP_PATTERN_TYPE_UNSPECIFIED
)
199 grep_set_pattern_type_option(opt
->pattern_type_option
, opt
);
200 else if (opt
->extended_regexp_option
)
202 * This branch *must* happen after setting from the
203 * opt->pattern_type_option above, we don't want
204 * grep.extendedRegexp to override grep.patternType!
206 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, opt
);
209 static struct grep_pat
*create_grep_pat(const char *pat
, size_t patlen
,
210 const char *origin
, int no
,
211 enum grep_pat_token t
,
212 enum grep_header_field field
)
214 struct grep_pat
*p
= xcalloc(1, sizeof(*p
));
215 p
->pattern
= xmemdupz(pat
, patlen
);
216 p
->patternlen
= patlen
;
224 static void do_append_grep_pat(struct grep_pat
***tail
, struct grep_pat
*p
)
231 case GREP_PATTERN
: /* atom */
232 case GREP_PATTERN_HEAD
:
233 case GREP_PATTERN_BODY
:
235 struct grep_pat
*new_pat
;
237 char *cp
= p
->pattern
+ p
->patternlen
, *nl
= NULL
;
238 while (++len
<= p
->patternlen
) {
239 if (*(--cp
) == '\n') {
246 new_pat
= create_grep_pat(nl
+ 1, len
- 1, p
->origin
,
247 p
->no
, p
->token
, p
->field
);
248 new_pat
->next
= p
->next
;
250 *tail
= &new_pat
->next
;
253 p
->patternlen
-= len
;
261 void append_header_grep_pattern(struct grep_opt
*opt
,
262 enum grep_header_field field
, const char *pat
)
264 struct grep_pat
*p
= create_grep_pat(pat
, strlen(pat
), "header", 0,
265 GREP_PATTERN_HEAD
, field
);
266 if (field
== GREP_HEADER_REFLOG
)
267 opt
->use_reflog_filter
= 1;
268 do_append_grep_pat(&opt
->header_tail
, p
);
271 void append_grep_pattern(struct grep_opt
*opt
, const char *pat
,
272 const char *origin
, int no
, enum grep_pat_token t
)
274 append_grep_pat(opt
, pat
, strlen(pat
), origin
, no
, t
);
277 void append_grep_pat(struct grep_opt
*opt
, const char *pat
, size_t patlen
,
278 const char *origin
, int no
, enum grep_pat_token t
)
280 struct grep_pat
*p
= create_grep_pat(pat
, patlen
, origin
, no
, t
, 0);
281 do_append_grep_pat(&opt
->pattern_tail
, p
);
284 struct grep_opt
*grep_opt_dup(const struct grep_opt
*opt
)
286 struct grep_pat
*pat
;
287 struct grep_opt
*ret
= xmalloc(sizeof(struct grep_opt
));
290 ret
->pattern_list
= NULL
;
291 ret
->pattern_tail
= &ret
->pattern_list
;
293 for(pat
= opt
->pattern_list
; pat
!= NULL
; pat
= pat
->next
)
295 if(pat
->token
== GREP_PATTERN_HEAD
)
296 append_header_grep_pattern(ret
, pat
->field
,
299 append_grep_pat(ret
, pat
->pattern
, pat
->patternlen
,
300 pat
->origin
, pat
->no
, pat
->token
);
306 static NORETURN
void compile_regexp_failed(const struct grep_pat
*p
,
312 xsnprintf(where
, sizeof(where
), "In '%s' at %d, ", p
->origin
, p
->no
);
314 xsnprintf(where
, sizeof(where
), "%s, ", p
->origin
);
318 die("%s'%s': %s", where
, p
->pattern
, error
);
321 static int is_fixed(const char *s
, size_t len
)
325 for (i
= 0; i
< len
; i
++) {
326 if (is_regex_special(s
[i
]))
334 #define GREP_PCRE2_DEBUG_MALLOC 0
336 static void *pcre2_malloc(PCRE2_SIZE size
, MAYBE_UNUSED
void *memory_data
)
338 void *pointer
= malloc(size
);
339 #if GREP_PCRE2_DEBUG_MALLOC
340 static int count
= 1;
341 fprintf(stderr
, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer
, count
++, size
);
346 static void pcre2_free(void *pointer
, MAYBE_UNUSED
void *memory_data
)
348 #if GREP_PCRE2_DEBUG_MALLOC
349 static int count
= 1;
351 fprintf(stderr
, "PCRE2:%p -> #%02d: free()\n", pointer
, count
++);
356 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
359 PCRE2_UCHAR errbuf
[256];
360 PCRE2_SIZE erroffset
;
361 int options
= PCRE2_MULTILINE
;
365 int literal
= !opt
->ignore_case
&& (p
->fixed
|| p
->is_fixed
);
368 * Call pcre2_general_context_create() before calling any
369 * other pcre2_*(). It sets up our malloc()/free() functions
370 * with which everything else is allocated.
372 p
->pcre2_general_context
= pcre2_general_context_create(
373 pcre2_malloc
, pcre2_free
, NULL
);
374 if (!p
->pcre2_general_context
)
375 die("Couldn't allocate PCRE2 general context");
377 if (opt
->ignore_case
) {
378 if (!opt
->ignore_locale
&& has_non_ascii(p
->pattern
)) {
379 p
->pcre2_tables
= pcre2_maketables(p
->pcre2_general_context
);
380 p
->pcre2_compile_context
= pcre2_compile_context_create(p
->pcre2_general_context
);
381 pcre2_set_character_tables(p
->pcre2_compile_context
,
384 options
|= PCRE2_CASELESS
;
386 if (!opt
->ignore_locale
&& is_utf8_locale() && !literal
)
387 options
|= (PCRE2_UTF
| PCRE2_MATCH_INVALID_UTF
);
389 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
390 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
391 if (PCRE2_MATCH_INVALID_UTF
&& options
& (PCRE2_UTF
| PCRE2_CASELESS
))
392 options
|= PCRE2_NO_START_OPTIMIZE
;
395 p
->pcre2_pattern
= pcre2_compile((PCRE2_SPTR
)p
->pattern
,
396 p
->patternlen
, options
, &error
, &erroffset
,
397 p
->pcre2_compile_context
);
399 if (p
->pcre2_pattern
) {
400 p
->pcre2_match_data
= pcre2_match_data_create_from_pattern(p
->pcre2_pattern
, p
->pcre2_general_context
);
401 if (!p
->pcre2_match_data
)
402 die("Couldn't allocate PCRE2 match data");
404 pcre2_get_error_message(error
, errbuf
, sizeof(errbuf
));
405 compile_regexp_failed(p
, (const char *)&errbuf
);
408 pcre2_config(PCRE2_CONFIG_JIT
, &p
->pcre2_jit_on
);
409 if (p
->pcre2_jit_on
) {
410 jitret
= pcre2_jit_compile(p
->pcre2_pattern
, PCRE2_JIT_COMPLETE
);
412 die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p
->pattern
, jitret
);
415 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
416 * tells us whether the library itself supports JIT,
417 * but to see whether we're going to be actually using
418 * JIT we need to extract PCRE2_INFO_JITSIZE from the
419 * pattern *after* we do pcre2_jit_compile() above.
421 * This is because if the pattern contains the
422 * (*NO_JIT) verb (see pcre2syntax(3))
423 * pcre2_jit_compile() will exit early with 0. If we
424 * then proceed to call pcre2_jit_match() further down
425 * the line instead of pcre2_match() we'll either
426 * segfault (pre PCRE 10.31) or run into a fatal error
429 patinforet
= pcre2_pattern_info(p
->pcre2_pattern
, PCRE2_INFO_JITSIZE
, &jitsizearg
);
431 BUG("pcre2_pattern_info() failed: %d", patinforet
);
432 if (jitsizearg
== 0) {
439 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
440 regmatch_t
*match
, int eflags
)
444 PCRE2_UCHAR errbuf
[256];
446 if (eflags
& REG_NOTBOL
)
447 flags
|= PCRE2_NOTBOL
;
450 ret
= pcre2_jit_match(p
->pcre2_pattern
, (unsigned char *)line
,
451 eol
- line
, 0, flags
, p
->pcre2_match_data
,
454 ret
= pcre2_match(p
->pcre2_pattern
, (unsigned char *)line
,
455 eol
- line
, 0, flags
, p
->pcre2_match_data
,
458 if (ret
< 0 && ret
!= PCRE2_ERROR_NOMATCH
) {
459 pcre2_get_error_message(ret
, errbuf
, sizeof(errbuf
));
460 die("%s failed with error code %d: %s",
461 (p
->pcre2_jit_on
? "pcre2_jit_match" : "pcre2_match"), ret
,
465 ovector
= pcre2_get_ovector_pointer(p
->pcre2_match_data
);
467 match
->rm_so
= (int)ovector
[0];
468 match
->rm_eo
= (int)ovector
[1];
474 static void free_pcre2_pattern(struct grep_pat
*p
)
476 pcre2_compile_context_free(p
->pcre2_compile_context
);
477 pcre2_code_free(p
->pcre2_pattern
);
478 pcre2_match_data_free(p
->pcre2_match_data
);
479 #ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
480 pcre2_maketables_free(p
->pcre2_general_context
, p
->pcre2_tables
);
482 free((void *)p
->pcre2_tables
);
484 pcre2_general_context_free(p
->pcre2_general_context
);
486 #else /* !USE_LIBPCRE2 */
487 static void compile_pcre2_pattern(struct grep_pat
*p
, const struct grep_opt
*opt
)
489 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
492 static int pcre2match(struct grep_pat
*p
, const char *line
, const char *eol
,
493 regmatch_t
*match
, int eflags
)
498 static void free_pcre2_pattern(struct grep_pat
*p
)
502 static void compile_fixed_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
504 struct strbuf sb
= STRBUF_INIT
;
508 basic_regex_quote_buf(&sb
, p
->pattern
);
509 if (opt
->ignore_case
)
510 regflags
|= REG_ICASE
;
511 err
= regcomp(&p
->regexp
, sb
.buf
, regflags
);
515 regerror(err
, &p
->regexp
, errbuf
, sizeof(errbuf
));
516 compile_regexp_failed(p
, errbuf
);
519 #endif /* !USE_LIBPCRE2 */
521 static void compile_regexp(struct grep_pat
*p
, struct grep_opt
*opt
)
524 int regflags
= REG_NEWLINE
;
526 p
->word_regexp
= opt
->word_regexp
;
527 p
->ignore_case
= opt
->ignore_case
;
528 p
->fixed
= opt
->fixed
;
530 if (memchr(p
->pattern
, 0, p
->patternlen
) && !opt
->pcre2
)
531 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
533 p
->is_fixed
= is_fixed(p
->pattern
, p
->patternlen
);
535 if (!p
->fixed
&& !p
->is_fixed
) {
536 const char *no_jit
= "(*NO_JIT)";
537 const int no_jit_len
= strlen(no_jit
);
538 if (starts_with(p
->pattern
, no_jit
) &&
539 is_fixed(p
->pattern
+ no_jit_len
,
540 p
->patternlen
- no_jit_len
))
544 if (p
->fixed
|| p
->is_fixed
) {
547 compile_pcre2_pattern(p
, opt
);
550 * E.g. t7811-grep-open.sh relies on the
551 * pattern being restored.
553 char *old_pattern
= p
->pattern
;
554 size_t old_patternlen
= p
->patternlen
;
555 struct strbuf sb
= STRBUF_INIT
;
558 * There is the PCRE2_LITERAL flag, but it's
559 * only in PCRE v2 10.30 and later. Needing to
560 * ifdef our way around that and dealing with
561 * it + PCRE2_MULTILINE being an error is more
562 * complex than just quoting this ourselves.
564 strbuf_add(&sb
, "\\Q", 2);
565 strbuf_add(&sb
, p
->pattern
, p
->patternlen
);
566 strbuf_add(&sb
, "\\E", 2);
569 p
->patternlen
= sb
.len
;
570 compile_pcre2_pattern(p
, opt
);
571 p
->pattern
= old_pattern
;
572 p
->patternlen
= old_patternlen
;
575 #else /* !USE_LIBPCRE2 */
576 compile_fixed_regexp(p
, opt
);
577 #endif /* !USE_LIBPCRE2 */
582 compile_pcre2_pattern(p
, opt
);
587 regflags
|= REG_ICASE
;
588 if (opt
->extended_regexp_option
)
589 regflags
|= REG_EXTENDED
;
590 err
= regcomp(&p
->regexp
, p
->pattern
, regflags
);
593 regerror(err
, &p
->regexp
, errbuf
, 1024);
594 compile_regexp_failed(p
, errbuf
);
598 static struct grep_expr
*grep_not_expr(struct grep_expr
*expr
)
600 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
601 z
->node
= GREP_NODE_NOT
;
606 static struct grep_expr
*grep_or_expr(struct grep_expr
*left
, struct grep_expr
*right
)
608 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
609 z
->node
= GREP_NODE_OR
;
610 z
->u
.binary
.left
= left
;
611 z
->u
.binary
.right
= right
;
615 static struct grep_expr
*compile_pattern_or(struct grep_pat
**);
616 static struct grep_expr
*compile_pattern_atom(struct grep_pat
**list
)
625 case GREP_PATTERN
: /* atom */
626 case GREP_PATTERN_HEAD
:
627 case GREP_PATTERN_BODY
:
629 x
->node
= GREP_NODE_ATOM
;
633 case GREP_OPEN_PAREN
:
635 x
= compile_pattern_or(list
);
636 if (!*list
|| (*list
)->token
!= GREP_CLOSE_PAREN
)
637 die("unmatched parenthesis");
638 *list
= (*list
)->next
;
645 static struct grep_expr
*compile_pattern_not(struct grep_pat
**list
)
656 die("--not not followed by pattern expression");
658 x
= compile_pattern_not(list
);
660 die("--not followed by non pattern expression");
661 return grep_not_expr(x
);
663 return compile_pattern_atom(list
);
667 static struct grep_expr
*compile_pattern_and(struct grep_pat
**list
)
670 struct grep_expr
*x
, *y
, *z
;
672 x
= compile_pattern_not(list
);
674 if (p
&& p
->token
== GREP_AND
) {
676 die("--and not preceded by pattern expression");
678 die("--and not followed by pattern expression");
680 y
= compile_pattern_and(list
);
682 die("--and not followed by pattern expression");
684 z
->node
= GREP_NODE_AND
;
685 z
->u
.binary
.left
= x
;
686 z
->u
.binary
.right
= y
;
692 static struct grep_expr
*compile_pattern_or(struct grep_pat
**list
)
695 struct grep_expr
*x
, *y
;
697 x
= compile_pattern_and(list
);
699 if (x
&& p
&& p
->token
!= GREP_CLOSE_PAREN
) {
700 y
= compile_pattern_or(list
);
702 die("not a pattern expression %s", p
->pattern
);
703 return grep_or_expr(x
, y
);
708 static struct grep_expr
*compile_pattern_expr(struct grep_pat
**list
)
710 return compile_pattern_or(list
);
713 static struct grep_expr
*grep_true_expr(void)
715 struct grep_expr
*z
= xcalloc(1, sizeof(*z
));
716 z
->node
= GREP_NODE_TRUE
;
720 static struct grep_expr
*prep_header_patterns(struct grep_opt
*opt
)
723 struct grep_expr
*header_expr
;
724 struct grep_expr
*(header_group
[GREP_HEADER_FIELD_MAX
]);
725 enum grep_header_field fld
;
727 if (!opt
->header_list
)
730 for (p
= opt
->header_list
; p
; p
= p
->next
) {
731 if (p
->token
!= GREP_PATTERN_HEAD
)
732 BUG("a non-header pattern in grep header list.");
733 if (p
->field
< GREP_HEADER_FIELD_MIN
||
734 GREP_HEADER_FIELD_MAX
<= p
->field
)
735 BUG("unknown header field %d", p
->field
);
736 compile_regexp(p
, opt
);
739 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++)
740 header_group
[fld
] = NULL
;
742 for (p
= opt
->header_list
; p
; p
= p
->next
) {
744 struct grep_pat
*pp
= p
;
746 h
= compile_pattern_atom(&pp
);
747 if (!h
|| pp
!= p
->next
)
748 BUG("malformed header expr");
749 if (!header_group
[p
->field
]) {
750 header_group
[p
->field
] = h
;
753 header_group
[p
->field
] = grep_or_expr(h
, header_group
[p
->field
]);
758 for (fld
= 0; fld
< GREP_HEADER_FIELD_MAX
; fld
++) {
759 if (!header_group
[fld
])
762 header_expr
= grep_true_expr();
763 header_expr
= grep_or_expr(header_group
[fld
], header_expr
);
768 static struct grep_expr
*grep_splice_or(struct grep_expr
*x
, struct grep_expr
*y
)
770 struct grep_expr
*z
= x
;
773 assert(x
->node
== GREP_NODE_OR
);
774 if (x
->u
.binary
.right
&&
775 x
->u
.binary
.right
->node
== GREP_NODE_TRUE
) {
776 x
->u
.binary
.right
= y
;
779 x
= x
->u
.binary
.right
;
784 void compile_grep_patterns(struct grep_opt
*opt
)
787 struct grep_expr
*header_expr
= prep_header_patterns(opt
);
789 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
791 case GREP_PATTERN
: /* atom */
792 case GREP_PATTERN_HEAD
:
793 case GREP_PATTERN_BODY
:
794 compile_regexp(p
, opt
);
802 if (opt
->all_match
|| opt
->no_body_match
|| header_expr
)
804 else if (!opt
->extended
)
807 p
= opt
->pattern_list
;
809 opt
->pattern_expression
= compile_pattern_expr(&p
);
811 die("incomplete pattern expression: %s", p
->pattern
);
813 if (opt
->no_body_match
&& opt
->pattern_expression
)
814 opt
->pattern_expression
= grep_not_expr(opt
->pattern_expression
);
819 if (!opt
->pattern_expression
)
820 opt
->pattern_expression
= header_expr
;
821 else if (opt
->all_match
)
822 opt
->pattern_expression
= grep_splice_or(header_expr
,
823 opt
->pattern_expression
);
825 opt
->pattern_expression
= grep_or_expr(opt
->pattern_expression
,
830 static void free_pattern_expr(struct grep_expr
*x
)
837 free_pattern_expr(x
->u
.unary
);
841 free_pattern_expr(x
->u
.binary
.left
);
842 free_pattern_expr(x
->u
.binary
.right
);
848 void free_grep_patterns(struct grep_opt
*opt
)
850 struct grep_pat
*p
, *n
;
852 for (p
= opt
->pattern_list
; p
; p
= n
) {
855 case GREP_PATTERN
: /* atom */
856 case GREP_PATTERN_HEAD
:
857 case GREP_PATTERN_BODY
:
858 if (p
->pcre2_pattern
)
859 free_pcre2_pattern(p
);
872 free_pattern_expr(opt
->pattern_expression
);
875 static const char *end_of_line(const char *cp
, unsigned long *left
)
877 unsigned long l
= *left
;
878 while (l
&& *cp
!= '\n') {
886 static int word_char(char ch
)
888 return isalnum(ch
) || ch
== '_';
891 static void output_color(struct grep_opt
*opt
, const void *data
, size_t size
,
894 if (want_color(opt
->color
) && color
&& color
[0]) {
895 opt
->output(opt
, color
, strlen(color
));
896 opt
->output(opt
, data
, size
);
897 opt
->output(opt
, GIT_COLOR_RESET
, strlen(GIT_COLOR_RESET
));
899 opt
->output(opt
, data
, size
);
902 static void output_sep(struct grep_opt
*opt
, char sign
)
904 if (opt
->null_following_name
)
905 opt
->output(opt
, "\0", 1);
907 output_color(opt
, &sign
, 1, opt
->colors
[GREP_COLOR_SEP
]);
910 static void show_name(struct grep_opt
*opt
, const char *name
)
912 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
913 opt
->output(opt
, opt
->null_following_name
? "\0" : "\n", 1);
916 static int patmatch(struct grep_pat
*p
,
917 const char *line
, const char *eol
,
918 regmatch_t
*match
, int eflags
)
922 if (p
->pcre2_pattern
)
923 hit
= !pcre2match(p
, line
, eol
, match
, eflags
);
925 hit
= !regexec_buf(&p
->regexp
, line
, eol
- line
, 1, match
,
931 static void strip_timestamp(const char *bol
, const char **eol_p
)
933 const char *eol
= *eol_p
;
935 while (bol
< --eol
) {
948 { "committer ", 10 },
952 static int headerless_match_one_pattern(struct grep_pat
*p
,
953 const char *bol
, const char *eol
,
954 enum grep_context ctx
,
955 regmatch_t
*pmatch
, int eflags
)
958 const char *start
= bol
;
960 if ((p
->token
!= GREP_PATTERN
) &&
961 ((p
->token
== GREP_PATTERN_HEAD
) != (ctx
== GREP_CONTEXT_HEAD
)))
965 hit
= patmatch(p
, bol
, eol
, pmatch
, eflags
);
967 if (hit
&& p
->word_regexp
) {
968 if ((pmatch
[0].rm_so
< 0) ||
969 (eol
- bol
) < pmatch
[0].rm_so
||
970 (pmatch
[0].rm_eo
< 0) ||
971 (eol
- bol
) < pmatch
[0].rm_eo
)
972 die("regexp returned nonsense");
974 /* Match beginning must be either beginning of the
975 * line, or at word boundary (i.e. the last char must
976 * not be a word char). Similarly, match end must be
977 * either end of the line, or at word boundary
978 * (i.e. the next char must not be a word char).
980 if ( ((pmatch
[0].rm_so
== 0) ||
981 !word_char(bol
[pmatch
[0].rm_so
-1])) &&
982 ((pmatch
[0].rm_eo
== (eol
-bol
)) ||
983 !word_char(bol
[pmatch
[0].rm_eo
])) )
988 /* Words consist of at least one character. */
989 if (pmatch
->rm_so
== pmatch
->rm_eo
)
992 if (!hit
&& pmatch
[0].rm_so
+ bol
+ 1 < eol
) {
993 /* There could be more than one match on the
994 * line, and the first match might not be
995 * strict word match. But later ones could be!
996 * Forward to the next possible start, i.e. the
997 * next position following a non-word char.
999 bol
= pmatch
[0].rm_so
+ bol
+ 1;
1000 while (word_char(bol
[-1]) && bol
< eol
)
1002 eflags
|= REG_NOTBOL
;
1008 pmatch
[0].rm_so
+= bol
- start
;
1009 pmatch
[0].rm_eo
+= bol
- start
;
1014 static int match_one_pattern(struct grep_pat
*p
,
1015 const char *bol
, const char *eol
,
1016 enum grep_context ctx
, regmatch_t
*pmatch
,
1022 if (p
->token
== GREP_PATTERN_HEAD
) {
1023 assert(p
->field
< ARRAY_SIZE(header_field
));
1024 field
= header_field
[p
->field
].field
;
1025 len
= header_field
[p
->field
].len
;
1026 if (strncmp(bol
, field
, len
))
1031 case GREP_HEADER_AUTHOR
:
1032 case GREP_HEADER_COMMITTER
:
1033 strip_timestamp(bol
, &eol
);
1040 return headerless_match_one_pattern(p
, bol
, eol
, ctx
, pmatch
, eflags
);
1044 static int match_expr_eval(struct grep_opt
*opt
, struct grep_expr
*x
,
1045 const char *bol
, const char *eol
,
1046 enum grep_context ctx
, ssize_t
*col
,
1047 ssize_t
*icol
, int collect_hits
)
1052 die("Not a valid grep expression");
1054 case GREP_NODE_TRUE
:
1057 case GREP_NODE_ATOM
:
1060 h
= match_one_pattern(x
->u
.atom
, bol
, eol
, ctx
,
1062 if (h
&& (*col
< 0 || tmp
.rm_so
< *col
))
1065 if (x
->u
.atom
->token
== GREP_PATTERN_BODY
)
1070 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1072 h
= !match_expr_eval(opt
, x
->u
.unary
, bol
, eol
, ctx
, icol
, col
,
1076 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1078 if (h
|| opt
->columnnum
) {
1080 * Don't short-circuit AND when given --column, since a
1081 * NOT earlier in the tree may turn this into an OR. In
1082 * this case, see the below comment.
1084 h
&= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
,
1089 if (!(collect_hits
|| opt
->columnnum
)) {
1091 * Don't short-circuit OR when given --column (or
1092 * collecting hits) to ensure we don't skip a later
1093 * child that would produce an earlier match.
1095 return (match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
,
1096 ctx
, col
, icol
, 0) ||
1097 match_expr_eval(opt
, x
->u
.binary
.right
, bol
,
1098 eol
, ctx
, col
, icol
, 0));
1100 h
= match_expr_eval(opt
, x
->u
.binary
.left
, bol
, eol
, ctx
, col
,
1103 x
->u
.binary
.left
->hit
|= h
;
1104 h
|= match_expr_eval(opt
, x
->u
.binary
.right
, bol
, eol
, ctx
, col
,
1105 icol
, collect_hits
);
1108 die("Unexpected node type (internal error) %d", x
->node
);
1115 static int match_expr(struct grep_opt
*opt
,
1116 const char *bol
, const char *eol
,
1117 enum grep_context ctx
, ssize_t
*col
,
1118 ssize_t
*icol
, int collect_hits
)
1120 struct grep_expr
*x
= opt
->pattern_expression
;
1121 return match_expr_eval(opt
, x
, bol
, eol
, ctx
, col
, icol
, collect_hits
);
1124 static int match_line(struct grep_opt
*opt
,
1125 const char *bol
, const char *eol
,
1126 ssize_t
*col
, ssize_t
*icol
,
1127 enum grep_context ctx
, int collect_hits
)
1133 return match_expr(opt
, bol
, eol
, ctx
, col
, icol
,
1136 /* we do not call with collect_hits without being extended */
1137 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1139 if (match_one_pattern(p
, bol
, eol
, ctx
, &tmp
, 0)) {
1141 if (!opt
->columnnum
) {
1143 * Without --column, any single match on a line
1144 * is enough to know that it needs to be
1145 * printed. With --column, scan _all_ patterns
1146 * to find the earliest.
1150 if (*col
< 0 || tmp
.rm_so
< *col
)
1157 static int match_next_pattern(struct grep_pat
*p
,
1158 const char *bol
, const char *eol
,
1159 enum grep_context ctx
,
1160 regmatch_t
*pmatch
, int eflags
)
1164 if (!headerless_match_one_pattern(p
, bol
, eol
, ctx
, &match
, eflags
))
1166 if (match
.rm_so
< 0 || match
.rm_eo
< 0)
1168 if (pmatch
->rm_so
>= 0 && pmatch
->rm_eo
>= 0) {
1169 if (match
.rm_so
> pmatch
->rm_so
)
1171 if (match
.rm_so
== pmatch
->rm_so
&& match
.rm_eo
< pmatch
->rm_eo
)
1174 pmatch
->rm_so
= match
.rm_so
;
1175 pmatch
->rm_eo
= match
.rm_eo
;
1179 int grep_next_match(struct grep_opt
*opt
,
1180 const char *bol
, const char *eol
,
1181 enum grep_context ctx
, regmatch_t
*pmatch
,
1182 enum grep_header_field field
, int eflags
)
1187 pmatch
->rm_so
= pmatch
->rm_eo
= -1;
1189 for (p
= ((ctx
== GREP_CONTEXT_HEAD
)
1190 ? opt
->header_list
: opt
->pattern_list
);
1193 case GREP_PATTERN_HEAD
:
1194 if ((field
!= GREP_HEADER_FIELD_MAX
) &&
1195 (p
->field
!= field
))
1198 case GREP_PATTERN
: /* atom */
1199 case GREP_PATTERN_BODY
:
1200 hit
|= match_next_pattern(p
, bol
, eol
, ctx
,
1211 static void show_line_header(struct grep_opt
*opt
, const char *name
,
1212 unsigned lno
, ssize_t cno
, char sign
)
1214 if (opt
->heading
&& opt
->last_shown
== 0) {
1215 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1216 opt
->output(opt
, "\n", 1);
1218 opt
->last_shown
= lno
;
1220 if (!opt
->heading
&& opt
->pathname
) {
1221 output_color(opt
, name
, strlen(name
), opt
->colors
[GREP_COLOR_FILENAME
]);
1222 output_sep(opt
, sign
);
1226 xsnprintf(buf
, sizeof(buf
), "%d", lno
);
1227 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_LINENO
]);
1228 output_sep(opt
, sign
);
1231 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1232 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1233 * being called with a context line.
1235 if (opt
->columnnum
&& cno
) {
1237 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, (uintmax_t)cno
);
1238 output_color(opt
, buf
, strlen(buf
), opt
->colors
[GREP_COLOR_COLUMNNO
]);
1239 output_sep(opt
, sign
);
1243 static void show_line(struct grep_opt
*opt
,
1244 const char *bol
, const char *eol
,
1245 const char *name
, unsigned lno
, ssize_t cno
, char sign
)
1247 int rest
= eol
- bol
;
1248 const char *match_color
= NULL
;
1249 const char *line_color
= NULL
;
1251 if (opt
->file_break
&& opt
->last_shown
== 0) {
1252 if (opt
->show_hunk_mark
)
1253 opt
->output(opt
, "\n", 1);
1254 } else if (opt
->pre_context
|| opt
->post_context
|| opt
->funcbody
) {
1255 if (opt
->last_shown
== 0) {
1256 if (opt
->show_hunk_mark
) {
1257 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1258 opt
->output(opt
, "\n", 1);
1260 } else if (lno
> opt
->last_shown
+ 1) {
1261 output_color(opt
, "--", 2, opt
->colors
[GREP_COLOR_SEP
]);
1262 opt
->output(opt
, "\n", 1);
1265 if (!opt
->only_matching
) {
1267 * In case the line we're being called with contains more than
1268 * one match, leave printing each header to the loop below.
1270 show_line_header(opt
, name
, lno
, cno
, sign
);
1272 if (opt
->color
|| opt
->only_matching
) {
1274 enum grep_context ctx
= GREP_CONTEXT_BODY
;
1279 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
1281 match_color
= opt
->colors
[GREP_COLOR_MATCH_CONTEXT
];
1283 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
1284 else if (sign
== '-')
1285 line_color
= opt
->colors
[GREP_COLOR_CONTEXT
];
1286 else if (sign
== '=')
1287 line_color
= opt
->colors
[GREP_COLOR_FUNCTION
];
1289 while (grep_next_match(opt
, bol
, eol
, ctx
, &match
,
1290 GREP_HEADER_FIELD_MAX
, eflags
)) {
1291 if (match
.rm_so
== match
.rm_eo
)
1294 if (opt
->only_matching
)
1295 show_line_header(opt
, name
, lno
, cno
, sign
);
1297 output_color(opt
, bol
, match
.rm_so
, line_color
);
1298 output_color(opt
, bol
+ match
.rm_so
,
1299 match
.rm_eo
- match
.rm_so
, match_color
);
1300 if (opt
->only_matching
)
1301 opt
->output(opt
, "\n", 1);
1304 rest
-= match
.rm_eo
;
1305 eflags
= REG_NOTBOL
;
1308 if (!opt
->only_matching
) {
1309 output_color(opt
, bol
, rest
, line_color
);
1310 opt
->output(opt
, "\n", 1);
1317 * This lock protects access to the gitattributes machinery, which is
1320 pthread_mutex_t grep_attr_mutex
;
1322 static inline void grep_attr_lock(void)
1325 pthread_mutex_lock(&grep_attr_mutex
);
1328 static inline void grep_attr_unlock(void)
1331 pthread_mutex_unlock(&grep_attr_mutex
);
1334 static int match_funcname(struct grep_opt
*opt
, struct grep_source
*gs
,
1335 const char *bol
, const char *eol
)
1337 xdemitconf_t
*xecfg
= opt
->priv
;
1338 if (xecfg
&& !xecfg
->find_func
) {
1339 grep_source_load_driver(gs
, opt
->repo
->index
);
1340 if (gs
->driver
->funcname
.pattern
) {
1341 const struct userdiff_funcname
*pe
= &gs
->driver
->funcname
;
1342 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
1344 xecfg
= opt
->priv
= NULL
;
1350 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
1351 xecfg
->find_func_priv
) >= 0;
1356 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
1361 static void show_funcname_line(struct grep_opt
*opt
, struct grep_source
*gs
,
1362 const char *bol
, unsigned lno
)
1364 while (bol
> gs
->buf
) {
1365 const char *eol
= --bol
;
1367 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1371 if (lno
<= opt
->last_shown
)
1374 if (match_funcname(opt
, gs
, bol
, eol
)) {
1375 show_line(opt
, bol
, eol
, gs
->name
, lno
, 0, '=');
1381 static int is_empty_line(const char *bol
, const char *eol
);
1383 static void show_pre_context(struct grep_opt
*opt
, struct grep_source
*gs
,
1384 const char *bol
, const char *end
, unsigned lno
)
1386 unsigned cur
= lno
, from
= 1, funcname_lno
= 0, orig_from
;
1387 int funcname_needed
= !!opt
->funcname
, comment_needed
= 0;
1389 if (opt
->pre_context
< lno
)
1390 from
= lno
- opt
->pre_context
;
1391 if (from
<= opt
->last_shown
)
1392 from
= opt
->last_shown
+ 1;
1394 if (opt
->funcbody
) {
1395 if (match_funcname(opt
, gs
, bol
, end
))
1398 funcname_needed
= 1;
1399 from
= opt
->last_shown
+ 1;
1403 while (bol
> gs
->buf
&& cur
> from
) {
1404 const char *next_bol
= bol
;
1405 const char *eol
= --bol
;
1407 while (bol
> gs
->buf
&& bol
[-1] != '\n')
1410 if (comment_needed
&& (is_empty_line(bol
, eol
) ||
1411 match_funcname(opt
, gs
, bol
, eol
))) {
1420 if (funcname_needed
&& match_funcname(opt
, gs
, bol
, eol
)) {
1422 funcname_needed
= 0;
1430 /* We need to look even further back to find a function signature. */
1431 if (opt
->funcname
&& funcname_needed
)
1432 show_funcname_line(opt
, gs
, bol
, cur
);
1436 const char *eol
= bol
, sign
= (cur
== funcname_lno
) ? '=' : '-';
1438 while (*eol
!= '\n')
1440 show_line(opt
, bol
, eol
, gs
->name
, cur
, 0, sign
);
1446 static int should_lookahead(struct grep_opt
*opt
)
1451 return 0; /* punt for too complex stuff */
1454 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1455 if (p
->token
!= GREP_PATTERN
)
1456 return 0; /* punt for "header only" and stuff */
1461 static int look_ahead(struct grep_opt
*opt
,
1462 unsigned long *left_p
,
1466 unsigned lno
= *lno_p
;
1467 const char *bol
= *bol_p
;
1469 const char *sp
, *last_bol
;
1470 regoff_t earliest
= -1;
1472 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
1476 hit
= patmatch(p
, bol
, bol
+ *left_p
, &m
, 0);
1477 if (!hit
|| m
.rm_so
< 0 || m
.rm_eo
< 0)
1479 if (earliest
< 0 || m
.rm_so
< earliest
)
1484 *bol_p
= bol
+ *left_p
;
1488 for (sp
= bol
+ earliest
; bol
< sp
&& sp
[-1] != '\n'; sp
--)
1489 ; /* find the beginning of the line */
1492 for (sp
= bol
; sp
< last_bol
; sp
++) {
1496 *left_p
-= last_bol
- bol
;
1502 static int fill_textconv_grep(struct repository
*r
,
1503 struct userdiff_driver
*driver
,
1504 struct grep_source
*gs
)
1506 struct diff_filespec
*df
;
1510 if (!driver
|| !driver
->textconv
)
1511 return grep_source_load(gs
);
1514 * The textconv interface is intimately tied to diff_filespecs, so we
1515 * have to pretend to be one. If we could unify the grep_source
1516 * and diff_filespec structs, this mess could just go away.
1518 df
= alloc_filespec(gs
->path
);
1520 case GREP_SOURCE_OID
:
1521 fill_filespec(df
, gs
->identifier
, 1, 0100644);
1523 case GREP_SOURCE_FILE
:
1524 fill_filespec(df
, null_oid(), 0, 0100644);
1527 BUG("attempt to textconv something without a path?");
1531 * fill_textconv is not remotely thread-safe; it modifies the global
1532 * diff tempfile structure, writes to the_repo's odb and might
1533 * internally call thread-unsafe functions such as the
1534 * prepare_packed_git() lazy-initializator. Because of the last two, we
1535 * must ensure mutual exclusion between this call and the object reading
1536 * API, thus we use obj_read_lock() here.
1538 * TODO: allowing text conversion to run in parallel with object
1539 * reading operations might increase performance in the multithreaded
1540 * non-worktreee git-grep with --textconv.
1543 size
= fill_textconv(r
, driver
, df
, &buf
);
1548 * The normal fill_textconv usage by the diff machinery would just keep
1549 * the textconv'd buf separate from the diff_filespec. But much of the
1550 * grep code passes around a grep_source and assumes that its "buf"
1551 * pointer is the beginning of the thing we are searching. So let's
1552 * install our textconv'd version into the grep_source, taking care not
1553 * to leak any existing buffer.
1555 grep_source_clear_data(gs
);
1562 static int is_empty_line(const char *bol
, const char *eol
)
1564 while (bol
< eol
&& isspace(*bol
))
1569 static int grep_source_1(struct grep_opt
*opt
, struct grep_source
*gs
, int collect_hits
)
1572 const char *peek_bol
= NULL
;
1575 unsigned last_hit
= 0;
1576 int binary_match_only
= 0;
1578 int try_lookahead
= 0;
1579 int show_function
= 0;
1580 struct userdiff_driver
*textconv
= NULL
;
1581 enum grep_context ctx
= GREP_CONTEXT_HEAD
;
1584 if (!opt
->status_only
&& gs
->name
== NULL
)
1585 BUG("grep call which could print a name requires "
1586 "grep_source.name be non-NULL");
1589 opt
->output
= std_output
;
1591 if (opt
->pre_context
|| opt
->post_context
|| opt
->file_break
||
1593 /* Show hunk marks, except for the first file. */
1594 if (opt
->last_shown
)
1595 opt
->show_hunk_mark
= 1;
1597 * If we're using threads then we can't easily identify
1598 * the first file. Always put hunk marks in that case
1599 * and skip the very first one later in work_done().
1601 if (opt
->output
!= std_output
)
1602 opt
->show_hunk_mark
= 1;
1604 opt
->last_shown
= 0;
1606 if (opt
->allow_textconv
) {
1607 grep_source_load_driver(gs
, opt
->repo
->index
);
1609 * We might set up the shared textconv cache data here, which
1610 * is not thread-safe. Also, get_oid_with_context() and
1611 * parse_object() might be internally called. As they are not
1612 * currently thread-safe and might be racy with object reading,
1613 * obj_read_lock() must be called.
1617 textconv
= userdiff_get_textconv(opt
->repo
, gs
->driver
);
1623 * We know the result of a textconv is text, so we only have to care
1624 * about binary handling if we are not using it.
1627 switch (opt
->binary
) {
1628 case GREP_BINARY_DEFAULT
:
1629 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1630 binary_match_only
= 1;
1632 case GREP_BINARY_NOMATCH
:
1633 if (grep_source_is_binary(gs
, opt
->repo
->index
))
1634 return 0; /* Assume unmatch */
1636 case GREP_BINARY_TEXT
:
1639 BUG("unknown binary handling mode");
1643 memset(&xecfg
, 0, sizeof(xecfg
));
1646 try_lookahead
= should_lookahead(opt
);
1648 if (fill_textconv_grep(opt
->repo
, textconv
, gs
) < 0)
1657 ssize_t col
= -1, icol
= -1;
1660 * look_ahead() skips quickly to the line that possibly
1661 * has the next hit; don't call it if we need to do
1662 * something more than just skipping the current line
1663 * in response to an unmatch for the current line. E.g.
1664 * inside a post-context window, we will show the current
1665 * line as a context around the previous hit when it
1670 && (show_function
||
1671 lno
<= last_hit
+ opt
->post_context
))
1672 && look_ahead(opt
, &left
, &lno
, &bol
))
1674 eol
= end_of_line(bol
, &left
);
1676 if ((ctx
== GREP_CONTEXT_HEAD
) && (eol
== bol
))
1677 ctx
= GREP_CONTEXT_BODY
;
1679 hit
= match_line(opt
, bol
, eol
, &col
, &icol
, ctx
, collect_hits
);
1684 /* "grep -v -e foo -e bla" should list lines
1685 * that do not have either, so inversion should
1690 if (opt
->unmatch_name_only
) {
1697 if (opt
->status_only
)
1699 if (opt
->name_only
) {
1700 show_name(opt
, gs
->name
);
1705 if (binary_match_only
) {
1706 opt
->output(opt
, "Binary file ", 12);
1707 output_color(opt
, gs
->name
, strlen(gs
->name
),
1708 opt
->colors
[GREP_COLOR_FILENAME
]);
1709 opt
->output(opt
, " matches\n", 9);
1712 /* Hit at this line. If we haven't shown the
1713 * pre-context lines, we would need to show them.
1715 if (opt
->pre_context
|| opt
->funcbody
)
1716 show_pre_context(opt
, gs
, bol
, eol
, lno
);
1717 else if (opt
->funcname
)
1718 show_funcname_line(opt
, gs
, bol
, lno
);
1719 cno
= opt
->invert
? icol
: col
;
1722 * A negative cno indicates that there was no
1723 * match on the line. We are thus inverted and
1724 * being asked to show all lines that _don't_
1725 * match a given expression. Therefore, set cno
1726 * to 0 to suggest the whole line matches.
1730 show_line(opt
, bol
, eol
, gs
->name
, lno
, cno
+ 1, ':');
1736 if (show_function
&& (!peek_bol
|| peek_bol
< bol
)) {
1737 unsigned long peek_left
= left
;
1738 const char *peek_eol
= eol
;
1741 * Trailing empty lines are not interesting.
1742 * Peek past them to see if they belong to the
1743 * body of the current function.
1746 while (is_empty_line(peek_bol
, peek_eol
)) {
1747 peek_bol
= peek_eol
+ 1;
1748 peek_eol
= end_of_line(peek_bol
, &peek_left
);
1751 if (match_funcname(opt
, gs
, peek_bol
, peek_eol
))
1754 if (show_function
||
1755 (last_hit
&& lno
<= last_hit
+ opt
->post_context
)) {
1756 /* If the last hit is within the post context,
1757 * we need to show this line.
1759 show_line(opt
, bol
, eol
, gs
->name
, lno
, col
+ 1, '-');
1773 if (opt
->status_only
)
1774 return opt
->unmatch_name_only
;
1775 if (opt
->unmatch_name_only
) {
1776 /* We did not see any hit, so we want to show this */
1777 show_name(opt
, gs
->name
);
1781 xdiff_clear_find_func(&xecfg
);
1785 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1786 * which feels mostly useless but sometimes useful. Maybe
1787 * make it another option? For now suppress them.
1789 if (opt
->count
&& count
) {
1791 if (opt
->pathname
) {
1792 output_color(opt
, gs
->name
, strlen(gs
->name
),
1793 opt
->colors
[GREP_COLOR_FILENAME
]);
1794 output_sep(opt
, ':');
1796 xsnprintf(buf
, sizeof(buf
), "%u\n", count
);
1797 opt
->output(opt
, buf
, strlen(buf
));
1803 static void clr_hit_marker(struct grep_expr
*x
)
1805 /* All-hit markers are meaningful only at the very top level
1810 if (x
->node
!= GREP_NODE_OR
)
1812 x
->u
.binary
.left
->hit
= 0;
1813 x
= x
->u
.binary
.right
;
1817 static int chk_hit_marker(struct grep_expr
*x
)
1819 /* Top level nodes have hit markers. See if they all are hits */
1821 if (x
->node
!= GREP_NODE_OR
)
1823 if (!x
->u
.binary
.left
->hit
)
1825 x
= x
->u
.binary
.right
;
1829 int grep_source(struct grep_opt
*opt
, struct grep_source
*gs
)
1832 * we do not have to do the two-pass grep when we do not check
1833 * buffer-wide "all-match".
1835 if (!opt
->all_match
&& !opt
->no_body_match
)
1836 return grep_source_1(opt
, gs
, 0);
1838 /* Otherwise the toplevel "or" terms hit a bit differently.
1839 * We first clear hit markers from them.
1841 clr_hit_marker(opt
->pattern_expression
);
1843 grep_source_1(opt
, gs
, 1);
1845 if (opt
->all_match
&& !chk_hit_marker(opt
->pattern_expression
))
1847 if (opt
->no_body_match
&& opt
->body_hit
)
1850 return grep_source_1(opt
, gs
, 0);
1853 static void grep_source_init_buf(struct grep_source
*gs
,
1857 gs
->type
= GREP_SOURCE_BUF
;
1863 gs
->identifier
= NULL
;
1866 int grep_buffer(struct grep_opt
*opt
, const char *buf
, unsigned long size
)
1868 struct grep_source gs
;
1871 grep_source_init_buf(&gs
, buf
, size
);
1873 r
= grep_source(opt
, &gs
);
1875 grep_source_clear(&gs
);
1879 void grep_source_init_file(struct grep_source
*gs
, const char *name
,
1882 gs
->type
= GREP_SOURCE_FILE
;
1883 gs
->name
= xstrdup_or_null(name
);
1884 gs
->path
= xstrdup_or_null(path
);
1888 gs
->identifier
= xstrdup(path
);
1891 void grep_source_init_oid(struct grep_source
*gs
, const char *name
,
1892 const char *path
, const struct object_id
*oid
,
1893 struct repository
*repo
)
1895 gs
->type
= GREP_SOURCE_OID
;
1896 gs
->name
= xstrdup_or_null(name
);
1897 gs
->path
= xstrdup_or_null(path
);
1901 gs
->identifier
= oiddup(oid
);
1905 void grep_source_clear(struct grep_source
*gs
)
1907 FREE_AND_NULL(gs
->name
);
1908 FREE_AND_NULL(gs
->path
);
1909 FREE_AND_NULL(gs
->identifier
);
1910 grep_source_clear_data(gs
);
1913 void grep_source_clear_data(struct grep_source
*gs
)
1916 case GREP_SOURCE_FILE
:
1917 case GREP_SOURCE_OID
:
1918 /* these types own the buffer */
1919 free((char *)gs
->buf
);
1923 case GREP_SOURCE_BUF
:
1924 /* leave user-provided buf intact */
1929 static int grep_source_load_oid(struct grep_source
*gs
)
1931 enum object_type type
;
1933 gs
->buf
= repo_read_object_file(gs
->repo
, gs
->identifier
, &type
,
1936 return error(_("'%s': unable to read %s"),
1938 oid_to_hex(gs
->identifier
));
1942 static int grep_source_load_file(struct grep_source
*gs
)
1944 const char *filename
= gs
->identifier
;
1950 if (lstat(filename
, &st
) < 0) {
1952 if (errno
!= ENOENT
)
1953 error_errno(_("failed to stat '%s'"), filename
);
1956 if (!S_ISREG(st
.st_mode
))
1958 size
= xsize_t(st
.st_size
);
1959 i
= open(filename
, O_RDONLY
);
1962 data
= xmallocz(size
);
1963 if (st
.st_size
!= read_in_full(i
, data
, size
)) {
1964 error_errno(_("'%s': short read"), filename
);
1976 static int grep_source_load(struct grep_source
*gs
)
1982 case GREP_SOURCE_FILE
:
1983 return grep_source_load_file(gs
);
1984 case GREP_SOURCE_OID
:
1985 return grep_source_load_oid(gs
);
1986 case GREP_SOURCE_BUF
:
1987 return gs
->buf
? 0 : -1;
1989 BUG("invalid grep_source type to load");
1992 void grep_source_load_driver(struct grep_source
*gs
,
1993 struct index_state
*istate
)
2000 gs
->driver
= userdiff_find_by_path(istate
, gs
->path
);
2002 gs
->driver
= userdiff_find_by_name("default");
2006 static int grep_source_is_binary(struct grep_source
*gs
,
2007 struct index_state
*istate
)
2009 grep_source_load_driver(gs
, istate
);
2010 if (gs
->driver
->binary
!= -1)
2011 return gs
->driver
->binary
;
2013 if (!grep_source_load(gs
))
2014 return buffer_is_binary(gs
->buf
, gs
->size
);