blob: d4cf818be9488f1b94fdf4766a8f73db6bfd1029 [file] [log] [blame]
しらいしななこf2c66ed2007-06-30 14:37:09 +09001#!/bin/sh
2# Copyright (c) 2007, Nanako Shiraishi
3
Stephan Beyera5ab00c2008-08-16 05:27:31 +02004dashless=$(basename "$0" | sed -e 's/-/ /')
5USAGE="list [<options>]
Stephen Boydfcdd0e92009-06-17 18:07:37 -07006 or: $dashless show [<stash>]
7 or: $dashless drop [-q|--quiet] [<stash>]
8 or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
Stephan Beyera5ab00c2008-08-16 05:27:31 +02009 or: $dashless branch <branchname> [<stash>]
David Caldwell78751302011-06-24 17:56:06 -070010 or: $dashless [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
11 [-u|--include-untracked] [-a|--all] [<message>]]
Stephan Beyera5ab00c2008-08-16 05:27:31 +020012 or: $dashless clear"
しらいしななこf2c66ed2007-06-30 14:37:09 +090013
James Bowes3cd24912007-07-06 15:57:47 -040014SUBDIRECTORY_OK=Yes
Junio C Hamano8f321a32007-11-06 01:50:02 -080015OPTIONS_SPEC=
Elia Pintod0ea45b2014-04-23 06:44:01 -070016START_DIR=$(pwd)
しらいしななこf2c66ed2007-06-30 14:37:09 +090017. git-sh-setup
Ævar Arnfjörð Bjarmason8a583be2011-05-21 18:44:10 +000018. git-sh-i18n
しらいしななこf2c66ed2007-06-30 14:37:09 +090019require_work_tree
Junio C Hamanoceff0792007-07-25 15:32:22 -070020cd_to_toplevel
しらいしななこf2c66ed2007-06-30 14:37:09 +090021
22TMP="$GIT_DIR/.git-stash.$$"
Johannes Sixt3ba2e862011-03-16 09:18:49 +010023TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
24trap 'rm -f "$TMP-"* "$TMPindex"' 0
しらいしななこf2c66ed2007-06-30 14:37:09 +090025
26ref_stash=refs/stash
27
Thomas Rastdda1f2a2009-08-13 14:29:44 +020028if 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
しらいしななこf2c66ed2007-06-30 14:37:09 +090036no_changes () {
Johannes Schindelin6848d582008-05-14 18:03:59 +010037 git diff-index --quiet --cached HEAD --ignore-submodules -- &&
David Caldwell78751302011-06-24 17:56:06 -070038 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
しらいしななこf2c66ed2007-06-30 14:37:09 +090046}
47
48clear_stash () {
Junio C Hamano3023dc62008-01-05 01:35:54 -080049 if test $# != 0
50 then
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +000051 die "$(gettext "git stash clear with parameters is unimplemented")"
Junio C Hamano3023dc62008-01-05 01:35:54 -080052 fi
David Aguilar1956dfa2014-09-15 20:24:10 -070053 if current=$(git rev-parse --verify --quiet $ref_stash)
Junio C Hamano7ab3cc72007-07-26 23:24:28 -070054 then
Emil Medvea9ee9bf2007-11-07 15:10:27 -060055 git update-ref -d $ref_stash $current
Junio C Hamano7ab3cc72007-07-26 23:24:28 -070056 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +090057}
58
Junio C Hamanobc9e7392007-07-08 01:38:32 -070059create_stash () {
Junio C Hamano9f62e182007-07-04 22:46:09 -070060 stash_msg="$1"
David Caldwell78751302011-06-24 17:56:06 -070061 untracked="$2"
Junio C Hamano9f62e182007-07-04 22:46:09 -070062
Junio C Hamano1eff26c2008-09-04 02:41:22 -070063 git update-index -q --refresh
しらいしななこf2c66ed2007-06-30 14:37:09 +090064 if no_changes
65 then
しらいしななこf2c66ed2007-06-30 14:37:09 +090066 exit 0
67 fi
しらいしななこf12e9252007-07-28 10:44:48 +090068
しらいしななこf2c66ed2007-06-30 14:37:09 +090069 # state of the base commit
Junio C Hamano5be60072007-07-02 22:52:14 -070070 if b_commit=$(git rev-parse --verify HEAD)
しらいしななこf2c66ed2007-06-30 14:37:09 +090071 then
Jeff Kingb0e621a2010-04-08 15:42:37 -040072 head=$(git rev-list --oneline -n 1 HEAD --)
しらいしななこf2c66ed2007-06-30 14:37:09 +090073 else
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +000074 die "$(gettext "You do not have the initial commit yet")"
しらいしななこf2c66ed2007-06-30 14:37:09 +090075 fi
76
Junio C Hamano5be60072007-07-02 22:52:14 -070077 if branch=$(git symbolic-ref -q HEAD)
しらいしななこf2c66ed2007-06-30 14:37:09 +090078 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
Junio C Hamano5be60072007-07-02 22:52:14 -070086 i_tree=$(git write-tree) &&
Jean-Luc Herren6143fa22007-09-12 20:45:03 +020087 i_commit=$(printf 'index on %s\n' "$msg" |
Junio C Hamano5be60072007-07-02 22:52:14 -070088 git commit-tree $i_tree -p $b_commit) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +000089 die "$(gettext "Cannot save the current index state")"
しらいしななこf2c66ed2007-06-30 14:37:09 +090090
David Caldwell78751302011-06-24 17:56:06 -070091 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 | (
Elia Pintobed137d2014-05-23 03:15:31 -070097 GIT_INDEX_FILE="$TMPindex" &&
98 export GIT_INDEX_FILE &&
David Caldwell78751302011-06-24 17:56:06 -070099 rm -f "$TMPindex" &&
100 git update-index -z --add --remove --stdin &&
101 u_tree=$(git write-tree) &&
102 printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree &&
103 rm -f "$TMPindex"
104 ) ) || die "Cannot save the untracked files"
105
106 untracked_commit_option="-p $u_commit";
107 else
108 untracked_commit_option=
109 fi
110
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200111 if test -z "$patch_mode"
112 then
113
114 # state of the working tree
115 w_tree=$( (
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100116 git read-tree --index-output="$TMPindex" -m $i_tree &&
117 GIT_INDEX_FILE="$TMPindex" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200118 export GIT_INDEX_FILE &&
Jonathon Mah44df2e22011-12-30 16:14:01 -0800119 git diff --name-only -z HEAD -- >"$TMP-stagenames" &&
120 git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200121 git write-tree &&
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100122 rm -f "$TMPindex"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200123 ) ) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000124 die "$(gettext "Cannot save the current worktree state")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200125
126 else
127
Junio C Hamanob24f56d2007-07-08 01:28:18 -0700128 rm -f "$TMP-index" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200129 GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
130
131 # find out what the user wants
132 GIT_INDEX_FILE="$TMP-index" \
133 git add--interactive --patch=stash -- &&
134
135 # state of the working tree
136 w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000137 die "$(gettext "Cannot save the current worktree state")"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900138
Jonathon Mah44df2e22011-12-30 16:14:01 -0800139 git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200140 test -s "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000141 die "$(gettext "No changes selected")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200142
143 rm -f "$TMP-index" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000144 die "$(gettext "Cannot remove temporary index (can't happen)")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200145
146 fi
147
しらいしななこf2c66ed2007-06-30 14:37:09 +0900148 # create the stash
Junio C Hamano9f62e182007-07-04 22:46:09 -0700149 if test -z "$stash_msg"
150 then
151 stash_msg=$(printf 'WIP on %s' "$msg")
152 else
153 stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
154 fi
155 w_commit=$(printf '%s\n' "$stash_msg" |
Junio C Hamano22f41282011-07-22 14:46:28 -0700156 git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
157 die "$(gettext "Cannot record working tree state")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700158}
しらいしななこf2c66ed2007-06-30 14:37:09 +0900159
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530160store_stash () {
161 while test $# != 0
162 do
163 case "$1" in
164 -m|--message)
165 shift
166 stash_msg="$1"
167 ;;
168 -q|--quiet)
169 quiet=t
170 ;;
171 *)
172 break
173 ;;
174 esac
175 shift
176 done
177 test $# = 1 ||
178 die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
179
180 w_commit="$1"
181 if test -z "$stash_msg"
182 then
183 stash_msg="Created via \"git stash store\"."
184 fi
185
186 # Make sure the reflog for stash is kept.
187 : >>"$GIT_DIR/logs/$ref_stash"
188 git update-ref -m "$stash_msg" $ref_stash $w_commit
189 ret=$?
190 test $ret != 0 && test -z $quiet &&
191 die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
192 return $ret
193}
194
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700195save_stash () {
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200196 keep_index=
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200197 patch_mode=
David Caldwell78751302011-06-24 17:56:06 -0700198 untracked=
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700199 while test $# != 0
200 do
201 case "$1" in
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200202 -k|--keep-index)
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700203 keep_index=t
204 ;;
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200205 --no-keep-index)
Dan McGee3a243f72011-04-07 12:04:20 -0500206 keep_index=n
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200207 ;;
208 -p|--patch)
209 patch_mode=t
Dan McGee3a243f72011-04-07 12:04:20 -0500210 # only default to keep if we don't already have an override
211 test -z "$keep_index" && keep_index=t
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700212 ;;
213 -q|--quiet)
214 GIT_QUIET=t
215 ;;
David Caldwell78751302011-06-24 17:56:06 -0700216 -u|--include-untracked)
217 untracked=untracked
218 ;;
219 -a|--all)
220 untracked=all
221 ;;
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200222 --)
223 shift
224 break
225 ;;
226 -*)
Ævar Arnfjörð Bjarmasoneed10642011-05-21 18:44:17 +0000227 option="$1"
228 # TRANSLATORS: $option is an invalid option, like
229 # `--blah-blah'. The 7 spaces at the beginning of the
230 # second line correspond to "error: ". So you should line
231 # up the second line with however many characters the
232 # translation of "error: " takes in your language. E.g. in
233 # English this is:
234 #
235 # $ git stash save --blah-blah 2>&1 | head -n 2
236 # error: unknown option for 'stash save': --blah-blah
237 # To provide a message, use git stash save -- '--blah-blah'
Ross Lagerwalled3c4002012-04-14 14:37:29 +0200238 eval_gettextln "error: unknown option for 'stash save': \$option
239 To provide a message, use git stash save -- '\$option'"
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200240 usage
241 ;;
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700242 *)
243 break
244 ;;
245 esac
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200246 shift
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700247 done
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200248
David Caldwell78751302011-06-24 17:56:06 -0700249 if test -n "$patch_mode" && test -n "$untracked"
250 then
Brandon Caseyd88acc92011-08-26 19:59:25 -0500251 die "Can't use --patch and --include-untracked or --all at the same time"
David Caldwell78751302011-06-24 17:56:06 -0700252 fi
253
Junio C Hamano47629dc2008-07-23 13:33:44 -0700254 stash_msg="$*"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700255
Junio C Hamano1eff26c2008-09-04 02:41:22 -0700256 git update-index -q --refresh
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700257 if no_changes
258 then
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000259 say "$(gettext "No local changes to save")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700260 exit 0
261 fi
262 test -f "$GIT_DIR/logs/$ref_stash" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000263 clear_stash || die "$(gettext "Cannot initialize stash")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700264
David Caldwell78751302011-06-24 17:56:06 -0700265 create_stash "$stash_msg" $untracked
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530266 store_stash -m "$stash_msg" -q $w_commit ||
267 die "$(gettext "Cannot save the current status")"
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700268 say Saved working directory and index state "$stash_msg"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200269
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200270 if test -z "$patch_mode"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200271 then
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200272 git reset --hard ${GIT_QUIET:+-q}
David Caldwell78751302011-06-24 17:56:06 -0700273 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
274 if test -n "$untracked"
275 then
Brandon Casey7474b8b2011-08-26 19:59:27 -0500276 git clean --force --quiet -d $CLEAN_X_OPTION
David Caldwell78751302011-06-24 17:56:06 -0700277 fi
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200278
Dan McGee3a243f72011-04-07 12:04:20 -0500279 if test "$keep_index" = "t" && test -n $i_tree
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200280 then
281 git read-tree --reset -u $i_tree
282 fi
283 else
284 git apply -R < "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000285 die "$(gettext "Cannot remove worktree changes")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200286
Dan McGee3a243f72011-04-07 12:04:20 -0500287 if test "$keep_index" != "t"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200288 then
289 git reset
290 fi
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200291 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +0900292}
293
Jeff King401de402007-07-02 00:21:24 -0400294have_stash () {
David Aguilar1956dfa2014-09-15 20:24:10 -0700295 git rev-parse --verify --quiet $ref_stash >/dev/null
Jeff King401de402007-07-02 00:21:24 -0400296}
297
しらいしななこf2c66ed2007-06-30 14:37:09 +0900298list_stash () {
Jeff King401de402007-07-02 00:21:24 -0400299 have_stash || return 0
Jeff King288c67c2014-08-06 14:35:25 -0400300 git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
しらいしななこf2c66ed2007-06-30 14:37:09 +0900301}
302
303show_stash () {
Jon Seymoura9bf09e2010-08-21 14:09:02 +1000304 assert_stash_like "$@"
Gustaf Hendeby14cd4582010-03-16 18:52:37 +0100305
Jon Seymoura9bf09e2010-08-21 14:09:02 +1000306 git diff ${FLAGS:---stat} $b_commit $w_commit
しらいしななこf2c66ed2007-06-30 14:37:09 +0900307}
308
Jon Seymouref763122010-08-21 14:46:22 +1000309#
310# Parses the remaining options looking for flags and
311# at most one revision defaulting to ${ref_stash}@{0}
312# if none found.
313#
314# Derives related tree and commit objects from the
315# revision, if one is found.
316#
317# stash records the work tree, and is a merge between the
318# base commit (first parent) and the index tree (second parent).
319#
320# REV is set to the symbolic version of the specified stash-like commit
321# IS_STASH_LIKE is non-blank if ${REV} looks like a stash
322# IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
323# s is set to the SHA1 of the stash commit
324# w_commit is set to the commit containing the working tree
325# b_commit is set to the base commit
326# i_commit is set to the commit containing the index tree
David Caldwell78751302011-06-24 17:56:06 -0700327# u_commit is set to the commit containing the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000328# w_tree is set to the working tree
329# b_tree is set to the base tree
330# i_tree is set to the index tree
David Caldwell78751302011-06-24 17:56:06 -0700331# u_tree is set to the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000332#
333# GIT_QUIET is set to t if -q is specified
334# INDEX_OPTION is set to --index if --index is specified.
335# FLAGS is set to the remaining flags
336#
337# dies if:
338# * too many revisions specified
339# * no revision is specified and there is no stash stack
340# * a revision is specified which cannot be resolve to a SHA1
341# * a non-existent stash reference is specified
342#
343
344parse_flags_and_rev()
345{
346 test "$PARSE_CACHE" = "$*" && return 0 # optimisation
347 PARSE_CACHE="$*"
348
349 IS_STASH_LIKE=
350 IS_STASH_REF=
351 INDEX_OPTION=
352 s=
353 w_commit=
354 b_commit=
355 i_commit=
David Caldwell78751302011-06-24 17:56:06 -0700356 u_commit=
Jon Seymouref763122010-08-21 14:46:22 +1000357 w_tree=
358 b_tree=
359 i_tree=
David Caldwell78751302011-06-24 17:56:06 -0700360 u_tree=
Jon Seymouref763122010-08-21 14:46:22 +1000361
Øystein Walle2a07e432014-01-07 09:22:15 +0100362 REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
Jon Seymour2bea5932010-09-28 01:32:45 +1000363
364 FLAGS=
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400365 for opt
366 do
367 case "$opt" in
Jon Seymour2bea5932010-09-28 01:32:45 +1000368 -q|--quiet)
369 GIT_QUIET=-t
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400370 ;;
Jon Seymouref763122010-08-21 14:46:22 +1000371 --index)
372 INDEX_OPTION=--index
373 ;;
Jon Seymour2bea5932010-09-28 01:32:45 +1000374 -*)
375 FLAGS="${FLAGS}${FLAGS:+ }$opt"
Jon Seymouref763122010-08-21 14:46:22 +1000376 ;;
377 esac
Jon Seymouref763122010-08-21 14:46:22 +1000378 done
379
Øystein Walle2a07e432014-01-07 09:22:15 +0100380 eval set -- $REV
Jon Seymouref763122010-08-21 14:46:22 +1000381
382 case $# in
383 0)
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000384 have_stash || die "$(gettext "No stash found.")"
Jon Seymouref763122010-08-21 14:46:22 +1000385 set -- ${ref_stash}@{0}
386 ;;
387 1)
388 :
389 ;;
390 *)
Ævar Arnfjörð Bjarmason33ceddc2011-05-21 18:44:14 +0000391 die "$(eval_gettext "Too many revisions specified: \$REV")"
Jon Seymouref763122010-08-21 14:46:22 +1000392 ;;
393 esac
394
David Aguilar1956dfa2014-09-15 20:24:10 -0700395 REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16 +0000396 reference="$1"
Alex Henriead5fe372014-08-30 13:56:01 -0600397 die "$(eval_gettext "\$reference is not a valid reference")"
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16 +0000398 }
Jon Seymouref763122010-08-21 14:46:22 +1000399
David Aguilar1956dfa2014-09-15 20:24:10 -0700400 i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
Øystein Walle2a07e432014-01-07 09:22:15 +0100401 set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
Jon Seymouref763122010-08-21 14:46:22 +1000402 s=$1 &&
403 w_commit=$1 &&
404 b_commit=$2 &&
405 w_tree=$3 &&
406 b_tree=$4 &&
407 i_tree=$5 &&
408 IS_STASH_LIKE=t &&
409 test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
410 IS_STASH_REF=t
David Caldwell78751302011-06-24 17:56:06 -0700411
David Aguilar1956dfa2014-09-15 20:24:10 -0700412 u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
Øystein Walle2a07e432014-01-07 09:22:15 +0100413 u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
Jon Seymouref763122010-08-21 14:46:22 +1000414}
415
416is_stash_like()
417{
418 parse_flags_and_rev "$@"
419 test -n "$IS_STASH_LIKE"
420}
421
422assert_stash_like() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000423 is_stash_like "$@" || {
424 args="$*"
425 die "$(eval_gettext "'\$args' is not a stash-like commit")"
426 }
Jon Seymouref763122010-08-21 14:46:22 +1000427}
428
429is_stash_ref() {
430 is_stash_like "$@" && test -n "$IS_STASH_REF"
431}
432
433assert_stash_ref() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000434 is_stash_ref "$@" || {
435 args="$*"
436 die "$(eval_gettext "'\$args' is not a stash reference")"
437 }
Jon Seymouref763122010-08-21 14:46:22 +1000438}
439
しらいしななこf2c66ed2007-06-30 14:37:09 +0900440apply_stash () {
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700441
Jon Seymour064ed102010-08-21 14:08:58 +1000442 assert_stash_like "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900443
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000444 git update-index -q --refresh || die "$(gettext "unable to refresh index")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300445
446 # current index state
447 c_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000448 die "$(gettext "Cannot apply a stash in the middle of a merge")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300449
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700450 unstashed_index_tree=
Jon Seymour064ed102010-08-21 14:08:58 +1000451 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200452 test "$c_tree" != "$i_tree"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700453 then
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400454 git diff-tree --binary $s^2^..$s^2 | git apply --cached
Johannes Schindelin150937c2007-07-02 12:14:49 +0100455 test $? -ne 0 &&
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000456 die "$(gettext "Conflicts in index. Try without --index.")"
Martin Koegler52070792009-07-22 07:30:58 +0200457 unstashed_index_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000458 die "$(gettext "Could not save index tree")"
Johannes Schindelin150937c2007-07-02 12:14:49 +0100459 git reset
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700460 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100461
David Caldwell78751302011-06-24 17:56:06 -0700462 if test -n "$u_tree"
463 then
464 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
465 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
466 rm -f "$TMPindex" ||
467 die 'Could not restore untracked files from stash'
468 fi
469
しらいしななこf2c66ed2007-06-30 14:37:09 +0900470 eval "
471 GITHEAD_$w_tree='Stashed changes' &&
472 GITHEAD_$c_tree='Updated upstream' &&
473 GITHEAD_$b_tree='Version stash was based on' &&
474 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
475 "
476
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700477 if test -n "$GIT_QUIET"
478 then
Junio C Hamano69ae92b2010-10-13 11:36:36 -0700479 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700480 fi
Martin Koegler52070792009-07-22 07:30:58 +0200481 if git merge-recursive $b_tree -- $c_tree $w_tree
しらいしななこf2c66ed2007-06-30 14:37:09 +0900482 then
483 # No conflict
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700484 if test -n "$unstashed_index_tree"
485 then
486 git read-tree "$unstashed_index_tree"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700487 else
488 a="$TMP-added" &&
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400489 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
Junio C Hamano83b3df72007-07-27 23:51:45 -0700490 git read-tree --reset $c_tree &&
491 git update-index --add --stdin <"$a" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000492 die "$(gettext "Cannot unstage modified files")"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700493 rm -f "$a"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700494 fi
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700495 squelch=
496 if test -n "$GIT_QUIET"
497 then
498 squelch='>/dev/null 2>&1'
499 fi
Piotr Krukowiecki26b59b42011-03-12 09:57:46 +0100500 (cd "$START_DIR" && eval "git status $squelch") || :
しらいしななこf2c66ed2007-06-30 14:37:09 +0900501 else
502 # Merge conflict; keep the exit status from merge-recursive
Johannes Schindelin150937c2007-07-02 12:14:49 +0100503 status=$?
Phil Hord743bf6d2012-07-10 18:52:28 -0400504 git rerere
Jon Seymour064ed102010-08-21 14:08:58 +1000505 if test -n "$INDEX_OPTION"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700506 then
Jon Seymour6fdd50e2011-08-07 21:58:16 +1000507 gettextln "Index was not unstashed." >&2
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700508 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100509 exit $status
しらいしななこf2c66ed2007-06-30 14:37:09 +0900510 fi
511}
512
Jon Seymourf2768722010-08-21 14:09:00 +1000513pop_stash() {
514 assert_stash_ref "$@"
515
Junio C Hamano2d4c9932014-02-26 14:18:54 -0800516 if apply_stash "$@"
517 then
518 drop_stash "$@"
519 else
520 status=$?
521 say "The stash is kept in case you need it again."
522 exit $status
523 fi
Jon Seymourf2768722010-08-21 14:09:00 +1000524}
525
Brandon Caseye25d5f92008-02-22 13:04:54 -0600526drop_stash () {
Jon Seymour92e39e42010-08-21 14:08:59 +1000527 assert_stash_ref "$@"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600528
Jon Seymour92e39e42010-08-21 14:08:59 +1000529 git reflog delete --updateref --rewrite "${REV}" &&
Ævar Arnfjörð Bjarmasond4ca6c82011-05-21 18:44:18 +0000530 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
531 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600532
533 # clear_stash if we just dropped the last stash entry
David Aguilar1956dfa2014-09-15 20:24:10 -0700534 git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
535 clear_stash
Brandon Caseye25d5f92008-02-22 13:04:54 -0600536}
537
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530538apply_to_branch () {
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000539 test -n "$1" || die "$(gettext "No branch name specified")"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530540 branch=$1
Jon Seymourfb433dc2010-08-21 14:09:01 +1000541 shift 1
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530542
Jon Seymourfb433dc2010-08-21 14:09:01 +1000543 set -- --index "$@"
544 assert_stash_like "$@"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530545
Jon Seymourfb433dc2010-08-21 14:09:01 +1000546 git checkout -b $branch $REV^ &&
Jon Seymour57693d02010-09-28 23:19:52 +1000547 apply_stash "$@" && {
548 test -z "$IS_STASH_REF" || drop_stash "$@"
549 }
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530550}
551
Jon Seymouref763122010-08-21 14:46:22 +1000552PARSE_CACHE='--not-parsed'
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200553# The default command is "save" if nothing but options are given
554seen_non_option=
555for opt
556do
557 case "$opt" in
558 -*) ;;
559 *) seen_non_option=t; break ;;
560 esac
561done
562
563test -n "$seen_non_option" || set "save" "$@"
564
しらいしななこf2c66ed2007-06-30 14:37:09 +0900565# Main command set
566case "$1" in
Junio C Hamanofcb10a92007-07-02 23:15:45 -0700567list)
568 shift
しらいしななこf2c66ed2007-06-30 14:37:09 +0900569 list_stash "$@"
570 ;;
571show)
572 shift
573 show_stash "$@"
574 ;;
Kevin Leung683befa2007-12-03 10:34:05 +0800575save)
576 shift
Junio C Hamano47629dc2008-07-23 13:33:44 -0700577 save_stash "$@"
Kevin Leung683befa2007-12-03 10:34:05 +0800578 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900579apply)
580 shift
581 apply_stash "$@"
582 ;;
583clear)
Junio C Hamano3023dc62008-01-05 01:35:54 -0800584 shift
585 clear_stash "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900586 ;;
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700587create)
Ramkumar Ramachandra0719f302013-06-15 18:43:24 +0530588 shift
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700589 create_stash "$*" && echo "$w_commit"
590 ;;
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530591store)
592 shift
593 store_stash "$@"
594 ;;
Brandon Caseye25d5f92008-02-22 13:04:54 -0600595drop)
596 shift
597 drop_stash "$@"
598 ;;
Brandon Caseybd56ff52008-02-22 16:52:50 -0600599pop)
600 shift
Jon Seymourf2768722010-08-21 14:09:00 +1000601 pop_stash "$@"
Brandon Caseybd56ff52008-02-22 16:52:50 -0600602 ;;
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530603branch)
604 shift
605 apply_to_branch "$@"
606 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900607*)
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200608 case $# in
609 0)
Wincent Colaiuta97bc00a2007-12-22 18:31:25 +0100610 save_stash &&
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000611 say "$(gettext "(To restore them type \"git stash apply\")")"
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200612 ;;
613 *)
Kevin Leung683befa2007-12-03 10:34:05 +0800614 usage
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200615 esac
Junio C Hamano9f62e182007-07-04 22:46:09 -0700616 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900617esac