git-stash.sh: fix typo in error message
[git/mingw.git] / git-stash.sh
blob0d167fcdf607c4c9eb21bd1d6678423d0a65fb27
1 #!/bin/sh
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 clear"
14 SUBDIRECTORY_OK=Yes
15 OPTIONS_SPEC=
16 START_DIR=`pwd`
17 . git-sh-setup
18 require_work_tree
19 cd_to_toplevel
21 TMP="$GIT_DIR/.git-stash.$$"
22 TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
23 trap 'rm -f "$TMP-"* "$TMPindex"' 0
25 ref_stash=refs/stash
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)"
30 else
31 help_color=
32 reset_color=
35 no_changes () {
36 git diff-index --quiet --cached HEAD --ignore-submodules -- &&
37 git diff-files --quiet --ignore-submodules &&
38 (test -z "$untracked" || test -z "$(untracked_files)")
41 untracked_files () {
42 excl_opt=--exclude-standard
43 test "$untracked" = "all" && excl_opt=
44 git ls-files -o -z $excl_opt
47 clear_stash () {
48 if test $# != 0
49 then
50 die "git stash clear with parameters is unimplemented"
52 if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
53 then
54 git update-ref -d $ref_stash $current
58 create_stash () {
59 stash_msg="$1"
60 untracked="$2"
62 git update-index -q --refresh
63 if no_changes
64 then
65 exit 0
68 # state of the base commit
69 if b_commit=$(git rev-parse --verify HEAD)
70 then
71 head=$(git rev-list --oneline -n 1 HEAD --)
72 else
73 die "You do not have the initial commit yet"
76 if branch=$(git symbolic-ref -q HEAD)
77 then
78 branch=${branch#refs/heads/}
79 else
80 branch='(no branch)'
82 msg=$(printf '%s: %s' "$branch" "$head")
84 # state of the index
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"
91 then
92 # Untracked files are stored by themselves in a parentless commit, for
93 # ease of unpacking later.
94 u_commit=$(
95 untracked_files | (
96 export GIT_INDEX_FILE="$TMPindex"
97 rm -f "$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 &&
101 rm -f "$TMPindex"
102 ) ) || die "Cannot save the untracked files"
104 untracked_commit_option="-p $u_commit";
105 else
106 untracked_commit_option=
109 if test -z "$patch_mode"
110 then
112 # state of the working tree
113 w_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 &&
118 git write-tree &&
119 rm -f "$TMPindex"
120 ) ) ||
121 die "Cannot save the current worktree state"
123 else
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)"
145 # create the stash
146 if test -z "$stash_msg"
147 then
148 stash_msg=$(printf 'WIP on %s' "$msg")
149 else
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"
157 save_stash () {
158 keep_index=
159 patch_mode=
160 untracked=
161 while test $# != 0
163 case "$1" in
164 -k|--keep-index)
165 keep_index=t
167 --no-keep-index)
168 keep_index=n
170 -p|--patch)
171 patch_mode=t
172 # only default to keep if we don't already have an override
173 test -z "$keep_index" && keep_index=t
175 -q|--quiet)
176 GIT_QUIET=t
178 -u|--include-untracked)
179 untracked=untracked
181 -a|--all)
182 untracked=all
185 shift
186 break
189 echo "error: unknown option for 'stash save': $1"
190 echo " To provide a message, use git stash save -- '$1'"
191 usage
194 break
196 esac
197 shift
198 done
200 if test -n "$patch_mode" && test -n "$untracked"
201 then
202 die "Can't use --patch and --include-untracked or --all at the same time"
205 stash_msg="$*"
207 git update-index -q --refresh
208 if no_changes
209 then
210 say 'No local changes to save'
211 exit 0
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"
226 then
227 git reset --hard ${GIT_QUIET:+-q}
228 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
229 if test -n "$untracked"
230 then
231 git clean --force --quiet $CLEAN_X_OPTION
234 if test "$keep_index" = "t" && test -n $i_tree
235 then
236 git read-tree --reset -u $i_tree
238 else
239 git apply -R < "$TMP-patch" ||
240 die "Cannot remove worktree changes"
242 if test "$keep_index" != "t"
243 then
244 git reset
249 have_stash () {
250 git rev-parse --verify $ref_stash >/dev/null 2>&1
253 list_stash () {
254 have_stash || return 0
255 git log --format="%gd: %gs" -g "$@" $ref_stash --
258 show_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}
267 # if none found.
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
292 # dies if:
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
302 PARSE_CACHE="$*"
304 IS_STASH_LIKE=
305 IS_STASH_REF=
306 INDEX_OPTION=
308 w_commit=
309 b_commit=
310 i_commit=
311 u_commit=
312 w_tree=
313 b_tree=
314 i_tree=
315 u_tree=
317 REV=$(git rev-parse --no-flags --symbolic "$@") || exit 1
319 FLAGS=
320 for opt
322 case "$opt" in
323 -q|--quiet)
324 GIT_QUIET=-t
326 --index)
327 INDEX_OPTION=--index
330 FLAGS="${FLAGS}${FLAGS:+ }$opt"
332 esac
333 done
335 set -- $REV
337 case $# in
339 have_stash || die "No stash found."
340 set -- ${ref_stash}@{0}
346 die "Too many revisions specified: $REV"
348 esac
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) &&
354 s=$1 &&
355 w_commit=$1 &&
356 b_commit=$2 &&
357 w_tree=$3 &&
358 b_tree=$4 &&
359 i_tree=$5 &&
360 IS_STASH_LIKE=t &&
361 test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
362 IS_STASH_REF=t
364 u_commit=$(git rev-parse --quiet --verify $REV^3 2>/dev/null) &&
365 u_tree=$(git rev-parse $REV^3: 2>/dev/null)
368 is_stash_like()
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"
378 is_stash_ref() {
379 is_stash_like "$@" && test -n "$IS_STASH_REF"
382 assert_stash_ref() {
383 is_stash_ref "$@" || die "'$*' is not a stash reference"
386 apply_stash () {
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"
399 then
400 git diff-tree --binary $s^2^..$s^2 | git apply --cached
401 test $? -ne 0 &&
402 die 'Conflicts in index. Try without --index.'
403 unstashed_index_tree=$(git write-tree) ||
404 die 'Could not save index tree'
405 git reset
408 if test -n "$u_tree"
409 then
410 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
411 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
412 rm -f "$TMPindex" ||
413 die 'Could not restore untracked files from stash'
416 eval "
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"
424 then
425 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
427 if git merge-recursive $b_tree -- $c_tree $w_tree
428 then
429 # No conflict
430 if test -n "$unstashed_index_tree"
431 then
432 git read-tree "$unstashed_index_tree"
433 else
434 a="$TMP-added" &&
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"
439 rm -f "$a"
441 squelch=
442 if test -n "$GIT_QUIET"
443 then
444 squelch='>/dev/null 2>&1'
446 (cd "$START_DIR" && eval "git status $squelch") || :
447 else
448 # Merge conflict; keep the exit status from merge-recursive
449 status=$?
450 if test -n "$INDEX_OPTION"
451 then
452 echo >&2 'Index was not unstashed.'
454 exit $status
458 pop_stash() {
459 assert_stash_ref "$@"
461 apply_stash "$@" &&
462 drop_stash "$@"
465 drop_stash () {
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
475 apply_to_branch () {
476 test -n "$1" || die 'No branch name specified'
477 branch=$1
478 shift 1
480 set -- --index "$@"
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
491 seen_non_option=
492 for opt
494 case "$opt" in
495 -*) ;;
496 *) seen_non_option=t; break ;;
497 esac
498 done
500 test -n "$seen_non_option" || set "save" "$@"
502 # Main command set
503 case "$1" in
504 list)
505 shift
506 list_stash "$@"
508 show)
509 shift
510 show_stash "$@"
512 save)
513 shift
514 save_stash "$@"
516 apply)
517 shift
518 apply_stash "$@"
520 clear)
521 shift
522 clear_stash "$@"
524 create)
525 if test $# -gt 0 && test "$1" = create
526 then
527 shift
529 create_stash "$*" && echo "$w_commit"
531 drop)
532 shift
533 drop_stash "$@"
535 pop)
536 shift
537 pop_stash "$@"
539 branch)
540 shift
541 apply_to_branch "$@"
544 case $# in
546 save_stash &&
547 say '(To restore them type "git stash apply")'
550 usage
551 esac
553 esac