]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: avoid sparse-index expansion for in-cone paths
authorTed Nyman <tnyman@openai.com>
Mon, 20 Jul 2026 22:31:21 +0000 (15:31 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 Jul 2026 17:33:25 +0000 (10:33 -0700)
`git stash push -- <pathspec>` expands a sparse index before checking
whether the pathspec matches any tracked paths. This is unnecessary
when the pathspec is wholly inside the sparse-checkout cone and makes
a path-limited stash proportional to the size of the full index.

Use `pathspec_needs_expanded_index()` to expand only when a pathspec
can match part of a sparse-directory entry, as `git rm` and `git
reset` already do. Keep the full-index behavior for pathspecs that
need it.

Add compatibility coverage for literal, prefixed, wildcard, file,
multiple, staged, and missing pathspecs. Add the corresponding
path-limited stash case to p2000.

On a cone-mode repository with 349,525 tracked paths and 49 sparse
index entries, the best of three runs changed from 18.87s to 0.06s.
Trace2 reported four index expansions before this change and none
after it.

Signed-off-by: Ted Nyman <tnyman@openai.com>
Reviewed-by: Taylor Blau <ttaylorr@openai.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/perf/p2000-sparse-operations.sh
t/t1092-sparse-checkout-compatibility.sh

index c4809f299a313bf2ad20eacbf196e5f76a18b8a0..72c52571f8c06cd9812359ae5778fdb1dd930856 100644 (file)
@@ -1702,8 +1702,8 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
        if (!include_untracked && ps->nr) {
                char *ps_matched = xcalloc(ps->nr, 1);
 
-               /* TODO: audit for interaction with sparse-index. */
-               ensure_full_index(the_repository->index);
+               if (pathspec_needs_expanded_index(the_repository->index, ps))
+                       ensure_full_index(the_repository->index);
                for (size_t i = 0; i < the_repository->index->cache_nr; i++)
                        ce_path_match(the_repository->index, the_repository->index->cache[i], ps,
                                      ps_matched);
index aadf22bc2f0bb29f0dfe390d5554a1901337f98d..548a61cd9064bc0077b07f12e2771bc9568e8400 100755 (executable)
@@ -108,6 +108,7 @@ test_perf_on_all () {
 
 test_perf_on_all git status
 test_perf_on_all 'git stash && git stash pop'
+test_perf_on_all "git stash push -- $SPARSE_CONE/a && git stash pop"
 test_perf_on_all 'echo >>new && git stash -u && git stash pop'
 test_perf_on_all git add -A
 test_perf_on_all git add .
index d4a9c0bbf2d0fdc8fdcdc814b27acd3b7aa5ecbe..c3f7bfe551a247119a36a60025ae8c18f974b1c7 100755 (executable)
@@ -1598,6 +1598,61 @@ test_expect_success 'sparse-index is not expanded: stash' '
        ensure_not_expanded stash pop
 '
 
+test_expect_success 'sparse-index is not expanded: stash in-cone pathspec' '
+       init_repos &&
+
+       echo unrelated >>sparse-index/deep/e &&
+       echo literal >>sparse-index/deep/a &&
+       ensure_not_expanded stash push -- deep/a &&
+       test_grep ! literal sparse-index/deep/a &&
+       test_grep unrelated sparse-index/deep/e &&
+       ensure_not_expanded stash pop &&
+       test_grep literal sparse-index/deep/a &&
+
+       echo prefixed >>sparse-index/deep/a &&
+       ensure_not_expanded -C deep stash push -- a &&
+       test_grep ! prefixed sparse-index/deep/a &&
+       test_grep unrelated sparse-index/deep/e &&
+       ensure_not_expanded stash pop &&
+       test_grep prefixed sparse-index/deep/a &&
+
+       echo wildcard >>sparse-index/deep/a &&
+       ensure_not_expanded stash push -- "deep/a*" &&
+       test_grep ! wildcard sparse-index/deep/a &&
+       test_grep unrelated sparse-index/deep/e &&
+       ensure_not_expanded stash pop &&
+       test_grep wildcard sparse-index/deep/a &&
+
+       echo pathspec-file >>sparse-index/deep/a &&
+       echo deep/a >pathspec-file &&
+       ensure_not_expanded stash push --pathspec-from-file=../pathspec-file &&
+       test_grep ! pathspec-file sparse-index/deep/a &&
+       test_grep unrelated sparse-index/deep/e &&
+       ensure_not_expanded stash pop &&
+       test_grep pathspec-file sparse-index/deep/a &&
+
+       echo multiple-a >>sparse-index/deep/a &&
+       echo multiple-e >>sparse-index/deep/e &&
+       ensure_not_expanded stash push -- deep/a deep/e &&
+       test_grep ! multiple-a sparse-index/deep/a &&
+       test_grep ! multiple-e sparse-index/deep/e &&
+       ensure_not_expanded stash pop &&
+       test_grep multiple-a sparse-index/deep/a &&
+       test_grep multiple-e sparse-index/deep/e &&
+
+       echo staged >>sparse-index/deep/a &&
+       git -C sparse-index add deep/a &&
+       ensure_not_expanded stash push --staged -- deep/a &&
+       test_grep ! staged sparse-index/deep/a &&
+       test_grep unrelated sparse-index/deep/e &&
+       ensure_not_expanded stash pop --index &&
+       test_grep staged sparse-index/deep/a &&
+       test_must_fail git -C sparse-index diff --cached --quiet -- deep/a &&
+
+       ensure_not_expanded ! stash push -- deep/does-not-exist &&
+       test_grep "did not match any file" sparse-index-error
+'
+
 test_expect_success 'describe tested on all' '
        init_repos &&