Skip to content

Commit d1482a2

Browse files
committed
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]>
1 parent 0a27527 commit d1482a2

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

@@ -2458,6 +2464,38 @@ test_expect_success 'sparse-index is not expanded: git add -p' '
24582464
ensure_expanded add -i <in
24592465
'
24602466

2467+
test_expect_success 'sparse-index is not expanded: checkout -p, reset -p' '
2468+
init_repos &&
2469+
2470+
# Does not expand when edits are within sparse checkout.
2471+
echo "new content" >sparse-index/deep/a &&
2472+
echo "new content" >sparse-index/deep/deeper1/a &&
2473+
git -C sparse-index commit -a -m "inside-changes" &&
2474+
2475+
test_write_lines y y >in &&
2476+
ensure_not_expanded checkout HEAD~1 --patch <in &&
2477+
2478+
echo "new content" >sparse-index/deep/a &&
2479+
echo "new content" >sparse-index/deep/deeper1/a &&
2480+
git -C sparse-index add . &&
2481+
ensure_not_expanded reset --patch <in &&
2482+
2483+
# -p does expand when edits are outside sparse checkout.
2484+
mkdir -p sparse-index/folder1 &&
2485+
echo "new content" >sparse-index/folder1/a &&
2486+
git -C sparse-index add --sparse folder1 &&
2487+
git -C sparse-index sparse-checkout reapply &&
2488+
ensure_expanded reset --patch <in &&
2489+
2490+
# Fully reset the index.
2491+
mkdir -p sparse-index/folder1 &&
2492+
echo "new content" >sparse-index/folder1/a &&
2493+
git -C sparse-index add --sparse folder1 &&
2494+
git -C sparse-index commit -m "folder1 change" &&
2495+
git -C sparse-index sparse-checkout reapply &&
2496+
ensure_expanded checkout HEAD~1 --patch <in
2497+
'
2498+
24612499
test_expect_success 'advice.sparseIndexExpanded' '
24622500
init_repos &&
24632501

0 commit comments

Comments
 (0)