Skip to content

Commit 2a07e43

Browse files
Ossegitster
authored andcommitted
stash: handle specifying stashes with $IFS
When trying to pop/apply a stash specified with an argument containing IFS whitespace, git-stash will throw an error: $ git stash pop 'stash@{two hours ago}' Too many revisions specified: stash@{two hours ago} This happens because word splitting is used to count non-option arguments. Make use of rev-parse's --sq option to quote the arguments for us to ensure a correct count. Add quotes where necessary. Also add a test that verifies correct behaviour. Helped-by: Thomas Rast <[email protected]> Signed-off-by: Øystein Walle <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c90d3db commit 2a07e43

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

git-stash.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ parse_flags_and_rev()
358358
i_tree=
359359
u_tree=
360360

361-
REV=$(git rev-parse --no-flags --symbolic "$@") || exit 1
361+
REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
362362

363363
FLAGS=
364364
for opt
@@ -376,7 +376,7 @@ parse_flags_and_rev()
376376
esac
377377
done
378378

379-
set -- $REV
379+
eval set -- $REV
380380

381381
case $# in
382382
0)
@@ -391,13 +391,13 @@ parse_flags_and_rev()
391391
;;
392392
esac
393393

394-
REV=$(git rev-parse --quiet --symbolic --verify $1 2>/dev/null) || {
394+
REV=$(git rev-parse --quiet --symbolic --verify "$1" 2>/dev/null) || {
395395
reference="$1"
396396
die "$(eval_gettext "\$reference is not valid reference")"
397397
}
398398

399-
i_commit=$(git rev-parse --quiet --verify $REV^2 2>/dev/null) &&
400-
set -- $(git rev-parse $REV $REV^1 $REV: $REV^1: $REV^2: 2>/dev/null) &&
399+
i_commit=$(git rev-parse --quiet --verify "$REV^2" 2>/dev/null) &&
400+
set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
401401
s=$1 &&
402402
w_commit=$1 &&
403403
b_commit=$2 &&
@@ -408,8 +408,8 @@ parse_flags_and_rev()
408408
test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
409409
IS_STASH_REF=t
410410

411-
u_commit=$(git rev-parse --quiet --verify $REV^3 2>/dev/null) &&
412-
u_tree=$(git rev-parse $REV^3: 2>/dev/null)
411+
u_commit=$(git rev-parse --quiet --verify "$REV^3" 2>/dev/null) &&
412+
u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
413413
}
414414

415415
is_stash_like()

t/t3903-stash.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,16 @@ test_expect_success 'store updates stash ref and reflog' '
673673
grep quux bazzy
674674
'
675675

676+
test_expect_success 'handle stash specification with spaces' '
677+
git stash clear &&
678+
echo pig >file &&
679+
git stash &&
680+
stamp=$(git log -g --format="%cd" -1 refs/stash) &&
681+
test_tick &&
682+
echo cow >file &&
683+
git stash &&
684+
git stash apply "stash@{$stamp}" &&
685+
grep pig file
686+
'
687+
676688
test_done

0 commit comments

Comments
 (0)