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