]>
Commit | Line | Data |
---|---|---|
f2c66ed1 NS |
1 | #!/bin/sh |
2 | # Copyright (c) 2007, Nanako Shiraishi | |
3 | ||
683befa1 | 4 | USAGE='[ | save | list | show | apply | clear | create ]' |
f2c66ed1 | 5 | |
3cd2491a | 6 | SUBDIRECTORY_OK=Yes |
8f321a39 | 7 | OPTIONS_SPEC= |
f2c66ed1 NS |
8 | . git-sh-setup |
9 | require_work_tree | |
ceff079b | 10 | cd_to_toplevel |
f2c66ed1 NS |
11 | |
12 | TMP="$GIT_DIR/.git-stash.$$" | |
13 | trap 'rm -f "$TMP-*"' 0 | |
14 | ||
15 | ref_stash=refs/stash | |
16 | ||
17 | no_changes () { | |
38762c47 | 18 | git diff-index --quiet --cached HEAD -- && |
5be60078 | 19 | git diff-files --quiet |
f2c66ed1 NS |
20 | } |
21 | ||
22 | clear_stash () { | |
7ab3cc70 JH |
23 | if current=$(git rev-parse --verify $ref_stash 2>/dev/null) |
24 | then | |
a9ee9bf9 | 25 | git update-ref -d $ref_stash $current |
7ab3cc70 | 26 | fi |
f2c66ed1 NS |
27 | } |
28 | ||
bc9e7399 | 29 | create_stash () { |
9f62e18a JH |
30 | stash_msg="$1" |
31 | ||
f2c66ed1 NS |
32 | if no_changes |
33 | then | |
f2c66ed1 NS |
34 | exit 0 |
35 | fi | |
f12e925a | 36 | |
f2c66ed1 | 37 | # state of the base commit |
5be60078 | 38 | if b_commit=$(git rev-parse --verify HEAD) |
f2c66ed1 | 39 | then |
aa4f31d5 | 40 | head=$(git log --no-color --abbrev-commit --pretty=oneline -n 1 HEAD --) |
f2c66ed1 NS |
41 | else |
42 | die "You do not have the initial commit yet" | |
43 | fi | |
44 | ||
5be60078 | 45 | if branch=$(git symbolic-ref -q HEAD) |
f2c66ed1 NS |
46 | then |
47 | branch=${branch#refs/heads/} | |
48 | else | |
49 | branch='(no branch)' | |
50 | fi | |
51 | msg=$(printf '%s: %s' "$branch" "$head") | |
52 | ||
53 | # state of the index | |
5be60078 | 54 | i_tree=$(git write-tree) && |
6143fa2c | 55 | i_commit=$(printf 'index on %s\n' "$msg" | |
5be60078 | 56 | git commit-tree $i_tree -p $b_commit) || |
f2c66ed1 NS |
57 | die "Cannot save the current index state" |
58 | ||
59 | # state of the working tree | |
60 | w_tree=$( ( | |
b24f56d6 JH |
61 | rm -f "$TMP-index" && |
62 | cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" && | |
f2c66ed1 NS |
63 | GIT_INDEX_FILE="$TMP-index" && |
64 | export GIT_INDEX_FILE && | |
b24f56d6 | 65 | git read-tree -m $i_tree && |
5be60078 JH |
66 | git add -u && |
67 | git write-tree && | |
f2c66ed1 NS |
68 | rm -f "$TMP-index" |
69 | ) ) || | |
70 | die "Cannot save the current worktree state" | |
71 | ||
72 | # create the stash | |
9f62e18a JH |
73 | if test -z "$stash_msg" |
74 | then | |
75 | stash_msg=$(printf 'WIP on %s' "$msg") | |
76 | else | |
77 | stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg") | |
78 | fi | |
79 | w_commit=$(printf '%s\n' "$stash_msg" | | |
5be60078 | 80 | git commit-tree $w_tree -p $b_commit -p $i_commit) || |
f2c66ed1 | 81 | die "Cannot record working tree state" |
bc9e7399 | 82 | } |
f2c66ed1 | 83 | |
bc9e7399 JH |
84 | save_stash () { |
85 | stash_msg="$1" | |
86 | ||
87 | if no_changes | |
88 | then | |
7c390d90 | 89 | echo 'No local changes to save' |
bc9e7399 JH |
90 | exit 0 |
91 | fi | |
92 | test -f "$GIT_DIR/logs/$ref_stash" || | |
93 | clear_stash || die "Cannot initialize stash" | |
94 | ||
95 | create_stash "$stash_msg" | |
a9ee9bf9 EM |
96 | |
97 | # Make sure the reflog for stash is kept. | |
98 | : >>"$GIT_DIR/logs/$ref_stash" | |
f2c66ed1 | 99 | |
9f62e18a | 100 | git update-ref -m "$stash_msg" $ref_stash $w_commit || |
f2c66ed1 | 101 | die "Cannot save the current status" |
7c390d90 | 102 | printf 'Saved working directory and index state "%s"\n' "$stash_msg" |
f2c66ed1 NS |
103 | } |
104 | ||
401de405 | 105 | have_stash () { |
5be60078 | 106 | git rev-parse --verify $ref_stash >/dev/null 2>&1 |
401de405 JK |
107 | } |
108 | ||
f2c66ed1 | 109 | list_stash () { |
401de405 | 110 | have_stash || return 0 |
aa4f31d5 | 111 | git log --no-color --pretty=oneline -g "$@" $ref_stash -- | |
f2c66ed1 NS |
112 | sed -n -e 's/^[.0-9a-f]* refs\///p' |
113 | } | |
114 | ||
115 | show_stash () { | |
5be60078 | 116 | flags=$(git rev-parse --no-revs --flags "$@") |
f2c66ed1 NS |
117 | if test -z "$flags" |
118 | then | |
119 | flags=--stat | |
120 | fi | |
5be60078 | 121 | s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") |
f2c66ed1 | 122 | |
5be60078 JH |
123 | w_commit=$(git rev-parse --verify "$s") && |
124 | b_commit=$(git rev-parse --verify "$s^") && | |
e7187e4e | 125 | git diff $flags $b_commit $w_commit |
f2c66ed1 NS |
126 | } |
127 | ||
128 | apply_stash () { | |
5be60078 | 129 | git diff-files --quiet || |
f2c66ed1 NS |
130 | die 'Cannot restore on top of a dirty state' |
131 | ||
150937c4 JS |
132 | unstash_index= |
133 | case "$1" in | |
134 | --index) | |
135 | unstash_index=t | |
136 | shift | |
137 | esac | |
138 | ||
f2c66ed1 | 139 | # current index state |
5be60078 | 140 | c_tree=$(git write-tree) || |
f2c66ed1 NS |
141 | die 'Cannot apply a stash in the middle of a merge' |
142 | ||
cbeaccc3 JH |
143 | # stash records the work tree, and is a merge between the |
144 | # base commit (first parent) and the index tree (second parent). | |
5be60078 JH |
145 | s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") && |
146 | w_tree=$(git rev-parse --verify "$s:") && | |
cbeaccc3 JH |
147 | b_tree=$(git rev-parse --verify "$s^1:") && |
148 | i_tree=$(git rev-parse --verify "$s^2:") || | |
f2c66ed1 NS |
149 | die "$*: no valid stashed state found" |
150 | ||
cbeaccc3 JH |
151 | unstashed_index_tree= |
152 | if test -n "$unstash_index" && test "$b_tree" != "$i_tree" | |
153 | then | |
89d750bf | 154 | git diff-tree --binary $s^2^..$s^2 | git apply --cached |
150937c4 JS |
155 | test $? -ne 0 && |
156 | die 'Conflicts in index. Try without --index.' | |
157 | unstashed_index_tree=$(git-write-tree) || | |
158 | die 'Could not save index tree' | |
159 | git reset | |
cbeaccc3 | 160 | fi |
150937c4 | 161 | |
f2c66ed1 NS |
162 | eval " |
163 | GITHEAD_$w_tree='Stashed changes' && | |
164 | GITHEAD_$c_tree='Updated upstream' && | |
165 | GITHEAD_$b_tree='Version stash was based on' && | |
166 | export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree | |
167 | " | |
168 | ||
169 | if git-merge-recursive $b_tree -- $c_tree $w_tree | |
170 | then | |
171 | # No conflict | |
cbeaccc3 JH |
172 | if test -n "$unstashed_index_tree" |
173 | then | |
174 | git read-tree "$unstashed_index_tree" | |
83b3df7d JH |
175 | else |
176 | a="$TMP-added" && | |
89d750bf | 177 | git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" && |
83b3df7d JH |
178 | git read-tree --reset $c_tree && |
179 | git update-index --add --stdin <"$a" || | |
180 | die "Cannot unstage modified files" | |
181 | rm -f "$a" | |
cbeaccc3 JH |
182 | fi |
183 | git status || : | |
f2c66ed1 NS |
184 | else |
185 | # Merge conflict; keep the exit status from merge-recursive | |
150937c4 | 186 | status=$? |
cbeaccc3 JH |
187 | if test -n "$unstash_index" |
188 | then | |
189 | echo >&2 'Index was not unstashed.' | |
190 | fi | |
150937c4 | 191 | exit $status |
f2c66ed1 NS |
192 | fi |
193 | } | |
194 | ||
195 | # Main command set | |
196 | case "$1" in | |
fcb10a96 JH |
197 | list) |
198 | shift | |
f2c66ed1 NS |
199 | if test $# = 0 |
200 | then | |
201 | set x -n 10 | |
202 | shift | |
203 | fi | |
204 | list_stash "$@" | |
205 | ;; | |
206 | show) | |
207 | shift | |
208 | show_stash "$@" | |
209 | ;; | |
683befa1 KL |
210 | save) |
211 | shift | |
212 | save_stash "$*" && git-reset --hard | |
213 | ;; | |
f2c66ed1 NS |
214 | apply) |
215 | shift | |
216 | apply_stash "$@" | |
217 | ;; | |
218 | clear) | |
219 | clear_stash | |
220 | ;; | |
bc9e7399 JH |
221 | create) |
222 | if test $# -gt 0 && test "$1" = create | |
223 | then | |
224 | shift | |
225 | fi | |
226 | create_stash "$*" && echo "$w_commit" | |
227 | ;; | |
f2c66ed1 | 228 | *) |
683befa1 | 229 | if test $# -eq 0 |
9f62e18a | 230 | then |
97bc00a4 | 231 | save_stash && |
7c390d90 | 232 | echo '(To restore them type "git stash apply")' && |
97bc00a4 | 233 | git-reset --hard |
683befa1 KL |
234 | else |
235 | usage | |
9f62e18a | 236 | fi |
9f62e18a | 237 | ;; |
f2c66ed1 | 238 | esac |