Skip to content

Commit d26792a

Browse files
committed
Merge branch 'pb/stash-refuse-to-kill'
"git stash save" is not just about "saving" the local changes, but also is to restore the working tree state to that of HEAD. If you changed a non-directory into a directory in the local change, you may have untracked files in that directory, which have to be killed while doing so, unless you run it with --include-untracked. Teach the command to detect and error out before spreading the damage. This needed a small fix to "ls-files --killed". * pb/stash-refuse-to-kill: git stash: avoid data loss when "git stash save" kills a directory treat_directory(): do not declare submodules to be untracked
2 parents 77f3c3f + a736531 commit d26792a

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

Documentation/git-stash.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ SYNOPSIS
1414
'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
1515
'git stash' branch <branchname> [<stash>]
1616
'git stash' [save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
17-
[-u|--include-untracked] [-a|--all] [<message>]]
17+
[-u|--include-untracked] [-a|--all] [-f|--force]
18+
[<message>]]
1819
'git stash' clear
1920
'git stash' create [<message>]
2021
'git stash' store [-m|--message <message>] [-q|--quiet] <commit>
@@ -44,7 +45,7 @@ is also possible).
4445
OPTIONS
4546
-------
4647

47-
save [-p|--patch] [--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [<message>]::
48+
save [-p|--patch] [--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [-f|--force] [<message>]::
4849

4950
Save your local modifications to a new 'stash', and run `git reset
5051
--hard` to revert them. The <message> part is optional and gives
@@ -71,6 +72,13 @@ linkgit:git-add[1] to learn how to operate the `--patch` mode.
7172
+
7273
The `--patch` option implies `--keep-index`. You can use
7374
`--no-keep-index` to override this.
75+
+
76+
In some cases, saving a stash could mean irretrievably removing some
77+
data - if a directory with untracked files replaces a tracked file of
78+
the same name, the new untracked files are not saved (except in case
79+
of `--include-untracked`) but the original tracked file shall be restored.
80+
By default, `stash save` will abort in such a case; `--force` will allow
81+
it to remove the untracked files.
7482

7583
list [<options>]::
7684

dir.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
10361036
return path_recurse;
10371037

10381038
case index_gitdir:
1039-
if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1040-
return path_none;
1041-
return path_untracked;
1039+
return path_none;
10421040

10431041
case index_nonexistent:
10441042
if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)

git-stash.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ save_stash () {
195195
keep_index=
196196
patch_mode=
197197
untracked=
198+
force=
198199
while test $# != 0
199200
do
200201
case "$1" in
@@ -215,6 +216,9 @@ save_stash () {
215216
-u|--include-untracked)
216217
untracked=untracked
217218
;;
219+
-f|--force)
220+
force=t
221+
;;
218222
-a|--all)
219223
untracked=all
220224
;;
@@ -258,6 +262,14 @@ save_stash () {
258262
say "$(gettext "No local changes to save")"
259263
exit 0
260264
fi
265+
if test -z "$untracked$force" &&
266+
test -n "$(git ls-files --killed | head -n 1)"
267+
then
268+
say "$(gettext "The following untracked files would NOT be saved but need to be removed by stash save:")"
269+
test -n "$GIT_QUIET" || git ls-files --killed | sed 's/^/\t/'
270+
say "$(gettext "Aborting. Consider using either the --force or --include-untracked option.")" >&2
271+
exit 1
272+
fi
261273
test -f "$GIT_DIR/logs/$ref_stash" ||
262274
clear_stash || die "$(gettext "Cannot initialize stash")"
263275

t/t3010-ls-files-killed-modified.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This test prepares the following in the cache:
1111
path1 - a symlink
1212
path2/file2 - a file in a directory
1313
path3/file3 - a file in a directory
14+
submod1/ - a submodule
15+
submod2/ - another submodule
1416
1517
and the following on the filesystem:
1618
@@ -21,9 +23,11 @@ and the following on the filesystem:
2123
path4 - a file
2224
path5 - a symlink
2325
path6/file6 - a file in a directory
26+
submod1/ - a submodule (modified from the cache)
27+
submod2/ - a submodule (matches the cache)
2428
25-
git ls-files -k should report that existing filesystem
26-
objects except path4, path5 and path6/file6 to be killed.
29+
git ls-files -k should report that existing filesystem objects
30+
path0/*, path1/*, path2 and path3 to be killed.
2731
2832
Also for modification test, the cache and working tree have:
2933
@@ -33,7 +37,7 @@ Also for modification test, the cache and working tree have:
3337
path10 - a non-empty file, cache dirtied.
3438
3539
We should report path0, path1, path2/file2, path3/file3, path7 and path8
36-
modified without reporting path9 and path10.
40+
modified without reporting path9 and path10. submod1 is also modified.
3741
'
3842
. ./test-lib.sh
3943

@@ -48,6 +52,18 @@ test_expect_success 'git update-index --add to add various paths.' '
4852
: >path9 &&
4953
date >path10 &&
5054
git update-index --add -- path0 path?/file? path7 path8 path9 path10 &&
55+
for i in 1 2
56+
do
57+
git init submod$i &&
58+
(
59+
cd submod$i && git commit --allow-empty -m "empty $i"
60+
) || break
61+
done &&
62+
git update-index --add submod[12]
63+
(
64+
cd submod1 &&
65+
git commit --allow-empty -m "empty 1 (updated)"
66+
) &&
5167
rm -fr path? # leave path10 alone
5268
'
5369

@@ -94,6 +110,7 @@ test_expect_success 'validate git ls-files -m output.' '
94110
path3/file3
95111
path7
96112
path8
113+
submod1
97114
EOF
98115
test_cmp .expected .output
99116
'

t/t3903-stash.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,22 @@ test_expect_success 'store updates stash ref and reflog' '
673673
grep quux bazzy
674674
'
675675

676+
test_expect_success 'stash a change to turn a non-directory to a directory' '
677+
git reset --hard &&
678+
>testfile &&
679+
git add testfile &&
680+
git commit -m "add testfile as a regular file" &&
681+
rm testfile &&
682+
mkdir testfile &&
683+
>testfile/file &&
684+
test_must_fail git stash save "recover regular file" &&
685+
test -f testfile/file
686+
'
687+
688+
test_expect_success 'stash a change to turn a non-directory to a directory (forced)' '
689+
git stash save --force "recover regular file (forced)" &&
690+
! test -f testfile/file &&
691+
test -f testfile
692+
'
693+
676694
test_done

0 commit comments

Comments
 (0)