Merge branch 'ab/grep-lose-opt-regflags'
authorJunio C Hamano <gitster@pobox.com>
Thu, 13 Jul 2017 23:14:54 +0000 (16:14 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 13 Jul 2017 23:14:54 +0000 (16:14 -0700)
Code cleanup.

* ab/grep-lose-opt-regflags:
grep: remove redundant REG_NEWLINE when compiling fixed regex
grep: remove regflags from the public grep_opt API
grep: remove redundant and verbose re-assignments to 0
grep: remove redundant "fixed" field re-assignment to 0
grep: adjust a redundant grep pattern type assignment
grep: remove redundant double assignment to 0

builtin/grep.c
grep.c
grep.h
revision.c
index 0d6e669732dadd574c69924578bf85ed6e6aeaf8..7e79eb1a754a5f604829e3e9e6d98d83712be9b6 100644 (file)
@@ -1170,8 +1170,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 
        if (!opt.pattern_list)
                die(_("no pattern given."));
-       if (!opt.fixed && opt.ignore_case)
-               opt.regflags |= REG_ICASE;
 
        /*
         * We have to find "--" in a separate pass, because its presence
diff --git a/grep.c b/grep.c
index 98733db623ae2a0c4b3c11b71e3560429ec975ff..2efec0e182d5c20fb77903fc61dedd09a8ee7ba3 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -35,10 +35,8 @@ void init_grep_defaults(void)
        memset(opt, 0, sizeof(*opt));
        opt->relative = 1;
        opt->pathname = 1;
-       opt->regflags = REG_NEWLINE;
        opt->max_depth = -1;
        opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
-       opt->extended_regexp_option = 0;
        color_set(opt->color_context, "");
        color_set(opt->color_filename, "");
        color_set(opt->color_function, "");
@@ -79,10 +77,7 @@ int grep_config(const char *var, const char *value, void *cb)
                return -1;
 
        if (!strcmp(var, "grep.extendedregexp")) {
-               if (git_config_bool(var, value))
-                       opt->extended_regexp_option = 1;
-               else
-                       opt->extended_regexp_option = 0;
+               opt->extended_regexp_option = git_config_bool(var, value);
                return 0;
        }
 
@@ -157,7 +152,6 @@ void grep_init(struct grep_opt *opt, const char *prefix)
        opt->linenum = def->linenum;
        opt->max_depth = def->max_depth;
        opt->pathname = def->pathname;
-       opt->regflags = def->regflags;
        opt->relative = def->relative;
        opt->output = def->output;
 
@@ -173,33 +167,41 @@ void grep_init(struct grep_opt *opt, const char *prefix)
 
 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
 {
+       /*
+        * When committing to the pattern type by setting the relevant
+        * fields in grep_opt it's generally not necessary to zero out
+        * the fields we're not choosing, since they won't have been
+        * set by anything. The extended_regexp_option field is the
+        * only exception to this.
+        *
+        * This is because in the process of parsing grep.patternType
+        * & grep.extendedRegexp we set opt->pattern_type_option and
+        * opt->extended_regexp_option, respectively. We then
+        * internally use opt->extended_regexp_option to see if we're
+        * compiling an ERE. It must be unset if that's not actually
+        * the case.
+        */
+       if (pattern_type != GREP_PATTERN_TYPE_ERE &&
+           opt->extended_regexp_option)
+               opt->extended_regexp_option = 0;
+
        switch (pattern_type) {
        case GREP_PATTERN_TYPE_UNSPECIFIED:
                /* fall through */
 
        case GREP_PATTERN_TYPE_BRE:
-               opt->fixed = 0;
-               opt->pcre1 = 0;
-               opt->pcre2 = 0;
                break;
 
        case GREP_PATTERN_TYPE_ERE:
-               opt->fixed = 0;
-               opt->pcre1 = 0;
-               opt->pcre2 = 0;
-               opt->regflags |= REG_EXTENDED;
+               opt->extended_regexp_option = 1;
                break;
 
        case GREP_PATTERN_TYPE_FIXED:
                opt->fixed = 1;
-               opt->pcre1 = 0;
-               opt->pcre2 = 0;
                break;
 
        case GREP_PATTERN_TYPE_PCRE:
-               opt->fixed = 0;
 #ifdef USE_LIBPCRE2
-               opt->pcre1 = 0;
                opt->pcre2 = 1;
 #else
                /*
@@ -209,7 +211,6 @@ static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, st
                 * "cannot use Perl-compatible regexes[...]".
                 */
                opt->pcre1 = 1;
-               opt->pcre2 = 0;
 #endif
                break;
        }
@@ -222,6 +223,11 @@ void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_o
        else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
                grep_set_pattern_type_option(opt->pattern_type_option, opt);
        else if (opt->extended_regexp_option)
+               /*
+                * This branch *must* happen after setting from the
+                * opt->pattern_type_option above, we don't want
+                * grep.extendedRegexp to override grep.patternType!
+                */
                grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
 }
 
@@ -587,7 +593,7 @@ static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
 {
        struct strbuf sb = STRBUF_INIT;
        int err;
-       int regflags = opt->regflags;
+       int regflags = 0;
 
        basic_regex_quote_buf(&sb, p->pattern);
        if (opt->ignore_case)
@@ -606,12 +612,12 @@ static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
 
 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 {
-       int icase, ascii_only;
+       int ascii_only;
        int err;
+       int regflags = REG_NEWLINE;
 
        p->word_regexp = opt->word_regexp;
        p->ignore_case = opt->ignore_case;
-       icase          = opt->regflags & REG_ICASE || p->ignore_case;
        ascii_only     = !has_non_ascii(p->pattern);
 
        /*
@@ -629,12 +635,10 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
        if (opt->fixed ||
            has_null(p->pattern, p->patternlen) ||
            is_fixed(p->pattern, p->patternlen))
-               p->fixed = !icase || ascii_only;
-       else
-               p->fixed = 0;
+               p->fixed = !p->ignore_case || ascii_only;
 
        if (p->fixed) {
-               p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL);
+               p->kws = kwsalloc(p->ignore_case ? tolower_trans_tbl : NULL);
                kwsincr(p->kws, p->pattern, p->patternlen);
                kwsprep(p->kws);
                return;
@@ -658,7 +662,11 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
                return;
        }
 
-       err = regcomp(&p->regexp, p->pattern, opt->regflags);
+       if (p->ignore_case)
+               regflags |= REG_ICASE;
+       if (opt->extended_regexp_option)
+               regflags |= REG_EXTENDED;
+       err = regcomp(&p->regexp, p->pattern, regflags);
        if (err) {
                char errbuf[1024];
                regerror(err, &p->regexp, errbuf, 1024);
diff --git a/grep.h b/grep.h
index b8f93bfc2d518beddc90fed74e419cb1c6fb07f1..0c091e5104a83c0f83c65fd0bef9f39caf8f644d 100644 (file)
--- a/grep.h
+++ b/grep.h
@@ -162,7 +162,6 @@ struct grep_opt {
        char color_match_selected[COLOR_MAXLEN];
        char color_selected[COLOR_MAXLEN];
        char color_sep[COLOR_MAXLEN];
-       int regflags;
        unsigned pre_context;
        unsigned post_context;
        unsigned last_shown;
index 9ff120b30518aa2b3f7cc214a1c921d739108d7c..6603af944486db6d5ec560dd9c655d24c31ce9e9 100644 (file)
@@ -1362,7 +1362,6 @@ void init_revisions(struct rev_info *revs, const char *prefix)
        init_grep_defaults();
        grep_init(&revs->grep_filter, prefix);
        revs->grep_filter.status_only = 1;
-       revs->grep_filter.regflags = REG_NEWLINE;
 
        diff_setup(&revs->diffopt);
        if (prefix && !revs->diffopt.prefix) {
@@ -2022,7 +2021,6 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
        } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
                revs->grep_filter.ignore_case = 1;
-               revs->grep_filter.regflags |= REG_ICASE;
                DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
        } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
                revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;