@@ -384,6 +384,38 @@ test_expect_success 'add, commit, checkout' '
384384 test_all_match git checkout -
385385'
386386
387+ test_expect_success ' git add -p' '
388+ init_repos &&
389+
390+ write_script edit-contents <<-\EOF &&
391+ echo text >>$1
392+ EOF
393+
394+ # Does not expand when edits are within sparse checkout.
395+ run_on_all ../edit-contents deep/a &&
396+ run_on_all ../edit-contents deep/deeper1/a &&
397+
398+ test_write_lines y n >in &&
399+ run_on_all git add -p <in &&
400+ test_all_match git status --porcelain=v2 &&
401+ test_all_match git reset &&
402+
403+ test_write_lines u 1 "" q >in &&
404+ run_on_all git add -i <in &&
405+ test_all_match git status --porcelain=v2 &&
406+ test_all_match git reset --hard &&
407+
408+ run_on_sparse mkdir -p folder1 &&
409+ run_on_all ../edit-contents folder1/a &&
410+ test_write_lines y n y >in &&
411+ run_on_all git add -p <in &&
412+ test_sparse_match git status --porcelain=v2 &&
413+ test_sparse_match git reset &&
414+ test_write_lines u 2 3 "" q >in &&
415+ run_on_all git add -i <in &&
416+ test_sparse_match git status --porcelain=v2
417+ '
418+
387419test_expect_success ' deep changes during checkout' '
388420 init_repos &&
389421
@@ -1340,6 +1372,30 @@ test_expect_success 'submodule handling' '
13401372 grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
13411373'
13421374
1375+ test_expect_success ' git apply functionality' '
1376+ init_repos &&
1377+
1378+ test_all_match git checkout base &&
1379+
1380+ git -C full-checkout diff base..merge-right -- deep >patch-in-sparse &&
1381+ git -C full-checkout diff base..merge-right -- folder2 >patch-outside &&
1382+
1383+ # Apply a patch to a file inside the sparse definition
1384+ test_all_match git apply --index --stat ../patch-in-sparse &&
1385+ test_all_match git status --porcelain=v2 &&
1386+
1387+ # Apply a patch to a file outside the sparse definition
1388+ test_sparse_match test_must_fail git apply ../patch-outside &&
1389+ grep "No such file or directory" sparse-checkout-err &&
1390+
1391+ # But it works with --index and --cached
1392+ test_all_match git apply --index --stat ../patch-outside &&
1393+ test_all_match git status --porcelain=v2 &&
1394+ test_all_match git reset --hard &&
1395+ test_all_match git apply --cached --stat ../patch-outside &&
1396+ test_all_match git status --porcelain=v2
1397+ '
1398+
13431399# When working with a sparse index, some commands will need to expand the
13441400# index to operate properly. If those commands also write the index back
13451401# to disk, they need to convert the index to sparse before writing.
@@ -2345,6 +2401,52 @@ test_expect_success 'sparse-index is not expanded: check-attr' '
23452401 ensure_not_expanded check-attr -a --cached -- folder1/a
23462402'
23472403
2404+ test_expect_success ' sparse-index is not expanded: git apply' '
2405+ init_repos &&
2406+
2407+ git -C sparse-index checkout base &&
2408+ git -C full-checkout diff base..merge-right -- deep >patch-in-sparse &&
2409+ git -C full-checkout diff base..merge-right -- folder2 >patch-outside &&
2410+
2411+ # Apply a patch to a file inside the sparse definition
2412+ ensure_not_expanded apply --index --stat ../patch-in-sparse &&
2413+
2414+ # Apply a patch to a file outside the sparse definition
2415+ # Fails when caring about the worktree.
2416+ ensure_not_expanded ! apply ../patch-outside &&
2417+
2418+ # Expands when using --index.
2419+ ensure_expanded apply --index ../patch-outside &&
2420+ git -C sparse-index reset --hard &&
2421+
2422+ # Does not expand when using --cached.
2423+ ensure_not_expanded apply --cached ../patch-outside
2424+ '
2425+
2426+ test_expect_success ' sparse-index is not expanded: git add -p' '
2427+ init_repos &&
2428+
2429+ # Does not expand when edits are within sparse checkout.
2430+ echo "new content" >sparse-index/deep/a &&
2431+ echo "new content" >sparse-index/deep/deeper1/a &&
2432+ test_write_lines y n >in &&
2433+ ensure_not_expanded add -p <in &&
2434+ git -C sparse-index reset &&
2435+ ensure_not_expanded add -i <in &&
2436+
2437+ mkdir -p sparse-index/folder1 &&
2438+ echo "new content" >sparse-index/folder1/a &&
2439+
2440+ # -p does expand when edits are outside sparse checkout.
2441+ test_write_lines y n y >in &&
2442+ ensure_expanded add -p <in &&
2443+
2444+ # but -i does not expand.
2445+ git -C sparse-index reset &&
2446+ test_write_lines u 2 3 "" q >in &&
2447+ ensure_not_expanded add -i <in
2448+ '
2449+
23482450test_expect_success ' advice.sparseIndexExpanded' '
23492451 init_repos &&
23502452
0 commit comments