| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
use super::*; |
| 12 |
use crate::objects::{AtomRange, AtomRef}; |
| 13 |
use crate::rewrite::{propagate, Correspondence}; |
| 14 |
|
| 15 |
impl Repo { |
| 16 |
|
| 17 |
fn rewritable_head(&self, hash: &ChangeHash) -> Result<Change> { |
| 18 |
let heads = self.bookmark_heads()?; |
| 19 |
if !heads.contains(hash) { |
| 20 |
return fail(format!("change #{} is not a head of {}; amend works on heads only (describe, split, and abandon work anywhere)", hash.short(12), self.config.bookmark)); |
| 21 |
} |
| 22 |
let c = self.store.get_change(hash)?; |
| 23 |
if c.kind == ChangeKind::WorkingCopy { |
| 24 |
return fail("that is the working copy; commit it instead"); |
| 25 |
} |
| 26 |
Ok(c) |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
fn replace_head( |
| 33 |
&self, |
| 34 |
old: &ChangeHash, |
| 35 |
new: Option<&Change>, |
| 36 |
command: &str, |
| 37 |
) -> Result<Option<ChangeHash>> { |
| 38 |
let new_hash = match new { |
| 39 |
Some(c) => Some(self.store_signed(c)?), |
| 40 |
None => None, |
| 41 |
}; |
| 42 |
let old_change = self.store.get_change(old)?; |
| 43 |
let (op_ref, op) = self.head_op()?; |
| 44 |
let bookmark = self.config.bookmark.clone(); |
| 45 |
let heads = self.heads_of(&op.view, &bookmark)?; |
| 46 |
let replace = |set: &[ChangeHash]| -> Vec<ChangeHash> { |
| 47 |
let mut out: Vec<ChangeHash> = set.iter().filter(|h| *h != old).copied().collect(); |
| 48 |
match new_hash { |
| 49 |
Some(n) => out.push(n), |
| 50 |
None => out.extend(old_change.deps.iter().copied()), |
| 51 |
} |
| 52 |
out |
| 53 |
}; |
| 54 |
let candidates = replace(&heads); |
| 55 |
let st = self.load_state(&candidates)?; |
| 56 |
let new_heads = st.maximal(&candidates); |
| 57 |
|
| 58 |
let (_, wc) = self.working_copy()?; |
| 59 |
let base = self.load_state(&wc.deps)?; |
| 60 |
let wc_base = base.maximal(&wc.deps); |
| 61 |
let wc_base = if wc_base.contains(old) { |
| 62 |
let c = replace(&wc_base); |
| 63 |
let st = self.load_state(&c)?; |
| 64 |
st.maximal(&c) |
| 65 |
} else { |
| 66 |
wc_base |
| 67 |
}; |
| 68 |
let fresh = self.new_working_copy(wc_base, &wc.message); |
| 69 |
let wc_hash = self.store.put_change(&fresh)?; |
| 70 |
|
| 71 |
let mut view = op.view.clone(); |
| 72 |
view.bookmarks.insert( |
| 73 |
bookmark, |
| 74 |
self.store.put_state(&StateObject::new(new_heads))?, |
| 75 |
); |
| 76 |
view.workspaces.insert(self.workspace, wc_hash); |
| 77 |
self.record_op(&[op_ref], command, view)?; |
| 78 |
self.drop_manifest(); |
| 79 |
Ok(new_hash) |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
fn rewrite_change( |
| 86 |
&self, |
| 87 |
old: &ChangeHash, |
| 88 |
new: Option<&Change>, |
| 89 |
corr: &Correspondence, |
| 90 |
command: &str, |
| 91 |
) -> Result<Option<ChangeHash>> { |
| 92 |
self.rewrite_change_noted(old, new, corr, command) |
| 93 |
.map(|(h, _)| h) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
fn rewrite_change_noted( |
| 98 |
&self, |
| 99 |
old: &ChangeHash, |
| 100 |
new: Option<&Change>, |
| 101 |
corr: &Correspondence, |
| 102 |
command: &str, |
| 103 |
) -> Result<(Option<ChangeHash>, Vec<String>)> { |
| 104 |
let (op_ref, op) = self.head_op()?; |
| 105 |
let bookmark = self.config.bookmark.clone(); |
| 106 |
let heads = self.heads_of(&op.view, &bookmark)?; |
| 107 |
let (wc_hash, _) = self.working_copy()?; |
| 108 |
let mut all = heads.clone(); |
| 109 |
all.push(wc_hash); |
| 110 |
let state = self.load_state(&all)?; |
| 111 |
if !state.closure(&wc_hash).contains(old) |
| 112 |
&& !heads.contains(old) |
| 113 |
&& !state.heads().contains(old) |
| 114 |
&& !state.contains(old) |
| 115 |
{ |
| 116 |
return fail(format!( |
| 117 |
"change #{} is not in {} or under the working copy", |
| 118 |
old.short(12), |
| 119 |
bookmark |
| 120 |
)); |
| 121 |
} |
| 122 |
let old_change = self.store.get_change(old)?; |
| 123 |
if old_change.kind == ChangeKind::WorkingCopy { |
| 124 |
return fail("that is the working copy; commit it instead"); |
| 125 |
} |
| 126 |
let load = |h: &ChangeHash| self.store.get_change(h).ok(); |
| 127 |
let propagation = |
| 128 |
propagate(&state, *old, new, corr, &load).map_err(|e| RepoError::Msg(e.to_string()))?; |
| 129 |
let rewritten = propagation.changes; |
| 130 |
|
| 131 |
let mut mapping: HashMap<ChangeHash, ChangeHash> = HashMap::new(); |
| 132 |
let new_hash = match new { |
| 133 |
Some(c) => { |
| 134 |
let h = self.store_signed(c)?; |
| 135 |
mapping.insert(*old, h); |
| 136 |
Some(h) |
| 137 |
} |
| 138 |
None => None, |
| 139 |
}; |
| 140 |
for (h, c) in &rewritten { |
| 141 |
let nh = if c.kind == ChangeKind::WorkingCopy { |
| 142 |
self.store.put_change(c)? |
| 143 |
} else { |
| 144 |
self.store_signed(c)? |
| 145 |
}; |
| 146 |
mapping.insert(*h, nh); |
| 147 |
} |
| 148 |
let remap = |set: &[ChangeHash]| -> Vec<ChangeHash> { |
| 149 |
let mut out = Vec::new(); |
| 150 |
for h in set { |
| 151 |
if h == old && new_hash.is_none() { |
| 152 |
out.extend(old_change.deps.iter().copied()); |
| 153 |
} else { |
| 154 |
out.push(*mapping.get(h).unwrap_or(h)); |
| 155 |
} |
| 156 |
} |
| 157 |
out |
| 158 |
}; |
| 159 |
let candidates = remap(&heads); |
| 160 |
let st = self.load_state(&candidates)?; |
| 161 |
let new_heads = st.maximal(&candidates); |
| 162 |
let new_wc = mapping.get(&wc_hash).copied().unwrap_or(wc_hash); |
| 163 |
|
| 164 |
let mut view = op.view.clone(); |
| 165 |
view.bookmarks.insert( |
| 166 |
bookmark, |
| 167 |
self.store.put_state(&StateObject::new(new_heads))?, |
| 168 |
); |
| 169 |
view.workspaces.insert(self.workspace, new_wc); |
| 170 |
self.record_op(&[op_ref], command, view)?; |
| 171 |
self.drop_manifest(); |
| 172 |
Ok((new_hash, propagation.notes)) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
pub fn describe(&self, hash: &ChangeHash, message: &str) -> Result<ChangeHash> { |
| 177 |
if message.trim().is_empty() { |
| 178 |
return fail("a message is required"); |
| 179 |
} |
| 180 |
let mut c = self.store.get_change(hash)?; |
| 181 |
if c.kind == ChangeKind::WorkingCopy { |
| 182 |
return fail("that is the working copy; commit it instead"); |
| 183 |
} |
| 184 |
if c.message == message.trim_end() { |
| 185 |
return Ok(*hash); |
| 186 |
} |
| 187 |
let corr = Correspondence::identity(&c); |
| 188 |
c.message = message.trim_end().to_string(); |
| 189 |
c.hlc = Hlc::now(); |
| 190 |
let id = c.change_id; |
| 191 |
Ok(self |
| 192 |
.rewrite_change( |
| 193 |
hash, |
| 194 |
Some(&c), |
| 195 |
&corr, |
| 196 |
&format!("oxy describe {} -m {message:?}", id.short(8)), |
| 197 |
)? |
| 198 |
.unwrap()) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
pub fn amend(&self, message: Option<&str>) -> Result<ChangeHash> { |
| 204 |
let snap = self.snapshot("oxy amend")?; |
| 205 |
if snap |
| 206 |
.statuses |
| 207 |
.iter() |
| 208 |
.any(|s| s.status == ChangeStatus::ConflictEdited) |
| 209 |
{ |
| 210 |
return fail("a file still contains conflict markers"); |
| 211 |
} |
| 212 |
let base_heads = snap.base.maximal(&snap.wc.deps); |
| 213 |
let [head] = base_heads.as_slice() else { |
| 214 |
return fail(format!( |
| 215 |
"the working copy sits on {} heads; check out the single change to amend", |
| 216 |
base_heads.len() |
| 217 |
)); |
| 218 |
}; |
| 219 |
let head = *head; |
| 220 |
let old = self.rewritable_head(&head)?; |
| 221 |
if snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty() && message.is_none() { |
| 222 |
return fail( |
| 223 |
"nothing to amend: the working copy is clean and no new message was given", |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
let beneath = self.load_state(&old.deps)?; |
| 228 |
let beneath_snapshot = beneath.materialize(); |
| 229 |
let reuse = added_node_paths(&old, &beneath_snapshot); |
| 230 |
let built = self.build_against(&beneath, &beneath_snapshot, reuse)?; |
| 231 |
if built.tree_ops.is_empty() && built.edits.is_empty() { |
| 232 |
return fail("amending would leave the change empty; use `oxy abandon` to drop it"); |
| 233 |
} |
| 234 |
let mut deps: BTreeSet<ChangeHash> = old.deps.iter().copied().collect(); |
| 235 |
deps.extend(built.contributors.iter().copied()); |
| 236 |
let mut new = Change { |
| 237 |
change_id: old.change_id, |
| 238 |
deps: vec![], |
| 239 |
author: self.author(), |
| 240 |
hlc: Hlc::now(), |
| 241 |
message: message |
| 242 |
.map(|m| m.trim_end().to_string()) |
| 243 |
.unwrap_or(old.message.clone()), |
| 244 |
tree_ops: built.tree_ops, |
| 245 |
edits: built.edits, |
| 246 |
kind: if built.resolves || old.kind == ChangeKind::Resolution { |
| 247 |
ChangeKind::Resolution |
| 248 |
} else { |
| 249 |
ChangeKind::Normal |
| 250 |
}, |
| 251 |
}; |
| 252 |
deps.extend(referenced_changes(&new, &beneath)); |
| 253 |
new.deps = deps.into_iter().collect(); |
| 254 |
new.normalize(); |
| 255 |
Ok(self |
| 256 |
.replace_head( |
| 257 |
&head, |
| 258 |
Some(&new), |
| 259 |
&format!("oxy amend {}", old.change_id.short(8)), |
| 260 |
)? |
| 261 |
.unwrap()) |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
pub fn abandon(&self, hash: &ChangeHash) -> Result<Vec<String>> { |
| 269 |
let c = self.store.get_change(hash)?; |
| 270 |
if c.kind == ChangeKind::WorkingCopy { |
| 271 |
return fail("that is the working copy; commit it instead"); |
| 272 |
} |
| 273 |
let heads = self.bookmark_heads()?; |
| 274 |
if heads.contains(hash) { |
| 275 |
self.replace_head(hash, None, &format!("oxy abandon {}", c.change_id.short(8)))?; |
| 276 |
Ok(vec![]) |
| 277 |
} else { |
| 278 |
let (_, notes) = self.rewrite_change_noted( |
| 279 |
hash, |
| 280 |
None, |
| 281 |
&Correspondence::default(), |
| 282 |
&format!("oxy abandon {}", c.change_id.short(8)), |
| 283 |
)?; |
| 284 |
Ok(notes) |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
fn fold_atom( |
| 293 |
r: AtomRef, |
| 294 |
target: &ChangeHash, |
| 295 |
wc_offset: &HashMap<NodeId, u32>, |
| 296 |
node: &NodeId, |
| 297 |
) -> Option<AtomRef> { |
| 298 |
match r { |
| 299 |
AtomRef::Atom { change, index } if change == *target => Some(AtomRef::Atom { |
| 300 |
change: ObjectRef::ZERO, |
| 301 |
index, |
| 302 |
}), |
| 303 |
AtomRef::Atom { change, index } if change.is_zero() => Some(AtomRef::Atom { |
| 304 |
change: ObjectRef::ZERO, |
| 305 |
index: index + wc_offset.get(node).copied().unwrap_or(0), |
| 306 |
}), |
| 307 |
|
| 308 |
other => Some(other), |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
impl Repo { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
pub fn amend_into(&self, target: &ChangeHash, message: Option<&str>) -> Result<ChangeHash> { |
| 319 |
let snap = self.snapshot("oxy amend")?; |
| 320 |
if snap |
| 321 |
.statuses |
| 322 |
.iter() |
| 323 |
.any(|s| s.status == ChangeStatus::ConflictEdited) |
| 324 |
{ |
| 325 |
return fail("a file still contains conflict markers"); |
| 326 |
} |
| 327 |
let wc = &snap.wc; |
| 328 |
if wc.tree_ops.is_empty() && wc.edits.is_empty() && message.is_none() { |
| 329 |
return fail( |
| 330 |
"nothing to amend: the working copy is clean and no new message was given", |
| 331 |
); |
| 332 |
} |
| 333 |
let old = self.store.get_change(target)?; |
| 334 |
if old.kind == ChangeKind::WorkingCopy { |
| 335 |
return fail("that is the working copy; commit it instead"); |
| 336 |
} |
| 337 |
|
| 338 |
let closure = self.load_state(&[*target])?; |
| 339 |
let ok_change = |h: &ChangeHash| *h == *target || closure.contains(h); |
| 340 |
let ok_node = |n: &NodeId| n.is_root() || closure.tree().introducer(n).is_some(); |
| 341 |
let too_new = |what: &str| { |
| 342 |
RepoError::Msg(format!( |
| 343 |
"{what} was introduced after change #{}; amend into a later change instead", |
| 344 |
target.short(12) |
| 345 |
)) |
| 346 |
}; |
| 347 |
for op in &wc.tree_ops { |
| 348 |
match op { |
| 349 |
TreeOp::AddNode { parent, .. } => { |
| 350 |
if !ok_node(parent) { |
| 351 |
return Err(too_new("the parent directory")); |
| 352 |
} |
| 353 |
} |
| 354 |
TreeOp::RemoveNode { node, placement } => { |
| 355 |
if !ok_node(node) || !ok_change(&placement.change) { |
| 356 |
return Err(too_new("a removed file")); |
| 357 |
} |
| 358 |
} |
| 359 |
TreeOp::Move { |
| 360 |
node, old, parent, .. |
| 361 |
} => { |
| 362 |
if !ok_node(node) || !ok_change(&old.change) || !ok_node(parent) { |
| 363 |
return Err(too_new("a moved file")); |
| 364 |
} |
| 365 |
} |
| 366 |
TreeOp::SetMode { node, old, .. } => { |
| 367 |
if !ok_node(node) || !ok_change(&old.change) { |
| 368 |
return Err(too_new("a file whose mode changed")); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
let wc_added: BTreeSet<NodeId> = wc |
| 374 |
.tree_ops |
| 375 |
.iter() |
| 376 |
.filter_map(|o| { |
| 377 |
if let TreeOp::AddNode { node, .. } = o { |
| 378 |
Some(*node) |
| 379 |
} else { |
| 380 |
None |
| 381 |
} |
| 382 |
}) |
| 383 |
.collect(); |
| 384 |
for e in &wc.edits { |
| 385 |
let n = e.node(); |
| 386 |
if !wc_added.contains(&n) && !ok_node(&n) { |
| 387 |
return Err(too_new("an edited file")); |
| 388 |
} |
| 389 |
let refs: Vec<ChangeHash> = match e { |
| 390 |
Edit::Insert { after, before, .. } => [after, before] |
| 391 |
.iter() |
| 392 |
.filter_map(|r| { |
| 393 |
if let AtomRef::Atom { change, .. } = r { |
| 394 |
Some(*change) |
| 395 |
} else { |
| 396 |
None |
| 397 |
} |
| 398 |
}) |
| 399 |
.collect(), |
| 400 |
Edit::Delete { atoms, .. } => atoms.iter().map(|r| r.change).collect(), |
| 401 |
Edit::Replace { .. } => vec![], |
| 402 |
}; |
| 403 |
if refs.iter().any(|h| !h.is_zero() && !ok_change(h)) { |
| 404 |
return Err(too_new("an edited line")); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
let mut per_node: HashMap<NodeId, u32> = HashMap::new(); |
| 412 |
for e in &old.edits { |
| 413 |
if let Edit::Insert { node, lines, .. } = e { |
| 414 |
*per_node.entry(*node).or_default() += lines.len() as u32; |
| 415 |
} |
| 416 |
} |
| 417 |
let op_offset = old.tree_ops.len() as u32; |
| 418 |
let fold_op = |r: OpRef| -> OpRef { |
| 419 |
if r.change == *target { |
| 420 |
OpRef { |
| 421 |
change: ObjectRef::ZERO, |
| 422 |
index: r.index, |
| 423 |
} |
| 424 |
} else if r.change.is_zero() { |
| 425 |
OpRef { |
| 426 |
change: ObjectRef::ZERO, |
| 427 |
index: r.index + op_offset, |
| 428 |
} |
| 429 |
} else { |
| 430 |
r |
| 431 |
} |
| 432 |
}; |
| 433 |
let mut tree_ops = old.tree_ops.clone(); |
| 434 |
for op in &wc.tree_ops { |
| 435 |
tree_ops.push(match op { |
| 436 |
TreeOp::RemoveNode { node, placement } => TreeOp::RemoveNode { |
| 437 |
node: *node, |
| 438 |
placement: fold_op(*placement), |
| 439 |
}, |
| 440 |
TreeOp::Move { |
| 441 |
node, |
| 442 |
old, |
| 443 |
parent, |
| 444 |
name, |
| 445 |
} => TreeOp::Move { |
| 446 |
node: *node, |
| 447 |
old: fold_op(*old), |
| 448 |
parent: *parent, |
| 449 |
name: name.clone(), |
| 450 |
}, |
| 451 |
TreeOp::SetMode { node, old, mode } => TreeOp::SetMode { |
| 452 |
node: *node, |
| 453 |
old: fold_op(*old), |
| 454 |
mode: *mode, |
| 455 |
}, |
| 456 |
add => add.clone(), |
| 457 |
}); |
| 458 |
} |
| 459 |
let mut edits = old.edits.clone(); |
| 460 |
for e in &wc.edits { |
| 461 |
edits.push(match e { |
| 462 |
Edit::Insert { |
| 463 |
node, |
| 464 |
after, |
| 465 |
before, |
| 466 |
lines, |
| 467 |
} => Edit::Insert { |
| 468 |
node: *node, |
| 469 |
after: fold_atom(*after, target, &per_node, node) |
| 470 |
.ok_or_else(|| too_new("an anchor line"))?, |
| 471 |
before: fold_atom(*before, target, &per_node, node) |
| 472 |
.ok_or_else(|| too_new("an anchor line"))?, |
| 473 |
lines: lines.clone(), |
| 474 |
}, |
| 475 |
Edit::Delete { node, atoms } => Edit::Delete { |
| 476 |
node: *node, |
| 477 |
atoms: atoms |
| 478 |
.iter() |
| 479 |
.map(|r| { |
| 480 |
if r.change == *target { |
| 481 |
AtomRange { |
| 482 |
change: ObjectRef::ZERO, |
| 483 |
from: r.from, |
| 484 |
to: r.to, |
| 485 |
} |
| 486 |
} else if r.change.is_zero() { |
| 487 |
let off = per_node.get(node).copied().unwrap_or(0); |
| 488 |
AtomRange { |
| 489 |
change: ObjectRef::ZERO, |
| 490 |
from: r.from + off, |
| 491 |
to: r.to + off, |
| 492 |
} |
| 493 |
} else { |
| 494 |
*r |
| 495 |
} |
| 496 |
}) |
| 497 |
.collect(), |
| 498 |
}, |
| 499 |
other => other.clone(), |
| 500 |
}); |
| 501 |
} |
| 502 |
let mut new = Change { |
| 503 |
change_id: old.change_id, |
| 504 |
deps: vec![], |
| 505 |
author: self.author(), |
| 506 |
hlc: Hlc::now(), |
| 507 |
message: message |
| 508 |
.map(|m| m.trim_end().to_string()) |
| 509 |
.unwrap_or(old.message.clone()), |
| 510 |
tree_ops, |
| 511 |
edits, |
| 512 |
kind: old.kind, |
| 513 |
}; |
| 514 |
let beneath = self.load_state(&old.deps)?; |
| 515 |
let mut deps: BTreeSet<ChangeHash> = old.deps.iter().copied().collect(); |
| 516 |
deps.extend( |
| 517 |
referenced_changes(&new, &beneath) |
| 518 |
.into_iter() |
| 519 |
.filter(|h| *h != *target), |
| 520 |
); |
| 521 |
deps.extend( |
| 522 |
wc.deps |
| 523 |
.iter() |
| 524 |
.filter(|d| closure.contains(d) && **d != *target) |
| 525 |
.copied(), |
| 526 |
); |
| 527 |
new.deps = deps.into_iter().collect(); |
| 528 |
new.normalize(); |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
let corr = Correspondence::identity(&old); |
| 533 |
let new_hash = self |
| 534 |
.rewrite_change( |
| 535 |
target, |
| 536 |
Some(&new), |
| 537 |
&corr, |
| 538 |
&format!("oxy amend --into {}", old.change_id.short(8)), |
| 539 |
)? |
| 540 |
.unwrap(); |
| 541 |
let (op_ref, op) = self.head_op()?; |
| 542 |
let heads = self.heads_of(&op.view, &self.config.bookmark)?; |
| 543 |
let fresh = self.new_working_copy(heads, ""); |
| 544 |
let wc_hash = self.store.put_change(&fresh)?; |
| 545 |
let mut view = op.view.clone(); |
| 546 |
view.workspaces.insert(self.workspace, wc_hash); |
| 547 |
self.record_op(&[op_ref], "oxy amend (working copy)", view)?; |
| 548 |
self.drop_manifest(); |
| 549 |
Ok(new_hash) |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
pub fn split(&self, hash: &ChangeHash, paths: &[String]) -> Result<(ChangeHash, ChangeHash)> { |
| 558 |
if paths.is_empty() { |
| 559 |
return fail("name at least one path to split out"); |
| 560 |
} |
| 561 |
if !self.bookmark_heads()?.contains(hash) { |
| 562 |
return self.split_by_node(hash, paths); |
| 563 |
} |
| 564 |
let old = self.rewritable_head(hash)?; |
| 565 |
let beneath = self.load_state(&old.deps)?; |
| 566 |
let beneath_snapshot = beneath.materialize(); |
| 567 |
let full = self.load_state(&[*hash])?; |
| 568 |
let full_snapshot = full.materialize(); |
| 569 |
let selected = |p: &str| { |
| 570 |
paths |
| 571 |
.iter() |
| 572 |
.any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))) |
| 573 |
}; |
| 574 |
|
| 575 |
|
| 576 |
let entries_of = |snap: &Snapshot, |
| 577 |
keep_from_full: &dyn Fn(&str) -> bool| |
| 578 |
-> Result<BTreeMap<String, FsEntry>> { |
| 579 |
let mut out = BTreeMap::new(); |
| 580 |
for (p, _) in snap.files.iter().chain(full_snapshot.files.iter()) { |
| 581 |
let take_full = keep_from_full(p); |
| 582 |
let src = if take_full { |
| 583 |
full_snapshot.files.get(p) |
| 584 |
} else { |
| 585 |
snap.files.get(p) |
| 586 |
}; |
| 587 |
let Some(src) = src else { continue }; |
| 588 |
if out.contains_key(p) { |
| 589 |
continue; |
| 590 |
} |
| 591 |
let content = match &src.content { |
| 592 |
FileContent::Dir => continue, |
| 593 |
FileContent::Text { bytes, .. } => bytes.clone(), |
| 594 |
FileContent::Opaque { blobs } => match blobs.first() { |
| 595 |
Some(b) => self.read_blob(b)?, |
| 596 |
None => continue, |
| 597 |
}, |
| 598 |
}; |
| 599 |
out.insert(p.clone(), FsEntry::new(src.kind, src.mode.exec, content)); |
| 600 |
} |
| 601 |
Ok(out) |
| 602 |
}; |
| 603 |
let mid_entries = entries_of(&beneath_snapshot, &|p| selected(p))?; |
| 604 |
let first = |
| 605 |
self.build_from_entries(&beneath, &beneath_snapshot, HashMap::new(), &mid_entries)?; |
| 606 |
if first.tree_ops.is_empty() && first.edits.is_empty() { |
| 607 |
return fail("the named paths are not touched by that change"); |
| 608 |
} |
| 609 |
let mut c1 = Change { |
| 610 |
change_id: ChangeId::random(), |
| 611 |
deps: old.deps.clone(), |
| 612 |
author: old.author, |
| 613 |
hlc: Hlc::now(), |
| 614 |
message: format!( |
| 615 |
"{} (part 1: {})", |
| 616 |
old.message.lines().next().unwrap_or(""), |
| 617 |
paths.join(", ") |
| 618 |
), |
| 619 |
tree_ops: first.tree_ops, |
| 620 |
edits: first.edits, |
| 621 |
kind: ChangeKind::Normal, |
| 622 |
}; |
| 623 |
let mut d1: BTreeSet<ChangeHash> = referenced_changes(&c1, &beneath); |
| 624 |
d1.extend(old.deps.iter().copied()); |
| 625 |
c1.deps = d1.into_iter().collect(); |
| 626 |
c1.normalize(); |
| 627 |
let h1 = self.store_signed(&c1)?; |
| 628 |
let mut mid = beneath.clone(); |
| 629 |
mid.add(c1)?; |
| 630 |
let mid_snapshot = mid.materialize(); |
| 631 |
let rest_entries = entries_of(&full_snapshot, &|_| true)?; |
| 632 |
let rest = self.build_from_entries(&mid, &mid_snapshot, HashMap::new(), &rest_entries)?; |
| 633 |
if rest.tree_ops.is_empty() && rest.edits.is_empty() { |
| 634 |
return fail( |
| 635 |
"everything in that change is under the named paths; nothing would remain", |
| 636 |
); |
| 637 |
} |
| 638 |
let mut c2 = Change { |
| 639 |
change_id: old.change_id, |
| 640 |
deps: vec![], |
| 641 |
author: old.author, |
| 642 |
hlc: Hlc::now(), |
| 643 |
message: old.message.clone(), |
| 644 |
tree_ops: rest.tree_ops, |
| 645 |
edits: rest.edits, |
| 646 |
kind: ChangeKind::Normal, |
| 647 |
}; |
| 648 |
let mut d2: BTreeSet<ChangeHash> = referenced_changes(&c2, &mid); |
| 649 |
d2.extend(old.deps.iter().copied()); |
| 650 |
c2.deps = d2.into_iter().collect(); |
| 651 |
c2.normalize(); |
| 652 |
let h2 = self |
| 653 |
.replace_head( |
| 654 |
hash, |
| 655 |
Some(&c2), |
| 656 |
&format!("oxy split {} {}", old.change_id.short(8), paths.join(" ")), |
| 657 |
)? |
| 658 |
.unwrap(); |
| 659 |
self.add_to_bookmark(h1)?; |
| 660 |
Ok((h1, h2)) |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
fn add_to_bookmark(&self, h: ChangeHash) -> Result<()> { |
| 666 |
let (op_ref, op) = self.head_op()?; |
| 667 |
let bookmark = self.config.bookmark.clone(); |
| 668 |
let mut heads = self.heads_of(&op.view, &bookmark)?; |
| 669 |
heads.push(h); |
| 670 |
let st = self.load_state(&heads)?; |
| 671 |
let heads = st.maximal(&heads); |
| 672 |
let mut view = op.view.clone(); |
| 673 |
view.bookmarks.insert( |
| 674 |
bookmark, |
| 675 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 676 |
); |
| 677 |
let (_, wc) = self.working_copy()?; |
| 678 |
let mut base = wc.deps.clone(); |
| 679 |
base.push(h); |
| 680 |
let wc_base = self.load_state(&base)?.maximal(&base); |
| 681 |
let fresh = self.new_working_copy(wc_base, &wc.message); |
| 682 |
view.workspaces |
| 683 |
.insert(self.workspace, self.store.put_change(&fresh)?); |
| 684 |
self.record_op(&[op_ref], "oxy split (heads)", view)?; |
| 685 |
self.drop_manifest(); |
| 686 |
Ok(()) |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
fn split_by_node( |
| 694 |
&self, |
| 695 |
hash: &ChangeHash, |
| 696 |
paths: &[String], |
| 697 |
) -> Result<(ChangeHash, ChangeHash)> { |
| 698 |
let old = self.store.get_change(hash)?; |
| 699 |
if old.kind == ChangeKind::WorkingCopy { |
| 700 |
return fail("that is the working copy; commit it instead"); |
| 701 |
} |
| 702 |
let selected = |p: &str| { |
| 703 |
paths |
| 704 |
.iter() |
| 705 |
.any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))) |
| 706 |
}; |
| 707 |
let beneath = self.load_state(&old.deps)?; |
| 708 |
let full = self.load_state(&[*hash])?; |
| 709 |
let mut removed: BTreeSet<NodeId> = BTreeSet::new(); |
| 710 |
for snap in [beneath.materialize(), full.materialize()] { |
| 711 |
for (p, f) in &snap.files { |
| 712 |
if selected(p) { |
| 713 |
removed.insert(f.node); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
for op in &old.tree_ops { |
| 719 |
if let TreeOp::AddNode { node, parent, .. } = op { |
| 720 |
if removed.contains(parent) { |
| 721 |
removed.insert(*node); |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
if removed.is_empty() { |
| 726 |
return fail("the named paths are not touched by that change"); |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
let heads = self.bookmark_heads()?; |
| 731 |
let (wc_hash, _) = self.working_copy()?; |
| 732 |
let mut all = heads.clone(); |
| 733 |
all.push(wc_hash); |
| 734 |
let state = self.load_state(&all)?; |
| 735 |
for (h, meta) in state.changes() { |
| 736 |
if !meta.deps.contains(hash) { |
| 737 |
continue; |
| 738 |
} |
| 739 |
let d = self.store.get_change(h)?; |
| 740 |
if d.tree_ops.iter().any(|op| removed.contains(&op.node())) |
| 741 |
|| d.edits.iter().any(|e| removed.contains(&e.node())) |
| 742 |
{ |
| 743 |
return fail(format!( |
| 744 |
"change {} depends on the split-off paths of {}; split it first", |
| 745 |
d.change_id.short(8), |
| 746 |
old.change_id.short(8) |
| 747 |
)); |
| 748 |
} |
| 749 |
} |
| 750 |
let mut c1 = Change { |
| 751 |
change_id: ChangeId::random(), |
| 752 |
deps: old.deps.clone(), |
| 753 |
author: old.author, |
| 754 |
hlc: Hlc::now(), |
| 755 |
message: format!( |
| 756 |
"{} (part 1: {})", |
| 757 |
old.message.lines().next().unwrap_or(""), |
| 758 |
paths.join(", ") |
| 759 |
), |
| 760 |
tree_ops: old |
| 761 |
.tree_ops |
| 762 |
.iter() |
| 763 |
.filter(|op| removed.contains(&op.node())) |
| 764 |
.cloned() |
| 765 |
.collect(), |
| 766 |
edits: old |
| 767 |
.edits |
| 768 |
.iter() |
| 769 |
.filter(|e| removed.contains(&e.node())) |
| 770 |
.cloned() |
| 771 |
.collect(), |
| 772 |
kind: ChangeKind::Normal, |
| 773 |
}; |
| 774 |
let mut c2 = old.clone(); |
| 775 |
c2.hlc = Hlc::now(); |
| 776 |
let mut ops_map: HashMap<u32, u32> = HashMap::new(); |
| 777 |
let mut kept_ops = Vec::new(); |
| 778 |
for (i, op) in old.tree_ops.iter().enumerate() { |
| 779 |
if !removed.contains(&op.node()) { |
| 780 |
ops_map.insert(i as u32, kept_ops.len() as u32); |
| 781 |
kept_ops.push(op.clone()); |
| 782 |
} |
| 783 |
} |
| 784 |
c2.tree_ops = kept_ops; |
| 785 |
c2.edits.retain(|e| !removed.contains(&e.node())); |
| 786 |
if c1.tree_ops.is_empty() && c1.edits.is_empty() { |
| 787 |
return fail("the named paths are not touched by that change"); |
| 788 |
} |
| 789 |
if c2.tree_ops.is_empty() && c2.edits.is_empty() { |
| 790 |
return fail( |
| 791 |
"everything in that change is under the named paths; use `oxy abandon` instead", |
| 792 |
); |
| 793 |
} |
| 794 |
let mut d1: BTreeSet<ChangeHash> = referenced_changes(&c1, &beneath); |
| 795 |
d1.extend(old.deps.iter().copied()); |
| 796 |
c1.deps = d1.into_iter().collect(); |
| 797 |
c1.normalize(); |
| 798 |
let mut d2: BTreeSet<ChangeHash> = referenced_changes(&c2, &beneath); |
| 799 |
d2.extend(old.deps.iter().copied()); |
| 800 |
c2.deps = d2.into_iter().collect(); |
| 801 |
c2.normalize(); |
| 802 |
let corr = Correspondence { |
| 803 |
atoms: Correspondence::identity(&old).atoms, |
| 804 |
ops: ops_map, |
| 805 |
}; |
| 806 |
let h2 = self |
| 807 |
.rewrite_change( |
| 808 |
hash, |
| 809 |
Some(&c2), |
| 810 |
&corr, |
| 811 |
&format!("oxy split {} {}", old.change_id.short(8), paths.join(" ")), |
| 812 |
)? |
| 813 |
.unwrap(); |
| 814 |
let h1 = self.store_signed(&c1)?; |
| 815 |
self.add_to_bookmark(h1)?; |
| 816 |
Ok((h1, h2)) |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
impl Repo { |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
pub fn squash(&self, c: &ChangeHash, into: &ChangeHash) -> Result<ChangeHash> { |
| 825 |
if c == into { |
| 826 |
return fail("a change cannot be squashed into itself"); |
| 827 |
} |
| 828 |
let src = self.store.get_change(c)?; |
| 829 |
let dst = self.store.get_change(into)?; |
| 830 |
if src.kind == ChangeKind::WorkingCopy { |
| 831 |
return fail("that is the working copy; use `oxy amend --into`"); |
| 832 |
} |
| 833 |
if dst.kind == ChangeKind::WorkingCopy { |
| 834 |
return fail("cannot squash into the working copy"); |
| 835 |
} |
| 836 |
let closure = self.load_state(&[*into])?; |
| 837 |
let outside: Vec<String> = src |
| 838 |
.deps |
| 839 |
.iter() |
| 840 |
.filter(|d| *d != into && !closure.contains(d)) |
| 841 |
.map(|d| d.short(12)) |
| 842 |
.collect(); |
| 843 |
if !outside.is_empty() { |
| 844 |
return fail(format!("{} depends on {} which #{} does not; squash those first or squash into a later change", src.change_id.short(8), outside.join(", "), into.short(12))); |
| 845 |
} |
| 846 |
let ok_change = |h: &ChangeHash| *h == *into || closure.contains(h); |
| 847 |
let ok_node = |n: &NodeId| n.is_root() || closure.tree().introducer(n).is_some(); |
| 848 |
let too_new = |what: &str| { |
| 849 |
RepoError::Msg(format!( |
| 850 |
"{what} was introduced after change #{}; squash into a later change instead", |
| 851 |
into.short(12) |
| 852 |
)) |
| 853 |
}; |
| 854 |
let src_added: BTreeSet<NodeId> = src |
| 855 |
.tree_ops |
| 856 |
.iter() |
| 857 |
.filter_map(|o| { |
| 858 |
if let TreeOp::AddNode { node, .. } = o { |
| 859 |
Some(*node) |
| 860 |
} else { |
| 861 |
None |
| 862 |
} |
| 863 |
}) |
| 864 |
.collect(); |
| 865 |
for op in &src.tree_ops { |
| 866 |
match op { |
| 867 |
TreeOp::AddNode { parent, .. } => { |
| 868 |
if !ok_node(parent) && !src_added.contains(parent) { |
| 869 |
return Err(too_new("the parent directory")); |
| 870 |
} |
| 871 |
} |
| 872 |
TreeOp::RemoveNode { node, placement } => { |
| 873 |
if !ok_node(node) || !ok_change(&placement.change) { |
| 874 |
return Err(too_new("a removed file")); |
| 875 |
} |
| 876 |
} |
| 877 |
TreeOp::Move { |
| 878 |
node, old, parent, .. |
| 879 |
} => { |
| 880 |
if !ok_node(node) |
| 881 |
|| !ok_change(&old.change) |
| 882 |
|| (!ok_node(parent) && !src_added.contains(parent)) |
| 883 |
{ |
| 884 |
return Err(too_new("a moved file")); |
| 885 |
} |
| 886 |
} |
| 887 |
TreeOp::SetMode { node, old, .. } => { |
| 888 |
if !ok_node(node) || !ok_change(&old.change) { |
| 889 |
return Err(too_new("a file whose mode changed")); |
| 890 |
} |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
for e in &src.edits { |
| 895 |
let n = e.node(); |
| 896 |
if !src_added.contains(&n) && !ok_node(&n) { |
| 897 |
return Err(too_new("an edited file")); |
| 898 |
} |
| 899 |
let refs: Vec<ChangeHash> = match e { |
| 900 |
Edit::Insert { after, before, .. } => [after, before] |
| 901 |
.iter() |
| 902 |
.filter_map(|r| { |
| 903 |
if let AtomRef::Atom { change, .. } = r { |
| 904 |
Some(*change) |
| 905 |
} else { |
| 906 |
None |
| 907 |
} |
| 908 |
}) |
| 909 |
.collect(), |
| 910 |
Edit::Delete { atoms, .. } => atoms.iter().map(|r| r.change).collect(), |
| 911 |
Edit::Replace { .. } => vec![], |
| 912 |
}; |
| 913 |
if refs.iter().any(|h| !h.is_zero() && !ok_change(h)) { |
| 914 |
return Err(too_new("an edited line")); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
let mut per_node: HashMap<NodeId, u32> = HashMap::new(); |
| 920 |
for e in &dst.edits { |
| 921 |
if let Edit::Insert { node, lines, .. } = e { |
| 922 |
*per_node.entry(*node).or_default() += lines.len() as u32; |
| 923 |
} |
| 924 |
} |
| 925 |
let op_offset = dst.tree_ops.len() as u32; |
| 926 |
let fold_op = |r: OpRef| -> OpRef { |
| 927 |
if r.change == *into { |
| 928 |
OpRef { |
| 929 |
change: ObjectRef::ZERO, |
| 930 |
index: r.index, |
| 931 |
} |
| 932 |
} else if r.change.is_zero() { |
| 933 |
OpRef { |
| 934 |
change: ObjectRef::ZERO, |
| 935 |
index: r.index + op_offset, |
| 936 |
} |
| 937 |
} else { |
| 938 |
r |
| 939 |
} |
| 940 |
}; |
| 941 |
let mut tree_ops = dst.tree_ops.clone(); |
| 942 |
for op in &src.tree_ops { |
| 943 |
tree_ops.push(match op { |
| 944 |
TreeOp::RemoveNode { node, placement } => TreeOp::RemoveNode { |
| 945 |
node: *node, |
| 946 |
placement: fold_op(*placement), |
| 947 |
}, |
| 948 |
TreeOp::Move { |
| 949 |
node, |
| 950 |
old, |
| 951 |
parent, |
| 952 |
name, |
| 953 |
} => TreeOp::Move { |
| 954 |
node: *node, |
| 955 |
old: fold_op(*old), |
| 956 |
parent: *parent, |
| 957 |
name: name.clone(), |
| 958 |
}, |
| 959 |
TreeOp::SetMode { node, old, mode } => TreeOp::SetMode { |
| 960 |
node: *node, |
| 961 |
old: fold_op(*old), |
| 962 |
mode: *mode, |
| 963 |
}, |
| 964 |
add => add.clone(), |
| 965 |
}); |
| 966 |
} |
| 967 |
let mut edits = dst.edits.clone(); |
| 968 |
for e in &src.edits { |
| 969 |
edits.push(match e { |
| 970 |
Edit::Insert { |
| 971 |
node, |
| 972 |
after, |
| 973 |
before, |
| 974 |
lines, |
| 975 |
} => Edit::Insert { |
| 976 |
node: *node, |
| 977 |
after: fold_atom(*after, into, &per_node, node) |
| 978 |
.ok_or_else(|| too_new("an anchor line"))?, |
| 979 |
before: fold_atom(*before, into, &per_node, node) |
| 980 |
.ok_or_else(|| too_new("an anchor line"))?, |
| 981 |
lines: lines.clone(), |
| 982 |
}, |
| 983 |
Edit::Delete { node, atoms } => Edit::Delete { |
| 984 |
node: *node, |
| 985 |
atoms: atoms |
| 986 |
.iter() |
| 987 |
.map(|r| { |
| 988 |
if r.change == *into { |
| 989 |
AtomRange { |
| 990 |
change: ObjectRef::ZERO, |
| 991 |
from: r.from, |
| 992 |
to: r.to, |
| 993 |
} |
| 994 |
} else if r.change.is_zero() { |
| 995 |
let off = per_node.get(node).copied().unwrap_or(0); |
| 996 |
AtomRange { |
| 997 |
change: ObjectRef::ZERO, |
| 998 |
from: r.from + off, |
| 999 |
to: r.to + off, |
| 1000 |
} |
| 1001 |
} else { |
| 1002 |
*r |
| 1003 |
} |
| 1004 |
}) |
| 1005 |
.collect(), |
| 1006 |
}, |
| 1007 |
other => other.clone(), |
| 1008 |
}); |
| 1009 |
} |
| 1010 |
let mut new = Change { |
| 1011 |
change_id: dst.change_id, |
| 1012 |
deps: vec![], |
| 1013 |
author: dst.author, |
| 1014 |
hlc: Hlc::now(), |
| 1015 |
message: dst.message.clone(), |
| 1016 |
tree_ops, |
| 1017 |
edits, |
| 1018 |
kind: if dst.kind == ChangeKind::Resolution || src.kind == ChangeKind::Resolution { |
| 1019 |
ChangeKind::Resolution |
| 1020 |
} else { |
| 1021 |
ChangeKind::Normal |
| 1022 |
}, |
| 1023 |
}; |
| 1024 |
let beneath = self.load_state(&dst.deps)?; |
| 1025 |
let mut deps: BTreeSet<ChangeHash> = dst.deps.iter().copied().collect(); |
| 1026 |
deps.extend( |
| 1027 |
referenced_changes(&new, &beneath) |
| 1028 |
.into_iter() |
| 1029 |
.filter(|h| *h != *into && *h != *c), |
| 1030 |
); |
| 1031 |
deps.extend(src.deps.iter().filter(|d| **d != *into).copied()); |
| 1032 |
new.deps = deps.into_iter().collect(); |
| 1033 |
new.normalize(); |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
let cmd = format!( |
| 1038 |
"oxy squash {} --into {}", |
| 1039 |
src.change_id.short(8), |
| 1040 |
dst.change_id.short(8) |
| 1041 |
); |
| 1042 |
let dst_new = self |
| 1043 |
.rewrite_change(into, Some(&new), &Correspondence::identity(&dst), &cmd)? |
| 1044 |
.unwrap(); |
| 1045 |
|
| 1046 |
let (wc_hash, _) = self.working_copy()?; |
| 1047 |
let mut all = self.bookmark_heads()?; |
| 1048 |
all.push(wc_hash); |
| 1049 |
let state = self.load_state(&all)?; |
| 1050 |
let src_now = state |
| 1051 |
.changes() |
| 1052 |
.find(|(_, m)| m.change_id == src.change_id) |
| 1053 |
.map(|(h, _)| *h) |
| 1054 |
.unwrap_or(*c); |
| 1055 |
let src_change = self.store.get_change(&src_now)?; |
| 1056 |
let mut corr = Correspondence { |
| 1057 |
atoms: HashMap::new(), |
| 1058 |
ops: HashMap::new(), |
| 1059 |
}; |
| 1060 |
let mut counts: HashMap<NodeId, u32> = HashMap::new(); |
| 1061 |
for e in &src_change.edits { |
| 1062 |
if let Edit::Insert { node, lines, .. } = e { |
| 1063 |
let off = per_node.get(node).copied().unwrap_or(0); |
| 1064 |
let idx = counts.entry(*node).or_default(); |
| 1065 |
for _ in lines { |
| 1066 |
corr.atoms.insert((*node, *idx), *idx + off); |
| 1067 |
*idx += 1; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
} |
| 1071 |
for i in 0..src_change.tree_ops.len() as u32 { |
| 1072 |
corr.ops.insert(i, i + op_offset); |
| 1073 |
} |
| 1074 |
let final_new = self.store.get_change(&dst_new)?; |
| 1075 |
self.rewrite_change(&src_now, Some(&final_new), &corr, &cmd)?; |
| 1076 |
Ok(dst_new) |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
pub fn rebase(&self, c: &ChangeHash, onto: &[ChangeHash]) -> Result<ChangeHash> { |
| 1083 |
let old = self.store.get_change(c)?; |
| 1084 |
if old.kind == ChangeKind::WorkingCopy { |
| 1085 |
return fail("that is the working copy; check out the new base instead"); |
| 1086 |
} |
| 1087 |
let target = self.load_state(onto)?; |
| 1088 |
if target.contains(c) { |
| 1089 |
return fail(format!( |
| 1090 |
"#{} is already part of the target state", |
| 1091 |
c.short(12) |
| 1092 |
)); |
| 1093 |
} |
| 1094 |
let onto = target.maximal(onto); |
| 1095 |
let target_snapshot = target.materialize(); |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
let mut with_c = target.clone(); |
| 1100 |
if with_c.add(old.clone()).is_err() { |
| 1101 |
return fail(format!( |
| 1102 |
"the target state lacks dependencies of #{}; rebase onto a state that has them", |
| 1103 |
c.short(12) |
| 1104 |
)); |
| 1105 |
} |
| 1106 |
let after = with_c.materialize(); |
| 1107 |
|
| 1108 |
let mut touched: BTreeSet<String> = BTreeSet::new(); |
| 1109 |
for p in target_snapshot.files.keys().chain(after.files.keys()) { |
| 1110 |
match (target_snapshot.files.get(p), after.files.get(p)) { |
| 1111 |
(Some(a), Some(b)) |
| 1112 |
if a.kind == b.kind && a.mode == b.mode && a.content == b.content => {} |
| 1113 |
_ => { |
| 1114 |
touched.insert(p.clone()); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
} |
| 1118 |
for p in &touched { |
| 1119 |
if target_snapshot |
| 1120 |
.files |
| 1121 |
.get(p) |
| 1122 |
.is_some_and(|f| f.is_conflicted()) |
| 1123 |
|| after.files.get(p).is_some_and(|f| f.is_conflicted()) |
| 1124 |
{ |
| 1125 |
return fail(format!("{p} would be conflicted; resolve the target state or the conflict with #{} first", c.short(12))); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 1130 |
let mut reuse: HashMap<String, (NodeId, NodeKind)> = HashMap::new(); |
| 1131 |
let content_of = |f: &crate::state::FileState| -> Result<Option<Vec<u8>>> { |
| 1132 |
Ok(match &f.content { |
| 1133 |
FileContent::Dir => None, |
| 1134 |
FileContent::Text { bytes, .. } => Some(bytes.clone()), |
| 1135 |
FileContent::Opaque { blobs } => match blobs.first() { |
| 1136 |
Some(b) => Some(self.read_blob(b)?), |
| 1137 |
None => None, |
| 1138 |
}, |
| 1139 |
}) |
| 1140 |
}; |
| 1141 |
for (p, f) in &target_snapshot.files { |
| 1142 |
if touched.contains(p) { |
| 1143 |
continue; |
| 1144 |
} |
| 1145 |
if let Some(content) = content_of(f)? { |
| 1146 |
entries.insert(p.clone(), FsEntry::new(f.kind, f.mode.exec, content)); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
for p in &touched { |
| 1150 |
if let Some(f) = after.files.get(p) { |
| 1151 |
if let Some(content) = content_of(f)? { |
| 1152 |
entries.insert(p.clone(), FsEntry::new(f.kind, f.mode.exec, content)); |
| 1153 |
} |
| 1154 |
if !target_snapshot.files.contains_key(p) { |
| 1155 |
reuse.insert(p.clone(), (f.node, f.kind)); |
| 1156 |
} |
| 1157 |
} |
| 1158 |
} |
| 1159 |
let built = self.build_from_entries(&target, &target_snapshot, reuse, &entries)?; |
| 1160 |
if built.tree_ops.is_empty() && built.edits.is_empty() { |
| 1161 |
return fail( |
| 1162 |
"rebasing would leave the change empty; the target already has its effect", |
| 1163 |
); |
| 1164 |
} |
| 1165 |
let mut new = Change { |
| 1166 |
change_id: old.change_id, |
| 1167 |
deps: vec![], |
| 1168 |
author: old.author, |
| 1169 |
hlc: Hlc::now(), |
| 1170 |
message: old.message.clone(), |
| 1171 |
tree_ops: built.tree_ops, |
| 1172 |
edits: built.edits, |
| 1173 |
kind: if built.resolves { |
| 1174 |
ChangeKind::Resolution |
| 1175 |
} else { |
| 1176 |
ChangeKind::Normal |
| 1177 |
}, |
| 1178 |
}; |
| 1179 |
let mut deps: BTreeSet<ChangeHash> = onto.iter().copied().collect(); |
| 1180 |
deps.extend(referenced_changes(&new, &target)); |
| 1181 |
deps.extend(built.contributors.iter().copied()); |
| 1182 |
new.deps = deps.into_iter().collect(); |
| 1183 |
new.normalize(); |
| 1184 |
let corr = Correspondence::by_content(&old, &new); |
| 1185 |
let cmd = format!( |
| 1186 |
"oxy rebase {} --onto {}", |
| 1187 |
old.change_id.short(8), |
| 1188 |
onto.iter() |
| 1189 |
.map(|h| h.short(12)) |
| 1190 |
.collect::<Vec<_>>() |
| 1191 |
.join(" ") |
| 1192 |
); |
| 1193 |
Ok(self.rewrite_change(c, Some(&new), &corr, &cmd)?.unwrap()) |
| 1194 |
} |
| 1195 |
} |