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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use std::sync::atomic::Ordering;
use crate::error::{ResultExt, message};
use gix_status::index_as_worktree::{Change, EntryStatus};
use crate::{
Result,
bstr::BString,
config::cache::util::ApplyLeniencyDefault,
status::{Platform, index_worktree, index_worktree::BuiltinSubmoduleStatus},
worktree::IndexPersistedOrInMemory,
};
pub(super) mod types;
use types::{ApplyChange, Item, Iter, Outcome};
/// Lifecycle
impl<Progress> Platform<'_, Progress>
where
Progress: gix_utils::progress::Progress,
{
/// Turn the platform into an iterator for changes between the head-tree and the index, and the index and the working tree,
/// while optionally listing untracked and/or ignored files.
///
/// * `patterns`
/// - Optional patterns to use to limit the paths to look at. If empty, all paths are considered.
///
/// ### Important: Undefined ordering
///
/// When compiled with the `parallel` feature, three operations are run at once:
///
/// * a dirwalk to find untracked and possibly ignored files
/// * an entry-by-entry check to see which of the tracked files changed, and how
/// * a tree-index comparison
///
/// All of these generate distinct events which may now happen in any order, so consumers
/// that are ordering dependent have to restore their desired order.
///
/// This isn't feasible to do here as it would mean that returned items would have to be delayed,
/// degrading performance for everyone who isn't order-dependent.
#[doc(alias = "diff_index_to_workdir", alias = "git2")]
pub fn into_iter(self, patterns: impl IntoIterator<Item = BString>) -> Result<Iter> {
let index = match self.index {
None => IndexPersistedOrInMemory::Persisted(self.repo.index_or_empty()?),
Some(index) => index,
};
let obtain_tree_id = || -> Result<Option<gix_hash::ObjectId>> {
Ok(match self.head_tree {
Some(None) => Some(
self.repo
.head_tree_id_or_empty()
.or_raise(|| message("Could not obtain the tree id pointed to by `HEAD`"))?
.into(),
),
Some(Some(tree_id)) => Some(tree_id),
None => None,
})
};
let skip_hash = crate::config::tree::Index::SKIP_HASH
.enrich_error(self.repo.config.resolved.boolean(crate::config::tree::Index::SKIP_HASH))
.with_lenient_default(self.repo.config.lenient_config)?
.unwrap_or_default();
let should_interrupt = self.should_interrupt.clone().unwrap_or_default();
let submodule = BuiltinSubmoduleStatus::new(self.repo.clone().into_sync(), self.submodules)?;
#[cfg(feature = "parallel")]
{
let (tx, rx) = std::sync::mpsc::channel();
let patterns: Vec<_> = patterns.into_iter().collect();
let join_tree_index = if let Some(tree_id) = obtain_tree_id()? {
std::thread::Builder::new()
.name("gix::status::tree_index::producer".into())
.spawn(crate::trace::in_thread({
let repo = self.repo.clone().into_sync();
let should_interrupt = should_interrupt.clone();
let tx = tx.clone();
let tree_index_renames = self.tree_index_renames;
let index = index.clone();
let crate::Pathspec { repo: _, stack, search } = self.repo.index_worktree_status_pathspec(
&patterns,
&index,
self.index_worktree_options.dirwalk_options.as_ref(),
)?;
move || -> std::result::Result<_, _> {
let repo = repo.to_thread_local();
let mut pathspec = crate::Pathspec {
repo: &repo,
stack,
search,
};
repo.tree_index_status(
&tree_id,
&index,
Some(&mut pathspec),
tree_index_renames,
|change, _, _| {
let action = if tx.send(change.into_owned().into()).is_err()
|| should_interrupt.load(Ordering::Acquire)
{
std::ops::ControlFlow::Break(())
} else {
std::ops::ControlFlow::Continue(())
};
Ok(action)
},
)
}
}))
.or_raise(|| message("Failed to spawn producer thread"))?
.into()
} else {
None
};
let mut collect = Collect { tx };
let join_index_worktree = std::thread::Builder::new()
.name("gix::status::index_worktree::producer".into())
.spawn(crate::trace::in_thread({
let repo = self.repo.clone().into_sync();
let options = self.index_worktree_options;
let should_interrupt = should_interrupt.clone();
let mut progress = self.progress;
move || -> Result<_> {
let repo = repo.to_thread_local();
let out = repo.index_worktree_status(
&index,
patterns,
&mut collect,
gix_status::index_as_worktree::traits::FastEq,
submodule,
&mut progress,
&should_interrupt,
options,
)?;
Ok(Outcome {
index_worktree: out,
tree_index: None,
worktree_index: index,
changes: None,
skip_hash,
})
}
}))
.or_raise(|| message("Failed to spawn producer thread"))?;
Ok(Iter {
rx_and_join: Some((rx, join_index_worktree, join_tree_index)),
should_interrupt,
index_changes: Vec::new(),
out: None,
})
}
#[cfg(not(feature = "parallel"))]
{
let mut collect = Collect { items: Vec::new() };
let repo = self.repo;
let options = self.index_worktree_options;
let mut progress = self.progress;
let patterns: Vec<BString> = patterns.into_iter().collect();
let (mut items, tree_index) = match obtain_tree_id()? {
Some(tree_id) => {
let mut pathspec = repo.index_worktree_status_pathspec(
&patterns,
&index,
self.index_worktree_options.dirwalk_options.as_ref(),
)?;
let mut items = Vec::new();
let tree_index = self.repo.tree_index_status(
&tree_id,
&index,
Some(&mut pathspec),
self.tree_index_renames,
|change, _, _| {
items.push(change.into_owned().into());
let action = if should_interrupt.load(Ordering::Acquire) {
std::ops::ControlFlow::Break(())
} else {
std::ops::ControlFlow::Continue(())
};
Ok(action)
},
)?;
(items, Some(tree_index))
}
None => (Vec::new(), None),
};
let out = repo.index_worktree_status(
&index,
patterns,
&mut collect,
gix_status::index_as_worktree::traits::FastEq,
submodule,
&mut progress,
&should_interrupt,
options,
)?;
let mut iter = Iter {
items: Vec::new().into_iter(),
index_changes: Vec::new(),
out: None,
};
let mut out = Outcome {
index_worktree: out,
worktree_index: index,
tree_index,
changes: None,
skip_hash,
};
items.extend(
collect
.items
.into_iter()
.filter_map(|item| iter.maybe_keep_index_change(item)),
);
out.changes = (!iter.index_changes.is_empty()).then(|| std::mem::take(&mut iter.index_changes));
iter.items = items.into_iter();
iter.out = Some(out);
Ok(iter)
}
}
}
impl Iterator for Iter {
type Item = Result<Item>;
fn next(&mut self) -> Option<Self::Item> {
#[cfg(feature = "parallel")]
loop {
let (rx, _join_worktree, _join_tree) = self.rx_and_join.as_ref()?;
match rx.recv_timeout(std::time::Duration::from_millis(25)) {
Ok(item) => {
if let Some(item) = self.maybe_keep_index_change(item) {
break Some(Ok(item));
}
continue;
}
// NOTE: this isn't necessary when index::from-tree also supports interrupts. As it stands,
// on big repositories it can go up to 500ms which aren't interruptible, so this is another
// way to not wait for this. Once it can be interrupted, this won't be needed anymore.
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
if self.should_interrupt.load(Ordering::SeqCst) {
return None;
}
}
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
let (_rx, worktree_handle, tree_handle) = self.rx_and_join.take()?;
let tree_index = if let Some(handle) = tree_handle {
match handle.join().expect("no panic") {
Ok(out) => Some(out),
Err(err) => break Some(Err(err)),
}
} else {
None
};
break match worktree_handle.join().expect("no panic") {
Ok(mut out) => {
out.changes = Some(std::mem::take(&mut self.index_changes));
out.tree_index = tree_index;
self.out = Some(out);
None
}
Err(err) => Some(Err(err)),
};
}
}
}
#[cfg(not(feature = "parallel"))]
self.items.next().map(Ok)
}
}
/// Access
impl Iter {
/// Return the outcome of the iteration, or `None` if the iterator isn't fully consumed.
pub fn outcome_mut(&mut self) -> Option<&mut Outcome> {
self.out.as_mut()
}
/// Turn the iterator into the iteration outcome, which is `None` on error or if the iteration
/// isn't complete.
pub fn into_outcome(mut self) -> Option<Outcome> {
self.out.take()
}
}
impl Iter {
fn maybe_keep_index_change(&mut self, item: Item) -> Option<Item> {
match item {
Item::IndexWorktree(index_worktree::Item::Modification {
status: EntryStatus::NeedsUpdate(stat),
entry_index,
..
}) => {
self.index_changes.push((entry_index, ApplyChange::NewStat(stat)));
return None;
}
Item::IndexWorktree(index_worktree::Item::Modification {
status:
EntryStatus::Change(Change::Modification {
set_entry_stat_size_zero,
..
}),
entry_index,
..
}) if set_entry_stat_size_zero => {
self.index_changes.push((entry_index, ApplyChange::SetSizeToZero));
}
_ => {}
}
Some(item)
}
}
#[cfg(feature = "parallel")]
impl Drop for Iter {
fn drop(&mut self) {
crate::util::parallel_iter_drop(self.rx_and_join.take(), &self.should_interrupt);
}
}
struct Collect {
#[cfg(feature = "parallel")]
tx: std::sync::mpsc::Sender<Item>,
#[cfg(not(feature = "parallel"))]
items: Vec<Item>,
}
impl<'index> gix_status::index_as_worktree_with_renames::VisitEntry<'index> for Collect {
type ContentChange =
<gix_status::index_as_worktree::traits::FastEq as gix_status::index_as_worktree::traits::CompareBlobs>::Output;
type SubmoduleStatus = <BuiltinSubmoduleStatus as gix_status::index_as_worktree::traits::SubmoduleStatus>::Output;
fn visit_entry(
&mut self,
entry: gix_status::index_as_worktree_with_renames::Entry<'index, Self::ContentChange, Self::SubmoduleStatus>,
) {
// NOTE: we assume that the receiver triggers interruption so the operation will stop if the receiver is down.
let item = Item::IndexWorktree(entry.into());
#[cfg(feature = "parallel")]
self.tx.send(item).ok();
#[cfg(not(feature = "parallel"))]
self.items.push(item);
}
}