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>]]
12 or: $dashless push [--patch] [-k|--[no-]keep-index] [-q|--quiet]
13 [-u|--include-untracked] [-a|--all] [-m <message>]
23 TMP
="$GIT_DIR/.git-stash.$$"
24 TMPindex
=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$
25 trap 'rm -f "$TMP-"* "$TMPindex"' 0
29 if git config
--get-colorbool color.interactive
; then
30 help_color
="$(git config --get-color color.interactive.help 'red bold')"
31 reset_color
="$(git config --get-color '' reset)"
38 git diff-index
--quiet --cached HEAD
--ignore-submodules -- &&
39 git diff-files
--quiet --ignore-submodules &&
40 (test -z "$untracked" ||
test -z "$(untracked_files)")
44 excl_opt
=--exclude-standard
45 test "$untracked" = "all" && excl_opt
=
46 git ls-files
-o -z $excl_opt
52 die
"$(gettext "git stash
clear with parameters is unimplemented
")"
54 if current
=$
(git rev-parse
--verify --quiet $ref_stash)
56 git update-ref
-d $ref_stash $current
64 git update-index
-q --refresh
70 # state of the base commit
71 if b_commit
=$
(git rev-parse
--verify HEAD
)
73 head=$
(git rev-list
--oneline -n 1 HEAD
--)
75 die
"$(gettext "You
do not have the initial commit yet
")"
78 if branch
=$
(git symbolic-ref
-q HEAD
)
80 branch
=${branch#refs/heads/}
84 msg
=$
(printf '%s: %s' "$branch" "$head")
87 i_tree
=$
(git write-tree
) &&
88 i_commit
=$
(printf 'index on %s\n' "$msg" |
89 git commit-tree
$i_tree -p $b_commit) ||
90 die
"$(gettext "Cannot save the current index state
")"
92 if test -n "$untracked"
94 # Untracked files are stored by themselves in a parentless commit, for
95 # ease of unpacking later.
98 GIT_INDEX_FILE
="$TMPindex" &&
99 export GIT_INDEX_FILE
&&
101 git update-index
-z --add --remove --stdin &&
102 u_tree
=$
(git write-tree
) &&
103 printf 'untracked files on %s\n' "$msg" | git commit-tree
$u_tree &&
105 ) ) || die
"$(gettext "Cannot save the untracked files
")"
107 untracked_commit_option
="-p $u_commit";
109 untracked_commit_option
=
112 if test -z "$patch_mode"
115 # state of the working tree
117 git read-tree
--index-output="$TMPindex" -m $i_tree &&
118 GIT_INDEX_FILE
="$TMPindex" &&
119 export GIT_INDEX_FILE
&&
120 git diff-index
--name-only -z HEAD
-- >"$TMP-stagenames" &&
121 git update-index
-z --add --remove --stdin <"$TMP-stagenames" &&
125 die
"$(gettext "Cannot save the current worktree state
")"
129 rm -f "$TMP-index" &&
130 GIT_INDEX_FILE
="$TMP-index" git read-tree HEAD
&&
132 # find out what the user wants
133 GIT_INDEX_FILE
="$TMP-index" \
134 git add--interactive
--patch=stash
-- &&
136 # state of the working tree
137 w_tree
=$
(GIT_INDEX_FILE
="$TMP-index" git write-tree
) ||
138 die
"$(gettext "Cannot save the current worktree state
")"
140 git diff-tree
-p HEAD
$w_tree -- >"$TMP-patch" &&
141 test -s "$TMP-patch" ||
142 die
"$(gettext "No changes selected
")"
144 rm -f "$TMP-index" ||
145 die
"$(gettext "Cannot remove temporary index
(can
't happen)")"
150 if test -z "$stash_msg"
152 stash_msg=$(printf 'WIP on
%s
' "$msg")
154 stash_msg=$(printf 'On
%s
: %s
' "$branch" "$stash_msg")
156 w_commit=$(printf '%s
\n' "$stash_msg" |
157 git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
158 die "$(gettext "Cannot record working tree state")"
179 die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
182 if test -z "$stash_msg"
184 stash_msg="Created via \"git stash store\"."
187 git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
189 test $ret != 0 && test -z "$quiet" &&
190 die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
210 # only default to keep if we don't already have an override
211 test -z "$keep_index" && keep_index
=t
216 -u|
--include-untracked)
224 test -z ${1+x} && usage
236 # TRANSLATORS: $option is an invalid option, like
237 # `--blah-blah'. The 7 spaces at the beginning of the
238 # second line correspond to "error: ". So you should line
239 # up the second line with however many characters the
240 # translation of "error: " takes in your language. E.g. in
243 # $ git stash save --blah-blah 2>&1 | head -n 2
244 # error: unknown option for 'stash save': --blah-blah
245 # To provide a message, use git stash save -- '--blah-blah'
246 eval_gettextln
"error: unknown option for 'stash save': \$option
247 To provide a message, use git stash save -- '\$option'"
257 if test -n "$patch_mode" && test -n "$untracked"
259 die
"$(gettext "Can
't use --patch and --include-untracked or --all at the same time")"
262 git update-index -q --refresh
265 say "$(gettext "No local changes to save")"
268 git reflog exists $ref_stash ||
269 clear_stash || die "$(gettext "Cannot initialize stash")"
271 create_stash "$stash_msg" $untracked
272 store_stash -m "$stash_msg" -q $w_commit ||
273 die "$(gettext "Cannot save the current status")"
274 say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
276 if test -z "$patch_mode"
278 git reset --hard ${GIT_QUIET:+-q}
279 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
280 if test -n "$untracked"
282 git clean --force --quiet -d $CLEAN_X_OPTION
285 if test "$keep_index" = "t" && test -n "$i_tree"
287 git read-tree --reset -u $i_tree
290 git apply -R < "$TMP-patch" ||
291 die "$(gettext "Cannot remove worktree changes")"
293 if test "$keep_index" != "t"
310 # pass all options through to push_stash
311 push_options="$push_options $1"
322 if test -z "$stash_msg"
324 push_stash $push_options
326 push_stash $push_options -m "$stash_msg"
331 git rev-parse --verify --quiet $ref_stash >/dev/null
335 have_stash || return 0
336 git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
340 ALLOW_UNKNOWN_FLAGS=t
341 assert_stash_like "$@"
345 if test "$(git config --bool stash.showStat || echo true)" = "true"
350 if test "$(git config --bool stash.showPatch || echo false)" = "true"
352 FLAGS=${FLAGS}${FLAGS:+ }-p
361 git diff ${FLAGS} $b_commit $w_commit
370 # Parses the remaining options looking for flags and
371 # at most one revision defaulting to ${ref_stash}@{0}
374 # Derives related tree and commit objects from the
375 # revision, if one is found.
377 # stash records the work tree, and is a merge between the
378 # base commit (first parent) and the index tree (second parent).
380 # REV is set to the symbolic version of the specified stash-like commit
381 # IS_STASH_LIKE is non-blank if ${REV} looks like a stash
382 # IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
383 # s is set to the SHA1 of the stash commit
384 # w_commit is set to the commit containing the working tree
385 # b_commit is set to the base commit
386 # i_commit is set to the commit containing the index tree
387 # u_commit is set to the commit containing the untracked files tree
388 # w_tree is set to the working tree
389 # b_tree is set to the base tree
390 # i_tree is set to the index tree
391 # u_tree is set to the untracked files tree
393 # GIT_QUIET is set to t if -q is specified
394 # INDEX_OPTION is set to --index if --index is specified.
395 # FLAGS is set to the remaining flags (if allowed)
398 # * too many revisions specified
399 # * no revision is specified and there is no stash stack
400 # * a revision is specified which cannot be resolve to a SHA1
401 # * a non-existent stash reference is specified
402 # * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
405 parse_flags_and_rev()
407 test "$PARSE_CACHE" = "$*" && return 0 # optimisation
438 test "$ALLOW_UNKNOWN_FLAGS" = t ||
439 die "$(eval_gettext "unknown option: \$opt")"
440 FLAGS="${FLAGS}${FLAGS:+ }$opt"
443 REV="${REV}${REV:+ }'$opt'"
452 have_stash || die "$(gettext "No stash found.")"
453 set -- ${ref_stash}@{0}
459 die "$(eval_gettext "Too many revisions specified: \$REV")"
468 set -- "${ref_stash}@{$1}"
472 REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
474 die "$(eval_gettext "\$reference is not a valid reference")"
477 i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
478 set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
486 test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
489 u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
490 u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
495 parse_flags_and_rev "$@"
496 test -n "$IS_STASH_LIKE"
499 assert_stash_like() {
500 is_stash_like "$@" || {
502 die "$(eval_gettext "'\
$args' is not a stash-like commit")"
507 is_stash_like "$@" && test -n "$IS_STASH_REF"
511 is_stash_ref "$@" || {
513 die "$(eval_gettext "'\
$args' is not a stash reference")"
519 assert_stash_like "$@"
521 git update-index -q --refresh || die "$(gettext "unable to refresh index")"
523 # current index state
524 c_tree=$(git write-tree) ||
525 die "$(gettext "Cannot apply a stash in the middle of a merge")"
527 unstashed_index_tree=
528 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
529 test "$c_tree" != "$i_tree"
531 git diff-tree --binary $s^2^..$s^2 | git apply --cached
533 die "$(gettext "Conflicts in index. Try without --index.")"
534 unstashed_index_tree=$(git write-tree) ||
535 die "$(gettext "Could not save index tree")"
541 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
542 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
544 die "$(gettext "Could not restore untracked files from stash")"
548 GITHEAD_$w_tree='Stashed changes
' &&
549 GITHEAD_$c_tree='Updated upstream
' &&
550 GITHEAD_$b_tree='Version stash was based on
' &&
551 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
554 if test -n "$GIT_QUIET"
556 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
558 if git merge-recursive $b_tree -- $c_tree $w_tree
561 if test -n "$unstashed_index_tree"
563 git read-tree "$unstashed_index_tree"
566 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
567 git read-tree --reset $c_tree &&
568 git update-index --add --stdin <"$a" ||
569 die "$(gettext "Cannot unstage modified files")"
573 if test -n "$GIT_QUIET"
575 squelch='>/dev
/null
2>&1'
577 (cd "$START_DIR" && eval "git status $squelch") || :
579 # Merge conflict; keep the exit status from merge-recursive
582 if test -n "$INDEX_OPTION"
584 gettextln "Index was not unstashed." >&2
591 assert_stash_ref "$@"
598 say "$(gettext "The stash is kept in case you need it again.")"
604 assert_stash_ref "$@"
606 git reflog delete --updateref --rewrite "${REV}" &&
607 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
608 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
610 # clear_stash if we just dropped the last stash entry
611 git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
616 test -n "$1" || die "$(gettext "No branch name specified")"
621 assert_stash_like "$@"
623 git checkout -b $branch $REV^ &&
624 apply_stash "$@" && {
625 test -z "$IS_STASH_REF" || drop_stash "$@"
629 PARSE_CACHE='--not-parsed'
630 # The default command is "save" if nothing but options are given
636 *) seen_non_option=t; break ;;
640 test -n "$seen_non_option" || set "save" "$@"
670 create_stash "$*" && echo "$w_commit"
692 say "$(gettext "(To restore them type \"git stash apply\")")"