blob: fc8f8ae6401dddcceaa82f9e9c748f5c185536d6 [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>]
Thomas Gummerer1ada5022017-02-28 20:33:39 +000010 or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
11 [-u|--include-untracked] [-a|--all] [<message>]
12 or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]
13 [-u|--include-untracked] [-a|--all] [-m <message>]
14 [-- <pathspec>...]]
Stephan Beyera5ab00c2008-08-16 05:27:31 +020015 or: $dashless clear"
しらいしななこf2c66ed2007-06-30 14:37:09 +090016
James Bowes3cd24912007-07-06 15:57:47 -040017SUBDIRECTORY_OK=Yes
Junio C Hamano8f321a32007-11-06 01:50:02 -080018OPTIONS_SPEC=
Elia Pintod0ea45b2014-04-23 06:44:01 -070019START_DIR=$(pwd)
しらいしななこf2c66ed2007-06-30 14:37:09 +090020. git-sh-setup
21require_work_tree
Patrick Steinhardt22fc7032017-06-13 13:38:34 +020022prefix=$(git rev-parse --show-prefix) || exit 1
Junio C Hamanoceff0792007-07-25 15:32:22 -070023cd_to_toplevel
しらいしななこf2c66ed2007-06-30 14:37:09 +090024
25TMP="$GIT_DIR/.git-stash.$$"
Nguyễn Thái Ngọc Duyc697b572014-11-30 15:24:32 +070026TMPindex=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$
Johannes Sixt3ba2e862011-03-16 09:18:49 +010027trap 'rm -f "$TMP-"* "$TMPindex"' 0
しらいしななこf2c66ed2007-06-30 14:37:09 +090028
29ref_stash=refs/stash
30
Thomas Rastdda1f2a2009-08-13 14:29:44 +020031if git config --get-colorbool color.interactive; then
32 help_color="$(git config --get-color color.interactive.help 'red bold')"
33 reset_color="$(git config --get-color '' reset)"
34else
35 help_color=
36 reset_color=
37fi
38
しらいしななこf2c66ed2007-06-30 14:37:09 +090039no_changes () {
Thomas Gummererdf6bba02017-02-28 20:33:38 +000040 git diff-index --quiet --cached HEAD --ignore-submodules -- "$@" &&
41 git diff-files --quiet --ignore-submodules -- "$@" &&
David Caldwell78751302011-06-24 17:56:06 -070042 (test -z "$untracked" || test -z "$(untracked_files)")
43}
44
45untracked_files () {
Kevin Daudt5fc92f82017-08-14 23:43:33 +020046 if test "$1" = "-z"
47 then
48 shift
49 z=-z
50 else
51 z=
52 fi
David Caldwell78751302011-06-24 17:56:06 -070053 excl_opt=--exclude-standard
54 test "$untracked" = "all" && excl_opt=
Kevin Daudt5fc92f82017-08-14 23:43:33 +020055 git ls-files -o $z $excl_opt -- "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +090056}
57
58clear_stash () {
Junio C Hamano3023dc62008-01-05 01:35:54 -080059 if test $# != 0
60 then
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +000061 die "$(gettext "git stash clear with parameters is unimplemented")"
Junio C Hamano3023dc62008-01-05 01:35:54 -080062 fi
David Aguilar1956dfa2014-09-15 20:24:10 -070063 if current=$(git rev-parse --verify --quiet $ref_stash)
Junio C Hamano7ab3cc72007-07-26 23:24:28 -070064 then
Emil Medvea9ee9bf2007-11-07 15:10:27 -060065 git update-ref -d $ref_stash $current
Junio C Hamano7ab3cc72007-07-26 23:24:28 -070066 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +090067}
68
Junio C Hamanobc9e7392007-07-08 01:38:32 -070069create_stash () {
Thomas Gummerer9ca63262017-02-19 11:03:10 +000070 stash_msg=
71 untracked=
72 while test $# != 0
73 do
74 case "$1" in
75 -m|--message)
76 shift
77 stash_msg=${1?"BUG: create_stash () -m requires an argument"}
78 ;;
Phil Hord56754732017-11-22 13:20:30 -080079 -m*)
80 stash_msg=${1#-m}
81 ;;
82 --message=*)
83 stash_msg=${1#--message=}
84 ;;
Thomas Gummerer9ca63262017-02-19 11:03:10 +000085 -u|--include-untracked)
86 shift
87 untracked=${1?"BUG: create_stash () -u requires an argument"}
88 ;;
Thomas Gummererdf6bba02017-02-28 20:33:38 +000089 --)
90 shift
91 break
92 ;;
Thomas Gummerer9ca63262017-02-19 11:03:10 +000093 esac
94 shift
95 done
Junio C Hamano9f62e182007-07-04 22:46:09 -070096
Junio C Hamano1eff26c2008-09-04 02:41:22 -070097 git update-index -q --refresh
Thomas Gummererdf6bba02017-02-28 20:33:38 +000098 if no_changes "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +090099 then
しらいしななこf2c66ed2007-06-30 14:37:09 +0900100 exit 0
101 fi
しらいしななこf12e9252007-07-28 10:44:48 +0900102
しらいしななこf2c66ed2007-06-30 14:37:09 +0900103 # state of the base commit
Junio C Hamano5be60072007-07-02 22:52:14 -0700104 if b_commit=$(git rev-parse --verify HEAD)
しらいしななこf2c66ed2007-06-30 14:37:09 +0900105 then
Jeff Kingb0e621a2010-04-08 15:42:37 -0400106 head=$(git rev-list --oneline -n 1 HEAD --)
しらいしななこf2c66ed2007-06-30 14:37:09 +0900107 else
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000108 die "$(gettext "You do not have the initial commit yet")"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900109 fi
110
Junio C Hamano5be60072007-07-02 22:52:14 -0700111 if branch=$(git symbolic-ref -q HEAD)
しらいしななこf2c66ed2007-06-30 14:37:09 +0900112 then
113 branch=${branch#refs/heads/}
114 else
115 branch='(no branch)'
116 fi
117 msg=$(printf '%s: %s' "$branch" "$head")
118
119 # state of the index
Junio C Hamano5be60072007-07-02 22:52:14 -0700120 i_tree=$(git write-tree) &&
Jean-Luc Herren6143fa22007-09-12 20:45:03 +0200121 i_commit=$(printf 'index on %s\n' "$msg" |
Junio C Hamano5be60072007-07-02 22:52:14 -0700122 git commit-tree $i_tree -p $b_commit) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000123 die "$(gettext "Cannot save the current index state")"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900124
David Caldwell78751302011-06-24 17:56:06 -0700125 if test -n "$untracked"
126 then
127 # Untracked files are stored by themselves in a parentless commit, for
128 # ease of unpacking later.
129 u_commit=$(
Kevin Daudt5fc92f82017-08-14 23:43:33 +0200130 untracked_files -z "$@" | (
Elia Pintobed137d2014-05-23 03:15:31 -0700131 GIT_INDEX_FILE="$TMPindex" &&
132 export GIT_INDEX_FILE &&
David Caldwell78751302011-06-24 17:56:06 -0700133 rm -f "$TMPindex" &&
134 git update-index -z --add --remove --stdin &&
135 u_tree=$(git write-tree) &&
136 printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree &&
137 rm -f "$TMPindex"
Vasco Almeida850251f2016-09-19 13:08:21 +0000138 ) ) || die "$(gettext "Cannot save the untracked files")"
David Caldwell78751302011-06-24 17:56:06 -0700139
140 untracked_commit_option="-p $u_commit";
141 else
142 untracked_commit_option=
143 fi
144
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200145 if test -z "$patch_mode"
146 then
147
148 # state of the working tree
149 w_tree=$( (
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100150 git read-tree --index-output="$TMPindex" -m $i_tree &&
151 GIT_INDEX_FILE="$TMPindex" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200152 export GIT_INDEX_FILE &&
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000153 git diff-index --name-only -z HEAD -- "$@" >"$TMP-stagenames" &&
Jonathon Mah44df2e22011-12-30 16:14:01 -0800154 git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200155 git write-tree &&
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100156 rm -f "$TMPindex"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200157 ) ) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000158 die "$(gettext "Cannot save the current worktree state")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200159
160 else
161
Junio C Hamanob24f56d2007-07-08 01:28:18 -0700162 rm -f "$TMP-index" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200163 GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
164
165 # find out what the user wants
166 GIT_INDEX_FILE="$TMP-index" \
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000167 git add--interactive --patch=stash -- "$@" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200168
169 # state of the working tree
170 w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000171 die "$(gettext "Cannot save the current worktree state")"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900172
Jonathon Mah44df2e22011-12-30 16:14:01 -0800173 git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200174 test -s "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000175 die "$(gettext "No changes selected")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200176
177 rm -f "$TMP-index" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000178 die "$(gettext "Cannot remove temporary index (can't happen)")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200179
180 fi
181
しらいしななこf2c66ed2007-06-30 14:37:09 +0900182 # create the stash
Junio C Hamano9f62e182007-07-04 22:46:09 -0700183 if test -z "$stash_msg"
184 then
185 stash_msg=$(printf 'WIP on %s' "$msg")
186 else
187 stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
188 fi
189 w_commit=$(printf '%s\n' "$stash_msg" |
Junio C Hamano22f41282011-07-22 14:46:28 -0700190 git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
191 die "$(gettext "Cannot record working tree state")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700192}
しらいしななこf2c66ed2007-06-30 14:37:09 +0900193
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530194store_stash () {
195 while test $# != 0
196 do
197 case "$1" in
198 -m|--message)
199 shift
200 stash_msg="$1"
201 ;;
Phil Hord56754732017-11-22 13:20:30 -0800202 -m*)
203 stash_msg=${1#-m}
204 ;;
205 --message=*)
206 stash_msg=${1#--message=}
207 ;;
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530208 -q|--quiet)
209 quiet=t
210 ;;
211 *)
212 break
213 ;;
214 esac
215 shift
216 done
217 test $# = 1 ||
218 die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
219
220 w_commit="$1"
221 if test -z "$stash_msg"
222 then
223 stash_msg="Created via \"git stash store\"."
224 fi
225
David Turner89dea972015-07-21 17:04:56 -0400226 git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530227 ret=$?
Jeff King268ef4d2016-05-13 16:47:33 -0400228 test $ret != 0 && test -z "$quiet" &&
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530229 die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
230 return $ret
231}
232
Thomas Gummererf5727e22017-02-19 11:03:08 +0000233push_stash () {
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200234 keep_index=
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200235 patch_mode=
David Caldwell78751302011-06-24 17:56:06 -0700236 untracked=
Thomas Gummererf5727e22017-02-19 11:03:08 +0000237 stash_msg=
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700238 while test $# != 0
239 do
240 case "$1" in
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200241 -k|--keep-index)
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700242 keep_index=t
243 ;;
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200244 --no-keep-index)
Dan McGee3a243f72011-04-07 12:04:20 -0500245 keep_index=n
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200246 ;;
247 -p|--patch)
248 patch_mode=t
Dan McGee3a243f72011-04-07 12:04:20 -0500249 # only default to keep if we don't already have an override
250 test -z "$keep_index" && keep_index=t
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700251 ;;
252 -q|--quiet)
253 GIT_QUIET=t
254 ;;
David Caldwell78751302011-06-24 17:56:06 -0700255 -u|--include-untracked)
256 untracked=untracked
257 ;;
258 -a|--all)
259 untracked=all
260 ;;
Thomas Gummererf5727e22017-02-19 11:03:08 +0000261 -m|--message)
262 shift
263 test -z ${1+x} && usage
264 stash_msg=$1
265 ;;
Phil Hord56754732017-11-22 13:20:30 -0800266 -m*)
267 stash_msg=${1#-m}
268 ;;
269 --message=*)
270 stash_msg=${1#--message=}
271 ;;
Jeff King5ba28312015-05-20 14:17:46 -0400272 --help)
273 show_help
274 ;;
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200275 --)
276 shift
277 break
278 ;;
279 -*)
Ævar Arnfjörð Bjarmasoneed10642011-05-21 18:44:17 +0000280 option="$1"
Thomas Gummererc0c0c822017-10-22 18:04:09 +0100281 eval_gettextln "error: unknown option for 'stash push': \$option"
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200282 usage
283 ;;
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700284 *)
285 break
286 ;;
287 esac
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200288 shift
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700289 done
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200290
Patrick Steinhardt22fc7032017-06-13 13:38:34 +0200291 eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
292
David Caldwell78751302011-06-24 17:56:06 -0700293 if test -n "$patch_mode" && test -n "$untracked"
294 then
Vasco Almeida850251f2016-09-19 13:08:21 +0000295 die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")"
David Caldwell78751302011-06-24 17:56:06 -0700296 fi
297
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000298 test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1
299
Junio C Hamano1eff26c2008-09-04 02:41:22 -0700300 git update-index -q --refresh
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000301 if no_changes "$@"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700302 then
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000303 say "$(gettext "No local changes to save")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700304 exit 0
305 fi
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000306
David Turner89dea972015-07-21 17:04:56 -0400307 git reflog exists $ref_stash ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000308 clear_stash || die "$(gettext "Cannot initialize stash")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700309
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000310 create_stash -m "$stash_msg" -u "$untracked" -- "$@"
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530311 store_stash -m "$stash_msg" -q $w_commit ||
312 die "$(gettext "Cannot save the current status")"
Vasco Almeida599e7a02016-08-10 10:50:30 +0000313 say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200314
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200315 if test -z "$patch_mode"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200316 then
Nicolas Morey-Chaisemartinbbffd872017-08-11 19:14:43 +0200317 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
318 if test -n "$untracked"
319 then
320 git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
321 fi
322
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000323 if test $# != 0
324 then
Thomas Gummererbba067d2018-01-06 00:24:20 +0000325 git add -u -- "$@" |
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000326 git checkout-index -z --force --stdin
Thomas Gummererbba067d2018-01-06 00:24:20 +0000327 git diff-index -p --cached --binary HEAD -- "$@" | git apply --index -R
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000328 else
Thomas Gummerer1790f4f2017-03-21 22:12:17 +0000329 git reset --hard -q
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000330 fi
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200331
Jeff King268ef4d2016-05-13 16:47:33 -0400332 if test "$keep_index" = "t" && test -n "$i_tree"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200333 then
Thomas Gummerere0e7f992017-03-21 22:12:19 +0000334 git read-tree --reset $i_tree
335 git ls-files -z --modified -- "$@" |
336 git checkout-index -z --force --stdin
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200337 fi
338 else
339 git apply -R < "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000340 die "$(gettext "Cannot remove worktree changes")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200341
Dan McGee3a243f72011-04-07 12:04:20 -0500342 if test "$keep_index" != "t"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200343 then
Thomas Gummerer869fb8f2017-03-21 22:12:18 +0000344 git reset -q -- "$@"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200345 fi
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200346 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +0900347}
348
Thomas Gummererf5727e22017-02-19 11:03:08 +0000349save_stash () {
350 push_options=
351 while test $# != 0
352 do
353 case "$1" in
354 --)
355 shift
356 break
357 ;;
358 -*)
359 # pass all options through to push_stash
360 push_options="$push_options $1"
361 ;;
362 *)
363 break
364 ;;
365 esac
366 shift
367 done
368
369 stash_msg="$*"
370
371 if test -z "$stash_msg"
372 then
373 push_stash $push_options
374 else
375 push_stash $push_options -m "$stash_msg"
376 fi
377}
378
Jeff King401de402007-07-02 00:21:24 -0400379have_stash () {
David Aguilar1956dfa2014-09-15 20:24:10 -0700380 git rev-parse --verify --quiet $ref_stash >/dev/null
Jeff King401de402007-07-02 00:21:24 -0400381}
382
しらいしななこf2c66ed2007-06-30 14:37:09 +0900383list_stash () {
Jeff King401de402007-07-02 00:21:24 -0400384 have_stash || return 0
Jeff King288c67c2014-08-06 14:35:25 -0400385 git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
しらいしななこf2c66ed2007-06-30 14:37:09 +0900386}
387
388show_stash () {
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400389 ALLOW_UNKNOWN_FLAGS=t
Jon Seymoura9bf09e2010-08-21 14:09:02 +1000390 assert_stash_like "$@"
Gustaf Hendeby14cd4582010-03-16 18:52:37 +0100391
Namhyung Kim3086c062015-08-30 00:25:57 +0900392 if test -z "$FLAGS"
393 then
394 if test "$(git config --bool stash.showStat || echo true)" = "true"
395 then
396 FLAGS=--stat
397 fi
398
399 if test "$(git config --bool stash.showPatch || echo false)" = "true"
400 then
401 FLAGS=${FLAGS}${FLAGS:+ }-p
402 fi
403
404 if test -z "$FLAGS"
405 then
406 return 0
407 fi
408 fi
409
410 git diff ${FLAGS} $b_commit $w_commit
しらいしななこf2c66ed2007-06-30 14:37:09 +0900411}
412
Jeff King5ba28312015-05-20 14:17:46 -0400413show_help () {
414 exec git help stash
415 exit 1
416}
417
Jon Seymouref763122010-08-21 14:46:22 +1000418#
419# Parses the remaining options looking for flags and
420# at most one revision defaulting to ${ref_stash}@{0}
421# if none found.
422#
423# Derives related tree and commit objects from the
424# revision, if one is found.
425#
426# stash records the work tree, and is a merge between the
427# base commit (first parent) and the index tree (second parent).
428#
429# REV is set to the symbolic version of the specified stash-like commit
430# IS_STASH_LIKE is non-blank if ${REV} looks like a stash
431# IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
432# s is set to the SHA1 of the stash commit
433# w_commit is set to the commit containing the working tree
434# b_commit is set to the base commit
435# i_commit is set to the commit containing the index tree
David Caldwell78751302011-06-24 17:56:06 -0700436# u_commit is set to the commit containing the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000437# w_tree is set to the working tree
438# b_tree is set to the base tree
439# i_tree is set to the index tree
David Caldwell78751302011-06-24 17:56:06 -0700440# u_tree is set to the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000441#
442# GIT_QUIET is set to t if -q is specified
443# INDEX_OPTION is set to --index if --index is specified.
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400444# FLAGS is set to the remaining flags (if allowed)
Jon Seymouref763122010-08-21 14:46:22 +1000445#
446# dies if:
447# * too many revisions specified
448# * no revision is specified and there is no stash stack
449# * a revision is specified which cannot be resolve to a SHA1
450# * a non-existent stash reference is specified
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400451# * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
Jon Seymouref763122010-08-21 14:46:22 +1000452#
453
454parse_flags_and_rev()
455{
456 test "$PARSE_CACHE" = "$*" && return 0 # optimisation
457 PARSE_CACHE="$*"
458
459 IS_STASH_LIKE=
460 IS_STASH_REF=
461 INDEX_OPTION=
462 s=
463 w_commit=
464 b_commit=
465 i_commit=
David Caldwell78751302011-06-24 17:56:06 -0700466 u_commit=
Jon Seymouref763122010-08-21 14:46:22 +1000467 w_tree=
468 b_tree=
469 i_tree=
David Caldwell78751302011-06-24 17:56:06 -0700470 u_tree=
Jon Seymouref763122010-08-21 14:46:22 +1000471
Jon Seymour2bea5932010-09-28 01:32:45 +1000472 FLAGS=
Aaron M Watsona56c8f52016-10-24 19:40:13 -0400473 REV=
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400474 for opt
475 do
476 case "$opt" in
Jon Seymour2bea5932010-09-28 01:32:45 +1000477 -q|--quiet)
478 GIT_QUIET=-t
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400479 ;;
Jon Seymouref763122010-08-21 14:46:22 +1000480 --index)
481 INDEX_OPTION=--index
482 ;;
Jeff King5ba28312015-05-20 14:17:46 -0400483 --help)
484 show_help
485 ;;
Jon Seymour2bea5932010-09-28 01:32:45 +1000486 -*)
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400487 test "$ALLOW_UNKNOWN_FLAGS" = t ||
488 die "$(eval_gettext "unknown option: \$opt")"
Jon Seymour2bea5932010-09-28 01:32:45 +1000489 FLAGS="${FLAGS}${FLAGS:+ }$opt"
Jon Seymouref763122010-08-21 14:46:22 +1000490 ;;
Aaron M Watsona56c8f52016-10-24 19:40:13 -0400491 *)
492 REV="${REV}${REV:+ }'$opt'"
493 ;;
Jon Seymouref763122010-08-21 14:46:22 +1000494 esac
Jon Seymouref763122010-08-21 14:46:22 +1000495 done
496
Øystein Walle2a07e432014-01-07 09:22:15 +0100497 eval set -- $REV
Jon Seymouref763122010-08-21 14:46:22 +1000498
499 case $# in
500 0)
Liam Beguine01db912017-06-17 18:30:50 -0400501 have_stash || die "$(gettext "No stash entries found.")"
Jon Seymouref763122010-08-21 14:46:22 +1000502 set -- ${ref_stash}@{0}
503 ;;
504 1)
505 :
506 ;;
507 *)
Ævar Arnfjörð Bjarmason33ceddc2011-05-21 18:44:14 +0000508 die "$(eval_gettext "Too many revisions specified: \$REV")"
Jon Seymouref763122010-08-21 14:46:22 +1000509 ;;
510 esac
511
Aaron M Watsona56c8f52016-10-24 19:40:13 -0400512 case "$1" in
513 *[!0-9]*)
514 :
515 ;;
516 *)
517 set -- "${ref_stash}@{$1}"
518 ;;
519 esac
520
David Aguilar1956dfa2014-09-15 20:24:10 -0700521 REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16 +0000522 reference="$1"
Alex Henriead5fe372014-08-30 13:56:01 -0600523 die "$(eval_gettext "\$reference is not a valid reference")"
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16 +0000524 }
Jon Seymouref763122010-08-21 14:46:22 +1000525
David Aguilar1956dfa2014-09-15 20:24:10 -0700526 i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
Øystein Walle2a07e432014-01-07 09:22:15 +0100527 set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
Jon Seymouref763122010-08-21 14:46:22 +1000528 s=$1 &&
529 w_commit=$1 &&
530 b_commit=$2 &&
531 w_tree=$3 &&
532 b_tree=$4 &&
533 i_tree=$5 &&
534 IS_STASH_LIKE=t &&
535 test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
536 IS_STASH_REF=t
David Caldwell78751302011-06-24 17:56:06 -0700537
David Aguilar1956dfa2014-09-15 20:24:10 -0700538 u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
Øystein Walle2a07e432014-01-07 09:22:15 +0100539 u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
Jon Seymouref763122010-08-21 14:46:22 +1000540}
541
542is_stash_like()
543{
544 parse_flags_and_rev "$@"
545 test -n "$IS_STASH_LIKE"
546}
547
548assert_stash_like() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000549 is_stash_like "$@" || {
550 args="$*"
551 die "$(eval_gettext "'\$args' is not a stash-like commit")"
552 }
Jon Seymouref763122010-08-21 14:46:22 +1000553}
554
555is_stash_ref() {
556 is_stash_like "$@" && test -n "$IS_STASH_REF"
557}
558
559assert_stash_ref() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000560 is_stash_ref "$@" || {
561 args="$*"
562 die "$(eval_gettext "'\$args' is not a stash reference")"
563 }
Jon Seymouref763122010-08-21 14:46:22 +1000564}
565
しらいしななこf2c66ed2007-06-30 14:37:09 +0900566apply_stash () {
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700567
Jon Seymour064ed102010-08-21 14:08:58 +1000568 assert_stash_like "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900569
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000570 git update-index -q --refresh || die "$(gettext "unable to refresh index")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300571
572 # current index state
573 c_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000574 die "$(gettext "Cannot apply a stash in the middle of a merge")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300575
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700576 unstashed_index_tree=
Jon Seymour064ed102010-08-21 14:08:58 +1000577 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200578 test "$c_tree" != "$i_tree"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700579 then
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400580 git diff-tree --binary $s^2^..$s^2 | git apply --cached
Johannes Schindelin150937c2007-07-02 12:14:49 +0100581 test $? -ne 0 &&
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000582 die "$(gettext "Conflicts in index. Try without --index.")"
Martin Koegler52070792009-07-22 07:30:58 +0200583 unstashed_index_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000584 die "$(gettext "Could not save index tree")"
Johannes Schindelin150937c2007-07-02 12:14:49 +0100585 git reset
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700586 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100587
David Caldwell78751302011-06-24 17:56:06 -0700588 if test -n "$u_tree"
589 then
Michael Forney974ce802017-08-04 23:49:05 -0700590 GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" &&
David Caldwell78751302011-06-24 17:56:06 -0700591 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
592 rm -f "$TMPindex" ||
Liam Beguine01db912017-06-17 18:30:50 -0400593 die "$(gettext "Could not restore untracked files from stash entry")"
David Caldwell78751302011-06-24 17:56:06 -0700594 fi
595
しらいしななこf2c66ed2007-06-30 14:37:09 +0900596 eval "
597 GITHEAD_$w_tree='Stashed changes' &&
598 GITHEAD_$c_tree='Updated upstream' &&
599 GITHEAD_$b_tree='Version stash was based on' &&
600 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
601 "
602
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700603 if test -n "$GIT_QUIET"
604 then
Junio C Hamano69ae92b2010-10-13 11:36:36 -0700605 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700606 fi
Martin Koegler52070792009-07-22 07:30:58 +0200607 if git merge-recursive $b_tree -- $c_tree $w_tree
しらいしななこf2c66ed2007-06-30 14:37:09 +0900608 then
609 # No conflict
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700610 if test -n "$unstashed_index_tree"
611 then
612 git read-tree "$unstashed_index_tree"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700613 else
614 a="$TMP-added" &&
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400615 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
Junio C Hamano83b3df72007-07-27 23:51:45 -0700616 git read-tree --reset $c_tree &&
617 git update-index --add --stdin <"$a" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000618 die "$(gettext "Cannot unstage modified files")"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700619 rm -f "$a"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700620 fi
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700621 squelch=
622 if test -n "$GIT_QUIET"
623 then
624 squelch='>/dev/null 2>&1'
625 fi
Piotr Krukowiecki26b59b42011-03-12 09:57:46 +0100626 (cd "$START_DIR" && eval "git status $squelch") || :
しらいしななこf2c66ed2007-06-30 14:37:09 +0900627 else
628 # Merge conflict; keep the exit status from merge-recursive
Johannes Schindelin150937c2007-07-02 12:14:49 +0100629 status=$?
Phil Hord743bf6d2012-07-10 18:52:28 -0400630 git rerere
Jon Seymour064ed102010-08-21 14:08:58 +1000631 if test -n "$INDEX_OPTION"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700632 then
Jon Seymour6fdd50e2011-08-07 21:58:16 +1000633 gettextln "Index was not unstashed." >&2
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700634 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100635 exit $status
しらいしななこf2c66ed2007-06-30 14:37:09 +0900636 fi
637}
638
Jon Seymourf2768722010-08-21 14:09:00 +1000639pop_stash() {
640 assert_stash_ref "$@"
641
Junio C Hamano2d4c9932014-02-26 14:18:54 -0800642 if apply_stash "$@"
643 then
644 drop_stash "$@"
645 else
646 status=$?
Liam Beguine01db912017-06-17 18:30:50 -0400647 say "$(gettext "The stash entry is kept in case you need it again.")"
Junio C Hamano2d4c9932014-02-26 14:18:54 -0800648 exit $status
649 fi
Jon Seymourf2768722010-08-21 14:09:00 +1000650}
651
Brandon Caseye25d5f92008-02-22 13:04:54 -0600652drop_stash () {
Jon Seymour92e39e42010-08-21 14:08:59 +1000653 assert_stash_ref "$@"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600654
Jon Seymour92e39e42010-08-21 14:08:59 +1000655 git reflog delete --updateref --rewrite "${REV}" &&
Ævar Arnfjörð Bjarmasond4ca6c82011-05-21 18:44:18 +0000656 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
657 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600658
659 # clear_stash if we just dropped the last stash entry
David Aguilar1956dfa2014-09-15 20:24:10 -0700660 git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
661 clear_stash
Brandon Caseye25d5f92008-02-22 13:04:54 -0600662}
663
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530664apply_to_branch () {
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000665 test -n "$1" || die "$(gettext "No branch name specified")"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530666 branch=$1
Jon Seymourfb433dc2010-08-21 14:09:01 +1000667 shift 1
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530668
Jon Seymourfb433dc2010-08-21 14:09:01 +1000669 set -- --index "$@"
670 assert_stash_like "$@"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530671
Jon Seymourfb433dc2010-08-21 14:09:01 +1000672 git checkout -b $branch $REV^ &&
Jon Seymour57693d02010-09-28 23:19:52 +1000673 apply_stash "$@" && {
674 test -z "$IS_STASH_REF" || drop_stash "$@"
675 }
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530676}
677
Thomas Gummerer9e140902017-02-28 20:33:40 +0000678test "$1" = "-p" && set "push" "$@"
679
Jon Seymouref763122010-08-21 14:46:22 +1000680PARSE_CACHE='--not-parsed'
Thomas Gummerer1ada5022017-02-28 20:33:39 +0000681# The default command is "push" if nothing but options are given
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200682seen_non_option=
683for opt
684do
685 case "$opt" in
Thomas Gummerer9e140902017-02-28 20:33:40 +0000686 --) break ;;
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200687 -*) ;;
688 *) seen_non_option=t; break ;;
689 esac
690done
691
Thomas Gummerer1ada5022017-02-28 20:33:39 +0000692test -n "$seen_non_option" || set "push" "$@"
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200693
しらいしななこf2c66ed2007-06-30 14:37:09 +0900694# Main command set
695case "$1" in
Junio C Hamanofcb10a92007-07-02 23:15:45 -0700696list)
697 shift
しらいしななこf2c66ed2007-06-30 14:37:09 +0900698 list_stash "$@"
699 ;;
700show)
701 shift
702 show_stash "$@"
703 ;;
Kevin Leung683befa2007-12-03 10:34:05 +0800704save)
705 shift
Junio C Hamano47629dc2008-07-23 13:33:44 -0700706 save_stash "$@"
Kevin Leung683befa2007-12-03 10:34:05 +0800707 ;;
Thomas Gummererf5727e22017-02-19 11:03:08 +0000708push)
709 shift
710 push_stash "$@"
711 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900712apply)
713 shift
714 apply_stash "$@"
715 ;;
716clear)
Junio C Hamano3023dc62008-01-05 01:35:54 -0800717 shift
718 clear_stash "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900719 ;;
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700720create)
Ramkumar Ramachandra0719f302013-06-15 18:43:24 +0530721 shift
Thomas Gummerer9ca63262017-02-19 11:03:10 +0000722 create_stash -m "$*" && echo "$w_commit"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700723 ;;
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530724store)
725 shift
726 store_stash "$@"
727 ;;
Brandon Caseye25d5f92008-02-22 13:04:54 -0600728drop)
729 shift
730 drop_stash "$@"
731 ;;
Brandon Caseybd56ff52008-02-22 16:52:50 -0600732pop)
733 shift
Jon Seymourf2768722010-08-21 14:09:00 +1000734 pop_stash "$@"
Brandon Caseybd56ff52008-02-22 16:52:50 -0600735 ;;
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530736branch)
737 shift
738 apply_to_branch "$@"
739 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900740*)
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200741 case $# in
742 0)
Thomas Gummerer1ada5022017-02-28 20:33:39 +0000743 push_stash &&
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000744 say "$(gettext "(To restore them type \"git stash apply\")")"
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200745 ;;
746 *)
Kevin Leung683befa2007-12-03 10:34:05 +0800747 usage
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200748 esac
Junio C Hamano9f62e182007-07-04 22:46:09 -0700749 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900750esac