builtin/stash.c: let parse-options parse subcommands

'git stash' parses its subcommands with a long list of if-else if
statements.  parse-options has just learned to parse subcommands, so
let's use that facility instead, with the benefits of shorter code,
and listing subcommands for Bash completion.

Note that the push_stash() function implementing the 'push' subcommand
accepts an extra flag parameter to indicate whether push was assumed,
so add a wrapper function with the standard subcommand function
signature.

Note also that this change "hides" the '-h' option in 'git stash push
-h' from the parse_option() call in cmd_stash(), as it comes after the
subcommand.  Consequently, from now on it will emit the usage of the
'push' subcommand instead of the usage of 'git stash'.  We had a
failing test for this case, which can now be flipped to expect
success.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
SZEDER Gábor 2022-08-19 18:04:10 +02:00 committed by Junio C Hamano
parent 1c3502b198
commit 2b057d97d7
2 changed files with 25 additions and 30 deletions

View File

@ -1739,6 +1739,11 @@ static int push_stash(int argc, const char **argv, const char *prefix,
include_untracked, only_staged); include_untracked, only_staged);
} }
static int push_stash_unassumed(int argc, const char **argv, const char *prefix)
{
return push_stash(argc, argv, prefix, 0);
}
static int save_stash(int argc, const char **argv, const char *prefix) static int save_stash(int argc, const char **argv, const char *prefix)
{ {
int keep_index = -1; int keep_index = -1;
@ -1787,15 +1792,28 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
pid_t pid = getpid(); pid_t pid = getpid();
const char *index_file; const char *index_file;
struct strvec args = STRVEC_INIT; struct strvec args = STRVEC_INIT;
parse_opt_subcommand_fn *fn = NULL;
struct option options[] = { struct option options[] = {
OPT_SUBCOMMAND("apply", &fn, apply_stash),
OPT_SUBCOMMAND("clear", &fn, clear_stash),
OPT_SUBCOMMAND("drop", &fn, drop_stash),
OPT_SUBCOMMAND("pop", &fn, pop_stash),
OPT_SUBCOMMAND("branch", &fn, branch_stash),
OPT_SUBCOMMAND("list", &fn, list_stash),
OPT_SUBCOMMAND("show", &fn, show_stash),
OPT_SUBCOMMAND("store", &fn, store_stash),
OPT_SUBCOMMAND("create", &fn, create_stash),
OPT_SUBCOMMAND("push", &fn, push_stash_unassumed),
OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE),
OPT_END() OPT_END()
}; };
git_config(git_stash_config, NULL); git_config(git_stash_config, NULL);
argc = parse_options(argc, argv, prefix, options, git_stash_usage, argc = parse_options(argc, argv, prefix, options, git_stash_usage,
PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_DASHDASH); PARSE_OPT_SUBCOMMAND_OPTIONAL |
PARSE_OPT_KEEP_UNKNOWN_OPT |
PARSE_OPT_KEEP_DASHDASH);
prepare_repo_settings(the_repository); prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0; the_repository->settings.command_requires_full_index = 0;
@ -1804,33 +1822,10 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
(uintmax_t)pid); (uintmax_t)pid);
if (!argc) if (fn)
return !!push_stash(0, NULL, prefix, 0); return !!fn(argc, argv, prefix);
else if (!strcmp(argv[0], "apply")) else if (!argc)
return !!apply_stash(argc, argv, prefix); return !!push_stash_unassumed(0, NULL, prefix);
else if (!strcmp(argv[0], "clear"))
return !!clear_stash(argc, argv, prefix);
else if (!strcmp(argv[0], "drop"))
return !!drop_stash(argc, argv, prefix);
else if (!strcmp(argv[0], "pop"))
return !!pop_stash(argc, argv, prefix);
else if (!strcmp(argv[0], "branch"))
return !!branch_stash(argc, argv, prefix);
else if (!strcmp(argv[0], "list"))
return !!list_stash(argc, argv, prefix);
else if (!strcmp(argv[0], "show"))
return !!show_stash(argc, argv, prefix);
else if (!strcmp(argv[0], "store"))
return !!store_stash(argc, argv, prefix);
else if (!strcmp(argv[0], "create"))
return !!create_stash(argc, argv, prefix);
else if (!strcmp(argv[0], "push"))
return !!push_stash(argc, argv, prefix, 0);
else if (!strcmp(argv[0], "save"))
return !!save_stash(argc, argv, prefix);
else if (*argv[0] != '-')
usage_msg_optf(_("unknown subcommand: %s"),
git_stash_usage, options, argv[0]);
/* Assume 'stash push' */ /* Assume 'stash push' */
strvec_push(&args, "push"); strvec_push(&args, "push");

View File

@ -25,7 +25,7 @@ test_expect_success 'usage on main command -h emits a summary of subcommands' '
grep -F "or: git stash show" usage grep -F "or: git stash show" usage
' '
test_expect_failure 'usage for subcommands should emit subcommand usage' ' test_expect_success 'usage for subcommands should emit subcommand usage' '
test_expect_code 129 git stash push -h >usage && test_expect_code 129 git stash push -h >usage &&
grep -F "usage: git stash [push" usage grep -F "usage: git stash [push" usage
' '