Skip to content

Commit 59d418f

Browse files
peffgitster
authored andcommitted
stash: fix accidental apply of non-existent stashes
Once upon a time, "git rev-parse ref@{9999999}" did not generate an error. Therefore when we got an invalid stash reference in "stash apply", we could end up not noticing until quite late. Commit b0f0ecd (detached-stash: work around git rev-parse failure to detect bad log refs, 2010-08-21) handled this by checking for the "Log for stash has only %d entries" warning on stderr when we validated the ref. A few days later, e6eedc3 (rev-parse: exit with non-zero status if ref@{n} is not valid., 2010-08-24) fixed the original issue. That made the extra stderr test superfluous, but also introduced a new bug. Now the early call to: git rev-parse --symbolic "$@" fails, but we don't notice the exit code. Worse, its empty output means we think the user didn't provide us a ref, and we try to apply stash@{0}. This patch checks the rev-parse exit code and fails early in the revision parsing process. We can also get rid of the stderr test; as a bonus, this means that "stash apply" can now run under GIT_TRACE=1 properly. Signed-off-by: Jeff King <[email protected]> Acked-by: Jon Seymour <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7ed863a commit 59d418f

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

git-stash.sh

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ parse_flags_and_rev()
264264
b_tree=
265265
i_tree=
266266

267-
REV=$(git rev-parse --no-flags --symbolic "$@" 2>/dev/null)
267+
REV=$(git rev-parse --no-flags --symbolic "$@") || exit 1
268268

269269
FLAGS=
270270
for opt
@@ -310,16 +310,6 @@ parse_flags_and_rev()
310310
IS_STASH_LIKE=t &&
311311
test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
312312
IS_STASH_REF=t
313-
314-
if test "${REV}" != "${REV%{*\}}"
315-
then
316-
# maintainers: it would be better if git rev-parse indicated
317-
# this condition with a non-zero status code but as of 1.7.2.1 it
318-
# it did not. So, we use non-empty stderr output as a proxy for the
319-
# condition of interest.
320-
test -z "$(git rev-parse "$REV" 2>&1 >/dev/null)" || die "$REV does not exist in the stash log"
321-
fi
322-
323313
}
324314

325315
is_stash_like()

t/t3903-stash.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ test_expect_success 'parents of stash' '
3737
test_cmp output expect
3838
'
3939

40+
test_expect_success 'applying bogus stash does nothing' '
41+
test_must_fail git stash apply stash@{1} &&
42+
echo 1 >expect &&
43+
test_cmp expect file
44+
'
45+
4046
test_expect_success 'apply needs clean working directory' '
4147
echo 4 > other-file &&
4248
git add other-file &&

0 commit comments

Comments
 (0)