git-stash: remove untracked/ignored directories when stashed
[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 clear"
13
14 SUBDIRECTORY_OK=Yes
15 OPTIONS_SPEC=
16 START_DIR=`pwd`
17 . git-sh-setup
18 require_work_tree
19 cd_to_toplevel
20
21 TMP="$GIT_DIR/.git-stash.$$"
22 TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
23 trap 'rm -f "$TMP-"* "$TMPindex"' 0
24
25 ref_stash=refs/stash
26
27 if git config --get-colorbool color.interactive; then
28        help_color="$(git config --get-color color.interactive.help 'red bold')"
29        reset_color="$(git config --get-color '' reset)"
30 else
31        help_color=
32        reset_color=
33 fi
34
35 no_changes () {
36         git diff-index --quiet --cached HEAD --ignore-submodules -- &&
37         git diff-files --quiet --ignore-submodules &&
38         (test -z "$untracked" || test -z "$(untracked_files)")
39 }
40
41 untracked_files () {
42         excl_opt=--exclude-standard
43         test "$untracked" = "all" && excl_opt=
44         git ls-files -o -z $excl_opt
45 }
46
47 clear_stash () {
48         if test $# != 0
49         then
50                 die "git stash clear with parameters is unimplemented"
51         fi
52         if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
53         then
54                 git update-ref -d $ref_stash $current
55         fi
56 }
57
58 create_stash () {
59         stash_msg="$1"
60         untracked="$2"
61
62         git update-index -q --refresh
63         if no_changes
64         then
65                 exit 0
66         fi
67
68         # state of the base commit
69         if b_commit=$(git rev-parse --verify HEAD)
70         then
71                 head=$(git rev-list --oneline -n 1 HEAD --)
72         else
73                 die "You do not have the initial commit yet"
74         fi
75
76         if branch=$(git symbolic-ref -q HEAD)
77         then
78                 branch=${branch#refs/heads/}
79         else
80                 branch='(no branch)'
81         fi
82         msg=$(printf '%s: %s' "$branch" "$head")
83
84         # state of the index
85         i_tree=$(git write-tree) &&
86         i_commit=$(printf 'index on %s\n' "$msg" |
87                 git commit-tree $i_tree -p $b_commit) ||
88                 die "Cannot save the current index state"
89
90         if test -n "$untracked"
91         then
92                 # Untracked files are stored by themselves in a parentless commit, for
93                 # ease of unpacking later.
94                 u_commit=$(
95                         untracked_files | (
96                                 export GIT_INDEX_FILE="$TMPindex"
97                                 rm -f "$TMPindex" &&
98                                 git update-index -z --add --remove --stdin &&
99                                 u_tree=$(git write-tree) &&
100                                 printf 'untracked files on %s\n' "$msg" | git commit-tree $u_tree  &&
101                                 rm -f "$TMPindex"
102                 ) ) || die "Cannot save the untracked files"
103
104                 untracked_commit_option="-p $u_commit";
105         else
106                 untracked_commit_option=
107         fi
108
109         if test -z "$patch_mode"
110         then
111
112                 # state of the working tree
113                 w_tree=$( (
114                         git read-tree --index-output="$TMPindex" -m $i_tree &&
115                         GIT_INDEX_FILE="$TMPindex" &&
116                         export GIT_INDEX_FILE &&
117                         git diff --name-only -z HEAD | git update-index -z --add --remove --stdin &&
118                         git write-tree &&
119                         rm -f "$TMPindex"
120                 ) ) ||
121                         die "Cannot save the current worktree state"
122
123         else
124
125                 rm -f "$TMP-index" &&
126                 GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
127
128                 # find out what the user wants
129                 GIT_INDEX_FILE="$TMP-index" \
130                         git add--interactive --patch=stash -- &&
131
132                 # state of the working tree
133                 w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
134                 die "Cannot save the current worktree state"
135
136                 git diff-tree -p HEAD $w_tree > "$TMP-patch" &&
137                 test -s "$TMP-patch" ||
138                 die "No changes selected"
139
140                 rm -f "$TMP-index" ||
141                 die "Cannot remove temporary index (can't happen)"
142
143         fi
144
145         # create the stash
146         if test -z "$stash_msg"
147         then
148                 stash_msg=$(printf 'WIP on %s' "$msg")
149         else
150                 stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
151         fi
152         w_commit=$(printf '%s\n' "$stash_msg" |
153                 git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
154                 die "Cannot record working tree state"
155 }
156
157 save_stash () {
158         keep_index=
159         patch_mode=
160         untracked=
161         while test $# != 0
162         do
163                 case "$1" in
164                 -k|--keep-index)
165                         keep_index=t
166                         ;;
167                 --no-keep-index)
168                         keep_index=n
169                         ;;
170                 -p|--patch)
171                         patch_mode=t
172                         # only default to keep if we don't already have an override
173                         test -z "$keep_index" && keep_index=t
174                         ;;
175                 -q|--quiet)
176                         GIT_QUIET=t
177                         ;;
178                 -u|--include-untracked)
179                         untracked=untracked
180                         ;;
181                 -a|--all)
182                         untracked=all
183                         ;;
184                 --)
185                         shift
186                         break
187                         ;;
188                 -*)
189                         echo "error: unknown option for 'stash save': $1"
190                         echo "       To provide a message, use git stash save -- '$1'"
191                         usage
192                         ;;
193                 *)
194                         break
195                         ;;
196                 esac
197                 shift
198         done
199
200         if test -n "$patch_mode" && test -n "$untracked"
201         then
202             die "Can't use --patch and --include-untracked or --all at the same time"
203         fi
204
205         stash_msg="$*"
206
207         git update-index -q --refresh
208         if no_changes
209         then
210                 say 'No local changes to save'
211                 exit 0
212         fi
213         test -f "$GIT_DIR/logs/$ref_stash" ||
214                 clear_stash || die "Cannot initialize stash"
215
216         create_stash "$stash_msg" $untracked
217
218         # Make sure the reflog for stash is kept.
219         : >>"$GIT_DIR/logs/$ref_stash"
220
221         git update-ref -m "$stash_msg" $ref_stash $w_commit ||
222                 die "Cannot save the current status"
223         say Saved working directory and index state "$stash_msg"
224
225         if test -z "$patch_mode"
226         then
227                 git reset --hard ${GIT_QUIET:+-q}
228                 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
229                 if test -n "$untracked"
230                 then
231                         git clean --force --quiet -d $CLEAN_X_OPTION
232                 fi
233
234                 if test "$keep_index" = "t" && test -n $i_tree
235                 then
236                         git read-tree --reset -u $i_tree
237                 fi
238         else
239                 git apply -R < "$TMP-patch" ||
240                 die "Cannot remove worktree changes"
241
242                 if test "$keep_index" != "t"
243                 then
244                         git reset
245                 fi
246         fi
247 }
248
249 have_stash () {
250         git rev-parse --verify $ref_stash >/dev/null 2>&1
251 }
252
253 list_stash () {
254         have_stash || return 0
255         git log --format="%gd: %gs" -g "$@" $ref_stash --
256 }
257
258 show_stash () {
259         assert_stash_like "$@"
260
261         git diff ${FLAGS:---stat} $b_commit $w_commit
262 }
263
264 #
265 # Parses the remaining options looking for flags and
266 # at most one revision defaulting to ${ref_stash}@{0}
267 # if none found.
268 #
269 # Derives related tree and commit objects from the
270 # revision, if one is found.
271 #
272 # stash records the work tree, and is a merge between the
273 # base commit (first parent) and the index tree (second parent).
274 #
275 #   REV is set to the symbolic version of the specified stash-like commit
276 #   IS_STASH_LIKE is non-blank if ${REV} looks like a stash
277 #   IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
278 #   s is set to the SHA1 of the stash commit
279 #   w_commit is set to the commit containing the working tree
280 #   b_commit is set to the base commit
281 #   i_commit is set to the commit containing the index tree
282 #   u_commit is set to the commit containing the untracked files tree
283 #   w_tree is set to the working tree
284 #   b_tree is set to the base tree
285 #   i_tree is set to the index tree
286 #   u_tree is set to the untracked files tree
287 #
288 #   GIT_QUIET is set to t if -q is specified
289 #   INDEX_OPTION is set to --index if --index is specified.
290 #   FLAGS is set to the remaining flags
291 #
292 # dies if:
293 #   * too many revisions specified
294 #   * no revision is specified and there is no stash stack
295 #   * a revision is specified which cannot be resolve to a SHA1
296 #   * a non-existent stash reference is specified
297 #
298
299 parse_flags_and_rev()
300 {
301         test "$PARSE_CACHE" = "$*" && return 0 # optimisation
302         PARSE_CACHE="$*"
303
304         IS_STASH_LIKE=
305         IS_STASH_REF=
306         INDEX_OPTION=
307         s=
308         w_commit=
309         b_commit=
310         i_commit=
311         u_commit=
312         w_tree=
313         b_tree=
314         i_tree=
315         u_tree=
316
317         REV=$(git rev-parse --no-flags --symbolic "$@") || exit 1
318
319         FLAGS=
320         for opt
321         do
322                 case "$opt" in
323                         -q|--quiet)
324                                 GIT_QUIET=-t
325                         ;;
326                         --index)
327                                 INDEX_OPTION=--index
328                         ;;
329                         -*)
330                                 FLAGS="${FLAGS}${FLAGS:+ }$opt"
331                         ;;
332                 esac
333         done
334
335         set -- $REV
336
337         case $# in
338                 0)
339                         have_stash || die "No stash found."
340                         set -- ${ref_stash}@{0}
341                 ;;
342                 1)
343                         :
344                 ;;
345                 *)
346                         die "Too many revisions specified: $REV"
347                 ;;
348         esac
349
350         REV=$(git rev-parse --quiet --symbolic --verify $1 2>/dev/null) || die "$1 is not valid reference"
351
352         i_commit=$(git rev-parse --quiet --verify $REV^2 2>/dev/null) &&
353         set -- $(git rev-parse $REV $REV^1 $REV: $REV^1: $REV^2: 2>/dev/null) &&
354         s=$1 &&
355         w_commit=$1 &&
356         b_commit=$2 &&
357         w_tree=$3 &&
358         b_tree=$4 &&
359         i_tree=$5 &&
360         IS_STASH_LIKE=t &&
361         test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
362         IS_STASH_REF=t
363
364         u_commit=$(git rev-parse --quiet --verify $REV^3 2>/dev/null) &&
365         u_tree=$(git rev-parse $REV^3: 2>/dev/null)
366 }
367
368 is_stash_like()
369 {
370         parse_flags_and_rev "$@"
371         test -n "$IS_STASH_LIKE"
372 }
373
374 assert_stash_like() {
375         is_stash_like "$@" || die "'$*' is not a stash-like commit"
376 }
377
378 is_stash_ref() {
379         is_stash_like "$@" && test -n "$IS_STASH_REF"
380 }
381
382 assert_stash_ref() {
383         is_stash_ref "$@" || die "'$*' is not a stash reference"
384 }
385
386 apply_stash () {
387
388         assert_stash_like "$@"
389
390         git update-index -q --refresh || die 'unable to refresh index'
391
392         # current index state
393         c_tree=$(git write-tree) ||
394                 die 'Cannot apply a stash in the middle of a merge'
395
396         unstashed_index_tree=
397         if test -n "$INDEX_OPTION" && test "$b_tree" != "$i_tree" &&
398                         test "$c_tree" != "$i_tree"
399         then
400                 git diff-tree --binary $s^2^..$s^2 | git apply --cached
401                 test $? -ne 0 &&
402                         die 'Conflicts in index. Try without --index.'
403                 unstashed_index_tree=$(git write-tree) ||
404                         die 'Could not save index tree'
405                 git reset
406         fi
407
408         if test -n "$u_tree"
409         then
410                 GIT_INDEX_FILE="$TMPindex" git-read-tree "$u_tree" &&
411                 GIT_INDEX_FILE="$TMPindex" git checkout-index --all &&
412                 rm -f "$TMPindex" ||
413                 die 'Could not restore untracked files from stash'
414         fi
415
416         eval "
417                 GITHEAD_$w_tree='Stashed changes' &&
418                 GITHEAD_$c_tree='Updated upstream' &&
419                 GITHEAD_$b_tree='Version stash was based on' &&
420                 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
421         "
422
423         if test -n "$GIT_QUIET"
424         then
425                 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
426         fi
427         if git merge-recursive $b_tree -- $c_tree $w_tree
428         then
429                 # No conflict
430                 if test -n "$unstashed_index_tree"
431                 then
432                         git read-tree "$unstashed_index_tree"
433                 else
434                         a="$TMP-added" &&
435                         git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
436                         git read-tree --reset $c_tree &&
437                         git update-index --add --stdin <"$a" ||
438                                 die "Cannot unstage modified files"
439                         rm -f "$a"
440                 fi
441                 squelch=
442                 if test -n "$GIT_QUIET"
443                 then
444                         squelch='>/dev/null 2>&1'
445                 fi
446                 (cd "$START_DIR" && eval "git status $squelch") || :
447         else
448                 # Merge conflict; keep the exit status from merge-recursive
449                 status=$?
450                 if test -n "$INDEX_OPTION"
451                 then
452                         echo >&2 'Index was not unstashed.'
453                 fi
454                 exit $status
455         fi
456 }
457
458 pop_stash() {
459         assert_stash_ref "$@"
460
461         apply_stash "$@" &&
462         drop_stash "$@"
463 }
464
465 drop_stash () {
466         assert_stash_ref "$@"
467
468         git reflog delete --updateref --rewrite "${REV}" &&
469                 say "Dropped ${REV} ($s)" || die "${REV}: Could not drop stash entry"
470
471         # clear_stash if we just dropped the last stash entry
472         git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
473 }
474
475 apply_to_branch () {
476         test -n "$1" || die 'No branch name specified'
477         branch=$1
478         shift 1
479
480         set -- --index "$@"
481         assert_stash_like "$@"
482
483         git checkout -b $branch $REV^ &&
484         apply_stash "$@" && {
485                 test -z "$IS_STASH_REF" || drop_stash "$@"
486         }
487 }
488
489 PARSE_CACHE='--not-parsed'
490 # The default command is "save" if nothing but options are given
491 seen_non_option=
492 for opt
493 do
494         case "$opt" in
495         -*) ;;
496         *) seen_non_option=t; break ;;
497         esac
498 done
499
500 test -n "$seen_non_option" || set "save" "$@"
501
502 # Main command set
503 case "$1" in
504 list)
505         shift
506         list_stash "$@"
507         ;;
508 show)
509         shift
510         show_stash "$@"
511         ;;
512 save)
513         shift
514         save_stash "$@"
515         ;;
516 apply)
517         shift
518         apply_stash "$@"
519         ;;
520 clear)
521         shift
522         clear_stash "$@"
523         ;;
524 create)
525         if test $# -gt 0 && test "$1" = create
526         then
527                 shift
528         fi
529         create_stash "$*" && echo "$w_commit"
530         ;;
531 drop)
532         shift
533         drop_stash "$@"
534         ;;
535 pop)
536         shift
537         pop_stash "$@"
538         ;;
539 branch)
540         shift
541         apply_to_branch "$@"
542         ;;
543 *)
544         case $# in
545         0)
546                 save_stash &&
547                 say '(To restore them type "git stash apply")'
548                 ;;
549         *)
550                 usage
551         esac
552         ;;
553 esac