]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1092-sparse-checkout-compatibility.sh
2394c36d881106f410df28bca6c2ce0cd65b6bb1
[thirdparty/git.git] / t / t1092-sparse-checkout-compatibility.sh
1 #!/bin/sh
2
3 test_description='compare full workdir to sparse workdir'
4
5 GIT_TEST_SPLIT_INDEX=0
6 GIT_TEST_SPARSE_INDEX=
7
8 . ./test-lib.sh
9
10 test_expect_success 'setup' '
11 git init initial-repo &&
12 (
13 GIT_TEST_SPARSE_INDEX=0 &&
14 cd initial-repo &&
15 echo a >a &&
16 echo "after deep" >e &&
17 echo "after folder1" >g &&
18 echo "after x" >z &&
19 mkdir folder1 folder2 deep x &&
20 mkdir deep/deeper1 deep/deeper2 deep/before deep/later &&
21 mkdir deep/deeper1/deepest &&
22 echo "after deeper1" >deep/e &&
23 echo "after deepest" >deep/deeper1/e &&
24 cp a folder1 &&
25 cp a folder2 &&
26 cp a x &&
27 cp a deep &&
28 cp a deep/before &&
29 cp a deep/deeper1 &&
30 cp a deep/deeper2 &&
31 cp a deep/later &&
32 cp a deep/deeper1/deepest &&
33 cp -r deep/deeper1/deepest deep/deeper2 &&
34 mkdir deep/deeper1/0 &&
35 mkdir deep/deeper1/0/0 &&
36 touch deep/deeper1/0/1 &&
37 touch deep/deeper1/0/0/0 &&
38 >folder1- &&
39 >folder1.x &&
40 >folder10 &&
41 cp -r deep/deeper1/0 folder1 &&
42 cp -r deep/deeper1/0 folder2 &&
43 echo >>folder1/0/0/0 &&
44 echo >>folder2/0/1 &&
45 git add . &&
46 git commit -m "initial commit" &&
47 git checkout -b base &&
48 for dir in folder1 folder2 deep
49 do
50 git checkout -b update-$dir &&
51 echo "updated $dir" >$dir/a &&
52 git commit -a -m "update $dir" || return 1
53 done &&
54
55 git checkout -b rename-base base &&
56 cat >folder1/larger-content <<-\EOF &&
57 matching
58 lines
59 help
60 inexact
61 renames
62 EOF
63 cp folder1/larger-content folder2/ &&
64 cp folder1/larger-content deep/deeper1/ &&
65 git add . &&
66 git commit -m "add interesting rename content" &&
67
68 git checkout -b rename-out-to-out rename-base &&
69 mv folder1/a folder2/b &&
70 mv folder1/larger-content folder2/edited-content &&
71 echo >>folder2/edited-content &&
72 echo >>folder2/0/1 &&
73 echo stuff >>deep/deeper1/a &&
74 git add . &&
75 git commit -m "rename folder1/... to folder2/..." &&
76
77 git checkout -b rename-out-to-in rename-base &&
78 mv folder1/a deep/deeper1/b &&
79 echo more stuff >>deep/deeper1/a &&
80 rm folder2/0/1 &&
81 mkdir folder2/0/1 &&
82 echo >>folder2/0/1/1 &&
83 mv folder1/larger-content deep/deeper1/edited-content &&
84 echo >>deep/deeper1/edited-content &&
85 git add . &&
86 git commit -m "rename folder1/... to deep/deeper1/..." &&
87
88 git checkout -b rename-in-to-out rename-base &&
89 mv deep/deeper1/a folder1/b &&
90 echo >>folder2/0/1 &&
91 rm -rf folder1/0/0 &&
92 echo >>folder1/0/0 &&
93 mv deep/deeper1/larger-content folder1/edited-content &&
94 echo >>folder1/edited-content &&
95 git add . &&
96 git commit -m "rename deep/deeper1/... to folder1/..." &&
97
98 git checkout -b deepest base &&
99 echo "updated deepest" >deep/deeper1/deepest/a &&
100 git commit -a -m "update deepest" &&
101
102 git checkout -f base &&
103 git reset --hard
104 )
105 '
106
107 init_repos () {
108 rm -rf full-checkout sparse-checkout sparse-index &&
109
110 # create repos in initial state
111 cp -r initial-repo full-checkout &&
112 git -C full-checkout reset --hard &&
113
114 cp -r initial-repo sparse-checkout &&
115 git -C sparse-checkout reset --hard &&
116
117 cp -r initial-repo sparse-index &&
118 git -C sparse-index reset --hard &&
119
120 # initialize sparse-checkout definitions
121 git -C sparse-checkout sparse-checkout init --cone &&
122 git -C sparse-checkout sparse-checkout set deep &&
123 git -C sparse-index sparse-checkout init --cone --sparse-index &&
124 test_cmp_config -C sparse-index true index.sparse &&
125 git -C sparse-index sparse-checkout set deep
126 }
127
128 run_on_sparse () {
129 (
130 cd sparse-checkout &&
131 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-checkout-out 2>../sparse-checkout-err
132 ) &&
133 (
134 cd sparse-index &&
135 GIT_PROGRESS_DELAY=100000 "$@" >../sparse-index-out 2>../sparse-index-err
136 )
137 }
138
139 run_on_all () {
140 (
141 cd full-checkout &&
142 GIT_PROGRESS_DELAY=100000 "$@" >../full-checkout-out 2>../full-checkout-err
143 ) &&
144 run_on_sparse "$@"
145 }
146
147 test_all_match () {
148 run_on_all "$@" &&
149 test_cmp full-checkout-out sparse-checkout-out &&
150 test_cmp full-checkout-out sparse-index-out &&
151 test_cmp full-checkout-err sparse-checkout-err &&
152 test_cmp full-checkout-err sparse-index-err
153 }
154
155 test_sparse_match () {
156 run_on_sparse "$@" &&
157 test_cmp sparse-checkout-out sparse-index-out &&
158 test_cmp sparse-checkout-err sparse-index-err
159 }
160
161 test_expect_success 'sparse-index contents' '
162 init_repos &&
163
164 test-tool -C sparse-index read-cache --table >cache &&
165 for dir in folder1 folder2 x
166 do
167 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
168 grep "040000 tree $TREE $dir/" cache \
169 || return 1
170 done &&
171
172 git -C sparse-index sparse-checkout set folder1 &&
173
174 test-tool -C sparse-index read-cache --table >cache &&
175 for dir in deep folder2 x
176 do
177 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
178 grep "040000 tree $TREE $dir/" cache \
179 || return 1
180 done &&
181
182 git -C sparse-index sparse-checkout set deep/deeper1 &&
183
184 test-tool -C sparse-index read-cache --table >cache &&
185 for dir in deep/deeper2 folder1 folder2 x
186 do
187 TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
188 grep "040000 tree $TREE $dir/" cache \
189 || return 1
190 done &&
191
192 # Disabling the sparse-index removes tree entries with full ones
193 git -C sparse-index sparse-checkout init --no-sparse-index &&
194
195 test-tool -C sparse-index read-cache --table >cache &&
196 ! grep "040000 tree" cache &&
197 test_sparse_match test-tool read-cache --table
198 '
199
200 test_expect_success 'expanded in-memory index matches full index' '
201 init_repos &&
202 test_sparse_match test-tool read-cache --expand --table
203 '
204
205 test_expect_success 'status with options' '
206 init_repos &&
207 test_sparse_match ls &&
208 test_all_match git status --porcelain=v2 &&
209 test_all_match git status --porcelain=v2 -z -u &&
210 test_all_match git status --porcelain=v2 -uno &&
211 run_on_all touch README.md &&
212 test_all_match git status --porcelain=v2 &&
213 test_all_match git status --porcelain=v2 -z -u &&
214 test_all_match git status --porcelain=v2 -uno &&
215 test_all_match git add README.md &&
216 test_all_match git status --porcelain=v2 &&
217 test_all_match git status --porcelain=v2 -z -u &&
218 test_all_match git status --porcelain=v2 -uno
219 '
220
221 test_expect_success 'status reports sparse-checkout' '
222 init_repos &&
223 git -C sparse-checkout status >full &&
224 git -C sparse-index status >sparse &&
225 test_i18ngrep "You are in a sparse checkout with " full &&
226 test_i18ngrep "You are in a sparse checkout." sparse
227 '
228
229 test_expect_success 'add, commit, checkout' '
230 init_repos &&
231
232 write_script edit-contents <<-\EOF &&
233 echo text >>$1
234 EOF
235 run_on_all ../edit-contents README.md &&
236
237 test_all_match git add README.md &&
238 test_all_match git status --porcelain=v2 &&
239 test_all_match git commit -m "Add README.md" &&
240
241 test_all_match git checkout HEAD~1 &&
242 test_all_match git checkout - &&
243
244 run_on_all ../edit-contents README.md &&
245
246 test_all_match git add -A &&
247 test_all_match git status --porcelain=v2 &&
248 test_all_match git commit -m "Extend README.md" &&
249
250 test_all_match git checkout HEAD~1 &&
251 test_all_match git checkout - &&
252
253 run_on_all ../edit-contents deep/newfile &&
254
255 test_all_match git status --porcelain=v2 -uno &&
256 test_all_match git status --porcelain=v2 &&
257 test_all_match git add . &&
258 test_all_match git status --porcelain=v2 &&
259 test_all_match git commit -m "add deep/newfile" &&
260
261 test_all_match git checkout HEAD~1 &&
262 test_all_match git checkout -
263 '
264
265 test_expect_success 'status/add: outside sparse cone' '
266 init_repos &&
267
268 # adding a "missing" file outside the cone should fail
269 test_sparse_match test_must_fail git add folder1/a &&
270
271 # folder1 is at HEAD, but outside the sparse cone
272 run_on_sparse mkdir folder1 &&
273 cp initial-repo/folder1/a sparse-checkout/folder1/a &&
274 cp initial-repo/folder1/a sparse-index/folder1/a &&
275
276 test_sparse_match git status &&
277
278 write_script edit-contents <<-\EOF &&
279 echo text >>$1
280 EOF
281 run_on_sparse ../edit-contents folder1/a &&
282 run_on_all ../edit-contents folder1/new &&
283
284 test_sparse_match git status --porcelain=v2 &&
285
286 # This "git add folder1/a" fails with a warning
287 # in the sparse repos, differing from the full
288 # repo. This is intentional.
289 test_sparse_match test_must_fail git add folder1/a &&
290 test_sparse_match test_must_fail git add --refresh folder1/a &&
291 test_all_match git status --porcelain=v2 &&
292
293 test_all_match git add . &&
294 test_all_match git status --porcelain=v2 &&
295 test_all_match git commit -m folder1/new &&
296
297 run_on_all ../edit-contents folder1/newer &&
298 test_all_match git add folder1/ &&
299 test_all_match git status --porcelain=v2 &&
300 test_all_match git commit -m folder1/newer
301 '
302
303 test_expect_success 'checkout and reset --hard' '
304 init_repos &&
305
306 test_all_match git checkout update-folder1 &&
307 test_all_match git status --porcelain=v2 &&
308
309 test_all_match git checkout update-deep &&
310 test_all_match git status --porcelain=v2 &&
311
312 test_all_match git checkout -b reset-test &&
313 test_all_match git reset --hard deepest &&
314 test_all_match git reset --hard update-folder1 &&
315 test_all_match git reset --hard update-folder2
316 '
317
318 test_expect_success 'diff --staged' '
319 init_repos &&
320
321 write_script edit-contents <<-\EOF &&
322 echo text >>README.md
323 EOF
324 run_on_all ../edit-contents &&
325
326 test_all_match git diff &&
327 test_all_match git diff --staged &&
328 test_all_match git add README.md &&
329 test_all_match git diff &&
330 test_all_match git diff --staged
331 '
332
333 test_expect_success 'diff with renames and conflicts' '
334 init_repos &&
335
336 for branch in rename-out-to-out rename-out-to-in rename-in-to-out
337 do
338 test_all_match git checkout rename-base &&
339 test_all_match git checkout $branch -- . &&
340 test_all_match git status --porcelain=v2 &&
341 test_all_match git diff --staged --no-renames &&
342 test_all_match git diff --staged --find-renames || return 1
343 done
344 '
345
346 test_expect_success 'diff with directory/file conflicts' '
347 init_repos &&
348
349 for branch in rename-out-to-out rename-out-to-in rename-in-to-out
350 do
351 git -C full-checkout reset --hard &&
352 test_sparse_match git reset --hard &&
353 test_all_match git checkout $branch &&
354 test_all_match git checkout rename-base -- . &&
355 test_all_match git status --porcelain=v2 &&
356 test_all_match git diff --staged --no-renames &&
357 test_all_match git diff --staged --find-renames || return 1
358 done
359 '
360
361 test_expect_success 'log with pathspec outside sparse definition' '
362 init_repos &&
363
364 test_all_match git log -- a &&
365 test_all_match git log -- folder1/a &&
366 test_all_match git log -- folder2/a &&
367 test_all_match git log -- deep/a &&
368 test_all_match git log -- deep/deeper1/a &&
369 test_all_match git log -- deep/deeper1/deepest/a &&
370
371 test_all_match git checkout update-folder1 &&
372 test_all_match git log -- folder1/a
373 '
374
375 test_expect_success 'blame with pathspec inside sparse definition' '
376 init_repos &&
377
378 test_all_match git blame a &&
379 test_all_match git blame deep/a &&
380 test_all_match git blame deep/deeper1/a &&
381 test_all_match git blame deep/deeper1/deepest/a
382 '
383
384 # TODO: blame currently does not support blaming files outside of the
385 # sparse definition. It complains that the file doesn't exist locally.
386 test_expect_failure 'blame with pathspec outside sparse definition' '
387 init_repos &&
388
389 test_all_match git blame folder1/a &&
390 test_all_match git blame folder2/a &&
391 test_all_match git blame deep/deeper2/a &&
392 test_all_match git blame deep/deeper2/deepest/a
393 '
394
395 # TODO: reset currently does not behave as expected when in a
396 # sparse-checkout.
397 test_expect_failure 'checkout and reset (mixed)' '
398 init_repos &&
399
400 test_all_match git checkout -b reset-test update-deep &&
401 test_all_match git reset deepest &&
402 test_all_match git reset update-folder1 &&
403 test_all_match git reset update-folder2
404 '
405
406 # Ensure that sparse-index behaves identically to
407 # sparse-checkout with a full index.
408 test_expect_success 'checkout and reset (mixed) [sparse]' '
409 init_repos &&
410
411 test_sparse_match git checkout -b reset-test update-deep &&
412 test_sparse_match git reset deepest &&
413 test_sparse_match git reset update-folder1 &&
414 test_sparse_match git reset update-folder2
415 '
416
417 test_expect_success 'merge' '
418 init_repos &&
419
420 test_all_match git checkout -b merge update-deep &&
421 test_all_match git merge -m "folder1" update-folder1 &&
422 test_all_match git rev-parse HEAD^{tree} &&
423 test_all_match git merge -m "folder2" update-folder2 &&
424 test_all_match git rev-parse HEAD^{tree}
425 '
426
427 test_expect_success 'merge with outside renames' '
428 init_repos &&
429
430 for type in out-to-out out-to-in in-to-out
431 do
432 test_all_match git reset --hard &&
433 test_all_match git checkout -f -b merge-$type update-deep &&
434 test_all_match git merge -m "$type" rename-$type &&
435 test_all_match git rev-parse HEAD^{tree} || return 1
436 done
437 '
438
439 # Sparse-index fails to convert the index in the
440 # final 'git cherry-pick' command.
441 test_expect_success 'cherry-pick with conflicts' '
442 init_repos &&
443
444 write_script edit-conflict <<-\EOF &&
445 echo $1 >conflict
446 EOF
447
448 test_all_match git checkout -b to-cherry-pick &&
449 run_on_all ../edit-conflict ABC &&
450 test_all_match git add conflict &&
451 test_all_match git commit -m "conflict to pick" &&
452
453 test_all_match git checkout -B base HEAD~1 &&
454 run_on_all ../edit-conflict DEF &&
455 test_all_match git add conflict &&
456 test_all_match git commit -m "conflict in base" &&
457
458 test_all_match test_must_fail git cherry-pick to-cherry-pick
459 '
460
461 test_expect_success 'clean' '
462 init_repos &&
463
464 echo bogus >>.gitignore &&
465 run_on_all cp ../.gitignore . &&
466 test_all_match git add .gitignore &&
467 test_all_match git commit -m "ignore bogus files" &&
468
469 run_on_sparse mkdir folder1 &&
470 run_on_all touch folder1/bogus &&
471
472 test_all_match git status --porcelain=v2 &&
473 test_all_match git clean -f &&
474 test_all_match git status --porcelain=v2 &&
475 test_sparse_match ls &&
476 test_sparse_match ls folder1 &&
477
478 test_all_match git clean -xf &&
479 test_all_match git status --porcelain=v2 &&
480 test_sparse_match ls &&
481 test_sparse_match ls folder1 &&
482
483 test_all_match git clean -xdf &&
484 test_all_match git status --porcelain=v2 &&
485 test_sparse_match ls &&
486 test_sparse_match ls folder1 &&
487
488 test_sparse_match test_path_is_dir folder1
489 '
490
491 test_expect_success 'submodule handling' '
492 init_repos &&
493
494 test_all_match mkdir modules &&
495 test_all_match touch modules/a &&
496 test_all_match git add modules &&
497 test_all_match git commit -m "add modules directory" &&
498
499 run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
500 test_all_match git commit -m "add submodule" &&
501
502 # having a submodule prevents "modules" from collapse
503 test-tool -C sparse-index read-cache --table >cache &&
504 grep "100644 blob .* modules/a" cache &&
505 grep "160000 commit $(git -C initial-repo rev-parse HEAD) modules/sub" cache
506 '
507
508 test_expect_success 'sparse-index is expanded and converted back' '
509 init_repos &&
510
511 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
512 git -C sparse-index -c core.fsmonitor="" reset --hard &&
513 test_region index convert_to_sparse trace2.txt &&
514 test_region index ensure_full_index trace2.txt
515 '
516
517 test_expect_success 'sparse-index is not expanded' '
518 init_repos &&
519
520 rm -f trace2.txt &&
521 echo >>sparse-index/untracked.txt &&
522 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
523 git -C sparse-index status &&
524 test_region ! index ensure_full_index trace2.txt
525 '
526
527 test_expect_success 'reset mixed and checkout orphan' '
528 init_repos &&
529
530 test_all_match git checkout rename-out-to-in &&
531
532 # Sparse checkouts do not agree with full checkouts about
533 # how to report a directory/file conflict during a reset.
534 # This command would fail with test_all_match because the
535 # full checkout reports "T folder1/0/1" while a sparse
536 # checkout reports "D folder1/0/1". This matches because
537 # the sparse checkouts skip "adding" the other side of
538 # the conflict.
539 test_sparse_match git reset --mixed HEAD~1 &&
540 test_sparse_match test-tool read-cache --table --expand &&
541 test_sparse_match git status --porcelain=v2 &&
542
543 # At this point, sparse-checkouts behave differently
544 # from the full-checkout.
545 test_sparse_match git checkout --orphan new-branch &&
546 test_sparse_match test-tool read-cache --table --expand &&
547 test_sparse_match git status --porcelain=v2
548 '
549
550 test_expect_success 'add everything with deep new file' '
551 init_repos &&
552
553 run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
554
555 run_on_all touch deep/deeper1/x &&
556 test_all_match git add . &&
557 test_all_match git status --porcelain=v2
558 '
559
560 test_done