All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Martin Ågren" <martin.agren@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 4/8] builtin/config: collect "value_regexp" data in a struct
Date: Thu, 21 Nov 2019 14:22:52 +0900	[thread overview]
Message-ID: <xmqqy2w9951v.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <336eaa77e4974f84ea1eef473672e1d300f3a43d.1573670565.git.martin.agren@gmail.com> ("Martin =?utf-8?Q?=C3=85gren=22's?= message of "Wed, 13 Nov 2019 19:55:03 +0100")

Martin Ågren <martin.agren@gmail.com> writes:

> `git config` can take an optional "value_regexp". Collect the
> `regex_t`-pointer and the `do_not_match` flag into a new `struct
> cmd_line_value`.

A "struct cmd_line_value" sounded, to me at least during my first
reading, as if it is about all command line options, but that is not
at all what you meant to imply.  Is this only about the optional
value-regexp (if so perhaps calling it "value_regexp_option" would
have helped me avoid such a misunderstanding)?

> Rather than signalling/judging presence of a regexp by the NULL-ness of
> the pointer, introduce a `mode` enum.

OK.  Tangentially this makes readers wonder why the existing code
for key_regexp does not follow the same "NULL-ness" pattern but has
a separate use_key_regexp boolean.  It appears that the original
code is quite confused---it is totally outside the scope of this
series to clean it up and inject sanity into it though ;-)

>  static regex_t *key_regexp;
> -static regex_t *regexp;
> +static struct {
> +	enum { none, regexp } mode;

We often use the same identifier for a struct and an instance of the
struct, taking advantage of the fact that they live in separate
namespaces, but lowercase enumerated values like 'regexp' that
collides with the field name (and possibly a variable name used
elsewhere) smells a bit too much.

> +	regex_t *regexp;
> +	int do_not_match; /* used with `regexp` */
> +} cmd_line_value;
>  static int show_keys;
>  static int omit_values;
>  static int use_key_regexp;

> @@ -283,19 +288,21 @@ static int collect_config(const char *key_, const char *value_, void *cb)
>  static int handle_value_regex(const char *regex_)
>  {
>  	if (!regex_) {
> -		regexp = NULL;
> +		cmd_line_value.mode = none;
>  		return 0;

Now we are back to relying on cmd_line_value.regexp staying to be
NULL after initialized, which is the state before the previous
patch.  If the end result is correct, then it is OK, I think, but
then the previous step shouldn't have added the NULL assignment here
in the first place.

>  	}
>  
> +	cmd_line_value.mode = regexp;
> +
>  	if (regex_[0] == '!') {
> -		do_not_match = 1;
> +		cmd_line_value.do_not_match = 1;
>  		regex_++;
>  	}
>  
> -	regexp = (regex_t*)xmalloc(sizeof(regex_t));
> -	if (regcomp(regexp, regex_, REG_EXTENDED)) {
> +	cmd_line_value.regexp = xmalloc(sizeof(*cmd_line_value.regexp));
> +	if (regcomp(cmd_line_value.regexp, regex_, REG_EXTENDED)) {
>  		error(_("invalid pattern: %s"), regex_);
> -		FREE_AND_NULL(regexp);
> +		FREE_AND_NULL(cmd_line_value.regexp);

Hmph.  !regexp in old code should mean cmd_line_value.mode==regexp
in the new world order after this patch is applied, no?  Should we
be treaking the mode field here before we leave?  I think it should
not matter, but thought it wouldn't hurt to ask.

In collect_config(), cmd_line_value.regexp is blindly passed to
regexec(3) as long as cmd_line_value.mode==regexp, so the invariant
"when .mode is regexp, .regexp must be valid, or collect_config() would
never be called for such cmd_line_value" is rather important to
avoid crashing ;-)

>  		return CONFIG_INVALID_PATTERN;
>  	}
>  
> @@ -372,9 +379,9 @@ static int get_value(const char *key_, const char *regex_)
>  		regfree(key_regexp);
>  		free(key_regexp);
>  	}
> -	if (regexp) {
> -		regfree(regexp);
> -		free(regexp);
> +	if (cmd_line_value.regexp) {
> +		regfree(cmd_line_value.regexp);
> +		free(cmd_line_value.regexp);

Likewise.

>  	}
>  
>  	return ret;

  reply	other threads:[~2019-11-21  5:23 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-13 18:54 [PATCH 0/8] builtin/config: canonicalize "value_regex" with `--type=bool[-or-int]` Martin Ågren
2019-11-13 18:55 ` [PATCH 1/8] config: make `git_parse_maybe_bool_text()` public Martin Ågren
2019-11-13 18:55 ` [PATCH 2/8] t1300: modernize part of script Martin Ågren
2019-11-21  4:54   ` Junio C Hamano
2019-11-13 18:55 ` [PATCH 3/8] builtin/config: extract `handle_value_regex()` from `get_value()` Martin Ågren
2019-11-21  5:02   ` Junio C Hamano
2019-11-21 19:53     ` Martin Ågren
2019-11-13 18:55 ` [PATCH 4/8] builtin/config: collect "value_regexp" data in a struct Martin Ågren
2019-11-21  5:22   ` Junio C Hamano [this message]
2019-11-21 19:55     ` Martin Ågren
2019-11-22  6:30       ` Junio C Hamano
2019-11-13 18:55 ` [PATCH 5/8] builtin/config: canonicalize "value_regex" with `--type=bool` Martin Ågren
2019-11-21  5:37   ` Junio C Hamano
2019-11-13 18:55 ` [PATCH 6/8] builtin/config: canonicalize "value_regex" with `--type=bool-or-int` Martin Ågren
2019-11-13 18:55 ` [PATCH 7/8] builtin/config: warn if "value_regex" doesn't canonicalize as boolean Martin Ågren
2019-11-21  5:43   ` Junio C Hamano
2019-11-21 19:58     ` Martin Ågren
2019-11-13 18:55 ` [PATCH 8/8] builtin/config: die " Martin Ågren
2019-11-14  2:18 ` [PATCH 0/8] builtin/config: canonicalize "value_regex" with `--type=bool[-or-int]` Junio C Hamano
2019-11-14  6:40   ` Martin Ågren
2019-11-14  6:29 ` Jeff King
2019-11-14  6:54   ` Martin Ågren
2019-11-14  7:37     ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqy2w9951v.fsf@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=martin.agren@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.