@matheusbsbs / matheus-bagliano / commits / 150937c425
Fork 0

Teach git-stash to "apply --index"

When given this subcommand, git-stash will try to merge the stashed index into the current one. Only trivial merges are possible, since we have no index for the index ;-) If a trivial merge is not possible, git-stash will bail out with a hint to skip the --index option. For good measure, finally include a test case. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jul 2, 2007 at 12:14 UTC 150937c42591b0e9fb6c17f96c459d57b5f3bfdb
2 files changed +89 -1
git-stash.sh
+20 -1
@@ -111,6 +111,13 @@ apply_stash () {
111 git diff-files --quiet ||
112 die 'Cannot restore on top of a dirty state'
113
114 + unstash_index=
115 + case "$1" in
116 + --index)
117 + unstash_index=t
118 + shift
119 + esac
120 +
121 # current index state
122 c_tree=$(git write-tree) ||
123 die 'Cannot apply a stash in the middle of a merge'
@@ -120,6 +127,15 @@ apply_stash () {
127 b_tree=$(git rev-parse --verify "$s^:") ||
128 die "$*: no valid stashed state found"
129
130 + test -z "$unstash_index" || {
131 + git diff --binary $s^2^..$s^2 | git apply --cached
132 + test $? -ne 0 &&
133 + die 'Conflicts in index. Try without --index.'
134 + unstashed_index_tree=$(git-write-tree) ||
135 + die 'Could not save index tree'
136 + git reset
137 + }
138 +
139 eval "
140 GITHEAD_$w_tree='Stashed changes' &&
141 GITHEAD_$c_tree='Updated upstream' &&
@@ -137,9 +153,12 @@ apply_stash () {
153 die "Cannot unstage modified files"
154 git-status
155 rm -f "$a"
156 + test -z "$unstash_index" || git read-tree $unstashed_index_tree
157 else
158 # Merge conflict; keep the exit status from merge-recursive
142 - exit
159 + status=$?
160 + test -z "$unstash_index" || echo 'Index was not unstashed.' >&2
161 + exit $status
162 fi
163 }
164
t/t3903-stash.sh new
+69
@@ -0,0 +1,69 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2007 Johannes E Schindelin
4 +#
5 +
6 +test_description='Test git-stash'
7 +
8 +. ./test-lib.sh
9 +
10 +test_expect_success 'stash some dirty working directory' '
11 + echo 1 > file &&
12 + git add file &&
13 + test_tick &&
14 + git commit -m initial &&
15 + echo 2 > file &&
16 + git add file &&
17 + echo 3 > file &&
18 + test_tick &&
19 + git stash &&
20 + git diff-files --quiet &&
21 + git diff-index --cached --quiet HEAD
22 +'
23 +
24 +cat > expect << EOF
25 +diff --git a/file b/file
26 +index 0cfbf08..00750ed 100644
27 +--- a/file
28 ++++ b/file
29 +@@ -1 +1 @@
30 +-2
31 ++3
32 +EOF
33 +
34 +test_expect_success 'parents of stash' '
35 + test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
36 + git diff stash^2..stash > output &&
37 + diff -u output expect
38 +'
39 +
40 +test_expect_success 'apply needs clean working directory' '
41 + echo 4 > other-file &&
42 + git add other-file &&
43 + echo 5 > other-file
44 + ! git stash apply
45 +'
46 +
47 +test_expect_success 'apply stashed changes' '
48 + git add other-file &&
49 + test_tick &&
50 + git commit -m other-file &&
51 + git stash apply &&
52 + test 3 = $(cat file) &&
53 + test 1 = $(git show :file) &&
54 + test 1 = $(git show HEAD:file)
55 +'
56 +
57 +test_expect_success 'apply stashed changes (including index)' '
58 + git reset --hard HEAD^ &&
59 + echo 6 > other-file &&
60 + git add other-file &&
61 + test_tick &&
62 + git commit -m other-file &&
63 + git stash apply --index &&
64 + test 3 = $(cat file) &&
65 + test 2 = $(git show :file) &&
66 + test 1 = $(git show HEAD:file)
67 +'
68 +
69 +test_done