Skip to content

Commit 22fc703

Browse files
pks-tgitster
authored andcommitted
git-stash: fix pushing stash with pathspec from subdir
The `git stash push` command recently gained the ability to get a pathspec as its argument to only stash matching files. Calling this command from a subdirectory does not work, though, as one of the first things we do is changing to the top level directory without keeping track of the prefix from which the command is being run. Fix the shortcoming by storing the prefix previous to the call to `cd_to_toplevel` and then subsequently using `git rev-parse --prefix` to correctly resolve the pathspec. Add a test to catch future breakage of this usecase. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3851e44 commit 22fc703

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

git-stash.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ OPTIONS_SPEC=
1919
START_DIR=$(pwd)
2020
. git-sh-setup
2121
require_work_tree
22+
prefix=$(git rev-parse --show-prefix) || exit 1
2223
cd_to_toplevel
2324

2425
TMP="$GIT_DIR/.git-stash.$$"
@@ -273,6 +274,8 @@ push_stash () {
273274
shift
274275
done
275276

277+
eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
278+
276279
if test -n "$patch_mode" && test -n "$untracked"
277280
then
278281
die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")"

t/t3903-stash.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,22 @@ test_expect_success 'stash -- <pathspec> stashes and restores the file' '
812812
test_path_is_file bar
813813
'
814814

815+
test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
816+
mkdir sub &&
817+
>foo &&
818+
>bar &&
819+
git add foo bar &&
820+
(
821+
cd sub &&
822+
git stash push -- ../foo
823+
) &&
824+
test_path_is_file bar &&
825+
test_path_is_missing foo &&
826+
git stash pop &&
827+
test_path_is_file foo &&
828+
test_path_is_file bar
829+
'
830+
815831
test_expect_success 'stash with multiple pathspec arguments' '
816832
>foo &&
817833
>bar &&

0 commit comments

Comments
 (0)