commit: integrate with sparse-index

Update 'git commit' to allow using the sparse-index in memory without expanding to a full one. The only place that had an ensure_full_index() call was in cache_tree_update(). The recursive algorithm for update_one() was already updated in 2de37c536 (cache-tree: integrate with sparse directory entries, 2021-03-03) to handle sparse directory entries in the index. Most of this change involves testing different command-line options that allow specifying which on-disk changes should be included in the commit. This includes no options (only take currently-staged changes), -a (take all tracked changes), and --include (take a list of specific changes). To simplify testing that these options do not expand the index, update the test that previously verified that 'git status' does not expand the index with a helper method, ensure_not_expanded(). This allows 'git commit' to operate much faster when the sparse-checkout cone is much smaller than the full list of files at HEAD. Here are the relevant lines from p2000-sparse-operations.sh: Test HEAD~1 HEAD ---------------------------------------------------------------------------------- 2000.14: git commit -a -m A (full-v3) 0.35(0.26+0.06) 0.36(0.28+0.07) +2.9% 2000.15: git commit -a -m A (full-v4) 0.32(0.26+0.05) 0.34(0.28+0.06) +6.3% 2000.16: git commit -a -m A (sparse-v3) 0.63(0.59+0.06) 0.04(0.05+0.05) -93.7% 2000.17: git commit -a -m A (sparse-v4) 0.64(0.59+0.08) 0.04(0.04+0.04) -93.8% It is important to compare the full-index case to the sparse-index case, so the improvement for index version v4 is actually an 88% improvement in this synthetic example. In a real repository with over two million files at HEAD and 60,000 files in the sparse-checkout definition, the time for 'git commit -a' went from 2.61 seconds to 134ms. I compared this to the result if the index only contained the paths in the sparse-checkout definition and found the theoretical optimum to be 120ms, so the out-of-cone paths only add a 12% overhead. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 29, 2021 at 02:13 UTC daa1acefc55bb6492c00519634e0a7622b3b6d69
3 files changed +46 -6
builtin/commit.c
+3
@@ -1682,6 +1682,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1682 if (argc == 2 && !strcmp(argv[1], "-h"))
1683 usage_with_options(builtin_commit_usage, builtin_commit_options);
1684
1685 + prepare_repo_settings(the_repository);
1686 + the_repository->settings.command_requires_full_index = 0;
1687 +
1688 status_init_config(&s, git_commit_config);
1689 s.commit_template = 1;
1690 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
cache-tree.c
-2
@@ -461,8 +461,6 @@ int cache_tree_update(struct index_state *istate, int flags)
461 if (i)
462 return i;
463
464 - ensure_full_index(istate);
465 -
464 if (!istate->cache_tree)
465 istate->cache_tree = cache_tree();
466
t/t1092-sparse-checkout-compatibility.sh
+43 -4
@@ -262,6 +262,34 @@ test_expect_success 'add, commit, checkout' '
262 test_all_match git checkout -
263 '
264
265 +test_expect_success 'commit including unstaged changes' '
266 + init_repos &&
267 +
268 + write_script edit-file <<-\EOF &&
269 + echo $1 >$2
270 + EOF
271 +
272 + run_on_all ../edit-file 1 a &&
273 + run_on_all ../edit-file 1 deep/a &&
274 +
275 + test_all_match git commit -m "-a" -a &&
276 + test_all_match git status --porcelain=v2 &&
277 +
278 + run_on_all ../edit-file 2 a &&
279 + run_on_all ../edit-file 2 deep/a &&
280 +
281 + test_all_match git commit -m "--include" --include deep/a &&
282 + test_all_match git status --porcelain=v2 &&
283 + test_all_match git commit -m "--include" --include a &&
284 + test_all_match git status --porcelain=v2 &&
285 +
286 + run_on_all ../edit-file 3 a &&
287 + run_on_all ../edit-file 3 deep/a &&
288 +
289 + test_all_match git commit -m "--amend" -a --amend &&
290 + test_all_match git status --porcelain=v2
291 +'
292 +
293 test_expect_success 'status/add: outside sparse cone' '
294 init_repos &&
295
@@ -514,14 +542,25 @@ test_expect_success 'sparse-index is expanded and converted back' '
542 test_region index ensure_full_index trace2.txt
543 '
544
517 -test_expect_success 'sparse-index is not expanded' '
518 - init_repos &&
519 -
545 +ensure_not_expanded () {
546 rm -f trace2.txt &&
547 echo >>sparse-index/untracked.txt &&
548 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
523 - git -C sparse-index status &&
549 + git -C sparse-index "$@" &&
550 test_region ! index ensure_full_index trace2.txt
551 +}
552 +
553 +test_expect_success 'sparse-index is not expanded' '
554 + init_repos &&
555 +
556 + ensure_not_expanded status &&
557 + ensure_not_expanded commit --allow-empty -m empty &&
558 + echo >>sparse-index/a &&
559 + ensure_not_expanded commit -a -m a &&
560 + echo >>sparse-index/a &&
561 + ensure_not_expanded commit --include a -m a &&
562 + echo >>sparse-index/deep/deeper1/a &&
563 + ensure_not_expanded commit --include deep/deeper1/a -m deeper
564 '
565
566 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout