]> git.ipfire.org Git - thirdparty/git.git/blame - git-stash.sh
Git 1.7.8-rc2
[thirdparty/git.git] / git-stash.sh
CommitLineData
f2c66ed1
NS
1#!/bin/sh
2# Copyright (c) 2007, Nanako Shiraishi
3
a5ab00c5
SB
4dashless=$(basename "$0" | sed -e 's/-/ /')
5USAGE="list [<options>]
fcdd0e92
SB
6 or: $dashless show [<stash>]
7 or: $dashless drop [-q|--quiet] [<stash>]
8 or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
a5ab00c5 9 or: $dashless branch <branchname> [<stash>]
78751302
DC
10 or: $dashless [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
11 [-u|--include-untracked] [-a|--all] [<message>]]
a5ab00c5 12 or: $dashless clear"
f2c66ed1 13
3cd2491a 14SUBDIRECTORY_OK=Yes
8f321a39 15OPTIONS_SPEC=
26b59b48 16START_DIR=`pwd`
f2c66ed1 17. git-sh-setup
8a583bec 18. git-sh-i18n
f2c66ed1 19require_work_tree
ceff079b 20cd_to_toplevel
f2c66ed1
NS
21
22TMP="$GIT_DIR/.git-stash.$$"
3ba2e865
JS
23TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
24trap 'rm -f "$TMP-"* "$TMPindex"' 0
f2c66ed1
NS
25
26ref_stash=refs/stash
27
dda1f2a5
TR
28if git config --get-colorbool color.interactive; then
29 help_color="$(git config --get-color color.interactive.help 'red bold')"
30 reset_color="$(git config --get-color '' reset)"
31else
32 help_color=
33 reset_color=
34fi
35
f2c66ed1 36no_changes () {
6848d58c 37 git diff-index --quiet --cached HEAD --ignore-submodules -- &&
78751302
DC
38 git diff-files --quiet --ignore-submodules &&
39 (test -z "$untracked" || test -z "$(untracked_files)")
40}
41
42untracked_files () {
43 excl_opt=--exclude-standard
44 test "$untracked" = "all" && excl_opt=
45 git ls-files -o -z $excl_opt
f2c66ed1
NS
46}
47
48clear_stash () {
3023dc69
JH
49 if test $# != 0
50 then
b1440ce2 51 die "$(gettext "git stash clear with parameters is unimplemented")"
3023dc69 52 fi
0e32126f 53 if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
7ab3cc70 54 then
a9ee9bf9 55 git update-ref -d $ref_stash $current
7ab3cc70 56 fi
f2c66ed1
NS
57}
58
bc9e7399 59create_stash () {
9f62e18a 60 stash_msg="$1"
78751302 61 untracked="$2"
9f62e18a 62
1eff26c0 63 git update-index -q --refresh
f2c66ed1
NS
64 if no_changes
65 then
f2c66ed1
NS
66 exit 0
67 fi
f12e925a 68
f2c66ed1 69 # state of the base commit
5be60078 70 if b_commit=$(git rev-parse --verify HEAD)
f2c66ed1 71 then
b0e621ad 72 head=$(git rev-list --oneline -n 1 HEAD --)
f2c66ed1 73 else
b1440ce2 74 die "$(gettext "You do not have the initial commit yet")"
f2c66ed1
NS
75 fi
76
5be60078 77 if branch=$(git symbolic-ref -q HEAD)
f2c66ed1
NS
78 then
79 branch=${branch#refs/heads/}
80 else
81 branch='(no branch)'
82 fi
83 msg=$(printf '%s: %s' "$branch" "$head")
84
85 # state of the index
5be60078 86 i_tree=$(git write-tree) &&
6143fa2c 87 i_commit=$(printf 'index on %s\n' "$msg" |
5be60078 88 git commit-tree $i_tree -p $b_commit) ||
b1440ce2 89 die "$(gettext "Cannot save the current index state")"
f2c66ed1 90
78751302
DC
91 if test -n "$untracked"
92 then
93 # Untracked files are stored by themselves in a parentless commit, for
94 # ease of unpacking later.
95 u_commit=$(
96 untracked_files | (
97 export GIT_INDEX_FILE="$TMPindex"
98 rm -f "$TMPindex" &&
99 git update-index -z --add --remove --stdin &&
100 u_tree=$(git write-tree) &&
101 printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree &&
102 rm -f "$TMPindex"
103 ) ) || die "Cannot save the untracked files"
104
105 untracked_commit_option="-p $u_commit";
106 else
107 untracked_commit_option=
108 fi
109
dda1f2a5
TR
110 if test -z "$patch_mode"
111 then
112
113 # state of the working tree
114 w_tree=$( (
3ba2e865
JS
115 git read-tree --index-output="$TMPindex" -m $i_tree &&
116 GIT_INDEX_FILE="$TMPindex" &&
dda1f2a5 117 export GIT_INDEX_FILE &&
7aa5d43c 118 git diff --name-only -z HEAD | git update-index -z --add --remove --stdin &&
dda1f2a5 119 git write-tree &&
3ba2e865 120 rm -f "$TMPindex"
dda1f2a5 121 ) ) ||
b1440ce2 122 die "$(gettext "Cannot save the current worktree state")"
dda1f2a5
TR
123
124 else
125
b24f56d6 126 rm -f "$TMP-index" &&
dda1f2a5
TR
127 GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
128
129 # find out what the user wants
130 GIT_INDEX_FILE="$TMP-index" \
131 git add--interactive --patch=stash -- &&
132
133 # state of the working tree
134 w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
b1440ce2 135 die "$(gettext "Cannot save the current worktree state")"
f2c66ed1 136
dda1f2a5
TR
137 git diff-tree -p HEAD $w_tree > "$TMP-patch" &&
138 test -s "$TMP-patch" ||
b1440ce2 139 die "$(gettext "No changes selected")"
dda1f2a5
TR
140
141 rm -f "$TMP-index" ||
b1440ce2 142 die "$(gettext "Cannot remove temporary index (can't happen)")"
dda1f2a5
TR
143
144 fi
145
f2c66ed1 146 # create the stash
9f62e18a
JH
147 if test -z "$stash_msg"
148 then
149 stash_msg=$(printf 'WIP on %s' "$msg")
150 else
151 stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
152 fi
153 w_commit=$(printf '%s\n' "$stash_msg" |
22f41286
JH
154 git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
155 die "$(gettext "Cannot record working tree state")"
bc9e7399 156}
f2c66ed1 157
bc9e7399 158save_stash () {
7bedebca 159 keep_index=
dda1f2a5 160 patch_mode=
78751302 161 untracked=
fcdd0e92
SB
162 while test $# != 0
163 do
164 case "$1" in
ea41cfc4 165 -k|--keep-index)
fcdd0e92
SB
166 keep_index=t
167 ;;
dda1f2a5 168 --no-keep-index)
3a243f70 169 keep_index=n
dda1f2a5
TR
170 ;;
171 -p|--patch)
172 patch_mode=t
3a243f70
DM
173 # only default to keep if we don't already have an override
174 test -z "$keep_index" && keep_index=t
fcdd0e92
SB
175 ;;
176 -q|--quiet)
177 GIT_QUIET=t
178 ;;
78751302
DC
179 -u|--include-untracked)
180 untracked=untracked
181 ;;
182 -a|--all)
183 untracked=all
184 ;;
3c2eb80f
MM
185 --)
186 shift
187 break
188 ;;
189 -*)
eed10649
ÆAB
190 option="$1"
191 # TRANSLATORS: $option is an invalid option, like
192 # `--blah-blah'. The 7 spaces at the beginning of the
193 # second line correspond to "error: ". So you should line
194 # up the second line with however many characters the
195 # translation of "error: " takes in your language. E.g. in
196 # English this is:
197 #
198 # $ git stash save --blah-blah 2>&1 | head -n 2
199 # error: unknown option for 'stash save': --blah-blah
200 # To provide a message, use git stash save -- '--blah-blah'
6fdd50e9
JS
201 eval_gettextln "$("error: unknown option for 'stash save': \$option
202 To provide a message, use git stash save -- '\$option'")"
3c2eb80f
MM
203 usage
204 ;;
fcdd0e92
SB
205 *)
206 break
207 ;;
208 esac
7bedebca 209 shift
fcdd0e92 210 done
7bedebca 211
78751302
DC
212 if test -n "$patch_mode" && test -n "$untracked"
213 then
d88acc91 214 die "Can't use --patch and --include-untracked or --all at the same time"
78751302
DC
215 fi
216
47629dcf 217 stash_msg="$*"
bc9e7399 218
1eff26c0 219 git update-index -q --refresh
bc9e7399
JH
220 if no_changes
221 then
5a175871 222 say "$(gettext "No local changes to save")"
bc9e7399
JH
223 exit 0
224 fi
225 test -f "$GIT_DIR/logs/$ref_stash" ||
b1440ce2 226 clear_stash || die "$(gettext "Cannot initialize stash")"
bc9e7399 227
78751302 228 create_stash "$stash_msg" $untracked
a9ee9bf9
EM
229
230 # Make sure the reflog for stash is kept.
231 : >>"$GIT_DIR/logs/$ref_stash"
f2c66ed1 232
9f62e18a 233 git update-ref -m "$stash_msg" $ref_stash $w_commit ||
b1440ce2 234 die "$(gettext "Cannot save the current status")"
fcdd0e92 235 say Saved working directory and index state "$stash_msg"
7bedebca 236
dda1f2a5 237 if test -z "$patch_mode"
7bedebca 238 then
dda1f2a5 239 git reset --hard ${GIT_QUIET:+-q}
78751302
DC
240 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
241 if test -n "$untracked"
242 then
7474b8b4 243 git clean --force --quiet -d $CLEAN_X_OPTION
78751302 244 fi
dda1f2a5 245
3a243f70 246 if test "$keep_index" = "t" && test -n $i_tree
dda1f2a5
TR
247 then
248 git read-tree --reset -u $i_tree
249 fi
250 else
251 git apply -R < "$TMP-patch" ||
b1440ce2 252 die "$(gettext "Cannot remove worktree changes")"
dda1f2a5 253
3a243f70 254 if test "$keep_index" != "t"
dda1f2a5
TR
255 then
256 git reset
257 fi
7bedebca 258 fi
f2c66ed1
NS
259}
260
401de405 261have_stash () {
0e32126f 262 git rev-parse --verify $ref_stash >/dev/null 2>&1
401de405
JK
263}
264
f2c66ed1 265list_stash () {
401de405 266 have_stash || return 0
391c53bd 267 git log --format="%gd: %gs" -g "$@" $ref_stash --
f2c66ed1
NS
268}
269
270show_stash () {
a9bf09e1 271 assert_stash_like "$@"
14cd4581 272
a9bf09e1 273 git diff ${FLAGS:---stat} $b_commit $w_commit
f2c66ed1
NS
274}
275
ef763129
JS
276#
277# Parses the remaining options looking for flags and
278# at most one revision defaulting to ${ref_stash}@{0}
279# if none found.
280#
281# Derives related tree and commit objects from the
282# revision, if one is found.
283#
284# stash records the work tree, and is a merge between the
285# base commit (first parent) and the index tree (second parent).
286#
287# REV is set to the symbolic version of the specified stash-like commit
288# IS_STASH_LIKE is non-blank if ${REV} looks like a stash
289# IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
290# s is set to the SHA1 of the stash commit
291# w_commit is set to the commit containing the working tree
292# b_commit is set to the base commit
293# i_commit is set to the commit containing the index tree
78751302 294# u_commit is set to the commit containing the untracked files tree
ef763129
JS
295# w_tree is set to the working tree
296# b_tree is set to the base tree
297# i_tree is set to the index tree
78751302 298# u_tree is set to the untracked files tree
ef763129
JS
299#
300# GIT_QUIET is set to t if -q is specified
301# INDEX_OPTION is set to --index if --index is specified.
302# FLAGS is set to the remaining flags
303#
304# dies if:
305# * too many revisions specified
306# * no revision is specified and there is no stash stack
307# * a revision is specified which cannot be resolve to a SHA1
308# * a non-existent stash reference is specified
309#
310
311parse_flags_and_rev()
312{
313 test "$PARSE_CACHE" = "$*" && return 0 # optimisation
314 PARSE_CACHE="$*"
315
316 IS_STASH_LIKE=
317 IS_STASH_REF=
318 INDEX_OPTION=
319 s=
320 w_commit=
321 b_commit=
322 i_commit=
78751302 323 u_commit=
ef763129
JS
324 w_tree=
325 b_tree=
326 i_tree=
78751302 327 u_tree=
ef763129 328
59d418fe 329 REV=$(git rev-parse --no-flags --symbolic "$@") || exit 1
ef763129
JS
330
331 FLAGS=
2bea593b 332 for opt
ef763129 333 do
2bea593b
JS
334 case "$opt" in
335 -q|--quiet)
336 GIT_QUIET=-t
337 ;;
ef763129
JS
338 --index)
339 INDEX_OPTION=--index
340 ;;
2bea593b
JS
341 -*)
342 FLAGS="${FLAGS}${FLAGS:+ }$opt"
ef763129
JS
343 ;;
344 esac
ef763129
JS
345 done
346
347 set -- $REV
348
349 case $# in
350 0)
b1440ce2 351 have_stash || die "$(gettext "No stash found.")"
ef763129
JS
352 set -- ${ref_stash}@{0}
353 ;;
354 1)
355 :
356 ;;
357 *)
33ceddcf 358 die "$(eval_gettext "Too many revisions specified: \$REV")"
ef763129
JS
359 ;;
360 esac
361
155da748
ÆAB
362 REV=$(git rev-parse --quiet --symbolic --verify $1 2>/dev/null) || {
363 reference="$1"
364 die "$(eval_gettext "\$reference is not valid reference")"
365 }
ef763129
JS
366
367 i_commit=$(git rev-parse --quiet --verify $REV^2 2>/dev/null) &&
368 set -- $(git rev-parse $REV $REV^1 $REV: $REV^1: $REV^2: 2>/dev/null) &&
369 s=$1 &&
370 w_commit=$1 &&
371 b_commit=$2 &&
372 w_tree=$3 &&
373 b_tree=$4 &&
374 i_tree=$5 &&
375 IS_STASH_LIKE=t &&
376 test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
377 IS_STASH_REF=t
78751302
DC
378
379 u_commit=$(git rev-parse --quiet --verify $REV^3 2>/dev/null) &&
380 u_tree=$(git rev-parse $REV^3: 2>/dev/null)
ef763129
JS
381}
382
383is_stash_like()
384{
385 parse_flags_and_rev "$@"
386 test -n "$IS_STASH_LIKE"
387}
388
389assert_stash_like() {
777b6f13
ÆAB
390 is_stash_like "$@" || {
391 args="$*"
392 die "$(eval_gettext "'\$args' is not a stash-like commit")"
393 }
ef763129
JS
394}
395
396is_stash_ref() {
397 is_stash_like "$@" && test -n "$IS_STASH_REF"
398}
399
400assert_stash_ref() {
777b6f13
ÆAB
401 is_stash_ref "$@" || {
402 args="$*"
403 die "$(eval_gettext "'\$args' is not a stash reference")"
404 }
ef763129
JS
405}
406
f2c66ed1 407apply_stash () {
fcdd0e92 408
064ed100 409 assert_stash_like "$@"
f2c66ed1 410
b1440ce2 411 git update-index -q --refresh || die "$(gettext "unable to refresh index")"
5fd448f1
OA
412
413 # current index state
414 c_tree=$(git write-tree) ||
b1440ce2 415 die "$(gettext "Cannot apply a stash in the middle of a merge")"
5fd448f1 416
cbeaccc3 417 unstashed_index_tree=
064ed100 418 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
7bedebca 419 test "$c_tree" != "$i_tree"
cbeaccc3 420 then
89d750bf 421 git diff-tree --binary $s^2^..$s^2 | git apply --cached
150937c4 422 test $? -ne 0 &&
b1440ce2 423 die "$(gettext "Conflicts in index. Try without --index.")"
52070790 424 unstashed_index_tree=$(git write-tree) ||
b1440ce2 425 die "$(gettext "Could not save index tree")"
150937c4 426 git reset
cbeaccc3 427 fi
150937c4 428
78751302
DC
429 if test -n "$u_tree"
430 then
431 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
432 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
433 rm -f "$TMPindex" ||
434 die 'Could not restore untracked files from stash'
435 fi
436
f2c66ed1
NS
437 eval "
438 GITHEAD_$w_tree='Stashed changes' &&
439 GITHEAD_$c_tree='Updated upstream' &&
440 GITHEAD_$b_tree='Version stash was based on' &&
441 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
442 "
443
fcdd0e92
SB
444 if test -n "$GIT_QUIET"
445 then
69ae92bd 446 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
fcdd0e92 447 fi
52070790 448 if git merge-recursive $b_tree -- $c_tree $w_tree
f2c66ed1
NS
449 then
450 # No conflict
cbeaccc3
JH
451 if test -n "$unstashed_index_tree"
452 then
453 git read-tree "$unstashed_index_tree"
83b3df7d
JH
454 else
455 a="$TMP-added" &&
89d750bf 456 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
83b3df7d
JH
457 git read-tree --reset $c_tree &&
458 git update-index --add --stdin <"$a" ||
b1440ce2 459 die "$(gettext "Cannot unstage modified files")"
83b3df7d 460 rm -f "$a"
cbeaccc3 461 fi
fcdd0e92
SB
462 squelch=
463 if test -n "$GIT_QUIET"
464 then
465 squelch='>/dev/null 2>&1'
466 fi
26b59b48 467 (cd "$START_DIR" && eval "git status $squelch") || :
f2c66ed1
NS
468 else
469 # Merge conflict; keep the exit status from merge-recursive
150937c4 470 status=$?
064ed100 471 if test -n "$INDEX_OPTION"
cbeaccc3 472 then
6fdd50e9 473 gettextln "Index was not unstashed." >&2
cbeaccc3 474 fi
150937c4 475 exit $status
f2c66ed1
NS
476 fi
477}
478
f276872d
JS
479pop_stash() {
480 assert_stash_ref "$@"
481
482 apply_stash "$@" &&
483 drop_stash "$@"
484}
485
e25d5f9c 486drop_stash () {
92e39e44 487 assert_stash_ref "$@"
e25d5f9c 488
92e39e44 489 git reflog delete --updateref --rewrite "${REV}" &&
d4ca6c8b
ÆAB
490 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
491 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
e25d5f9c
BC
492
493 # clear_stash if we just dropped the last stash entry
0e32126f 494 git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
e25d5f9c
BC
495}
496
656b5034 497apply_to_branch () {
b1440ce2 498 test -n "$1" || die "$(gettext "No branch name specified")"
656b5034 499 branch=$1
fb433dc9 500 shift 1
656b5034 501
fb433dc9
JS
502 set -- --index "$@"
503 assert_stash_like "$@"
504
505 git checkout -b $branch $REV^ &&
57693d03
JS
506 apply_stash "$@" && {
507 test -z "$IS_STASH_REF" || drop_stash "$@"
508 }
656b5034
AMS
509}
510
ef763129 511PARSE_CACHE='--not-parsed'
3c2eb80f
MM
512# The default command is "save" if nothing but options are given
513seen_non_option=
514for opt
515do
516 case "$opt" in
517 -*) ;;
518 *) seen_non_option=t; break ;;
519 esac
520done
521
522test -n "$seen_non_option" || set "save" "$@"
523
f2c66ed1
NS
524# Main command set
525case "$1" in
fcb10a96
JH
526list)
527 shift
f2c66ed1
NS
528 list_stash "$@"
529 ;;
530show)
531 shift
532 show_stash "$@"
533 ;;
683befa1
KL
534save)
535 shift
47629dcf 536 save_stash "$@"
683befa1 537 ;;
f2c66ed1
NS
538apply)
539 shift
540 apply_stash "$@"
541 ;;
542clear)
3023dc69
JH
543 shift
544 clear_stash "$@"
f2c66ed1 545 ;;
bc9e7399
JH
546create)
547 if test $# -gt 0 && test "$1" = create
548 then
549 shift
550 fi
551 create_stash "$*" && echo "$w_commit"
552 ;;
e25d5f9c
BC
553drop)
554 shift
555 drop_stash "$@"
556 ;;
bd56ff54
BC
557pop)
558 shift
f276872d 559 pop_stash "$@"
bd56ff54 560 ;;
656b5034
AMS
561branch)
562 shift
563 apply_to_branch "$@"
564 ;;
f2c66ed1 565*)
3c2eb80f
MM
566 case $# in
567 0)
97bc00a4 568 save_stash &&
5a175871 569 say "$(gettext "(To restore them type \"git stash apply\")")"
ea41cfc4
JS
570 ;;
571 *)
683befa1 572 usage
ea41cfc4 573 esac
9f62e18a 574 ;;
f2c66ed1 575esac