Skip to content

Commit 3e932c1

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. We need to move that earlier to ensure it prevents a full index expansion on read. Add more test cases that confirm that these interactive add options work with the sparse index. One interesting aspect here is that the '-i' option avoids expanding the sparse index when a sparse directory exists on disk while the '-p' option does hit the ensure_full_index() method. This leaves some room for improvement, but this case should be atypical as users should remain within their sparse-checkout. Signed-off-by: Derrick Stolee <[email protected]>
1 parent c5b1cba commit 3e932c1

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-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: 56 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
@@ -2391,6 +2423,30 @@ test_expect_success 'sparse-index is not expanded: git apply' '
23912423
ensure_not_expanded apply --cached ../patch-outside
23922424
'
23932425

2426+
test_expect_success 'sparse-index is not expanded: git add -p' '
2427+
init_repos &&
2428+
2429+
# Does not expand when edits are within sparse checkout.
2430+
echo "new content" >sparse-index/deep/a &&
2431+
echo "new content" >sparse-index/deep/deeper1/a &&
2432+
test_write_lines y n >in &&
2433+
ensure_not_expanded add -p <in &&
2434+
git -C sparse-index reset &&
2435+
ensure_not_expanded add -i <in &&
2436+
2437+
mkdir -p sparse-index/folder1 &&
2438+
echo "new content" >sparse-index/folder1/a &&
2439+
2440+
# -p does expand when edits are outside sparse checkout.
2441+
test_write_lines y n y >in &&
2442+
ensure_expanded add -p <in &&
2443+
2444+
# but -i does not expand.
2445+
git -C sparse-index reset &&
2446+
test_write_lines u 2 3 "" q >in &&
2447+
ensure_not_expanded add -i <in
2448+
'
2449+
23942450
test_expect_success 'advice.sparseIndexExpanded' '
23952451
init_repos &&
23962452

0 commit comments

Comments
 (0)