Skip to content

Commit a736531

Browse files
paskygitster
authored andcommitted
git stash: avoid data loss when "git stash save" kills a directory
"stash save" is about saving the local change to the working tree, but also about restoring the state of the last commit to the working tree. When a local change is to turn a non-directory to a directory, in order to restore the non-directory, everything in the directory needs to be removed. Which is fine when running "git stash save --include-untracked", but without that option, untracked, newly created files in the directory will have to be discarded, if the state you are restoring to has a non-directory at the same path as the directory. Introduce a safety valve to fail the operation in such case, using the "ls-files --killed" which was designed for this exact purpose. The "stash save" is stopped when untracked files need to be discarded because their leading path ceased to be a directory, and the user is required to pass --force to really have the data removed. Signed-off-by: Petr Baudis <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 26c986e commit a736531

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
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

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/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)