From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 855E5C433FE for ; Wed, 22 Dec 2021 02:59:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242023AbhLVC7L (ORCPT ); Tue, 21 Dec 2021 21:59:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242009AbhLVC7J (ORCPT ); Tue, 21 Dec 2021 21:59:09 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F09B0C06173F for ; Tue, 21 Dec 2021 18:59:08 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id v11so1794885wrw.10 for ; Tue, 21 Dec 2021 18:59:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8/ffV74A+23WdxJZGAnIA4wFSTAA5QeJVrn9NMY32Mw=; b=IXQuJaErwEr6zWb86JRNX2pXkXH6hXVOHshMp6MSWwpCn7efn/5a7s0tuzeBvXT508 7B55nQADNEIKy6s2yzpmBxzWJs50XCcZ5QontWLGoNqTUG8UDgsEC1jbbN7pnr8LFulH SJF8Zytakw7wzf8bBx7jGGu1IzEATo38dOH3hS+WBqDx+bFVEBSlZrR6g1rbMyi2wQWE cOdWItaxIoDZi7sAxnuBeGkWMv9KnV5gq4HiobSOJeJ5Qsbo4uMTc/4CuUkkN184jsGe 9OHyCaoKkXT3YlalqG1p+8wSJbC2UjdbbLTdtVW94tzsfMjBOs+/gxSW1TgVYZqkTM7i Ri5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8/ffV74A+23WdxJZGAnIA4wFSTAA5QeJVrn9NMY32Mw=; b=C5JV3Q1VfdN5OPN/ThUoDf0kBwWzXfwlB62v/Oz/QnVxItb5t+Bz1IYx4cjMWIWlIw rvm6qRMfg9YBaV7e40oTkOu8LfJG4f+QAJwVxyl9gPz90Qsjj0vxJfz5qwwWz+CLwVFY K0wksrUXKWgrjwaQmZ8IEnH/9ANGbt12dhc0XSTfkOODbdYxmBgOJ/YPHGnU31OYVNnE bd02Xb+gvv3nvls2WEzjvr85QYBFcOYYrF4AUzUU8S3aWKA5HPotHj54hRetkA3Yx1Pa +hBBbwI3pSymULtE4OCLeIkK+T972YnLvfs1LS1ynKqHzsczC35czG9w/Uhc6KMdCMV8 Gicg== X-Gm-Message-State: AOAM533/lTz1hDZ5IFaYqppPqjE66SiWU2gus97ocTRjPyFw2l5M7Q4n SyxFI7oUPsHbUNhBSS4bUUaOxvC9rPfa1Q== X-Google-Smtp-Source: ABdhPJzgSGQ3G8WDbWDudF1OVzaYWkWAyNAauIw1sC0RPwZG2OhLMHU7RYCk0/MPrRUbPFHC0vENiA== X-Received: by 2002:adf:ec09:: with SMTP id x9mr628358wrn.111.1640141947320; Tue, 21 Dec 2021 18:59:07 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id n15sm611577wru.66.2021.12.21.18.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 18:59:06 -0800 (PST) From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Subject: [PATCH v5 7/7] grep: simplify config parsing and option parsing Date: Wed, 22 Dec 2021 03:58:57 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.1146.gb52885e7c44 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Simplify the parsing of "grep.patternType" and "grep.extendedRegexp". This changes no behavior, but gets rid of complex parsing logic that isn't needed anymore. When "grep.patternType" was introduced in 84befcd0a4a (grep: add a grep.patternType configuration setting, 2012-08-03) we promised that: 1. You can set "grep.patternType", and "[setting it to] 'default' will return to the default matching behavior". 2. We'd support the existing "grep.extendedRegexp" option, but ignore it when the new "grep.patternType" option is set. We said we'd only ignore the older "grep.extendedRegexp" option "when the `grep.patternType` option is set. to a value other than 'default'". In a preceding commit we changed grep_config() to be called after grep_init(), which means that much of the complexity here can go away. Now as before when we only understand a "grep.extendedRegexp" setting of "true", and if "grep.patterntype=default" is set we'll interpret it as "grep.patterntype=basic", except if we previously saw a "grep.extendedRegexp", then it's interpreted as "grep.patterntype=extended". See my 07a3d411739 (grep: remove regflags from the public grep_opt API, 2017-06-29) for addition of the two comments being removed here, i.e. the complexity noted in that commit is now going away. We don't need grep_commit_pattern_type() anymore, we can instead have OPT_SET_INT() in "builtin/grep.c" manipulate the "pattern_type_option" member in "struct grep_opt" directly. We can also do away with the indirection of the "int fixed" and "int pcre2" members in favor of using "pattern_type_option" directly in "grep.c". Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/grep.c | 10 +++---- grep.c | 81 +++++++++++--------------------------------------- grep.h | 9 ++---- revision.c | 2 -- 4 files changed, 24 insertions(+), 78 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 0ea124321b6..942c4b25077 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -845,7 +845,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix) int i; int dummy; int use_index = 1; - int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED; int allow_revs; struct option options[] = { @@ -879,16 +878,16 @@ int cmd_grep(int argc, const char **argv, const char *prefix) N_("descend at most levels"), PARSE_OPT_NONEG, NULL, 1 }, OPT_GROUP(""), - OPT_SET_INT('E', "extended-regexp", &pattern_type_arg, + OPT_SET_INT('E', "extended-regexp", &opt.pattern_type_option, N_("use extended POSIX regular expressions"), GREP_PATTERN_TYPE_ERE), - OPT_SET_INT('G', "basic-regexp", &pattern_type_arg, + OPT_SET_INT('G', "basic-regexp", &opt.pattern_type_option, N_("use basic POSIX regular expressions (default)"), GREP_PATTERN_TYPE_BRE), - OPT_SET_INT('F', "fixed-strings", &pattern_type_arg, + OPT_SET_INT('F', "fixed-strings", &opt.pattern_type_option, N_("interpret patterns as fixed strings"), GREP_PATTERN_TYPE_FIXED), - OPT_SET_INT('P', "perl-regexp", &pattern_type_arg, + OPT_SET_INT('P', "perl-regexp", &opt.pattern_type_option, N_("use Perl-compatible regular expressions"), GREP_PATTERN_TYPE_PCRE), OPT_GROUP(""), @@ -982,7 +981,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix) argc = parse_options(argc, argv, prefix, options, grep_usage, PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_STOP_AT_NON_OPTION); - grep_commit_pattern_type(pattern_type_arg, &opt); if (use_index && !startup_info->have_repository) { int fallback = 0; diff --git a/grep.c b/grep.c index 8dfa0300786..b5342064066 100644 --- a/grep.c +++ b/grep.c @@ -33,9 +33,7 @@ static const char *color_grep_slots[] = { static int parse_pattern_type_arg(const char *opt, const char *arg) { - if (!strcmp(arg, "default")) - return GREP_PATTERN_TYPE_UNSPECIFIED; - else if (!strcmp(arg, "basic")) + if (!strcmp(arg, "basic")) return GREP_PATTERN_TYPE_BRE; else if (!strcmp(arg, "extended")) return GREP_PATTERN_TYPE_ERE; @@ -61,11 +59,23 @@ int grep_config(const char *var, const char *value, void *cb) return -1; if (!strcmp(var, "grep.extendedregexp")) { + if (opt->extended_regexp_option) + return 0; opt->extended_regexp_option = git_config_bool(var, value); + if (opt->extended_regexp_option) + opt->pattern_type_option = GREP_PATTERN_TYPE_ERE; + return 0; + } + + if (!strcmp(var, "grep.patterntype") && + !strcmp(value, "default")) { + opt->pattern_type_option = opt->extended_regexp_option == 1 + ? GREP_PATTERN_TYPE_ERE : GREP_PATTERN_TYPE_BRE; return 0; } if (!strcmp(var, "grep.patterntype")) { + opt->extended_regexp_option = -1; /* ignore */ opt->pattern_type_option = parse_pattern_type_arg(var, value); return 0; } @@ -115,62 +125,6 @@ void grep_init(struct grep_opt *opt, struct repository *repo) opt->header_tail = &opt->header_list; } -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: - break; - - case GREP_PATTERN_TYPE_ERE: - opt->extended_regexp_option = 1; - break; - - case GREP_PATTERN_TYPE_FIXED: - opt->fixed = 1; - break; - - case GREP_PATTERN_TYPE_PCRE: - opt->pcre2 = 1; - break; - } -} - -void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt) -{ - if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED) - grep_set_pattern_type_option(pattern_type, opt); - 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); -} - static struct grep_pat *create_grep_pat(const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t, @@ -490,9 +444,10 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) p->word_regexp = opt->word_regexp; p->ignore_case = opt->ignore_case; - p->fixed = opt->fixed; + p->fixed = opt->pattern_type_option == GREP_PATTERN_TYPE_FIXED; - if (memchr(p->pattern, 0, p->patternlen) && !opt->pcre2) + if (opt->pattern_type_option != GREP_PATTERN_TYPE_PCRE && + memchr(p->pattern, 0, p->patternlen)) die(_("given pattern contains NULL byte (via -f ). This is only supported with -P under PCRE v2")); p->is_fixed = is_fixed(p->pattern, p->patternlen); @@ -543,14 +498,14 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) return; } - if (opt->pcre2) { + if (opt->pattern_type_option == GREP_PATTERN_TYPE_PCRE) { compile_pcre2_pattern(p, opt); return; } if (p->ignore_case) regflags |= REG_ICASE; - if (opt->extended_regexp_option) + if (opt->pattern_type_option == GREP_PATTERN_TYPE_ERE) regflags |= REG_EXTENDED; err = regcomp(&p->regexp, p->pattern, regflags); if (err) { diff --git a/grep.h b/grep.h index b651eb291f7..ab2ce833b40 100644 --- a/grep.h +++ b/grep.h @@ -94,8 +94,7 @@ enum grep_expr_node { }; enum grep_pattern_type { - GREP_PATTERN_TYPE_UNSPECIFIED = 0, - GREP_PATTERN_TYPE_BRE, + GREP_PATTERN_TYPE_BRE = 0, GREP_PATTERN_TYPE_ERE, GREP_PATTERN_TYPE_FIXED, GREP_PATTERN_TYPE_PCRE @@ -143,7 +142,6 @@ struct grep_opt { int unmatch_name_only; int count; int word_regexp; - int fixed; int all_match; #define GREP_BINARY_DEFAULT 0 #define GREP_BINARY_NOMATCH 1 @@ -152,7 +150,6 @@ struct grep_opt { int allow_textconv; int extended; int use_reflog_filter; - int pcre2; int relative; int pathname; int null_following_name; @@ -162,7 +159,7 @@ struct grep_opt { int funcname; int funcbody; int extended_regexp_option; - int pattern_type_option; + enum grep_pattern_type pattern_type_option; int ignore_locale; char colors[NR_GREP_COLORS][COLOR_MAXLEN]; unsigned pre_context; @@ -181,7 +178,6 @@ struct grep_opt { .relative = 1, \ .pathname = 1, \ .max_depth = -1, \ - .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, \ .colors = { \ [GREP_COLOR_CONTEXT] = "", \ [GREP_COLOR_FILENAME] = "", \ @@ -200,7 +196,6 @@ struct grep_opt { int grep_config(const char *var, const char *value, void *); void grep_init(struct grep_opt *, struct repository *repo); -void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt); void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t); void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t); diff --git a/revision.c b/revision.c index 495328e859c..298d0ea7574 100644 --- a/revision.c +++ b/revision.c @@ -2860,8 +2860,6 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s diff_setup_done(&revs->diffopt); - grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED, - &revs->grep_filter); if (!is_encoding_utf8(get_log_output_encoding())) revs->grep_filter.ignore_locale = 1; compile_grep_patterns(&revs->grep_filter); -- 2.34.1.1146.gb52885e7c44