Skip to content

Commit d6cc2df

Browse files
peffgitster
authored andcommitted
stash: complain about unknown flags
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 <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fdf96a2 commit d6cc2df

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

git-stash.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ list_stash () {
301301
}
302302

303303
show_stash () {
304+
ALLOW_UNKNOWN_FLAGS=t
304305
assert_stash_like "$@"
305306

306307
git diff ${FLAGS:---stat} $b_commit $w_commit
@@ -332,13 +333,14 @@ show_stash () {
332333
#
333334
# GIT_QUIET is set to t if -q is specified
334335
# INDEX_OPTION is set to --index if --index is specified.
335-
# FLAGS is set to the remaining flags
336+
# FLAGS is set to the remaining flags (if allowed)
336337
#
337338
# dies if:
338339
# * too many revisions specified
339340
# * no revision is specified and there is no stash stack
340341
# * a revision is specified which cannot be resolve to a SHA1
341342
# * a non-existent stash reference is specified
343+
# * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
342344
#
343345

344346
parse_flags_and_rev()
@@ -372,6 +374,8 @@ parse_flags_and_rev()
372374
INDEX_OPTION=--index
373375
;;
374376
-*)
377+
test "$ALLOW_UNKNOWN_FLAGS" = t ||
378+
die "$(eval_gettext "unknown option: \$opt")"
375379
FLAGS="${FLAGS}${FLAGS:+ }$opt"
376380
;;
377381
esac

t/t3903-stash.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ test_expect_success 'unstashing in a subdirectory' '
9393
)
9494
'
9595

96+
test_expect_success 'stash drop complains of extra options' '
97+
test_must_fail git stash drop --foo
98+
'
99+
96100
test_expect_success 'drop top stash' '
97101
git reset --hard &&
98102
git stash list > stashlist1 &&

0 commit comments

Comments
 (0)