2 # Copyright (c) 2007, Nanako Shiraishi
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>]]
18 TMP
="$GIT_DIR/.git-stash.$$"
19 trap 'rm -f "$TMP-*"' 0
24 git diff-index
--quiet --cached HEAD
--ignore-submodules -- &&
25 git diff-files
--quiet --ignore-submodules
31 die
"git stash clear with parameters is unimplemented"
33 if current
=$
(git rev-parse
-q --verify $ref_stash)
35 git update-ref
-d $ref_stash $current
42 git update-index
-q --refresh
48 # state of the base commit
49 if b_commit
=$
(git rev-parse
--verify HEAD
)
51 head=$
(git log
--no-color --abbrev-commit --pretty=oneline
-n 1 HEAD
--)
53 die
"You do not have the initial commit yet"
56 if branch
=$
(git symbolic-ref
-q HEAD
)
58 branch
=${branch#refs/heads/}
62 msg
=$
(printf '%s: %s' "$branch" "$head")
65 i_tree
=$
(git write-tree
) &&
66 i_commit
=$
(printf 'index on %s\n' "$msg" |
67 git commit-tree
$i_tree -p $b_commit) ||
68 die
"Cannot save the current index state"
70 # state of the working tree
73 cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
74 GIT_INDEX_FILE
="$TMP-index" &&
75 export GIT_INDEX_FILE
&&
76 git read-tree
-m $i_tree &&
81 die
"Cannot save the current worktree state"
84 if test -z "$stash_msg"
86 stash_msg
=$
(printf 'WIP on %s' "$msg")
88 stash_msg
=$
(printf 'On %s: %s' "$branch" "$stash_msg")
90 w_commit
=$
(printf '%s\n' "$stash_msg" |
91 git commit-tree
$w_tree -p $b_commit -p $i_commit) ||
92 die
"Cannot record working tree state"
105 git update-index
-q --refresh
108 echo 'No local changes to save'
111 test -f "$GIT_DIR/logs/$ref_stash" ||
112 clear_stash || die
"Cannot initialize stash"
114 create_stash
"$stash_msg"
116 # Make sure the reflog for stash is kept.
117 : >>"$GIT_DIR/logs/$ref_stash"
119 git update-ref
-m "$stash_msg" $ref_stash $w_commit ||
120 die
"Cannot save the current status"
121 printf 'Saved working directory and index state "%s"\n' "$stash_msg"
125 if test -n "$keep_index" && test -n $i_tree
127 git read-tree
--reset -u $i_tree
132 git rev-parse
-q --verify $ref_stash >/dev
/null
136 have_stash ||
return 0
137 git log
--no-color --pretty=oneline
-g "$@" $ref_stash -- |
138 sed -n -e 's/^[.0-9a-f]* refs\///p'
142 flags
=$
(git rev-parse
--no-revs --flags "$@")
148 w_commit
=$
(git rev-parse
--verify --default $ref_stash "$@") &&
149 b_commit
=$
(git rev-parse
--verify "$w_commit^") &&
150 git
diff $flags $b_commit $w_commit
154 git update-index
-q --refresh &&
155 git diff-files
--quiet --ignore-submodules ||
156 die
'Cannot apply to a dirty working tree, please stage your changes'
165 # current index state
166 c_tree
=$
(git write-tree
) ||
167 die
'Cannot apply a stash in the middle of a merge'
169 # stash records the work tree, and is a merge between the
170 # base commit (first parent) and the index tree (second parent).
171 s
=$
(git rev-parse
--verify --default $ref_stash "$@") &&
172 w_tree
=$
(git rev-parse
--verify "$s:") &&
173 b_tree
=$
(git rev-parse
--verify "$s^1:") &&
174 i_tree
=$
(git rev-parse
--verify "$s^2:") ||
175 die
"$*: no valid stashed state found"
177 unstashed_index_tree
=
178 if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
179 test "$c_tree" != "$i_tree"
181 git diff-tree
--binary $s^
2^..
$s^
2 | git apply
--cached
183 die
'Conflicts in index. Try without --index.'
184 unstashed_index_tree
=$
(git-write-tree
) ||
185 die
'Could not save index tree'
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
196 if git-merge-recursive
$b_tree -- $c_tree $w_tree
199 if test -n "$unstashed_index_tree"
201 git read-tree
"$unstashed_index_tree"
204 git diff-index
--cached --name-only --diff-filter=A
$c_tree >"$a" &&
205 git read-tree
--reset $c_tree &&
206 git update-index
--add --stdin <"$a" ||
207 die
"Cannot unstage modified files"
212 # Merge conflict; keep the exit status from merge-recursive
214 if test -n "$unstash_index"
216 echo >&2 'Index was not unstashed.'
223 have_stash || die
'No stash entries to drop'
227 set x
"$ref_stash@{0}"
230 # Verify supplied argument looks like a stash entry
231 s
=$
(git rev-parse
--verify "$@") &&
232 git rev-parse
-q --verify "$s:" > /dev
/null
&&
233 git rev-parse
-q --verify "$s^1:" > /dev
/null
&&
234 git rev-parse
-q --verify "$s^2:" > /dev
/null ||
235 die
"$*: not a valid stashed state"
237 git reflog delete
--updateref --rewrite "$@" &&
238 echo "Dropped $* ($s)" || die
"$*: Could not drop stash entry"
240 # clear_stash if we just dropped the last stash entry
241 git rev-parse
-q --verify "$ref_stash@{0}" > /dev
/null || clear_stash
245 have_stash || die
'Nothing to apply'
247 test -n "$1" || die
'No branch name specified'
252 set x
"$ref_stash@{0}"
256 git-checkout
-b $branch $stash^
&&
257 apply_stash
--index $stash &&
289 if test $# -gt 0 && test "$1" = create
293 create_stash
"$*" && echo "$w_commit"
303 test -z "$unstash_index" ||
shift
315 echo '(To restore them type "git stash apply")'