@matheusbsbs / matheus-bagliano / commits / cbeaccc316
Fork 0

Fix git-stash apply --index

Two bugs that made the command practically unusable were fixed with this. - A stash created with a clean index does not have any difference between the base tree and the index tree. Trying to apply the diff between them to the index would error out with "No changes". Even when the user asked to unstash with --index, do not bother with --index action if the base tree and the index tree match. - After successfully performing the working tree merge, the index was reloaded from an earlier state of unstashed index with "read-tree"; this left all the paths cache dirty. By moving the call to git-status after this read-tree, match the cached stat information in the index. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 27, 2007 at 23:41 UTC cbeaccc316affa2e0fb0b60f569d5da9bf444881
1 file changed +17 -6
git-stash.sh
+17 -6
@@ -128,19 +128,24 @@ apply_stash () {
128 c_tree=$(git write-tree) ||
129 die 'Cannot apply a stash in the middle of a merge'
130
131 + # stash records the work tree, and is a merge between the
132 + # base commit (first parent) and the index tree (second parent).
133 s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") &&
134 w_tree=$(git rev-parse --verify "$s:") &&
133 - b_tree=$(git rev-parse --verify "$s^:") ||
135 + b_tree=$(git rev-parse --verify "$s^1:") &&
136 + i_tree=$(git rev-parse --verify "$s^2:") ||
137 die "$*: no valid stashed state found"
138
136 - test -z "$unstash_index" || {
139 + unstashed_index_tree=
140 + if test -n "$unstash_index" && test "$b_tree" != "$i_tree"
141 + then
142 git diff --binary $s^2^..$s^2 | git apply --cached
143 test $? -ne 0 &&
144 die 'Conflicts in index. Try without --index.'
145 unstashed_index_tree=$(git-write-tree) ||
146 die 'Could not save index tree'
147 git reset
143 - }
148 + fi
149
150 eval "
151 GITHEAD_$w_tree='Stashed changes' &&
@@ -157,13 +162,19 @@ apply_stash () {
162 git read-tree --reset $c_tree &&
163 git update-index --add --stdin <"$a" ||
164 die "Cannot unstage modified files"
160 - git-status
165 rm -f "$a"
162 - test -z "$unstash_index" || git read-tree $unstashed_index_tree
166 + if test -n "$unstashed_index_tree"
167 + then
168 + git read-tree "$unstashed_index_tree"
169 + fi
170 + git status || :
171 else
172 # Merge conflict; keep the exit status from merge-recursive
173 status=$?
166 - test -z "$unstash_index" || echo 'Index was not unstashed.' >&2
174 + if test -n "$unstash_index"
175 + then
176 + echo >&2 'Index was not unstashed.'
177 + fi
178 exit $status
179 fi
180 }