Skip to content

Commit 11ce430

Browse files
committed
Merge branch 'ds/sparse-apply-add-p' into next
"git apply" and "git add -i/-p" code paths no longer unnecessarily expand sparse-index while working. * ds/sparse-apply-add-p: p2000: add performance test for 'git add -p' git add: make -p/-i aware of sparse index apply: integrate with the sparse index
2 parents d8fb987 + 98ef755 commit 11ce430

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
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
/*

builtin/apply.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static const char * const apply_usage[] = {
1212
int cmd_apply(int argc,
1313
const char **argv,
1414
const char *prefix,
15-
struct repository *repo UNUSED)
15+
struct repository *repo)
1616
{
1717
int force_apply = 0;
1818
int options = 0;
@@ -35,6 +35,11 @@ int cmd_apply(int argc,
3535
&state, &force_apply, &options,
3636
apply_usage);
3737

38+
if (repo) {
39+
prepare_repo_settings(repo);
40+
repo->settings.command_requires_full_index = 0;
41+
}
42+
3843
if (check_apply_state(&state, force_apply))
3944
exit(128);
4045

t/perf/p2000-sparse-operations.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,6 @@ test_perf_on_all git diff-tree HEAD
135135
test_perf_on_all git diff-tree HEAD -- $SPARSE_CONE/a
136136
test_perf_on_all "git worktree add ../temp && git worktree remove ../temp"
137137
test_perf_on_all git check-attr -a -- $SPARSE_CONE/a
138+
test_perf_on_all 'echo >>a && test_write_lines y | git add -p'
138139

139140
test_done

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 102 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
@@ -1340,6 +1372,30 @@ test_expect_success 'submodule handling' '
13401372
grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
13411373
'
13421374

1375+
test_expect_success 'git apply functionality' '
1376+
init_repos &&
1377+
1378+
test_all_match git checkout base &&
1379+
1380+
git -C full-checkout diff base..merge-right -- deep >patch-in-sparse &&
1381+
git -C full-checkout diff base..merge-right -- folder2 >patch-outside &&
1382+
1383+
# Apply a patch to a file inside the sparse definition
1384+
test_all_match git apply --index --stat ../patch-in-sparse &&
1385+
test_all_match git status --porcelain=v2 &&
1386+
1387+
# Apply a patch to a file outside the sparse definition
1388+
test_sparse_match test_must_fail git apply ../patch-outside &&
1389+
grep "No such file or directory" sparse-checkout-err &&
1390+
1391+
# But it works with --index and --cached
1392+
test_all_match git apply --index --stat ../patch-outside &&
1393+
test_all_match git status --porcelain=v2 &&
1394+
test_all_match git reset --hard &&
1395+
test_all_match git apply --cached --stat ../patch-outside &&
1396+
test_all_match git status --porcelain=v2
1397+
'
1398+
13431399
# When working with a sparse index, some commands will need to expand the
13441400
# index to operate properly. If those commands also write the index back
13451401
# to disk, they need to convert the index to sparse before writing.
@@ -2345,6 +2401,52 @@ test_expect_success 'sparse-index is not expanded: check-attr' '
23452401
ensure_not_expanded check-attr -a --cached -- folder1/a
23462402
'
23472403

2404+
test_expect_success 'sparse-index is not expanded: git apply' '
2405+
init_repos &&
2406+
2407+
git -C sparse-index checkout base &&
2408+
git -C full-checkout diff base..merge-right -- deep >patch-in-sparse &&
2409+
git -C full-checkout diff base..merge-right -- folder2 >patch-outside &&
2410+
2411+
# Apply a patch to a file inside the sparse definition
2412+
ensure_not_expanded apply --index --stat ../patch-in-sparse &&
2413+
2414+
# Apply a patch to a file outside the sparse definition
2415+
# Fails when caring about the worktree.
2416+
ensure_not_expanded ! apply ../patch-outside &&
2417+
2418+
# Expands when using --index.
2419+
ensure_expanded apply --index ../patch-outside &&
2420+
git -C sparse-index reset --hard &&
2421+
2422+
# Does not expand when using --cached.
2423+
ensure_not_expanded apply --cached ../patch-outside
2424+
'
2425+
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+
23482450
test_expect_success 'advice.sparseIndexExpanded' '
23492451
init_repos &&
23502452

0 commit comments

Comments
 (0)