blob: 2fb651b2b8d9d91a130b1cbd11c3c2b6b1cf961b [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
Junio C Hamanoceff0792007-07-25 15:32:22 -070022cd_to_toplevel
しらいしななこf2c66ed2007-06-30 14:37:09 +090023
24TMP="$GIT_DIR/.git-stash.$$"
Nguyễn Thái Ngọc Duyc697b572014-11-30 15:24:32 +070025TMPindex=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$
Johannes Sixt3ba2e862011-03-16 09:18:49 +010026trap 'rm -f "$TMP-"* "$TMPindex"' 0
しらいしななこf2c66ed2007-06-30 14:37:09 +090027
28ref_stash=refs/stash
29
Thomas Rastdda1f2a2009-08-13 14:29:44 +020030if git config --get-colorbool color.interactive; then
31 help_color="$(git config --get-color color.interactive.help 'red bold')"
32 reset_color="$(git config --get-color '' reset)"
33else
34 help_color=
35 reset_color=
36fi
37
しらいしななこf2c66ed2007-06-30 14:37:09 +090038no_changes () {
Thomas Gummererdf6bba02017-02-28 20:33:38 +000039 git diff-index --quiet --cached HEAD --ignore-submodules -- "$@" &&
40 git diff-files --quiet --ignore-submodules -- "$@" &&
David Caldwell78751302011-06-24 17:56:06 -070041 (test -z "$untracked" || test -z "$(untracked_files)")
42}
43
44untracked_files () {
45 excl_opt=--exclude-standard
46 test "$untracked" = "all" && excl_opt=
Thomas Gummererdf6bba02017-02-28 20:33:38 +000047 git ls-files -o -z $excl_opt -- "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +090048}
49
50clear_stash () {
Junio C Hamano3023dc62008-01-05 01:35:54 -080051 if test $# != 0
52 then
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +000053 die "$(gettext "git stash clear with parameters is unimplemented")"
Junio C Hamano3023dc62008-01-05 01:35:54 -080054 fi
David Aguilar1956dfa2014-09-15 20:24:10 -070055 if current=$(git rev-parse --verify --quiet $ref_stash)
Junio C Hamano7ab3cc72007-07-26 23:24:28 -070056 then
Emil Medvea9ee9bf2007-11-07 15:10:27 -060057 git update-ref -d $ref_stash $current
Junio C Hamano7ab3cc72007-07-26 23:24:28 -070058 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +090059}
60
Junio C Hamanobc9e7392007-07-08 01:38:32 -070061create_stash () {
Thomas Gummerer9ca63262017-02-19 11:03:10 +000062 stash_msg=
63 untracked=
64 while test $# != 0
65 do
66 case "$1" in
67 -m|--message)
68 shift
69 stash_msg=${1?"BUG: create_stash () -m requires an argument"}
70 ;;
71 -u|--include-untracked)
72 shift
73 untracked=${1?"BUG: create_stash () -u requires an argument"}
74 ;;
Thomas Gummererdf6bba02017-02-28 20:33:38 +000075 --)
76 shift
77 break
78 ;;
Thomas Gummerer9ca63262017-02-19 11:03:10 +000079 esac
80 shift
81 done
Junio C Hamano9f62e182007-07-04 22:46:09 -070082
Junio C Hamano1eff26c2008-09-04 02:41:22 -070083 git update-index -q --refresh
Thomas Gummererdf6bba02017-02-28 20:33:38 +000084 if no_changes "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +090085 then
しらいしななこf2c66ed2007-06-30 14:37:09 +090086 exit 0
87 fi
しらいしななこf12e9252007-07-28 10:44:48 +090088
しらいしななこf2c66ed2007-06-30 14:37:09 +090089 # state of the base commit
Junio C Hamano5be60072007-07-02 22:52:14 -070090 if b_commit=$(git rev-parse --verify HEAD)
しらいしななこf2c66ed2007-06-30 14:37:09 +090091 then
Jeff Kingb0e621a2010-04-08 15:42:37 -040092 head=$(git rev-list --oneline -n 1 HEAD --)
しらいしななこf2c66ed2007-06-30 14:37:09 +090093 else
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +000094 die "$(gettext "You do not have the initial commit yet")"
しらいしななこf2c66ed2007-06-30 14:37:09 +090095 fi
96
Junio C Hamano5be60072007-07-02 22:52:14 -070097 if branch=$(git symbolic-ref -q HEAD)
しらいしななこf2c66ed2007-06-30 14:37:09 +090098 then
99 branch=${branch#refs/heads/}
100 else
101 branch='(no branch)'
102 fi
103 msg=$(printf '%s: %s' "$branch" "$head")
104
105 # state of the index
Junio C Hamano5be60072007-07-02 22:52:14 -0700106 i_tree=$(git write-tree) &&
Jean-Luc Herren6143fa22007-09-12 20:45:03 +0200107 i_commit=$(printf 'index on %s\n' "$msg" |
Junio C Hamano5be60072007-07-02 22:52:14 -0700108 git commit-tree $i_tree -p $b_commit) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000109 die "$(gettext "Cannot save the current index state")"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900110
David Caldwell78751302011-06-24 17:56:06 -0700111 if test -n "$untracked"
112 then
113 # Untracked files are stored by themselves in a parentless commit, for
114 # ease of unpacking later.
115 u_commit=$(
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000116 untracked_files "$@" | (
Elia Pintobed137d2014-05-23 03:15:31 -0700117 GIT_INDEX_FILE="$TMPindex" &&
118 export GIT_INDEX_FILE &&
David Caldwell78751302011-06-24 17:56:06 -0700119 rm -f "$TMPindex" &&
120 git update-index -z --add --remove --stdin &&
121 u_tree=$(git write-tree) &&
122 printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree &&
123 rm -f "$TMPindex"
Vasco Almeida850251f2016-09-19 13:08:21 +0000124 ) ) || die "$(gettext "Cannot save the untracked files")"
David Caldwell78751302011-06-24 17:56:06 -0700125
126 untracked_commit_option="-p $u_commit";
127 else
128 untracked_commit_option=
129 fi
130
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200131 if test -z "$patch_mode"
132 then
133
134 # state of the working tree
135 w_tree=$( (
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100136 git read-tree --index-output="$TMPindex" -m $i_tree &&
137 GIT_INDEX_FILE="$TMPindex" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200138 export GIT_INDEX_FILE &&
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000139 git diff-index --name-only -z HEAD -- "$@" >"$TMP-stagenames" &&
Jonathon Mah44df2e22011-12-30 16:14:01 -0800140 git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200141 git write-tree &&
Johannes Sixt3ba2e862011-03-16 09:18:49 +0100142 rm -f "$TMPindex"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200143 ) ) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000144 die "$(gettext "Cannot save the current worktree state")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200145
146 else
147
Junio C Hamanob24f56d2007-07-08 01:28:18 -0700148 rm -f "$TMP-index" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200149 GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
150
151 # find out what the user wants
152 GIT_INDEX_FILE="$TMP-index" \
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000153 git add--interactive --patch=stash -- "$@" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200154
155 # state of the working tree
156 w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000157 die "$(gettext "Cannot save the current worktree state")"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900158
Jonathon Mah44df2e22011-12-30 16:14:01 -0800159 git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200160 test -s "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000161 die "$(gettext "No changes selected")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200162
163 rm -f "$TMP-index" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000164 die "$(gettext "Cannot remove temporary index (can't happen)")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200165
166 fi
167
しらいしななこf2c66ed2007-06-30 14:37:09 +0900168 # create the stash
Junio C Hamano9f62e182007-07-04 22:46:09 -0700169 if test -z "$stash_msg"
170 then
171 stash_msg=$(printf 'WIP on %s' "$msg")
172 else
173 stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
174 fi
175 w_commit=$(printf '%s\n' "$stash_msg" |
Junio C Hamano22f41282011-07-22 14:46:28 -0700176 git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
177 die "$(gettext "Cannot record working tree state")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700178}
しらいしななこf2c66ed2007-06-30 14:37:09 +0900179
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530180store_stash () {
181 while test $# != 0
182 do
183 case "$1" in
184 -m|--message)
185 shift
186 stash_msg="$1"
187 ;;
188 -q|--quiet)
189 quiet=t
190 ;;
191 *)
192 break
193 ;;
194 esac
195 shift
196 done
197 test $# = 1 ||
198 die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
199
200 w_commit="$1"
201 if test -z "$stash_msg"
202 then
203 stash_msg="Created via \"git stash store\"."
204 fi
205
David Turner89dea972015-07-21 17:04:56 -0400206 git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530207 ret=$?
Jeff King268ef4d2016-05-13 16:47:33 -0400208 test $ret != 0 && test -z "$quiet" &&
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530209 die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
210 return $ret
211}
212
Thomas Gummererf5727e22017-02-19 11:03:08 +0000213push_stash () {
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200214 keep_index=
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200215 patch_mode=
David Caldwell78751302011-06-24 17:56:06 -0700216 untracked=
Thomas Gummererf5727e22017-02-19 11:03:08 +0000217 stash_msg=
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700218 while test $# != 0
219 do
220 case "$1" in
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200221 -k|--keep-index)
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700222 keep_index=t
223 ;;
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200224 --no-keep-index)
Dan McGee3a243f72011-04-07 12:04:20 -0500225 keep_index=n
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200226 ;;
227 -p|--patch)
228 patch_mode=t
Dan McGee3a243f72011-04-07 12:04:20 -0500229 # only default to keep if we don't already have an override
230 test -z "$keep_index" && keep_index=t
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700231 ;;
232 -q|--quiet)
233 GIT_QUIET=t
234 ;;
David Caldwell78751302011-06-24 17:56:06 -0700235 -u|--include-untracked)
236 untracked=untracked
237 ;;
238 -a|--all)
239 untracked=all
240 ;;
Thomas Gummererf5727e22017-02-19 11:03:08 +0000241 -m|--message)
242 shift
243 test -z ${1+x} && usage
244 stash_msg=$1
245 ;;
Jeff King5ba28312015-05-20 14:17:46 -0400246 --help)
247 show_help
248 ;;
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200249 --)
250 shift
251 break
252 ;;
253 -*)
Ævar Arnfjörð Bjarmasoneed10642011-05-21 18:44:17 +0000254 option="$1"
255 # TRANSLATORS: $option is an invalid option, like
256 # `--blah-blah'. The 7 spaces at the beginning of the
257 # second line correspond to "error: ". So you should line
258 # up the second line with however many characters the
259 # translation of "error: " takes in your language. E.g. in
260 # English this is:
261 #
262 # $ git stash save --blah-blah 2>&1 | head -n 2
263 # error: unknown option for 'stash save': --blah-blah
264 # To provide a message, use git stash save -- '--blah-blah'
Ross Lagerwalled3c4002012-04-14 14:37:29 +0200265 eval_gettextln "error: unknown option for 'stash save': \$option
266 To provide a message, use git stash save -- '\$option'"
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200267 usage
268 ;;
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700269 *)
270 break
271 ;;
272 esac
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200273 shift
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700274 done
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200275
David Caldwell78751302011-06-24 17:56:06 -0700276 if test -n "$patch_mode" && test -n "$untracked"
277 then
Vasco Almeida850251f2016-09-19 13:08:21 +0000278 die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")"
David Caldwell78751302011-06-24 17:56:06 -0700279 fi
280
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000281 test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1
282
Junio C Hamano1eff26c2008-09-04 02:41:22 -0700283 git update-index -q --refresh
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000284 if no_changes "$@"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700285 then
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000286 say "$(gettext "No local changes to save")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700287 exit 0
288 fi
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000289
David Turner89dea972015-07-21 17:04:56 -0400290 git reflog exists $ref_stash ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000291 clear_stash || die "$(gettext "Cannot initialize stash")"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700292
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000293 create_stash -m "$stash_msg" -u "$untracked" -- "$@"
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530294 store_stash -m "$stash_msg" -q $w_commit ||
295 die "$(gettext "Cannot save the current status")"
Vasco Almeida599e7a02016-08-10 10:50:30 +0000296 say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200297
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200298 if test -z "$patch_mode"
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200299 then
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000300 if test $# != 0
301 then
Thomas Gummerer1790f4f2017-03-21 22:12:17 +0000302 git reset -q -- "$@"
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000303 git ls-files -z --modified -- "$@" |
304 git checkout-index -z --force --stdin
Thomas Gummerer1790f4f2017-03-21 22:12:17 +0000305 git clean --force -q -d -- "$@"
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000306 else
Thomas Gummerer1790f4f2017-03-21 22:12:17 +0000307 git reset --hard -q
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000308 fi
David Caldwell78751302011-06-24 17:56:06 -0700309 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
310 if test -n "$untracked"
311 then
Thomas Gummererdf6bba02017-02-28 20:33:38 +0000312 git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
David Caldwell78751302011-06-24 17:56:06 -0700313 fi
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200314
Jeff King268ef4d2016-05-13 16:47:33 -0400315 if test "$keep_index" = "t" && test -n "$i_tree"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200316 then
Thomas Gummerere0e7f992017-03-21 22:12:19 +0000317 git read-tree --reset $i_tree
318 git ls-files -z --modified -- "$@" |
319 git checkout-index -z --force --stdin
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200320 fi
321 else
322 git apply -R < "$TMP-patch" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000323 die "$(gettext "Cannot remove worktree changes")"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200324
Dan McGee3a243f72011-04-07 12:04:20 -0500325 if test "$keep_index" != "t"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200326 then
Thomas Gummerer869fb8f2017-03-21 22:12:18 +0000327 git reset -q -- "$@"
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200328 fi
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200329 fi
しらいしななこf2c66ed2007-06-30 14:37:09 +0900330}
331
Thomas Gummererf5727e22017-02-19 11:03:08 +0000332save_stash () {
333 push_options=
334 while test $# != 0
335 do
336 case "$1" in
337 --)
338 shift
339 break
340 ;;
341 -*)
342 # pass all options through to push_stash
343 push_options="$push_options $1"
344 ;;
345 *)
346 break
347 ;;
348 esac
349 shift
350 done
351
352 stash_msg="$*"
353
354 if test -z "$stash_msg"
355 then
356 push_stash $push_options
357 else
358 push_stash $push_options -m "$stash_msg"
359 fi
360}
361
Jeff King401de402007-07-02 00:21:24 -0400362have_stash () {
David Aguilar1956dfa2014-09-15 20:24:10 -0700363 git rev-parse --verify --quiet $ref_stash >/dev/null
Jeff King401de402007-07-02 00:21:24 -0400364}
365
しらいしななこf2c66ed2007-06-30 14:37:09 +0900366list_stash () {
Jeff King401de402007-07-02 00:21:24 -0400367 have_stash || return 0
Jeff King288c67c2014-08-06 14:35:25 -0400368 git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
しらいしななこf2c66ed2007-06-30 14:37:09 +0900369}
370
371show_stash () {
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400372 ALLOW_UNKNOWN_FLAGS=t
Jon Seymoura9bf09e2010-08-21 14:09:02 +1000373 assert_stash_like "$@"
Gustaf Hendeby14cd4582010-03-16 18:52:37 +0100374
Namhyung Kim3086c062015-08-30 00:25:57 +0900375 if test -z "$FLAGS"
376 then
377 if test "$(git config --bool stash.showStat || echo true)" = "true"
378 then
379 FLAGS=--stat
380 fi
381
382 if test "$(git config --bool stash.showPatch || echo false)" = "true"
383 then
384 FLAGS=${FLAGS}${FLAGS:+ }-p
385 fi
386
387 if test -z "$FLAGS"
388 then
389 return 0
390 fi
391 fi
392
393 git diff ${FLAGS} $b_commit $w_commit
しらいしななこf2c66ed2007-06-30 14:37:09 +0900394}
395
Jeff King5ba28312015-05-20 14:17:46 -0400396show_help () {
397 exec git help stash
398 exit 1
399}
400
Jon Seymouref763122010-08-21 14:46:22 +1000401#
402# Parses the remaining options looking for flags and
403# at most one revision defaulting to ${ref_stash}@{0}
404# if none found.
405#
406# Derives related tree and commit objects from the
407# revision, if one is found.
408#
409# stash records the work tree, and is a merge between the
410# base commit (first parent) and the index tree (second parent).
411#
412# REV is set to the symbolic version of the specified stash-like commit
413# IS_STASH_LIKE is non-blank if ${REV} looks like a stash
414# IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
415# s is set to the SHA1 of the stash commit
416# w_commit is set to the commit containing the working tree
417# b_commit is set to the base commit
418# i_commit is set to the commit containing the index tree
David Caldwell78751302011-06-24 17:56:06 -0700419# u_commit is set to the commit containing the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000420# w_tree is set to the working tree
421# b_tree is set to the base tree
422# i_tree is set to the index tree
David Caldwell78751302011-06-24 17:56:06 -0700423# u_tree is set to the untracked files tree
Jon Seymouref763122010-08-21 14:46:22 +1000424#
425# GIT_QUIET is set to t if -q is specified
426# INDEX_OPTION is set to --index if --index is specified.
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400427# FLAGS is set to the remaining flags (if allowed)
Jon Seymouref763122010-08-21 14:46:22 +1000428#
429# dies if:
430# * too many revisions specified
431# * no revision is specified and there is no stash stack
432# * a revision is specified which cannot be resolve to a SHA1
433# * a non-existent stash reference is specified
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400434# * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
Jon Seymouref763122010-08-21 14:46:22 +1000435#
436
437parse_flags_and_rev()
438{
439 test "$PARSE_CACHE" = "$*" && return 0 # optimisation
440 PARSE_CACHE="$*"
441
442 IS_STASH_LIKE=
443 IS_STASH_REF=
444 INDEX_OPTION=
445 s=
446 w_commit=
447 b_commit=
448 i_commit=
David Caldwell78751302011-06-24 17:56:06 -0700449 u_commit=
Jon Seymouref763122010-08-21 14:46:22 +1000450 w_tree=
451 b_tree=
452 i_tree=
David Caldwell78751302011-06-24 17:56:06 -0700453 u_tree=
Jon Seymouref763122010-08-21 14:46:22 +1000454
Jon Seymour2bea5932010-09-28 01:32:45 +1000455 FLAGS=
Aaron M Watsona56c8f52016-10-24 19:40:13 -0400456 REV=
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400457 for opt
458 do
459 case "$opt" in
Jon Seymour2bea5932010-09-28 01:32:45 +1000460 -q|--quiet)
461 GIT_QUIET=-t
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400462 ;;
Jon Seymouref763122010-08-21 14:46:22 +1000463 --index)
464 INDEX_OPTION=--index
465 ;;
Jeff King5ba28312015-05-20 14:17:46 -0400466 --help)
467 show_help
468 ;;
Jon Seymour2bea5932010-09-28 01:32:45 +1000469 -*)
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400470 test "$ALLOW_UNKNOWN_FLAGS" = t ||
471 die "$(eval_gettext "unknown option: \$opt")"
Jon Seymour2bea5932010-09-28 01:32:45 +1000472 FLAGS="${FLAGS}${FLAGS:+ }$opt"
Jon Seymouref763122010-08-21 14:46:22 +1000473 ;;
Aaron M Watsona56c8f52016-10-24 19:40:13 -0400474 *)
475 REV="${REV}${REV:+ }'$opt'"
476 ;;
Jon Seymouref763122010-08-21 14:46:22 +1000477 esac
Jon Seymouref763122010-08-21 14:46:22 +1000478 done
479
Øystein Walle2a07e432014-01-07 09:22:15 +0100480 eval set -- $REV
Jon Seymouref763122010-08-21 14:46:22 +1000481
482 case $# in
483 0)
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000484 have_stash || die "$(gettext "No stash found.")"
Jon Seymouref763122010-08-21 14:46:22 +1000485 set -- ${ref_stash}@{0}
486 ;;
487 1)
488 :
489 ;;
490 *)
Ævar Arnfjörð Bjarmason33ceddc2011-05-21 18:44:14 +0000491 die "$(eval_gettext "Too many revisions specified: \$REV")"
Jon Seymouref763122010-08-21 14:46:22 +1000492 ;;
493 esac
494
Aaron M Watsona56c8f52016-10-24 19:40:13 -0400495 case "$1" in
496 *[!0-9]*)
497 :
498 ;;
499 *)
500 set -- "${ref_stash}@{$1}"
501 ;;
502 esac
503
David Aguilar1956dfa2014-09-15 20:24:10 -0700504 REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16 +0000505 reference="$1"
Alex Henriead5fe372014-08-30 13:56:01 -0600506 die "$(eval_gettext "\$reference is not a valid reference")"
Ævar Arnfjörð Bjarmason155da742011-05-21 18:44:16 +0000507 }
Jon Seymouref763122010-08-21 14:46:22 +1000508
David Aguilar1956dfa2014-09-15 20:24:10 -0700509 i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
Øystein Walle2a07e432014-01-07 09:22:15 +0100510 set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
Jon Seymouref763122010-08-21 14:46:22 +1000511 s=$1 &&
512 w_commit=$1 &&
513 b_commit=$2 &&
514 w_tree=$3 &&
515 b_tree=$4 &&
516 i_tree=$5 &&
517 IS_STASH_LIKE=t &&
518 test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
519 IS_STASH_REF=t
David Caldwell78751302011-06-24 17:56:06 -0700520
David Aguilar1956dfa2014-09-15 20:24:10 -0700521 u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
Øystein Walle2a07e432014-01-07 09:22:15 +0100522 u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
Jon Seymouref763122010-08-21 14:46:22 +1000523}
524
525is_stash_like()
526{
527 parse_flags_and_rev "$@"
528 test -n "$IS_STASH_LIKE"
529}
530
531assert_stash_like() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000532 is_stash_like "$@" || {
533 args="$*"
534 die "$(eval_gettext "'\$args' is not a stash-like commit")"
535 }
Jon Seymouref763122010-08-21 14:46:22 +1000536}
537
538is_stash_ref() {
539 is_stash_like "$@" && test -n "$IS_STASH_REF"
540}
541
542assert_stash_ref() {
Ævar Arnfjörð Bjarmason777b6f12011-05-21 18:44:15 +0000543 is_stash_ref "$@" || {
544 args="$*"
545 die "$(eval_gettext "'\$args' is not a stash reference")"
546 }
Jon Seymouref763122010-08-21 14:46:22 +1000547}
548
しらいしななこf2c66ed2007-06-30 14:37:09 +0900549apply_stash () {
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700550
Jon Seymour064ed102010-08-21 14:08:58 +1000551 assert_stash_like "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900552
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000553 git update-index -q --refresh || die "$(gettext "unable to refresh index")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300554
555 # current index state
556 c_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000557 die "$(gettext "Cannot apply a stash in the middle of a merge")"
Ori Avtalion5fd448f2009-08-11 14:12:13 +0300558
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700559 unstashed_index_tree=
Jon Seymour064ed102010-08-21 14:08:58 +1000560 if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200561 test "$c_tree" != "$i_tree"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700562 then
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400563 git diff-tree --binary $s^2^..$s^2 | git apply --cached
Johannes Schindelin150937c2007-07-02 12:14:49 +0100564 test $? -ne 0 &&
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000565 die "$(gettext "Conflicts in index. Try without --index.")"
Martin Koegler52070792009-07-22 07:30:58 +0200566 unstashed_index_tree=$(git write-tree) ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000567 die "$(gettext "Could not save index tree")"
Johannes Schindelin150937c2007-07-02 12:14:49 +0100568 git reset
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700569 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100570
David Caldwell78751302011-06-24 17:56:06 -0700571 if test -n "$u_tree"
572 then
573 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
574 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
575 rm -f "$TMPindex" ||
Vasco Almeida850251f2016-09-19 13:08:21 +0000576 die "$(gettext "Could not restore untracked files from stash")"
David Caldwell78751302011-06-24 17:56:06 -0700577 fi
578
しらいしななこf2c66ed2007-06-30 14:37:09 +0900579 eval "
580 GITHEAD_$w_tree='Stashed changes' &&
581 GITHEAD_$c_tree='Updated upstream' &&
582 GITHEAD_$b_tree='Version stash was based on' &&
583 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
584 "
585
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700586 if test -n "$GIT_QUIET"
587 then
Junio C Hamano69ae92b2010-10-13 11:36:36 -0700588 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700589 fi
Martin Koegler52070792009-07-22 07:30:58 +0200590 if git merge-recursive $b_tree -- $c_tree $w_tree
しらいしななこf2c66ed2007-06-30 14:37:09 +0900591 then
592 # No conflict
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700593 if test -n "$unstashed_index_tree"
594 then
595 git read-tree "$unstashed_index_tree"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700596 else
597 a="$TMP-added" &&
Shawn O. Pearce89d750b2007-10-18 21:28:43 -0400598 git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
Junio C Hamano83b3df72007-07-27 23:51:45 -0700599 git read-tree --reset $c_tree &&
600 git update-index --add --stdin <"$a" ||
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000601 die "$(gettext "Cannot unstage modified files")"
Junio C Hamano83b3df72007-07-27 23:51:45 -0700602 rm -f "$a"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700603 fi
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700604 squelch=
605 if test -n "$GIT_QUIET"
606 then
607 squelch='>/dev/null 2>&1'
608 fi
Piotr Krukowiecki26b59b42011-03-12 09:57:46 +0100609 (cd "$START_DIR" && eval "git status $squelch") || :
しらいしななこf2c66ed2007-06-30 14:37:09 +0900610 else
611 # Merge conflict; keep the exit status from merge-recursive
Johannes Schindelin150937c2007-07-02 12:14:49 +0100612 status=$?
Phil Hord743bf6d2012-07-10 18:52:28 -0400613 git rerere
Jon Seymour064ed102010-08-21 14:08:58 +1000614 if test -n "$INDEX_OPTION"
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700615 then
Jon Seymour6fdd50e2011-08-07 21:58:16 +1000616 gettextln "Index was not unstashed." >&2
Junio C Hamanocbeaccc2007-07-27 23:41:31 -0700617 fi
Johannes Schindelin150937c2007-07-02 12:14:49 +0100618 exit $status
しらいしななこf2c66ed2007-06-30 14:37:09 +0900619 fi
620}
621
Jon Seymourf2768722010-08-21 14:09:00 +1000622pop_stash() {
623 assert_stash_ref "$@"
624
Junio C Hamano2d4c9932014-02-26 14:18:54 -0800625 if apply_stash "$@"
626 then
627 drop_stash "$@"
628 else
629 status=$?
Vasco Almeida599e7a02016-08-10 10:50:30 +0000630 say "$(gettext "The stash is kept in case you need it again.")"
Junio C Hamano2d4c9932014-02-26 14:18:54 -0800631 exit $status
632 fi
Jon Seymourf2768722010-08-21 14:09:00 +1000633}
634
Brandon Caseye25d5f92008-02-22 13:04:54 -0600635drop_stash () {
Jon Seymour92e39e42010-08-21 14:08:59 +1000636 assert_stash_ref "$@"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600637
Jon Seymour92e39e42010-08-21 14:08:59 +1000638 git reflog delete --updateref --rewrite "${REV}" &&
Ævar Arnfjörð Bjarmasond4ca6c82011-05-21 18:44:18 +0000639 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
640 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
Brandon Caseye25d5f92008-02-22 13:04:54 -0600641
642 # clear_stash if we just dropped the last stash entry
David Aguilar1956dfa2014-09-15 20:24:10 -0700643 git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
644 clear_stash
Brandon Caseye25d5f92008-02-22 13:04:54 -0600645}
646
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530647apply_to_branch () {
Ævar Arnfjörð Bjarmasonb1440ce2011-05-21 18:44:13 +0000648 test -n "$1" || die "$(gettext "No branch name specified")"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530649 branch=$1
Jon Seymourfb433dc2010-08-21 14:09:01 +1000650 shift 1
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530651
Jon Seymourfb433dc2010-08-21 14:09:01 +1000652 set -- --index "$@"
653 assert_stash_like "$@"
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530654
Jon Seymourfb433dc2010-08-21 14:09:01 +1000655 git checkout -b $branch $REV^ &&
Jon Seymour57693d02010-09-28 23:19:52 +1000656 apply_stash "$@" && {
657 test -z "$IS_STASH_REF" || drop_stash "$@"
658 }
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530659}
660
Thomas Gummerer9e140902017-02-28 20:33:40 +0000661test "$1" = "-p" && set "push" "$@"
662
Jon Seymouref763122010-08-21 14:46:22 +1000663PARSE_CACHE='--not-parsed'
Thomas Gummerer1ada5022017-02-28 20:33:39 +0000664# The default command is "push" if nothing but options are given
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200665seen_non_option=
666for opt
667do
668 case "$opt" in
Thomas Gummerer9e140902017-02-28 20:33:40 +0000669 --) break ;;
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200670 -*) ;;
671 *) seen_non_option=t; break ;;
672 esac
673done
674
Thomas Gummerer1ada5022017-02-28 20:33:39 +0000675test -n "$seen_non_option" || set "push" "$@"
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200676
しらいしななこf2c66ed2007-06-30 14:37:09 +0900677# Main command set
678case "$1" in
Junio C Hamanofcb10a92007-07-02 23:15:45 -0700679list)
680 shift
しらいしななこf2c66ed2007-06-30 14:37:09 +0900681 list_stash "$@"
682 ;;
683show)
684 shift
685 show_stash "$@"
686 ;;
Kevin Leung683befa2007-12-03 10:34:05 +0800687save)
688 shift
Junio C Hamano47629dc2008-07-23 13:33:44 -0700689 save_stash "$@"
Kevin Leung683befa2007-12-03 10:34:05 +0800690 ;;
Thomas Gummererf5727e22017-02-19 11:03:08 +0000691push)
692 shift
693 push_stash "$@"
694 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900695apply)
696 shift
697 apply_stash "$@"
698 ;;
699clear)
Junio C Hamano3023dc62008-01-05 01:35:54 -0800700 shift
701 clear_stash "$@"
しらいしななこf2c66ed2007-06-30 14:37:09 +0900702 ;;
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700703create)
Ramkumar Ramachandra0719f302013-06-15 18:43:24 +0530704 shift
Thomas Gummerer9ca63262017-02-19 11:03:10 +0000705 create_stash -m "$*" && echo "$w_commit"
Junio C Hamanobc9e7392007-07-08 01:38:32 -0700706 ;;
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530707store)
708 shift
709 store_stash "$@"
710 ;;
Brandon Caseye25d5f92008-02-22 13:04:54 -0600711drop)
712 shift
713 drop_stash "$@"
714 ;;
Brandon Caseybd56ff52008-02-22 16:52:50 -0600715pop)
716 shift
Jon Seymourf2768722010-08-21 14:09:00 +1000717 pop_stash "$@"
Brandon Caseybd56ff52008-02-22 16:52:50 -0600718 ;;
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530719branch)
720 shift
721 apply_to_branch "$@"
722 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900723*)
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200724 case $# in
725 0)
Thomas Gummerer1ada5022017-02-28 20:33:39 +0000726 push_stash &&
Ævar Arnfjörð Bjarmason5a175872011-05-21 18:44:12 +0000727 say "$(gettext "(To restore them type \"git stash apply\")")"
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200728 ;;
729 *)
Kevin Leung683befa2007-12-03 10:34:05 +0800730 usage
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200731 esac
Junio C Hamano9f62e182007-07-04 22:46:09 -0700732 ;;
しらいしななこf2c66ed2007-06-30 14:37:09 +0900733esac