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