| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use super::*; |
| 7 | |
|
| 8 | |
|
| 9 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 10 | | pub enum ViewChange { |
| 11 | | BookmarkCreated { |
| 12 | | name: String, |
| 13 | | heads: usize, |
| 14 | | }, |
| 15 | | BookmarkDeleted { |
| 16 | | name: String, |
| 17 | | }, |
| 18 | | BookmarkMoved { |
| 19 | | name: String, |
| 20 | | from: usize, |
| 21 | | to: usize, |
| 22 | | }, |
| 23 | | RemoteMoved { |
| 24 | | remote: String, |
| 25 | | name: String, |
| 26 | | }, |
| 27 | | TagCreated { |
| 28 | | name: String, |
| 29 | | }, |
| 30 | | TagDeleted { |
| 31 | | name: String, |
| 32 | | }, |
| 33 | | Policy, |
| 34 | | |
| 35 | | WorkingCopy { |
| 36 | | change: ChangeHash, |
| 37 | | }, |
| 38 | | } |
| 39 | |
|
| 40 | |
|
| 41 | | #[derive(Debug)] |
| 42 | | pub struct Undone { |
| 43 | | |
| 44 | | pub command: String, |
| 45 | | |
| 46 | | pub changes: Vec<ViewChange>, |
| 47 | | |
| 48 | | pub others_kept: usize, |
| 49 | | |
| 50 | | |
| 51 | | pub pushed: Vec<(String, String)>, |
| 52 | | } |
| 53 | |
|
| 54 | |
|
| 55 | | #[derive(Debug)] |
| 56 | | pub struct RestoreReport { |
| 57 | | |
| 58 | | pub op: ObjectRef, |
| 59 | | pub command: String, |
| 60 | | pub checkout: CheckoutReport, |
| 61 | | } |
| 62 | |
|
| 63 | |
|
| 64 | | #[derive(Debug)] |
| 65 | | pub struct RevertReport { |
| 66 | | pub op: ObjectRef, |
| 67 | | pub command: String, |
| 68 | | |
| 69 | | pub reverted: Vec<String>, |
| 70 | | |
| 71 | | |
| 72 | | pub kept: Vec<String>, |
| 73 | | |
| 74 | | pub checked_out: bool, |
| 75 | | } |
| 76 | |
|
| 77 | | impl Repo { |
| 78 | | |
| 79 | | |
| 80 | 1× | pub fn at_op(mut self, spec: &str) -> Result<Repo> { |
| 81 | 1× | let (r, _) = self.resolve_op(spec)?; |
| 82 | 1× | self.at_op = Some(r); |
| 83 | 1× | Ok(self) |
| 84 | 1× | } |
| 85 | |
|
| 86 | | |
| 87 | 8× | pub fn is_at_op(&self) -> bool { |
| 88 | 8× | self.at_op.is_some() |
| 89 | 8× | } |
| 90 | |
|
| 91 | | |
| 92 | | |
| 93 | 6× | pub fn resolve_op(&self, spec: &str) -> Result<(ObjectRef, Op)> { |
| 94 | 6× | let spec = spec.trim(); |
| 95 | 6× | let log = self.op_log()?; |
| 96 | 6× | if let Some(rest) = spec.strip_prefix('@') { |
| 97 | 1× | if rest.chars().all(|c| c == '-') { |
| 98 | 1× | return log |
| 99 | 1× | .into_iter() |
| 100 | 1× | .nth(rest.len()) |
| 101 | 1× | .ok_or_else(|| RepoError::Msg(format!("the operation log has no {spec}"))); |
| 102 | 0× | } |
| 103 | 5× | } |
| 104 | 203× | if spec.is_empty() || !spec.chars().all(|c| c.is_ascii_hexdigit()) { |
| 105 | 1× | return fail(format!( |
| 106 | | "{spec:?} is not an operation: give an ID from oxy op log, @ or @-" |
| 107 | | )); |
| 108 | 4× | } |
| 109 | 4× | let spec = spec.to_ascii_lowercase(); |
| 110 | 4× | let mut found = log |
| 111 | 4× | .into_iter() |
| 112 | 25× | .filter(|(r, _)| r.to_hex().starts_with(&spec)); |
| 113 | 4× | match (found.next(), found.next()) { |
| 114 | 4× | (Some(one), None) => Ok(one), |
| 115 | 0× | (None, _) => fail(format!("no operation {spec} in oxy op log")), |
| 116 | 0× | (Some(_), Some(_)) => fail(format!( |
| 117 | | "operation {spec} is ambiguous: give more of its ID" |
| 118 | | )), |
| 119 | | } |
| 120 | 6× | } |
| 121 | |
|
| 122 | | |
| 123 | | |
| 124 | 26× | pub fn view_changes(&self, before: &View, after: &View) -> Result<Vec<ViewChange>> { |
| 125 | 26× | let mut out = Vec::new(); |
| 126 | 34× | let heads = |st: &ObjectRef| -> Result<usize> { Ok(self.store.get_state(st)?.heads.len()) }; |
| 127 | 26× | let names: BTreeSet<&String> = before |
| 128 | 26× | .bookmarks |
| 129 | 26× | .keys() |
| 130 | 26× | .chain(after.bookmarks.keys()) |
| 131 | 26× | .collect(); |
| 132 | 31× | for name in names { |
| 133 | 31× | match (before.bookmarks.get(name), after.bookmarks.get(name)) { |
| 134 | 0× | (None, Some(a)) => out.push(ViewChange::BookmarkCreated { |
| 135 | 0× | name: name.clone(), |
| 136 | 0× | heads: heads(a)?, |
| 137 | | }), |
| 138 | 1× | (Some(_), None) => out.push(ViewChange::BookmarkDeleted { name: name.clone() }), |
| 139 | 30× | (Some(b), Some(a)) if a != b => out.push(ViewChange::BookmarkMoved { |
| 140 | 17× | name: name.clone(), |
| 141 | 17× | from: heads(b)?, |
| 142 | 17× | to: heads(a)?, |
| 143 | | }), |
| 144 | 13× | _ => {} |
| 145 | | } |
| 146 | | } |
| 147 | 26× | for (remote, bookmarks) in &after.remotes { |
| 148 | 11× | for (name, st) in bookmarks { |
| 149 | 11× | if before.remotes.get(remote).and_then(|b| b.get(name)) != Some(st) { |
| 150 | 1× | out.push(ViewChange::RemoteMoved { |
| 151 | 1× | remote: remote.clone(), |
| 152 | 1× | name: name.clone(), |
| 153 | 1× | }); |
| 154 | 10× | } |
| 155 | | } |
| 156 | | } |
| 157 | 26× | for name in after.tags.keys().filter(|t| !before.tags.contains_key(*t)) { |
| 158 | 0× | out.push(ViewChange::TagCreated { name: name.clone() }); |
| 159 | 0× | } |
| 160 | 26× | for name in before.tags.keys().filter(|t| !after.tags.contains_key(*t)) { |
| 161 | 0× | out.push(ViewChange::TagDeleted { name: name.clone() }); |
| 162 | 0× | } |
| 163 | 26× | if before.policy != after.policy { |
| 164 | 0× | out.push(ViewChange::Policy); |
| 165 | 26× | } |
| 166 | 26× | if let Some(w) = after.workspaces.get(&self.workspace) { |
| 167 | 26× | if before.workspaces.get(&self.workspace) != Some(w) { |
| 168 | 23× | out.push(ViewChange::WorkingCopy { change: *w }); |
| 169 | 23× | } |
| 170 | 0× | } |
| 171 | 26× | Ok(out) |
| 172 | 26× | } |
| 173 | |
|
| 174 | | |
| 175 | | |
| 176 | | |
| 177 | | |
| 178 | | |
| 179 | 1× | pub fn op_restore(&self, spec: &str, discard: bool) -> Result<RestoreReport> { |
| 180 | 1× | let _wc = self.lock_working_copy()?; |
| 181 | 1× | let (target_ref, target) = self.resolve_op(spec)?; |
| 182 | 1× | let command = format!("oxy op restore {}", target_ref.short(12)); |
| 183 | | |
| 184 | 1× | if !discard && !self.clean_by_manifest()? { |
| 185 | 0× | let snap = self.snapshot_status(&command)?; |
| 186 | 0× | if !snap.statuses.is_empty() { |
| 187 | 0× | return fail(format!( |
| 188 | | "the working copy has uncommitted changes ({}): commit them, or drop them with --discard", |
| 189 | 0× | plural_paths(snap.statuses.len()) |
| 190 | | )); |
| 191 | 0× | } |
| 192 | 1× | } |
| 193 | 1× | let (head_ref, head) = self.head_op()?; |
| 194 | 1× | let mut view = head.view.clone(); |
| 195 | 1× | view.bookmarks = target.view.bookmarks.clone(); |
| 196 | 1× | self.record_op(&[head_ref], &command, view)?; |
| 197 | | |
| 198 | | |
| 199 | 1× | let base = match target |
| 200 | 1× | .view |
| 201 | 1× | .workspaces |
| 202 | 1× | .get(&self.workspace) |
| 203 | 1× | .map(|w| self.store.get_change(w)) |
| 204 | | { |
| 205 | 1× | Some(Ok(wc)) => self.load_state_history(&wc.deps)?.maximal(&wc.deps), |
| 206 | | Some(Err(StoreError::NotFound(_))) | None => { |
| 207 | 0× | match target.view.bookmarks.get(&self.config.bookmark) { |
| 208 | 0× | Some(st) => self.store.get_state(st)?.heads, |
| 209 | 0× | None => self.bookmark_heads()?, |
| 210 | | } |
| 211 | | } |
| 212 | 0× | Some(Err(e)) => return Err(e.into()), |
| 213 | | }; |
| 214 | 1× | let checkout = self.checkout(&base, discard, &format!("{command} (checkout)"))?; |
| 215 | 1× | Ok(RestoreReport { |
| 216 | 1× | op: target_ref, |
| 217 | 1× | command: target.command, |
| 218 | 1× | checkout, |
| 219 | 1× | }) |
| 220 | 1× | } |
| 221 | |
|
| 222 | | |
| 223 | | |
| 224 | | |
| 225 | | |
| 226 | 2× | pub fn op_revert(&self, spec: &str) -> Result<RevertReport> { |
| 227 | 2× | let _wc = self.lock_working_copy()?; |
| 228 | 2× | let (target_ref, target) = self.resolve_op(spec)?; |
| 229 | 2× | if target.is_snapshot() { |
| 230 | 0× | return fail( |
| 231 | | "that operation only recorded the working copy: it moved no bookmark to revert", |
| 232 | | ); |
| 233 | 2× | } |
| 234 | 2× | let Some(parent) = target.parents.first() else { |
| 235 | 0× | return fail("that is the first operation: there is nothing before it"); |
| 236 | | }; |
| 237 | 2× | let before = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?.view; |
| 238 | 2× | let (head_ref, head) = self.head_op()?; |
| 239 | 2× | let mut view = head.view.clone(); |
| 240 | 2× | let (mut reverted, mut kept) = (Vec::new(), Vec::new()); |
| 241 | 2× | let names: BTreeSet<&String> = before |
| 242 | 2× | .bookmarks |
| 243 | 2× | .keys() |
| 244 | 2× | .chain(target.view.bookmarks.keys()) |
| 245 | 2× | .collect(); |
| 246 | 4× | for name in names { |
| 247 | 4× | let (was, became) = (before.bookmarks.get(name), target.view.bookmarks.get(name)); |
| 248 | 4× | if was == became { |
| 249 | 2× | continue; |
| 250 | 2× | } |
| 251 | 2× | if head.view.bookmarks.get(name) != became { |
| 252 | 1× | kept.push(name.clone()); |
| 253 | 1× | continue; |
| 254 | 1× | } |
| 255 | 1× | match was { |
| 256 | 0× | Some(st) => view.bookmarks.insert(name.clone(), *st), |
| 257 | 1× | None => view.bookmarks.remove(name), |
| 258 | | }; |
| 259 | 1× | reverted.push(name.clone()); |
| 260 | | } |
| 261 | 2× | if reverted.is_empty() { |
| 262 | 1× | return fail(if kept.is_empty() { |
| 263 | 0× | "that operation moved no bookmark: nothing to revert".to_string() |
| 264 | | } else { |
| 265 | 1× | format!( |
| 266 | | "every bookmark that operation moved has moved again since ({}); oxy op restore goes back to an operation instead", |
| 267 | 1× | kept.join(", ") |
| 268 | | ) |
| 269 | | }); |
| 270 | 1× | } |
| 271 | 1× | let command = format!("oxy op revert {}", target_ref.short(12)); |
| 272 | 1× | let current = self.config.bookmark.clone(); |
| 273 | 1× | let mut old = self.heads_of(&head.view, ¤t)?; |
| 274 | 1× | old.sort(); |
| 275 | 1× | self.record_op(&[head_ref], &command, view.clone())?; |
| 276 | 1× | let mut checked_out = false; |
| 277 | 1× | if reverted.contains(¤t) { |
| 278 | 0× | let snap = self.snapshot(&command)?; |
| 279 | 0× | let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty(); |
| 280 | 0× | let mut base = snap.base.maximal(&snap.wc.deps); |
| 281 | 0× | base.sort(); |
| 282 | 0× | if clean && base == old { |
| 283 | 0× | let new = self.heads_of(&view, ¤t)?; |
| 284 | 0× | self.checkout(&new, false, &format!("{command} (checkout)"))?; |
| 285 | 0× | checked_out = true; |
| 286 | 0× | } |
| 287 | 1× | } |
| 288 | 1× | Ok(RevertReport { |
| 289 | 1× | op: target_ref, |
| 290 | 1× | command: target.command, |
| 291 | 1× | reverted, |
| 292 | 1× | kept, |
| 293 | 1× | checked_out, |
| 294 | 1× | }) |
| 295 | 2× | } |
| 296 | | } |
| 297 | |
|
| 298 | 0× | fn plural_paths(n: usize) -> String { |
| 299 | 0× | format!("{n} path{}", if n == 1 { "" } else { "s" }) |
| 300 | 0× | } |