Skip to content

Commit

Permalink
blame: enable and test the sparse index
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ldennington authored and gitster committed Dec 6, 2021
1 parent 51ba65b commit add4c86
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
3 changes: 3 additions & 0 deletions builtin/blame.c
Expand Up @@ -940,6 +940,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
revs.diffopt.flags.follow_renames = 0;
argc = parse_options_end(&ctx);

prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

if (incremental || (output_option & OUTPUT_PORCELAIN)) {
if (show_progress > 0)
die(_("--progress can't be used with --incremental or porcelain formats"));
Expand Down
2 changes: 2 additions & 0 deletions t/perf/p2000-sparse-operations.sh
Expand Up @@ -115,5 +115,7 @@ 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_perf_on_all git blame $SPARSE_CONE/a
test_perf_on_all git blame $SPARSE_CONE/f3/a

test_done
49 changes: 38 additions & 11 deletions t/t1092-sparse-checkout-compatibility.sh
Expand Up @@ -472,21 +472,36 @@ test_expect_success 'log with pathspec outside sparse definition' '
test_expect_success 'blame with pathspec inside sparse definition' '
init_repos &&
test_all_match git blame a &&
test_all_match git blame deep/a &&
test_all_match git blame deep/deeper1/a &&
test_all_match git blame deep/deeper1/deepest/a
for file in a \
deep/a \
deep/deeper1/a \
deep/deeper1/deepest/a
do
test_all_match git blame $file
done
'

# TODO: blame currently does not support blaming files outside of the
# sparse definition. It complains that the file doesn't exist locally.
test_expect_failure 'blame with pathspec outside sparse definition' '
# Without a revision specified, blame will error if passed any file that
# is not present in the working directory (even if the file is tracked).
# Here we just verify that this is also true with sparse checkouts.
test_expect_success 'blame with pathspec outside sparse definition' '
init_repos &&
test_sparse_match git sparse-checkout set &&
test_all_match git blame folder1/a &&
test_all_match git blame folder2/a &&
test_all_match git blame deep/deeper2/a &&
test_all_match git blame deep/deeper2/deepest/a
for file in a \
deep/a \
deep/deeper1/a \
deep/deeper1/deepest/a
do
test_sparse_match test_must_fail git blame $file &&
cat >expect <<-EOF &&
fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
EOF
# We compare sparse-checkout-err and sparse-index-err in
# `test_sparse_match`. Given we know they are the same, we
# only check the content of sparse-index-err here.
test_cmp expect sparse-index-err
done
'

test_expect_success 'checkout and reset (mixed)' '
Expand Down Expand Up @@ -937,6 +952,18 @@ test_expect_success 'sparse index is not expanded: diff' '
ensure_not_expanded diff --cached
'

test_expect_success 'sparse index is not expanded: blame' '
init_repos &&
for file in a \
deep/a \
deep/deeper1/a \
deep/deeper1/deepest/a
do
ensure_not_expanded blame $file
done
'

# 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' '
Expand Down

0 comments on commit add4c86

Please sign in to comment.