Merge branch 'master' of git://git.kernel.org/pub/scm/gitk/gitk
[gitweb.git] / git-stash.sh
index eac5551380539e629c07e3f6afece9b5e2d16c0a..f90dffd4caf88d649a9998a43aee18e57d3f594f 100755 (executable)
@@ -6,6 +6,7 @@ USAGE='[ | list | show | apply | clear]'
 SUBDIRECTORY_OK=Yes
 . git-sh-setup
 require_work_tree
+cd_to_toplevel
 
 TMP="$GIT_DIR/.git-stash.$$"
 trap 'rm -f "$TMP-*"' 0
@@ -18,9 +19,10 @@ no_changes () {
 }
 
 clear_stash () {
-       logfile="$GIT_DIR/logs/$ref_stash" &&
-       mkdir -p "$(dirname "$logfile")" &&
-       : >"$logfile"
+       if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
+       then
+               git update-ref -d refs/stash $current
+       fi
 }
 
 save_stash () {
@@ -58,11 +60,11 @@ save_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" &&
                export GIT_INDEX_FILE &&
-
-               rm -f "$TMP-index" &&
-               git read-tree $i_tree &&
+               git read-tree -m $i_tree &&
                git add -u &&
                git write-tree &&
                rm -f "$TMP-index"