2 # Copyright (c) 2007, Nanako Shiraishi
4 dashless
=$
(basename "$0" |
sed -e 's/-/ /')
5 USAGE
="list [<options>]
6 or: $dashless show [<stash>]
7 or: $dashless drop [-q|--quiet] [<stash>]
8 or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
9 or: $dashless branch <branchname> [<stash>]
10 or: $dashless [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
11 [-u|--include-untracked] [-a|--all] [<message>]]
21 TMP
="$GIT_DIR/.git-stash.$$"
22 TMPindex
=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
23 trap 'rm -f "$TMP-"* "$TMPindex"' 0
27 if git config
--get-colorbool color.interactive
; then
28 help_color
="$(git config --get-color color.interactive.help 'red bold')"
29 reset_color
="$(git config --get-color '' reset)"
36 git diff-index
--quiet --cached HEAD
--ignore-submodules -- &&
37 git diff-files
--quiet --ignore-submodules &&
38 (test -z "$untracked" ||
test -z "$(untracked_files)")
42 excl_opt
=--exclude-standard
43 test "$untracked" = "all" && excl_opt
=
44 git ls-files
-o -z $excl_opt
50 die
"git stash clear with parameters is unimplemented"
52 if current
=$
(git rev-parse
--verify $ref_stash 2>/dev
/null
)
54 git update-ref
-d $ref_stash $current
62 git update-index
-q --refresh
68 # state of the base commit
69 if b_commit
=$
(git rev-parse
--verify HEAD
)
71 head=$
(git rev-list
--oneline -n 1 HEAD
--)
73 die
"You do not have the initial commit yet"
76 if branch
=$
(git symbolic-ref
-q HEAD
)
78 branch
=${branch#refs/heads/}
82 msg
=$
(printf '%s: %s' "$branch" "$head")
85 i_tree
=$
(git write-tree
) &&
86 i_commit
=$
(printf 'index on %s\n' "$msg" |
87 git commit-tree
$i_tree -p $b_commit) ||
88 die
"Cannot save the current index state"
90 if test -n "$untracked"
92 # Untracked files are stored by themselves in a parentless commit, for
93 # ease of unpacking later.
96 export GIT_INDEX_FILE
="$TMPindex"
98 git update-index
-z --add --remove --stdin &&
99 u_tree
=$
(git write-tree
) &&
100 printf 'untracked files on %s\n' "$msg" | git commit-tree
$u_tree &&
102 ) ) || die
"Cannot save the untracked files"
104 untracked_commit_option
="-p $u_commit";
106 untracked_commit_option
=
109 if test -z "$patch_mode"
112 # state of the working tree
114 git read-tree
--index-output="$TMPindex" -m $i_tree &&
115 GIT_INDEX_FILE
="$TMPindex" &&
116 export GIT_INDEX_FILE
&&
117 git
diff --name-only -z HEAD | git update-index
-z --add --remove --stdin &&
121 die
"Cannot save the current worktree state"
125 rm -f "$TMP-index" &&
126 GIT_INDEX_FILE
="$TMP-index" git read-tree HEAD
&&
128 # find out what the user wants
129 GIT_INDEX_FILE
="$TMP-index" \
130 git add--interactive
--patch=stash
-- &&
132 # state of the working tree
133 w_tree
=$
(GIT_INDEX_FILE
="$TMP-index" git write-tree
) ||
134 die
"Cannot save the current worktree state"
136 git diff-tree
-p HEAD
$w_tree > "$TMP-patch" &&
137 test -s "$TMP-patch" ||
138 die
"No changes selected"
140 rm -f "$TMP-index" ||
141 die
"Cannot remove temporary index (can't happen)"
146 if test -z "$stash_msg"
148 stash_msg
=$
(printf 'WIP on %s' "$msg")
150 stash_msg
=$
(printf 'On %s: %s' "$branch" "$stash_msg")
152 w_commit
=$
(printf '%s\n' "$stash_msg" |
153 git commit-tree
$w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
154 die
"Cannot record working tree state"
172 # only default to keep if we don't already have an override
173 test -z "$keep_index" && keep_index
=t
178 -u|
--include-untracked)
189 echo "error: unknown option for 'stash save': $1"
190 echo " To provide a message, use git stash save -- '$1'"
200 if test -n "$patch_mode" && test -n "$untracked"
202 die
"Can't use --patch and --include-untracked or --all at the same time"
207 git update-index
-q --refresh
210 say
'No local changes to save'
213 test -f "$GIT_DIR/logs/$ref_stash" ||
214 clear_stash || die
"Cannot initialize stash"
216 create_stash
"$stash_msg" $untracked
218 # Make sure the reflog for stash is kept.
219 : >>"$GIT_DIR/logs/$ref_stash"
221 git update-ref
-m "$stash_msg" $ref_stash $w_commit ||
222 die
"Cannot save the current status"
223 say Saved working directory and index state
"$stash_msg"
225 if test -z "$patch_mode"
227 git
reset --hard ${GIT_QUIET:+-q}
228 test "$untracked" = "all" && CLEAN_X_OPTION
=-x || CLEAN_X_OPTION
=
229 if test -n "$untracked"
231 git clean
--force --quiet $CLEAN_X_OPTION
234 if test "$keep_index" = "t" && test -n $i_tree
236 git read-tree
--reset -u $i_tree
239 git apply
-R < "$TMP-patch" ||
240 die
"Cannot remove worktree changes"
242 if test "$keep_index" != "t"
250 git rev-parse
--verify $ref_stash >/dev
/null
2>&1
254 have_stash ||
return 0
255 git log
--format="%gd: %gs" -g "$@" $ref_stash --
259 assert_stash_like
"$@"
261 git
diff ${FLAGS:---stat} $b_commit $w_commit
265 # Parses the remaining options looking for flags and
266 # at most one revision defaulting to ${ref_stash}@{0}
269 # Derives related tree and commit objects from the
270 # revision, if one is found.
272 # stash records the work tree, and is a merge between the
273 # base commit (first parent) and the index tree (second parent).
275 # REV is set to the symbolic version of the specified stash-like commit
276 # IS_STASH_LIKE is non-blank if ${REV} looks like a stash
277 # IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
278 # s is set to the SHA1 of the stash commit
279 # w_commit is set to the commit containing the working tree
280 # b_commit is set to the base commit
281 # i_commit is set to the commit containing the index tree
282 # u_commit is set to the commit containing the untracked files tree
283 # w_tree is set to the working tree
284 # b_tree is set to the base tree
285 # i_tree is set to the index tree
286 # u_tree is set to the untracked files tree
288 # GIT_QUIET is set to t if -q is specified
289 # INDEX_OPTION is set to --index if --index is specified.
290 # FLAGS is set to the remaining flags
293 # * too many revisions specified
294 # * no revision is specified and there is no stash stack
295 # * a revision is specified which cannot be resolve to a SHA1
296 # * a non-existent stash reference is specified
299 parse_flags_and_rev
()
301 test "$PARSE_CACHE" = "$*" && return 0 # optimisation
317 REV
=$
(git rev-parse
--no-flags --symbolic "$@") ||
exit 1
330 FLAGS
="${FLAGS}${FLAGS:+ }$opt"
339 have_stash || die
"No stash found."
340 set -- ${ref_stash}@
{0}
346 die
"Too many revisions specified: $REV"
350 REV
=$
(git rev-parse
--quiet --symbolic --verify $1 2>/dev
/null
) || die
"$1 is not valid reference"
352 i_commit
=$
(git rev-parse
--quiet --verify $REV^
2 2>/dev
/null
) &&
353 set -- $
(git rev-parse
$REV $REV^
1 $REV: $REV^
1: $REV^
2: 2>/dev
/null
) &&
361 test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
364 u_commit
=$
(git rev-parse
--quiet --verify $REV^
3 2>/dev
/null
) &&
365 u_tree
=$
(git rev-parse
$REV^
3: 2>/dev
/null
)
370 parse_flags_and_rev
"$@"
371 test -n "$IS_STASH_LIKE"
374 assert_stash_like
() {
375 is_stash_like
"$@" || die
"'$*' is not a stash-like commit"
379 is_stash_like
"$@" && test -n "$IS_STASH_REF"
383 is_stash_ref
"$@" || die
"'$*' is not a stash reference"
388 assert_stash_like
"$@"
390 git update-index
-q --refresh || die
'unable to refresh index'
392 # current index state
393 c_tree
=$
(git write-tree
) ||
394 die
'Cannot apply a stash in the middle of a merge'
396 unstashed_index_tree
=
397 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
398 test "$c_tree" != "$i_tree"
400 git diff-tree
--binary $s^
2^..
$s^
2 | git apply
--cached
402 die
'Conflicts in index. Try without --index.'
403 unstashed_index_tree
=$
(git write-tree
) ||
404 die
'Could not save index tree'
410 GIT_INDEX_FILE
="$TMPindex" git-read-tree
"$u_tree" &&
411 GIT_INDEX_FILE
="$TMPindex" git checkout-index
--all &&
413 die
'Could not restore untracked files from stash'
417 GITHEAD_$w_tree='Stashed changes' &&
418 GITHEAD_$c_tree='Updated upstream' &&
419 GITHEAD_$b_tree='Version stash was based on' &&
420 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
423 if test -n "$GIT_QUIET"
425 GIT_MERGE_VERBOSITY
=0 && export GIT_MERGE_VERBOSITY
427 if git merge-recursive
$b_tree -- $c_tree $w_tree
430 if test -n "$unstashed_index_tree"
432 git read-tree
"$unstashed_index_tree"
435 git diff-index
--cached --name-only --diff-filter=A
$c_tree >"$a" &&
436 git read-tree
--reset $c_tree &&
437 git update-index
--add --stdin <"$a" ||
438 die
"Cannot unstage modified files"
442 if test -n "$GIT_QUIET"
444 squelch
='>/dev/null 2>&1'
446 (cd "$START_DIR" && eval "git status $squelch") ||
:
448 # Merge conflict; keep the exit status from merge-recursive
450 if test -n "$INDEX_OPTION"
452 echo >&2 'Index was not unstashed.'
459 assert_stash_ref
"$@"
466 assert_stash_ref
"$@"
468 git reflog delete
--updateref --rewrite "${REV}" &&
469 say
"Dropped ${REV} ($s)" || die
"${REV}: Could not drop stash entry"
471 # clear_stash if we just dropped the last stash entry
472 git rev-parse
--verify "$ref_stash@{0}" > /dev
/null
2>&1 || clear_stash
476 test -n "$1" || die
'No branch name specified'
481 assert_stash_like
"$@"
483 git checkout
-b $branch $REV^
&&
484 apply_stash
"$@" && {
485 test -z "$IS_STASH_REF" || drop_stash
"$@"
489 PARSE_CACHE
='--not-parsed'
490 # The default command is "save" if nothing but options are given
496 *) seen_non_option
=t
; break ;;
500 test -n "$seen_non_option" ||
set "save" "$@"
525 if test $# -gt 0 && test "$1" = create
529 create_stash
"$*" && echo "$w_commit"
547 say
'(To restore them type "git stash apply")'