Skip to content

Commit 51ba65b

Browse files
ldenningtonderrickstolee
authored andcommitted
diff: enable and test the sparse index
Enable the sparse index within the 'git diff' command. Its implementation already safely integrates with the sparse index because it shares code with the 'git status' and 'git checkout' commands that were already integrated. For more details see: d76723e (status: use sparse-index throughout, 2021-07-14) 1ba5f45 (checkout: stop expanding sparse indexes, 2021-06-29) The most interesting thing to do is to add tests that verify that 'git diff' behaves correctly when the sparse index is enabled. These cases are: 1. The index is not expanded for 'diff' and 'diff --staged' 2. 'diff' and 'diff --staged' behave the same in full checkout, sparse checkout, and sparse index repositories in the following partially-staged scenarios (i.e. the index, HEAD, and working directory differ at a given path): 1. Path is within sparse-checkout cone 2. Path is outside sparse-checkout cone 3. A merge conflict exists for paths outside sparse-checkout cone The `p2000` tests demonstrate a ~44% execution time reduction for 'git diff' and a ~86% execution time reduction for 'git diff --staged' using a sparse index: Test before after ------------------------------------------------------------- 2000.30: git diff (full-v3) 0.33 0.34 +3.0% 2000.31: git diff (full-v4) 0.33 0.35 +6.1% 2000.32: git diff (sparse-v3) 0.53 0.31 -41.5% 2000.33: git diff (sparse-v4) 0.54 0.29 -46.3% 2000.34: git diff --cached (full-v3) 0.07 0.07 +0.0% 2000.35: git diff --cached (full-v4) 0.07 0.08 +14.3% 2000.36: git diff --cached (sparse-v3) 0.28 0.04 -85.7% 2000.37: git diff --cached (sparse-v4) 0.23 0.03 -87.0% Co-authored-by: Derrick Stolee <[email protected]> Signed-off-by: Lessley Dennington <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 338e2a9 commit 51ba65b

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

builtin/diff.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
437437

438438
prefix = setup_git_directory_gently(&nongit);
439439

440+
if (!nongit) {
441+
prepare_repo_settings(the_repository);
442+
the_repository->settings.command_requires_full_index = 0;
443+
}
444+
440445
if (!no_index) {
441446
/*
442447
* Treat git diff with at least one path outside of the

t/perf/p2000-sparse-operations.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,7 @@ test_perf_on_all git checkout -f -
113113
test_perf_on_all git reset
114114
test_perf_on_all git reset --hard
115115
test_perf_on_all git reset -- does-not-exist
116+
test_perf_on_all git diff
117+
test_perf_on_all git diff --cached
116118

117119
test_done

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,52 @@ test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
891891
)
892892
'
893893

894+
test_expect_success 'sparse index is not expanded: diff' '
895+
init_repos &&
896+
897+
write_script edit-contents <<-\EOF &&
898+
echo text >>$1
899+
EOF
900+
901+
# Add file within cone
902+
test_sparse_match git sparse-checkout set deep &&
903+
run_on_all ../edit-contents deep/testfile &&
904+
test_all_match git add deep/testfile &&
905+
run_on_all ../edit-contents deep/testfile &&
906+
907+
test_all_match git diff &&
908+
test_all_match git diff --cached &&
909+
ensure_not_expanded diff &&
910+
ensure_not_expanded diff --cached &&
911+
912+
# Add file outside cone
913+
test_all_match git reset --hard &&
914+
run_on_all mkdir newdirectory &&
915+
run_on_all ../edit-contents newdirectory/testfile &&
916+
test_sparse_match git sparse-checkout set newdirectory &&
917+
test_all_match git add newdirectory/testfile &&
918+
run_on_all ../edit-contents newdirectory/testfile &&
919+
test_sparse_match git sparse-checkout set &&
920+
921+
test_all_match git diff &&
922+
test_all_match git diff --cached &&
923+
ensure_not_expanded diff &&
924+
ensure_not_expanded diff --cached &&
925+
926+
# Merge conflict outside cone
927+
# The sparse checkout will report a warning that is not in the
928+
# full checkout, so we use `run_on_all` instead of
929+
# `test_all_match`
930+
run_on_all git reset --hard &&
931+
test_all_match git checkout merge-left &&
932+
test_all_match test_must_fail git merge merge-right &&
933+
934+
test_all_match git diff &&
935+
test_all_match git diff --cached &&
936+
ensure_not_expanded diff &&
937+
ensure_not_expanded diff --cached
938+
'
939+
894940
# NEEDSWORK: a sparse-checkout behaves differently from a full checkout
895941
# in this scenario, but it shouldn't.
896942
test_expect_success 'reset mixed and checkout orphan' '

0 commit comments

Comments
 (0)