blob: c76669284ce48411b0cd580c5943e3eb0a785884 [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 &&
Charles Bailey7aa5d432010-04-18 19:28:05 +0100118 git diff --name-only -z HEAD | git update-index -z --add --remove --stdin &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200119 git write-tree &&
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100120 rm -f "$TMPindex"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200121 ) ) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000122 die "$(gettext "Cannot save the current worktree state")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200123
124 else
125
Junio C Hamanob24f56d2007-07-08 01:28:18 -0700126 rm -f "$TMP-index" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200127 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) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000135 die "$(gettext "Cannot save the current worktree state")"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900136
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200137 git diff-tree -p HEAD $w_tree > "$TMP-patch" &&
138 test -s "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000139 die "$(gettext "No changes selected")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200140
141 rm -f "$TMP-index" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000142 die "$(gettext "Cannot remove temporary index (can't happen)")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200143
144 fi
145
しらいしななこf2c66ed2007-06-30 14:37:09 +0900146 # create the stash
Junio C Hamano9f62e182007-07-04 22:46:09 -0700147 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" |
Junio C Hamano22f41282011-07-22 14:46:28 -0700154 git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
155 die "$(gettext "Cannot record working tree state")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700156}
しらいしななこf2c66ed2007-06-30 14:37:09 +0900157
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700158save_stash () {
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200159 keep_index=
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200160 patch_mode=
David Caldwell78751302011-06-24 17:56:06 -0700161 untracked=
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700162 while test $# != 0
163 do
164 case "$1" in
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200165 -k|--keep-index)
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700166 keep_index=t
167 ;;
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200168 --no-keep-index)
Dan McGee3a243f72011-04-07 12:04:20 -0500169 keep_index=n
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200170 ;;
171 -p|--patch)
172 patch_mode=t
Dan McGee3a243f72011-04-07 12:04:20 -0500173 # only default to keep if we don't already have an override
174 test -z "$keep_index" && keep_index=t
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700175 ;;
176 -q|--quiet)
177 GIT_QUIET=t
178 ;;
David Caldwell78751302011-06-24 17:56:06 -0700179 -u|--include-untracked)
180 untracked=untracked
181 ;;
182 -a|--all)
183 untracked=all
184 ;;
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200185 --)
186 shift
187 break
188 ;;
189 -*)
Ævar Arnfjörð Bjarmasoneed10642011-05-21 18:44:17 +0000190 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'
Jon Seymour6fdd50e2011-08-07 21:58:16 +1000201 eval_gettextln "$("error: unknown option for 'stash save': \$option
202 To provide a message, use git stash save -- '\$option'")"
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200203 usage
204 ;;
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700205 *)
206 break
207 ;;
208 esac
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200209 shift
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700210 done
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200211
David Caldwell78751302011-06-24 17:56:06 -0700212 if test -n "$patch_mode" && test -n "$untracked"
213 then
Brandon Caseyd88acc92011-08-26 19:59:25 -0500214 die "Can't use --patch and --include-untracked or --all at the same time"
David Caldwell78751302011-06-24 17:56:06 -0700215 fi
216
Junio C Hamano47629dc2008-07-23 13:33:44 -0700217 stash_msg="$*"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700218
Junio C Hamano1eff26c2008-09-04 02:41:22 -0700219 git update-index -q --refresh
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700220 if no_changes
221 then
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000222 say "$(gettext "No local changes to save")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700223 exit 0
224 fi
225 test -f "$GIT_DIR/logs/$ref_stash" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000226 clear_stash || die "$(gettext "Cannot initialize stash")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700227
David Caldwell78751302011-06-24 17:56:06 -0700228 create_stash "$stash_msg" $untracked
Emil Medvea9ee9bf2007-11-07 15:10:27 -0600229
230 # Make sure the reflog for stash is kept.
231 : >>"$GIT_DIR/logs/$ref_stash"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900232
Junio C Hamano9f62e182007-07-04 22:46:09 -0700233 git update-ref -m "$stash_msg" $ref_stash $w_commit ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000234 die "$(gettext "Cannot save the current status")"
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700235 say Saved working directory and index state "$stash_msg"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200236
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200237 if test -z "$patch_mode"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200238 then
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200239 git reset --hard ${GIT_QUIET:+-q}
David Caldwell78751302011-06-24 17:56:06 -0700240 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
241 if test -n "$untracked"
242 then
Brandon Casey7474b8b2011-08-26 19:59:27 -0500243 git clean --force --quiet -d $CLEAN_X_OPTION
David Caldwell78751302011-06-24 17:56:06 -0700244 fi
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200245
Dan McGee3a243f72011-04-07 12:04:20 -0500246 if test "$keep_index" = "t" && test -n $i_tree
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200247 then
248 git read-tree --reset -u $i_tree
249 fi
250 else
251 git apply -R < "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000252 die "$(gettext "Cannot remove worktree changes")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200253
Dan McGee3a243f72011-04-07 12:04:20 -0500254 if test "$keep_index" != "t"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200255 then
256 git reset
257 fi
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200258 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +0900259}
260
Jeff King401de402007-07-02 00:21:24 -0400261have_stash () {
Junio C Hamano0e321262008-12-07 17:30:35 -0800262 git rev-parse --verify $ref_stash >/dev/null 2>&1
Jeff King401de402007-07-02 00:21:24 -0400263}
264
しらいしななこf2c66ed2007-06-30 14:37:09 +0900265list_stash () {
Jeff King401de402007-07-02 00:21:24 -0400266 have_stash || return 0
Thomas Rast391c53b2009-10-19 17:48:11 +0200267 git log --format="%gd: %gs" -g "$@" $ref_stash --
しらいしななこf2c66ed2007-06-30 14:37:09 +0900268}
269
270show_stash () {
Jon Seymoura9bf09e2010-08-21 14:09:02 +1000271 assert_stash_like "$@"
Gustaf Hendeby14cd4582010-03-16 18:52:37 +0100272
Jon Seymoura9bf09e2010-08-21 14:09:02 +1000273 git diff ${FLAGS:---stat} $b_commit $w_commit
しらいしななこf2c66ed2007-06-30 14:37:09 +0900274}
275
Jon Seymouref763122010-08-21 14:46:22 +1000276#
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
David Caldwell78751302011-06-24 17:56:06 -0700294# u_commit is set to the commit containing the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000295# 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
David Caldwell78751302011-06-24 17:56:06 -0700298# u_tree is set to the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000299#
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=
David Caldwell78751302011-06-24 17:56:06 -0700323 u_commit=
Jon Seymouref763122010-08-21 14:46:22 +1000324 w_tree=
325 b_tree=
326 i_tree=
David Caldwell78751302011-06-24 17:56:06 -0700327 u_tree=
Jon Seymouref763122010-08-21 14:46:22 +1000328
Jeff King59d418f2011-04-05 17:20:25 -0400329 REV=$(git rev-parse --no-flags --symbolic "$@") || exit 1
Jon Seymour2bea5932010-09-28 01:32:45 +1000330
331 FLAGS=
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400332 for opt
333 do
334 case "$opt" in
Jon Seymour2bea5932010-09-28 01:32:45 +1000335 -q|--quiet)
336 GIT_QUIET=-t
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400337 ;;
Jon Seymouref763122010-08-21 14:46:22 +1000338 --index)
339 INDEX_OPTION=--index
340 ;;
Jon Seymour2bea5932010-09-28 01:32:45 +1000341 -*)
342 FLAGS="${FLAGS}${FLAGS:+ }$opt"
Jon Seymouref763122010-08-21 14:46:22 +1000343 ;;
344 esac
Jon Seymouref763122010-08-21 14:46:22 +1000345 done
346
347 set -- $REV
348
349 case $# in
350 0)
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000351 have_stash || die "$(gettext "No stash found.")"
Jon Seymouref763122010-08-21 14:46:22 +1000352 set -- ${ref_stash}@{0}
353 ;;
354 1)
355 :
356 ;;
357 *)
Ævar Arnfjörð Bjarmason33ceddc2011-05-21 18:44:14 +0000358 die "$(eval_gettext "Too many revisions specified: \$REV")"
Jon Seymouref763122010-08-21 14:46:22 +1000359 ;;
360 esac
361
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16 +0000362 REV=$(git rev-parse --quiet --symbolic --verify $1 2>/dev/null) || {
363 reference="$1"
364 die "$(eval_gettext "\$reference is not valid reference")"
365 }
Jon Seymouref763122010-08-21 14:46:22 +1000366
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
David Caldwell78751302011-06-24 17:56:06 -0700378
379 u_commit=$(git rev-parse --quiet --verify $REV^3 2>/dev/null) &&
380 u_tree=$(git rev-parse $REV^3: 2>/dev/null)
Jon Seymouref763122010-08-21 14:46:22 +1000381}
382
383is_stash_like()
384{
385 parse_flags_and_rev "$@"
386 test -n "$IS_STASH_LIKE"
387}
388
389assert_stash_like() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000390 is_stash_like "$@" || {
391 args="$*"
392 die "$(eval_gettext "'\$args' is not a stash-like commit")"
393 }
Jon Seymouref763122010-08-21 14:46:22 +1000394}
395
396is_stash_ref() {
397 is_stash_like "$@" && test -n "$IS_STASH_REF"
398}
399
400assert_stash_ref() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000401 is_stash_ref "$@" || {
402 args="$*"
403 die "$(eval_gettext "'\$args' is not a stash reference")"
404 }
Jon Seymouref763122010-08-21 14:46:22 +1000405}
406
しらいしななこf2c66ed2007-06-30 14:37:09 +0900407apply_stash () {
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700408
Jon Seymour064ed102010-08-21 14:08:58 +1000409 assert_stash_like "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900410
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000411 git update-index -q --refresh || die "$(gettext "unable to refresh index")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300412
413 # current index state
414 c_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000415 die "$(gettext "Cannot apply a stash in the middle of a merge")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300416
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700417 unstashed_index_tree=
Jon Seymour064ed102010-08-21 14:08:58 +1000418 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200419 test "$c_tree" != "$i_tree"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700420 then
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400421 git diff-tree --binary $s^2^..$s^2 | git apply --cached
Johannes Schindelin150937c2007-07-02 12:14:49 +0100422 test $? -ne 0 &&
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000423 die "$(gettext "Conflicts in index. Try without --index.")"
Martin Koegler52070792009-07-22 07:30:58 +0200424 unstashed_index_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000425 die "$(gettext "Could not save index tree")"
Johannes Schindelin150937c2007-07-02 12:14:49 +0100426 git reset
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700427 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100428
David Caldwell78751302011-06-24 17:56:06 -0700429 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
しらいしななこf2c66ed2007-06-30 14:37:09 +0900437 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
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700444 if test -n "$GIT_QUIET"
445 then
Junio C Hamano69ae92b2010-10-13 11:36:36 -0700446 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700447 fi
Martin Koegler52070792009-07-22 07:30:58 +0200448 if git merge-recursive $b_tree -- $c_tree $w_tree
しらいしななこf2c66ed2007-06-30 14:37:09 +0900449 then
450 # No conflict
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700451 if test -n "$unstashed_index_tree"
452 then
453 git read-tree "$unstashed_index_tree"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700454 else
455 a="$TMP-added" &&
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400456 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
Junio C Hamano83b3df72007-07-27 23:51:45 -0700457 git read-tree --reset $c_tree &&
458 git update-index --add --stdin <"$a" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000459 die "$(gettext "Cannot unstage modified files")"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700460 rm -f "$a"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700461 fi
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700462 squelch=
463 if test -n "$GIT_QUIET"
464 then
465 squelch='>/dev/null 2>&1'
466 fi
Piotr Krukowiecki26b59b42011-03-12 09:57:46 +0100467 (cd "$START_DIR" && eval "git status $squelch") || :
しらいしななこf2c66ed2007-06-30 14:37:09 +0900468 else
469 # Merge conflict; keep the exit status from merge-recursive
Johannes Schindelin150937c2007-07-02 12:14:49 +0100470 status=$?
Jon Seymour064ed102010-08-21 14:08:58 +1000471 if test -n "$INDEX_OPTION"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700472 then
Jon Seymour6fdd50e2011-08-07 21:58:16 +1000473 gettextln "Index was not unstashed." >&2
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700474 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100475 exit $status
しらいしななこf2c66ed2007-06-30 14:37:09 +0900476 fi
477}
478
Jon Seymourf2768722010-08-21 14:09:00 +1000479pop_stash() {
480 assert_stash_ref "$@"
481
482 apply_stash "$@" &&
483 drop_stash "$@"
484}
485
Brandon Caseye25d5f92008-02-22 13:04:54 -0600486drop_stash () {
Jon Seymour92e39e42010-08-21 14:08:59 +1000487 assert_stash_ref "$@"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600488
Jon Seymour92e39e42010-08-21 14:08:59 +1000489 git reflog delete --updateref --rewrite "${REV}" &&
Ævar Arnfjörð Bjarmasond4ca6c82011-05-21 18:44:18 +0000490 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
491 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600492
493 # clear_stash if we just dropped the last stash entry
Junio C Hamano0e321262008-12-07 17:30:35 -0800494 git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
Brandon Caseye25d5f92008-02-22 13:04:54 -0600495}
496
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530497apply_to_branch () {
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000498 test -n "$1" || die "$(gettext "No branch name specified")"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530499 branch=$1
Jon Seymourfb433dc2010-08-21 14:09:01 +1000500 shift 1
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530501
Jon Seymourfb433dc2010-08-21 14:09:01 +1000502 set -- --index "$@"
503 assert_stash_like "$@"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530504
Jon Seymourfb433dc2010-08-21 14:09:01 +1000505 git checkout -b $branch $REV^ &&
Jon Seymour57693d02010-09-28 23:19:52 +1000506 apply_stash "$@" && {
507 test -z "$IS_STASH_REF" || drop_stash "$@"
508 }
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530509}
510
Jon Seymouref763122010-08-21 14:46:22 +1000511PARSE_CACHE='--not-parsed'
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200512# 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
しらいしななこf2c66ed2007-06-30 14:37:09 +0900524# Main command set
525case "$1" in
Junio C Hamanofcb10a92007-07-02 23:15:45 -0700526list)
527 shift
しらいしななこf2c66ed2007-06-30 14:37:09 +0900528 list_stash "$@"
529 ;;
530show)
531 shift
532 show_stash "$@"
533 ;;
Kevin Leung683befa2007-12-03 10:34:05 +0800534save)
535 shift
Junio C Hamano47629dc2008-07-23 13:33:44 -0700536 save_stash "$@"
Kevin Leung683befa2007-12-03 10:34:05 +0800537 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900538apply)
539 shift
540 apply_stash "$@"
541 ;;
542clear)
Junio C Hamano3023dc62008-01-05 01:35:54 -0800543 shift
544 clear_stash "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900545 ;;
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700546create)
547 if test $# -gt 0 && test "$1" = create
548 then
549 shift
550 fi
551 create_stash "$*" && echo "$w_commit"
552 ;;
Brandon Caseye25d5f92008-02-22 13:04:54 -0600553drop)
554 shift
555 drop_stash "$@"
556 ;;
Brandon Caseybd56ff52008-02-22 16:52:50 -0600557pop)
558 shift
Jon Seymourf2768722010-08-21 14:09:00 +1000559 pop_stash "$@"
Brandon Caseybd56ff52008-02-22 16:52:50 -0600560 ;;
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530561branch)
562 shift
563 apply_to_branch "$@"
564 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900565*)
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200566 case $# in
567 0)
Wincent Colaiuta97bc00a2007-12-22 18:31:25 +0100568 save_stash &&
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000569 say "$(gettext "(To restore them type \"git stash apply\")")"
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200570 ;;
571 *)
Kevin Leung683befa2007-12-03 10:34:05 +0800572 usage
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200573 esac
Junio C Hamano9f62e182007-07-04 22:46:09 -0700574 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900575esac