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