Merge tag 'v2.3.0'
[git] / git-stash.sh
1 #!/bin/sh
2 # Copyright (c) 2007, Nanako Shiraishi
3
4 dashless=$(basename "$0" | sed -e 's/-/ /')
5 USAGE="list [<options>]
6    or: $dashless show [<stash>]
7    or: $dashless drop [-q|--quiet] [<stash>]
8    or: $dashless ( pop | apply ) [--index|--stage] [-q|--quiet] [<stash>]
9    or: $dashless branch <branchname> [<stash>]
10    or: $dashless [save [--patch] [-k|--[no-]keep-index|--[no-]stage] [-q|--quiet]
11                        [-u|--include-untracked] [-a|--all] [<message>]]
12    or: $dashless clear"
13
14 SUBDIRECTORY_OK=Yes
15 OPTIONS_SPEC=
16 START_DIR=$(pwd)
17 . git-sh-setup
18 . git-sh-i18n
19 require_work_tree
20 cd_to_toplevel
21
22 TMP="$GIT_DIR/.git-stash.$$"
23 TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
24 trap 'rm -f "$TMP-"* "$TMPindex"' 0
25
26 ref_stash=refs/stash
27
28 if 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)"
31 else
32        help_color=
33        reset_color=
34 fi
35
36 no_changes () {
37         git diff-index --quiet --cached HEAD --ignore-submodules -- &&
38         git diff-files --quiet --ignore-submodules &&
39         (test -z "$untracked" || test -z "$(untracked_files)")
40 }
41
42 untracked_files () {
43         excl_opt=--exclude-standard
44         test "$untracked" = "all" && excl_opt=
45         git ls-files -o -z $excl_opt
46 }
47
48 clear_stash () {
49         if test $# != 0
50         then
51                 die "$(gettext "git stash clear with parameters is unimplemented")"
52         fi
53         if current=$(git rev-parse --verify --quiet $ref_stash)
54         then
55                 git update-ref -d $ref_stash $current
56         fi
57 }
58
59 create_stash () {
60         stash_msg="$1"
61         untracked="$2"
62
63         git update-index -q --refresh
64         if no_changes
65         then
66                 exit 0
67         fi
68
69         # state of the base commit
70         if b_commit=$(git rev-parse --verify HEAD)
71         then
72                 head=$(git rev-list --oneline -n 1 HEAD --)
73         else
74                 die "$(gettext "You do not have the initial commit yet")"
75         fi
76
77         if branch=$(git symbolic-ref -q HEAD)
78         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
86         i_tree=$(git write-tree) &&
87         i_commit=$(printf 'index on %s\n' "$msg" |
88                 git commit-tree $i_tree -p $b_commit) ||
89                 die "$(gettext "Cannot save the current index state")"
90
91         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                                 GIT_INDEX_FILE="$TMPindex" &&
98                                 export GIT_INDEX_FILE &&
99                                 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
111         if test -z "$patch_mode"
112         then
113
114                 # state of the working tree
115                 w_tree=$( (
116                         git read-tree --index-output="$TMPindex" -m $i_tree &&
117                         GIT_INDEX_FILE="$TMPindex" &&
118                         export GIT_INDEX_FILE &&
119                         git diff --name-only -z HEAD -- >"$TMP-stagenames" &&
120                         git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
121                         git write-tree &&
122                         rm -f "$TMPindex"
123                 ) ) ||
124                         die "$(gettext "Cannot save the current worktree state")"
125
126         else
127
128                 rm -f "$TMP-index" &&
129                 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) ||
137                 die "$(gettext "Cannot save the current worktree state")"
138
139                 git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
140                 test -s "$TMP-patch" ||
141                 die "$(gettext "No changes selected")"
142
143                 rm -f "$TMP-index" ||
144                 die "$(gettext "Cannot remove temporary index (can't happen)")"
145
146         fi
147
148         # create the stash
149         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" |
156         git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
157         die "$(gettext "Cannot record working tree state")"
158 }
159
160 store_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
195 save_stash () {
196         keep_index=
197         patch_mode=
198         untracked=
199         while test $# != 0
200         do
201                 case "$1" in
202                 -k|--keep-index)
203                         keep_index=t
204                         ;;
205                 --no-keep-index)
206                         keep_index=n
207                         ;;
208                 --stage)
209                         keep_index=n
210                         ;;
211                 --no-stage)
212                         keep_index=t
213                         ;;
214                 -p|--patch)
215                         patch_mode=t
216                         # only default to keep if we don't already have an override
217                         test -z "$keep_index" && keep_index=t
218                         ;;
219                 -q|--quiet)
220                         GIT_QUIET=t
221                         ;;
222                 -u|--include-untracked)
223                         untracked=untracked
224                         ;;
225                 -a|--all)
226                         untracked=all
227                         ;;
228                 --)
229                         shift
230                         break
231                         ;;
232                 -*)
233                         option="$1"
234                         # TRANSLATORS: $option is an invalid option, like
235                         # `--blah-blah'. The 7 spaces at the beginning of the
236                         # second line correspond to "error: ". So you should line
237                         # up the second line with however many characters the
238                         # translation of "error: " takes in your language. E.g. in
239                         # English this is:
240                         #
241                         #    $ git stash save --blah-blah 2>&1 | head -n 2
242                         #    error: unknown option for 'stash save': --blah-blah
243                         #           To provide a message, use git stash save -- '--blah-blah'
244                         eval_gettextln "error: unknown option for 'stash save': \$option
245        To provide a message, use git stash save -- '\$option'"
246                         usage
247                         ;;
248                 *)
249                         break
250                         ;;
251                 esac
252                 shift
253         done
254
255         if test -n "$patch_mode" && test -n "$untracked"
256         then
257             die "Can't use --patch and --include-untracked or --all at the same time"
258         fi
259
260         stash_msg="$*"
261
262         git update-index -q --refresh
263         if no_changes
264         then
265                 say "$(gettext "No local changes to save")"
266                 exit 0
267         fi
268         test -f "$GIT_DIR/logs/$ref_stash" ||
269                 clear_stash || die "$(gettext "Cannot initialize stash")"
270
271         create_stash "$stash_msg" $untracked
272         store_stash -m "$stash_msg" -q $w_commit ||
273         die "$(gettext "Cannot save the current status")"
274         say Saved working directory and index state "$stash_msg"
275
276         if test -z "$patch_mode"
277         then
278                 git reset --hard ${GIT_QUIET:+-q}
279                 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
280                 if test -n "$untracked"
281                 then
282                         git clean --force --quiet -d $CLEAN_X_OPTION
283                 fi
284
285                 if test "$keep_index" = "t" && test -n $i_tree
286                 then
287                         git read-tree --reset -u $i_tree
288                 fi
289         else
290                 git apply -R < "$TMP-patch" ||
291                 die "$(gettext "Cannot remove worktree changes")"
292
293                 if test "$keep_index" != "t"
294                 then
295                         git reset
296                 fi
297         fi
298 }
299
300 have_stash () {
301         git rev-parse --verify --quiet $ref_stash >/dev/null
302 }
303
304 list_stash () {
305         have_stash || return 0
306         git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
307 }
308
309 show_stash () {
310         assert_stash_like "$@"
311
312         git diff ${FLAGS:---stat} $b_commit $w_commit
313 }
314
315 #
316 # Parses the remaining options looking for flags and
317 # at most one revision defaulting to ${ref_stash}@{0}
318 # if none found.
319 #
320 # Derives related tree and commit objects from the
321 # revision, if one is found.
322 #
323 # stash records the work tree, and is a merge between the
324 # base commit (first parent) and the index tree (second parent).
325 #
326 #   REV is set to the symbolic version of the specified stash-like commit
327 #   IS_STASH_LIKE is non-blank if ${REV} looks like a stash
328 #   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
329 #   s is set to the SHA1 of the stash commit
330 #   w_commit is set to the commit containing the working tree
331 #   b_commit is set to the base commit
332 #   i_commit is set to the commit containing the index tree
333 #   u_commit is set to the commit containing the untracked files tree
334 #   w_tree is set to the working tree
335 #   b_tree is set to the base tree
336 #   i_tree is set to the index tree
337 #   u_tree is set to the untracked files tree
338 #
339 #   GIT_QUIET is set to t if -q is specified
340 #   INDEX_OPTION is set to --index if --index is specified.
341 #   FLAGS is set to the remaining flags
342 #
343 # dies if:
344 #   * too many revisions specified
345 #   * no revision is specified and there is no stash stack
346 #   * a revision is specified which cannot be resolve to a SHA1
347 #   * a non-existent stash reference is specified
348 #
349
350 parse_flags_and_rev()
351 {
352         test "$PARSE_CACHE" = "$*" && return 0 # optimisation
353         PARSE_CACHE="$*"
354
355         IS_STASH_LIKE=
356         IS_STASH_REF=
357         INDEX_OPTION=
358         s=
359         w_commit=
360         b_commit=
361         i_commit=
362         u_commit=
363         w_tree=
364         b_tree=
365         i_tree=
366         u_tree=
367
368         REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
369
370         FLAGS=
371         for opt
372         do
373                 case "$opt" in
374                         -q|--quiet)
375                                 GIT_QUIET=-t
376                         ;;
377                         --index|--stage)
378                                 INDEX_OPTION=--index
379                         ;;
380                         -*)
381                                 FLAGS="${FLAGS}${FLAGS:+ }$opt"
382                         ;;
383                 esac
384         done
385
386         eval set -- $REV
387
388         case $# in
389                 0)
390                         have_stash || die "$(gettext "No stash found.")"
391                         set -- ${ref_stash}@{0}
392                 ;;
393                 1)
394                         :
395                 ;;
396                 *)
397                         die "$(eval_gettext "Too many revisions specified: \$REV")"
398                 ;;
399         esac
400
401         REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
402                 reference="$1"
403                 die "$(eval_gettext "\$reference is not a valid reference")"
404         }
405
406         i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
407         set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
408         s=$1 &&
409         w_commit=$1 &&
410         b_commit=$2 &&
411         w_tree=$3 &&
412         b_tree=$4 &&
413         i_tree=$5 &&
414         IS_STASH_LIKE=t &&
415         test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
416         IS_STASH_REF=t
417
418         u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
419         u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
420 }
421
422 is_stash_like()
423 {
424         parse_flags_and_rev "$@"
425         test -n "$IS_STASH_LIKE"
426 }
427
428 assert_stash_like() {
429         is_stash_like "$@" || {
430                 args="$*"
431                 die "$(eval_gettext "'\$args' is not a stash-like commit")"
432         }
433 }
434
435 is_stash_ref() {
436         is_stash_like "$@" && test -n "$IS_STASH_REF"
437 }
438
439 assert_stash_ref() {
440         is_stash_ref "$@" || {
441                 args="$*"
442                 die "$(eval_gettext "'\$args' is not a stash reference")"
443         }
444 }
445
446 apply_stash () {
447
448         assert_stash_like "$@"
449
450         git update-index -q --refresh || die "$(gettext "unable to refresh index")"
451
452         # current index state
453         c_tree=$(git write-tree) ||
454                 die "$(gettext "Cannot apply a stash in the middle of a merge")"
455
456         unstashed_index_tree=
457         if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
458                         test "$c_tree" != "$i_tree"
459         then
460                 git diff-tree --binary $s^2^..$s^2 | git apply --cached
461                 test $? -ne 0 &&
462                         die "$(gettext "Conflicts in index. Try without --index.")"
463                 unstashed_index_tree=$(git write-tree) ||
464                         die "$(gettext "Could not save index tree")"
465                 git reset
466         fi
467
468         if test -n "$u_tree"
469         then
470                 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
471                 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
472                 rm -f "$TMPindex" ||
473                 die 'Could not restore untracked files from stash'
474         fi
475
476         eval "
477                 GITHEAD_$w_tree='Stashed changes' &&
478                 GITHEAD_$c_tree='Updated upstream' &&
479                 GITHEAD_$b_tree='Version stash was based on' &&
480                 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
481         "
482
483         if test -n "$GIT_QUIET"
484         then
485                 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
486         fi
487         if git merge-recursive $b_tree -- $c_tree $w_tree
488         then
489                 # No conflict
490                 if test -n "$unstashed_index_tree"
491                 then
492                         git read-tree "$unstashed_index_tree"
493                 else
494                         a="$TMP-added" &&
495                         git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
496                         git read-tree --reset $c_tree &&
497                         git update-index --add --stdin <"$a" ||
498                                 die "$(gettext "Cannot unstage modified files")"
499                         rm -f "$a"
500                 fi
501                 squelch=
502                 if test -n "$GIT_QUIET"
503                 then
504                         squelch='>/dev/null 2>&1'
505                 fi
506                 (cd "$START_DIR" && eval "git status $squelch") || :
507         else
508                 # Merge conflict; keep the exit status from merge-recursive
509                 status=$?
510                 git rerere
511                 if test -n "$INDEX_OPTION"
512                 then
513                         gettextln "Index was not unstashed." >&2
514                 fi
515                 exit $status
516         fi
517 }
518
519 pop_stash() {
520         assert_stash_ref "$@"
521
522         if apply_stash "$@"
523         then
524                 drop_stash "$@"
525         else
526                 status=$?
527                 say "The stash is kept in case you need it again."
528                 exit $status
529         fi
530 }
531
532 drop_stash () {
533         assert_stash_ref "$@"
534
535         git reflog delete --updateref --rewrite "${REV}" &&
536                 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
537                 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
538
539         # clear_stash if we just dropped the last stash entry
540         git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
541         clear_stash
542 }
543
544 apply_to_branch () {
545         test -n "$1" || die "$(gettext "No branch name specified")"
546         branch=$1
547         shift 1
548
549         set -- --index "$@"
550         assert_stash_like "$@"
551
552         git checkout -b $branch $REV^ &&
553         apply_stash "$@" && {
554                 test -z "$IS_STASH_REF" || drop_stash "$@"
555         }
556 }
557
558 PARSE_CACHE='--not-parsed'
559 # The default command is "save" if nothing but options are given
560 seen_non_option=
561 for opt
562 do
563         case "$opt" in
564         -*) ;;
565         *) seen_non_option=t; break ;;
566         esac
567 done
568
569 test -n "$seen_non_option" || set "save" "$@"
570
571 # Main command set
572 case "$1" in
573 list)
574         shift
575         list_stash "$@"
576         ;;
577 show)
578         shift
579         show_stash "$@"
580         ;;
581 save)
582         shift
583         save_stash "$@"
584         ;;
585 apply)
586         shift
587         apply_stash "$@"
588         ;;
589 clear)
590         shift
591         clear_stash "$@"
592         ;;
593 create)
594         shift
595         create_stash "$*" && echo "$w_commit"
596         ;;
597 store)
598         shift
599         store_stash "$@"
600         ;;
601 drop)
602         shift
603         drop_stash "$@"
604         ;;
605 pop)
606         shift
607         pop_stash "$@"
608         ;;
609 branch)
610         shift
611         apply_to_branch "$@"
612         ;;
613 *)
614         case $# in
615         0)
616                 save_stash &&
617                 say "$(gettext "(To restore them type \"git stash apply\")")"
618                 ;;
619         *)
620                 usage
621         esac
622         ;;
623 esac