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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 13, 2017 at 13:38 UTC 22fc703ec949602e9fd4e2ab0bb63dd47c2945b5
2 files changed +19
git-stash.sh
+3
@@ -19,6 +19,7 @@ OPTIONS_SPEC=
19 START_DIR=$(pwd)
20 . git-sh-setup
21 require_work_tree
22 +prefix=$(git rev-parse --show-prefix) || exit 1
23 cd_to_toplevel
24
25 TMP="$GIT_DIR/.git-stash.$$"
@@ -273,6 +274,8 @@ push_stash () {
274 shift
275 done
276
277 + eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
278 +
279 if test -n "$patch_mode" && test -n "$untracked"
280 then
281 die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")"
t/t3903-stash.sh
+16
@@ -812,6 +812,22 @@ test_expect_success 'stash -- <pathspec> stashes and restores the file' '
812 test_path_is_file bar
813 '
814
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 +
831 test_expect_success 'stash with multiple pathspec arguments' '
832 >foo &&
833 >bar &&