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: d76723ee53 (status: use sparse-index throughout, 2021-07-14) 1ba5f45132 (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 <dstolee@microsoft.com> Signed-off-by: Lessley Dennington <lessleydennington@gmail.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lessley Dennington committed Dec 6, 2021 at 15:56 UTC 51ba65b5c355a835fb0d83ee88dc7276dccb66a4
3 files changed +53
builtin/diff.c
+5
@@ -437,6 +437,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
437
438 prefix = setup_git_directory_gently(&nongit);
439
440 + if (!nongit) {
441 + prepare_repo_settings(the_repository);
442 + the_repository->settings.command_requires_full_index = 0;
443 + }
444 +
445 if (!no_index) {
446 /*
447 * Treat git diff with at least one path outside of the
t/perf/p2000-sparse-operations.sh
+2
@@ -113,5 +113,7 @@ test_perf_on_all git checkout -f -
113 test_perf_on_all git reset
114 test_perf_on_all git reset --hard
115 test_perf_on_all git reset -- does-not-exist
116 +test_perf_on_all git diff
117 +test_perf_on_all git diff --cached
118
119 test_done
t/t1092-sparse-checkout-compatibility.sh
+46
@@ -891,6 +891,52 @@ test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
891 )
892 '
893
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 +
940 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
941 # in this scenario, but it shouldn't.
942 test_expect_success 'reset mixed and checkout orphan' '