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 <vdye@github.com> Signed-off-by: Shuqi Liang <cheskaqiqi@gmail.com> Acked-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shuqi Liang committed Jun 6, 2023 at 13:26 UTC 8fac776f441c69af60825452a3b5d4adecd47ad9
3 files changed +42
builtin/worktree.c
+4
@@ -1200,5 +1200,9 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
1200 prefix = "";
1201
1202 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 +
1207 return fn(ac, av, prefix);
1208 }
t/perf/p2000-sparse-operations.sh
+1
@@ -131,5 +131,6 @@ test_perf_on_all git describe --dirty
131 test_perf_on_all 'echo >>new && git describe --dirty'
132 test_perf_on_all git diff-files
133 test_perf_on_all git diff-files -- $SPARSE_CONE/a
134 +test_perf_on_all "git worktree add ../temp && git worktree remove ../temp"
135
136 test_done
t/t1092-sparse-checkout-compatibility.sh
+37
@@ -2180,4 +2180,41 @@ test_expect_success 'sparse index is not expanded: diff-files' '
2180 ensure_not_expanded diff-files -- "deep/*"
2181 '
2182
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 +
2220 test_done