@@ -7,7 +7,8 @@ USAGE="list [<options>]
7
7
or: $dashless drop [-q|--quiet] [<stash>]
8
8
or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
9
9
or: $dashless branch <branchname> [<stash>]
10
- or: $dashless [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [<message>]]
10
+ or: $dashless [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
11
+ [-u|--include-untracked] [-a|--all] [<message>]]
11
12
or: $dashless clear"
12
13
13
14
SUBDIRECTORY_OK=Yes
34
35
35
36
no_changes () {
36
37
git diff-index --quiet --cached HEAD --ignore-submodules -- &&
37
- git diff-files --quiet --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
38
46
}
39
47
40
48
clear_stash () {
@@ -50,6 +58,7 @@ clear_stash () {
50
58
51
59
create_stash () {
52
60
stash_msg=" $1 "
61
+ untracked=" $2 "
53
62
54
63
git update-index -q --refresh
55
64
if no_changes
@@ -79,6 +88,25 @@ create_stash () {
79
88
git commit-tree $i_tree -p $b_commit ) ||
80
89
die " $( gettext " Cannot save the current index state" ) "
81
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
+ export GIT_INDEX_FILE=" $TMPindex "
98
+ rm -f " $TMPindex " &&
99
+ git update-index -z --add --remove --stdin &&
100
+ u_tree=$( git write-tree) &&
101
+ printf ' untracked files on %s\n' " $msg " | git commit-tree $u_tree &&
102
+ rm -f " $TMPindex "
103
+ ) ) || die " Cannot save the untracked files"
104
+
105
+ untracked_commit_option=" -p $u_commit " ;
106
+ else
107
+ untracked_commit_option=
108
+ fi
109
+
82
110
if test -z " $patch_mode "
83
111
then
84
112
@@ -123,13 +151,14 @@ create_stash () {
123
151
stash_msg=$( printf ' On %s: %s' " $branch " " $stash_msg " )
124
152
fi
125
153
w_commit=$( printf ' %s\n' " $stash_msg " |
126
- git commit-tree $w_tree -p $b_commit -p $i_commit ) ||
127
- die " $( gettext " Cannot record working tree state" ) "
154
+ git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option ) ||
155
+ die " $( gettext " Cannot record working tree state" ) "
128
156
}
129
157
130
158
save_stash () {
131
159
keep_index=
132
160
patch_mode=
161
+ untracked=
133
162
while test $# ! = 0
134
163
do
135
164
case " $1 " in
@@ -147,6 +176,12 @@ save_stash () {
147
176
-q|--quiet)
148
177
GIT_QUIET=t
149
178
;;
179
+ -u|--include-untracked)
180
+ untracked=untracked
181
+ ;;
182
+ -a|--all)
183
+ untracked=all
184
+ ;;
150
185
--)
151
186
shift
152
187
break
@@ -174,6 +209,11 @@ save_stash () {
174
209
shift
175
210
done
176
211
212
+ if test -n " $patch_mode " && test -n " $untracked "
213
+ then
214
+ die " Can't use --patch and ---include-untracked or --all at the same time"
215
+ fi
216
+
177
217
stash_msg=" $* "
178
218
179
219
git update-index -q --refresh
@@ -185,7 +225,7 @@ save_stash () {
185
225
test -f " $GIT_DIR /logs/$ref_stash " ||
186
226
clear_stash || die " $( gettext " Cannot initialize stash" ) "
187
227
188
- create_stash " $stash_msg "
228
+ create_stash " $stash_msg " $untracked
189
229
190
230
# Make sure the reflog for stash is kept.
191
231
: >> " $GIT_DIR /logs/$ref_stash "
@@ -197,6 +237,11 @@ save_stash () {
197
237
if test -z " $patch_mode "
198
238
then
199
239
git reset --hard ${GIT_QUIET: +-q}
240
+ test " $untracked " = " all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
241
+ if test -n " $untracked "
242
+ then
243
+ git clean --force --quiet $CLEAN_X_OPTION
244
+ fi
200
245
201
246
if test " $keep_index " = " t" && test -n $i_tree
202
247
then
@@ -246,9 +291,11 @@ show_stash () {
246
291
# w_commit is set to the commit containing the working tree
247
292
# b_commit is set to the base commit
248
293
# i_commit is set to the commit containing the index tree
294
+ # u_commit is set to the commit containing the untracked files tree
249
295
# w_tree is set to the working tree
250
296
# b_tree is set to the base tree
251
297
# i_tree is set to the index tree
298
+ # u_tree is set to the untracked files tree
252
299
#
253
300
# GIT_QUIET is set to t if -q is specified
254
301
# INDEX_OPTION is set to --index if --index is specified.
@@ -273,9 +320,11 @@ parse_flags_and_rev()
273
320
w_commit=
274
321
b_commit=
275
322
i_commit=
323
+ u_commit=
276
324
w_tree=
277
325
b_tree=
278
326
i_tree=
327
+ u_tree=
279
328
280
329
REV=$( git rev-parse --no-flags --symbolic " $@ " ) || exit 1
281
330
@@ -326,6 +375,9 @@ parse_flags_and_rev()
326
375
IS_STASH_LIKE=t &&
327
376
test " $ref_stash " = " $( git rev-parse --symbolic-full-name " ${REV%@* } " ) " &&
328
377
IS_STASH_REF=t
378
+
379
+ u_commit=$( git rev-parse --quiet --verify $REV ^3 2> /dev/null) &&
380
+ u_tree=$( git rev-parse $REV ^3: 2> /dev/null)
329
381
}
330
382
331
383
is_stash_like ()
@@ -374,6 +426,14 @@ apply_stash () {
374
426
git reset
375
427
fi
376
428
429
+ if test -n " $u_tree "
430
+ then
431
+ GIT_INDEX_FILE=" $TMPindex " git-read-tree " $u_tree " &&
432
+ GIT_INDEX_FILE=" $TMPindex " git checkout-index --all &&
433
+ rm -f " $TMPindex " ||
434
+ die ' Could not restore untracked files from stash'
435
+ fi
436
+
377
437
eval "
378
438
GITHEAD_$w_tree ='Stashed changes' &&
379
439
GITHEAD_$c_tree ='Updated upstream' &&
0 commit comments