sparse-checkout: check for dirty status
[git] / t / t1091-sparse-checkout-builtin.sh
1 #!/bin/sh
2
3 test_description='sparse checkout builtin tests'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup' '
8         git init repo &&
9         (
10                 cd repo &&
11                 echo "initial" >a &&
12                 mkdir folder1 folder2 deep &&
13                 mkdir deep/deeper1 deep/deeper2 &&
14                 mkdir deep/deeper1/deepest &&
15                 cp a folder1 &&
16                 cp a folder2 &&
17                 cp a deep &&
18                 cp a deep/deeper1 &&
19                 cp a deep/deeper2 &&
20                 cp a deep/deeper1/deepest &&
21                 git add . &&
22                 git commit -m "initial commit"
23         )
24 '
25
26 test_expect_success 'git sparse-checkout list (empty)' '
27         git -C repo sparse-checkout list >list 2>err &&
28         test_must_be_empty list &&
29         test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err
30 '
31
32 test_expect_success 'git sparse-checkout list (populated)' '
33         test_when_finished rm -f repo/.git/info/sparse-checkout &&
34         cat >repo/.git/info/sparse-checkout <<-EOF &&
35                 /folder1/*
36                 /deep/
37                 **/a
38                 !*bin*
39         EOF
40         cp repo/.git/info/sparse-checkout expect &&
41         git -C repo sparse-checkout list >list &&
42         test_cmp expect list
43 '
44
45 test_expect_success 'git sparse-checkout init' '
46         git -C repo sparse-checkout init &&
47         cat >expect <<-EOF &&
48                 /*
49                 !/*/
50         EOF
51         test_cmp expect repo/.git/info/sparse-checkout &&
52         test_cmp_config -C repo true core.sparsecheckout &&
53         ls repo >dir  &&
54         echo a >expect &&
55         test_cmp expect dir
56 '
57
58 test_expect_success 'git sparse-checkout list after init' '
59         git -C repo sparse-checkout list >actual &&
60         cat >expect <<-EOF &&
61                 /*
62                 !/*/
63         EOF
64         test_cmp expect actual
65 '
66
67 test_expect_success 'init with existing sparse-checkout' '
68         echo "*folder*" >> repo/.git/info/sparse-checkout &&
69         git -C repo sparse-checkout init &&
70         cat >expect <<-EOF &&
71                 /*
72                 !/*/
73                 *folder*
74         EOF
75         test_cmp expect repo/.git/info/sparse-checkout &&
76         ls repo >dir  &&
77         cat >expect <<-EOF &&
78                 a
79                 folder1
80                 folder2
81         EOF
82         test_cmp expect dir
83 '
84
85 test_expect_success 'clone --sparse' '
86         git clone --sparse repo clone &&
87         git -C clone sparse-checkout list >actual &&
88         cat >expect <<-EOF &&
89                 /*
90                 !/*/
91         EOF
92         test_cmp expect actual &&
93         ls clone >dir &&
94         echo a >expect &&
95         test_cmp expect dir
96 '
97
98 test_expect_success 'set enables config' '
99         git init empty-config &&
100         (
101                 cd empty-config &&
102                 test_commit test file &&
103                 test_path_is_missing .git/config.worktree &&
104                 test_must_fail git sparse-checkout set nothing &&
105                 test_path_is_file .git/config.worktree &&
106                 test_must_fail git config core.sparseCheckout &&
107                 git sparse-checkout set "/*" &&
108                 test_cmp_config true core.sparseCheckout
109         )
110 '
111
112 test_expect_success 'set sparse-checkout using builtin' '
113         git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
114         cat >expect <<-EOF &&
115                 /*
116                 !/*/
117                 *folder*
118         EOF
119         git -C repo sparse-checkout list >actual &&
120         test_cmp expect actual &&
121         test_cmp expect repo/.git/info/sparse-checkout &&
122         ls repo >dir  &&
123         cat >expect <<-EOF &&
124                 a
125                 folder1
126                 folder2
127         EOF
128         test_cmp expect dir
129 '
130
131 test_expect_success 'set sparse-checkout using --stdin' '
132         cat >expect <<-EOF &&
133                 /*
134                 !/*/
135                 /folder1/
136                 /folder2/
137         EOF
138         git -C repo sparse-checkout set --stdin <expect &&
139         git -C repo sparse-checkout list >actual &&
140         test_cmp expect actual &&
141         test_cmp expect repo/.git/info/sparse-checkout &&
142         ls repo >dir  &&
143         cat >expect <<-EOF &&
144                 a
145                 folder1
146                 folder2
147         EOF
148         test_cmp expect dir
149 '
150
151 test_expect_success 'cone mode: match patterns' '
152         git -C repo config --worktree core.sparseCheckoutCone true &&
153         rm -rf repo/a repo/folder1 repo/folder2 &&
154         git -C repo read-tree -mu HEAD 2>err &&
155         test_i18ngrep ! "disabling cone patterns" err &&
156         git -C repo reset --hard &&
157         ls repo >dir  &&
158         cat >expect <<-EOF &&
159                 a
160                 folder1
161                 folder2
162         EOF
163         test_cmp expect dir
164 '
165
166 test_expect_success 'cone mode: warn on bad pattern' '
167         test_when_finished mv sparse-checkout repo/.git/info/ &&
168         cp repo/.git/info/sparse-checkout . &&
169         echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
170         git -C repo read-tree -mu HEAD 2>err &&
171         test_i18ngrep "unrecognized negative pattern" err
172 '
173
174 test_expect_success 'sparse-checkout disable' '
175         test_when_finished rm -rf repo/.git/info/sparse-checkout &&
176         git -C repo sparse-checkout disable &&
177         test_path_is_file repo/.git/info/sparse-checkout &&
178         git -C repo config --list >config &&
179         test_must_fail git config core.sparseCheckout &&
180         ls repo >dir &&
181         cat >expect <<-EOF &&
182                 a
183                 deep
184                 folder1
185                 folder2
186         EOF
187         test_cmp expect dir
188 '
189
190 test_expect_success 'cone mode: init and set' '
191         git -C repo sparse-checkout init --cone &&
192         git -C repo config --list >config &&
193         test_i18ngrep "core.sparsecheckoutcone=true" config &&
194         ls repo >dir  &&
195         echo a >expect &&
196         test_cmp expect dir &&
197         git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
198         test_must_be_empty err &&
199         ls repo >dir  &&
200         cat >expect <<-EOF &&
201                 a
202                 deep
203         EOF
204         test_cmp expect dir &&
205         ls repo/deep >dir  &&
206         cat >expect <<-EOF &&
207                 a
208                 deeper1
209         EOF
210         test_cmp expect dir &&
211         ls repo/deep/deeper1 >dir  &&
212         cat >expect <<-EOF &&
213                 a
214                 deepest
215         EOF
216         test_cmp expect dir &&
217         cat >expect <<-EOF &&
218                 /*
219                 !/*/
220                 /deep/
221                 !/deep/*/
222                 /deep/deeper1/
223                 !/deep/deeper1/*/
224                 /deep/deeper1/deepest/
225         EOF
226         test_cmp expect repo/.git/info/sparse-checkout &&
227         git -C repo sparse-checkout set --stdin 2>err <<-EOF &&
228                 folder1
229                 folder2
230         EOF
231         test_must_be_empty err &&
232         cat >expect <<-EOF &&
233                 a
234                 folder1
235                 folder2
236         EOF
237         ls repo >dir &&
238         test_cmp expect dir
239 '
240
241 test_expect_success 'cone mode: set with nested folders' '
242         git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
243         test_line_count = 0 err &&
244         cat >expect <<-EOF &&
245                 /*
246                 !/*/
247                 /deep/
248         EOF
249         test_cmp repo/.git/info/sparse-checkout expect
250 '
251
252 test_expect_success 'revert to old sparse-checkout on bad update' '
253         test_when_finished git -C repo reset --hard &&
254         echo update >repo/deep/deeper2/a &&
255         cp repo/.git/info/sparse-checkout expect &&
256         test_must_fail git -C repo sparse-checkout set deep/deeper1 2>err &&
257         test_i18ngrep "cannot set sparse-checkout patterns" err &&
258         test_cmp repo/.git/info/sparse-checkout expect &&
259         ls repo/deep >dir &&
260         cat >expect <<-EOF &&
261                 a
262                 deeper1
263                 deeper2
264         EOF
265         test_cmp dir expect
266 '
267
268 test_expect_success 'revert to old sparse-checkout on empty update' '
269         git init empty-test &&
270         (
271                 echo >file &&
272                 git add file &&
273                 git commit -m "test" &&
274                 test_must_fail git sparse-checkout set nothing 2>err &&
275                 test_i18ngrep "Sparse checkout leaves no entry on working directory" err &&
276                 test_i18ngrep ! ".git/index.lock" err &&
277                 git sparse-checkout set file
278         )
279 '
280
281 test_expect_success 'fail when lock is taken' '
282         test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
283         touch repo/.git/info/sparse-checkout.lock &&
284         test_must_fail git -C repo sparse-checkout set deep 2>err &&
285         test_i18ngrep "File exists" err
286 '
287
288 test_expect_success '.gitignore should not warn about cone mode' '
289         git -C repo config --worktree core.sparseCheckoutCone true &&
290         echo "**/bin/*" >repo/.gitignore &&
291         git -C repo reset --hard 2>err &&
292         test_i18ngrep ! "disabling cone patterns" err
293 '
294
295 test_expect_success 'sparse-checkout (init|set|disable) fails with dirty status' '
296         git clone repo dirty &&
297         echo dirty >dirty/folder1/a &&
298         test_must_fail git -C dirty sparse-checkout init &&
299         test_must_fail git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
300         test_must_fail git -C dirty sparse-checkout disable &&
301         git -C dirty reset --hard &&
302         git -C dirty sparse-checkout init &&
303         git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
304         git -C dirty sparse-checkout disable
305 '
306
307 test_done