Subject: Re: [PATCH v3 6/6] commit-graph: show usage on "commit-graph [write|verify] garbage"

From: SZEDER Gábor <[email-masked]>
Date: Tue, 20 Jul 2021 19:47:39 +0200

On Tue, Jul 20, 2021 at 01:39:45PM +0200, Ævar Arnfjörð Bjarmason wrote:
> Change the parse_options() invocation in the commit-graph code to make
> sense. We're calling it twice, once for common options parsing, and
> then for the sub-commands.
> 
> But we never checked if we had something leftover in argc in "write"
> or "verify", as a result we'd silently accept garbage in these
> subcommands. Let's not do that.
> 
> Signed-off-by: Ævar Arnfjörð Bjarmason <[email-masked]>
> ---
>  builtin/commit-graph.c  | 10 ++++++++--
>  t/t5318-commit-graph.sh |  5 +++++
>  2 files changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
> index bf34aa43f22..88cbcb5a64f 100644
> --- a/builtin/commit-graph.c
> +++ b/builtin/commit-graph.c
> @@ -104,7 +104,10 @@ static int graph_verify(int argc, const char **argv)
>  	opts.progress = isatty(2);
>  	argc = parse_options(argc, argv, NULL,
>  			     options,
> -			     builtin_commit_graph_verify_usage, 0);
> +			     builtin_commit_graph_verify_usage,
> +			     PARSE_OPT_KEEP_UNKNOWN);
> +	if (argc)
> +		usage_with_options(builtin_commit_graph_verify_usage, options);

Checking 'argc' alone is sufficient to catch unsupported parameters.

Using PARSE_OPT_KEEP_UNKNOWN is not only unnecessary, but arguably
wrong here, because 'git commit-graph write --foo' won't print "error:
unknown option `foo'", and we don't want to pass the remaining
unrecognized options to a different command, like e.g. 'git difftool',
or another parse_options(), like e.g. 'git archive'.

>  	if (!opts.obj_dir)
>  		opts.obj_dir = get_object_directory();
> @@ -261,7 +264,10 @@ static int graph_write(int argc, const char **argv)
>  
>  	argc = parse_options(argc, argv, NULL,
>  			     options,
> -			     builtin_commit_graph_write_usage, 0);
> +			     builtin_commit_graph_write_usage,
> +			     PARSE_OPT_KEEP_UNKNOWN);
> +	if (argc)
> +		usage_with_options(builtin_commit_graph_write_usage, options);
>  
>  	if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
>  		die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
> diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
> index af88f805aa2..09a2ccd2920 100755
> --- a/t/t5318-commit-graph.sh
> +++ b/t/t5318-commit-graph.sh
> @@ -5,6 +5,11 @@ test_description='commit graph'
>  
>  GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
>  
> +test_expect_success 'usage' '
> +	test_expect_code 129 git commit-graph write blah &&
> +	test_expect_code 129 git commit-graph write verify
> +'
> +
>  test_expect_success 'setup full repo' '
>  	mkdir full &&
>  	cd "$TRASH_DIRECTORY/full" &&
> -- 
> 2.32.0.874.ge7a9d58bfcf
>