Merge branch 'jk/maint-stash-oob'
authorJunio C Hamano <gitster@pobox.com>
Wed, 27 Apr 2011 18:36:42 +0000 (11:36 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 Apr 2011 18:36:42 +0000 (11:36 -0700)
* jk/maint-stash-oob:
stash: fix false positive in the invalid ref test.
stash: fix accidental apply of non-existent stashes

Conflicts:
t/t3903-stash.sh

1  2 
git-stash.sh
t/t3903-stash.sh
diff --combined git-stash.sh
index fbd4bc152d7fb0ba81f0dd5c999ffba3c2276108,4f26deacca64f3ce5e0cd4b00745ab8ecf52b551..07ac3235904d7f0ac6c2e79af8b35f84c8f0874c
@@@ -12,14 -12,12 +12,14 @@@ USAGE="list [<options>
  
  SUBDIRECTORY_OK=Yes
  OPTIONS_SPEC=
 +START_DIR=`pwd`
  . git-sh-setup
  require_work_tree
  cd_to_toplevel
  
  TMP="$GIT_DIR/.git-stash.$$"
 -trap 'rm -f "$TMP-*"' 0
 +TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
 +trap 'rm -f "$TMP-"* "$TMPindex"' 0
  
  ref_stash=refs/stash
  
@@@ -83,12 -81,14 +83,12 @@@ create_stash () 
  
                # state of the working tree
                w_tree=$( (
 -                      rm -f "$TMP-index" &&
 -                      cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
 -                      GIT_INDEX_FILE="$TMP-index" &&
 +                      git read-tree --index-output="$TMPindex" -m $i_tree &&
 +                      GIT_INDEX_FILE="$TMPindex" &&
                        export GIT_INDEX_FILE &&
 -                      git read-tree -m $i_tree &&
                        git diff --name-only -z HEAD | git update-index -z --add --remove --stdin &&
                        git write-tree &&
 -                      rm -f "$TMP-index"
 +                      rm -f "$TMPindex"
                ) ) ||
                        die "Cannot save the current worktree state"
  
@@@ -264,7 -264,7 +264,7 @@@ parse_flags_and_rev(
        b_tree=
        i_tree=
  
-       REV=$(git rev-parse --no-flags --symbolic "$@" 2>/dev/null)
+       REV=$(git rev-parse --no-flags --symbolic "$@") || exit 1
  
        FLAGS=
        for opt
        IS_STASH_LIKE=t &&
        test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
        IS_STASH_REF=t
-       if test "${REV}" != "${REV%{*\}}"
-       then
-               # maintainers: it would be better if git rev-parse indicated
-               # this condition with a non-zero status code but as of 1.7.2.1 it
-               # it did not. So, we use non-empty stderr output as a proxy for the
-               # condition of interest.
-               test -z "$(git rev-parse "$REV" 2>&1 >/dev/null)" || die "$REV does not exist in the stash log"
-       fi
  }
  
  is_stash_like()
@@@ -344,7 -334,9 +334,7 @@@ apply_stash () 
  
        assert_stash_like "$@"
  
 -      git update-index -q --refresh &&
 -      git diff-files --quiet --ignore-submodules ||
 -              die 'Cannot apply to a dirty working tree, please stage your changes'
 +      git update-index -q --refresh || die 'unable to refresh index'
  
        # current index state
        c_tree=$(git write-tree) ||
                then
                        squelch='>/dev/null 2>&1'
                fi
 -              eval "git status $squelch" || :
 +              (cd "$START_DIR" && eval "git status $squelch") || :
        else
                # Merge conflict; keep the exit status from merge-recursive
                status=$?
diff --combined t/t3903-stash.sh
index 5fcf52a0715138d98aa62a1e75be7005dc4faf3f,3c91f48b204764804fbf6d1cef1133d3fe518b87..a27d778624c1fe8ca39038f5913eb34a822b23a3
@@@ -37,26 -37,20 +37,32 @@@ test_expect_success 'parents of stash' 
        test_cmp output expect
  '
  
 -test_expect_success 'apply needs clean working directory' '
 -      echo 4 > other-file &&
+ test_expect_success 'applying bogus stash does nothing' '
+       test_must_fail git stash apply stash@{1} &&
+       echo 1 >expect &&
+       test_cmp expect file
+ '
 +test_expect_success 'apply does not need clean working directory' '
 +      echo 4 >other-file &&
        git add other-file &&
 -      echo 5 > other-file &&
 -      test_must_fail git stash apply
 +      echo 5 >other-file &&
 +      git stash apply &&
 +      echo 3 >expect &&
 +      test_cmp expect file
 +'
 +
 +test_expect_success 'apply does not clobber working directory changes' '
 +      git reset --hard &&
 +      echo 4 >file &&
 +      test_must_fail git stash apply &&
 +      echo 4 >expect &&
 +      test_cmp expect file
  '
  
  test_expect_success 'apply stashed changes' '
 +      git reset --hard &&
 +      echo 5 >other-file &&
        git add other-file &&
        test_tick &&
        git commit -m other-file &&
@@@ -549,11 -543,11 +555,11 @@@ test_expect_success 'invalid ref of th
        echo bar6 > file2 &&
        git add file2 &&
        git stash &&
-       test_must_fail git drop stash@{1} &&
-       test_must_fail git pop stash@{1} &&
-       test_must_fail git apply stash@{1} &&
-       test_must_fail git show stash@{1} &&
-       test_must_fail git branch tmp stash@{1} &&
+       test_must_fail git stash drop stash@{1} &&
+       test_must_fail git stash pop stash@{1} &&
+       test_must_fail git stash apply stash@{1} &&
+       test_must_fail git stash show stash@{1} &&
+       test_must_fail git stash branch tmp stash@{1} &&
        git stash drop
  '
  
@@@ -568,23 -562,4 +574,23 @@@ test_expect_success 'stash branch shoul
        git rev-parse stash@{0} --
  '
  
 +test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
 +      git stash clear &&
 +      echo 1 >subdir/subfile1 &&
 +      echo 2 >subdir/subfile2 &&
 +      git add subdir/subfile1 &&
 +      git commit -m subdir &&
 +      (
 +              cd subdir &&
 +              echo x >subfile1 &&
 +              echo x >../file &&
 +              git status >../expect &&
 +              git stash &&
 +              sane_unset GIT_MERGE_VERBOSITY &&
 +              git stash apply
 +      ) |
 +      sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
 +      test_cmp expect actual
 +'
 +
  test_done