The stash stack is Git-compatible ref and commit machinery. There is one API for it: Repository::stash returns a RepositoryStash handle with list, push, preview, create, apply, apply_commit, pop, drop, clear, diff and branch. The option and result types live in stash. Entries are addressed by their newest-first index (0 is stash@{0}), and the handle works on the repository's own work tree, so there is no separate work-tree argument. parse_stash_reference and StashReference parse the same selectors as the CLI (stash@{{n}}, an all-decimal index, or any other revision as a commit id). apply_reference, pop_reference, and branch_reference take a parsed reference; drop returns the removed W commit id.
Push and list
StashPushOptions carries the message, whether to include untracked files, StashMode (All, StagedOnly, KeepIndex), optional hunk selection and pathspecs, and the Signature used for the stash commits and the refs/stash reflog (usually repo.identity(IdentRole::Committer, now), see Repository). StashPushOptions::new sets the other fields to their defaults. RepositoryStash::push returns a StashPushResult (the stash commit, its message and tree), or Ok(None) when there is nothing to stash. RepositoryStash::preview takes the same options and lists the paths that would be saved without writing objects or refs. For a partial push (a hunk selection), both the result and the StashPreview report what the selection resolved to: stashed_hunks maps each path to the hunk ids it stashes and stashed_lines to its line range labels, so callers do not need to resolve the selection again. StashPushOptions::validate rejects incompatible flag combinations as StashPushValidationError. Stash commits are always written unsigned, whatever commit.gpgsign says, matching Git.
push, apply, apply_commit and pop take a ProgressSink and a CancelToken. Push reports StagingFiles while it hashes local changes and CheckingOutFiles while it resets them. A cancel before refs/stash moves returns Error::Cancelled with the repository unchanged; once the entry is stored it stays, even if the reset that follows is cancelled. A cancelled apply or pop keeps the entry, leaves the index alone and rolls its checkout back; only files it content-merged onto a moved HEAD before the cancel stay written.
Apply and pop
RepositoryStash::apply applies stash@{n} and keeps the entry. RepositoryStash::pop applies it and drops it only when it applied cleanly. Both take one StashApplyOptions (restore_index restores the stashed index, like git stash apply --index; overwrite_identical_untracked skips untracked collisions when on-disk bytes already match the stashed copy) and return a StashApplyOutcome whose conflicts lists each path left with conflict stages in the index as an operation::Conflict, the same paths git ls-files -u shows afterwards, with the same ConflictKind status reports for them. has_conflicts() is true exactly when a pop kept the entry. RepositoryStash::apply_commit applies any stash-shaped commit, such as one built by RepositoryStash::create, without touching refs/stash. Stash operations return StashError: an index past the end of the stack is NotFound, a malformed stash commit is CorruptStash, apply/pop refuse to overwrite local tracked edits with WouldOverwriteLocalChanges, and untracked collisions use CouldNotRestoreUntracked. These arrive as Error::Stash; repository, index or object failures underneath arrive as their own area variants.
Dropping an entry rewrites the refs/stash reflog without adding a new entry, so drop, pop and branch take no identity. drop returns that entry's W commit id so embedders can log it or recover with apply_commit.
use grit_lib::stash::StashApplyOptions;
use grit_lib::progress::{CancelToken, NullProgress};
let popped = repo
.stash()
.pop(0, &StashApplyOptions::default(), &mut NullProgress, &CancelToken::never())?;
for conflict in &popped.conflicts {
println!(
"conflict: {} ({}; stash@{{0}} was kept)",
conflict.path,
conflict.kind.as_str()
);
}