Skip to content

Commit 8fac776

Browse files
Cheskaqiqigitster
authored andcommitted
worktree: integrate with sparse-index
The index is read in 'worktree.c' at two points: 1.The 'validate_no_submodules' function, which checks if there are any submodules present in the worktree. 2.The 'check_clean_worktree' function, which verifies if a worktree is 'clean', i.e., there are no untracked or modified but uncommitted files. This is done by running the 'git status' command, and an error message is thrown if the worktree is not clean. Given that 'git status' is already sparse-aware, the function is also sparse-aware. Hence we can just set the requires-full-index to false for "git worktree". Add tests that verify that 'git worktree' behaves correctly when the sparse index is enabled and test to ensure the index is not expanded. The `p2000` tests demonstrate a ~20% execution time reduction for 'git worktree' using a sparse index: (Note:the p2000 test results didn't reflect the huge speedup because of the index reading time is minuscule comparing to the filesystem operations.) Test before after ----------------------------------------------------------------------- 2000.102: git worktree add....(full-v3) 3.15 2.82 -10.5% 2000.103: git worktree add....(full-v4) 3.14 2.84 -9.6% 2000.104: git worktree add....(sparse-v3) 2.59 2.14 -16.4% 2000.105: git worktree add....(sparse-v4) 2.10 1.57 -25.2% Helped-by: Victoria Dye <[email protected]> Signed-off-by: Shuqi Liang <[email protected]> Acked-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fe86abd commit 8fac776

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

builtin/worktree.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,5 +1200,9 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
12001200
prefix = "";
12011201

12021202
ac = parse_options(ac, av, prefix, options, git_worktree_usage, 0);
1203+
1204+
prepare_repo_settings(the_repository);
1205+
the_repository->settings.command_requires_full_index = 0;
1206+
12031207
return fn(ac, av, prefix);
12041208
}

t/perf/p2000-sparse-operations.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,6 @@ test_perf_on_all git describe --dirty
131131
test_perf_on_all 'echo >>new && git describe --dirty'
132132
test_perf_on_all git diff-files
133133
test_perf_on_all git diff-files -- $SPARSE_CONE/a
134+
test_perf_on_all "git worktree add ../temp && git worktree remove ../temp"
134135

135136
test_done

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,4 +2180,41 @@ test_expect_success 'sparse index is not expanded: diff-files' '
21802180
ensure_not_expanded diff-files -- "deep/*"
21812181
'
21822182

2183+
test_expect_success 'worktree' '
2184+
init_repos &&
2185+
2186+
write_script edit-contents <<-\EOF &&
2187+
echo text >>"$1"
2188+
EOF
2189+
2190+
for repo in full-checkout sparse-checkout sparse-index
2191+
do
2192+
worktree=${repo}-wt &&
2193+
git -C $repo worktree add ../$worktree &&
2194+
2195+
# Compare worktree content with "ls"
2196+
(cd $repo && ls) >worktree_contents &&
2197+
(cd $worktree && ls) >new_worktree_contents &&
2198+
test_cmp worktree_contents new_worktree_contents &&
2199+
2200+
# Compare index content with "ls-files --sparse"
2201+
git -C $repo ls-files --sparse >index_contents &&
2202+
git -C $worktree ls-files --sparse >new_index_contents &&
2203+
test_cmp index_contents new_index_contents &&
2204+
2205+
git -C $repo worktree remove ../$worktree || return 1
2206+
done &&
2207+
2208+
test_all_match git worktree add .worktrees/hotfix &&
2209+
run_on_all ../edit-contents .worktrees/hotfix/deep/a &&
2210+
test_all_match test_must_fail git worktree remove .worktrees/hotfix
2211+
'
2212+
2213+
test_expect_success 'worktree is not expanded' '
2214+
init_repos &&
2215+
2216+
ensure_not_expanded worktree add .worktrees/hotfix &&
2217+
ensure_not_expanded worktree remove .worktrees/hotfix
2218+
'
2219+
21832220
test_done

0 commit comments

Comments
 (0)