🌐 AI搜索 & 代理 主页
blob: d4cf818be9488f1b94fdf4766a8f73db6bfd1029 [file] [log] [blame]
しらいしななこf2c66ed2007-06-30 05:37:091#!/bin/sh
2# Copyright (c) 2007, Nanako Shiraishi
3
Stephan Beyera5ab00c2008-08-16 03:27:314dashless=$(basename "$0" | sed -e 's/-/ /')
5USAGE="list [<options>]
Stephen Boydfcdd0e92009-06-18 01:07:376 or: $dashless show [<stash>]
7 or: $dashless drop [-q|--quiet] [<stash>]
8 or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
Stephan Beyera5ab00c2008-08-16 03:27:319 or: $dashless branch <branchname> [<stash>]
David Caldwell78751302011-06-25 00:56:0610 or: $dashless [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
11 [-u|--include-untracked] [-a|--all] [<message>]]
Stephan Beyera5ab00c2008-08-16 03:27:3112 or: $dashless clear"
しらいしななこf2c66ed2007-06-30 05:37:0913
James Bowes3cd24912007-07-06 19:57:4714SUBDIRECTORY_OK=Yes
Junio C Hamano8f321a32007-11-06 09:50:0215OPTIONS_SPEC=
Elia Pintod0ea45b2014-04-23 13:44:0116START_DIR=$(pwd)
しらいしななこf2c66ed2007-06-30 05:37:0917. git-sh-setup
Ævar Arnfjörð Bjarmason8a583be2011-05-21 18:44:1018. git-sh-i18n
しらいしななこf2c66ed2007-06-30 05:37:0919require_work_tree
Junio C Hamanoceff0792007-07-25 22:32:2220cd_to_toplevel
しらいしななこf2c66ed2007-06-30 05:37:0921
22TMP="$GIT_DIR/.git-stash.$$"
Johannes Sixt3ba2e862011-03-16 08:18:4923TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
24trap 'rm -f "$TMP-"* "$TMPindex"' 0
しらいしななこf2c66ed2007-06-30 05:37:0925
26ref_stash=refs/stash
27
Thomas Rastdda1f2a2009-08-13 12:29:4428if 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 05:37:0936no_changes () {
Johannes Schindelin6848d582008-05-14 17:03:5937 git diff-index --quiet --cached HEAD --ignore-submodules -- &&
David Caldwell78751302011-06-25 00:56:0638 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 05:37:0946}
47
48clear_stash () {
Junio C Hamano3023dc62008-01-05 09:35:5449 if test $# != 0
50 then
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:1351 die "$(gettext "git stash clear with parameters is unimplemented")"
Junio C Hamano3023dc62008-01-05 09:35:5452 fi
David Aguilar1956dfa2014-09-16 03:24:1053 if current=$(git rev-parse --verify --quiet $ref_stash)
Junio C Hamano7ab3cc72007-07-27 06:24:2854 then
Emil Medvea9ee9bf2007-11-07 21:10:2755 git update-ref -d $ref_stash $current
Junio C Hamano7ab3cc72007-07-27 06:24:2856 fi
しらいしななこf2c66ed2007-06-30 05:37:0957}
58
Junio C Hamanobc9e7392007-07-08 08:38:3259create_stash () {
Junio C Hamano9f62e182007-07-05 05:46:0960 stash_msg="$1"
David Caldwell78751302011-06-25 00:56:0661 untracked="$2"
Junio C Hamano9f62e182007-07-05 05:46:0962
Junio C Hamano1eff26c2008-09-04 09:41:2263 git update-index -q --refresh
しらいしななこf2c66ed2007-06-30 05:37:0964 if no_changes
65 then
しらいしななこf2c66ed2007-06-30 05:37:0966 exit 0
67 fi
しらいしななこf12e9252007-07-28 01:44:4868
しらいしななこf2c66ed2007-06-30 05:37:0969 # state of the base commit
Junio C Hamano5be60072007-07-03 05:52:1470 if b_commit=$(git rev-parse --verify HEAD)
しらいしななこf2c66ed2007-06-30 05:37:0971 then
Jeff Kingb0e621a2010-04-08 19:42:3772 head=$(git rev-list --oneline -n 1 HEAD --)
しらいしななこf2c66ed2007-06-30 05:37:0973 else
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:1374 die "$(gettext "You do not have the initial commit yet")"
しらいしななこf2c66ed2007-06-30 05:37:0975 fi
76
Junio C Hamano5be60072007-07-03 05:52:1477 if branch=$(git symbolic-ref -q HEAD)
しらいしななこf2c66ed2007-06-30 05:37:0978 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-03 05:52:1486 i_tree=$(git write-tree) &&
Jean-Luc Herren6143fa22007-09-12 18:45:0387 i_commit=$(printf 'index on %s\n' "$msg" |
Junio C Hamano5be60072007-07-03 05:52:1488 git commit-tree $i_tree -p $b_commit) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:1389 die "$(gettext "Cannot save the current index state")"
しらいしななこf2c66ed2007-06-30 05:37:0990
David Caldwell78751302011-06-25 00:56:0691 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 10:15:3197 GIT_INDEX_FILE="$TMPindex" &&
98 export GIT_INDEX_FILE &&
David Caldwell78751302011-06-25 00:56:0699 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 12:29:44111 if test -z "$patch_mode"
112 then
113
114 # state of the working tree
115 w_tree=$( (
Johannes Sixt3ba2e862011-03-16 08:18:49116 git read-tree --index-output="$TMPindex" -m $i_tree &&
117 GIT_INDEX_FILE="$TMPindex" &&
Thomas Rastdda1f2a2009-08-13 12:29:44118 export GIT_INDEX_FILE &&
Jonathon Mah44df2e22011-12-31 00:14:01119 git diff --name-only -z HEAD -- >"$TMP-stagenames" &&
120 git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
Thomas Rastdda1f2a2009-08-13 12:29:44121 git write-tree &&
Johannes Sixt3ba2e862011-03-16 08:18:49122 rm -f "$TMPindex"
Thomas Rastdda1f2a2009-08-13 12:29:44123 ) ) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13124 die "$(gettext "Cannot save the current worktree state")"
Thomas Rastdda1f2a2009-08-13 12:29:44125
126 else
127
Junio C Hamanob24f56d2007-07-08 08:28:18128 rm -f "$TMP-index" &&
Thomas Rastdda1f2a2009-08-13 12:29:44129 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:13137 die "$(gettext "Cannot save the current worktree state")"
しらいしななこf2c66ed2007-06-30 05:37:09138
Jonathon Mah44df2e22011-12-31 00:14:01139 git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
Thomas Rastdda1f2a2009-08-13 12:29:44140 test -s "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13141 die "$(gettext "No changes selected")"
Thomas Rastdda1f2a2009-08-13 12:29:44142
143 rm -f "$TMP-index" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13144 die "$(gettext "Cannot remove temporary index (can't happen)")"
Thomas Rastdda1f2a2009-08-13 12:29:44145
146 fi
147
しらいしななこf2c66ed2007-06-30 05:37:09148 # create the stash
Junio C Hamano9f62e182007-07-05 05:46:09149 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 21:46:28156 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 08:38:32158}
しらいしななこf2c66ed2007-06-30 05:37:09159
Ramkumar Ramachandrabd514ca2013-06-15 13:13:25160store_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 08:38:32195save_stash () {
SZEDER Gábor7bedebc2008-06-27 14:37:15196 keep_index=
Thomas Rastdda1f2a2009-08-13 12:29:44197 patch_mode=
David Caldwell78751302011-06-25 00:56:06198 untracked=
Stephen Boydfcdd0e92009-06-18 01:07:37199 while test $# != 0
200 do
201 case "$1" in
Johannes Schindelinea41cfc2009-07-27 18:37:10202 -k|--keep-index)
Stephen Boydfcdd0e92009-06-18 01:07:37203 keep_index=t
204 ;;
Thomas Rastdda1f2a2009-08-13 12:29:44205 --no-keep-index)
Dan McGee3a243f72011-04-07 17:04:20206 keep_index=n
Thomas Rastdda1f2a2009-08-13 12:29:44207 ;;
208 -p|--patch)
209 patch_mode=t
Dan McGee3a243f72011-04-07 17:04:20210 # only default to keep if we don't already have an override
211 test -z "$keep_index" && keep_index=t
Stephen Boydfcdd0e92009-06-18 01:07:37212 ;;
213 -q|--quiet)
214 GIT_QUIET=t
215 ;;
David Caldwell78751302011-06-25 00:56:06216 -u|--include-untracked)
217 untracked=untracked
218 ;;
219 -a|--all)
220 untracked=all
221 ;;
Matthieu Moy3c2eb802009-08-18 21:38:40222 --)
223 shift
224 break
225 ;;
226 -*)
Ævar Arnfjörð Bjarmasoneed10642011-05-21 18:44:17227 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 12:37:29238 eval_gettextln "error: unknown option for 'stash save': \$option
239 To provide a message, use git stash save -- '\$option'"
Matthieu Moy3c2eb802009-08-18 21:38:40240 usage
241 ;;
Stephen Boydfcdd0e92009-06-18 01:07:37242 *)
243 break
244 ;;
245 esac
SZEDER Gábor7bedebc2008-06-27 14:37:15246 shift
Stephen Boydfcdd0e92009-06-18 01:07:37247 done
SZEDER Gábor7bedebc2008-06-27 14:37:15248
David Caldwell78751302011-06-25 00:56:06249 if test -n "$patch_mode" && test -n "$untracked"
250 then
Brandon Caseyd88acc92011-08-27 00:59:25251 die "Can't use --patch and --include-untracked or --all at the same time"
David Caldwell78751302011-06-25 00:56:06252 fi
253
Junio C Hamano47629dc2008-07-23 20:33:44254 stash_msg="$*"
Junio C Hamanobc9e7392007-07-08 08:38:32255
Junio C Hamano1eff26c2008-09-04 09:41:22256 git update-index -q --refresh
Junio C Hamanobc9e7392007-07-08 08:38:32257 if no_changes
258 then
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12259 say "$(gettext "No local changes to save")"
Junio C Hamanobc9e7392007-07-08 08:38:32260 exit 0
261 fi
262 test -f "$GIT_DIR/logs/$ref_stash" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13263 clear_stash || die "$(gettext "Cannot initialize stash")"
Junio C Hamanobc9e7392007-07-08 08:38:32264
David Caldwell78751302011-06-25 00:56:06265 create_stash "$stash_msg" $untracked
Ramkumar Ramachandrabd514ca2013-06-15 13:13:25266 store_stash -m "$stash_msg" -q $w_commit ||
267 die "$(gettext "Cannot save the current status")"
Stephen Boydfcdd0e92009-06-18 01:07:37268 say Saved working directory and index state "$stash_msg"
SZEDER Gábor7bedebc2008-06-27 14:37:15269
Thomas Rastdda1f2a2009-08-13 12:29:44270 if test -z "$patch_mode"
SZEDER Gábor7bedebc2008-06-27 14:37:15271 then
Thomas Rastdda1f2a2009-08-13 12:29:44272 git reset --hard ${GIT_QUIET:+-q}
David Caldwell78751302011-06-25 00:56:06273 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
274 if test -n "$untracked"
275 then
Brandon Casey7474b8b2011-08-27 00:59:27276 git clean --force --quiet -d $CLEAN_X_OPTION
David Caldwell78751302011-06-25 00:56:06277 fi
Thomas Rastdda1f2a2009-08-13 12:29:44278
Dan McGee3a243f72011-04-07 17:04:20279 if test "$keep_index" = "t" && test -n $i_tree
Thomas Rastdda1f2a2009-08-13 12:29:44280 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:13285 die "$(gettext "Cannot remove worktree changes")"
Thomas Rastdda1f2a2009-08-13 12:29:44286
Dan McGee3a243f72011-04-07 17:04:20287 if test "$keep_index" != "t"
Thomas Rastdda1f2a2009-08-13 12:29:44288 then
289 git reset
290 fi
SZEDER Gábor7bedebc2008-06-27 14:37:15291 fi
しらいしななこf2c66ed2007-06-30 05:37:09292}
293
Jeff King401de402007-07-02 04:21:24294have_stash () {
David Aguilar1956dfa2014-09-16 03:24:10295 git rev-parse --verify --quiet $ref_stash >/dev/null
Jeff King401de402007-07-02 04:21:24296}
297
しらいしななこf2c66ed2007-06-30 05:37:09298list_stash () {
Jeff King401de402007-07-02 04:21:24299 have_stash || return 0
Jeff King288c67c2014-08-06 18:35:25300 git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
しらいしななこf2c66ed2007-06-30 05:37:09301}
302
303show_stash () {
Jon Seymoura9bf09e2010-08-21 04:09:02304 assert_stash_like "$@"
Gustaf Hendeby14cd4582010-03-16 17:52:37305
Jon Seymoura9bf09e2010-08-21 04:09:02306 git diff ${FLAGS:---stat} $b_commit $w_commit
しらいしななこf2c66ed2007-06-30 05:37:09307}
308
Jon Seymouref763122010-08-21 04:46:22309#
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-25 00:56:06327# u_commit is set to the commit containing the untracked files tree
Jon Seymouref763122010-08-21 04:46:22328# 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-25 00:56:06331# u_tree is set to the untracked files tree
Jon Seymouref763122010-08-21 04:46:22332#
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-25 00:56:06356 u_commit=
Jon Seymouref763122010-08-21 04:46:22357 w_tree=
358 b_tree=
359 i_tree=
David Caldwell78751302011-06-25 00:56:06360 u_tree=
Jon Seymouref763122010-08-21 04:46:22361
Øystein Walle2a07e432014-01-07 08:22:15362 REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
Jon Seymour2bea5932010-09-27 15:32:45363
364 FLAGS=
Brian Gernhardt9027fa92010-09-24 22:15:34365 for opt
366 do
367 case "$opt" in
Jon Seymour2bea5932010-09-27 15:32:45368 -q|--quiet)
369 GIT_QUIET=-t
Brian Gernhardt9027fa92010-09-24 22:15:34370 ;;
Jon Seymouref763122010-08-21 04:46:22371 --index)
372 INDEX_OPTION=--index
373 ;;
Jon Seymour2bea5932010-09-27 15:32:45374 -*)
375 FLAGS="${FLAGS}${FLAGS:+ }$opt"
Jon Seymouref763122010-08-21 04:46:22376 ;;
377 esac
Jon Seymouref763122010-08-21 04:46:22378 done
379
Øystein Walle2a07e432014-01-07 08:22:15380 eval set -- $REV
Jon Seymouref763122010-08-21 04:46:22381
382 case $# in
383 0)
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13384 have_stash || die "$(gettext "No stash found.")"
Jon Seymouref763122010-08-21 04:46:22385 set -- ${ref_stash}@{0}
386 ;;
387 1)
388 :
389 ;;
390 *)
Ævar Arnfjörð Bjarmason33ceddc2011-05-21 18:44:14391 die "$(eval_gettext "Too many revisions specified: \$REV")"
Jon Seymouref763122010-08-21 04:46:22392 ;;
393 esac
394
David Aguilar1956dfa2014-09-16 03:24:10395 REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16396 reference="$1"
Alex Henriead5fe372014-08-30 19:56:01397 die "$(eval_gettext "\$reference is not a valid reference")"
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16398 }
Jon Seymouref763122010-08-21 04:46:22399
David Aguilar1956dfa2014-09-16 03:24:10400 i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
Øystein Walle2a07e432014-01-07 08:22:15401 set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
Jon Seymouref763122010-08-21 04:46:22402 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-25 00:56:06411
David Aguilar1956dfa2014-09-16 03:24:10412 u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
Øystein Walle2a07e432014-01-07 08:22:15413 u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
Jon Seymouref763122010-08-21 04:46:22414}
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:15423 is_stash_like "$@" || {
424 args="$*"
425 die "$(eval_gettext "'\$args' is not a stash-like commit")"
426 }
Jon Seymouref763122010-08-21 04:46:22427}
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:15434 is_stash_ref "$@" || {
435 args="$*"
436 die "$(eval_gettext "'\$args' is not a stash reference")"
437 }
Jon Seymouref763122010-08-21 04:46:22438}
439
しらいしななこf2c66ed2007-06-30 05:37:09440apply_stash () {
Stephen Boydfcdd0e92009-06-18 01:07:37441
Jon Seymour064ed102010-08-21 04:08:58442 assert_stash_like "$@"
しらいしななこf2c66ed2007-06-30 05:37:09443
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13444 git update-index -q --refresh || die "$(gettext "unable to refresh index")"
Ori Avtalion5fd448f2009-08-11 11:12:13445
446 # current index state
447 c_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13448 die "$(gettext "Cannot apply a stash in the middle of a merge")"
Ori Avtalion5fd448f2009-08-11 11:12:13449
Junio C Hamanocbeaccc2007-07-28 06:41:31450 unstashed_index_tree=
Jon Seymour064ed102010-08-21 04:08:58451 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
SZEDER Gábor7bedebc2008-06-27 14:37:15452 test "$c_tree" != "$i_tree"
Junio C Hamanocbeaccc2007-07-28 06:41:31453 then
Shawn O. Pearce89d750b2007-10-19 01:28:43454 git diff-tree --binary $s^2^..$s^2 | git apply --cached
Johannes Schindelin150937c2007-07-02 11:14:49455 test $? -ne 0 &&
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13456 die "$(gettext "Conflicts in index. Try without --index.")"
Martin Koegler52070792009-07-22 05:30:58457 unstashed_index_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13458 die "$(gettext "Could not save index tree")"
Johannes Schindelin150937c2007-07-02 11:14:49459 git reset
Junio C Hamanocbeaccc2007-07-28 06:41:31460 fi
Johannes Schindelin150937c2007-07-02 11:14:49461
David Caldwell78751302011-06-25 00:56:06462 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 05:37:09470 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-18 01:07:37477 if test -n "$GIT_QUIET"
478 then
Junio C Hamano69ae92b2010-10-13 18:36:36479 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boydfcdd0e92009-06-18 01:07:37480 fi
Martin Koegler52070792009-07-22 05:30:58481 if git merge-recursive $b_tree -- $c_tree $w_tree
しらいしななこf2c66ed2007-06-30 05:37:09482 then
483 # No conflict
Junio C Hamanocbeaccc2007-07-28 06:41:31484 if test -n "$unstashed_index_tree"
485 then
486 git read-tree "$unstashed_index_tree"
Junio C Hamano83b3df72007-07-28 06:51:45487 else
488 a="$TMP-added" &&
Shawn O. Pearce89d750b2007-10-19 01:28:43489 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
Junio C Hamano83b3df72007-07-28 06:51:45490 git read-tree --reset $c_tree &&
491 git update-index --add --stdin <"$a" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13492 die "$(gettext "Cannot unstage modified files")"
Junio C Hamano83b3df72007-07-28 06:51:45493 rm -f "$a"
Junio C Hamanocbeaccc2007-07-28 06:41:31494 fi
Stephen Boydfcdd0e92009-06-18 01:07:37495 squelch=
496 if test -n "$GIT_QUIET"
497 then
498 squelch='>/dev/null 2>&1'
499 fi
Piotr Krukowiecki26b59b42011-03-12 08:57:46500 (cd "$START_DIR" && eval "git status $squelch") || :
しらいしななこf2c66ed2007-06-30 05:37:09501 else
502 # Merge conflict; keep the exit status from merge-recursive
Johannes Schindelin150937c2007-07-02 11:14:49503 status=$?
Phil Hord743bf6d2012-07-10 22:52:28504 git rerere
Jon Seymour064ed102010-08-21 04:08:58505 if test -n "$INDEX_OPTION"
Junio C Hamanocbeaccc2007-07-28 06:41:31506 then
Jon Seymour6fdd50e2011-08-07 11:58:16507 gettextln "Index was not unstashed." >&2
Junio C Hamanocbeaccc2007-07-28 06:41:31508 fi
Johannes Schindelin150937c2007-07-02 11:14:49509 exit $status
しらいしななこf2c66ed2007-06-30 05:37:09510 fi
511}
512
Jon Seymourf2768722010-08-21 04:09:00513pop_stash() {
514 assert_stash_ref "$@"
515
Junio C Hamano2d4c9932014-02-26 22:18:54516 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 04:09:00524}
525
Brandon Caseye25d5f92008-02-22 19:04:54526drop_stash () {
Jon Seymour92e39e42010-08-21 04:08:59527 assert_stash_ref "$@"
Brandon Caseye25d5f92008-02-22 19:04:54528
Jon Seymour92e39e42010-08-21 04:08:59529 git reflog delete --updateref --rewrite "${REV}" &&
Ævar Arnfjörð Bjarmasond4ca6c82011-05-21 18:44:18530 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
531 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
Brandon Caseye25d5f92008-02-22 19:04:54532
533 # clear_stash if we just dropped the last stash entry
David Aguilar1956dfa2014-09-16 03:24:10534 git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
535 clear_stash
Brandon Caseye25d5f92008-02-22 19:04:54536}
537
Abhijit Menon-Sen656b5032008-07-03 06:16:05538apply_to_branch () {
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13539 test -n "$1" || die "$(gettext "No branch name specified")"
Abhijit Menon-Sen656b5032008-07-03 06:16:05540 branch=$1
Jon Seymourfb433dc2010-08-21 04:09:01541 shift 1
Abhijit Menon-Sen656b5032008-07-03 06:16:05542
Jon Seymourfb433dc2010-08-21 04:09:01543 set -- --index "$@"
544 assert_stash_like "$@"
Abhijit Menon-Sen656b5032008-07-03 06:16:05545
Jon Seymourfb433dc2010-08-21 04:09:01546 git checkout -b $branch $REV^ &&
Jon Seymour57693d02010-09-28 13:19:52547 apply_stash "$@" && {
548 test -z "$IS_STASH_REF" || drop_stash "$@"
549 }
Abhijit Menon-Sen656b5032008-07-03 06:16:05550}
551
Jon Seymouref763122010-08-21 04:46:22552PARSE_CACHE='--not-parsed'
Matthieu Moy3c2eb802009-08-18 21:38:40553# 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 05:37:09565# Main command set
566case "$1" in
Junio C Hamanofcb10a92007-07-03 06:15:45567list)
568 shift
しらいしななこf2c66ed2007-06-30 05:37:09569 list_stash "$@"
570 ;;
571show)
572 shift
573 show_stash "$@"
574 ;;
Kevin Leung683befa2007-12-03 02:34:05575save)
576 shift
Junio C Hamano47629dc2008-07-23 20:33:44577 save_stash "$@"
Kevin Leung683befa2007-12-03 02:34:05578 ;;
しらいしななこf2c66ed2007-06-30 05:37:09579apply)
580 shift
581 apply_stash "$@"
582 ;;
583clear)
Junio C Hamano3023dc62008-01-05 09:35:54584 shift
585 clear_stash "$@"
しらいしななこf2c66ed2007-06-30 05:37:09586 ;;
Junio C Hamanobc9e7392007-07-08 08:38:32587create)
Ramkumar Ramachandra0719f302013-06-15 13:13:24588 shift
Junio C Hamanobc9e7392007-07-08 08:38:32589 create_stash "$*" && echo "$w_commit"
590 ;;
Ramkumar Ramachandrabd514ca2013-06-15 13:13:25591store)
592 shift
593 store_stash "$@"
594 ;;
Brandon Caseye25d5f92008-02-22 19:04:54595drop)
596 shift
597 drop_stash "$@"
598 ;;
Brandon Caseybd56ff52008-02-22 22:52:50599pop)
600 shift
Jon Seymourf2768722010-08-21 04:09:00601 pop_stash "$@"
Brandon Caseybd56ff52008-02-22 22:52:50602 ;;
Abhijit Menon-Sen656b5032008-07-03 06:16:05603branch)
604 shift
605 apply_to_branch "$@"
606 ;;
しらいしななこf2c66ed2007-06-30 05:37:09607*)
Matthieu Moy3c2eb802009-08-18 21:38:40608 case $# in
609 0)
Wincent Colaiuta97bc00a2007-12-22 17:31:25610 save_stash &&
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12611 say "$(gettext "(To restore them type \"git stash apply\")")"
Johannes Schindelinea41cfc2009-07-27 18:37:10612 ;;
613 *)
Kevin Leung683befa2007-12-03 02:34:05614 usage
Johannes Schindelinea41cfc2009-07-27 18:37:10615 esac
Junio C Hamano9f62e182007-07-05 05:46:09616 ;;
しらいしななこf2c66ed2007-06-30 05:37:09617esac