1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! Stash stack operations scoped to a [`Repository`](crate::repo::Repository).
//!
//! [`RepositoryStash`] (from [`Repository::stash`](crate::repo::Repository::stash)) is the one
//! public stash API; option and result types live in [`crate::stash`].
use crate::diff::{DiffEntry, DiffOptions, DiffStat};
use crate::objects::ObjectId;
use crate::porcelain::stash::{
apply_stash_entry, apply_stash_oid, apply_stash_reference, clear_stashes, create_stash,
drop_stash, list_stashes, parse_stash_reference, pop_stash_entry, pop_stash_reference,
preview_stash, push_stash, stash_branch, stash_branch_reference, stash_diff, stash_diff_stat,
stash_index_for_oid, StashApplyOptions, StashApplyOutcome, StashBranchOutcome,
StashDiffOptions, StashEntry, StashPreview, StashPushOptions, StashPushResult, StashReference,
};
use crate::progress::{CancelToken, ProgressSink};
use crate::repo::Repository;
/// Stash list/push/apply/pop/drop for one open repository.
///
/// Entries are addressed by their newest-first index (`0` is `stash@{0}`). Operations that touch
/// the worktree return [`crate::stash::StashError::BareRepository`] on a bare repository.
#[derive(Debug, Copy, Clone)]
pub struct RepositoryStash<'repo> {
repo: &'repo Repository,
}
impl<'repo> RepositoryStash<'repo> {
pub(crate) fn new(repo: &'repo Repository) -> Self {
Self { repo }
}
/// Borrow the underlying repository.
#[must_use]
pub fn repository(&self) -> &'repo Repository {
self.repo
}
/// List stash entries from `refs/stash` reflog (newest first).
pub fn list(&self) -> crate::error::Result<Vec<StashEntry>> {
list_stashes(self.repo)
}
/// Save local changes as a new `stash@{0}` and reset the saved paths to `HEAD`.
///
/// Returns `None` when there is nothing to stash. Untracked paths included in the stash
/// are removed from the worktree.
///
/// Hashing local changes reports [`crate::progress::ProgressPhase::StagingFiles`] and the
/// reset reports [`crate::progress::ProgressPhase::CheckingOutFiles`] to `progress`.
///
/// # Errors
///
/// - [`crate::stash::StashPushValidationError::DryRunUsePreview`] when
/// [`StashPushOptions::dry_run`] is set (use [`Self::preview`]).
/// - [`crate::stash::StashError::NoInitialCommit`] before the first commit.
/// - [`crate::error::Error::Cancelled`] when [`crate::progress::CancelToken::is_cancelled`]
/// is true on the `cancel` argument. Before `refs/stash` moves, the repository is
/// unchanged; once the entry is stored it stays, even when the reset that follows is
/// cancelled (the same rule as a failed reset).
/// - Stash build, ref, and checkout failures.
///
/// # Examples
///
/// ```
/// # use grit_lib::repo::Repository;
/// # use grit_lib::config::ident_resolve::IdentRole;
/// # use grit_lib::stash::StashPushOptions;
/// # use std::path::Path;
/// # fn demo() -> grit_lib::error::Result<()> {
/// let repo = Repository::discover(Some(Path::new(".")), &grit_lib::config::environment::RepositoryOptions::empty())?;
/// let mut opts = StashPushOptions::new(
/// repo.identity(IdentRole::Committer, repo.identity_now())?,
/// );
/// opts.message = Some("wip".into());
/// let _ = repo.stash().push(
/// &opts,
/// &mut grit_lib::progress::NullProgress,
/// &grit_lib::progress::CancelToken::never(),
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn push(
&self,
options: &StashPushOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> crate::error::Result<Option<StashPushResult>> {
push_stash(self.repo, options, progress, cancel)
}
/// Report which paths [`Self::push`] would save, without writing objects, refs, or files.
pub fn preview(
&self,
options: &StashPushOptions,
) -> crate::error::Result<Option<StashPreview>> {
preview_stash(self.repo, options)
}
/// Build a stash commit without updating `refs/stash` or touching the worktree.
pub fn create(&self, options: &StashPushOptions) -> crate::error::Result<Option<ObjectId>> {
create_stash(self.repo, options)
}
/// Apply `stash@{index}` without dropping it.
pub fn apply(
&self,
index: usize,
options: &StashApplyOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> crate::error::Result<StashApplyOutcome> {
apply_stash_entry(self.repo, index, options, progress, cancel)
}
/// Apply any stash-shaped commit without touching `refs/stash`.
pub fn apply_commit(
&self,
stash: &ObjectId,
options: &StashApplyOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> crate::error::Result<StashApplyOutcome> {
apply_stash_oid(self.repo, stash, options, progress, cancel)
}
/// Apply `stash@{index}` and drop it when it applied without conflicts.
pub fn pop(
&self,
index: usize,
options: &StashApplyOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> crate::error::Result<StashApplyOutcome> {
pop_stash_entry(self.repo, index, options, progress, cancel)
}
/// Remove `stash@{index}`, moving `refs/stash` to the next entry (or deleting it).
pub fn drop(&self, index: usize) -> crate::error::Result<ObjectId> {
drop_stash(self.repo, index)
}
/// Parse a stash selector (`stash@{{n}}`, decimal index, or commit id).
pub fn parse_reference(&self, spec: Option<&str>) -> crate::error::Result<StashReference> {
parse_stash_reference(self.repo, spec)
}
/// Newest-first index of a stash W commit on `refs/stash`, if present.
#[must_use]
pub fn index_for_oid(&self, oid: &ObjectId) -> Option<usize> {
stash_index_for_oid(self.repo, oid)
}
/// Apply a parsed stash reference without dropping it.
pub fn apply_reference(
&self,
reference: StashReference,
options: &StashApplyOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> crate::error::Result<StashApplyOutcome> {
apply_stash_reference(self.repo, reference, options, progress, cancel)
}
/// Pop a parsed stash reference; commit ids must still be on `refs/stash`.
pub fn pop_reference(
&self,
reference: StashReference,
options: &StashApplyOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> crate::error::Result<StashApplyOutcome> {
pop_stash_reference(self.repo, reference, options, progress, cancel)
}
/// Create a branch from a parsed stash reference (on- or off-stack).
pub fn branch_reference(
&self,
name: &str,
reference: StashReference,
) -> crate::error::Result<StashBranchOutcome> {
stash_branch_reference(self.repo, name, reference)
}
/// Remove every stash entry, returning how many were removed.
pub fn clear(&self) -> crate::error::Result<usize> {
clear_stashes(self.repo)
}
/// Tree diff of `stash@{index}` against the commit it was made on.
pub fn diff(
&self,
index: usize,
options: &StashDiffOptions,
) -> crate::error::Result<Vec<DiffEntry>> {
stash_diff(self.repo, index, options)
}
/// Per-file line counts of [`Self::diff`] (`git stash show --stat`).
///
/// Counts come from the line-count engine, without building hunks. Files whose every
/// change `diff_options.whitespace` ignores are left out, as Git does (see
/// [`ListedFileDiff::changes_all_ignored`](crate::diff::ListedFileDiff::changes_all_ignored)).
///
/// # Parameters
///
/// - `index`: newest-first stash index (`0` is `stash@{0}`).
/// - `options`: which stash parents to include.
/// - `diff_options`: line-diff settings, e.g. [`DiffOptions::from_config`].
///
/// # Errors
///
/// The [`Self::diff`] errors, plus config load and object read failures.
///
/// # Examples
///
/// ```
/// # use grit_lib::repo::Repository;
/// # use grit_lib::diff::DiffOptions;
/// # use grit_lib::stash::StashDiffOptions;
/// # use std::path::Path;
/// # fn demo() -> grit_lib::error::Result<()> {
/// let repo = Repository::discover(Some(Path::new(".")), &grit_lib::config::environment::RepositoryOptions::empty())?;
/// let diff_options = DiffOptions::from_config(repo.config().snapshot()?.as_ref());
/// let stat = repo.stash().diff_stat(0, &StashDiffOptions::default(), diff_options)?;
/// let _ = stat.totals().insertions;
/// # Ok(())
/// # }
/// ```
pub fn diff_stat(
&self,
index: usize,
options: &StashDiffOptions,
diff_options: DiffOptions,
) -> crate::error::Result<DiffStat> {
stash_diff_stat(self.repo, index, options, diff_options)
}
/// Create branch `name` at the stash base, switch, apply with index, drop when clean.
pub fn branch(&self, name: &str, index: usize) -> crate::error::Result<StashBranchOutcome> {
stash_branch(self.repo, name, index)
}
}