add: allow operating on a sparse-only index

Disable command_requires_full_index for 'git add'. This does not require any additional removals of ensure_full_index(). The main reason is that 'git add' discovers changes based on the pathspec and the worktree itself. These are then inserted into the index directly, and calls to index_name_pos() or index_file_exists() already call expand_to_path() at the appropriate time to support a sparse-index. Add a test to check that 'git add -A' and 'git add <file>' does not expand the index at all, as long as <file> is not within a sparse directory. This does not help the global 'git add .' case. We can measure the improvement using p2000-sparse-operations.sh with these results: Test HEAD~1 HEAD ------------------------------------------------------------------------------ 2000.6: git add -A (full-index-v3) 0.35(0.30+0.05) 0.37(0.29+0.06) +5.7% 2000.7: git add -A (full-index-v4) 0.31(0.26+0.06) 0.33(0.27+0.06) +6.5% 2000.8: git add -A (sparse-index-v3) 0.57(0.53+0.07) 0.05(0.04+0.08) -91.2% 2000.9: git add -A (sparse-index-v4) 0.58(0.55+0.06) 0.05(0.05+0.06) -91.4% While the 91% improvement seems impressive, it's important to recognize that previously we had significant overhead for expanding the sparse-index. Comparing to the full index case, 'git add -A' goes from 0.37s to 0.05s, which is "only" an 86% improvement. This modification to 'git add' creates some behavior change depending on the use of a sparse index. We modify a test in t1092 to demonstrate these changes which will be remedied in future changes. Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 29, 2021 at 14:52 UTC 5e7cbab19680745d4f3e0c50903025d9f02e7468
2 files changed +21 -7
builtin/add.c
+3
@@ -528,6 +528,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
528 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
529 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
530
531 + prepare_repo_settings(the_repository);
532 + the_repository->settings.command_requires_full_index = 0;
533 +
534 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
535
536 /*
t/t1092-sparse-checkout-compatibility.sh
+18 -7
@@ -340,21 +340,27 @@ test_expect_success 'status/add: outside sparse cone' '
340
341 test_sparse_match git status --porcelain=v2 &&
342
343 - # This "git add folder1/a" fails with a warning
344 - # in the sparse repos, differing from the full
345 - # repo. This is intentional.
343 + # Adding the path outside of the sparse-checkout cone should fail.
344 test_sparse_match test_must_fail git add folder1/a &&
347 - test_sparse_match test_must_fail git add --refresh folder1/a &&
348 - test_all_match git status --porcelain=v2 &&
345 +
346 + test_must_fail git -C sparse-checkout add --refresh folder1/a 2>sparse-checkout-err &&
347 + test_must_fail git -C sparse-index add --refresh folder1/a 2>sparse-index-err &&
348 + # NEEDSWORK: A sparse index changes the error message.
349 + ! test_cmp sparse-checkout-err sparse-index-err &&
350 +
351 + # NEEDSWORK: Adding a newly-tracked file outside the cone succeeds
352 + test_sparse_match git add folder1/new &&
353
354 test_all_match git add . &&
355 test_all_match git status --porcelain=v2 &&
356 test_all_match git commit -m folder1/new &&
357 + test_all_match git rev-parse HEAD^{tree} &&
358
359 run_on_all ../edit-contents folder1/newer &&
360 test_all_match git add folder1/ &&
361 test_all_match git status --porcelain=v2 &&
357 - test_all_match git commit -m folder1/newer
362 + test_all_match git commit -m folder1/newer &&
363 + test_all_match git rev-parse HEAD^{tree}
364 '
365
366 test_expect_success 'checkout and reset --hard' '
@@ -641,7 +647,12 @@ test_expect_success 'sparse-index is not expanded' '
647 git -C sparse-index reset --hard &&
648 ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
649 git -C sparse-index reset --hard &&
644 - ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1
650 + ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
651 +
652 + echo >>sparse-index/README.md &&
653 + ensure_not_expanded add -A &&
654 + echo >>sparse-index/extra.txt &&
655 + ensure_not_expanded add extra.txt
656 '
657
658 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout