diff-index: integrate with the sparse index

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 51ba65b5c35 (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 <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Aug 22, 2024 at 16:03 UTC b44c926c9fb28b3847efc0057c4563ad76372e30
2 files changed +16 -3
builtin/diff-index.c
+4
@@ -25,6 +25,10 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
25 usage(diff_cache_usage);
26
27 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
28 +
29 + prepare_repo_settings(the_repository);
30 + the_repository->settings.command_requires_full_index = 0;
31 +
32 repo_init_revisions(the_repository, &rev, prefix);
33 rev.abbrev = 0;
34 prefix = precompose_argv_prefix(argc, argv, prefix);
t/t1092-sparse-checkout-compatibility.sh
+12 -3
@@ -803,6 +803,8 @@ test_expect_success 'update-index --remove outside sparse definition' '
803 test_sparse_match git diff --cached --name-status &&
804 test_cmp expect sparse-checkout-out &&
805
806 + test_sparse_match git diff-index --cached HEAD &&
807 +
808 # Reset the state
809 test_all_match git reset --hard &&
810
@@ -812,6 +814,8 @@ test_expect_success 'update-index --remove outside sparse definition' '
814 test_sparse_match git diff --cached --name-status &&
815 test_must_be_empty sparse-checkout-out &&
816
817 + test_sparse_match git diff-index --cached HEAD &&
818 +
819 # Reset the state
820 test_all_match git reset --hard &&
821
@@ -823,7 +827,9 @@ test_expect_success 'update-index --remove outside sparse definition' '
827 D folder1/a
828 EOF
829 test_sparse_match git diff --cached --name-status &&
826 - test_cmp expect sparse-checkout-out
830 + test_cmp expect sparse-checkout-out &&
831 +
832 + test_sparse_match git diff-index --cached HEAD
833 '
834
835 test_expect_success 'update-index with directories' '
@@ -1551,7 +1557,7 @@ test_expect_success 'sparse-index is not expanded: describe' '
1557 ensure_not_expanded describe
1558 '
1559
1554 -test_expect_success 'sparse index is not expanded: diff' '
1560 +test_expect_success 'sparse index is not expanded: diff and diff-index' '
1561 init_repos &&
1562
1563 write_script edit-contents <<-\EOF &&
@@ -1568,6 +1574,7 @@ test_expect_success 'sparse index is not expanded: diff' '
1574 test_all_match git diff --cached &&
1575 ensure_not_expanded diff &&
1576 ensure_not_expanded diff --cached &&
1577 + ensure_not_expanded diff-index --cached HEAD &&
1578
1579 # Add file outside cone
1580 test_all_match git reset --hard &&
@@ -1582,6 +1589,7 @@ test_expect_success 'sparse index is not expanded: diff' '
1589 test_all_match git diff --cached &&
1590 ensure_not_expanded diff &&
1591 ensure_not_expanded diff --cached &&
1592 + ensure_not_expanded diff-index --cached HEAD &&
1593
1594 # Merge conflict outside cone
1595 # The sparse checkout will report a warning that is not in the
@@ -1594,7 +1602,8 @@ test_expect_success 'sparse index is not expanded: diff' '
1602 test_all_match git diff &&
1603 test_all_match git diff --cached &&
1604 ensure_not_expanded diff &&
1597 - ensure_not_expanded diff --cached
1605 + ensure_not_expanded diff --cached &&
1606 + ensure_not_expanded diff-index --cached HEAD
1607 '
1608
1609 test_expect_success 'sparse index is not expanded: show and rev-parse' '