Skip to content

Commit 2b057d9

Browse files
szedergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1c3502b commit 2b057d9

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

builtin/stash.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,11 @@ static int push_stash(int argc, const char **argv, const char *prefix,
17391739
include_untracked, only_staged);
17401740
}
17411741

1742+
static int push_stash_unassumed(int argc, const char **argv, const char *prefix)
1743+
{
1744+
return push_stash(argc, argv, prefix, 0);
1745+
}
1746+
17421747
static int save_stash(int argc, const char **argv, const char *prefix)
17431748
{
17441749
int keep_index = -1;
@@ -1787,15 +1792,28 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
17871792
pid_t pid = getpid();
17881793
const char *index_file;
17891794
struct strvec args = STRVEC_INIT;
1790-
1795+
parse_opt_subcommand_fn *fn = NULL;
17911796
struct option options[] = {
1797+
OPT_SUBCOMMAND("apply", &fn, apply_stash),
1798+
OPT_SUBCOMMAND("clear", &fn, clear_stash),
1799+
OPT_SUBCOMMAND("drop", &fn, drop_stash),
1800+
OPT_SUBCOMMAND("pop", &fn, pop_stash),
1801+
OPT_SUBCOMMAND("branch", &fn, branch_stash),
1802+
OPT_SUBCOMMAND("list", &fn, list_stash),
1803+
OPT_SUBCOMMAND("show", &fn, show_stash),
1804+
OPT_SUBCOMMAND("store", &fn, store_stash),
1805+
OPT_SUBCOMMAND("create", &fn, create_stash),
1806+
OPT_SUBCOMMAND("push", &fn, push_stash_unassumed),
1807+
OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE),
17921808
OPT_END()
17931809
};
17941810

17951811
git_config(git_stash_config, NULL);
17961812

17971813
argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1798-
PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_DASHDASH);
1814+
PARSE_OPT_SUBCOMMAND_OPTIONAL |
1815+
PARSE_OPT_KEEP_UNKNOWN_OPT |
1816+
PARSE_OPT_KEEP_DASHDASH);
17991817

18001818
prepare_repo_settings(the_repository);
18011819
the_repository->settings.command_requires_full_index = 0;
@@ -1804,33 +1822,10 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
18041822
strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
18051823
(uintmax_t)pid);
18061824

1807-
if (!argc)
1808-
return !!push_stash(0, NULL, prefix, 0);
1809-
else if (!strcmp(argv[0], "apply"))
1810-
return !!apply_stash(argc, argv, prefix);
1811-
else if (!strcmp(argv[0], "clear"))
1812-
return !!clear_stash(argc, argv, prefix);
1813-
else if (!strcmp(argv[0], "drop"))
1814-
return !!drop_stash(argc, argv, prefix);
1815-
else if (!strcmp(argv[0], "pop"))
1816-
return !!pop_stash(argc, argv, prefix);
1817-
else if (!strcmp(argv[0], "branch"))
1818-
return !!branch_stash(argc, argv, prefix);
1819-
else if (!strcmp(argv[0], "list"))
1820-
return !!list_stash(argc, argv, prefix);
1821-
else if (!strcmp(argv[0], "show"))
1822-
return !!show_stash(argc, argv, prefix);
1823-
else if (!strcmp(argv[0], "store"))
1824-
return !!store_stash(argc, argv, prefix);
1825-
else if (!strcmp(argv[0], "create"))
1826-
return !!create_stash(argc, argv, prefix);
1827-
else if (!strcmp(argv[0], "push"))
1828-
return !!push_stash(argc, argv, prefix, 0);
1829-
else if (!strcmp(argv[0], "save"))
1830-
return !!save_stash(argc, argv, prefix);
1831-
else if (*argv[0] != '-')
1832-
usage_msg_optf(_("unknown subcommand: %s"),
1833-
git_stash_usage, options, argv[0]);
1825+
if (fn)
1826+
return !!fn(argc, argv, prefix);
1827+
else if (!argc)
1828+
return !!push_stash_unassumed(0, NULL, prefix);
18341829

18351830
/* Assume 'stash push' */
18361831
strvec_push(&args, "push");

t/t3903-stash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test_expect_success 'usage on main command -h emits a summary of subcommands' '
2525
grep -F "or: git stash show" usage
2626
'
2727

28-
test_expect_failure 'usage for subcommands should emit subcommand usage' '
28+
test_expect_success 'usage for subcommands should emit subcommand usage' '
2929
test_expect_code 129 git stash push -h >usage &&
3030
grep -F "usage: git stash [push" usage
3131
'

0 commit comments

Comments
 (0)