for-each-ref: fix off by one read.
[git] / git-stash.sh
1 #!/bin/sh
2 # Copyright (c) 2007, Nanako Shiraishi
3
4 USAGE='[ | list | show | apply | clear]'
5
6 SUBDIRECTORY_OK=Yes
7 . git-sh-setup
8 require_work_tree
9 cd_to_toplevel
10
11 TMP="$GIT_DIR/.git-stash.$$"
12 trap 'rm -f "$TMP-*"' 0
13
14 ref_stash=refs/stash
15
16 no_changes () {
17         git diff-index --quiet --cached HEAD &&
18         git diff-files --quiet
19 }
20
21 clear_stash () {
22         if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
23         then
24                 git update-ref -d refs/stash $current
25         fi
26 }
27
28 save_stash () {
29         stash_msg="$1"
30
31         if no_changes
32         then
33                 echo >&2 'No local changes to save'
34                 exit 0
35         fi
36         test -f "$GIT_DIR/logs/$ref_stash" ||
37                 clear_stash || die "Cannot initialize stash"
38
39         # Make sure the reflog for stash is kept.
40         : >>"$GIT_DIR/logs/$ref_stash"
41
42         # state of the base commit
43         if b_commit=$(git rev-parse --verify HEAD)
44         then
45                 head=$(git log --abbrev-commit --pretty=oneline -n 1 HEAD)
46         else
47                 die "You do not have the initial commit yet"
48         fi
49
50         if branch=$(git symbolic-ref -q HEAD)
51         then
52                 branch=${branch#refs/heads/}
53         else
54                 branch='(no branch)'
55         fi
56         msg=$(printf '%s: %s' "$branch" "$head")
57
58         # state of the index
59         i_tree=$(git write-tree) &&
60         i_commit=$(printf 'index on %s\n' "$msg" |
61                 git commit-tree $i_tree -p $b_commit) ||
62                 die "Cannot save the current index state"
63
64         # state of the working tree
65         w_tree=$( (
66                 rm -f "$TMP-index" &&
67                 cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
68                 GIT_INDEX_FILE="$TMP-index" &&
69                 export GIT_INDEX_FILE &&
70                 git read-tree -m $i_tree &&
71                 git add -u &&
72                 git write-tree &&
73                 rm -f "$TMP-index"
74         ) ) ||
75                 die "Cannot save the current worktree state"
76
77         # create the stash
78         if test -z "$stash_msg"
79         then
80                 stash_msg=$(printf 'WIP on %s' "$msg")
81         else
82                 stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
83         fi
84         w_commit=$(printf '%s\n' "$stash_msg" |
85                 git commit-tree $w_tree -p $b_commit -p $i_commit) ||
86                 die "Cannot record working tree state"
87
88         git update-ref -m "$stash_msg" $ref_stash $w_commit ||
89                 die "Cannot save the current status"
90         printf >&2 'Saved "%s"\n' "$stash_msg"
91 }
92
93 have_stash () {
94         git rev-parse --verify $ref_stash >/dev/null 2>&1
95 }
96
97 list_stash () {
98         have_stash || return 0
99         git log --pretty=oneline -g "$@" $ref_stash |
100         sed -n -e 's/^[.0-9a-f]* refs\///p'
101 }
102
103 show_stash () {
104         flags=$(git rev-parse --no-revs --flags "$@")
105         if test -z "$flags"
106         then
107                 flags=--stat
108         fi
109         s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@")
110
111         w_commit=$(git rev-parse --verify "$s") &&
112         b_commit=$(git rev-parse --verify "$s^") &&
113         git diff $flags $b_commit $w_commit
114 }
115
116 apply_stash () {
117         git diff-files --quiet ||
118                 die 'Cannot restore on top of a dirty state'
119
120         unstash_index=
121         case "$1" in
122         --index)
123                 unstash_index=t
124                 shift
125         esac
126
127         # current index state
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:") &&
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
139         unstashed_index_tree=
140         if test -n "$unstash_index" && test "$b_tree" != "$i_tree"
141         then
142                 git diff-tree --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
148         fi
149
150         eval "
151                 GITHEAD_$w_tree='Stashed changes' &&
152                 GITHEAD_$c_tree='Updated upstream' &&
153                 GITHEAD_$b_tree='Version stash was based on' &&
154                 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
155         "
156
157         if git-merge-recursive $b_tree -- $c_tree $w_tree
158         then
159                 # No conflict
160                 if test -n "$unstashed_index_tree"
161                 then
162                         git read-tree "$unstashed_index_tree"
163                 else
164                         a="$TMP-added" &&
165                         git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
166                         git read-tree --reset $c_tree &&
167                         git update-index --add --stdin <"$a" ||
168                                 die "Cannot unstage modified files"
169                         rm -f "$a"
170                 fi
171                 git status || :
172         else
173                 # Merge conflict; keep the exit status from merge-recursive
174                 status=$?
175                 if test -n "$unstash_index"
176                 then
177                         echo >&2 'Index was not unstashed.'
178                 fi
179                 exit $status
180         fi
181 }
182
183 # Main command set
184 case "$1" in
185 list)
186         shift
187         if test $# = 0
188         then
189                 set x -n 10
190                 shift
191         fi
192         list_stash "$@"
193         ;;
194 show)
195         shift
196         show_stash "$@"
197         ;;
198 apply)
199         shift
200         apply_stash "$@"
201         ;;
202 clear)
203         clear_stash
204         ;;
205 help | usage)
206         usage
207         ;;
208 *)
209         if test $# -gt 0 && test "$1" = save
210         then
211                 shift
212         fi
213         save_stash "$*" && git-reset --hard
214         ;;
215 esac