]> git.ipfire.org Git - thirdparty/git.git/blobdiff - grep.c
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / grep.c
diff --git a/grep.c b/grep.c
index ca4f321cb311c843c3b22a1348a9575db4bae628..5bec7fd7935dedc94bbde1e8efe67745e2022702 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -362,6 +362,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
        int jitret;
        int patinforet;
        size_t jitsizearg;
+       int literal = !opt->ignore_case && (p->fixed || p->is_fixed);
 
        /*
         * Call pcre2_general_context_create() before calling any
@@ -382,8 +383,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
                }
                options |= PCRE2_CASELESS;
        }
-       if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-           !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+       if (!opt->ignore_locale && is_utf8_locale() && !literal)
                options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
 
 #ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
@@ -595,6 +595,35 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
        }
 }
 
+static struct grep_expr *grep_not_expr(struct grep_expr *expr)
+{
+       struct grep_expr *z = xcalloc(1, sizeof(*z));
+       z->node = GREP_NODE_NOT;
+       z->u.unary = expr;
+       return z;
+}
+
+static struct grep_expr *grep_binexp(enum grep_expr_node kind,
+                                    struct grep_expr *left,
+                                    struct grep_expr *right)
+{
+       struct grep_expr *z = xcalloc(1, sizeof(*z));
+       z->node = kind;
+       z->u.binary.left = left;
+       z->u.binary.right = right;
+       return z;
+}
+
+static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
+{
+       return grep_binexp(GREP_NODE_OR, left, right);
+}
+
+static struct grep_expr *grep_and_expr(struct grep_expr *left, struct grep_expr *right)
+{
+       return grep_binexp(GREP_NODE_AND, left, right);
+}
+
 static struct grep_expr *compile_pattern_or(struct grep_pat **);
 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
 {
@@ -638,12 +667,10 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
                if (!p->next)
                        die("--not not followed by pattern expression");
                *list = p->next;
-               CALLOC_ARRAY(x, 1);
-               x->node = GREP_NODE_NOT;
-               x->u.unary = compile_pattern_not(list);
-               if (!x->u.unary)
+               x = compile_pattern_not(list);
+               if (!x)
                        die("--not followed by non pattern expression");
-               return x;
+               return grep_not_expr(x);
        default:
                return compile_pattern_atom(list);
        }
@@ -652,7 +679,7 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
 {
        struct grep_pat *p;
-       struct grep_expr *x, *y, *z;
+       struct grep_expr *x, *y;
 
        x = compile_pattern_not(list);
        p = *list;
@@ -665,11 +692,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
                y = compile_pattern_and(list);
                if (!y)
                        die("--and not followed by pattern expression");
-               CALLOC_ARRAY(z, 1);
-               z->node = GREP_NODE_AND;
-               z->u.binary.left = x;
-               z->u.binary.right = y;
-               return z;
+               return grep_and_expr(x, y);
        }
        return x;
 }
@@ -677,7 +700,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
 {
        struct grep_pat *p;
-       struct grep_expr *x, *y, *z;
+       struct grep_expr *x, *y;
 
        x = compile_pattern_and(list);
        p = *list;
@@ -685,11 +708,7 @@ static struct grep_expr *compile_pattern_or(struct grep_pat **list)
                y = compile_pattern_or(list);
                if (!y)
                        die("not a pattern expression %s", p->pattern);
-               CALLOC_ARRAY(z, 1);
-               z->node = GREP_NODE_OR;
-               z->u.binary.left = x;
-               z->u.binary.right = y;
-               return z;
+               return grep_or_expr(x, y);
        }
        return x;
 }
@@ -706,15 +725,6 @@ static struct grep_expr *grep_true_expr(void)
        return z;
 }
 
-static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
-{
-       struct grep_expr *z = xcalloc(1, sizeof(*z));
-       z->node = GREP_NODE_OR;
-       z->u.binary.left = left;
-       z->u.binary.right = right;
-       return z;
-}
-
 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
 {
        struct grep_pat *p;
@@ -797,7 +807,7 @@ void compile_grep_patterns(struct grep_opt *opt)
                }
        }
 
-       if (opt->all_match || header_expr)
+       if (opt->all_match || opt->no_body_match || header_expr)
                opt->extended = 1;
        else if (!opt->extended)
                return;
@@ -808,6 +818,9 @@ void compile_grep_patterns(struct grep_opt *opt)
        if (p)
                die("incomplete pattern expression: %s", p->pattern);
 
+       if (opt->no_body_match && opt->pattern_expression)
+               opt->pattern_expression = grep_not_expr(opt->pattern_expression);
+
        if (!header_expr)
                return;
 
@@ -1057,6 +1070,8 @@ static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x,
                        if (h && (*col < 0 || tmp.rm_so < *col))
                                *col = tmp.rm_so;
                }
+               if (x->u.atom->token == GREP_PATTERN_BODY)
+                       opt->body_hit |= h;
                break;
        case GREP_NODE_NOT:
                /*
@@ -1825,16 +1840,19 @@ int grep_source(struct grep_opt *opt, struct grep_source *gs)
         * we do not have to do the two-pass grep when we do not check
         * buffer-wide "all-match".
         */
-       if (!opt->all_match)
+       if (!opt->all_match && !opt->no_body_match)
                return grep_source_1(opt, gs, 0);
 
        /* Otherwise the toplevel "or" terms hit a bit differently.
         * We first clear hit markers from them.
         */
        clr_hit_marker(opt->pattern_expression);
+       opt->body_hit = 0;
        grep_source_1(opt, gs, 1);
 
-       if (!chk_hit_marker(opt->pattern_expression))
+       if (opt->all_match && !chk_hit_marker(opt->pattern_expression))
+               return 0;
+       if (opt->no_body_match && opt->body_hit)
                return 0;
 
        return grep_source_1(opt, gs, 0);