Skip to content

Commit 875471c

Browse files
drafnelspearce
authored andcommitted
git-stash.sh: fix flawed fix of invalid ref handling (commit da65e7c)
The referenced commit tried to fix a flaw in stash's handling of a user supplied invalid ref. i.e. 'git stash apply fake_ref@{0}' should fail instead of applying stash@{0}. But, it did so in a naive way by avoiding the use of the --default option of rev-parse, and instead manually supplied the default revision if the user supplied an empty command line. This prevented a common usage scenario of supplying flags on the stash command line (i.e. non-empty command line) which would be parsed by lower level git commands, without supplying a specific revision. This should fall back to the default revision, but now it causes an error. e.g. 'git stash show -p' The correct fix is to use the --verify option of rev-parse, which fails properly if an invalid ref is supplied, and still allows falling back to a default ref when one is not supplied. Convert stash-drop to use --verify while we're at it, since specifying multiple revisions for any of these commands is also an error and --verify makes it so. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 027b5a4 commit 875471c

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

git-stash.sh

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,8 @@ show_stash () {
145145
flags=--stat
146146
fi
147147

148-
if test $# = 0
149-
then
150-
set x "$ref_stash@{0}"
151-
shift
152-
fi
153-
154-
s=$(git rev-parse --revs-only --no-flags "$@")
155-
156-
w_commit=$(git rev-parse --verify "$s") &&
157-
b_commit=$(git rev-parse --verify "$s^") &&
148+
w_commit=$(git rev-parse --verify --default $ref_stash "$@") &&
149+
b_commit=$(git rev-parse --verify "$w_commit^") &&
158150
git diff $flags $b_commit $w_commit
159151
}
160152

@@ -170,19 +162,13 @@ apply_stash () {
170162
shift
171163
esac
172164

173-
if test $# = 0
174-
then
175-
set x "$ref_stash@{0}"
176-
shift
177-
fi
178-
179165
# current index state
180166
c_tree=$(git write-tree) ||
181167
die 'Cannot apply a stash in the middle of a merge'
182168

183169
# stash records the work tree, and is a merge between the
184170
# base commit (first parent) and the index tree (second parent).
185-
s=$(git rev-parse --revs-only --no-flags "$@") &&
171+
s=$(git rev-parse --verify --default $ref_stash "$@") &&
186172
w_tree=$(git rev-parse --verify "$s:") &&
187173
b_tree=$(git rev-parse --verify "$s^1:") &&
188174
i_tree=$(git rev-parse --verify "$s^2:") ||
@@ -242,7 +228,7 @@ drop_stash () {
242228
shift
243229
fi
244230
# Verify supplied argument looks like a stash entry
245-
s=$(git rev-parse --revs-only --no-flags "$@") &&
231+
s=$(git rev-parse --verify "$@") &&
246232
git rev-parse --verify "$s:" > /dev/null 2>&1 &&
247233
git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
248234
git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||

0 commit comments

Comments
 (0)