]> git.ipfire.org Git - thirdparty/git.git/commitdiff
apply: integrate with the sparse index
authorDerrick Stolee <stolee@gmail.com>
Fri, 16 May 2025 14:55:27 +0000 (14:55 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 16 May 2025 19:00:33 +0000 (12:00 -0700)
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>
builtin/apply.c
t/t1092-sparse-checkout-compatibility.sh

index 84f1863d3ac3490bebbb746c61a2d695de09c848..a1e20c593d09032dcb016b15556f43f7694213b3 100644 (file)
@@ -12,7 +12,7 @@ static const char * const apply_usage[] = {
 int cmd_apply(int argc,
              const char **argv,
              const char *prefix,
-             struct repository *repo UNUSED)
+             struct repository *repo)
 {
        int force_apply = 0;
        int options = 0;
@@ -35,6 +35,11 @@ int cmd_apply(int argc,
                                   &state, &force_apply, &options,
                                   apply_usage);
 
+       if (repo) {
+               prepare_repo_settings(repo);
+               repo->settings.command_requires_full_index = 0;
+       }
+
        if (check_apply_state(&state, force_apply))
                exit(128);
 
index a4c7c41fc00aa3043c3f18fdc4c5d7785d17f7de..fa2472010d8abbca945ebd8795bde8a6b2d38d91 100755 (executable)
@@ -1340,6 +1340,30 @@ test_expect_success 'submodule handling' '
        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.
@@ -2347,6 +2371,35 @@ test_expect_success 'sparse-index is not expanded: check-attr' '
        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 &&