Skip to content

Commit

Permalink
builtin/stash.c: let parse-options parse subcommands
Browse files Browse the repository at this point in the history
'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>
  • Loading branch information
szeder authored and gitster committed Aug 19, 2022
1 parent 1c3502b commit 2b057d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
53 changes: 24 additions & 29 deletions builtin/stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,11 @@ static int push_stash(int argc, const char **argv, const char *prefix,
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)
{
int keep_index = -1;
Expand Down Expand Up @@ -1787,15 +1792,28 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
pid_t pid = getpid();
const char *index_file;
struct strvec args = STRVEC_INIT;

parse_opt_subcommand_fn *fn = NULL;
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()
};

git_config(git_stash_config, NULL);

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);
the_repository->settings.command_requires_full_index = 0;
Expand All @@ -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,
(uintmax_t)pid);

if (!argc)
return !!push_stash(0, NULL, prefix, 0);
else if (!strcmp(argv[0], "apply"))
return !!apply_stash(argc, argv, 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]);
if (fn)
return !!fn(argc, argv, prefix);
else if (!argc)
return !!push_stash_unassumed(0, NULL, prefix);

/* Assume 'stash push' */
strvec_push(&args, "push");
Expand Down
2 changes: 1 addition & 1 deletion t/t3903-stash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test_expect_success 'usage on main command -h emits a summary of subcommands' '
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 &&
grep -F "usage: git stash [push" usage
'
Expand Down

0 comments on commit 2b057d9

Please sign in to comment.