Code

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