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>

SZEDER Gábor committed Aug 19, 2022 at 18:04 UTC 2b057d97d796d9192ec899a2109924cabba23ba6
2 files changed +25 -30
builtin/stash.c
+24 -29
@@ -1739,6 +1739,11 @@ static int push_stash(int argc, const char **argv, const char *prefix,
1739 include_untracked, only_staged);
1740 }
1741
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 +
1747 static int save_stash(int argc, const char **argv, const char *prefix)
1748 {
1749 int keep_index = -1;
@@ -1787,15 +1792,28 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
1792 pid_t pid = getpid();
1793 const char *index_file;
1794 struct strvec args = STRVEC_INIT;
1790 -
1795 + parse_opt_subcommand_fn *fn = NULL;
1796 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),
1808 OPT_END()
1809 };
1810
1811 git_config(git_stash_config, NULL);
1812
1813 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);
1817
1818 prepare_repo_settings(the_repository);
1819 the_repository->settings.command_requires_full_index = 0;
@@ -1804,33 +1822,10 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
1822 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1823 (uintmax_t)pid);
1824
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);
1829
1830 /* Assume 'stash push' */
1831 strvec_push(&args, "push");
t/t3903-stash.sh
+1 -1
@@ -25,7 +25,7 @@ test_expect_success 'usage on main command -h emits a summary of subcommands' '
25 grep -F "or: git stash show" usage
26 '
27
28 -test_expect_failure 'usage for subcommands should emit subcommand usage' '
28 +test_expect_success 'usage for subcommands should emit subcommand usage' '
29 test_expect_code 129 git stash push -h >usage &&
30 grep -F "usage: git stash [push" usage
31 '