d799c763788ecd64b2508668f04c329b2de0c391
[git/git.git] / git-stash.sh
1 #!/bin/sh
2 # Copyright (c) 2007, Nanako Shiraishi
3
4 dashless=$(basename "$0" | sed -e 's/-/ /')
5 USAGE="list [<options>]
6 or: $dashless (show | drop | pop ) [<stash>]
7 or: $dashless apply [--index] [<stash>]
8 or: $dashless branch <branchname> [<stash>]
9 or: $dashless [save [--keep-index] [<message>]]
10 or: $dashless clear"
11
12 SUBDIRECTORY_OK=Yes
13 OPTIONS_SPEC=
14 . git-sh-setup
15 require_work_tree
16 cd_to_toplevel
17
18 TMP="$GIT_DIR/.git-stash.$$"
19 trap 'rm -f "$TMP-*"' 0
20
21 ref_stash=refs/stash
22
23 no_changes () {
24 git diff-index --quiet --cached HEAD --ignore-submodules -- &&
25 git diff-files --quiet --ignore-submodules
26 }
27
28 clear_stash () {
29 if test $# != 0
30 then
31 die "git stash clear with parameters is unimplemented"
32 fi
33 if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
34 then
35 git update-ref -d $ref_stash $current
36 fi
37 }
38
39 create_stash () {
40 stash_msg="$1"
41
42 git update-index -q --refresh
43 if no_changes
44 then
45 exit 0
46 fi
47
48 # state of the base commit
49 if b_commit=$(git rev-parse --verify HEAD)
50 then
51 head=$(git log --no-color --abbrev-commit --pretty=oneline -n 1 HEAD --)
52 else
53 die "You do not have the initial commit yet"
54 fi
55
56 if branch=$(git symbolic-ref -q HEAD)
57 then
58 branch=${branch#refs/heads/}
59 else
60 branch='(no branch)'
61 fi
62 msg=$(printf '%s: %s' "$branch" "$head")
63
64 # state of the index
65 i_tree=$(git write-tree) &&
66 i_commit=$(printf 'index on %s\n' "$msg" |
67 git commit-tree $i_tree -p $b_commit) ||
68 die "Cannot save the current index state"
69
70 # state of the working tree
71 w_tree=$( (
72 rm -f "$TMP-index" &&
73 cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
74 GIT_INDEX_FILE="$TMP-index" &&
75 export GIT_INDEX_FILE &&
76 git read-tree -m $i_tree &&
77 git add -u &&
78 git write-tree &&
79 rm -f "$TMP-index"
80 ) ) ||
81 die "Cannot save the current worktree state"
82
83 # create the stash
84 if test -z "$stash_msg"
85 then
86 stash_msg=$(printf 'WIP on %s' "$msg")
87 else
88 stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
89 fi
90 w_commit=$(printf '%s\n' "$stash_msg" |
91 git commit-tree $w_tree -p $b_commit -p $i_commit) ||
92 die "Cannot record working tree state"
93 }
94
95 save_stash () {
96 keep_index=
97 case "$1" in
98 --keep-index)
99 keep_index=t
100 shift
101 esac
102
103 stash_msg="$*"
104
105 git update-index -q --refresh
106 if no_changes
107 then
108 echo 'No local changes to save'
109 exit 0
110 fi
111 test -f "$GIT_DIR/logs/$ref_stash" ||
112 clear_stash || die "Cannot initialize stash"
113
114 create_stash "$stash_msg"
115
116 # Make sure the reflog for stash is kept.
117 : >>"$GIT_DIR/logs/$ref_stash"
118
119 git update-ref -m "$stash_msg" $ref_stash $w_commit ||
120 die "Cannot save the current status"
121 printf 'Saved working directory and index state "%s"\n' "$stash_msg"
122
123 git reset --hard
124
125 if test -n "$keep_index" && test -n $i_tree
126 then
127 git read-tree --reset -u $i_tree
128 fi
129 }
130
131 have_stash () {
132 git rev-parse --verify $ref_stash >/dev/null 2>&1
133 }
134
135 list_stash () {
136 have_stash || return 0
137 git log --no-color --pretty=oneline -g "$@" $ref_stash -- |
138 sed -n -e 's/^[.0-9a-f]* refs\///p'
139 }
140
141 show_stash () {
142 flags=$(git rev-parse --no-revs --flags "$@")
143 if test -z "$flags"
144 then
145 flags=--stat
146 fi
147 s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@")
148
149 w_commit=$(git rev-parse --verify "$s") &&
150 b_commit=$(git rev-parse --verify "$s^") &&
151 git diff $flags $b_commit $w_commit
152 }
153
154 apply_stash () {
155 git update-index -q --refresh &&
156 git diff-files --quiet --ignore-submodules ||
157 die 'Cannot restore on top of a dirty state'
158
159 unstash_index=
160 case "$1" in
161 --index)
162 unstash_index=t
163 shift
164 esac
165
166 # current index state
167 c_tree=$(git write-tree) ||
168 die 'Cannot apply a stash in the middle of a merge'
169
170 # stash records the work tree, and is a merge between the
171 # base commit (first parent) and the index tree (second parent).
172 s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") &&
173 w_tree=$(git rev-parse --verify "$s:") &&
174 b_tree=$(git rev-parse --verify "$s^1:") &&
175 i_tree=$(git rev-parse --verify "$s^2:") ||
176 die "$*: no valid stashed state found"
177
178 unstashed_index_tree=
179 if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
180 test "$c_tree" != "$i_tree"
181 then
182 git diff-tree --binary $s^2^..$s^2 | git apply --cached
183 test $? -ne 0 &&
184 die 'Conflicts in index. Try without --index.'
185 unstashed_index_tree=$(git-write-tree) ||
186 die 'Could not save index tree'
187 git reset
188 fi
189
190 eval "
191 GITHEAD_$w_tree='Stashed changes' &&
192 GITHEAD_$c_tree='Updated upstream' &&
193 GITHEAD_$b_tree='Version stash was based on' &&
194 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
195 "
196
197 if git-merge-recursive $b_tree -- $c_tree $w_tree
198 then
199 # No conflict
200 if test -n "$unstashed_index_tree"
201 then
202 git read-tree "$unstashed_index_tree"
203 else
204 a="$TMP-added" &&
205 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
206 git read-tree --reset $c_tree &&
207 git update-index --add --stdin <"$a" ||
208 die "Cannot unstage modified files"
209 rm -f "$a"
210 fi
211 git status || :
212 else
213 # Merge conflict; keep the exit status from merge-recursive
214 status=$?
215 if test -n "$unstash_index"
216 then
217 echo >&2 'Index was not unstashed.'
218 fi
219 exit $status
220 fi
221 }
222
223 drop_stash () {
224 have_stash || die 'No stash entries to drop'
225
226 if test $# = 0
227 then
228 set x "$ref_stash@{0}"
229 shift
230 fi
231 # Verify supplied argument looks like a stash entry
232 s=$(git rev-parse --revs-only --no-flags "$@") &&
233 git rev-parse --verify "$s:" > /dev/null 2>&1 &&
234 git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
235 git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
236 die "$*: not a valid stashed state"
237
238 git reflog delete --updateref --rewrite "$@" &&
239 echo "Dropped $* ($s)" || die "$*: Could not drop stash entry"
240
241 # clear_stash if we just dropped the last stash entry
242 git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
243 }
244
245 apply_to_branch () {
246 have_stash || die 'Nothing to apply'
247
248 test -n "$1" || die 'No branch name specified'
249 branch=$1
250
251 if test -z "$2"
252 then
253 set x "$ref_stash@{0}"
254 fi
255 stash=$2
256
257 git-checkout -b $branch $stash^ &&
258 apply_stash --index $stash &&
259 drop_stash $stash
260 }
261
262 # Main command set
263 case "$1" in
264 list)
265 shift
266 if test $# = 0
267 then
268 set x -n 10
269 shift
270 fi
271 list_stash "$@"
272 ;;
273 show)
274 shift
275 show_stash "$@"
276 ;;
277 save)
278 shift
279 save_stash "$@"
280 ;;
281 apply)
282 shift
283 apply_stash "$@"
284 ;;
285 clear)
286 shift
287 clear_stash "$@"
288 ;;
289 create)
290 if test $# -gt 0 && test "$1" = create
291 then
292 shift
293 fi
294 create_stash "$*" && echo "$w_commit"
295 ;;
296 drop)
297 shift
298 drop_stash "$@"
299 ;;
300 pop)
301 shift
302 if apply_stash "$@"
303 then
304 test -z "$unstash_index" || shift
305 drop_stash "$@"
306 fi
307 ;;
308 branch)
309 shift
310 apply_to_branch "$@"
311 ;;
312 *)
313 if test $# -eq 0
314 then
315 save_stash &&
316 echo '(To restore them type "git stash apply")'
317 else
318 usage
319 fi
320 ;;
321 esac