Merge branch 'bw/protocol-v1'
[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] [-q|--quiet] [<stash>]
9    or: $dashless branch <branchname> [<stash>]
10    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>...]]
15    or: $dashless clear"
16
17 SUBDIRECTORY_OK=Yes
18 OPTIONS_SPEC=
19 START_DIR=$(pwd)
20 . git-sh-setup
21 require_work_tree
22 prefix=$(git rev-parse --show-prefix) || exit 1
23 cd_to_toplevel
24
25 TMP="$GIT_DIR/.git-stash.$$"
26 TMPindex=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$
27 trap 'rm -f "$TMP-"* "$TMPindex"' 0
28
29 ref_stash=refs/stash
30
31 if 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)"
34 else
35        help_color=
36        reset_color=
37 fi
38
39 no_changes () {
40         git diff-index --quiet --cached HEAD --ignore-submodules -- "$@" &&
41         git diff-files --quiet --ignore-submodules -- "$@" &&
42         (test -z "$untracked" || test -z "$(untracked_files)")
43 }
44
45 untracked_files () {
46         if test "$1" = "-z"
47         then
48                 shift
49                 z=-z
50         else
51                 z=
52         fi
53         excl_opt=--exclude-standard
54         test "$untracked" = "all" && excl_opt=
55         git ls-files -o $z $excl_opt -- "$@"
56 }
57
58 clear_stash () {
59         if test $# != 0
60         then
61                 die "$(gettext "git stash clear with parameters is unimplemented")"
62         fi
63         if current=$(git rev-parse --verify --quiet $ref_stash)
64         then
65                 git update-ref -d $ref_stash $current
66         fi
67 }
68
69 create_stash () {
70         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                         ;;
79                 -m*)
80                         stash_msg=${1#-m}
81                         ;;
82                 --message=*)
83                         stash_msg=${1#--message=}
84                         ;;
85                 -u|--include-untracked)
86                         shift
87                         untracked=${1?"BUG: create_stash () -u requires an argument"}
88                         ;;
89                 --)
90                         shift
91                         break
92                         ;;
93                 esac
94                 shift
95         done
96
97         git update-index -q --refresh
98         if no_changes "$@"
99         then
100                 exit 0
101         fi
102
103         # state of the base commit
104         if b_commit=$(git rev-parse --verify HEAD)
105         then
106                 head=$(git rev-list --oneline -n 1 HEAD --)
107         else
108                 die "$(gettext "You do not have the initial commit yet")"
109         fi
110
111         if branch=$(git symbolic-ref -q HEAD)
112         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
120         i_tree=$(git write-tree) &&
121         i_commit=$(printf 'index on %s\n' "$msg" |
122                 git commit-tree $i_tree -p $b_commit) ||
123                 die "$(gettext "Cannot save the current index state")"
124
125         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=$(
130                         untracked_files -z "$@" | (
131                                 GIT_INDEX_FILE="$TMPindex" &&
132                                 export GIT_INDEX_FILE &&
133                                 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"
138                 ) ) || die "$(gettext "Cannot save the untracked files")"
139
140                 untracked_commit_option="-p $u_commit";
141         else
142                 untracked_commit_option=
143         fi
144
145         if test -z "$patch_mode"
146         then
147
148                 # state of the working tree
149                 w_tree=$( (
150                         git read-tree --index-output="$TMPindex" -m $i_tree &&
151                         GIT_INDEX_FILE="$TMPindex" &&
152                         export GIT_INDEX_FILE &&
153                         git diff-index --name-only -z HEAD -- "$@" >"$TMP-stagenames" &&
154                         git update-index -z --add --remove --stdin <"$TMP-stagenames" &&
155                         git write-tree &&
156                         rm -f "$TMPindex"
157                 ) ) ||
158                         die "$(gettext "Cannot save the current worktree state")"
159
160         else
161
162                 rm -f "$TMP-index" &&
163                 GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
164
165                 # find out what the user wants
166                 GIT_INDEX_FILE="$TMP-index" \
167                         git add--interactive --patch=stash -- "$@" &&
168
169                 # state of the working tree
170                 w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
171                 die "$(gettext "Cannot save the current worktree state")"
172
173                 git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
174                 test -s "$TMP-patch" ||
175                 die "$(gettext "No changes selected")"
176
177                 rm -f "$TMP-index" ||
178                 die "$(gettext "Cannot remove temporary index (can't happen)")"
179
180         fi
181
182         # create the stash
183         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" |
190         git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
191         die "$(gettext "Cannot record working tree state")"
192 }
193
194 store_stash () {
195         while test $# != 0
196         do
197                 case "$1" in
198                 -m|--message)
199                         shift
200                         stash_msg="$1"
201                         ;;
202                 -m*)
203                         stash_msg=${1#-m}
204                         ;;
205                 --message=*)
206                         stash_msg=${1#--message=}
207                         ;;
208                 -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
226         git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
227         ret=$?
228         test $ret != 0 && test -z "$quiet" &&
229         die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
230         return $ret
231 }
232
233 push_stash () {
234         keep_index=
235         patch_mode=
236         untracked=
237         stash_msg=
238         while test $# != 0
239         do
240                 case "$1" in
241                 -k|--keep-index)
242                         keep_index=t
243                         ;;
244                 --no-keep-index)
245                         keep_index=n
246                         ;;
247                 -p|--patch)
248                         patch_mode=t
249                         # only default to keep if we don't already have an override
250                         test -z "$keep_index" && keep_index=t
251                         ;;
252                 -q|--quiet)
253                         GIT_QUIET=t
254                         ;;
255                 -u|--include-untracked)
256                         untracked=untracked
257                         ;;
258                 -a|--all)
259                         untracked=all
260                         ;;
261                 -m|--message)
262                         shift
263                         test -z ${1+x} && usage
264                         stash_msg=$1
265                         ;;
266                 -m*)
267                         stash_msg=${1#-m}
268                         ;;
269                 --message=*)
270                         stash_msg=${1#--message=}
271                         ;;
272                 --help)
273                         show_help
274                         ;;
275                 --)
276                         shift
277                         break
278                         ;;
279                 -*)
280                         option="$1"
281                         eval_gettextln "error: unknown option for 'stash push': \$option"
282                         usage
283                         ;;
284                 *)
285                         break
286                         ;;
287                 esac
288                 shift
289         done
290
291         eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
292
293         if test -n "$patch_mode" && test -n "$untracked"
294         then
295                 die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")"
296         fi
297
298         test -n "$untracked" || git ls-files --error-unmatch -- "$@" >/dev/null || exit 1
299
300         git update-index -q --refresh
301         if no_changes "$@"
302         then
303                 say "$(gettext "No local changes to save")"
304                 exit 0
305         fi
306
307         git reflog exists $ref_stash ||
308                 clear_stash || die "$(gettext "Cannot initialize stash")"
309
310         create_stash -m "$stash_msg" -u "$untracked" -- "$@"
311         store_stash -m "$stash_msg" -q $w_commit ||
312         die "$(gettext "Cannot save the current status")"
313         say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
314
315         if test -z "$patch_mode"
316         then
317                 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
323                 if test $# != 0
324                 then
325                         git reset -q -- "$@"
326                         git ls-files -z --modified -- "$@" |
327                         git checkout-index -z --force --stdin
328                         git clean --force -q -d -- "$@"
329                 else
330                         git reset --hard -q
331                 fi
332
333                 if test "$keep_index" = "t" && test -n "$i_tree"
334                 then
335                         git read-tree --reset $i_tree
336                         git ls-files -z --modified -- "$@" |
337                         git checkout-index -z --force --stdin
338                 fi
339         else
340                 git apply -R < "$TMP-patch" ||
341                 die "$(gettext "Cannot remove worktree changes")"
342
343                 if test "$keep_index" != "t"
344                 then
345                         git reset -q -- "$@"
346                 fi
347         fi
348 }
349
350 save_stash () {
351         push_options=
352         while test $# != 0
353         do
354                 case "$1" in
355                 --)
356                         shift
357                         break
358                         ;;
359                 -*)
360                         # pass all options through to push_stash
361                         push_options="$push_options $1"
362                         ;;
363                 *)
364                         break
365                         ;;
366                 esac
367                 shift
368         done
369
370         stash_msg="$*"
371
372         if test -z "$stash_msg"
373         then
374                 push_stash $push_options
375         else
376                 push_stash $push_options -m "$stash_msg"
377         fi
378 }
379
380 have_stash () {
381         git rev-parse --verify --quiet $ref_stash >/dev/null
382 }
383
384 list_stash () {
385         have_stash || return 0
386         git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
387 }
388
389 show_stash () {
390         ALLOW_UNKNOWN_FLAGS=t
391         assert_stash_like "$@"
392
393         if test -z "$FLAGS"
394         then
395                 if test "$(git config --bool stash.showStat || echo true)" = "true"
396                 then
397                         FLAGS=--stat
398                 fi
399
400                 if test "$(git config --bool stash.showPatch || echo false)" = "true"
401                 then
402                         FLAGS=${FLAGS}${FLAGS:+ }-p
403                 fi
404
405                 if test -z "$FLAGS"
406                 then
407                         return 0
408                 fi
409         fi
410
411         git diff ${FLAGS} $b_commit $w_commit
412 }
413
414 show_help () {
415         exec git help stash
416         exit 1
417 }
418
419 #
420 # Parses the remaining options looking for flags and
421 # at most one revision defaulting to ${ref_stash}@{0}
422 # if none found.
423 #
424 # Derives related tree and commit objects from the
425 # revision, if one is found.
426 #
427 # stash records the work tree, and is a merge between the
428 # base commit (first parent) and the index tree (second parent).
429 #
430 #   REV is set to the symbolic version of the specified stash-like commit
431 #   IS_STASH_LIKE is non-blank if ${REV} looks like a stash
432 #   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
433 #   s is set to the SHA1 of the stash commit
434 #   w_commit is set to the commit containing the working tree
435 #   b_commit is set to the base commit
436 #   i_commit is set to the commit containing the index tree
437 #   u_commit is set to the commit containing the untracked files tree
438 #   w_tree is set to the working tree
439 #   b_tree is set to the base tree
440 #   i_tree is set to the index tree
441 #   u_tree is set to the untracked files tree
442 #
443 #   GIT_QUIET is set to t if -q is specified
444 #   INDEX_OPTION is set to --index if --index is specified.
445 #   FLAGS is set to the remaining flags (if allowed)
446 #
447 # dies if:
448 #   * too many revisions specified
449 #   * no revision is specified and there is no stash stack
450 #   * a revision is specified which cannot be resolve to a SHA1
451 #   * a non-existent stash reference is specified
452 #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
453 #
454
455 parse_flags_and_rev()
456 {
457         test "$PARSE_CACHE" = "$*" && return 0 # optimisation
458         PARSE_CACHE="$*"
459
460         IS_STASH_LIKE=
461         IS_STASH_REF=
462         INDEX_OPTION=
463         s=
464         w_commit=
465         b_commit=
466         i_commit=
467         u_commit=
468         w_tree=
469         b_tree=
470         i_tree=
471         u_tree=
472
473         FLAGS=
474         REV=
475         for opt
476         do
477                 case "$opt" in
478                         -q|--quiet)
479                                 GIT_QUIET=-t
480                         ;;
481                         --index)
482                                 INDEX_OPTION=--index
483                         ;;
484                         --help)
485                                 show_help
486                         ;;
487                         -*)
488                                 test "$ALLOW_UNKNOWN_FLAGS" = t ||
489                                         die "$(eval_gettext "unknown option: \$opt")"
490                                 FLAGS="${FLAGS}${FLAGS:+ }$opt"
491                         ;;
492                         *)
493                                 REV="${REV}${REV:+ }'$opt'"
494                         ;;
495                 esac
496         done
497
498         eval set -- $REV
499
500         case $# in
501                 0)
502                         have_stash || die "$(gettext "No stash entries found.")"
503                         set -- ${ref_stash}@{0}
504                 ;;
505                 1)
506                         :
507                 ;;
508                 *)
509                         die "$(eval_gettext "Too many revisions specified: \$REV")"
510                 ;;
511         esac
512
513         case "$1" in
514                 *[!0-9]*)
515                         :
516                 ;;
517                 *)
518                         set -- "${ref_stash}@{$1}"
519                 ;;
520         esac
521
522         REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
523                 reference="$1"
524                 die "$(eval_gettext "\$reference is not a valid reference")"
525         }
526
527         i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
528         set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
529         s=$1 &&
530         w_commit=$1 &&
531         b_commit=$2 &&
532         w_tree=$3 &&
533         b_tree=$4 &&
534         i_tree=$5 &&
535         IS_STASH_LIKE=t &&
536         test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
537         IS_STASH_REF=t
538
539         u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
540         u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
541 }
542
543 is_stash_like()
544 {
545         parse_flags_and_rev "$@"
546         test -n "$IS_STASH_LIKE"
547 }
548
549 assert_stash_like() {
550         is_stash_like "$@" || {
551                 args="$*"
552                 die "$(eval_gettext "'\$args' is not a stash-like commit")"
553         }
554 }
555
556 is_stash_ref() {
557         is_stash_like "$@" && test -n "$IS_STASH_REF"
558 }
559
560 assert_stash_ref() {
561         is_stash_ref "$@" || {
562                 args="$*"
563                 die "$(eval_gettext "'\$args' is not a stash reference")"
564         }
565 }
566
567 apply_stash () {
568
569         assert_stash_like "$@"
570
571         git update-index -q --refresh || die "$(gettext "unable to refresh index")"
572
573         # current index state
574         c_tree=$(git write-tree) ||
575                 die "$(gettext "Cannot apply a stash in the middle of a merge")"
576
577         unstashed_index_tree=
578         if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
579                         test "$c_tree" != "$i_tree"
580         then
581                 git diff-tree --binary $s^2^..$s^2 | git apply --cached
582                 test $? -ne 0 &&
583                         die "$(gettext "Conflicts in index. Try without --index.")"
584                 unstashed_index_tree=$(git write-tree) ||
585                         die "$(gettext "Could not save index tree")"
586                 git reset
587         fi
588
589         if test -n "$u_tree"
590         then
591                 GIT_INDEX_FILE="$TMPindex" git read-tree "$u_tree" &&
592                 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
593                 rm -f "$TMPindex" ||
594                 die "$(gettext "Could not restore untracked files from stash entry")"
595         fi
596
597         eval "
598                 GITHEAD_$w_tree='Stashed changes' &&
599                 GITHEAD_$c_tree='Updated upstream' &&
600                 GITHEAD_$b_tree='Version stash was based on' &&
601                 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
602         "
603
604         if test -n "$GIT_QUIET"
605         then
606                 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
607         fi
608         if git merge-recursive $b_tree -- $c_tree $w_tree
609         then
610                 # No conflict
611                 if test -n "$unstashed_index_tree"
612                 then
613                         git read-tree "$unstashed_index_tree"
614                 else
615                         a="$TMP-added" &&
616                         git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
617                         git read-tree --reset $c_tree &&
618                         git update-index --add --stdin <"$a" ||
619                                 die "$(gettext "Cannot unstage modified files")"
620                         rm -f "$a"
621                 fi
622                 squelch=
623                 if test -n "$GIT_QUIET"
624                 then
625                         squelch='>/dev/null 2>&1'
626                 fi
627                 (cd "$START_DIR" && eval "git status $squelch") || :
628         else
629                 # Merge conflict; keep the exit status from merge-recursive
630                 status=$?
631                 git rerere
632                 if test -n "$INDEX_OPTION"
633                 then
634                         gettextln "Index was not unstashed." >&2
635                 fi
636                 exit $status
637         fi
638 }
639
640 pop_stash() {
641         assert_stash_ref "$@"
642
643         if apply_stash "$@"
644         then
645                 drop_stash "$@"
646         else
647                 status=$?
648                 say "$(gettext "The stash entry is kept in case you need it again.")"
649                 exit $status
650         fi
651 }
652
653 drop_stash () {
654         assert_stash_ref "$@"
655
656         git reflog delete --updateref --rewrite "${REV}" &&
657                 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
658                 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
659
660         # clear_stash if we just dropped the last stash entry
661         git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
662         clear_stash
663 }
664
665 apply_to_branch () {
666         test -n "$1" || die "$(gettext "No branch name specified")"
667         branch=$1
668         shift 1
669
670         set -- --index "$@"
671         assert_stash_like "$@"
672
673         git checkout -b $branch $REV^ &&
674         apply_stash "$@" && {
675                 test -z "$IS_STASH_REF" || drop_stash "$@"
676         }
677 }
678
679 test "$1" = "-p" && set "push" "$@"
680
681 PARSE_CACHE='--not-parsed'
682 # The default command is "push" if nothing but options are given
683 seen_non_option=
684 for opt
685 do
686         case "$opt" in
687         --) break ;;
688         -*) ;;
689         *) seen_non_option=t; break ;;
690         esac
691 done
692
693 test -n "$seen_non_option" || set "push" "$@"
694
695 # Main command set
696 case "$1" in
697 list)
698         shift
699         list_stash "$@"
700         ;;
701 show)
702         shift
703         show_stash "$@"
704         ;;
705 save)
706         shift
707         save_stash "$@"
708         ;;
709 push)
710         shift
711         push_stash "$@"
712         ;;
713 apply)
714         shift
715         apply_stash "$@"
716         ;;
717 clear)
718         shift
719         clear_stash "$@"
720         ;;
721 create)
722         shift
723         create_stash -m "$*" && echo "$w_commit"
724         ;;
725 store)
726         shift
727         store_stash "$@"
728         ;;
729 drop)
730         shift
731         drop_stash "$@"
732         ;;
733 pop)
734         shift
735         pop_stash "$@"
736         ;;
737 branch)
738         shift
739         apply_to_branch "$@"
740         ;;
741 *)
742         case $# in
743         0)
744                 push_stash &&
745                 say "$(gettext "(To restore them type \"git stash apply\")")"
746                 ;;
747         *)
748                 usage
749         esac
750         ;;
751 esac