Skip to content

Commit add4c86

Browse files
ldenningtongitster
authored andcommitted
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 <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51ba65b commit add4c86

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

builtin/blame.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
940940
revs.diffopt.flags.follow_renames = 0;
941941
argc = parse_options_end(&ctx);
942942

943+
prepare_repo_settings(the_repository);
944+
the_repository->settings.command_requires_full_index = 0;
945+
943946
if (incremental || (output_option & OUTPUT_PORCELAIN)) {
944947
if (show_progress > 0)
945948
die(_("--progress can't be used with --incremental or porcelain formats"));

t/perf/p2000-sparse-operations.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@ test_perf_on_all git reset --hard
115115
test_perf_on_all git reset -- does-not-exist
116116
test_perf_on_all git diff
117117
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
118120

119121
test_done

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -472,21 +472,36 @@ test_expect_success 'log with pathspec outside sparse definition' '
472472
test_expect_success 'blame with pathspec inside sparse definition' '
473473
init_repos &&
474474
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
479482
'
480483

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' '
484488
init_repos &&
489+
test_sparse_match git sparse-checkout set &&
485490
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
490505
'
491506

492507
test_expect_success 'checkout and reset (mixed)' '
@@ -937,6 +952,18 @@ test_expect_success 'sparse index is not expanded: diff' '
937952
ensure_not_expanded diff --cached
938953
'
939954

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+
940967
# NEEDSWORK: a sparse-checkout behaves differently from a full checkout
941968
# in this scenario, but it shouldn't.
942969
test_expect_success 'reset mixed and checkout orphan' '

0 commit comments

Comments
 (0)