grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
'
+test_expect_success 'git apply functionality' '
+ init_repos &&
+
+ test_all_match git checkout base &&
+
+ git -C full-checkout diff base..merge-right -- deep >patch-in-sparse &&
+ git -C full-checkout diff base..merge-right -- folder2 >patch-outside &&
+
+ # Apply a patch to a file inside the sparse definition
+ test_all_match git apply --index --stat ../patch-in-sparse &&
+ test_all_match git status --porcelain=v2 &&
+
+ # Apply a patch to a file outside the sparse definition
+ test_sparse_match test_must_fail git apply ../patch-outside &&
+ grep "No such file or directory" sparse-checkout-err &&
+
+ # But it works with --index and --cached
+ test_all_match git apply --index --stat ../patch-outside &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git reset --hard &&
+ test_all_match git apply --cached --stat ../patch-outside &&
+ test_all_match git status --porcelain=v2
+'
+
# When working with a sparse index, some commands will need to expand the
# index to operate properly. If those commands also write the index back
# to disk, they need to convert the index to sparse before writing.
ensure_not_expanded check-attr -a --cached -- folder1/a
'
+test_expect_success 'sparse-index is not expanded: git apply' '
+ init_repos &&
+
+ git -C sparse-index checkout base &&
+ git -C full-checkout diff base..merge-right -- deep >patch-in-sparse &&
+ git -C full-checkout diff base..merge-right -- folder2 >patch-outside &&
+
+ # Apply a patch to a file inside the sparse definition
+ ensure_not_expanded apply --index --stat ../patch-in-sparse &&
+
+ # Apply a patch to a file outside the sparse definition
+ # Fails when caring about the worktree.
+ ensure_not_expanded ! apply ../patch-outside &&
+
+ # Expands when using --index.
+ ensure_expanded apply --index ../patch-outside &&
+
+ # Does not when index is partially expanded.
+ git -C sparse-index reset --hard &&
+ ensure_not_expanded apply --cached ../patch-outside &&
+
+ # Try again with a reset and collapsed index.
+ git -C sparse-index reset --hard &&
+ git -C sparse-index sparse-checkout reapply &&
+
+ # Expands when index is collapsed.
+ ensure_expanded apply --cached ../patch-outside
+'
+
test_expect_success 'advice.sparseIndexExpanded' '
init_repos &&