30.6 KiB, 3 runs
created by r2848102244:385, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Undoing one operation by writing its inverse, and saying what that costs. |
| 2 | //! |
| 3 | //! `ore undo` puts back a whole earlier state; this puts back one decision. A |
| 4 | //! maintainer who has taken a contribution and wants half of it does not want the |
| 5 | //! repository as it stood on Tuesday, and until now the only thing between those |
| 6 | //! two was doing it by hand. |
| 7 | //! |
| 8 | //! What is written is ordinary operations -- the inverse edits -- and one |
| 9 | //! [`Op::Reverts`] naming what they were for. Nothing is taken out of the |
| 10 | //! history, because nothing in this system ever is: the reverted operation stays |
| 11 | //! where it was, its author still wrote it, and a reader can see both the change |
| 12 | //! and its reversal. That the record grows is the design working rather than a |
| 13 | //! cost, and it is the difference between this and a forge whose tidier history |
| 14 | //! is tidier because what was rejected was erased before it landed. |
| 15 | //! |
| 16 | //! # What an inverse is, is not decided here |
| 17 | //! |
| 18 | //! [`Op::undoing`] is the table, and it is in the engine because a forge will |
| 19 | //! offer the same thing on a page and two tables of what an inverse is would be |
| 20 | //! two tables that could disagree about a history they both write into. What is |
| 21 | //! decided here is only what this tool reads and what it says. |
| 22 | //! |
| 23 | //! Three of the inverses need the state the operation was written against -- a |
| 24 | //! rename and a mode assertion record what they set and not what they replaced, |
| 25 | //! and a move records where content went and not where it came from. That state |
| 26 | //! is a render at the operation's **parents**, which is causally closed by |
| 27 | //! construction and costs one render. It is emphatically *not* a render of the |
| 28 | //! history with the operation taken out of it: an operation removed from the |
| 29 | //! middle leaves anchors naming atoms nothing created, and the renderer refuses |
| 30 | //! that rather than guessing, so the obvious implementation is impossible rather |
| 31 | //! than merely slow. |
| 32 | //! |
| 33 | //! # The bytes come back and the identity does not |
| 34 | //! |
| 35 | //! Restoring deleted text can only ever mint a copy. The tombstone set is |
| 36 | //! grow-only and is recomputed from the operation set on every render, so nothing |
| 37 | //! un-buries: a system that relabels a dead edge live can genuinely bring the |
| 38 | //! original back, and this one cannot. The consequence a person meets is that |
| 39 | //! `ore who` would credit whoever ran the revert for somebody else's writing, |
| 40 | //! which is the one thing this system claims never to do. So the copy is anchored |
| 41 | //! after the last byte of what it restores, the [`Op::Reverts`] record says what |
| 42 | //! was undone, and [`crate::verbs::who`] follows the two back to whoever wrote |
| 43 | //! the bytes first. See [`Restored`]. |
| 44 | //! |
| 45 | //! # And the flags say nothing about it, which this command has to say instead |
| 46 | //! |
| 47 | //! Every flag about two authors disagreeing keys on concurrency, without |
| 48 | //! exception: an overlap is *"only a concurrent pair is flagged"*, a tear |
| 49 | //! *"only a concurrent claim"*, a yield *"every member concurrent with it |
| 50 | //! yields"*, a stranding *"only a concurrent deleter is named"*. A revert is |
| 51 | //! authored against the frontier, so it is causally after everything it touches |
| 52 | //! and concurrent with nothing, and **not one of those flags can fire for |
| 53 | //! anything it does.** That is a property of what those flags are for rather than |
| 54 | //! an omission in them, and adding one would not fix it: there is no race to |
| 55 | //! report. |
| 56 | //! |
| 57 | //! It is worth being exact about the part that is *not* true, because the design |
| 58 | //! note this was built from says every flag in the engine keys on concurrency and |
| 59 | //! that is wrong. The cycle flags do not -- `Demoted`, `Dropped` and |
| 60 | //! `CrossedFile` are decided by the shape of the anchor graph, with no causality |
| 61 | //! argument anywhere near them -- and `MovedIntoDeleted` fires however the move |
| 62 | //! and the deletion were ordered, which the engine says outright. A revert can |
| 63 | //! and does raise those. What it can never raise is a flag about the work it |
| 64 | //! undid, or about anybody's work that was built on it, and that is the whole of |
| 65 | //! the hole. |
| 66 | //! |
| 67 | //! Which leaves this command as the only place a person can be told, so this |
| 68 | //! command tells them, every time, before it writes: what the revert will take |
| 69 | //! out, what else in the history is built on it, and -- in as many words -- that |
| 70 | //! reading `ore flags` afterwards is not a check on any of it. Saying nothing |
| 71 | //! here is the failure this module is shaped around; `--dry-run` is for anybody |
| 72 | //! who wants the answer without the operations. |
| 73 | |
| 74 | use crate::capture; |
| 75 | use crate::keys::mark_of; |
| 76 | use crate::place::{ |
| 77 | self, |
| 78 | Where, |
| 79 | }; |
| 80 | use crate::repo::Repo; |
| 81 | use crate::tree::{ |
| 82 | self, |
| 83 | Tree, |
| 84 | }; |
| 85 | use crate::verbs::{ |
| 86 | report, |
| 87 | report_moved_files, |
| 88 | show, |
| 89 | }; |
| 90 | |
| 91 | use oxedyne_fe2o3_core::prelude::*; |
| 92 | use oxedyne_fe2o3_ore::id::{ |
| 93 | ContentRange, |
| 94 | OpId, |
| 95 | }; |
| 96 | use oxedyne_fe2o3_ore::log::OpLog; |
| 97 | use oxedyne_fe2o3_ore::op::{ |
| 98 | Op, |
| 99 | Prior, |
| 100 | }; |
| 101 | use oxedyne_fe2o3_ore::seq::atom::Atoms; |
| 102 | |
| 103 | use std::collections::BTreeMap; |
| 104 | use std::str::FromStr; |
| 105 | |
| 106 | |
| 107 | /// How many operations are listed by name before the rest are counted. |
| 108 | const LIST_LIMIT: usize = 6; |
| 109 | |
| 110 | /// Longest stretch of restored content shown in the report. |
| 111 | const SHOW_LIMIT: usize = 40; |
| 112 | |
| 113 | |
| 114 | /// What `ore revert` was asked to do. |
| 115 | pub struct Asked { |
| 116 | /// The operation to undo. |
| 117 | pub op: OpId, |
| 118 | /// Say what the revert would write, and write none of it. |
| 119 | pub dry: bool, |
| 120 | } |
| 121 | |
| 122 | impl Asked { |
| 123 | |
| 124 | /// Reads the arguments `ore revert` was given. |
| 125 | /// |
| 126 | /// One identifier, spelled the way every command prints one, and optionally |
| 127 | /// `--dry-run`. There is no way to name a mark instead: a mark names a point |
| 128 | /// and a revert undoes an operation, and the two are not the same question -- |
| 129 | /// `ore undo <mark>` is the one that takes a point. |
| 130 | pub fn read(rest: &[String]) |
| 131 | -> Outcome<Self> |
| 132 | { |
| 133 | let dry = rest.iter().any(|a| a == "--dry-run"); |
| 134 | let named: Vec<&String> = rest.iter().filter(|a| !a.starts_with("--")).collect(); |
| 135 | let first = res!(named.first().ok_or_else(|| err!( |
| 136 | "`ore revert` needs one argument: the operation to undo, named the way \ |
| 137 | `ore log` and `ore who` print one, as in `ore revert r3065315576:4`."; |
| 138 | Invalid, Input, Missing))); |
| 139 | if named.len() > 1 { |
| 140 | return Err(err!( |
| 141 | "`ore revert` undoes one operation, and was given {}. Run it once for \ |
| 142 | each: the record each run writes says what that run undid, and a single \ |
| 143 | record naming several would say only that they went together.", |
| 144 | named.len(); |
| 145 | Invalid, Input, Excessive)); |
| 146 | } |
| 147 | let op = match OpId::from_str(first) { |
| 148 | Ok(id) => id, |
| 149 | Err(e) => return Err(err!(e, |
| 150 | "`ore revert` was given {:?}, which is not an operation identifier. \ |
| 151 | They are written r<replica>:<counter>, and `ore log` and `ore who` \ |
| 152 | print them in that form.", first; |
| 153 | Invalid, Input, Mismatch)), |
| 154 | }; |
| 155 | Ok(Self { op, dry }) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /// The inverse of one operation, worked out but not yet written. |
| 161 | /// |
| 162 | /// It is built whole before anything is authored, so that a report of what the |
| 163 | /// revert will do is a report of what it will actually do rather than of what it |
| 164 | /// intends to try. A revert that failed part way through would leave a history |
| 165 | /// holding half an inverse, which nothing downstream could tell from a whole one. |
| 166 | struct Plan { |
| 167 | /// The operation being undone. |
| 168 | target: OpId, |
| 169 | /// What it says, for the report. |
| 170 | op: Op, |
| 171 | /// The inverse edits, in the order they are authored. |
| 172 | edits: Vec<Op>, |
| 173 | /// Content the inverse kills, which is what leaves the working copy. |
| 174 | kills: Vec<ContentRange>, |
| 175 | /// Content the inverse puts back, as a copy under a new identity. |
| 176 | copies: Vec<ContentRange>, |
| 177 | /// Files the inverse retires, which is what undoing a file's creation is. |
| 178 | /// |
| 179 | /// Kept apart from the content, because it is a different size of |
| 180 | /// consequence and was silently reported as none at all when it was not: |
| 181 | /// undoing a creation takes every byte of that file out of every working copy |
| 182 | /// there will ever be, including whatever somebody has moved into it since. |
| 183 | retires: Vec<OpId>, |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /// `ore revert` -- writes the inverse of one operation, and a record saying so. |
| 188 | pub fn revert(repo: &mut Repo, asked: &Asked) |
| 189 | -> Outcome<()> |
| 190 | { |
| 191 | let what = res!(capture::capture(repo)); |
| 192 | report(&what); |
| 193 | println!(); |
| 194 | // Cloned out of the log at once, because everything after this borrows the |
| 195 | // repository to render it and the record would be borrowed with it. |
| 196 | let (op, parents) = { |
| 197 | let rec = res!(repo.log.get(&asked.op).ok_or_else(|| err!( |
| 198 | "This repository holds no operation {}. It holds {}, and `ore log` and \ |
| 199 | `ore who` print the identity of each. An operation another replica has \ |
| 200 | written is revertible here only once a sync has brought it over.", |
| 201 | asked.op, |
| 202 | match repo.log.len() { |
| 203 | 1 => fmt!("one operation"), |
| 204 | n => fmt!("{} operations", n), |
| 205 | }; |
| 206 | Invalid, Input, NotFound))); |
| 207 | (rec.op.clone(), rec.parents().to_vec()) |
| 208 | }; |
| 209 | let plan = res!(plan(repo, asked.op, op, &parents)); |
| 210 | // Said before anything is written, every time, because it is the only place it |
| 211 | // can be said: no flag fires for a revert, so the flag surface will look |
| 212 | // afterwards exactly as it looks now. |
| 213 | res!(foretell(repo, &plan)); |
| 214 | if asked.dry { |
| 215 | println!(); |
| 216 | println!("nothing was written: this was a dry run, and the {} operation{} above \ |
| 217 | {} the whole of what `ore revert {}` would write", |
| 218 | plan.edits.len() + 1, |
| 219 | if plan.edits.len() == 0 { "" } else { "s" }, |
| 220 | if plan.edits.is_empty() { "is" } else { "are" }, |
| 221 | plan.target); |
| 222 | return Ok(()); |
| 223 | } |
| 224 | res!(write(repo, &plan)); |
| 225 | Ok(()) |
| 226 | } |
| 227 | |
| 228 | /// Works out the inverse, reading whatever the operation itself could not say. |
| 229 | fn plan(repo: &Repo, target: OpId, op: Op, parents: &[OpId]) |
| 230 | -> Outcome<Plan> |
| 231 | { |
| 232 | let undoing = res!(op.undoing(target)); |
| 233 | let mut edits = undoing.written.clone(); |
| 234 | // What the inverse takes out of the working copy, which is what a splice's own |
| 235 | // insertion is undone by and is nothing at all for the rest. |
| 236 | let kills: Vec<ContentRange> = undoing.written.iter() |
| 237 | .flat_map(|e| e.regions().iter().copied()) |
| 238 | .collect(); |
| 239 | // And whichever files the inverse retires, which is a whole file's worth of |
| 240 | // consequence and not a content range at all. |
| 241 | let retires: Vec<OpId> = undoing.written.iter() |
| 242 | .filter_map(|e| match e { |
| 243 | Op::FileDelete { file } => Some(*file), |
| 244 | _ => None, |
| 245 | }) |
| 246 | .collect(); |
| 247 | // The bytes of what the operation buried. They are in the log whether or not |
| 248 | // they render anywhere, which is the whole reason a copy can be made of them. |
| 249 | if !undoing.copies.is_empty() { |
| 250 | let held: Vec<(OpId, &Op)> = repo.log.iter() |
| 251 | .map(|rec| (rec.head.id(), &rec.op)) |
| 252 | .collect(); |
| 253 | let atoms = res!(Atoms::build(&held)); |
| 254 | for was in &undoing.copies { |
| 255 | let bytes = res!(atoms.slice(was)).to_vec(); |
| 256 | edits.push(res!(Op::restoring(was, bytes))); |
| 257 | } |
| 258 | } |
| 259 | // And the one question an operation that asserts a value cannot answer about |
| 260 | // itself: what it replaced. |
| 261 | if let Some(prior) = &undoing.prior { |
| 262 | let before = match tree::at(&repo.log, parents) { |
| 263 | Ok(t) => t, |
| 264 | Err(e) => return Err(err!(e, |
| 265 | "The state {} was written against could not be rendered, and undoing a \ |
| 266 | {} needs it: the operation records what it set and not what it \ |
| 267 | replaced.", target, op.name(); |
| 268 | Invalid, Data)), |
| 269 | }; |
| 270 | edits.extend(res!(replaced(&before, prior, &op, target))); |
| 271 | } |
| 272 | Ok(Plan { target, op, edits, kills, copies: undoing.copies, retires }) |
| 273 | } |
| 274 | |
| 275 | /// Reads out of the state an operation was written against the one thing its |
| 276 | /// inverse needs and it did not record. |
| 277 | fn replaced(before: &Tree, prior: &Prior, op: &Op, target: OpId) |
| 278 | -> Outcome<Vec<Op>> |
| 279 | { |
| 280 | match prior { |
| 281 | Prior::Path { file } => { |
| 282 | let was = res!(before.get(*file).ok_or_else(|| err!( |
| 283 | "The state {} was written against holds no file {}, though the rename \ |
| 284 | names it.", target, file; |
| 285 | Bug, Missing))); |
| 286 | let path = was.path().to_vec(); |
| 287 | // A rename that put a file where it already was has nothing to undo, and |
| 288 | // writing the rename anyway would be an operation saying nothing. |
| 289 | match op { |
| 290 | Op::FileRename { path: now, .. } if *now == path => Ok(Vec::new()), |
| 291 | _ => Ok(vec![Op::FileRename { file: *file, path }]), |
| 292 | } |
| 293 | }, |
| 294 | Prior::Mode { file } => { |
| 295 | let was = res!(before.get(*file).ok_or_else(|| err!( |
| 296 | "The state {} was written against holds no file {}, though the mode \ |
| 297 | assertion names it.", target, file; |
| 298 | Bug, Missing))); |
| 299 | let mode = was.mode(); |
| 300 | match op { |
| 301 | Op::FileMode { mode: now, .. } if *now == mode => Ok(Vec::new()), |
| 302 | _ => Ok(vec![Op::FileMode { file: *file, mode }]), |
| 303 | } |
| 304 | }, |
| 305 | Prior::Place { src } => { |
| 306 | // One move per run, each back to the gap that run began at. One move |
| 307 | // carrying every run would put them all in one place, which is where the |
| 308 | // original move put them and not where they came from. |
| 309 | let placed = before.repo.placement(); |
| 310 | let mut out = Vec::new(); |
| 311 | for run in src { |
| 312 | let found = placed.find(std::slice::from_ref(run)); |
| 313 | let place = res!(found.first().ok_or_else(|| err!( |
| 314 | "The state {} was written against shows {} in no file, though the \ |
| 315 | move took it from one.", target, run; |
| 316 | Bug, Missing))); |
| 317 | let first = res!(place.spans.first().ok_or_else(|| err!( |
| 318 | "The state {} was written against puts {} in {} at no offset.", |
| 319 | target, run, place.file; |
| 320 | Bug, Missing))).at; |
| 321 | let last = res!(place.spans.last().ok_or_else(|| err!( |
| 322 | "The state {} was written against puts {} in {} at no offset.", |
| 323 | target, run, place.file; |
| 324 | Bug, Missing))).end(); |
| 325 | let rendered = res!(before.get(place.file).ok_or_else(|| err!( |
| 326 | "The state {} was written against names the file {}, which it does \ |
| 327 | not hold.", target, place.file; |
| 328 | Bug, Missing))); |
| 329 | // The gap the run **occupied**, and emphatically not the gap in front |
| 330 | // of its first byte. That gap's right origin is the run's own first |
| 331 | // byte, so the move would name its own source as its destination; the |
| 332 | // renderer breaks that cycle by demoting both origins, and reports two |
| 333 | // flags about a move that was perfectly ordinary. The bytes still land |
| 334 | // correctly, which is what makes it worth writing down: it was found by |
| 335 | // running `ore flags` after a revert and not by reading the result. |
| 336 | let (left, _) = res!(rendered.gap(first as usize)); |
| 337 | let (_, right) = res!(rendered.gap(last as usize)); |
| 338 | out.push(Op::Move { src: vec![*run], left, right }); |
| 339 | } |
| 340 | Ok(out) |
| 341 | }, |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /// Says what the revert will do, and what nothing will say about it afterwards. |
| 347 | /// |
| 348 | /// Three questions, in the order somebody deciding would ask them: what is being |
| 349 | /// undone, what will be written, and what else in this history is built on the |
| 350 | /// work going away. The fourth section is not a question and is printed whether or |
| 351 | /// not anything was found, because what it reports is that a whole surface of this |
| 352 | /// tool has nothing to say about this command and never will. |
| 353 | fn foretell(repo: &Repo, plan: &Plan) |
| 354 | -> Outcome<()> |
| 355 | { |
| 356 | let tree = res!(tree::whole(&repo.log)); |
| 357 | let placed = Where::of(&tree); |
| 358 | let put = tree.repo.placement(); |
| 359 | |
| 360 | println!("reverting {}, a {} by {}", plan.target, plan.op.name(), |
| 361 | plan.target.replica); |
| 362 | if let Some(rec) = repo.log.get(&plan.target) { |
| 363 | println!(" {} written over {}", mark_of(&repo.prov, plan.target), |
| 364 | match rec.parents().len() { |
| 365 | 0 => fmt!("nothing, being a root"), |
| 366 | 1 => fmt!("one operation"), |
| 367 | n => fmt!("{} operations", n), |
| 368 | }); |
| 369 | } |
| 370 | |
| 371 | // A whole file going is said first, and in its own terms. Counted as content |
| 372 | // ranges it comes to none, which is how a revert that retired a file holding a |
| 373 | // hundred and fifty bytes of somebody else's moved work announced itself as |
| 374 | // taking nothing out of any file. |
| 375 | println!(); |
| 376 | for file in &plan.retires { |
| 377 | match tree.get(*file) { |
| 378 | Some(rendered) => println!("this retires the file {}, which holds {} byte{} \ |
| 379 | now; they stay in the log and render nowhere a reader looks", |
| 380 | tree::shown(rendered.path()), |
| 381 | rendered.len(), if rendered.len() == 1 { "" } else { "s" }), |
| 382 | None => println!("this retires the file {}, which this render does not \ |
| 383 | hold", file), |
| 384 | } |
| 385 | println!(" and a file's deletion has no inverse, so this is the one revert \ |
| 386 | that cannot itself be reverted; `ore undo` writes the bytes again as a new \ |
| 387 | file, which is a different file"); |
| 388 | } |
| 389 | |
| 390 | // What leaves the working copy, and where it is now. Content that renders |
| 391 | // nowhere is said to render nowhere: somebody else may already have deleted |
| 392 | // it, and a revert that removes nothing visible is worth knowing about before |
| 393 | // it is written rather than after. |
| 394 | if plan.kills.is_empty() && plan.retires.is_empty() { |
| 395 | println!("this takes no content out of any file"); |
| 396 | } else if !plan.kills.is_empty() { |
| 397 | let found = put.find(&plan.kills); |
| 398 | let live: u64 = found.iter() |
| 399 | .flat_map(|p| p.spans.iter()) |
| 400 | .map(|s| s.len) |
| 401 | .sum(); |
| 402 | let named: u64 = plan.kills.iter().map(|r| r.len()).sum(); |
| 403 | if live == 0 { |
| 404 | println!("this removes {} byte{}, none of which any file shows now: \ |
| 405 | something has already taken them out", |
| 406 | named, if named == 1 { "" } else { "s" }); |
| 407 | } else { |
| 408 | println!("this removes {} byte{}, {}", |
| 409 | live, if live == 1 { "" } else { "s" }, |
| 410 | match live == named { |
| 411 | true => placed.of_content(&plan.kills), |
| 412 | false => fmt!("of the {} it names, {}; the rest is in no file", |
| 413 | named, placed.of_content(&plan.kills)), |
| 414 | }); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // And what comes back, which is a copy and has to be called one. |
| 419 | if !plan.copies.is_empty() { |
| 420 | let bytes: u64 = plan.copies.iter().map(|r| r.len()).sum(); |
| 421 | let already = put.find(&plan.copies); |
| 422 | println!("this puts back {} byte{} in {} run{}, as a copy under a new identity: \ |
| 423 | nothing here un-buries, so the bytes return and the names they had do not", |
| 424 | bytes, if bytes == 1 { "" } else { "s" }, |
| 425 | plan.copies.len(), if plan.copies.len() == 1 { "" } else { "s" }); |
| 426 | if !already.is_empty() { |
| 427 | println!(" and some of it renders already, so this history will show it \ |
| 428 | twice; that is what reverting a deletion somebody has themselves \ |
| 429 | reverted does"); |
| 430 | } |
| 431 | let held: Vec<(OpId, &Op)> = repo.log.iter() |
| 432 | .map(|rec| (rec.head.id(), &rec.op)) |
| 433 | .collect(); |
| 434 | let atoms = res!(Atoms::build(&held)); |
| 435 | for was in &plan.copies { |
| 436 | println!(" {:<20} first written by {} {}", |
| 437 | fmt!("{}", was), |
| 438 | mark_of(&repo.prov, was.op()), |
| 439 | show(res!(atoms.slice(was)), SHOW_LIMIT)); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | println!(); |
| 444 | println!("it writes {} operation{}: {} and one record saying {} was undone", |
| 445 | plan.edits.len() + 1, |
| 446 | if plan.edits.is_empty() { "" } else { "s" }, |
| 447 | match plan.edits.len() { |
| 448 | 0 => fmt!("no edit at all, this operation having nothing left to undo"), |
| 449 | 1 => fmt!("one edit"), |
| 450 | n => fmt!("{} edits", n), |
| 451 | }, |
| 452 | plan.target); |
| 453 | if plan.edits.is_empty() { |
| 454 | println!("an operation whose inverse is nothing is one that changed nothing; \ |
| 455 | the record is still written, so that the history says the attempt was made"); |
| 456 | } |
| 457 | |
| 458 | // What else is built on the work going away. Every one of these is a thing the |
| 459 | // flag surface would report if the revert were somebody else's concurrent edit, |
| 460 | // and will not report because it is not. |
| 461 | println!(); |
| 462 | println!("what goes with it"); |
| 463 | res!(consequences(repo, plan, &placed, &tree)); |
| 464 | |
| 465 | println!(); |
| 466 | println!("what nothing will tell you afterwards"); |
| 467 | println!(" No flag will report any of the above. Every flag about two authors \ |
| 468 | disagreeing -- an overlap, a tear, a yield, a stranding -- fires only where \ |
| 469 | neither author could see the other, and a revert is written against the \ |
| 470 | frontier: it is causally after everything it touches, so it is concurrent \ |
| 471 | with nothing."); |
| 472 | println!(" So `ore flags` will not say that this happened, and reading it \ |
| 473 | afterwards is not a check on any of it. This paragraph is the check, and it \ |
| 474 | is printed whether or not anything was found above."); |
| 475 | println!(" A flag may still fire for what this writes, which is a different \ |
| 476 | thing: the renderer reports a cycle among a move's anchors, and a move into \ |
| 477 | a file that has been deleted, however those operations were ordered. Those \ |
| 478 | are about the shape of the result and not about what was undone."); |
| 479 | println!(" Nor does anything above count work on a replica this one has not \ |
| 480 | synced with. What is measured is this log."); |
| 481 | Ok(()) |
| 482 | } |
| 483 | |
| 484 | /// Writes what else in the history is built on the work being undone. |
| 485 | /// |
| 486 | /// Four readings, and they are four because they go wrong four different ways. |
| 487 | /// Work moved *into* a file this retires goes dark with it; an edit made *inside* |
| 488 | /// the text keeps its bytes and loses its surroundings; an edit that *moved* the |
| 489 | /// text took it somewhere a reader may not expect to lose it from; and a note |
| 490 | /// *about* the text becomes a note on dead content. None of these is refused, |
| 491 | /// because a revert that only worked where nothing was built on it would be a |
| 492 | /// revert nobody could use; what they are is the reason to look before writing. |
| 493 | fn consequences(repo: &Repo, plan: &Plan, placed: &Where, tree: &Tree) |
| 494 | -> Outcome<()> |
| 495 | { |
| 496 | if plan.kills.is_empty() && plan.copies.is_empty() && plan.retires.is_empty() { |
| 497 | println!(" nothing: this revert names no content, so nothing anchored to \ |
| 498 | content is touched by it"); |
| 499 | return Ok(()); |
| 500 | } |
| 501 | let mut inside: Vec<OpId> = Vec::new(); |
| 502 | let mut carried: Vec<OpId> = Vec::new(); |
| 503 | let mut noted: Vec<OpId> = Vec::new(); |
| 504 | let mut into: Vec<OpId> = Vec::new(); |
| 505 | for rec in repo.log.iter() { |
| 506 | let id = rec.head.id(); |
| 507 | if id == plan.target { |
| 508 | continue; |
| 509 | } |
| 510 | // Work put into a file this retires. It is not deleted and it is not lost; |
| 511 | // it renders nowhere, which for a reader with the file open is the same |
| 512 | // thing, and it is the largest consequence this command has. |
| 513 | if rec.op.is_move() { |
| 514 | if let Some(file) = tree.repo.file_of(&id) { |
| 515 | if plan.retires.contains(&file) { |
| 516 | into.push(id); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | // Anchored within the bytes being removed: its own work survives, at a site |
| 521 | // whose context has gone. |
| 522 | let (left, right) = rec.op.origins(); |
| 523 | let anchored = [left, right].iter() |
| 524 | .filter_map(|a| *a) |
| 525 | .any(|a| plan.kills.iter().any(|r| r.contains(&a.content))); |
| 526 | if anchored { |
| 527 | inside.push(id); |
| 528 | } |
| 529 | // Named the bytes: moved them somewhere, or deleted part of them already. |
| 530 | if rec.op.regions().iter().any(|r| touches(r, &plan.kills)) { |
| 531 | carried.push(id); |
| 532 | } |
| 533 | // Said something about them, or about the content coming back as a copy, |
| 534 | // which the copy does not inherit. |
| 535 | if rec.op.note_on().iter() |
| 536 | .any(|r| touches(r, &plan.kills) || touches(r, &plan.copies)) |
| 537 | { |
| 538 | noted.push(id); |
| 539 | } |
| 540 | } |
| 541 | // And the notes in a file this retires, which the reading above cannot find: |
| 542 | // a note about content that has since been moved into the file names content |
| 543 | // of whatever wrote it, and nothing in the operation says which file that |
| 544 | // content is in now. The render says, and the render is what is asked. |
| 545 | for note in tree.repo.notes() { |
| 546 | if note.files().iter().any(|p| plan.retires.contains(&p.file)) { |
| 547 | noted.push(note.note()); |
| 548 | } |
| 549 | } |
| 550 | noted.sort(); |
| 551 | noted.dedup(); |
| 552 | let mut said = false; |
| 553 | if !into.is_empty() { |
| 554 | said = true; |
| 555 | println!(" {} operation{} moved content into a file this retires. Those \ |
| 556 | bytes go dark with it, and this is the one thing here the renderer does \ |
| 557 | report afterwards, as a buried flag:", |
| 558 | into.len(), if into.len() == 1 { "" } else { "s" }); |
| 559 | list(repo, &into, placed); |
| 560 | } |
| 561 | if !inside.is_empty() { |
| 562 | said = true; |
| 563 | println!(" {} operation{} wrote inside the bytes being removed. Their work \ |
| 564 | stays where it is, with the text it was written into gone from around it:", |
| 565 | inside.len(), if inside.len() == 1 { "" } else { "s" }); |
| 566 | list(repo, &inside, placed); |
| 567 | } |
| 568 | if !carried.is_empty() { |
| 569 | said = true; |
| 570 | println!(" {} operation{} named these bytes afterwards, moving or deleting \ |
| 571 | them. Whatever they did with them goes too:", |
| 572 | carried.len(), if carried.len() == 1 { "" } else { "s" }); |
| 573 | list(repo, &carried, placed); |
| 574 | } |
| 575 | if !noted.is_empty() { |
| 576 | said = true; |
| 577 | println!(" {} note{} about content this touches. A note follows content and \ |
| 578 | not bytes, so it stays with what is being taken away rather than moving to \ |
| 579 | whatever is put back:", |
| 580 | noted.len(), if noted.len() == 1 { "" } else { "s" }); |
| 581 | list(repo, ¬ed, placed); |
| 582 | } |
| 583 | if !said { |
| 584 | println!(" nothing else in this history names the content this touches"); |
| 585 | } |
| 586 | Ok(()) |
| 587 | } |
| 588 | |
| 589 | /// Writes a list of operations, with what is known about who wrote each and |
| 590 | /// where its own work is. |
| 591 | /// |
| 592 | /// The coordinate is of the operation's own content -- what a splice inserted or |
| 593 | /// what a move carried -- read through [`crate::place::wrote`], which is the same |
| 594 | /// reading `ore flags` puts under a flag. Looking the operation up in the render's |
| 595 | /// file index instead gives the file and no offset, since a move mints no atom |
| 596 | /// and owns no run of its own name. |
| 597 | fn list(repo: &Repo, ids: &[OpId], placed: &Where) { |
| 598 | for id in ids.iter().take(LIST_LIMIT) { |
| 599 | println!(" {} {:<14} {}", mark_of(&repo.prov, *id), fmt!("{}", id), |
| 600 | placed.of_content(&about(*id, &repo.log))); |
| 601 | } |
| 602 | if ids.len() > LIST_LIMIT { |
| 603 | println!(" and {} more", ids.len() - LIST_LIMIT); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /// Returns the content a listed operation is about. |
| 608 | /// |
| 609 | /// What it wrote, for everything that writes anything, which is |
| 610 | /// [`crate::place::wrote`]. A note writes nothing and is about something, and |
| 611 | /// asking the first question of it gives no coordinate at all; the two readings |
| 612 | /// are kept apart in the engine for exactly this reason, so both are asked here. |
| 613 | fn about(id: OpId, log: &OpLog) -> Vec<ContentRange> { |
| 614 | match log.op(&id) { |
| 615 | Some(Op::Note { on, .. }) => on.clone(), |
| 616 | _ => place::wrote(id, log), |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /// Reports whether a content range meets any of a set of them. |
| 621 | fn touches(one: &ContentRange, set: &[ContentRange]) -> bool { |
| 622 | set.iter().any(|other| one.intersects(other)) |
| 623 | } |
| 624 | |
| 625 | |
| 626 | /// Authors the record and the inverse edits, and brings the working copy to the |
| 627 | /// state they describe. |
| 628 | /// |
| 629 | /// The record goes **first**, and every edit names it among its parents. That is |
| 630 | /// what lets a reader with one edit in hand find the record that says what the |
| 631 | /// edit was for, without walking the graph and without guessing from the shape of |
| 632 | /// the operation: see [`Restored`], which is the reader that needs it. |
| 633 | fn write(repo: &mut Repo, plan: &Plan) |
| 634 | -> Outcome<()> |
| 635 | { |
| 636 | let replica = repo.cfg.replica; |
| 637 | let record = res!(repo.author(replica, Op::Reverts { |
| 638 | undone: vec![plan.target], |
| 639 | })); |
| 640 | println!(); |
| 641 | println!("{} says that {} was undone", record, plan.target); |
| 642 | for edit in &plan.edits { |
| 643 | let mut parents = repo.log.frontier(); |
| 644 | if !parents.contains(&record) { |
| 645 | parents.push(record); |
| 646 | } |
| 647 | let id = res!(repo.author_with(replica, parents, edit.clone())); |
| 648 | println!("{} {}", id, describe(edit)); |
| 649 | } |
| 650 | // The working copy is behind the log the moment the edits are authored, so it |
| 651 | // is written forward here rather than left for the next verb to capture: a |
| 652 | // capture would record the difference as somebody's edit, undoing the undo. |
| 653 | let tree = res!(tree::whole(&repo.log)); |
| 654 | let moved = res!(tree::materialise(&repo.root, &tree, tree::Surplus::Remove)); |
| 655 | println!(); |
| 656 | println!("the working copy is now the state with {} undone", plan.target); |
| 657 | report_moved_files(&moved); |
| 658 | Ok(()) |
| 659 | } |
| 660 | |
| 661 | /// Says in a few words what one inverse edit does. |
| 662 | fn describe(op: &Op) -> String { |
| 663 | match op { |
| 664 | Op::Splice { insert, remove, .. } if insert.is_empty() => fmt!( |
| 665 | "removes {} run{} of content", |
| 666 | remove.len(), if remove.len() == 1 { "" } else { "s" }), |
| 667 | Op::Splice { insert, .. } => match op.restored() { |
| 668 | Some(was) => fmt!("puts back a copy of {}, {} byte{}", |
| 669 | was, insert.len(), if insert.len() == 1 { "" } else { "s" }), |
| 670 | None => fmt!("inserts {} byte{}", |
| 671 | insert.len(), if insert.len() == 1 { "" } else { "s" }), |
| 672 | }, |
| 673 | Op::Move { src, .. } => fmt!("takes {} run{} back where they were", |
| 674 | src.len(), if src.len() == 1 { "" } else { "s" }), |
| 675 | Op::FileRename { path, .. } => fmt!("puts the file back at {}", tree::shown(path)), |
| 676 | Op::FileMode { mode, .. } => fmt!("says the file is {} again", mode), |
| 677 | Op::FileDelete { file } => fmt!("retires the file {}", file), |
| 678 | other => fmt!("{}", other.name()), |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | |
| 683 | /// Which of this history's operations put back a copy of somebody else's |
| 684 | /// writing, and whose writing it was. |
| 685 | /// |
| 686 | /// This is the reader the [`Op::Reverts`] record exists for. Restored bytes take a |
| 687 | /// new identity the moment they come back, so an accounting that read only the |
| 688 | /// identity would credit whoever ran the revert with text somebody else wrote -- |
| 689 | /// and crediting the wrong author is the one thing this system claims never to do. |
| 690 | /// |
| 691 | /// # Three facts, and none of them is enough alone |
| 692 | /// |
| 693 | /// A copy is recognised by all three together: |
| 694 | /// |
| 695 | /// 1. The edit names an [`Op::Reverts`] among its **parents**. Only a revert |
| 696 | /// authors an operation directly against that record. |
| 697 | /// 2. The edit has the shape [`Op::restoring`] gives one, so [`Op::restored`] |
| 698 | /// can say which run it is a copy of. That shape is not unique -- an ordinary |
| 699 | /// insertion at the end of a file has it too -- which is why it is not the |
| 700 | /// whole test. |
| 701 | /// 3. The record's `undone` names an operation that actually **removed** that |
| 702 | /// run. This is the link followed back: the reverted operation says what it |
| 703 | /// killed, and what it killed says who wrote it. |
| 704 | /// |
| 705 | /// Any one of the three on its own would credit an author for text somebody |
| 706 | /// merely appended, so all three are checked, and an edit failing any of them is |
| 707 | /// reported as the ordinary insertion it is. |
| 708 | pub struct Restored { |
| 709 | /// The restoring edit, to the record vouching for it and the run it copies. |
| 710 | by: BTreeMap<OpId, (OpId, ContentRange)>, |
| 711 | } |
| 712 | |
| 713 | impl Restored { |
| 714 | |
| 715 | /// Reads a log for every copy a revert put back. |
| 716 | /// |
| 717 | /// One pass over the records and one over their parents, so it costs what |
| 718 | /// reading the log costs. It is built once per verb, not once per run. |
| 719 | pub fn of(log: &OpLog) -> Self { |
| 720 | // Every record that says something was undone, with what it says. |
| 721 | let mut records: BTreeMap<OpId, &[OpId]> = BTreeMap::new(); |
| 722 | for rec in log.iter() { |
| 723 | if let Op::Reverts { undone } = &rec.op { |
| 724 | records.insert(rec.head.id(), undone.as_slice()); |
| 725 | } |
| 726 | } |
| 727 | let mut by: BTreeMap<OpId, (OpId, ContentRange)> = BTreeMap::new(); |
| 728 | if records.is_empty() { |
| 729 | return Self { by }; |
| 730 | } |
| 731 | for rec in log.iter() { |
| 732 | let was = match rec.op.restored() { |
| 733 | Some(r) => r, |
| 734 | None => continue, |
| 735 | }; |
| 736 | for parent in rec.parents() { |
| 737 | let undone = match records.get(parent) { |
| 738 | Some(u) => *u, |
| 739 | None => continue, |
| 740 | }; |
| 741 | // The record names what was undone; the operation named says what it |
| 742 | // removed; and what it removed is what this is a copy of. Without this |
| 743 | // step the shape alone would be the whole of the evidence. |
| 744 | let vouched = undone.iter().any(|id| match log.op(id) { |
| 745 | Some(Op::Splice { remove, .. }) => remove.iter() |
| 746 | .any(|r| r.intersects(&was)), |
| 747 | _ => false, |
| 748 | }); |
| 749 | if vouched { |
| 750 | by.insert(rec.head.id(), (*parent, was)); |
| 751 | break; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | Self { by } |
| 756 | } |
| 757 | |
| 758 | /// Reports whether anything here was put back by a revert. |
| 759 | pub fn is_empty(&self) -> bool { |
| 760 | self.by.is_empty() |
| 761 | } |
| 762 | |
| 763 | /// Returns the record that vouched for a copy and the run it is a copy of. |
| 764 | pub fn of_op(&self, id: &OpId) |
| 765 | -> Option<(OpId, ContentRange)> |
| 766 | { |
| 767 | self.by.get(id).copied() |
| 768 | } |
| 769 | } |