]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: enable and test the sparse index
authorLessley Dennington <lessleydennington@gmail.com>
Mon, 6 Dec 2021 15:56:00 +0000 (15:56 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 6 Dec 2021 17:55:06 +0000 (09:55 -0800)
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>
builtin/diff.c
t/perf/p2000-sparse-operations.sh
t/t1092-sparse-checkout-compatibility.sh

index dd8ce688ba7054e7ee76ca4764b1a17b84a3044a..fa4683377ebbe51ee42d964d29a30011f12f1261 100644 (file)
@@ -437,6 +437,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 
        prefix = setup_git_directory_gently(&nongit);
 
+       if (!nongit) {
+               prepare_repo_settings(the_repository);
+               the_repository->settings.command_requires_full_index = 0;
+       }
+
        if (!no_index) {
                /*
                 * Treat git diff with at least one path outside of the
index bfd332120c8df8dc27c77a2b74c1e2bf02f9e1d0..5cf94627383e17bc943789cadc0f959cddf0954c 100755 (executable)
@@ -113,5 +113,7 @@ test_perf_on_all git checkout -f -
 test_perf_on_all git reset
 test_perf_on_all git reset --hard
 test_perf_on_all git reset -- does-not-exist
+test_perf_on_all git diff
+test_perf_on_all git diff --cached
 
 test_done
index fea9942b909c5d78dcb72b12dff2fd51a3972e92..e6885d2340ba36f0beef4b7dae896eb1492fc8e5 100755 (executable)
@@ -891,6 +891,52 @@ test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
        )
 '
 
+test_expect_success 'sparse index is not expanded: diff' '
+       init_repos &&
+
+       write_script edit-contents <<-\EOF &&
+       echo text >>$1
+       EOF
+
+       # Add file within cone
+       test_sparse_match git sparse-checkout set deep &&
+       run_on_all ../edit-contents deep/testfile &&
+       test_all_match git add deep/testfile &&
+       run_on_all ../edit-contents deep/testfile &&
+
+       test_all_match git diff &&
+       test_all_match git diff --cached &&
+       ensure_not_expanded diff &&
+       ensure_not_expanded diff --cached &&
+
+       # Add file outside cone
+       test_all_match git reset --hard &&
+       run_on_all mkdir newdirectory &&
+       run_on_all ../edit-contents newdirectory/testfile &&
+       test_sparse_match git sparse-checkout set newdirectory &&
+       test_all_match git add newdirectory/testfile &&
+       run_on_all ../edit-contents newdirectory/testfile &&
+       test_sparse_match git sparse-checkout set &&
+
+       test_all_match git diff &&
+       test_all_match git diff --cached &&
+       ensure_not_expanded diff &&
+       ensure_not_expanded diff --cached &&
+
+       # Merge conflict outside cone
+       # The sparse checkout will report a warning that is not in the
+       # full checkout, so we use `run_on_all` instead of
+       # `test_all_match`
+       run_on_all git reset --hard &&
+       test_all_match git checkout merge-left &&
+       test_all_match test_must_fail git merge merge-right &&
+
+       test_all_match git diff &&
+       test_all_match git diff --cached &&
+       ensure_not_expanded diff &&
+       ensure_not_expanded diff --cached
+'
+
 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
 # in this scenario, but it shouldn't.
 test_expect_success 'reset mixed and checkout orphan' '