blob: bbefdf64244063f4708f381b8a772466d64e90e5 [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=
Piotr Krukowiecki26b59b42011-03-12 09:57:46 +010016START_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
Junio C Hamano0e321262008-12-07 17:30:35 -080053 if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
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 | (
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
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200110 if test -z "$patch_mode"
111 then
112
113 # state of the working tree
114 w_tree=$( (
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100115 git read-tree --index-output="$TMPindex" -m $i_tree &&
116 GIT_INDEX_FILE="$TMPindex" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200117 export GIT_INDEX_FILE &&
Jonathon Mah44df2e22011-12-30 16:14:01 -0800118 git diff --name-only -z HEAD -- >"$TMP-stagenames" &&
119 git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200120 git write-tree &&
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100121 rm -f "$TMPindex"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200122 ) ) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000123 die "$(gettext "Cannot save the current worktree state")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200124
125 else
126
Junio C Hamanob24f56d2007-07-08 01:28:18 -0700127 rm -f "$TMP-index" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200128 GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
129
130 # find out what the user wants
131 GIT_INDEX_FILE="$TMP-index" \
132 git add--interactive --patch=stash -- &&
133
134 # state of the working tree
135 w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000136 die "$(gettext "Cannot save the current worktree state")"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900137
Jonathon Mah44df2e22011-12-30 16:14:01 -0800138 git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200139 test -s "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000140 die "$(gettext "No changes selected")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200141
142 rm -f "$TMP-index" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000143 die "$(gettext "Cannot remove temporary index (can't happen)")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200144
145 fi
146
しらいしななこf2c66ed2007-06-30 14:37:09 +0900147 # create the stash
Junio C Hamano9f62e182007-07-04 22:46:09 -0700148 if test -z "$stash_msg"
149 then
150 stash_msg=$(printf 'WIP on %s' "$msg")
151 else
152 stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
153 fi
154 w_commit=$(printf '%s\n' "$stash_msg" |
Junio C Hamano22f41282011-07-22 14:46:28 -0700155 git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
156 die "$(gettext "Cannot record working tree state")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700157}
しらいしななこf2c66ed2007-06-30 14:37:09 +0900158
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700159save_stash () {
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200160 keep_index=
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200161 patch_mode=
David Caldwell78751302011-06-24 17:56:06 -0700162 untracked=
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700163 while test $# != 0
164 do
165 case "$1" in
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200166 -k|--keep-index)
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700167 keep_index=t
168 ;;
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200169 --no-keep-index)
Dan McGee3a243f72011-04-07 12:04:20 -0500170 keep_index=n
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200171 ;;
172 -p|--patch)
173 patch_mode=t
Dan McGee3a243f72011-04-07 12:04:20 -0500174 # only default to keep if we don't already have an override
175 test -z "$keep_index" && keep_index=t
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700176 ;;
177 -q|--quiet)
178 GIT_QUIET=t
179 ;;
David Caldwell78751302011-06-24 17:56:06 -0700180 -u|--include-untracked)
181 untracked=untracked
182 ;;
183 -a|--all)
184 untracked=all
185 ;;
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200186 --)
187 shift
188 break
189 ;;
190 -*)
Ævar Arnfjörð Bjarmasoneed10642011-05-21 18:44:17 +0000191 option="$1"
192 # TRANSLATORS: $option is an invalid option, like
193 # `--blah-blah'. The 7 spaces at the beginning of the
194 # second line correspond to "error: ". So you should line
195 # up the second line with however many characters the
196 # translation of "error: " takes in your language. E.g. in
197 # English this is:
198 #
199 # $ git stash save --blah-blah 2>&1 | head -n 2
200 # error: unknown option for 'stash save': --blah-blah
201 # To provide a message, use git stash save -- '--blah-blah'
Ross Lagerwalled3c4002012-04-14 14:37:29 +0200202 eval_gettextln "error: unknown option for 'stash save': \$option
203 To provide a message, use git stash save -- '\$option'"
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200204 usage
205 ;;
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700206 *)
207 break
208 ;;
209 esac
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200210 shift
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700211 done
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200212
David Caldwell78751302011-06-24 17:56:06 -0700213 if test -n "$patch_mode" && test -n "$untracked"
214 then
Brandon Caseyd88acc92011-08-26 19:59:25 -0500215 die "Can't use --patch and --include-untracked or --all at the same time"
David Caldwell78751302011-06-24 17:56:06 -0700216 fi
217
Junio C Hamano47629dc2008-07-23 13:33:44 -0700218 stash_msg="$*"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700219
Junio C Hamano1eff26c2008-09-04 02:41:22 -0700220 git update-index -q --refresh
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700221 if no_changes
222 then
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000223 say "$(gettext "No local changes to save")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700224 exit 0
225 fi
226 test -f "$GIT_DIR/logs/$ref_stash" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000227 clear_stash || die "$(gettext "Cannot initialize stash")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700228
David Caldwell78751302011-06-24 17:56:06 -0700229 create_stash "$stash_msg" $untracked
Emil Medvea9ee9bf2007-11-07 15:10:27 -0600230
231 # Make sure the reflog for stash is kept.
232 : >>"$GIT_DIR/logs/$ref_stash"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900233
Junio C Hamano9f62e182007-07-04 22:46:09 -0700234 git update-ref -m "$stash_msg" $ref_stash $w_commit ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000235 die "$(gettext "Cannot save the current status")"
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700236 say Saved working directory and index state "$stash_msg"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200237
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200238 if test -z "$patch_mode"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200239 then
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200240 git reset --hard ${GIT_QUIET:+-q}
David Caldwell78751302011-06-24 17:56:06 -0700241 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
242 if test -n "$untracked"
243 then
Brandon Casey7474b8b2011-08-26 19:59:27 -0500244 git clean --force --quiet -d $CLEAN_X_OPTION
David Caldwell78751302011-06-24 17:56:06 -0700245 fi
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200246
Dan McGee3a243f72011-04-07 12:04:20 -0500247 if test "$keep_index" = "t" && test -n $i_tree
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200248 then
249 git read-tree --reset -u $i_tree
250 fi
251 else
252 git apply -R < "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000253 die "$(gettext "Cannot remove worktree changes")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200254
Dan McGee3a243f72011-04-07 12:04:20 -0500255 if test "$keep_index" != "t"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200256 then
257 git reset
258 fi
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200259 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +0900260}
261
Jeff King401de402007-07-02 00:21:24 -0400262have_stash () {
Junio C Hamano0e321262008-12-07 17:30:35 -0800263 git rev-parse --verify $ref_stash >/dev/null 2>&1
Jeff King401de402007-07-02 00:21:24 -0400264}
265
しらいしななこf2c66ed2007-06-30 14:37:09 +0900266list_stash () {
Jeff King401de402007-07-02 00:21:24 -0400267 have_stash || return 0
Thomas Rast391c53b2009-10-19 17:48:11 +0200268 git log --format="%gd: %gs" -g "$@" $ref_stash --
しらいしななこf2c66ed2007-06-30 14:37:09 +0900269}
270
271show_stash () {
Jon Seymoura9bf09e2010-08-21 14:09:02 +1000272 assert_stash_like "$@"
Gustaf Hendeby14cd4582010-03-16 18:52:37 +0100273
Jon Seymoura9bf09e2010-08-21 14:09:02 +1000274 git diff ${FLAGS:---stat} $b_commit $w_commit
しらいしななこf2c66ed2007-06-30 14:37:09 +0900275}
276
Jon Seymouref763122010-08-21 14:46:22 +1000277#
278# Parses the remaining options looking for flags and
279# at most one revision defaulting to ${ref_stash}@{0}
280# if none found.
281#
282# Derives related tree and commit objects from the
283# revision, if one is found.
284#
285# stash records the work tree, and is a merge between the
286# base commit (first parent) and the index tree (second parent).
287#
288# REV is set to the symbolic version of the specified stash-like commit
289# IS_STASH_LIKE is non-blank if ${REV} looks like a stash
290# IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
291# s is set to the SHA1 of the stash commit
292# w_commit is set to the commit containing the working tree
293# b_commit is set to the base commit
294# i_commit is set to the commit containing the index tree
David Caldwell78751302011-06-24 17:56:06 -0700295# u_commit is set to the commit containing the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000296# w_tree is set to the working tree
297# b_tree is set to the base tree
298# i_tree is set to the index tree
David Caldwell78751302011-06-24 17:56:06 -0700299# u_tree is set to the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000300#
301# GIT_QUIET is set to t if -q is specified
302# INDEX_OPTION is set to --index if --index is specified.
303# FLAGS is set to the remaining flags
304#
305# dies if:
306# * too many revisions specified
307# * no revision is specified and there is no stash stack
308# * a revision is specified which cannot be resolve to a SHA1
309# * a non-existent stash reference is specified
310#
311
312parse_flags_and_rev()
313{
314 test "$PARSE_CACHE" = "$*" && return 0 # optimisation
315 PARSE_CACHE="$*"
316
317 IS_STASH_LIKE=
318 IS_STASH_REF=
319 INDEX_OPTION=
320 s=
321 w_commit=
322 b_commit=
323 i_commit=
David Caldwell78751302011-06-24 17:56:06 -0700324 u_commit=
Jon Seymouref763122010-08-21 14:46:22 +1000325 w_tree=
326 b_tree=
327 i_tree=
David Caldwell78751302011-06-24 17:56:06 -0700328 u_tree=
Jon Seymouref763122010-08-21 14:46:22 +1000329
Jeff King59d418f2011-04-05 17:20:25 -0400330 REV=$(git rev-parse --no-flags --symbolic "$@") || exit 1
Jon Seymour2bea5932010-09-28 01:32:45 +1000331
332 FLAGS=
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400333 for opt
334 do
335 case "$opt" in
Jon Seymour2bea5932010-09-28 01:32:45 +1000336 -q|--quiet)
337 GIT_QUIET=-t
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400338 ;;
Jon Seymouref763122010-08-21 14:46:22 +1000339 --index)
340 INDEX_OPTION=--index
341 ;;
Jon Seymour2bea5932010-09-28 01:32:45 +1000342 -*)
343 FLAGS="${FLAGS}${FLAGS:+ }$opt"
Jon Seymouref763122010-08-21 14:46:22 +1000344 ;;
345 esac
Jon Seymouref763122010-08-21 14:46:22 +1000346 done
347
348 set -- $REV
349
350 case $# in
351 0)
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000352 have_stash || die "$(gettext "No stash found.")"
Jon Seymouref763122010-08-21 14:46:22 +1000353 set -- ${ref_stash}@{0}
354 ;;
355 1)
356 :
357 ;;
358 *)
Ævar Arnfjörð Bjarmason33ceddc2011-05-21 18:44:14 +0000359 die "$(eval_gettext "Too many revisions specified: \$REV")"
Jon Seymouref763122010-08-21 14:46:22 +1000360 ;;
361 esac
362
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16 +0000363 REV=$(git rev-parse --quiet --symbolic --verify $1 2>/dev/null) || {
364 reference="$1"
365 die "$(eval_gettext "\$reference is not valid reference")"
366 }
Jon Seymouref763122010-08-21 14:46:22 +1000367
368 i_commit=$(git rev-parse --quiet --verify $REV^2 2>/dev/null) &&
369 set -- $(git rev-parse $REV $REV^1 $REV: $REV^1: $REV^2: 2>/dev/null) &&
370 s=$1 &&
371 w_commit=$1 &&
372 b_commit=$2 &&
373 w_tree=$3 &&
374 b_tree=$4 &&
375 i_tree=$5 &&
376 IS_STASH_LIKE=t &&
377 test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
378 IS_STASH_REF=t
David Caldwell78751302011-06-24 17:56:06 -0700379
380 u_commit=$(git rev-parse --quiet --verify $REV^3 2>/dev/null) &&
381 u_tree=$(git rev-parse $REV^3: 2>/dev/null)
Jon Seymouref763122010-08-21 14:46:22 +1000382}
383
384is_stash_like()
385{
386 parse_flags_and_rev "$@"
387 test -n "$IS_STASH_LIKE"
388}
389
390assert_stash_like() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000391 is_stash_like "$@" || {
392 args="$*"
393 die "$(eval_gettext "'\$args' is not a stash-like commit")"
394 }
Jon Seymouref763122010-08-21 14:46:22 +1000395}
396
397is_stash_ref() {
398 is_stash_like "$@" && test -n "$IS_STASH_REF"
399}
400
401assert_stash_ref() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000402 is_stash_ref "$@" || {
403 args="$*"
404 die "$(eval_gettext "'\$args' is not a stash reference")"
405 }
Jon Seymouref763122010-08-21 14:46:22 +1000406}
407
しらいしななこf2c66ed2007-06-30 14:37:09 +0900408apply_stash () {
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700409
Jon Seymour064ed102010-08-21 14:08:58 +1000410 assert_stash_like "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900411
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000412 git update-index -q --refresh || die "$(gettext "unable to refresh index")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300413
414 # current index state
415 c_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000416 die "$(gettext "Cannot apply a stash in the middle of a merge")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300417
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700418 unstashed_index_tree=
Jon Seymour064ed102010-08-21 14:08:58 +1000419 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200420 test "$c_tree" != "$i_tree"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700421 then
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400422 git diff-tree --binary $s^2^..$s^2 | git apply --cached
Johannes Schindelin150937c2007-07-02 12:14:49 +0100423 test $? -ne 0 &&
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000424 die "$(gettext "Conflicts in index. Try without --index.")"
Martin Koegler52070792009-07-22 07:30:58 +0200425 unstashed_index_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000426 die "$(gettext "Could not save index tree")"
Johannes Schindelin150937c2007-07-02 12:14:49 +0100427 git reset
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700428 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100429
David Caldwell78751302011-06-24 17:56:06 -0700430 if test -n "$u_tree"
431 then
432 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
433 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
434 rm -f "$TMPindex" ||
435 die 'Could not restore untracked files from stash'
436 fi
437
しらいしななこf2c66ed2007-06-30 14:37:09 +0900438 eval "
439 GITHEAD_$w_tree='Stashed changes' &&
440 GITHEAD_$c_tree='Updated upstream' &&
441 GITHEAD_$b_tree='Version stash was based on' &&
442 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
443 "
444
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700445 if test -n "$GIT_QUIET"
446 then
Junio C Hamano69ae92b2010-10-13 11:36:36 -0700447 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700448 fi
Martin Koegler52070792009-07-22 07:30:58 +0200449 if git merge-recursive $b_tree -- $c_tree $w_tree
しらいしななこf2c66ed2007-06-30 14:37:09 +0900450 then
451 # No conflict
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700452 if test -n "$unstashed_index_tree"
453 then
454 git read-tree "$unstashed_index_tree"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700455 else
456 a="$TMP-added" &&
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400457 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
Junio C Hamano83b3df72007-07-27 23:51:45 -0700458 git read-tree --reset $c_tree &&
459 git update-index --add --stdin <"$a" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000460 die "$(gettext "Cannot unstage modified files")"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700461 rm -f "$a"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700462 fi
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700463 squelch=
464 if test -n "$GIT_QUIET"
465 then
466 squelch='>/dev/null 2>&1'
467 fi
Piotr Krukowiecki26b59b42011-03-12 09:57:46 +0100468 (cd "$START_DIR" && eval "git status $squelch") || :
しらいしななこf2c66ed2007-06-30 14:37:09 +0900469 else
470 # Merge conflict; keep the exit status from merge-recursive
Johannes Schindelin150937c2007-07-02 12:14:49 +0100471 status=$?
Phil Hord743bf6d2012-07-10 18:52:28 -0400472 git rerere
Jon Seymour064ed102010-08-21 14:08:58 +1000473 if test -n "$INDEX_OPTION"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700474 then
Jon Seymour6fdd50e2011-08-07 21:58:16 +1000475 gettextln "Index was not unstashed." >&2
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700476 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100477 exit $status
しらいしななこf2c66ed2007-06-30 14:37:09 +0900478 fi
479}
480
Jon Seymourf2768722010-08-21 14:09:00 +1000481pop_stash() {
482 assert_stash_ref "$@"
483
484 apply_stash "$@" &&
485 drop_stash "$@"
486}
487
Brandon Caseye25d5f92008-02-22 13:04:54 -0600488drop_stash () {
Jon Seymour92e39e42010-08-21 14:08:59 +1000489 assert_stash_ref "$@"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600490
Jon Seymour92e39e42010-08-21 14:08:59 +1000491 git reflog delete --updateref --rewrite "${REV}" &&
Ævar Arnfjörð Bjarmasond4ca6c82011-05-21 18:44:18 +0000492 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
493 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600494
495 # clear_stash if we just dropped the last stash entry
Jonathon Mah44df2e22011-12-30 16:14:01 -0800496 git rev-parse --verify "$ref_stash@{0}" >/dev/null 2>&1 || clear_stash
Brandon Caseye25d5f92008-02-22 13:04:54 -0600497}
498
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530499apply_to_branch () {
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000500 test -n "$1" || die "$(gettext "No branch name specified")"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530501 branch=$1
Jon Seymourfb433dc2010-08-21 14:09:01 +1000502 shift 1
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530503
Jon Seymourfb433dc2010-08-21 14:09:01 +1000504 set -- --index "$@"
505 assert_stash_like "$@"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530506
Jon Seymourfb433dc2010-08-21 14:09:01 +1000507 git checkout -b $branch $REV^ &&
Jon Seymour57693d02010-09-28 23:19:52 +1000508 apply_stash "$@" && {
509 test -z "$IS_STASH_REF" || drop_stash "$@"
510 }
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530511}
512
Jon Seymouref763122010-08-21 14:46:22 +1000513PARSE_CACHE='--not-parsed'
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200514# The default command is "save" if nothing but options are given
515seen_non_option=
516for opt
517do
518 case "$opt" in
519 -*) ;;
520 *) seen_non_option=t; break ;;
521 esac
522done
523
524test -n "$seen_non_option" || set "save" "$@"
525
しらいしななこf2c66ed2007-06-30 14:37:09 +0900526# Main command set
527case "$1" in
Junio C Hamanofcb10a92007-07-02 23:15:45 -0700528list)
529 shift
しらいしななこf2c66ed2007-06-30 14:37:09 +0900530 list_stash "$@"
531 ;;
532show)
533 shift
534 show_stash "$@"
535 ;;
Kevin Leung683befa2007-12-03 10:34:05 +0800536save)
537 shift
Junio C Hamano47629dc2008-07-23 13:33:44 -0700538 save_stash "$@"
Kevin Leung683befa2007-12-03 10:34:05 +0800539 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900540apply)
541 shift
542 apply_stash "$@"
543 ;;
544clear)
Junio C Hamano3023dc62008-01-05 01:35:54 -0800545 shift
546 clear_stash "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900547 ;;
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700548create)
549 if test $# -gt 0 && test "$1" = create
550 then
551 shift
552 fi
553 create_stash "$*" && echo "$w_commit"
554 ;;
Brandon Caseye25d5f92008-02-22 13:04:54 -0600555drop)
556 shift
557 drop_stash "$@"
558 ;;
Brandon Caseybd56ff52008-02-22 16:52:50 -0600559pop)
560 shift
Jon Seymourf2768722010-08-21 14:09:00 +1000561 pop_stash "$@"
Brandon Caseybd56ff52008-02-22 16:52:50 -0600562 ;;
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530563branch)
564 shift
565 apply_to_branch "$@"
566 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900567*)
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200568 case $# in
569 0)
Wincent Colaiuta97bc00a2007-12-22 18:31:25 +0100570 save_stash &&
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000571 say "$(gettext "(To restore them type \"git stash apply\")")"
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200572 ;;
573 *)
Kevin Leung683befa2007-12-03 10:34:05 +0800574 usage
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200575 esac
Junio C Hamano9f62e182007-07-04 22:46:09 -0700576 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900577esac