Skip to content

Commit 7474b8b

Browse files
drafnelgitster
authored andcommitted
git-stash: remove untracked/ignored directories when stashed
The two new stash options --include-untracked and --all do not remove the untracked and/or ignored files that are stashed if those files reside in a subdirectory. e.g. the following sequence fails: mkdir untracked && echo hello >untracked/file.txt && git stash --include-untracked && test ! -f untracked/file.txt Within the git-stash script, git-clean is used to remove the untracked/ignored files, but since the -d option was not supplied, it does not remove directories. So, add -d to the git-clean arguments, and update the tests to test this functionality. Reported-by: Hilco Wijbenga <[email protected]> Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c995ef4 commit 7474b8b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

git-stash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ save_stash () {
228228
test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
229229
if test -n "$untracked"
230230
then
231-
git clean --force --quiet $CLEAN_X_OPTION
231+
git clean --force --quiet -d $CLEAN_X_OPTION
232232
fi
233233

234234
if test "$keep_index" = "t" && test -n $i_tree

t/t3905-stash-include-untracked.sh

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ test_expect_success 'stash save --include-untracked some dirty working directory
1717
echo 3 > file &&
1818
test_tick &&
1919
echo 1 > file2 &&
20+
mkdir untracked &&
21+
echo untracked >untracked/untracked &&
2022
git stash --include-untracked &&
2123
git diff-files --quiet &&
2224
git diff-index --cached --quiet HEAD
@@ -40,14 +42,23 @@ index 0000000..d00491f
4042
+++ b/file2
4143
@@ -0,0 +1 @@
4244
+1
45+
diff --git a/untracked/untracked b/untracked/untracked
46+
new file mode 100644
47+
index 0000000..5a72eb2
48+
--- /dev/null
49+
+++ b/untracked/untracked
50+
@@ -0,0 +1 @@
51+
+untracked
4352
EOF
4453
cat > expect.lstree <<EOF
4554
file2
55+
untracked
4656
EOF
4757

4858
test_expect_success 'stash save --include-untracked stashed the untracked files' '
4959
test "!" -f file2 &&
50-
git diff HEAD..stash^3 -- file2 >actual &&
60+
test ! -e untracked &&
61+
git diff HEAD stash^3 -- file2 untracked >actual &&
5162
test_cmp expect.diff actual &&
5263
git ls-tree --name-only stash^3: >actual &&
5364
test_cmp expect.lstree actual
@@ -67,15 +78,18 @@ cat > expect <<EOF
6778
?? actual
6879
?? expect
6980
?? file2
81+
?? untracked/
7082
EOF
7183

7284
test_expect_success 'stash pop after save --include-untracked leaves files untracked again' '
7385
git stash pop &&
7486
git status --porcelain >actual &&
75-
test_cmp expect actual
87+
test_cmp expect actual &&
88+
test "1" = "`cat file2`" &&
89+
test untracked = "`cat untracked/untracked`"
7690
'
7791

78-
git clean --force --quiet
92+
git clean --force --quiet -d
7993

8094
test_expect_success 'stash save -u dirty index' '
8195
echo 4 > file3 &&
@@ -125,12 +139,16 @@ test_expect_success 'stash save --include-untracked removed files got stashed' '
125139
cat > .gitignore <<EOF
126140
.gitignore
127141
ignored
142+
ignored.d/
128143
EOF
129144

130145
test_expect_success 'stash save --include-untracked respects .gitignore' '
131146
echo ignored > ignored &&
147+
mkdir ignored.d &&
148+
echo ignored >ignored.d/untracked &&
132149
git stash -u &&
133150
test -s ignored &&
151+
test -s ignored.d/untracked &&
134152
test -s .gitignore
135153
'
136154

@@ -143,12 +161,14 @@ test_expect_success 'stash save -u can stash with only untracked files different
143161
test_expect_success 'stash save --all does not respect .gitignore' '
144162
git stash -a &&
145163
test "!" -f ignored &&
164+
test "!" -e ignored.d &&
146165
test "!" -f .gitignore
147166
'
148167

149168
test_expect_success 'stash save --all is stash poppable' '
150169
git stash pop &&
151170
test -s ignored &&
171+
test -s ignored.d/untracked &&
152172
test -s .gitignore
153173
'
154174

0 commit comments

Comments
 (0)