diff mbox series

push: unset PARSE_OPT_OPTARG for --recurse-submodules

Message ID c2b92691ca85029fde3bd6969252cc827ca697b5.1587969824.git.liu.denton@gmail.com (mailing list archive)
State New, archived
Headers show
Series push: unset PARSE_OPT_OPTARG for --recurse-submodules | expand

Commit Message

Denton Liu April 27, 2020, 6:44 a.m. UTC
When the usage for `git push` is shown, it includes the following
lines

	--recurse-submodules[=(check|on-demand|no)]
			      control recursive pushing of submodules

which seem to indicate that the argument for --recurse-submodules is
optional. However, we cannot actually run that optiion without an
argument:

	$ git push --recurse-submodules
	fatal: recurse-submodules missing parameter

Unset PARSE_OPT_OPTARG so that it is clear that this option requires an
argument. Since the parse-options machinery guarantees that an argument
is present now, assume that `arg` is set in the else of
option_parse_recurse_submodules().

Reported-by: Andrew White <andrew.white@audinate.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 builtin/push.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Jeff King April 27, 2020, 12:02 p.m. UTC | #1
On Mon, Apr 27, 2020 at 02:44:08AM -0400, Denton Liu wrote:

> When the usage for `git push` is shown, it includes the following
> lines
> 
> 	--recurse-submodules[=(check|on-demand|no)]
> 			      control recursive pushing of submodules
> 
> which seem to indicate that the argument for --recurse-submodules is
> optional. However, we cannot actually run that optiion without an
> argument:
> 
> 	$ git push --recurse-submodules
> 	fatal: recurse-submodules missing parameter
> 
> Unset PARSE_OPT_OPTARG so that it is clear that this option requires an
> argument. Since the parse-options machinery guarantees that an argument
> is present now, assume that `arg` is set in the else of
> option_parse_recurse_submodules().

Yeah, I think this is the right solution. It looks like it was broken
since the option was introduced in d2b17b3220 (push: Don't push a
repository with unpushed submodules, 2011-08-20).

I wondered if it was copied from another similar option in another
command, and if so whether that option had the same problem. But it
doesn't look like it. The other --recurse-submodules options are all
PARSE_OPT_OPTARG, but they actually do something useful when they are
not given an option (they turn it to "on").

I don't know enough about the "push" case to say whether it would be a
good idea for it to behave similarly, but certainly your patch is an
improvement until somebody decides to look into it.

> diff --git a/builtin/push.c b/builtin/push.c
> index 6dbf0f0bb7..90f071fcf2 100644
> --- a/builtin/push.c
> +++ b/builtin/push.c
> @@ -434,10 +434,8 @@ static int option_parse_recurse_submodules(const struct option *opt,
>  
>  	if (unset)
>  		*recurse_submodules = RECURSE_SUBMODULES_OFF;
> -	else if (arg)
> -		*recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
>  	else
> -		die("%s missing parameter", opt->long_name);
> +		*recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);

What a lousy diff. It would be much easier to read if we chose to
replace the "else" and keep the big complicated line intact. :)
Both Myers and --histogram give the diff above, but --patience gives:

diff --git a/builtin/push.c b/builtin/push.c
index 6dbf0f0bb7..ac6cc07c8c 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -434,10 +434,8 @@ static int option_parse_recurse_submodules(const struct option *opt,
 
 	if (unset)
 		*recurse_submodules = RECURSE_SUBMODULES_OFF;
-	else if (arg)
+	else
 		*recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
-	else
-		die("%s missing parameter", opt->long_name);
 
 	return 0;
 }

Obviously not a complaint about your patch. I'm always just curious to
see cases where the various diff implementations do better or worse than
each other.

> @@ -554,7 +552,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
>  		  PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option },
>  		{ OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
>  			N_("control recursive pushing of submodules"),
> -			PARSE_OPT_OPTARG, option_parse_recurse_submodules },
> +			0, option_parse_recurse_submodules },

This could collapse down to OPT_CALLBACK() now, though I don't think
it's a big deal either way.

-Peff
Junio C Hamano April 27, 2020, 6:54 p.m. UTC | #2
Jeff King <peff@peff.net> writes:

>> @@ -554,7 +552,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
>>  		  PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option },
>>  		{ OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
>>  			N_("control recursive pushing of submodules"),
>> -			PARSE_OPT_OPTARG, option_parse_recurse_submodules },
>> +			0, option_parse_recurse_submodules },
>
> This could collapse down to OPT_CALLBACK() now, though I don't think
> it's a big deal either way.

I would prefer to see OPT_CALLBACK() used; it would send a strong
signal that this place is using the canned bog-standard and boring
pattern, and nothing fancy is going on.

Thanks.
Junio C Hamano April 28, 2020, 5:51 p.m. UTC | #3
Junio C Hamano <gitster@pobox.com> writes:

> Jeff King <peff@peff.net> writes:
>
>>> @@ -554,7 +552,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
>>>  		  PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option },
>>>  		{ OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
>>>  			N_("control recursive pushing of submodules"),
>>> -			PARSE_OPT_OPTARG, option_parse_recurse_submodules },
>>> +			0, option_parse_recurse_submodules },
>>
>> This could collapse down to OPT_CALLBACK() now, though I don't think
>> it's a big deal either way.
>
> I would prefer to see OPT_CALLBACK() used; it would send a strong
> signal that this place is using the canned bog-standard and boring
> pattern, and nothing fancy is going on.

Here is what I am going to queue for this fix; hopefully it can be
cherry-picked by folks who maintain older versions of Git for
distros and in-house use.

The other "huge" patch has also been adjusted by dropping the first
hunk on builtin/push.c from it, and then resurrecting the part to
use OPT_CALLBACK_F() for compare-and-swap (which happened to be in
the same hunk).

Thanks for the fix and clean-up.  Very much appreciated.

-- >8 --
From: Denton Liu <liu.denton@gmail.com>
Date: Mon, 27 Apr 2020 02:44:08 -0400
Subject: [PATCH] push: unset PARSE_OPT_OPTARG for --recurse-submodules

When the usage for `git push` is shown, it includes the following
lines

	--recurse-submodules[=(check|on-demand|no)]
			      control recursive pushing of submodules

which seem to indicate that the argument for --recurse-submodules is
optional. However, we cannot actually run that optiion without an
argument:

	$ git push --recurse-submodules
	fatal: recurse-submodules missing parameter

Unset PARSE_OPT_OPTARG so that it is clear that this option requires an
argument. Since the parse-options machinery guarantees that an argument
is present now, assume that `arg` is set in the else of
option_parse_recurse_submodules().

Reported-by: Andrew White <andrew.white@audinate.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/push.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/builtin/push.c b/builtin/push.c
index 6dbf0f0bb7..208c2540e9 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -434,10 +434,8 @@ static int option_parse_recurse_submodules(const struct option *opt,
 
 	if (unset)
 		*recurse_submodules = RECURSE_SUBMODULES_OFF;
-	else if (arg)
-		*recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
 	else
-		die("%s missing parameter", opt->long_name);
+		*recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
 
 	return 0;
 }
@@ -552,9 +550,8 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 		  0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
 		  N_("require old value of ref to be at this value"),
 		  PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option },
-		{ OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
-			N_("control recursive pushing of submodules"),
-			PARSE_OPT_OPTARG, option_parse_recurse_submodules },
+		OPT_CALLBACK(0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
+			     N_("control recursive pushing of submodules"), option_parse_recurse_submodules),
 		OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
 		OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
 		OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
Denton Liu April 28, 2020, 10:59 p.m. UTC | #4
Hi Junio,

On Tue, Apr 28, 2020 at 10:51:56AM -0700, Junio C Hamano wrote:
> Here is what I am going to queue for this fix; hopefully it can be
> cherry-picked by folks who maintain older versions of Git for
> distros and in-house use.
> 
> The other "huge" patch has also been adjusted by dropping the first
> hunk on builtin/push.c from it, and then resurrecting the part to
> use OPT_CALLBACK_F() for compare-and-swap (which happened to be in
> the same hunk).

Thanks, that approach makes sense to me. I looked at both patches that
you have queued and they look good.

-Denton
diff mbox series

Patch

diff --git a/builtin/push.c b/builtin/push.c
index 6dbf0f0bb7..90f071fcf2 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -434,10 +434,8 @@  static int option_parse_recurse_submodules(const struct option *opt,
 
 	if (unset)
 		*recurse_submodules = RECURSE_SUBMODULES_OFF;
-	else if (arg)
-		*recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
 	else
-		die("%s missing parameter", opt->long_name);
+		*recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
 
 	return 0;
 }
@@ -554,7 +552,7 @@  int cmd_push(int argc, const char **argv, const char *prefix)
 		  PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option },
 		{ OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
 			N_("control recursive pushing of submodules"),
-			PARSE_OPT_OPTARG, option_parse_recurse_submodules },
+			0, option_parse_recurse_submodules },
 		OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
 		OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
 		OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),