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
use bstr::{BStr, ByteSlice, ByteVec};
use std::cmp::Ordering;
use crate::{entry, extension, Entry, PathStorage, State, Version};
#[allow(dead_code)]
mod sparse;
impl State {
pub fn version(&self) -> Version {
self.version
}
pub fn object_hash(&self) -> git_hash::Kind {
self.object_hash
}
pub fn entries(&self) -> &[Entry] {
&self.entries
}
pub fn path_backing(&self) -> &PathStorage {
&self.path_backing
}
pub fn entries_with_paths_by_filter_map<'a, T>(
&'a self,
mut filter_map: impl FnMut(&'a BStr, &Entry) -> Option<T> + 'a,
) -> impl Iterator<Item = (&'a BStr, T)> + 'a {
self.entries.iter().filter_map(move |e| {
let p = e.path(self);
filter_map(p, e).map(|t| (p, t))
})
}
pub fn entries_mut_with_paths_in<'state, 'backing>(
&'state mut self,
backing: &'backing PathStorage,
) -> impl Iterator<Item = (&'state mut Entry, &'backing BStr)> {
self.entries.iter_mut().map(move |e| {
let path = backing[e.path.clone()].as_bstr();
(e, path)
})
}
pub fn entry_index_by_path_and_stage(&self, path: &BStr, stage: entry::Stage) -> Option<usize> {
self.entries
.binary_search_by(|e| e.path(self).cmp(path).then_with(|| e.stage().cmp(&stage)))
.ok()
}
pub fn entry_index_by_path_and_stage_bounded(
&self,
path: &BStr,
stage: entry::Stage,
upper_bound: usize,
) -> Option<usize> {
self.entries[..upper_bound]
.binary_search_by(|e| e.path(self).cmp(path).then_with(|| e.stage().cmp(&stage)))
.ok()
}
pub fn entry_by_path_and_stage(&self, path: &BStr, stage: entry::Stage) -> Option<&Entry> {
self.entry_index_by_path_and_stage(path, stage)
.map(|idx| &self.entries[idx])
}
pub fn entry(&self, idx: usize) -> &Entry {
&self.entries[idx]
}
pub fn is_sparse(&self) -> bool {
self.is_sparse
}
}
impl State {
pub fn return_path_backing(&mut self, backing: PathStorage) {
debug_assert!(
self.path_backing.is_empty(),
"BUG: return path backing only after taking it, once"
);
self.path_backing = backing;
}
pub fn entries_mut(&mut self) -> &mut [Entry] {
&mut self.entries
}
pub fn entries_mut_with_paths(&mut self) -> impl Iterator<Item = (&mut Entry, &BStr)> {
let paths = &self.path_backing;
self.entries.iter_mut().map(move |e| {
let path = paths[e.path.clone()].as_bstr();
(e, path)
})
}
pub fn take_path_backing(&mut self) -> PathStorage {
assert_eq!(
self.entries.is_empty(),
self.path_backing.is_empty(),
"BUG: cannot take out backing multiple times"
);
std::mem::take(&mut self.path_backing)
}
pub fn entry_mut_by_path_and_stage(&mut self, path: &BStr, stage: entry::Stage) -> Option<&mut Entry> {
self.entry_index_by_path_and_stage(path, stage)
.map(|idx| &mut self.entries[idx])
}
pub fn dangerously_push_entry(
&mut self,
stat: entry::Stat,
id: git_hash::ObjectId,
flags: entry::Flags,
mode: entry::Mode,
path: &BStr,
) {
let path = {
let path_start = self.path_backing.len();
self.path_backing.push_str(path);
path_start..self.path_backing.len()
};
self.entries.push(Entry {
stat,
id,
flags,
mode,
path,
});
}
pub fn sort_entries(&mut self) {
let path_backing = &self.path_backing;
self.entries.sort_by(|a, b| {
Entry::cmp_filepaths(a.path_in(path_backing), b.path_in(path_backing))
.then_with(|| a.stage().cmp(&b.stage()))
});
}
pub fn sort_entries_by(&mut self, mut compare: impl FnMut(&Entry, &Entry) -> Ordering) {
let path_backing = &self.path_backing;
self.entries.sort_by(|a, b| {
Entry::cmp_filepaths(a.path_in(path_backing), b.path_in(path_backing))
.then_with(|| a.stage().cmp(&b.stage()))
.then_with(|| compare(a, b))
});
}
}
impl State {
pub fn tree(&self) -> Option<&extension::Tree> {
self.tree.as_ref()
}
pub fn link(&self) -> Option<&extension::Link> {
self.link.as_ref()
}
pub fn resolve_undo(&self) -> Option<&extension::resolve_undo::Paths> {
self.resolve_undo.as_ref()
}
pub fn untracked(&self) -> Option<&extension::UntrackedCache> {
self.untracked.as_ref()
}
pub fn fs_monitor(&self) -> Option<&extension::FsMonitor> {
self.fs_monitor.as_ref()
}
}