blame: enable and test the sparse index

Enable the sparse index for the 'git blame' command. The index was already not expanded with this command, so the most interesting thing to do is to add tests that verify that 'git blame' behaves correctly when the sparse index is enabled and that its performance improves. More specifically, these cases are: 1. The index is not expanded for 'blame' when given paths in the sparse checkout cone at multiple levels. 2. Performance measurably improves for 'blame' with sparse index when given paths in the sparse checkout cone at multiple levels. The `p2000` tests demonstrate a ~60% execution time reduction when running 'blame' for a file two levels deep and and a ~30% execution time reduction for a file three levels deep. Test before after ---------------------------------------------------------------- 2000.62: git blame f2/f4/a (full-v3) 0.31 0.32 +3.2% 2000.63: git blame f2/f4/a (full-v4) 0.29 0.31 +6.9% 2000.64: git blame f2/f4/a (sparse-v3) 0.55 0.23 -58.2% 2000.65: git blame f2/f4/a (sparse-v4) 0.57 0.23 -59.6% 2000.66: git blame f2/f4/f3/a (full-v3) 0.77 0.85 +10.4% 2000.67: git blame f2/f4/f3/a (full-v4) 0.78 0.81 +3.8% 2000.68: git blame f2/f4/f3/a (sparse-v3) 1.07 0.72 -32.7% 2000.99: git blame f2/f4/f3/a (sparse-v4) 1.05 0.73 -30.5% We do not include paths outside the sparse checkout cone because blame does not support blaming files that are not present in the working directory. This is true in both sparse and full checkouts. 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 add4c864b60766174ad4f74ba7be17e66d61ef16
3 files changed +43 -11
builtin/blame.c
+3
@@ -940,6 +940,9 @@ parse_done:
940 revs.diffopt.flags.follow_renames = 0;
941 argc = parse_options_end(&ctx);
942
943 + prepare_repo_settings(the_repository);
944 + the_repository->settings.command_requires_full_index = 0;
945 +
946 if (incremental || (output_option & OUTPUT_PORCELAIN)) {
947 if (show_progress > 0)
948 die(_("--progress can't be used with --incremental or porcelain formats"));
t/perf/p2000-sparse-operations.sh
+2
@@ -115,5 +115,7 @@ 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 +test_perf_on_all git blame $SPARSE_CONE/a
119 +test_perf_on_all git blame $SPARSE_CONE/f3/a
120
121 test_done
t/t1092-sparse-checkout-compatibility.sh
+38 -11
@@ -472,21 +472,36 @@ test_expect_success 'log with pathspec outside sparse definition' '
472 test_expect_success 'blame with pathspec inside sparse definition' '
473 init_repos &&
474
475 - test_all_match git blame a &&
476 - test_all_match git blame deep/a &&
477 - test_all_match git blame deep/deeper1/a &&
478 - test_all_match git blame deep/deeper1/deepest/a
475 + for file in a \
476 + deep/a \
477 + deep/deeper1/a \
478 + deep/deeper1/deepest/a
479 + do
480 + test_all_match git blame $file
481 + done
482 '
483
481 -# TODO: blame currently does not support blaming files outside of the
482 -# sparse definition. It complains that the file doesn't exist locally.
483 -test_expect_failure 'blame with pathspec outside sparse definition' '
484 +# Without a revision specified, blame will error if passed any file that
485 +# is not present in the working directory (even if the file is tracked).
486 +# Here we just verify that this is also true with sparse checkouts.
487 +test_expect_success 'blame with pathspec outside sparse definition' '
488 init_repos &&
489 + test_sparse_match git sparse-checkout set &&
490
486 - test_all_match git blame folder1/a &&
487 - test_all_match git blame folder2/a &&
488 - test_all_match git blame deep/deeper2/a &&
489 - test_all_match git blame deep/deeper2/deepest/a
491 + for file in a \
492 + deep/a \
493 + deep/deeper1/a \
494 + deep/deeper1/deepest/a
495 + do
496 + test_sparse_match test_must_fail git blame $file &&
497 + cat >expect <<-EOF &&
498 + fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
499 + EOF
500 + # We compare sparse-checkout-err and sparse-index-err in
501 + # `test_sparse_match`. Given we know they are the same, we
502 + # only check the content of sparse-index-err here.
503 + test_cmp expect sparse-index-err
504 + done
505 '
506
507 test_expect_success 'checkout and reset (mixed)' '
@@ -937,6 +952,18 @@ test_expect_success 'sparse index is not expanded: diff' '
952 ensure_not_expanded diff --cached
953 '
954
955 +test_expect_success 'sparse index is not expanded: blame' '
956 + init_repos &&
957 +
958 + for file in a \
959 + deep/a \
960 + deep/deeper1/a \
961 + deep/deeper1/deepest/a
962 + do
963 + ensure_not_expanded blame $file
964 + done
965 +'
966 +
967 # NEEDSWORK: a sparse-checkout behaves differently from a full checkout
968 # in this scenario, but it shouldn't.
969 test_expect_success 'reset mixed and checkout orphan' '