Skip to content

Commit efab7dc

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 02ed855 commit efab7dc

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

builtin/reset.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ int cmd_reset(int argc,
420420
oidcpy(&oid, &tree->object.oid);
421421
}
422422

423+
prepare_repo_settings(the_repository);
424+
the_repository->settings.command_requires_full_index = 0;
425+
423426
if (patch_mode) {
424427
if (reset_type != NONE)
425428
die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
@@ -457,9 +460,6 @@ int cmd_reset(int argc,
457460
if (intent_to_add && reset_type != MIXED)
458461
die(_("the option '%s' requires '%s'"), "-N", "--mixed");
459462

460-
prepare_repo_settings(the_repository);
461-
the_repository->settings.command_requires_full_index = 0;
462-
463463
if (repo_read_index(the_repository) < 0)
464464
die(_("index file corrupt"));
465465

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ test_expect_success 'add, commit, checkout' '
384384
test_all_match git checkout -
385385
'
386386

387-
test_expect_success 'git add -p' '
387+
test_expect_success 'git add, checkout, and reset with -p' '
388388
init_repos &&
389389
390390
write_script edit-contents <<-\EOF &&
@@ -398,7 +398,7 @@ test_expect_success 'git add -p' '
398398
test_write_lines y n >in &&
399399
run_on_all git add -p <in &&
400400
test_all_match git status --porcelain=v2 &&
401-
test_all_match git reset &&
401+
test_all_match git reset -p <in &&
402402
403403
test_write_lines u 1 "" q >in &&
404404
run_on_all git add -i <in &&
@@ -413,6 +413,12 @@ test_expect_success 'git add -p' '
413413
test_sparse_match git reset &&
414414
test_write_lines u 2 3 "" q >in &&
415415
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 &&
416422
test_sparse_match git status --porcelain=v2
417423
'
418424

@@ -2460,6 +2466,38 @@ test_expect_success 'sparse-index is not expanded: git add -p' '
24602466
ensure_expanded add -i <in
24612467
'
24622468

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+
24632501
test_expect_success 'advice.sparseIndexExpanded' '
24642502
init_repos &&
24652503

0 commit comments

Comments
 (0)