Skip to content

Commit 0a27527

Browse files
committed
git add: make -p/-i aware of sparse index
It is slow to expand a sparse index in-memory due to parsing of trees. We aim to minimize that performance cost when possible. 'git add -p' uses 'git apply' child processes to modify the index, but still there are some expansions that occur. It turns out that control flows out of cmd_add() in the interactive cases before the lines that confirm that the builtin is integrated with the sparse index. Moving that integration point earlier in cmd_add() allows 'git add -p' and 'git add -p' to operate without expanding a sparse index to a full one. Add test cases that confirm that these interactive add options work with the sparse index. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 1adf81e commit 0a27527

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

builtin/add.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ int cmd_add(int argc,
390390

391391
argc = parse_options(argc, argv, prefix, builtin_add_options,
392392
builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
393+
394+
prepare_repo_settings(repo);
395+
repo->settings.command_requires_full_index = 0;
396+
393397
if (patch_interactive)
394398
add_interactive = 1;
395399
if (add_interactive) {
@@ -426,9 +430,6 @@ int cmd_add(int argc,
426430
add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
427431
require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
428432

429-
prepare_repo_settings(repo);
430-
repo->settings.command_requires_full_index = 0;
431-
432433
repo_hold_locked_index(repo, &lock_file, LOCK_DIE_ON_ERROR);
433434

434435
/*

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,38 @@ test_expect_success 'add, commit, checkout' '
384384
test_all_match git checkout -
385385
'
386386

387+
test_expect_success 'git add -p' '
388+
init_repos &&
389+
390+
write_script edit-contents <<-\EOF &&
391+
echo text >>$1
392+
EOF
393+
394+
# Does not expand when edits are within sparse checkout.
395+
run_on_all ../edit-contents deep/a &&
396+
run_on_all ../edit-contents deep/deeper1/a &&
397+
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 &&
402+
403+
test_write_lines u 1 "" q >in &&
404+
run_on_all git add -i <in &&
405+
test_all_match git status --porcelain=v2 &&
406+
test_all_match git reset --hard &&
407+
408+
run_on_sparse mkdir -p folder1 &&
409+
run_on_all ../edit-contents folder1/a &&
410+
test_write_lines y n y >in &&
411+
run_on_all git add -p <in &&
412+
test_sparse_match git status --porcelain=v2 &&
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+
387419
test_expect_success 'deep changes during checkout' '
388420
init_repos &&
389421
@@ -2398,6 +2430,34 @@ test_expect_success 'sparse-index is not expanded: git apply' '
23982430
ensure_expanded apply --cached ../patch-outside
23992431
'
24002432

2433+
test_expect_success 'sparse-index is not expanded: git add -p' '
2434+
init_repos &&
2435+
2436+
# Does not expand when edits are within sparse checkout.
2437+
echo "new content" >sparse-index/deep/a &&
2438+
echo "new content" >sparse-index/deep/deeper1/a &&
2439+
test_write_lines y n >in &&
2440+
ensure_not_expanded add -p <in &&
2441+
git -C sparse-index reset &&
2442+
ensure_not_expanded add -i <in &&
2443+
2444+
# -p does expand when edits are outside sparse checkout.
2445+
mkdir -p sparse-index/folder1 &&
2446+
echo "new content" >sparse-index/folder1/a &&
2447+
test_write_lines y n y >in &&
2448+
ensure_expanded add -p <in &&
2449+
2450+
# Fully reset the index.
2451+
git -C sparse-index reset --hard &&
2452+
git -C sparse-index sparse-checkout reapply &&
2453+
2454+
# -i does expand when edits are outside sparse checkout.
2455+
mkdir -p sparse-index/folder1 &&
2456+
echo "new content" >sparse-index/folder1/a &&
2457+
test_write_lines u 2 3 "" q >in &&
2458+
ensure_expanded add -i <in
2459+
'
2460+
24012461
test_expect_success 'advice.sparseIndexExpanded' '
24022462
init_repos &&
24032463

0 commit comments

Comments
 (0)