]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3905-stash-include-untracked.sh
t3905: remove nested git in command substitution
[thirdparty/git.git] / t / t3905-stash-include-untracked.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2011 David Caldwell
4 #
5
6 test_description='Test git stash --include-untracked'
7
8 . ./test-lib.sh
9
10 test_expect_success 'stash save --include-untracked some dirty working directory' '
11 echo 1 >file &&
12 git add file &&
13 test_tick &&
14 git commit -m initial &&
15 echo 2 >file &&
16 git add file &&
17 echo 3 >file &&
18 test_tick &&
19 echo 1 >file2 &&
20 echo 1 >HEAD &&
21 mkdir untracked &&
22 echo untracked >untracked/untracked &&
23 git stash --include-untracked &&
24 git diff-files --quiet &&
25 git diff-index --cached --quiet HEAD
26 '
27
28 test_expect_success 'stash save --include-untracked cleaned the untracked files' '
29 cat >expect <<-EOF &&
30 ?? actual
31 ?? expect
32 EOF
33
34 git status --porcelain >actual &&
35 test_cmp expect actual
36 '
37
38 test_expect_success 'stash save --include-untracked stashed the untracked files' '
39 one_blob=$(echo 1 | git hash-object --stdin) &&
40 tracked=$(git rev-parse --short "$one_blob") &&
41 untracked_blob=$(echo untracked | git hash-object --stdin) &&
42 untracked=$(git rev-parse --short "$untracked_blob") &&
43 cat >expect.diff <<-EOF &&
44 diff --git a/HEAD b/HEAD
45 new file mode 100644
46 index 0000000..$tracked
47 --- /dev/null
48 +++ b/HEAD
49 @@ -0,0 +1 @@
50 +1
51 diff --git a/file2 b/file2
52 new file mode 100644
53 index 0000000..$tracked
54 --- /dev/null
55 +++ b/file2
56 @@ -0,0 +1 @@
57 +1
58 diff --git a/untracked/untracked b/untracked/untracked
59 new file mode 100644
60 index 0000000..$untracked
61 --- /dev/null
62 +++ b/untracked/untracked
63 @@ -0,0 +1 @@
64 +untracked
65 EOF
66 cat >expect.lstree <<-EOF &&
67 HEAD
68 file2
69 untracked
70 EOF
71
72 test_path_is_missing file2 &&
73 test_path_is_missing untracked &&
74 test_path_is_missing HEAD &&
75 git diff HEAD stash^3 -- HEAD file2 untracked >actual &&
76 test_cmp expect.diff actual &&
77 git ls-tree --name-only stash^3: >actual &&
78 test_cmp expect.lstree actual
79 '
80 test_expect_success 'stash save --patch --include-untracked fails' '
81 test_must_fail git stash --patch --include-untracked
82 '
83
84 test_expect_success 'stash save --patch --all fails' '
85 test_must_fail git stash --patch --all
86 '
87
88 test_expect_success 'clean up untracked/untracked file to prepare for next tests' '
89 git clean --force --quiet
90
91 '
92
93 test_expect_success 'stash pop after save --include-untracked leaves files untracked again' '
94 cat >expect <<-EOF &&
95 M file
96 ?? HEAD
97 ?? actual
98 ?? expect
99 ?? file2
100 ?? untracked/
101 EOF
102
103 git stash pop &&
104 git status --porcelain >actual &&
105 test_cmp expect actual &&
106 test "1" = "$(cat file2)" &&
107 test untracked = "$(cat untracked/untracked)"
108 '
109
110 test_expect_success 'clean up untracked/ directory to prepare for next tests' '
111 git clean --force --quiet -d
112 '
113
114 test_expect_success 'stash save -u dirty index' '
115 echo 4 >file3 &&
116 git add file3 &&
117 test_tick &&
118 git stash -u
119 '
120
121 test_expect_success 'stash save --include-untracked dirty index got stashed' '
122 four_blob=$(echo 4 | git hash-object --stdin) &&
123 blob=$(git rev-parse --short "$four_blob") &&
124 cat >expect <<-EOF &&
125 diff --git a/file3 b/file3
126 new file mode 100644
127 index 0000000..$blob
128 --- /dev/null
129 +++ b/file3
130 @@ -0,0 +1 @@
131 +4
132 EOF
133
134 git stash pop --index &&
135 test_when_finished "git reset" &&
136 git diff --cached >actual &&
137 test_cmp expect actual
138 '
139
140 # Must direct output somewhere where it won't be considered an untracked file
141 test_expect_success 'stash save --include-untracked -q is quiet' '
142 echo 1 >file5 &&
143 git stash save --include-untracked --quiet >.git/stash-output.out 2>&1 &&
144 test_line_count = 0 .git/stash-output.out &&
145 rm -f .git/stash-output.out
146 '
147
148 test_expect_success 'stash save --include-untracked removed files' '
149 rm -f file &&
150 git stash save --include-untracked &&
151 echo 1 >expect &&
152 test_when_finished "rm -f expect" &&
153 test_cmp expect file
154 '
155
156 test_expect_success 'stash save --include-untracked removed files got stashed' '
157 git stash pop &&
158 test_path_is_missing file
159 '
160
161 test_expect_success 'stash save --include-untracked respects .gitignore' '
162 cat >.gitignore <<-EOF &&
163 .gitignore
164 ignored
165 ignored.d/
166 EOF
167
168 echo ignored >ignored &&
169 mkdir ignored.d &&
170 echo ignored >ignored.d/untracked &&
171 git stash -u &&
172 test -s ignored &&
173 test -s ignored.d/untracked &&
174 test -s .gitignore
175 '
176
177 test_expect_success 'stash save -u can stash with only untracked files different' '
178 echo 4 >file4 &&
179 git stash -u &&
180 test_path_is_missing file4
181 '
182
183 test_expect_success 'stash save --all does not respect .gitignore' '
184 git stash -a &&
185 test_path_is_missing ignored &&
186 test_path_is_missing ignored.d &&
187 test_path_is_missing .gitignore
188 '
189
190 test_expect_success 'stash save --all is stash poppable' '
191 git stash pop &&
192 test -s ignored &&
193 test -s ignored.d/untracked &&
194 test -s .gitignore
195 '
196
197 test_expect_success 'stash push --include-untracked with pathspec' '
198 >foo &&
199 >bar &&
200 git stash push --include-untracked -- foo &&
201 test_path_is_file bar &&
202 test_path_is_missing foo &&
203 git stash pop &&
204 test_path_is_file bar &&
205 test_path_is_file foo
206 '
207
208 test_expect_success 'stash push with $IFS character' '
209 >"foo bar" &&
210 >foo &&
211 >bar &&
212 git add foo* &&
213 git stash push --include-untracked -- "foo b*" &&
214 test_path_is_missing "foo bar" &&
215 test_path_is_file foo &&
216 test_path_is_file bar &&
217 git stash pop &&
218 test_path_is_file "foo bar" &&
219 test_path_is_file foo &&
220 test_path_is_file bar
221 '
222
223 test_expect_success 'stash previously ignored file' '
224 cat >.gitignore <<-EOF &&
225 ignored
226 ignored.d/*
227 EOF
228
229 git reset HEAD &&
230 git add .gitignore &&
231 git commit -m "Add .gitignore" &&
232 >ignored.d/foo &&
233 echo "!ignored.d/foo" >>.gitignore &&
234 git stash save --include-untracked &&
235 test_path_is_missing ignored.d/foo &&
236 git stash pop &&
237 test_path_is_file ignored.d/foo
238 '
239
240 test_expect_success 'stash -u -- <untracked> doesnt print error' '
241 >untracked &&
242 git stash push -u -- untracked 2>actual &&
243 test_path_is_missing untracked &&
244 test_line_count = 0 actual
245 '
246
247 test_expect_success 'stash -u -- <untracked> leaves rest of working tree in place' '
248 >tracked &&
249 git add tracked &&
250 >untracked &&
251 git stash push -u -- untracked &&
252 test_path_is_missing untracked &&
253 test_path_is_file tracked
254 '
255
256 test_expect_success 'stash -u -- <tracked> <untracked> clears changes in both' '
257 >tracked &&
258 git add tracked &&
259 >untracked &&
260 git stash push -u -- tracked untracked &&
261 test_path_is_missing tracked &&
262 test_path_is_missing untracked
263 '
264
265 test_expect_success 'stash --all -- <ignored> stashes ignored file' '
266 >ignored.d/bar &&
267 git stash push --all -- ignored.d/bar &&
268 test_path_is_missing ignored.d/bar
269 '
270
271 test_expect_success 'stash --all -- <tracked> <ignored> clears changes in both' '
272 >tracked &&
273 git add tracked &&
274 >ignored.d/bar &&
275 git stash push --all -- tracked ignored.d/bar &&
276 test_path_is_missing tracked &&
277 test_path_is_missing ignored.d/bar
278 '
279
280 test_expect_success 'stash -u -- <ignored> leaves ignored file alone' '
281 >ignored.d/bar &&
282 git stash push -u -- ignored.d/bar &&
283 test_path_is_file ignored.d/bar
284 '
285
286 test_expect_success 'stash -u -- <non-existent> shows no changes when there are none' '
287 git stash push -u -- non-existent >actual &&
288 echo "No local changes to save" >expect &&
289 test_i18ncmp expect actual
290 '
291
292 test_expect_success 'stash -u with globs' '
293 >untracked.txt &&
294 git stash -u -- ":(glob)**/*.txt" &&
295 test_path_is_missing untracked.txt
296 '
297
298 test_done