reset: integrate sparse index with --patch

Similar to the previous change for 'git add -p', the reset builtin checked for integration with the sparse index after possibly redirecting its logic toward the interactive logic. This means that the builtin would expand the sparse index to a full one upon read. Move this check earlier within cmd_reset() to improve performance here. Add tests to guarantee that we are not universally expanding the index. Add behavior tests to check that we are doing the same operations as a full index. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed May 16, 2025 at 14:55 UTC efab7dc1f49ef95f00560c30bca357dd445e7cb4
2 files changed +43 -5
builtin/reset.c
+3 -3
@@ -420,6 +420,9 @@ int cmd_reset(int argc,
420 oidcpy(&oid, &tree->object.oid);
421 }
422
423 + prepare_repo_settings(the_repository);
424 + the_repository->settings.command_requires_full_index = 0;
425 +
426 if (patch_mode) {
427 if (reset_type != NONE)
428 die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
@@ -457,9 +460,6 @@ int cmd_reset(int argc,
460 if (intent_to_add && reset_type != MIXED)
461 die(_("the option '%s' requires '%s'"), "-N", "--mixed");
462
460 - prepare_repo_settings(the_repository);
461 - the_repository->settings.command_requires_full_index = 0;
462 -
463 if (repo_read_index(the_repository) < 0)
464 die(_("index file corrupt"));
465
t/t1092-sparse-checkout-compatibility.sh
+40 -2
@@ -384,7 +384,7 @@ test_expect_success 'add, commit, checkout' '
384 test_all_match git checkout -
385 '
386
387 -test_expect_success 'git add -p' '
387 +test_expect_success 'git add, checkout, and reset with -p' '
388 init_repos &&
389
390 write_script edit-contents <<-\EOF &&
@@ -398,7 +398,7 @@ test_expect_success 'git add -p' '
398 test_write_lines y n >in &&
399 run_on_all git add -p <in &&
400 test_all_match git status --porcelain=v2 &&
401 - test_all_match git reset &&
401 + test_all_match git reset -p <in &&
402
403 test_write_lines u 1 "" q >in &&
404 run_on_all git add -i <in &&
@@ -413,6 +413,12 @@ test_expect_success 'git add -p' '
413 test_sparse_match git reset &&
414 test_write_lines u 2 3 "" q >in &&
415 run_on_all git add -i <in &&
416 + test_sparse_match git status --porcelain=v2 &&
417 +
418 + run_on_all git add --sparse folder1 &&
419 + run_on_all git commit -m "take changes" &&
420 + test_write_lines y n y >in &&
421 + test_sparse_match git checkout HEAD~1 --patch <in &&
422 test_sparse_match git status --porcelain=v2
423 '
424
@@ -2460,6 +2466,38 @@ test_expect_success 'sparse-index is not expanded: git add -p' '
2466 ensure_expanded add -i <in
2467 '
2468
2469 +test_expect_success 'sparse-index is not expanded: checkout -p, reset -p' '
2470 + init_repos &&
2471 +
2472 + # Does not expand when edits are within sparse checkout.
2473 + echo "new content" >sparse-index/deep/a &&
2474 + echo "new content" >sparse-index/deep/deeper1/a &&
2475 + git -C sparse-index commit -a -m "inside-changes" &&
2476 +
2477 + test_write_lines y y >in &&
2478 + ensure_not_expanded checkout HEAD~1 --patch <in &&
2479 +
2480 + echo "new content" >sparse-index/deep/a &&
2481 + echo "new content" >sparse-index/deep/deeper1/a &&
2482 + git -C sparse-index add . &&
2483 + ensure_not_expanded reset --patch <in &&
2484 +
2485 + # -p does expand when edits are outside sparse checkout.
2486 + mkdir -p sparse-index/folder1 &&
2487 + echo "new content" >sparse-index/folder1/a &&
2488 + git -C sparse-index add --sparse folder1 &&
2489 + git -C sparse-index sparse-checkout reapply &&
2490 + ensure_expanded reset --patch <in &&
2491 +
2492 + # Fully reset the index.
2493 + mkdir -p sparse-index/folder1 &&
2494 + echo "new content" >sparse-index/folder1/a &&
2495 + git -C sparse-index add --sparse folder1 &&
2496 + git -C sparse-index commit -m "folder1 change" &&
2497 + git -C sparse-index sparse-checkout reapply &&
2498 + ensure_expanded checkout HEAD~1 --patch <in
2499 +'
2500 +
2501 test_expect_success 'advice.sparseIndexExpanded' '
2502 init_repos &&
2503