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
//! The path rules (`MovePaths`) over root trees: rename a file or folder,
//! move everything into a subdirectory, keep only a subdirectory as the new
//! root. They work on tree objects, never on blobs, so every blob keeps its
//! id and a moved folder is the very same tree object at its new place.
//!
//! A rule runs on the root a commit has after the content rules (so a
//! removal glob names the paths of the original layout), in recipe order
//! among the path rules. A rename never overwrites: when its target exists
//! in a commit (a file, a folder, or a file where a folder would have to
//! be) the commit is reported with the path and the preview is refused.
use std::collections::HashMap;
use gitpoulpi_core::domain::rewrite::paths::clean_dir;
use gitpoulpi_core::domain::rewrite::{PathChange, PathMove};
use gitpoulpi_core::domain::Oid;
use gitpoulpi_core::{Error, Result};
use super::pack::{self, Sink};
use super::plumbing::{parse_tree, serialise_tree, TreeEntry};
use super::walk::Reader;
/// What one root became.
pub(super) struct Moved {
/// The new root; `None` when nothing is left (an extraction of a
/// directory the commit does not have).
pub(super) root: Option<Oid>,
/// The path rules that acted, as bits of the rule's position among the
/// path rules (the caller maps them).
pub(super) acted: Vec<usize>,
pub(super) paths: Vec<PathChange>,
/// The first path a rename would have overwritten.
pub(super) collision: Option<String>,
}
type Entry = (Vec<u8>, Oid);
/// A tree without an entry, and that entry.
type Taken = (Option<Oid>, Option<Entry>);
pub(super) struct Mover {
rules: Vec<PathMove>,
/// Bodies of the trees created in this run: they are in the run's pack,
/// not yet in the repository, so `cat-file` cannot read them.
made: HashMap<Oid, Vec<u8>>,
/// `(tree, rule, depth)` to the tree without the entry at the rule's
/// `from` path and that entry.
taken: HashMap<(Oid, usize, usize), Taken>,
}
fn segments(path: &str) -> Vec<Vec<u8>> {
clean_dir(path)
.unwrap_or_default()
.split('/')
.map(|s| s.as_bytes().to_vec())
.collect()
}
fn text(name: &[u8]) -> String {
String::from_utf8_lossy(name).into_owned()
}
impl Mover {
pub(super) fn new(rules: Vec<PathMove>) -> Self {
Self {
rules,
made: HashMap::new(),
taken: HashMap::new(),
}
}
/// Whether a file at `path` of the checked-out tree ends up elsewhere
/// (or nowhere): how an edited file is told from one the rules leave be.
pub(super) fn would_move(&self, path: &str) -> bool {
let mut at = Some(path.to_string());
for rule in &self.rules {
at = at.and_then(|p| rule.map_path(&p));
}
at.as_deref() != Some(path)
}
/// A tree another stage created (its body is not in the repository).
pub(super) fn remember(&mut self, id: Oid, body: &[u8]) {
self.made.insert(id, body.to_vec());
}
fn entries(&self, reader: &mut Reader, tree: Oid) -> Result<Vec<TreeEntry>> {
let body = match self.made.get(&tree) {
Some(body) => body.clone(),
None => reader.get(tree)?.1,
};
parse_tree(&body)
.ok_or_else(|| Error::Backend(format!("{} is not a readable tree", tree.to_hex())))
}
fn write(&mut self, sink: &mut Sink, entries: Vec<TreeEntry>) -> Result<Option<Oid>> {
if entries.is_empty() {
return Ok(None);
}
let body = serialise_tree(entries);
let id = sink.add(pack::TREE, &body)?;
self.made.insert(id, body);
Ok(Some(id))
}
/// `tree` without the entry at `segs[depth..]`, and that entry.
fn take(
&mut self,
reader: &mut Reader,
sink: &mut Sink,
(rule, tree): (usize, Oid),
segs: &[Vec<u8>],
depth: usize,
) -> Result<Taken> {
if let Some(done) = self.taken.get(&(tree, rule, depth)) {
return Ok(done.clone());
}
let mut entries = self.entries(reader, tree)?;
let result = match entries.iter().position(|e| e.name == segs[depth]) {
None => (Some(tree), None),
Some(i) if depth + 1 == segs.len() => {
let gone = entries.remove(i);
(
self.write(sink, entries)?,
Some((gone.mode.clone(), gone.oid)),
)
}
Some(i) if entries[i].is_tree() => {
let below = entries[i].oid;
let (rest, gone) = self.take(reader, sink, (rule, below), segs, depth + 1)?;
if gone.is_none() {
(Some(tree), None)
} else {
match rest {
Some(new) => entries[i].oid = new,
None => {
entries.remove(i);
}
}
(self.write(sink, entries)?, gone)
}
}
// A file where a folder is named: nothing there to take.
Some(_) => (Some(tree), None),
};
self.taken.insert((tree, rule, depth), result.clone());
Ok(result)
}
/// `tree` with `entry` at `segs[depth..]`; `Err(path)` names what is
/// already there.
fn put(
&mut self,
reader: &mut Reader,
sink: &mut Sink,
tree: Option<Oid>,
segs: &[Vec<u8>],
depth: usize,
entry: &Entry,
) -> Result<std::result::Result<Oid, String>> {
let mut entries = match tree {
Some(t) => self.entries(reader, t)?,
None => Vec::new(),
};
let name = &segs[depth];
let here = || text(&segs[..=depth].join(&b'/'));
let at = entries.iter().position(|e| &e.name == name);
if depth + 1 == segs.len() {
if at.is_some() {
return Ok(Err(here()));
}
entries.push(TreeEntry {
mode: entry.0.clone(),
name: name.clone(),
oid: entry.1,
});
} else {
match at {
Some(i) if entries[i].is_tree() => {
let below = entries[i].oid;
match self.put(reader, sink, Some(below), segs, depth + 1, entry)? {
Ok(new) => entries[i].oid = new,
Err(path) => return Ok(Err(path)),
}
}
// A file is in the way of a folder.
Some(_) => return Ok(Err(here())),
None => match self.put(reader, sink, None, segs, depth + 1, entry)? {
Ok(new) => entries.push(TreeEntry {
mode: b"40000".to_vec(),
name: name.clone(),
oid: new,
}),
Err(path) => return Ok(Err(path)),
},
}
}
let id = self
.write(sink, entries)?
.expect("a tree with an entry is not empty");
Ok(Ok(id))
}
/// Runs every path rule over `root`.
pub(super) fn apply(
&mut self,
reader: &mut Reader,
sink: &mut Sink,
root: Option<Oid>,
) -> Result<Moved> {
let mut out = Moved {
root,
acted: Vec::new(),
paths: Vec::new(),
collision: None,
};
for k in 0..self.rules.len() {
let Some(current) = out.root else { break };
match self.rules[k].clone() {
PathMove::Rename { from, to } => {
let (from_segs, to_segs) = (segments(&from), segments(&to));
let (rest, gone) = self.take(reader, sink, (k, current), &from_segs, 0)?;
let Some(entry) = gone else { continue };
match self.put(reader, sink, rest, &to_segs, 0, &entry)? {
Ok(new) => out.root = Some(new),
Err(path) => {
out.collision = Some(path);
return Ok(out);
}
}
out.acted.push(k);
out.paths.push(PathChange::Renamed {
from: clean_dir(&from).unwrap_or(from),
to: clean_dir(&to).unwrap_or(to),
});
}
PathMove::IntoSubdir { dir } => {
let mut tree = current;
for name in segments(&dir).into_iter().rev() {
tree = self
.write(
sink,
vec![TreeEntry {
mode: b"40000".to_vec(),
name,
oid: tree,
}],
)?
.expect("one entry");
}
out.root = Some(tree);
out.acted.push(k);
out.paths.push(PathChange::Renamed {
from: ".".into(),
to: clean_dir(&dir).unwrap_or(dir),
});
}
PathMove::ExtractSubdir { dir, .. } => {
let mut at = Some(current);
for name in segments(&dir) {
let Some(tree) = at else { break };
at = self
.entries(reader, tree)?
.into_iter()
.find(|e| e.name == name && e.is_tree())
.map(|e| e.oid);
}
out.root = at;
out.acted.push(k);
if at.is_some() {
out.paths.push(PathChange::Renamed {
from: clean_dir(&dir).unwrap_or(dir),
to: ".".into(),
});
}
}
}
}
Ok(out)
}
}