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