]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: don't show "git stash push" usage on bad "git stash" usage
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Thu, 16 Dec 2021 12:54:21 +0000 (13:54 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Dec 2021 22:03:46 +0000 (14:03 -0800)
Change the usage message emitted by "git stash --invalid-option" to
emit usage information for "git stash" in general, and not just for
the "push" command. I.e. before:

    $ git stash --invalid-option
    error: unknown option `invalid-option'
    usage: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
                     [-u|--include-untracked] [-a|--all] [-m|--message <message>]
                     [--] [<pathspec>...]]
    [...]

After:

    $ git stash --invalid-option
    error: unknown option `invalid-option'
    usage: git stash list [<options>]
       or: git stash show [<options>] [<stash>]
       or: git stash drop [-q|--quiet] [<stash>]
       or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
       or: git stash branch <branchname> [<stash>]
       or: git stash clear
       or: git stash [push [-p|--patch] [-S|--staged] [-k|--[no-]keep-index] [-q|--quiet]
                     [-u|--include-untracked] [-a|--all] [-m|--message <message>]
                     [--pathspec-from-file=<file> [--pathspec-file-nul]]
                     [--] [<pathspec>...]]
       or: git stash save [-p|--patch] [-S|--staged] [-k|--[no-]keep-index] [-q|--quiet]
                     [-u|--include-untracked] [-a|--all] [<message>]
    [...]

That we emitted the usage for just "push" in the case of the
subcommand not being explicitly specified was an unintentional
side-effect of how it was implemented. When it was converted to C in
d553f538b8a (stash: convert push to builtin, 2019-02-25) the pattern
of having per-subcommand usage information was rightly continued. The
"git-stash.sh" shellscript did not have that, and always printed the
equivalent of "git_stash_usage".

But in doing so the case of push being implicit and explicit was
conflated. A variable was added to track this in 8c3713cede7 (stash:
eliminate crude option parsing, 2020-02-17), but it did not update the
usage output accordingly.

This still leaves e.g. "git stash push -h" emitting the
"git_stash_usage" output, instead of "git_stash_push_usage". That
should be fixed, but is a much deeper misbehavior in parse_options()
not being aware of subcommands at all. I.e. in how
PARSE_OPT_KEEP_UNKNOWN and PARSE_OPT_NO_INTERNAL_HELP combine in
commands such as "git stash".

Perhaps PARSE_OPT_KEEP_UNKNOWN should imply
PARSE_OPT_NO_INTERNAL_HELP, or better yet parse_options() should be
extended to fully handle these subcommand cases that we handle
manually in "git stash", "git commit-graph", "git multi-pack-index"
etc. All of those musings would be a much bigger change than this
isolated fix though, so let's leave that for some other time.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/t3903-stash.sh

index a0ccc8654dff70bd3014dca9d804a75bdd4c3cbc..26ebefaa68253ad4ab0c1b1fe035f65efde7a744 100644 (file)
@@ -1625,6 +1625,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
        if (argc) {
                force_assume = !strcmp(argv[0], "-p");
                argc = parse_options(argc, argv, prefix, options,
+                                    push_assumed ? git_stash_usage :
                                     git_stash_push_usage,
                                     PARSE_OPT_KEEP_DASHDASH);
        }
index f0a82be9de7654a0956b1d9a4f24e161869e021e..711f02ff6b5e0f58350c0fcad0964bfbd082fcc7 100755 (executable)
@@ -10,6 +10,25 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
 . ./test-lib.sh
 
+test_expect_success 'usage on cmd and subcommand invalid option' '
+       test_expect_code 129 git stash --invalid-option 2>usage &&
+       grep "or: git stash" usage &&
+
+       test_expect_code 129 git stash push --invalid-option 2>usage &&
+       ! grep "or: git stash" usage
+'
+
+test_expect_success 'usage on main command -h emits a summary of subcommands' '
+       test_expect_code 129 git stash -h >usage &&
+       grep -F "usage: git stash list" usage &&
+       grep -F "or: git stash show" usage
+'
+
+test_expect_failure 'usage for subcommands should emit subcommand usage' '
+       test_expect_code 129 git stash push -h >usage &&
+       grep -F "usage: git stash [push" usage
+'
+
 diff_cmp () {
        for i in "$1" "$2"
        do