Skip to content

stash: speed up git_stash_save and fix path limited stashes losing changes - #7484

Open
AHSauge wants to merge 5 commits into
libgit2:mainfrom
AHSauge:feature/stash-performance
Open

AHSauge wants to merge 5 commits into
libgit2:mainfrom
AHSauge:feature/stash-performance

Conversation

@AHSauge

@AHSauge AHSauge commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

This PR is split over 5 commits and is a mixture of performance fixing and error fixing related to git_stash_save.

  1. Addresses a missing error propagation in commit_worktree where errors were treated as success
  2. Reuses index tree cache when building the workdir tree by duplicating it in commit_worktree
  3. Avoids a duplicate diff calculation by moving the HEAD-to-index and index-to-workdir diff creation from build_workdir_tree to git_stash_save_with_opts, and reuses them in ensure_there_are_changes_to_stash
  4. Fixes stashing with paths discarding unstashed changes, and limits stashed untracked files to the given paths
  5. Avoids another tree walk by limiting the reset after stashing, unless untracked or ignored files are being removed

Arguably the non-performance related fixes could be isolated in separate PRs, but my primary motivation here was to speed up the stashing.

The resulting speed-up in the Linux repo: stashing one modified file goes from 1.44s and 3 working tree walks to 0.58s and 1 working tree walk. For comparison, git stash is 0.62s.

Finally, worth noting that the test code is predominantly AI generated.

The prior code copied the repository index without its cache. This results in
unnecessary rehashing
Checking for changes resulted in a full status, essentially repeating
later HEAD-to-index and index-to-workdir diffs. In most cases this can
be avoided by using the diffs up front.

Add regression tests for corner cases this code touches, namely
untracked and ignored files.
Limit the reset and untracked files being stashed to the given paths.
Previously stashing paths would reset index and working tree to HEAD,
losing unstashed changes.
Previously, the reset would walk the entire tree again. This can be
limited to the paths being stashed if untracked or ignored files
aren't being removed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant