Subject: Re: [PATCH 05/12] builtin/show-ref: refactor `--exclude-existing` options

From: Patrick Steinhardt <[email-masked]>
Date: Wed, 25 Oct 2023 13:50:44 +0200

On Tue, Oct 24, 2023 at 02:48:14PM -0400, Eric Sunshine wrote:
> On Tue, Oct 24, 2023 at 9:11 AM Patrick Steinhardt <[email-masked]> wrote:
> > It's not immediately obvious options which options are applicable to
> > what subcommand ni git-show-ref(1) because all options exist as global
> 
> s/ni/in/
> 
> > state. This can easily cause confusion for the reader.
> >
> > Refactor options for the `--exclude-existing` subcommand to be contained
> > in a separate structure. This structure is stored on the stack and
> > passed down as required. Consequentially, it clearly delimits the scope
> 
> s/Consequentially/Consequently/
> 
> > of those options and requires the reader to worry less about global
> > state.
> >
> > Signed-off-by: Patrick Steinhardt <[email-masked]>
> > ---
> > diff --git a/builtin/show-ref.c b/builtin/show-ref.c
> > @@ -95,6 +94,11 @@ static int add_existing(const char *refname,
> > +struct exclude_existing_options {
> > +       int enabled;
> > +       const char *pattern;
> > +};
> 
> Do we need this `enabled` flag? Can't the same be achieved by checking
> whether `pattern` is NULL or not (see below)?

Yeah, we do. It's perfectly valid to pass `--exclude-existing` without
the optional pattern argument. We still want to use this mode in that
case, but don't populate the pattern.

An alternative would be to assign something like a sentinel value in
here. But I'd think that it's clearer to instead have an explicit
separate field for this.

> > @@ -104,11 +108,11 @@ static int add_existing(const char *refname,
> > -static int cmd_show_ref__exclude_existing(const char *match)
> > +static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts)
> 
> Since you're renaming `match` to `opts->pattern`...
> 
> >  {
> > -       int matchlen = match ? strlen(match) : 0;
> > +       int matchlen = opts->pattern ? strlen(opts->pattern) : 0;
> 
> ... and since you're touching this line anyway, maybe it makes sense
> to rename `matchlen` to `patternlen`?

Yes, let's do it. It's been more of an oversight rather than
intentional to keep the previous name.

> > @@ -124,11 +128,11 @@ static int cmd_show_ref__exclude_existing(const char *match)
> > -                       if (strncmp(ref, match, matchlen))
> > +                       if (strncmp(ref, opts->pattern, matchlen))
> 
> Especially since, as shown in this context, `matchlen` is really the
> length of the _pattern_, not the length of the resulting _match_.
> 
> > @@ -200,44 +204,46 @@ static int hash_callback(const struct option *opt, const char *arg, int unset)
> >  int cmd_show_ref(int argc, const char **argv, const char *prefix)
> >  {
> >         [...]
> > -       if (exclude_arg)
> > -               return cmd_show_ref__exclude_existing(exclude_existing_arg);
> > +       if (exclude_existing_opts.enabled)
> > +               return cmd_show_ref__exclude_existing(&exclude_existing_opts);
> 
> (continued from above) Can't this be handled without a separate `enabled` flag?
> 
>     if (exclude_existing_opts.pattern)
>         ...

See the explanation above.

Patrick