* [PATCH 1/2] pathspec: use match for sparse-index expansion checks
2026-07-20 22:31 [PATCH 0/2] stash: avoid sparse-index expansion for in-cone paths tnyman
@ 2026-07-20 22:31 ` tnyman
2026-07-20 22:31 ` [PATCH 2/2] stash: avoid sparse-index expansion for in-cone paths tnyman
1 sibling, 0 replies; 3+ messages in thread
From: tnyman @ 2026-07-20 22:31 UTC (permalink / raw)
To: git; +Cc: Ted Nyman, Derrick Stolee, Taylor Blau, Jeff King, Victoria Dye
From: Ted Nyman <tnyman@openai.com>
The pathspec parser computes `len` and `nowildcard_len` from
`item.match`, which includes any prefix added when a command is run
from a subdirectory. `item.original` can still contain the shorter,
unprefixed argument.
Using `item.original + item.nowildcard_len` in
`pathspec_needs_expanded_index()` can therefore read past the end of
the allocation. AddressSanitizer reports a heap-buffer-overflow for
prefixed wildcard pathspecs passed to `git rm` and `git reset` with a
sparse index.
The mismatch dates back to 4d1cfc1351 ("reset: make --mixed
sparse-aware", 2021-11-29), which introduced the helper using
`item.original`. b29ad38322 ("pathspec.h: move
pathspec_needs_expanded_index() from reset.c to here", 2022-08-07)
later moved it to `pathspec.c` and preserved the affected comparisons.
Use `item.match` consistently when checking whether a pathspec can
match a sparse-directory entry. Add coverage for prefixed wildcard
pathspecs so both commands keep the index sparse.
Signed-off-by: Ted Nyman <tnyman@openai.com>
---
pathspec.c | 12 ++++++------
t/t1092-sparse-checkout-compatibility.sh | 7 +++++++
2 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/pathspec.c b/pathspec.c
index f78b22709ccb67..281858f21f9c59 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -847,9 +847,9 @@ int pathspec_needs_expanded_index(struct index_state *istate,
* - not-in-cone/bar*: may need expanded index
* - **.c: may need expanded index
*/
- if (strspn(item.original + item.nowildcard_len, "*") ==
+ if (strspn(item.match + item.nowildcard_len, "*") ==
(unsigned int)(item.len - item.nowildcard_len) &&
- path_in_cone_mode_sparse_checkout(item.original, istate))
+ path_in_cone_mode_sparse_checkout(item.match, istate))
continue;
for (pos = 0; pos < istate->cache_nr; pos++) {
@@ -865,7 +865,7 @@ int pathspec_needs_expanded_index(struct index_state *istate,
*/
if ((unsigned int)item.nowildcard_len >
ce_namelen(ce) &&
- !strncmp(item.original, ce->name,
+ !strncmp(item.match, ce->name,
ce_namelen(ce))) {
res = 1;
break;
@@ -876,13 +876,13 @@ int pathspec_needs_expanded_index(struct index_state *istate,
* directory and the pathspec does not match the whole
* directory, need to expand the index.
*/
- if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
- wildmatch(item.original, ce->name, 0)) {
+ if (!strncmp(item.match, ce->name, item.nowildcard_len) &&
+ wildmatch(item.match, ce->name, 0)) {
res = 1;
break;
}
}
- } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
+ } else if (!path_in_cone_mode_sparse_checkout(item.match, istate) &&
!matches_skip_worktree(pathspec, i, &skip_worktree_seen))
res = 1;
diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh
index 9814431cd74aff..d0b42371663f9d 100755
--- a/t/t1092-sparse-checkout-compatibility.sh
+++ b/t/t1092-sparse-checkout-compatibility.sh
@@ -2119,6 +2119,13 @@ test_expect_success 'sparse index is not expanded: rm' '
ensure_not_expanded rm -r deep
'
+test_expect_success 'sparse index is not expanded: prefixed wildcard pathspec' '
+ init_repos &&
+
+ ensure_not_expanded -C deep rm --dry-run -- "a*" &&
+ ensure_not_expanded -C deep reset base -- "a*"
+'
+
test_expect_success 'grep with and --cached' '
init_repos &&
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/2] stash: avoid sparse-index expansion for in-cone paths
2026-07-20 22:31 [PATCH 0/2] stash: avoid sparse-index expansion for in-cone paths tnyman
2026-07-20 22:31 ` [PATCH 1/2] pathspec: use match for sparse-index expansion checks tnyman
@ 2026-07-20 22:31 ` tnyman
1 sibling, 0 replies; 3+ messages in thread
From: tnyman @ 2026-07-20 22:31 UTC (permalink / raw)
To: git; +Cc: Ted Nyman, Derrick Stolee, Taylor Blau, Jeff King, Victoria Dye
From: Ted Nyman <tnyman@openai.com>
`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>
---
builtin/stash.c | 4 +-
t/perf/p2000-sparse-operations.sh | 1 +
t/t1092-sparse-checkout-compatibility.sh | 55 ++++++++++++++++++++++++
3 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/builtin/stash.c b/builtin/stash.c
index c4809f299a313b..72c52571f8c06c 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -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);
diff --git a/t/perf/p2000-sparse-operations.sh b/t/perf/p2000-sparse-operations.sh
index aadf22bc2f0bb2..548a61cd9064bc 100755
--- a/t/perf/p2000-sparse-operations.sh
+++ b/t/perf/p2000-sparse-operations.sh
@@ -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 .
diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh
index d0b42371663f9d..4140c4d8ef2436 100755
--- a/t/t1092-sparse-checkout-compatibility.sh
+++ b/t/t1092-sparse-checkout-compatibility.sh
@@ -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 &&
^ permalink raw reply related [flat|nested] 3+ messages in thread