stash: complain about unknown flags
authorJeff King <peff@peff.net>
Wed, 20 May 2015 18:01:32 +0000 (14:01 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 20 May 2015 20:47:30 +0000 (13:47 -0700)
The option parser for git-stash stuffs unknown flags into
the $FLAGS variable, where they can be accessed by the
individual commands. However, most commands do not even look
at these extra flags, leading to unexpected results like
this:

$ git stash drop --help
Dropped refs/stash@{0} (e6cf6d80faf92bb7828f7b60c47fc61c03bd30a1)

We should notice the extra flags and bail. Rather than
annotate each command to reject a non-empty $FLAGS variable,
we can notice that "stash show" is the only command that
actually _wants_ arbitrary flags. So we switch the default
mode to reject unknown flags, and let stash_show() opt into
the feature.

Reported-by: Vincent Legoll <vincent.legoll@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-stash.sh
t/t3903-stash.sh
index d4cf818be9488f1b94fdf4766a8f73db6bfd1029..f540127da3b211250c7a09451726edada71ba8f1 100755 (executable)
@@ -301,6 +301,7 @@ list_stash () {
 }
 
 show_stash () {
+       ALLOW_UNKNOWN_FLAGS=t
        assert_stash_like "$@"
 
        git diff ${FLAGS:---stat} $b_commit $w_commit
@@ -332,13 +333,14 @@ show_stash () {
 #
 #   GIT_QUIET is set to t if -q is specified
 #   INDEX_OPTION is set to --index if --index is specified.
-#   FLAGS is set to the remaining flags
+#   FLAGS is set to the remaining flags (if allowed)
 #
 # dies if:
 #   * too many revisions specified
 #   * no revision is specified and there is no stash stack
 #   * a revision is specified which cannot be resolve to a SHA1
 #   * a non-existent stash reference is specified
+#   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
 #
 
 parse_flags_and_rev()
@@ -372,6 +374,8 @@ parse_flags_and_rev()
                                INDEX_OPTION=--index
                        ;;
                        -*)
+                               test "$ALLOW_UNKNOWN_FLAGS" = t ||
+                                       die "$(eval_gettext "unknown option: \$opt")"
                                FLAGS="${FLAGS}${FLAGS:+ }$opt"
                        ;;
                esac
index 1e29962fad30edb263cd07b6586f1b1a962c88cf..384678953e81745c048f357cdb05d2322e88a7b2 100755 (executable)
@@ -93,6 +93,10 @@ test_expect_success 'unstashing in a subdirectory' '
        )
 '
 
+test_expect_success 'stash drop complains of extra options' '
+       test_must_fail git stash drop --foo
+'
+
 test_expect_success 'drop top stash' '
        git reset --hard &&
        git stash list > stashlist1 &&