blob: b9ace9970492aaf48472904d978d809d90ca33db [file] [log] [blame]
しらいしななこf2c66ed2007-06-30 14:37:09 +09001#!/bin/sh
2# Copyright (c) 2007, Nanako Shiraishi
3
Stephan Beyera5ab00c2008-08-16 05:27:31 +02004dashless=$(basename "$0" | sed -e 's/-/ /')
5USAGE="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"
しらいしななこf2c66ed2007-06-30 14:37:09 +090011
James Bowes3cd24912007-07-06 15:57:47 -040012SUBDIRECTORY_OK=Yes
Junio C Hamano8f321a32007-11-06 01:50:02 -080013OPTIONS_SPEC=
しらいしななこf2c66ed2007-06-30 14:37:09 +090014. git-sh-setup
15require_work_tree
Junio C Hamanoceff0792007-07-25 15:32:22 -070016cd_to_toplevel
しらいしななこf2c66ed2007-06-30 14:37:09 +090017
18TMP="$GIT_DIR/.git-stash.$$"
19trap 'rm -f "$TMP-*"' 0
20
21ref_stash=refs/stash
22
23no_changes () {
Johannes Schindelin6848d582008-05-14 18:03:59 +010024 git diff-index --quiet --cached HEAD --ignore-submodules -- &&
25 git diff-files --quiet --ignore-submodules
しらいしななこf2c66ed2007-06-30 14:37:09 +090026}
27
28clear_stash () {
Junio C Hamano3023dc62008-01-05 01:35:54 -080029 if test $# != 0
30 then
31 die "git stash clear with parameters is unimplemented"
32 fi
Junio C Hamano0e321262008-12-07 17:30:35 -080033 if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
Junio C Hamano7ab3cc72007-07-26 23:24:28 -070034 then
Emil Medvea9ee9bf2007-11-07 15:10:27 -060035 git update-ref -d $ref_stash $current
Junio C Hamano7ab3cc72007-07-26 23:24:28 -070036 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +090037}
38
Junio C Hamanobc9e7392007-07-08 01:38:32 -070039create_stash () {
Junio C Hamano9f62e182007-07-04 22:46:09 -070040 stash_msg="$1"
41
Junio C Hamano1eff26c2008-09-04 02:41:22 -070042 git update-index -q --refresh
しらいしななこf2c66ed2007-06-30 14:37:09 +090043 if no_changes
44 then
しらいしななこf2c66ed2007-06-30 14:37:09 +090045 exit 0
46 fi
しらいしななこf12e9252007-07-28 10:44:48 +090047
しらいしななこf2c66ed2007-06-30 14:37:09 +090048 # state of the base commit
Junio C Hamano5be60072007-07-02 22:52:14 -070049 if b_commit=$(git rev-parse --verify HEAD)
しらいしななこf2c66ed2007-06-30 14:37:09 +090050 then
Pascal Obryaa4f31d2007-11-26 23:04:28 +010051 head=$(git log --no-color --abbrev-commit --pretty=oneline -n 1 HEAD --)
しらいしななこf2c66ed2007-06-30 14:37:09 +090052 else
53 die "You do not have the initial commit yet"
54 fi
55
Junio C Hamano5be60072007-07-02 22:52:14 -070056 if branch=$(git symbolic-ref -q HEAD)
しらいしななこf2c66ed2007-06-30 14:37:09 +090057 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
Junio C Hamano5be60072007-07-02 22:52:14 -070065 i_tree=$(git write-tree) &&
Jean-Luc Herren6143fa22007-09-12 20:45:03 +020066 i_commit=$(printf 'index on %s\n' "$msg" |
Junio C Hamano5be60072007-07-02 22:52:14 -070067 git commit-tree $i_tree -p $b_commit) ||
しらいしななこf2c66ed2007-06-30 14:37:09 +090068 die "Cannot save the current index state"
69
70 # state of the working tree
71 w_tree=$( (
Junio C Hamanob24f56d2007-07-08 01:28:18 -070072 rm -f "$TMP-index" &&
73 cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
しらいしななこf2c66ed2007-06-30 14:37:09 +090074 GIT_INDEX_FILE="$TMP-index" &&
75 export GIT_INDEX_FILE &&
Junio C Hamanob24f56d2007-07-08 01:28:18 -070076 git read-tree -m $i_tree &&
Junio C Hamano5be60072007-07-02 22:52:14 -070077 git add -u &&
78 git write-tree &&
しらいしななこf2c66ed2007-06-30 14:37:09 +090079 rm -f "$TMP-index"
80 ) ) ||
81 die "Cannot save the current worktree state"
82
83 # create the stash
Junio C Hamano9f62e182007-07-04 22:46:09 -070084 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" |
Junio C Hamano5be60072007-07-02 22:52:14 -070091 git commit-tree $w_tree -p $b_commit -p $i_commit) ||
しらいしななこf2c66ed2007-06-30 14:37:09 +090092 die "Cannot record working tree state"
Junio C Hamanobc9e7392007-07-08 01:38:32 -070093}
しらいしななこf2c66ed2007-06-30 14:37:09 +090094
Junio C Hamanobc9e7392007-07-08 01:38:32 -070095save_stash () {
SZEDER Gábor7bedebc2008-06-27 16:37:15 +020096 keep_index=
97 case "$1" in
98 --keep-index)
99 keep_index=t
100 shift
101 esac
102
Junio C Hamano47629dc2008-07-23 13:33:44 -0700103 stash_msg="$*"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700104
Junio C Hamano1eff26c2008-09-04 02:41:22 -0700105 git update-index -q --refresh
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700106 if no_changes
107 then
Marco Costalba7c390d92008-01-05 09:57:24 +0100108 echo 'No local changes to save'
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700109 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"
Emil Medvea9ee9bf2007-11-07 15:10:27 -0600115
116 # Make sure the reflog for stash is kept.
117 : >>"$GIT_DIR/logs/$ref_stash"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900118
Junio C Hamano9f62e182007-07-04 22:46:09 -0700119 git update-ref -m "$stash_msg" $ref_stash $w_commit ||
しらいしななこf2c66ed2007-06-30 14:37:09 +0900120 die "Cannot save the current status"
Marco Costalba7c390d92008-01-05 09:57:24 +0100121 printf 'Saved working directory and index state "%s"\n' "$stash_msg"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200122
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
しらいしななこf2c66ed2007-06-30 14:37:09 +0900129}
130
Jeff King401de402007-07-02 00:21:24 -0400131have_stash () {
Junio C Hamano0e321262008-12-07 17:30:35 -0800132 git rev-parse --verify $ref_stash >/dev/null 2>&1
Jeff King401de402007-07-02 00:21:24 -0400133}
134
しらいしななこf2c66ed2007-06-30 14:37:09 +0900135list_stash () {
Jeff King401de402007-07-02 00:21:24 -0400136 have_stash || return 0
Pascal Obryaa4f31d2007-11-26 23:04:28 +0100137 git log --no-color --pretty=oneline -g "$@" $ref_stash -- |
しらいしななこf2c66ed2007-06-30 14:37:09 +0900138 sed -n -e 's/^[.0-9a-f]* refs\///p'
139}
140
141show_stash () {
Junio C Hamano5be60072007-07-02 22:52:14 -0700142 flags=$(git rev-parse --no-revs --flags "$@")
しらいしななこf2c66ed2007-06-30 14:37:09 +0900143 if test -z "$flags"
144 then
145 flags=--stat
146 fi
Brandon Caseyda65e7c2008-09-23 18:57:09 -0500147
Brandon Casey875471c2008-10-02 18:52:11 -0500148 w_commit=$(git rev-parse --verify --default $ref_stash "$@") &&
149 b_commit=$(git rev-parse --verify "$w_commit^") &&
Shawn O. Pearcee7187e42007-10-19 02:18:14 -0400150 git diff $flags $b_commit $w_commit
しらいしななこf2c66ed2007-06-30 14:37:09 +0900151}
152
153apply_stash () {
Junio C Hamano1eff26c2008-09-04 02:41:22 -0700154 git update-index -q --refresh &&
Johannes Schindelin6848d582008-05-14 18:03:59 +0100155 git diff-files --quiet --ignore-submodules ||
Stephen Haberman2a79d2f2008-09-29 04:12:04 -0500156 die 'Cannot apply to a dirty working tree, please stage your changes'
しらいしななこf2c66ed2007-06-30 14:37:09 +0900157
Johannes Schindelin150937c2007-07-02 12:14:49 +0100158 unstash_index=
159 case "$1" in
160 --index)
161 unstash_index=t
162 shift
163 esac
164
しらいしななこf2c66ed2007-06-30 14:37:09 +0900165 # current index state
Junio C Hamano5be60072007-07-02 22:52:14 -0700166 c_tree=$(git write-tree) ||
しらいしななこf2c66ed2007-06-30 14:37:09 +0900167 die 'Cannot apply a stash in the middle of a merge'
168
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700169 # stash records the work tree, and is a merge between the
170 # base commit (first parent) and the index tree (second parent).
Brandon Casey875471c2008-10-02 18:52:11 -0500171 s=$(git rev-parse --verify --default $ref_stash "$@") &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700172 w_tree=$(git rev-parse --verify "$s:") &&
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700173 b_tree=$(git rev-parse --verify "$s^1:") &&
174 i_tree=$(git rev-parse --verify "$s^2:") ||
しらいしななこf2c66ed2007-06-30 14:37:09 +0900175 die "$*: no valid stashed state found"
176
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700177 unstashed_index_tree=
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200178 if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
179 test "$c_tree" != "$i_tree"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700180 then
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400181 git diff-tree --binary $s^2^..$s^2 | git apply --cached
Johannes Schindelin150937c2007-07-02 12:14:49 +0100182 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
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700187 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100188
しらいしななこf2c66ed2007-06-30 14:37:09 +0900189 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
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700199 if test -n "$unstashed_index_tree"
200 then
201 git read-tree "$unstashed_index_tree"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700202 else
203 a="$TMP-added" &&
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400204 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
Junio C Hamano83b3df72007-07-27 23:51:45 -0700205 git read-tree --reset $c_tree &&
206 git update-index --add --stdin <"$a" ||
207 die "Cannot unstage modified files"
208 rm -f "$a"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700209 fi
210 git status || :
しらいしななこf2c66ed2007-06-30 14:37:09 +0900211 else
212 # Merge conflict; keep the exit status from merge-recursive
Johannes Schindelin150937c2007-07-02 12:14:49 +0100213 status=$?
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700214 if test -n "$unstash_index"
215 then
216 echo >&2 'Index was not unstashed.'
217 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100218 exit $status
しらいしななこf2c66ed2007-06-30 14:37:09 +0900219 fi
220}
221
Brandon Caseye25d5f92008-02-22 13:04:54 -0600222drop_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
Brandon Casey875471c2008-10-02 18:52:11 -0500231 s=$(git rev-parse --verify "$@") &&
Junio C Hamano0e321262008-12-07 17:30:35 -0800232 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 ||
Brandon Caseye25d5f92008-02-22 13:04:54 -0600235 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
Junio C Hamano0e321262008-12-07 17:30:35 -0800241 git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
Brandon Caseye25d5f92008-02-22 13:04:54 -0600242}
243
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530244apply_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
しらいしななこf2c66ed2007-06-30 14:37:09 +0900261# Main command set
262case "$1" in
Junio C Hamanofcb10a92007-07-02 23:15:45 -0700263list)
264 shift
しらいしななこf2c66ed2007-06-30 14:37:09 +0900265 if test $# = 0
266 then
267 set x -n 10
268 shift
269 fi
270 list_stash "$@"
271 ;;
272show)
273 shift
274 show_stash "$@"
275 ;;
Kevin Leung683befa2007-12-03 10:34:05 +0800276save)
277 shift
Junio C Hamano47629dc2008-07-23 13:33:44 -0700278 save_stash "$@"
Kevin Leung683befa2007-12-03 10:34:05 +0800279 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900280apply)
281 shift
282 apply_stash "$@"
283 ;;
284clear)
Junio C Hamano3023dc62008-01-05 01:35:54 -0800285 shift
286 clear_stash "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900287 ;;
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700288create)
289 if test $# -gt 0 && test "$1" = create
290 then
291 shift
292 fi
293 create_stash "$*" && echo "$w_commit"
294 ;;
Brandon Caseye25d5f92008-02-22 13:04:54 -0600295drop)
296 shift
297 drop_stash "$@"
298 ;;
Brandon Caseybd56ff52008-02-22 16:52:50 -0600299pop)
300 shift
301 if apply_stash "$@"
302 then
303 test -z "$unstash_index" || shift
304 drop_stash "$@"
305 fi
306 ;;
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530307branch)
308 shift
309 apply_to_branch "$@"
310 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900311*)
Kevin Leung683befa2007-12-03 10:34:05 +0800312 if test $# -eq 0
Junio C Hamano9f62e182007-07-04 22:46:09 -0700313 then
Wincent Colaiuta97bc00a2007-12-22 18:31:25 +0100314 save_stash &&
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200315 echo '(To restore them type "git stash apply")'
Kevin Leung683befa2007-12-03 10:34:05 +0800316 else
317 usage
Junio C Hamano9f62e182007-07-04 22:46:09 -0700318 fi
Junio C Hamano9f62e182007-07-04 22:46:09 -0700319 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900320esac