Skip to content

Commit

Permalink
diff-index: integrate with the sparse index
Browse files Browse the repository at this point in the history
The sparse index allows focusing the index data structure on the files
present in the sparse-checkout, leaving only tree entries for
directories not within the sparse-checkout. Each builtin needs a
repository setting to indicate that it has been tested with the sparse
index before Git will allow the index to be loaded into memory in its
sparse form. This is a safety precaution.

There are still some builtins that haven't been integrated due to the
complexity of the integration and the lack of significant use. However,
'git diff-index' was neglected only because of initial data showing low
usage. The diff machinery was already integrated and there is no more
work to be done there but add some tests to be sure 'git diff-index'
behaves as expected.

For this purpose, we can follow the testing pattern used in 51ba65b
(diff: enable and test the sparse index, 2021-12-06). One difference
here is that we only verify that the sparse index case agrees with the
full index case, but do not generate the expected output. The 'git diff'
tests use the '--name-status' option to ease the creation of the
expected output, but that's not an option for 'diff-index'. Since the
underlying diff machinery is the same, a simple comparison is sufficient
to give some coverage.

Signed-off-by: Derrick Stolee <[email protected]>
  • Loading branch information
derrickstolee committed Aug 22, 2024
1 parent 80ccd8a commit ae5e5d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions builtin/diff-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
usage(diff_cache_usage);

git_config(git_diff_basic_config, NULL); /* no "diff" UI options */

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

repo_init_revisions(the_repository, &rev, prefix);
rev.abbrev = 0;
prefix = precompose_argv_prefix(argc, argv, prefix);
Expand Down
15 changes: 12 additions & 3 deletions t/t1092-sparse-checkout-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,8 @@ test_expect_success 'update-index --remove outside sparse definition' '
test_sparse_match git diff --cached --name-status &&
test_cmp expect sparse-checkout-out &&
test_sparse_match git diff-index --cached HEAD &&
# Reset the state
test_all_match git reset --hard &&
Expand All @@ -812,6 +814,8 @@ test_expect_success 'update-index --remove outside sparse definition' '
test_sparse_match git diff --cached --name-status &&
test_must_be_empty sparse-checkout-out &&
test_sparse_match git diff-index --cached HEAD &&
# Reset the state
test_all_match git reset --hard &&
Expand All @@ -823,7 +827,9 @@ test_expect_success 'update-index --remove outside sparse definition' '
D folder1/a
EOF
test_sparse_match git diff --cached --name-status &&
test_cmp expect sparse-checkout-out
test_cmp expect sparse-checkout-out &&
test_sparse_match git diff-index --cached HEAD
'

test_expect_success 'update-index with directories' '
Expand Down Expand Up @@ -1551,7 +1557,7 @@ test_expect_success 'sparse-index is not expanded: describe' '
ensure_not_expanded describe
'

test_expect_success 'sparse index is not expanded: diff' '
test_expect_success 'sparse index is not expanded: diff and diff-index' '
init_repos &&
write_script edit-contents <<-\EOF &&
Expand All @@ -1568,6 +1574,7 @@ test_expect_success 'sparse index is not expanded: diff' '
test_all_match git diff --cached &&
ensure_not_expanded diff &&
ensure_not_expanded diff --cached &&
ensure_not_expanded diff-index --cached HEAD &&
# Add file outside cone
test_all_match git reset --hard &&
Expand All @@ -1582,6 +1589,7 @@ test_expect_success 'sparse index is not expanded: diff' '
test_all_match git diff --cached &&
ensure_not_expanded diff &&
ensure_not_expanded diff --cached &&
ensure_not_expanded diff-index --cached HEAD &&
# Merge conflict outside cone
# The sparse checkout will report a warning that is not in the
Expand All @@ -1594,7 +1602,8 @@ test_expect_success 'sparse index is not expanded: diff' '
test_all_match git diff &&
test_all_match git diff --cached &&
ensure_not_expanded diff &&
ensure_not_expanded diff --cached
ensure_not_expanded diff --cached &&
ensure_not_expanded diff-index --cached HEAD
'

test_expect_success 'sparse index is not expanded: show and rev-parse' '
Expand Down

0 comments on commit ae5e5d8

Please sign in to comment.