1#!/bin/sh
2
3test_description='sparse checkout tests
4
5* (tag: removed, master) removed
6| D sub/added
7* (HEAD, tag: top) modified and added
8| M init.t
9| A sub/added
10* (tag: init) init
11 A init.t
12'
13
14. ./test-lib.sh
15. "$TEST_DIRECTORY"/lib-read-tree.sh
16
17test_expect_success 'setup' '
18 test_commit init &&
19 echo modified >>init.t &&
20
21 cat >expected <<-EOF &&
22 100644 $(git hash-object init.t) 0 init.t
23 100644 $EMPTY_BLOB 0 sub/added
24 100644 $EMPTY_BLOB 0 sub/addedtoo
25 100644 $EMPTY_BLOB 0 subsub/added
26 EOF
27 cat >expected.swt <<-\EOF &&
28 H init.t
29 H sub/added
30 H sub/addedtoo
31 H subsub/added
32 EOF
33
34 mkdir sub subsub &&
35 touch sub/added sub/addedtoo subsub/added &&
36 git add init.t sub/added sub/addedtoo subsub/added &&
37 git commit -m "modified and added" &&
38 git tag top &&
39 git rm sub/added &&
40 git commit -m removed &&
41 git tag removed &&
42 git checkout top &&
43 git ls-files --stage >result &&
44 test_cmp expected result
45'
46
47test_expect_success 'read-tree without .git/info/sparse-checkout' '
48 read_tree_u_must_succeed -m -u HEAD &&
49 git ls-files --stage >result &&
50 test_cmp expected result &&
51 git ls-files -t >result &&
52 test_cmp expected.swt result
53'
54
55test_expect_success 'read-tree with .git/info/sparse-checkout but disabled' '
56 echo >.git/info/sparse-checkout &&
57 read_tree_u_must_succeed -m -u HEAD &&
58 git ls-files -t >result &&
59 test_cmp expected.swt result &&
60 test -f init.t &&
61 test -f sub/added
62'
63
64test_expect_success 'read-tree --no-sparse-checkout with empty .git/info/sparse-checkout and enabled' '
65 git config core.sparsecheckout true &&
66 echo >.git/info/sparse-checkout &&
67 read_tree_u_must_succeed --no-sparse-checkout -m -u HEAD &&
68 git ls-files -t >result &&
69 test_cmp expected.swt result &&
70 test -f init.t &&
71 test -f sub/added
72'
73
74test_expect_success 'read-tree with empty .git/info/sparse-checkout' '
75 git config core.sparsecheckout true &&
76 echo >.git/info/sparse-checkout &&
77 read_tree_u_must_fail -m -u HEAD &&
78 git ls-files --stage >result &&
79 test_cmp expected result &&
80 git ls-files -t >result &&
81 test_cmp expected.swt result &&
82 test -f init.t &&
83 test -f sub/added
84'
85
86test_expect_success 'match directories with trailing slash' '
87 cat >expected.swt-noinit <<-\EOF &&
88 S init.t
89 H sub/added
90 H sub/addedtoo
91 S subsub/added
92 EOF
93
94 echo sub/ > .git/info/sparse-checkout &&
95 read_tree_u_must_succeed -m -u HEAD &&
96 git ls-files -t > result &&
97 test_cmp expected.swt-noinit result &&
98 test ! -f init.t &&
99 test -f sub/added
100'
101
102test_expect_success 'match directories without trailing slash' '
103 echo sub >.git/info/sparse-checkout &&
104 read_tree_u_must_succeed -m -u HEAD &&
105 git ls-files -t >result &&
106 test_cmp expected.swt-noinit result &&
107 test ! -f init.t &&
108 test -f sub/added
109'
110
111test_expect_success 'match directories with negated patterns' '
112 cat >expected.swt-negation <<\EOF &&
113S init.t
114S sub/added
115H sub/addedtoo
116S subsub/added
117EOF
118
119 cat >.git/info/sparse-checkout <<\EOF &&
120sub
121!sub/added
122EOF
123 git read-tree -m -u HEAD &&
124 git ls-files -t >result &&
125 test_cmp expected.swt-negation result &&
126 test ! -f init.t &&
127 test ! -f sub/added &&
128 test -f sub/addedtoo
129'
130
131test_expect_success 'match directories with negated patterns (2)' '
132 cat >expected.swt-negation2 <<\EOF &&
133H init.t
134H sub/added
135S sub/addedtoo
136H subsub/added
137EOF
138
139 cat >.git/info/sparse-checkout <<\EOF &&
140/*
141!sub
142sub/added
143EOF
144 git read-tree -m -u HEAD &&
145 git ls-files -t >result &&
146 test_cmp expected.swt-negation2 result &&
147 test -f init.t &&
148 test -f sub/added &&
149 test ! -f sub/addedtoo
150'
151
152test_expect_success 'match directory pattern' '
153 echo "s?b" >.git/info/sparse-checkout &&
154 read_tree_u_must_succeed -m -u HEAD &&
155 git ls-files -t >result &&
156 test_cmp expected.swt-noinit result &&
157 test ! -f init.t &&
158 test -f sub/added
159'
160
161test_expect_success 'checkout area changes' '
162 cat >expected.swt-nosub <<-\EOF &&
163 H init.t
164 S sub/added
165 S sub/addedtoo
166 S subsub/added
167 EOF
168
169 echo init.t >.git/info/sparse-checkout &&
170 read_tree_u_must_succeed -m -u HEAD &&
171 git ls-files -t >result &&
172 test_cmp expected.swt-nosub result &&
173 test -f init.t &&
174 test ! -f sub/added
175'
176
177test_expect_success 'read-tree updates worktree, absent case' '
178 echo sub/added >.git/info/sparse-checkout &&
179 git checkout -f top &&
180 read_tree_u_must_succeed -m -u HEAD^ &&
181 test ! -f init.t
182'
183
184test_expect_success 'read-tree updates worktree, dirty case' '
185 echo sub/added >.git/info/sparse-checkout &&
186 git checkout -f top &&
187 echo dirty >init.t &&
188 read_tree_u_must_succeed -m -u HEAD^ &&
189 grep -q dirty init.t &&
190 rm init.t
191'
192
193test_expect_success 'read-tree removes worktree, dirty case' '
194 echo init.t >.git/info/sparse-checkout &&
195 git checkout -f top &&
196 echo dirty >added &&
197 read_tree_u_must_succeed -m -u HEAD^ &&
198 grep -q dirty added
199'
200
201test_expect_success 'read-tree adds to worktree, absent case' '
202 echo init.t >.git/info/sparse-checkout &&
203 git checkout -f removed &&
204 read_tree_u_must_succeed -u -m HEAD^ &&
205 test ! -f sub/added
206'
207
208test_expect_success 'read-tree adds to worktree, dirty case' '
209 echo init.t >.git/info/sparse-checkout &&
210 git checkout -f removed &&
211 mkdir sub &&
212 echo dirty >sub/added &&
213 read_tree_u_must_succeed -u -m HEAD^ &&
214 grep -q dirty sub/added
215'
216
217test_expect_success 'index removal and worktree narrowing at the same time' '
218 >empty &&
219 echo init.t >.git/info/sparse-checkout &&
220 echo sub/added >>.git/info/sparse-checkout &&
221 git checkout -f top &&
222 echo init.t >.git/info/sparse-checkout &&
223 git checkout removed &&
224 git ls-files sub/added >result &&
225 test ! -f sub/added &&
226 test_cmp empty result
227'
228
229test_expect_success 'read-tree --reset removes outside worktree' '
230 echo init.t >.git/info/sparse-checkout &&
231 git checkout -f top &&
232 git reset --hard removed &&
233 git ls-files sub/added >result &&
234 test_must_be_empty result
235'
236
237test_expect_success 'print errors when failed to update worktree' '
238 echo sub >.git/info/sparse-checkout &&
239 git checkout -f init &&
240 mkdir sub &&
241 touch sub/added sub/addedtoo &&
242 test_must_fail git checkout top 2>actual &&
243 cat >expected <<\EOF &&
244error: The following untracked working tree files would be overwritten by checkout:
245 sub/added
246 sub/addedtoo
247Please move or remove them before you switch branches.
248Aborting
249EOF
250 test_i18ncmp expected actual
251'
252
253test_expect_success 'checkout without --ignore-skip-worktree-bits' '
254 echo "*" >.git/info/sparse-checkout &&
255 git checkout -f top &&
256 test_path_is_file init.t &&
257 echo sub >.git/info/sparse-checkout &&
258 git checkout &&
259 echo modified >> sub/added &&
260 git checkout . &&
261 test_path_is_missing init.t &&
262 git diff --exit-code HEAD
263'
264
265test_expect_success 'checkout with --ignore-skip-worktree-bits' '
266 echo "*" >.git/info/sparse-checkout &&
267 git checkout -f top &&
268 test_path_is_file init.t &&
269 echo sub >.git/info/sparse-checkout &&
270 git checkout &&
271 echo modified >> sub/added &&
272 git checkout --ignore-skip-worktree-bits . &&
273 test_path_is_file init.t &&
274 git diff --exit-code HEAD
275'
276
277test_done