Skip to content

Commit 45e1179

Browse files
committed
diff: enable and test the sparse index
Enable the sparse index within the 'git diff' command. Its implementation already safely integrates with the sparse index because it shares code with the 'git status' and 'git checkout' commands that were already integrated. The most interesting thing to do is to add tests that verify that 'git diff' behaves correctly when the sparse index is enabled. These cases are... 1. The index is not expanded for `diff` and `diff --staged` (which both involve reading from the index). 2. `diff` and `diff --staged` behave the same in full checkout, sparse checkout, and sparse index repositories in the following partially-staged scenarios (i.e. the index, HEAD, and working directory differ at a given path): 1. Path is within sparse-checkout cone. 2. Path is outside sparse-checkout cone. 3. A merge conflict exists for paths outside sparse-checkout cone. Signed-off-by: Lessley Dennington <[email protected]>
1 parent f28fc01 commit 45e1179

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

builtin/diff.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
437437

438438
prefix = setup_git_directory_gently(&nongit);
439439

440+
prepare_repo_settings(the_repository);
441+
the_repository->settings.command_requires_full_index = 0;
442+
440443
if (!no_index) {
441444
/*
442445
* Treat git diff with at least one path outside of the

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,39 @@ test_expect_success 'diff --staged' '
387387
test_all_match git diff --staged
388388
'
389389

390+
test_expect_success 'diff partially-staged' '
391+
init_repos &&
392+
393+
# Add file within cone
394+
test_all_match git sparse-checkout set deep &&
395+
run_on_all 'echo >deep/testfile' &&
396+
test_all_match git add deep/testfile &&
397+
run_on_all 'echo a new line >>deep/testfile' &&
398+
399+
test_all_match git diff &&
400+
test_all_match git diff --staged &&
401+
402+
# Add file outside cone
403+
test_all_match git reset --hard &&
404+
run_on_all mkdir newdirectory &&
405+
run_on_all 'echo >newdirectory/testfile' &&
406+
test_all_match git sparse-checkout set newdirectory &&
407+
test_all_match git add newdirectory/testfile &&
408+
run_on_all 'echo a new line >>newdirectory/testfile' &&
409+
test_all_match git sparse-checkout set &&
410+
411+
test_all_match git diff &&
412+
test_all_match git diff --staged &&
413+
414+
# Merge conflict outside cone
415+
test_all_match git reset --hard &&
416+
test_all_match git checkout merge-left &&
417+
test_all_match test_must_fail git merge merge-right &&
418+
419+
test_all_match git diff &&
420+
test_all_match git diff --staged
421+
'
422+
390423
# NEEDSWORK: sparse-checkout behaves differently from full-checkout when
391424
# running this test with 'df-conflict-2' after 'df-conflict-1'.
392425
test_expect_success 'diff with renames and conflicts' '
@@ -822,6 +855,12 @@ test_expect_success 'sparse-index is not expanded' '
822855
ensure_not_expanded reset base -- folder1 &&
823856
824857
ensure_not_expanded reset --hard update-deep &&
858+
859+
echo a test change >>sparse-index/README.md &&
860+
ensure_not_expanded diff &&
861+
git -C sparse-index add README.md &&
862+
ensure_not_expanded diff --staged &&
863+
825864
ensure_not_expanded checkout -f update-deep &&
826865
(
827866
sane_unset GIT_TEST_MERGE_ALGORITHM &&

0 commit comments

Comments
 (0)