stash: add --stage to pop and apply
[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 rev-parse --git-path 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         git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
187         ret=$?
188         test $ret != 0 && test -z $quiet &&
189         die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
190         return $ret
191 }
192
193 save_stash () {
194         keep_index=
195         patch_mode=
196         untracked=
197         while test $# != 0
198         do
199                 case "$1" in
200                 -k|--keep-index)
201                         keep_index=t
202                         ;;
203                 --no-keep-index)
204                         keep_index=n
205                         ;;
206                 --stage)
207                         keep_index=n
208                         ;;
209                 --no-stage)
210                         keep_index=t
211                         ;;
212                 -p|--patch)
213                         patch_mode=t
214                         # only default to keep if we don't already have an override
215                         test -z "$keep_index" && keep_index=t
216                         ;;
217                 -q|--quiet)
218                         GIT_QUIET=t
219                         ;;
220                 -u|--include-untracked)
221                         untracked=untracked
222                         ;;
223                 -a|--all)
224                         untracked=all
225                         ;;
226                 --help)
227                         show_help
228                         ;;
229                 --)
230                         shift
231                         break
232                         ;;
233                 -*)
234                         option="$1"
235                         # TRANSLATORS: $option is an invalid option, like
236                         # `--blah-blah'. The 7 spaces at the beginning of the
237                         # second line correspond to "error: ". So you should line
238                         # up the second line with however many characters the
239                         # translation of "error: " takes in your language. E.g. in
240                         # English this is:
241                         #
242                         #    $ git stash save --blah-blah 2>&1 | head -n 2
243                         #    error: unknown option for 'stash save': --blah-blah
244                         #           To provide a message, use git stash save -- '--blah-blah'
245                         eval_gettextln "error: unknown option for 'stash save': \$option
246        To provide a message, use git stash save -- '\$option'"
247                         usage
248                         ;;
249                 *)
250                         break
251                         ;;
252                 esac
253                 shift
254         done
255
256         if test -n "$patch_mode" && test -n "$untracked"
257         then
258             die "Can't use --patch and --include-untracked or --all at the same time"
259         fi
260
261         stash_msg="$*"
262
263         git update-index -q --refresh
264         if no_changes
265         then
266                 say "$(gettext "No local changes to save")"
267                 exit 0
268         fi
269         git reflog exists $ref_stash ||
270                 clear_stash || die "$(gettext "Cannot initialize stash")"
271
272         create_stash "$stash_msg" $untracked
273         store_stash -m "$stash_msg" -q $w_commit ||
274         die "$(gettext "Cannot save the current status")"
275         say Saved working directory and index state "$stash_msg"
276
277         if test -z "$patch_mode"
278         then
279                 git reset --hard ${GIT_QUIET:+-q}
280                 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
281                 if test -n "$untracked"
282                 then
283                         git clean --force --quiet -d $CLEAN_X_OPTION
284                 fi
285
286                 if test "$keep_index" = "t" && test -n $i_tree
287                 then
288                         git read-tree --reset -u $i_tree
289                 fi
290         else
291                 git apply -R < "$TMP-patch" ||
292                 die "$(gettext "Cannot remove worktree changes")"
293
294                 if test "$keep_index" != "t"
295                 then
296                         git reset
297                 fi
298         fi
299 }
300
301 have_stash () {
302         git rev-parse --verify --quiet $ref_stash >/dev/null
303 }
304
305 list_stash () {
306         have_stash || return 0
307         git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --
308 }
309
310 show_stash () {
311         ALLOW_UNKNOWN_FLAGS=t
312         assert_stash_like "$@"
313
314         if test -z "$FLAGS"
315         then
316                 if test "$(git config --bool stash.showStat || echo true)" = "true"
317                 then
318                         FLAGS=--stat
319                 fi
320
321                 if test "$(git config --bool stash.showPatch || echo false)" = "true"
322                 then
323                         FLAGS=${FLAGS}${FLAGS:+ }-p
324                 fi
325
326                 if test -z "$FLAGS"
327                 then
328                         return 0
329                 fi
330         fi
331
332         git diff ${FLAGS} $b_commit $w_commit
333 }
334
335 show_help () {
336         exec git help stash
337         exit 1
338 }
339
340 #
341 # Parses the remaining options looking for flags and
342 # at most one revision defaulting to ${ref_stash}@{0}
343 # if none found.
344 #
345 # Derives related tree and commit objects from the
346 # revision, if one is found.
347 #
348 # stash records the work tree, and is a merge between the
349 # base commit (first parent) and the index tree (second parent).
350 #
351 #   REV is set to the symbolic version of the specified stash-like commit
352 #   IS_STASH_LIKE is non-blank if ${REV} looks like a stash
353 #   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
354 #   s is set to the SHA1 of the stash commit
355 #   w_commit is set to the commit containing the working tree
356 #   b_commit is set to the base commit
357 #   i_commit is set to the commit containing the index tree
358 #   u_commit is set to the commit containing the untracked files tree
359 #   w_tree is set to the working tree
360 #   b_tree is set to the base tree
361 #   i_tree is set to the index tree
362 #   u_tree is set to the untracked files tree
363 #
364 #   GIT_QUIET is set to t if -q is specified
365 #   INDEX_OPTION is set to --index if --index is specified.
366 #   FLAGS is set to the remaining flags (if allowed)
367 #
368 # dies if:
369 #   * too many revisions specified
370 #   * no revision is specified and there is no stash stack
371 #   * a revision is specified which cannot be resolve to a SHA1
372 #   * a non-existent stash reference is specified
373 #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
374 #
375
376 parse_flags_and_rev()
377 {
378         test "$PARSE_CACHE" = "$*" && return 0 # optimisation
379         PARSE_CACHE="$*"
380
381         IS_STASH_LIKE=
382         IS_STASH_REF=
383         INDEX_OPTION=
384         s=
385         w_commit=
386         b_commit=
387         i_commit=
388         u_commit=
389         w_tree=
390         b_tree=
391         i_tree=
392         u_tree=
393
394         REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
395
396         FLAGS=
397         for opt
398         do
399                 case "$opt" in
400                         -q|--quiet)
401                                 GIT_QUIET=-t
402                         ;;
403                         --index|--stage)
404                                 INDEX_OPTION=--index
405                         ;;
406                         --help)
407                                 show_help
408                         ;;
409                         -*)
410                                 test "$ALLOW_UNKNOWN_FLAGS" = t ||
411                                         die "$(eval_gettext "unknown option: \$opt")"
412                                 FLAGS="${FLAGS}${FLAGS:+ }$opt"
413                         ;;
414                 esac
415         done
416
417         eval set -- $REV
418
419         case $# in
420                 0)
421                         have_stash || die "$(gettext "No stash found.")"
422                         set -- ${ref_stash}@{0}
423                 ;;
424                 1)
425                         :
426                 ;;
427                 *)
428                         die "$(eval_gettext "Too many revisions specified: \$REV")"
429                 ;;
430         esac
431
432         REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
433                 reference="$1"
434                 die "$(eval_gettext "\$reference is not a valid reference")"
435         }
436
437         i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
438         set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
439         s=$1 &&
440         w_commit=$1 &&
441         b_commit=$2 &&
442         w_tree=$3 &&
443         b_tree=$4 &&
444         i_tree=$5 &&
445         IS_STASH_LIKE=t &&
446         test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
447         IS_STASH_REF=t
448
449         u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
450         u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
451 }
452
453 is_stash_like()
454 {
455         parse_flags_and_rev "$@"
456         test -n "$IS_STASH_LIKE"
457 }
458
459 assert_stash_like() {
460         is_stash_like "$@" || {
461                 args="$*"
462                 die "$(eval_gettext "'\$args' is not a stash-like commit")"
463         }
464 }
465
466 is_stash_ref() {
467         is_stash_like "$@" && test -n "$IS_STASH_REF"
468 }
469
470 assert_stash_ref() {
471         is_stash_ref "$@" || {
472                 args="$*"
473                 die "$(eval_gettext "'\$args' is not a stash reference")"
474         }
475 }
476
477 apply_stash () {
478
479         assert_stash_like "$@"
480
481         git update-index -q --refresh || die "$(gettext "unable to refresh index")"
482
483         # current index state
484         c_tree=$(git write-tree) ||
485                 die "$(gettext "Cannot apply a stash in the middle of a merge")"
486
487         unstashed_index_tree=
488         if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
489                         test "$c_tree" != "$i_tree"
490         then
491                 git diff-tree --binary $s^2^..$s^2 | git apply --cached
492                 test $? -ne 0 &&
493                         die "$(gettext "Conflicts in index. Try without --index.")"
494                 unstashed_index_tree=$(git write-tree) ||
495                         die "$(gettext "Could not save index tree")"
496                 git reset
497         fi
498
499         if test -n "$u_tree"
500         then
501                 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
502                 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
503                 rm -f "$TMPindex" ||
504                 die 'Could not restore untracked files from stash'
505         fi
506
507         eval "
508                 GITHEAD_$w_tree='Stashed changes' &&
509                 GITHEAD_$c_tree='Updated upstream' &&
510                 GITHEAD_$b_tree='Version stash was based on' &&
511                 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
512         "
513
514         if test -n "$GIT_QUIET"
515         then
516                 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
517         fi
518         if git merge-recursive $b_tree -- $c_tree $w_tree
519         then
520                 # No conflict
521                 if test -n "$unstashed_index_tree"
522                 then
523                         git read-tree "$unstashed_index_tree"
524                 else
525                         a="$TMP-added" &&
526                         git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
527                         git read-tree --reset $c_tree &&
528                         git update-index --add --stdin <"$a" ||
529                                 die "$(gettext "Cannot unstage modified files")"
530                         rm -f "$a"
531                 fi
532                 squelch=
533                 if test -n "$GIT_QUIET"
534                 then
535                         squelch='>/dev/null 2>&1'
536                 fi
537                 (cd "$START_DIR" && eval "git status $squelch") || :
538         else
539                 # Merge conflict; keep the exit status from merge-recursive
540                 status=$?
541                 git rerere
542                 if test -n "$INDEX_OPTION"
543                 then
544                         gettextln "Index was not unstashed." >&2
545                 fi
546                 exit $status
547         fi
548 }
549
550 pop_stash() {
551         assert_stash_ref "$@"
552
553         if apply_stash "$@"
554         then
555                 drop_stash "$@"
556         else
557                 status=$?
558                 say "The stash is kept in case you need it again."
559                 exit $status
560         fi
561 }
562
563 drop_stash () {
564         assert_stash_ref "$@"
565
566         git reflog delete --updateref --rewrite "${REV}" &&
567                 say "$(eval_gettext "Dropped \${REV} (\$s)")" ||
568                 die "$(eval_gettext "\${REV}: Could not drop stash entry")"
569
570         # clear_stash if we just dropped the last stash entry
571         git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
572         clear_stash
573 }
574
575 apply_to_branch () {
576         test -n "$1" || die "$(gettext "No branch name specified")"
577         branch=$1
578         shift 1
579
580         set -- --index "$@"
581         assert_stash_like "$@"
582
583         git checkout -b $branch $REV^ &&
584         apply_stash "$@" && {
585                 test -z "$IS_STASH_REF" || drop_stash "$@"
586         }
587 }
588
589 PARSE_CACHE='--not-parsed'
590 # The default command is "save" if nothing but options are given
591 seen_non_option=
592 for opt
593 do
594         case "$opt" in
595         -*) ;;
596         *) seen_non_option=t; break ;;
597         esac
598 done
599
600 test -n "$seen_non_option" || set "save" "$@"
601
602 # Main command set
603 case "$1" in
604 list)
605         shift
606         list_stash "$@"
607         ;;
608 show)
609         shift
610         show_stash "$@"
611         ;;
612 save)
613         shift
614         save_stash "$@"
615         ;;
616 apply)
617         shift
618         apply_stash "$@"
619         ;;
620 clear)
621         shift
622         clear_stash "$@"
623         ;;
624 create)
625         shift
626         create_stash "$*" && echo "$w_commit"
627         ;;
628 store)
629         shift
630         store_stash "$@"
631         ;;
632 drop)
633         shift
634         drop_stash "$@"
635         ;;
636 pop)
637         shift
638         pop_stash "$@"
639         ;;
640 branch)
641         shift
642         apply_to_branch "$@"
643         ;;
644 *)
645         case $# in
646         0)
647                 save_stash &&
648                 say "$(gettext "(To restore them type \"git stash apply\")")"
649                 ;;
650         *)
651                 usage
652         esac
653         ;;
654 esac