apply: integrate with the sparse index

The sparse index allows storing directory entries in the index, marked with the skip-wortkree bit and pointing to a tree object. This may be an unexpected data shape for some implementation areas, so we are rolling it out incrementally on a builtin-per-builtin basis. This change enables the sparse index for 'git apply'. The main motivation for this change is that 'git apply' is used as a child process of 'git add -p' and expanding the sparse index for each of those child processes can lead to significant performance issues. The good news is that the actual index manipulation code used by 'git apply' is already integrated with the sparse index, so the only product change is to mark the builtin as allowing the sparse index so it isn't inflated on read. The more involved part of this change is around adding tests that verify how 'git apply' behaves in a sparse-checkout environment and whether or not the index expands in certain operations. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed May 16, 2025 at 14:55 UTC 952de281fe63eb03e0dcc8adf773ce54cb581b83
2 files changed +59 -1
builtin/apply.c
+6 -1
@@ -12,7 +12,7 @@ static const char * const apply_usage[] = {
12 int cmd_apply(int argc,
13 const char **argv,
14 const char *prefix,
15 - struct repository *repo UNUSED)
15 + struct repository *repo)
16 {
17 int force_apply = 0;
18 int options = 0;
@@ -35,6 +35,11 @@ int cmd_apply(int argc,
35 &state, &force_apply, &options,
36 apply_usage);
37
38 + if (repo) {
39 + prepare_repo_settings(repo);
40 + repo->settings.command_requires_full_index = 0;
41 + }
42 +
43 if (check_apply_state(&state, force_apply))
44 exit(128);
45
t/t1092-sparse-checkout-compatibility.sh
+53
@@ -1340,6 +1340,30 @@ test_expect_success 'submodule handling' '
1340 grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
1341 '
1342
1343 +test_expect_success 'git apply functionality' '
1344 + init_repos &&
1345 +
1346 + test_all_match git checkout base &&
1347 +
1348 + git -C full-checkout diff base..merge-right -- deep >patch-in-sparse &&
1349 + git -C full-checkout diff base..merge-right -- folder2 >patch-outside &&
1350 +
1351 + # Apply a patch to a file inside the sparse definition
1352 + test_all_match git apply --index --stat ../patch-in-sparse &&
1353 + test_all_match git status --porcelain=v2 &&
1354 +
1355 + # Apply a patch to a file outside the sparse definition
1356 + test_sparse_match test_must_fail git apply ../patch-outside &&
1357 + grep "No such file or directory" sparse-checkout-err &&
1358 +
1359 + # But it works with --index and --cached
1360 + test_all_match git apply --index --stat ../patch-outside &&
1361 + test_all_match git status --porcelain=v2 &&
1362 + test_all_match git reset --hard &&
1363 + test_all_match git apply --cached --stat ../patch-outside &&
1364 + test_all_match git status --porcelain=v2
1365 +'
1366 +
1367 # When working with a sparse index, some commands will need to expand the
1368 # index to operate properly. If those commands also write the index back
1369 # to disk, they need to convert the index to sparse before writing.
@@ -2347,6 +2371,35 @@ test_expect_success 'sparse-index is not expanded: check-attr' '
2371 ensure_not_expanded check-attr -a --cached -- folder1/a
2372 '
2373
2374 +test_expect_success 'sparse-index is not expanded: git apply' '
2375 + init_repos &&
2376 +
2377 + git -C sparse-index checkout base &&
2378 + git -C full-checkout diff base..merge-right -- deep >patch-in-sparse &&
2379 + git -C full-checkout diff base..merge-right -- folder2 >patch-outside &&
2380 +
2381 + # Apply a patch to a file inside the sparse definition
2382 + ensure_not_expanded apply --index --stat ../patch-in-sparse &&
2383 +
2384 + # Apply a patch to a file outside the sparse definition
2385 + # Fails when caring about the worktree.
2386 + ensure_not_expanded ! apply ../patch-outside &&
2387 +
2388 + # Expands when using --index.
2389 + ensure_expanded apply --index ../patch-outside &&
2390 +
2391 + # Does not when index is partially expanded.
2392 + git -C sparse-index reset --hard &&
2393 + ensure_not_expanded apply --cached ../patch-outside &&
2394 +
2395 + # Try again with a reset and collapsed index.
2396 + git -C sparse-index reset --hard &&
2397 + git -C sparse-index sparse-checkout reapply &&
2398 +
2399 + # Expands when index is collapsed.
2400 + ensure_expanded apply --cached ../patch-outside
2401 +'
2402 +
2403 test_expect_success 'advice.sparseIndexExpanded' '
2404 init_repos &&
2405