Skip to content

Commit daa1ace

Browse files
derrickstoleegitster
authored andcommitted
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 2de37c5 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 11042ab commit daa1ace

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

builtin/commit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16821682
if (argc == 2 && !strcmp(argv[1], "-h"))
16831683
usage_with_options(builtin_commit_usage, builtin_commit_options);
16841684

1685+
prepare_repo_settings(the_repository);
1686+
the_repository->settings.command_requires_full_index = 0;
1687+
16851688
status_init_config(&s, git_commit_config);
16861689
s.commit_template = 1;
16871690
status_format = STATUS_FORMAT_NONE; /* Ignore status.short */

cache-tree.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,6 @@ int cache_tree_update(struct index_state *istate, int flags)
461461
if (i)
462462
return i;
463463

464-
ensure_full_index(istate);
465-
466464
if (!istate->cache_tree)
467465
istate->cache_tree = cache_tree();
468466

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,34 @@ test_expect_success 'add, commit, checkout' '
262262
test_all_match git checkout -
263263
'
264264

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+
265293
test_expect_success 'status/add: outside sparse cone' '
266294
init_repos &&
267295
@@ -514,14 +542,25 @@ test_expect_success 'sparse-index is expanded and converted back' '
514542
test_region index ensure_full_index trace2.txt
515543
'
516544

517-
test_expect_success 'sparse-index is not expanded' '
518-
init_repos &&
519-
545+
ensure_not_expanded () {
520546
rm -f trace2.txt &&
521547
echo >>sparse-index/untracked.txt &&
522548
GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
523-
git -C sparse-index status &&
549+
git -C sparse-index "$@" &&
524550
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
525564
'
526565

527566
# NEEDSWORK: a sparse-checkout behaves differently from a full checkout

0 commit comments

Comments
 (0)