| 1 | |
|
| 2 | |
|
| 3 | | use super::*; |
| 4 | | use crate::objects::AtomRef; |
| 5 | |
|
| 6 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 7 | | pub enum ChangeStatus { |
| 8 | | Added, |
| 9 | | Modified, |
| 10 | | Removed, |
| 11 | | |
| 12 | | Renamed, |
| 13 | | ModeChanged, |
| 14 | | |
| 15 | | ConflictEdited, |
| 16 | | |
| 17 | | Resolved, |
| 18 | | } |
| 19 | |
|
| 20 | |
|
| 21 | 28× | pub fn has_conflict_markers(content: &[u8]) -> bool { |
| 22 | 78× | split_lines(content).iter().any(|l| { |
| 23 | 78× | l.starts_with(b"<<<<<<< oxy conflict") |
| 24 | 73× | || l.starts_with(b"======= oxy conflict") |
| 25 | 73× | || l.starts_with(b">>>>>>> oxy conflict") |
| 26 | 78× | }) |
| 27 | 28× | } |
| 28 | |
|
| 29 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 30 | | pub struct PathStatus { |
| 31 | | pub path: String, |
| 32 | | pub status: ChangeStatus, |
| 33 | | } |
| 34 | |
|
| 35 | |
|
| 36 | | pub(crate) struct BuiltChange { |
| 37 | | pub tree_ops: Vec<TreeOp>, |
| 38 | | pub edits: Vec<Edit>, |
| 39 | | pub contributors: Vec<ChangeHash>, |
| 40 | | pub resolves: bool, |
| 41 | | pub statuses: Vec<PathStatus>, |
| 42 | | |
| 43 | | pub blobs: HashMap<String, ObjectRef>, |
| 44 | | } |
| 45 | |
|
| 46 | |
|
| 47 | | pub struct SnapshotResult { |
| 48 | | |
| 49 | | |
| 50 | | pub wc_hash: ChangeHash, |
| 51 | | pub wc: Change, |
| 52 | | |
| 53 | | pub base: State, |
| 54 | | pub base_snapshot: Snapshot, |
| 55 | | pub statuses: Vec<PathStatus>, |
| 56 | | |
| 57 | | pub changed: bool, |
| 58 | | |
| 59 | | pub resolves: bool, |
| 60 | | |
| 61 | | pub contributors: Vec<ChangeHash>, |
| 62 | | |
| 63 | | pub(crate) attributes: crate::attributes::Attributes, |
| 64 | | |
| 65 | | |
| 66 | | pub untracked_nested: Vec<String>, |
| 67 | | |
| 68 | | |
| 69 | | pub(crate) manifest: Option<PendingManifest>, |
| 70 | | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | | pub(crate) struct PendingManifest { |
| 75 | | entries: BTreeMap<String, FsEntry>, |
| 76 | | blobs: HashMap<String, ObjectRef>, |
| 77 | | dirs: Option<super::WalkedDirs>, |
| 78 | | written: u128, |
| 79 | | } |
| 80 | |
|
| 81 | 22769× | pub fn is_binary(content: &[u8]) -> bool { |
| 82 | 22769× | content[..content.len().min(BINARY_SNIFF)].contains(&0) |
| 83 | 22769× | } |
| 84 | |
|
| 85 | | impl Repo { |
| 86 | | |
| 87 | | |
| 88 | | |
| 89 | 2× | pub(crate) fn build_against( |
| 90 | 2× | &self, |
| 91 | 2× | base: &State, |
| 92 | 2× | base_snapshot: &Snapshot, |
| 93 | 2× | reuse: HashMap<String, (NodeId, NodeKind)>, |
| 94 | 2× | ) -> Result<BuiltChange> { |
| 95 | 2× | let mut fs_entries = |
| 96 | 2× | self.walk_working_copy_with(None, &Self::tracked_kinds(base_snapshot), None)?; |
| 97 | 2× | self.platform_normalize(&mut fs_entries, base_snapshot); |
| 98 | 2× | self.build_from_entries(base, base_snapshot, reuse, &fs_entries) |
| 99 | 2× | } |
| 100 | |
|
| 101 | | |
| 102 | | |
| 103 | | |
| 104 | | |
| 105 | 1276× | fn platform_normalize(&self, entries: &mut BTreeMap<String, FsEntry>, base: &Snapshot) { |
| 106 | 1276× | let p = self.platform; |
| 107 | 1276× | if p.exec_bits && p.symlinks { |
| 108 | 1274× | return; |
| 109 | 2× | } |
| 110 | 4× | for (path, e) in entries.iter_mut() { |
| 111 | 4× | let Some(f) = base.files.get(path) else { |
| 112 | 0× | continue; |
| 113 | | }; |
| 114 | 4× | if !p.symlinks && f.kind == NodeKind::Symlink && e.kind == NodeKind::File { |
| 115 | 2× | e.kind = NodeKind::Symlink; |
| 116 | 2× | e.exec = false; |
| 117 | 2× | } |
| 118 | 4× | if !p.exec_bits && f.kind == NodeKind::File && e.kind == NodeKind::File { |
| 119 | 2× | e.exec = f.mode.exec; |
| 120 | 2× | } |
| 121 | | } |
| 122 | 1276× | } |
| 123 | |
|
| 124 | | |
| 125 | 1008× | pub(crate) fn build_from_entries( |
| 126 | 1008× | &self, |
| 127 | 1008× | base: &State, |
| 128 | 1008× | base_snapshot: &Snapshot, |
| 129 | 1008× | reuse: HashMap<String, (NodeId, NodeKind)>, |
| 130 | 1008× | fs_entries: &BTreeMap<String, FsEntry>, |
| 131 | 1008× | ) -> Result<BuiltChange> { |
| 132 | 1008× | self.build_from_entries_resolving(base, base_snapshot, reuse, fs_entries, &HashSet::new()) |
| 133 | 1008× | } |
| 134 | |
|
| 135 | | |
| 136 | | |
| 137 | | |
| 138 | | |
| 139 | | |
| 140 | 1023× | pub(crate) fn build_from_entries_resolving( |
| 141 | 1023× | &self, |
| 142 | 1023× | base: &State, |
| 143 | 1023× | base_snapshot: &Snapshot, |
| 144 | 1023× | reuse: HashMap<String, (NodeId, NodeKind)>, |
| 145 | 1023× | fs_entries: &BTreeMap<String, FsEntry>, |
| 146 | 1023× | resolve: &HashSet<String>, |
| 147 | 1023× | ) -> Result<BuiltChange> { |
| 148 | 1023× | self.build_from_entries_with( |
| 149 | 1023× | base, |
| 150 | 1023× | base_snapshot, |
| 151 | 1023× | reuse, |
| 152 | 1023× | fs_entries, |
| 153 | 1023× | resolve, |
| 154 | 1023× | Scope::default(), |
| 155 | | ) |
| 156 | 1023× | } |
| 157 | |
|
| 158 | | |
| 159 | | |
| 160 | | |
| 161 | | |
| 162 | | |
| 163 | | |
| 164 | | |
| 165 | | |
| 166 | 88× | pub(crate) fn build_from_entries_seeded( |
| 167 | 88× | &self, |
| 168 | 88× | base: &State, |
| 169 | 88× | base_snapshot: &Snapshot, |
| 170 | 88× | fs_entries: &BTreeMap<String, FsEntry>, |
| 171 | 88× | seed: [u8; 32], |
| 172 | 88× | dir_needed: Option<&dyn Fn(&str) -> bool>, |
| 173 | 88× | ) -> Result<BuiltChange> { |
| 174 | 88× | self.build_from_entries_with( |
| 175 | 88× | base, |
| 176 | 88× | base_snapshot, |
| 177 | 88× | HashMap::new(), |
| 178 | 88× | fs_entries, |
| 179 | 88× | &HashSet::new(), |
| 180 | 88× | Scope { |
| 181 | 88× | seed: Some(seed), |
| 182 | 88× | dir_needed, |
| 183 | 88× | }, |
| 184 | | ) |
| 185 | 88× | } |
| 186 | |
|
| 187 | | |
| 188 | | |
| 189 | | |
| 190 | | |
| 191 | | |
| 192 | 842× | fn build_changed_only( |
| 193 | 842× | &self, |
| 194 | 842× | base: &State, |
| 195 | 842× | base_snapshot: &Snapshot, |
| 196 | 842× | reuse: HashMap<String, (NodeId, NodeKind)>, |
| 197 | 842× | fs_entries: &BTreeMap<String, FsEntry>, |
| 198 | 842× | ) -> Result<BuiltChange> { |
| 199 | 842× | let pick = crate::timing::phase("snapshot: of which picking the changed"); |
| 200 | | |
| 201 | | |
| 202 | 842× | let changed: BTreeMap<String, FsEntry> = join(fs_entries, &base_snapshot.files) |
| 203 | 10999× | .filter_map(|(p, e, f)| { |
| 204 | 10999× | let e = e?; |
| 205 | 10870× | (!e.unchanged || f.is_none_or(|f| f.kind != e.kind || f.mode.exec != e.exec)) |
| 206 | 10870× | .then(|| (p.clone(), e.clone())) |
| 207 | 10999× | }) |
| 208 | 842× | .collect(); |
| 209 | 842× | let mut files: BTreeMap<String, crate::state::FileState> = BTreeMap::new(); |
| 210 | 6639× | for p in changed.keys() { |
| 211 | 6639× | if let Some(f) = base_snapshot.files.get(p) { |
| 212 | 3650× | files.insert(p.clone(), f.clone()); |
| 213 | 3650× | } |
| 214 | | } |
| 215 | | |
| 216 | 842× | let gone = join(&base_snapshot.files, fs_entries) |
| 217 | 10999× | .filter_map(|(p, f, e)| (e.is_none()).then_some((p, f?))); |
| 218 | 842× | for (p, f) in gone { |
| 219 | 129× | if f.kind == NodeKind::Dir { |
| 220 | 96× | continue; |
| 221 | 33× | } |
| 222 | 33× | files.insert(p.clone(), f.clone()); |
| 223 | 33× | let mut end = p.len(); |
| 224 | 38× | while let Some(i) = p[..end].rfind('/') { |
| 225 | 5× | let dir = &p[..i]; |
| 226 | 5× | if files.contains_key(dir) { |
| 227 | 0× | break; |
| 228 | 5× | } |
| 229 | 5× | if let Some(d) = base_snapshot.files.get(dir) { |
| 230 | 5× | files.insert(dir.to_string(), d.clone()); |
| 231 | 5× | } |
| 232 | 5× | end = i; |
| 233 | | } |
| 234 | | } |
| 235 | 842× | let part = Snapshot { |
| 236 | 842× | tree: base_snapshot.tree.clone(), |
| 237 | 842× | files, |
| 238 | 842× | unrendered: BTreeSet::new(), |
| 239 | 842× | }; |
| 240 | 842× | drop(pick); |
| 241 | 842× | let _build = crate::timing::phase("snapshot: of which building the change"); |
| 242 | | |
| 243 | 842× | let dir_needed = |dir: &str| { |
| 244 | 5× | let prefix = format!("{dir}/"); |
| 245 | 5× | fs_entries |
| 246 | 5× | .range(prefix.clone()..) |
| 247 | 5× | .next() |
| 248 | 5× | .is_some_and(|(k, _)| k.starts_with(&prefix)) |
| 249 | 5× | }; |
| 250 | 842× | self.build_from_entries_with( |
| 251 | 842× | base, |
| 252 | 842× | &part, |
| 253 | 842× | reuse, |
| 254 | 842× | &changed, |
| 255 | 842× | &HashSet::new(), |
| 256 | 842× | Scope { |
| 257 | 842× | seed: None, |
| 258 | 842× | dir_needed: Some(&dir_needed), |
| 259 | 842× | }, |
| 260 | | ) |
| 261 | 842× | } |
| 262 | |
|
| 263 | 1953× | fn build_from_entries_with( |
| 264 | 1953× | &self, |
| 265 | 1953× | base: &State, |
| 266 | 1953× | base_snapshot: &Snapshot, |
| 267 | 1953× | reuse: HashMap<String, (NodeId, NodeKind)>, |
| 268 | 1953× | fs_entries: &BTreeMap<String, FsEntry>, |
| 269 | 1953× | resolve: &HashSet<String>, |
| 270 | 1953× | scope: Scope, |
| 271 | 1953× | ) -> Result<BuiltChange> { |
| 272 | 1953× | let Scope { seed, dir_needed } = scope; |
| 273 | 1953× | let mut laps = crate::timing::Laps::start(); |
| 274 | | |
| 275 | | |
| 276 | 1953× | let attrs = crate::attributes::Attributes::from_files( |
| 277 | 1953× | fs_entries |
| 278 | 1953× | .iter() |
| 279 | 18695× | .filter(|(p, _)| p.as_str() == ".oxyattributes" || p.ends_with("/.oxyattributes")) |
| 280 | 1953× | .map(|(p, e)| (p.as_str(), e.content.as_slice())), |
| 281 | | ); |
| 282 | 1953× | let mut b = ChangeBuilder::new(base, base_snapshot, reuse).with_attributes(attrs); |
| 283 | 1953× | b.resolve = resolve.clone(); |
| 284 | 1953× | b.node_seed = seed; |
| 285 | 1953× | laps.lap("build: builder"); |
| 286 | | |
| 287 | | |
| 288 | | |
| 289 | 1953× | let renames = detect_renames(base_snapshot, fs_entries, |p| self.in_scope(p)); |
| 290 | 1953× | let renamed_from: HashMap<&str, &str> = renames |
| 291 | 1953× | .iter() |
| 292 | 1953× | .map(|(old, new)| (new.as_str(), old.as_str())) |
| 293 | 1953× | .collect(); |
| 294 | 1953× | let renamed_to: HashMap<&str, &str> = renames |
| 295 | 1953× | .iter() |
| 296 | 1953× | .map(|(old, new)| (old.as_str(), new.as_str())) |
| 297 | 1953× | .collect(); |
| 298 | 1953× | laps.lap("build: renames"); |
| 299 | 1953× | b.diff_in_parallel(fs_entries); |
| 300 | 1953× | laps.lap("build: diffs"); |
| 301 | 18695× | for (path, fs_entry) in fs_entries { |
| 302 | 18695× | match base_snapshot.files.get(path) { |
| 303 | 6031× | None => match renamed_from.get(path.as_str()) { |
| 304 | 32× | Some(old) => { |
| 305 | 32× | b.move_path(self, old, path, fs_entry, &base_snapshot.files[*old])? |
| 306 | | } |
| 307 | 5999× | None => b.add_path(self, path, fs_entry)?, |
| 308 | | }, |
| 309 | 12664× | Some(expected) => b.update_path(self, path, fs_entry, expected)?, |
| 310 | | } |
| 311 | | } |
| 312 | 1953× | laps.lap("build: paths"); |
| 313 | | |
| 314 | | |
| 315 | 1953× | let mut needed_dirs: HashSet<&str> = HashSet::new(); |
| 316 | 18708× | fn parents_of<'a>(p: &'a str, set: &mut HashSet<&'a str>) { |
| 317 | 18708× | let mut end = p.len(); |
| 318 | 19357× | while let Some(i) = p[..end].rfind('/') { |
| 319 | 15431× | if !set.insert(&p[..i]) { |
| 320 | 14782× | break; |
| 321 | 649× | } |
| 322 | 649× | end = i; |
| 323 | | } |
| 324 | 18708× | } |
| 325 | 18695× | for p in fs_entries.keys() { |
| 326 | 18695× | parents_of(p, &mut needed_dirs); |
| 327 | 18695× | } |
| 328 | 13223× | for p in base_snapshot.files.keys() { |
| 329 | 13223× | if !self.in_scope(p) { |
| 330 | 13× | parents_of(p, &mut needed_dirs); |
| 331 | 13210× | } |
| 332 | | } |
| 333 | 13223× | for (path, expected) in &base_snapshot.files { |
| 334 | 13223× | if fs_entries.contains_key(path) |
| 335 | 559× | || !self.in_scope(path) |
| 336 | 546× | || renamed_to.contains_key(path.as_str()) |
| 337 | | { |
| 338 | 12709× | continue; |
| 339 | 514× | } |
| 340 | 514× | let still_needed = expected.kind == NodeKind::Dir |
| 341 | 410× | && match dir_needed { |
| 342 | 21× | Some(f) => f(path), |
| 343 | 389× | None => needed_dirs.contains(path.as_str()), |
| 344 | | }; |
| 345 | 514× | if !still_needed { |
| 346 | 131× | b.remove_path(path, expected); |
| 347 | 383× | } |
| 348 | | } |
| 349 | 1953× | laps.lap("build: removals"); |
| 350 | 1953× | if seed.is_some() { |
| 351 | 88× | b.retire_stray_placements(); |
| 352 | 1865× | } |
| 353 | 1953× | laps.lap("build: stray placements"); |
| 354 | 1953× | let mut statuses = b.statuses; |
| 355 | 5655× | statuses.sort_by(|a, b| a.path.cmp(&b.path)); |
| 356 | 1953× | Ok(BuiltChange { |
| 357 | 1953× | tree_ops: b.tree_ops, |
| 358 | 1953× | edits: b.edits, |
| 359 | 1953× | contributors: b.contributors.into_iter().collect(), |
| 360 | 1953× | resolves: b.resolves, |
| 361 | 1953× | statuses, |
| 362 | 1953× | blobs: b.blobs, |
| 363 | 1953× | }) |
| 364 | 1953× | } |
| 365 | |
|
| 366 | | |
| 367 | | |
| 368 | | |
| 369 | 681× | pub(crate) fn store_signed(&self, change: &Change) -> Result<ChangeHash> { |
| 370 | 681× | let ours = change.author == self.author(); |
| 371 | 681× | if self.config.sign_changes && ours && self.secret_key()?.is_none() { |
| 372 | 1× | return fail("core.sign is \"change\" but no secret key is available for author.key"); |
| 373 | 680× | } |
| 374 | 680× | let storing = crate::timing::phase("store: the change"); |
| 375 | 680× | let hash = self.store.put_change(change)?; |
| 376 | 680× | drop(storing); |
| 377 | 680× | if self.config.sign_changes && ours { |
| 378 | 41× | let _t = crate::timing::phase("store: sign it"); |
| 379 | 41× | self.sign_change(&hash)?; |
| 380 | 639× | } |
| 381 | 680× | Ok(hash) |
| 382 | 681× | } |
| 383 | |
|
| 384 | | |
| 385 | | |
| 386 | 432× | pub fn snapshot(&self, command: &str) -> Result<SnapshotResult> { |
| 387 | 432× | self.snapshot_as(command, false, false) |
| 388 | 432× | } |
| 389 | |
|
| 390 | | |
| 391 | | |
| 392 | | |
| 393 | | |
| 394 | | |
| 395 | | |
| 396 | | |
| 397 | | |
| 398 | 403× | pub fn snapshot_status(&self, command: &str) -> Result<SnapshotResult> { |
| 399 | 403× | self.snapshot_as(command, true, false) |
| 400 | 403× | } |
| 401 | |
|
| 402 | | |
| 403 | | |
| 404 | | |
| 405 | | |
| 406 | | |
| 407 | | |
| 408 | | |
| 409 | 1277× | fn snapshot_as( |
| 410 | 1277× | &self, |
| 411 | 1277× | command: &str, |
| 412 | 1277× | partial: bool, |
| 413 | 1277× | for_commit: bool, |
| 414 | 1277× | ) -> Result<SnapshotResult> { |
| 415 | 1277× | let _lock = self.lock_snapshot()?; |
| 416 | 1277× | let batch = self.store.begin_batch()?; |
| 417 | 1277× | let result = self.snapshot_locked(command, partial, for_commit); |
| 418 | 1277× | batch.finish()?; |
| 419 | 1277× | result |
| 420 | 1277× | } |
| 421 | |
|
| 422 | 1277× | fn snapshot_locked( |
| 423 | 1277× | &self, |
| 424 | 1277× | command: &str, |
| 425 | 1277× | partial: bool, |
| 426 | 1277× | for_commit: bool, |
| 427 | 1277× | ) -> Result<SnapshotResult> { |
| 428 | 1277× | let _all = crate::timing::phase("snapshot"); |
| 429 | 1277× | let (op_ref, op) = self.head_op()?; |
| 430 | 1277× | let (old_hash, old_wc) = self.working_copy()?; |
| 431 | | |
| 432 | | |
| 433 | 1275× | let mut base = { |
| 434 | 1275× | let _t = crate::timing::phase("snapshot: load base history"); |
| 435 | 1275× | self.load_state_indexed(&old_wc.deps)? |
| 436 | | }; |
| 437 | 1275× | let view = { |
| 438 | 1275× | let _t = crate::timing::phase("snapshot: tree view"); |
| 439 | 1275× | base.tree().view() |
| 440 | | }; |
| 441 | 1275× | let manifest = { |
| 442 | 1275× | let _t = crate::timing::phase("snapshot: read manifest"); |
| 443 | 1275× | self.read_manifest()?.map(|(_, _, m)| m) |
| 444 | | }; |
| 445 | 1275× | let tracked: BTreeMap<String, (NodeKind, Option<ObjectRef>)> = { |
| 446 | 1275× | let _t = crate::timing::phase("snapshot: tracked paths"); |
| 447 | | |
| 448 | 1275× | view.entries |
| 449 | 1275× | .iter() |
| 450 | 9470× | .filter(|(_, e)| e.kind != NodeKind::Dir) |
| 451 | 9086× | .map(|(p, e)| { |
| 452 | 9086× | let blob = if e.kind != NodeKind::File || e.mode.opaque { |
| 453 | 151× | base.live_blobs(&e.node).first().copied() |
| 454 | | } else { |
| 455 | 8935× | None |
| 456 | | }; |
| 457 | 9086× | (p.clone(), (e.kind, blob)) |
| 458 | 9086× | }) |
| 459 | 1275× | .collect() |
| 460 | | }; |
| 461 | | |
| 462 | 12088× | let key_of = |p: &str| -> Option<[u8; 32]> { |
| 463 | 12088× | let e = view.entries.get(p)?; |
| 464 | 8983× | if e.kind != NodeKind::File || e.mode.opaque { |
| 465 | 124× | return None; |
| 466 | 8859× | } |
| 467 | 8859× | base.graph_key(&e.node) |
| 468 | 12088× | }; |
| 469 | 1274× | let mut fs_entries = { |
| 470 | 1275× | let _t = crate::timing::phase("snapshot: read working copy"); |
| 471 | 1275× | self.walk_working_copy_with(manifest.as_deref(), &tracked, Some(&key_of))? |
| 472 | | }; |
| 473 | | |
| 474 | | |
| 475 | 1274× | let keys: HashMap<String, [u8; 32]> = fs_entries |
| 476 | 1274× | .iter() |
| 477 | 12118× | .filter(|(_, e)| !e.unchanged && e.kind == NodeKind::File) |
| 478 | 7845× | .filter_map(|(p, _)| Some((p.clone(), key_of(p)?))) |
| 479 | 1274× | .collect(); |
| 480 | 1274× | let base_snapshot = { |
| 481 | 1274× | let _t = crate::timing::phase("snapshot: load and render base"); |
| 482 | 1274× | if partial { |
| 483 | 845× | let sets = crate::timing::phase("snapshot: of which choosing graphs"); |
| 484 | | |
| 485 | 845× | let need: Vec<(&str, NodeId)> = join(&view.entries, &fs_entries) |
| 486 | 11014× | .filter_map(|(p, v, f)| { |
| 487 | 11014× | let v = v?; |
| 488 | 8024× | (v.kind == NodeKind::File && !f.is_some_and(|f| f.unchanged)) |
| 489 | 8024× | .then_some((p.as_str(), v.node)) |
| 490 | 11014× | }) |
| 491 | 845× | .collect(); |
| 492 | 845× | let render: std::collections::HashSet<&str> = |
| 493 | 845× | need.iter().map(|(p, _)| *p).collect(); |
| 494 | 845× | let need: Vec<NodeId> = need.iter().map(|(_, n)| *n).collect(); |
| 495 | 845× | drop(sets); |
| 496 | | { |
| 497 | 845× | let _t = crate::timing::phase("snapshot: of which building graphs"); |
| 498 | 845× | self.load_graphs_of(&mut base, &need)?; |
| 499 | | } |
| 500 | 845× | let _t = crate::timing::phase("snapshot: of which rendering"); |
| 501 | 845× | base.materialize_only(&render) |
| 502 | | } else { |
| 503 | 429× | self.load_all_graphs(&mut base)?; |
| 504 | 429× | base.materialize() |
| 505 | | } |
| 506 | | }; |
| 507 | |
|
| 508 | | |
| 509 | | |
| 510 | 1274× | let reuse = added_node_paths(&old_wc, &base_snapshot); |
| 511 | 1274× | self.platform_normalize(&mut fs_entries, &base_snapshot); |
| 512 | 1274× | let built = { |
| 513 | 1274× | let _t = crate::timing::phase("snapshot: diff against base"); |
| 514 | 1274× | if partial && self.config.sparse_paths.is_empty() { |
| 515 | | #[cfg(test)] |
| 516 | 570× | let full = |
| 517 | 570× | self.build_from_entries(&base, &base_snapshot, reuse.clone(), &fs_entries)?; |
| 518 | 842× | let built = self.build_changed_only(&base, &base_snapshot, reuse, &fs_entries)?; |
| 519 | | #[cfg(test)] |
| 520 | | { |
| 521 | | |
| 522 | | |
| 523 | 1140× | let norm = |b: &BuiltChange| { |
| 524 | 1140× | let text = format!("{:?} {:?} {:?}", b.tree_ops, b.edits, b.statuses); |
| 525 | 1140× | let mut seen: Vec<String> = Vec::new(); |
| 526 | 1140× | let mut out = String::new(); |
| 527 | 1140× | let mut rest = text.as_str(); |
| 528 | 19764× | while let Some(i) = rest.find("NodeId(") { |
| 529 | 18624× | out.push_str(&rest[..i]); |
| 530 | 18624× | let end = i + rest[i..].find(')').unwrap() + 1; |
| 531 | 18624× | let id = rest[i..end].to_string(); |
| 532 | 9045888× | let n = seen.iter().position(|x| *x == id).unwrap_or_else(|| { |
| 533 | 6982× | seen.push(id); |
| 534 | 6982× | seen.len() - 1 |
| 535 | 6982× | }); |
| 536 | 18624× | out.push_str(&format!("N{n}")); |
| 537 | 18624× | rest = &rest[end..]; |
| 538 | | } |
| 539 | 1140× | out.push_str(rest); |
| 540 | 1140× | out |
| 541 | 1140× | }; |
| 542 | 570× | assert_eq!( |
| 543 | 570× | norm(&built), |
| 544 | 570× | norm(&full), |
| 545 | | "a changed-only build is the full one" |
| 546 | | ); |
| 547 | | } |
| 548 | 842× | built |
| 549 | | } else { |
| 550 | 432× | self.build_from_entries(&base, &base_snapshot, reuse, &fs_entries)? |
| 551 | | } |
| 552 | | }; |
| 553 | | |
| 554 | | |
| 555 | | { |
| 556 | 3729× | let changed: HashSet<&str> = built.statuses.iter().map(|s| s.path.as_str()).collect(); |
| 557 | 12118× | for (p, e) in fs_entries.iter_mut() { |
| 558 | 12118× | if e.unchanged || e.kind != NodeKind::File || changed.contains(p.as_str()) { |
| 559 | 7903× | continue; |
| 560 | 4215× | } |
| 561 | 4215× | e.graph_key = match base_snapshot.files.get(p).map(|f| &f.content) { |
| 562 | | Some(FileContent::Text { |
| 563 | 4103× | bytes, |
| 564 | 4103× | materialized, |
| 565 | 4103× | }) if !materialized.is_conflicted() && *bytes == *e.content => { |
| 566 | 4097× | keys.get(p).copied() |
| 567 | | } |
| 568 | 118× | _ => None, |
| 569 | | }; |
| 570 | | } |
| 571 | | } |
| 572 | |
|
| 573 | 1274× | let mut wc = Change { |
| 574 | 1274× | version: crate::objects::CHANGE_VERSION, |
| 575 | 1274× | change_id: old_wc.change_id, |
| 576 | 1274× | deps: vec![], |
| 577 | 1274× | author: self.author(), |
| 578 | 1274× | hlc: old_wc.hlc, |
| 579 | 1274× | message: old_wc.message.clone(), |
| 580 | 1274× | tree_ops: built.tree_ops, |
| 581 | 1274× | edits: built.edits, |
| 582 | 1274× | kind: ChangeKind::WorkingCopy, |
| 583 | 1274× | }; |
| 584 | 1274× | let base_heads = base.maximal(&old_wc.deps); |
| 585 | 1274× | let mut deps: BTreeSet<ChangeHash> = base_heads.into_iter().collect(); |
| 586 | 1274× | deps.extend(referenced_changes(&wc, &base)); |
| 587 | 1274× | deps.extend(built.contributors.iter().copied()); |
| 588 | 1274× | wc.deps = deps.into_iter().collect(); |
| 589 | 1274× | wc.normalize(); |
| 590 | 1274× | let resolves = built.resolves; |
| 591 | 1274× | let contributors = built.contributors; |
| 592 | 1274× | let statuses = built.statuses; |
| 593 | | |
| 594 | 1274× | let record = crate::timing::phase("snapshot: hash and record"); |
| 595 | | |
| 596 | | |
| 597 | | |
| 598 | | |
| 599 | | |
| 600 | | |
| 601 | 1274× | let (new_hash, changed, encoded) = if for_commit { |
| 602 | 442× | (old_hash, true, Vec::new()) |
| 603 | | } else { |
| 604 | 832× | let encoded = wc.encode_streaming(|batch| self.store.put_texts(&batch))?; |
| 605 | 832× | let new_hash = crate::hash::hash_object(crate::hash::ObjectType::Change, &encoded); |
| 606 | 832× | (new_hash, new_hash != old_hash, encoded) |
| 607 | | }; |
| 608 | 1274× | if changed && !for_commit { |
| 609 | 295× | self.store.put(crate::hash::ObjectType::Change, &encoded)?; |
| 610 | 295× | let mut view = op.view.clone(); |
| 611 | 295× | view.workspaces.insert(self.workspace, new_hash); |
| 612 | 295× | self.record_op(&[op_ref], &format!("{command}{SNAPSHOT_SUFFIX}"), view)?; |
| 613 | 979× | } |
| 614 | 1274× | drop(record); |
| 615 | | |
| 616 | 1274× | let _t = crate::timing::phase("snapshot: write manifest"); |
| 617 | 1274× | let conflicted = base_snapshot.is_conflicted(); |
| 618 | 1274× | let uncommitted = !wc.tree_ops.is_empty() || !wc.edits.is_empty(); |
| 619 | | |
| 620 | | |
| 621 | | |
| 622 | | |
| 623 | 1274× | let worth = match self.read_manifest()? { |
| 624 | 442× | _ if for_commit => true, |
| 625 | 313× | None => true, |
| 626 | 519× | Some((c, u, m)) => { |
| 627 | 519× | c != conflicted |
| 628 | 519× | || u != uncommitted |
| 629 | 290× | || m.entries.len() != fs_entries.len() |
| 630 | 375× | || fs_entries.iter().any(|(p, e)| { |
| 631 | 375× | !e.unchanged && (e.graph_key.is_some() || !m.entries.contains_key(p)) |
| 632 | 375× | }) |
| 633 | 48× | || built |
| 634 | 48× | .blobs |
| 635 | 48× | .iter() |
| 636 | 48× | .any(|(p, b)| m.entries.get(p).and_then(|e| e.blob) != Some(*b)) |
| 637 | | |
| 638 | | |
| 639 | | |
| 640 | 63× | || (uncommitted && statuses.iter().any(|st| { |
| 641 | 63× | let Some(e) = fs_entries.get(&st.path) else { |
| 642 | 3× | return m.entries.contains_key(&st.path); |
| 643 | | }; |
| 644 | 60× | let Some(me) = m.entries.get(&st.path) else { |
| 645 | 0× | return true; |
| 646 | | }; |
| 647 | 60× | let hash = e |
| 648 | 60× | .content_hash |
| 649 | 60× | .unwrap_or_else(|| *blake3::hash(&e.content).as_bytes()); |
| 650 | 60× | let stat = e.stat.or_else(|| { |
| 651 | 0× | fs::symlink_metadata(self.root.join(&st.path)) |
| 652 | 0× | .ok() |
| 653 | 0× | .map(|md| (md.len(), super::prompt::mtime_ns(&md))) |
| 654 | 0× | }); |
| 655 | | |
| 656 | | |
| 657 | 60× | me.hash != hash || me.exec != e.exec || stat != Some((me.size, me.mtime_ns)) |
| 658 | 63× | })) |
| 659 | | } |
| 660 | | }; |
| 661 | 1274× | let attributes = crate::attributes::Attributes::from_files( |
| 662 | 1274× | fs_entries |
| 663 | 1274× | .iter() |
| 664 | 12118× | .filter(|(p, _)| is_attributes(p)) |
| 665 | 1274× | .map(|(p, e)| (p.as_str(), e.content.as_slice())), |
| 666 | | ); |
| 667 | 1274× | let mut manifest = None; |
| 668 | 1274× | if for_commit { |
| 669 | 442× | manifest = Some(PendingManifest { |
| 670 | 442× | entries: fs_entries, |
| 671 | 442× | blobs: built.blobs.clone(), |
| 672 | 442× | dirs: self.walked_dirs.take(), |
| 673 | 442× | written: super::prompt::now_ns(), |
| 674 | 442× | }); |
| 675 | 442× | } else { |
| 676 | 832× | if worth { |
| 677 | 787× | self.write_manifest_entries(conflicted, uncommitted, &fs_entries, &built.blobs)?; |
| 678 | 45× | } |
| 679 | 832× | self.write_status_cache((uncommitted && !conflicted).then_some(&new_hash), &statuses); |
| 680 | | } |
| 681 | 1274× | Ok(SnapshotResult { |
| 682 | 1274× | wc_hash: new_hash, |
| 683 | 1274× | wc, |
| 684 | 1274× | base, |
| 685 | 1274× | base_snapshot, |
| 686 | 1274× | statuses, |
| 687 | 1274× | changed, |
| 688 | 1274× | resolves, |
| 689 | 1274× | contributors, |
| 690 | 1274× | attributes, |
| 691 | 1274× | untracked_nested: self.untracked_nested.borrow().clone(), |
| 692 | 1274× | manifest, |
| 693 | 1274× | }) |
| 694 | 1277× | } |
| 695 | |
|
| 696 | | |
| 697 | | |
| 698 | | |
| 699 | 7× | pub fn status_json(&self, snap: &SnapshotResult) -> Result<String> { |
| 700 | 7× | let heads = self.bookmark_heads()?; |
| 701 | 7× | let base_snapshot = &snap.base_snapshot; |
| 702 | 7× | let by_node = base_snapshot.tree.by_node(); |
| 703 | 7× | let path_of = |n: &NodeId| by_node.get(n).map(|e| e.path.clone()); |
| 704 | 7× | let id_of = |h: &ChangeHash| { |
| 705 | 4× | snap.base |
| 706 | 4× | .get(h) |
| 707 | 4× | .map(|m| m.change_id.to_hex()) |
| 708 | 4× | .unwrap_or_else(|| h.to_hex()) |
| 709 | 4× | }; |
| 710 | 7× | let paths = json_array(snap.statuses.iter().map(|s| { |
| 711 | 4× | format!( |
| 712 | | "{{\"path\":{},\"status\":{}}}", |
| 713 | 4× | json_str(&s.path), |
| 714 | 4× | json_str(&format!("{:?}", s.status)) |
| 715 | | ) |
| 716 | 4× | })); |
| 717 | 7× | let mut conflicts: Vec<String> = Vec::new(); |
| 718 | 7× | for (p, f) in &base_snapshot.files { |
| 719 | 7× | if !f.is_conflicted() { |
| 720 | 5× | continue; |
| 721 | 2× | } |
| 722 | 2× | let (kind, changes) = match &f.content { |
| 723 | | FileContent::Text { .. } => { |
| 724 | 2× | ("text", snap.base.conflict_regions(&f.node).contributors) |
| 725 | | } |
| 726 | 0× | FileContent::Opaque { blobs } => { |
| 727 | 0× | ("replace", snap.base.blob_contributors(&f.node, blobs)) |
| 728 | | } |
| 729 | 0× | FileContent::Dir => continue, |
| 730 | | }; |
| 731 | 2× | let resolved = match snap |
| 732 | 2× | .statuses |
| 733 | 2× | .iter() |
| 734 | 2× | .find(|s| s.path == *p) |
| 735 | 2× | .map(|s| s.status) |
| 736 | | { |
| 737 | 1× | Some(ChangeStatus::Resolved) => "true", |
| 738 | 0× | Some(ChangeStatus::ConflictEdited) => "\"markers\"", |
| 739 | 1× | _ => "false", |
| 740 | | }; |
| 741 | 2× | conflicts.push(format!( |
| 742 | | "{{\"kind\":{},\"path\":{},\"node\":{},\"changes\":{},\"resolved\":{resolved}}}", |
| 743 | 2× | json_str(kind), |
| 744 | 2× | json_str(p), |
| 745 | 2× | json_str(&f.node.to_hex()), |
| 746 | 4× | json_array(changes.iter().map(|h| json_str(&id_of(h)))) |
| 747 | | )); |
| 748 | | } |
| 749 | 7× | for c in &base_snapshot.tree.conflicts { |
| 750 | 0× | let (kind, nodes): (&str, Vec<NodeId>) = match c { |
| 751 | 0× | crate::tree::TreeConflict::Name { nodes, .. } => ("name", nodes.clone()), |
| 752 | 0× | crate::tree::TreeConflict::MultiPlacement { node, .. } => { |
| 753 | 0× | ("multi-placement", vec![*node]) |
| 754 | | } |
| 755 | 0× | crate::tree::TreeConflict::Orphan { node, .. } => ("orphan", vec![*node]), |
| 756 | 0× | crate::tree::TreeConflict::Cycle { node } => ("cycle", vec![*node]), |
| 757 | 0× | crate::tree::TreeConflict::Mode { node, .. } => ("mode", vec![*node]), |
| 758 | | }; |
| 759 | 0× | let detail = match c { |
| 760 | 0× | crate::tree::TreeConflict::Name { name, parent, .. } => { |
| 761 | 0× | let dir = if *parent == NodeId::ROOT { |
| 762 | 0× | String::new() |
| 763 | | } else { |
| 764 | 0× | path_of(parent).unwrap_or_default() |
| 765 | | }; |
| 766 | 0× | format!( |
| 767 | | ",\"path\":{}", |
| 768 | 0× | json_str(&if dir.is_empty() { |
| 769 | 0× | name.clone() |
| 770 | | } else { |
| 771 | 0× | format!("{dir}/{name}") |
| 772 | | }) |
| 773 | | ) |
| 774 | | } |
| 775 | 0× | _ => nodes |
| 776 | 0× | .first() |
| 777 | 0× | .and_then(path_of) |
| 778 | 0× | .map(|p| format!(",\"path\":{}", json_str(&p))) |
| 779 | 0× | .unwrap_or_default(), |
| 780 | | }; |
| 781 | 0× | conflicts.push(format!( |
| 782 | | "{{\"kind\":{},\"nodes\":{}{detail}}}", |
| 783 | 0× | json_str(kind), |
| 784 | 0× | json_array(nodes.iter().map(|n| json_str(&n.to_hex()))) |
| 785 | | )); |
| 786 | | } |
| 787 | 7× | Ok(format!( |
| 788 | | "{{\"bookmark\":{},\"working_copy\":{},\"heads\":{},\"clean\":{},\"conflicted\":{},\"paths\":{paths},\"conflicts\":{},\"untracked_nested\":{},\"skipped_large\":{}}}", |
| 789 | 7× | json_str(&self.config.bookmark), |
| 790 | 7× | json_str(&snap.wc.change_id.to_hex()), |
| 791 | 7× | heads.len(), |
| 792 | 7× | snap.statuses.is_empty(), |
| 793 | 7× | base_snapshot.is_conflicted(), |
| 794 | 7× | json_array(conflicts), |
| 795 | 7× | json_array(snap.untracked_nested.iter().map(|p| json_str(p))), |
| 796 | 7× | json_array( |
| 797 | 7× | self.skipped_large |
| 798 | 7× | .borrow() |
| 799 | 7× | .iter() |
| 800 | 7× | .map(|(p, n)| format!("{{\"path\":{},\"size\":{n}}}", json_str(p))) |
| 801 | | ) |
| 802 | | )) |
| 803 | 7× | } |
| 804 | |
|
| 805 | | |
| 806 | 623× | pub fn commit(&self, message: &str, extra_deps: &[ChangeHash]) -> Result<ChangeHash> { |
| 807 | 623× | self.commit_selection(message, extra_deps, &Selection::All) |
| 808 | 623× | } |
| 809 | |
|
| 810 | | |
| 811 | | |
| 812 | 7× | pub fn hunks(&self) -> Result<(SnapshotResult, Vec<Hunk>)> { |
| 813 | 7× | let snap = self.snapshot_status("oxy commit")?; |
| 814 | 7× | let hunks = render_hunks(&snap.wc, &snap.base, &snap.base_snapshot); |
| 815 | 7× | Ok((snap, hunks)) |
| 816 | 7× | } |
| 817 | |
|
| 818 | | |
| 819 | | |
| 820 | | |
| 821 | | |
| 822 | | |
| 823 | | |
| 824 | | |
| 825 | | |
| 826 | | |
| 827 | | |
| 828 | 643× | fn committed_graph_keys( |
| 829 | 643× | &self, |
| 830 | 643× | st: &State, |
| 831 | 643× | paths: &[String], |
| 832 | 643× | attrs: &crate::attributes::Attributes, |
| 833 | 643× | ) -> HashMap<String, [u8; 32]> { |
| 834 | 643× | let _t = crate::timing::phase("commit: graph keys"); |
| 835 | 643× | let view = st.tree().view(); |
| 836 | 643× | paths |
| 837 | 643× | .iter() |
| 838 | 3473× | .filter_map(|p| { |
| 839 | 3473× | let e = view.entries.get(p)?; |
| 840 | 3428× | (e.kind == NodeKind::File && !e.mode.opaque && attrs.lookup(p).eol.is_none()) |
| 841 | 3428× | .then_some(())?; |
| 842 | 3378× | Some((p.clone(), st.graph_key(&e.node)?)) |
| 843 | 3473× | }) |
| 844 | 643× | .collect() |
| 845 | 643× | } |
| 846 | |
|
| 847 | 628× | pub fn commit_selection( |
| 848 | 628× | &self, |
| 849 | 628× | message: &str, |
| 850 | 628× | extra_deps: &[ChangeHash], |
| 851 | 628× | selection: &Selection, |
| 852 | 628× | ) -> Result<ChangeHash> { |
| 853 | 628× | Ok(self |
| 854 | 628× | .commit_selection_partial(message, extra_deps, selection)? |
| 855 | | .0) |
| 856 | 628× | } |
| 857 | |
|
| 858 | | |
| 859 | | |
| 860 | | |
| 861 | 654× | pub fn commit_selection_partial( |
| 862 | 654× | &self, |
| 863 | 654× | message: &str, |
| 864 | 654× | extra_deps: &[ChangeHash], |
| 865 | 654× | selection: &Selection, |
| 866 | 654× | ) -> Result<(ChangeHash, bool)> { |
| 867 | | |
| 868 | | |
| 869 | 654× | let batch = self.store.begin_batch()?; |
| 870 | 654× | let out = self.commit_selection_batched(message, extra_deps, selection); |
| 871 | 654× | batch.finish()?; |
| 872 | 654× | out |
| 873 | 654× | } |
| 874 | |
|
| 875 | | |
| 876 | | |
| 877 | 433× | fn write_pending_manifest( |
| 878 | 433× | &self, |
| 879 | 433× | mut m: PendingManifest, |
| 880 | 433× | conflicted: bool, |
| 881 | 433× | keys: HashMap<String, [u8; 32]>, |
| 882 | 433× | ) -> Result<()> { |
| 883 | 2825× | for (path, k) in keys { |
| 884 | 2825× | if let Some(e) = m.entries.get_mut(&path) { |
| 885 | 2825× | e.graph_key = Some(k); |
| 886 | 2825× | } |
| 887 | | } |
| 888 | 433× | self.write_manifest_entries_as_of( |
| 889 | 433× | conflicted, |
| 890 | | false, |
| 891 | 433× | &m.entries, |
| 892 | 433× | &m.blobs, |
| 893 | 433× | m.written, |
| 894 | 433× | m.dirs.unwrap_or_default(), |
| 895 | | ) |
| 896 | 433× | } |
| 897 | |
|
| 898 | 654× | fn commit_selection_batched( |
| 899 | 654× | &self, |
| 900 | 654× | message: &str, |
| 901 | 654× | extra_deps: &[ChangeHash], |
| 902 | 654× | selection: &Selection, |
| 903 | 654× | ) -> Result<(ChangeHash, bool)> { |
| 904 | 654× | let _wc = self.lock_working_copy()?; |
| 905 | 654× | let mut snap = match self.snapshot_from_cache()? { |
| 906 | 212× | Some(snap) => snap, |
| 907 | 442× | None => self.snapshot_as("oxy commit", true, true)?, |
| 908 | | }; |
| 909 | 654× | let pending = snap.manifest.take(); |
| 910 | 654× | if snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty() { |
| 911 | 3× | if let Some(m) = pending { |
| 912 | 3× | let conflicted = snap.base_snapshot.is_conflicted(); |
| 913 | 3× | self.write_pending_manifest(m, conflicted, HashMap::new())?; |
| 914 | 0× | } |
| 915 | 3× | return fail("nothing to commit: the working copy matches its base"); |
| 916 | 651× | } |
| 917 | 651× | if let Some(s) = snap |
| 918 | 651× | .statuses |
| 919 | 651× | .iter() |
| 920 | 3490× | .find(|s| s.status == ChangeStatus::ConflictEdited) |
| 921 | | { |
| 922 | 0× | return fail(format!("{} still contains conflict markers", s.path)); |
| 923 | 651× | } |
| 924 | 651× | if message.trim().is_empty() { |
| 925 | 0× | return fail("a commit message is required"); |
| 926 | 651× | } |
| 927 | | |
| 928 | | |
| 929 | | |
| 930 | 651× | let wc_refs = referenced_changes(&snap.wc, &snap.base); |
| 931 | 3490× | let changed_paths: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect(); |
| 932 | 651× | let (tree_ops, edits, partial) = match selection { |
| 933 | 643× | Selection::All => ( |
| 934 | 643× | std::mem::take(&mut snap.wc.tree_ops), |
| 935 | 643× | std::mem::take(&mut snap.wc.edits), |
| 936 | 643× | false, |
| 937 | 643× | ), |
| 938 | 4× | Selection::Paths(paths) => { |
| 939 | 4× | let (o, e) = select_paths(&snap.wc, &snap.base_snapshot, paths); |
| 940 | 4× | let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len(); |
| 941 | 4× | (o, e, partial) |
| 942 | | } |
| 943 | 4× | Selection::Hunks(indices) => { |
| 944 | 4× | let (o, e) = select_hunks(&snap.wc, indices); |
| 945 | 4× | let partial = o.len() != snap.wc.tree_ops.len() || e.len() != snap.wc.edits.len(); |
| 946 | 4× | (o, e, partial) |
| 947 | | } |
| 948 | | }; |
| 949 | 651× | if tree_ops.is_empty() && edits.is_empty() { |
| 950 | 0× | return fail("nothing selected to commit"); |
| 951 | 651× | } |
| 952 | 651× | if partial && snap.resolves { |
| 953 | 0× | return fail("a conflict resolution must be committed whole"); |
| 954 | 651× | } |
| 955 | | |
| 956 | 651× | self.run_hook( |
| 957 | 651× | "pre-commit", |
| 958 | 651× | &format!( |
| 959 | | "{{\"event\":\"pre-commit\",\"message\":{},\"paths\":{}}}", |
| 960 | 651× | json_str(message), |
| 961 | 3490× | json_array(snap.statuses.iter().map(|s| format!( |
| 962 | | "{{\"path\":{},\"status\":{}}}", |
| 963 | 3490× | json_str(&s.path), |
| 964 | 3490× | json_str(&format!("{:?}", s.status)) |
| 965 | | ))) |
| 966 | | ), |
| 967 | 1× | )?; |
| 968 | 650× | let (op_ref, op) = self.head_op()?; |
| 969 | 650× | let mut deps: BTreeSet<ChangeHash> = wc_refs; |
| 970 | | |
| 971 | 650× | if self.config.semantic_deps { |
| 972 | 1× | let touched: Vec<String> = snap.statuses.iter().map(|s| s.path.clone()).collect(); |
| 973 | 1× | let by_node = snap.base_snapshot.tree.by_node(); |
| 974 | 1× | let mut candidates: Vec<(ChangeId, ChangeHash, Vec<String>)> = Vec::new(); |
| 975 | 1× | let mut seen: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 976 | 2× | for h in deps.iter().chain(snap.base.maximal(&snap.wc.deps).iter()) { |
| 977 | 2× | if !seen.insert(*h) { |
| 978 | 0× | continue; |
| 979 | 2× | } |
| 980 | 2× | let c = self.store.get_change(h)?; |
| 981 | 2× | let mut paths: BTreeSet<String> = BTreeSet::new(); |
| 982 | 6× | for n in c |
| 983 | 2× | .tree_ops |
| 984 | 2× | .iter() |
| 985 | 3× | .map(|o| o.node()) |
| 986 | 3× | .chain(c.edits.iter().map(|e| e.node())) |
| 987 | | { |
| 988 | 6× | if let Some(e) = by_node.get(&n) { |
| 989 | 6× | paths.insert(e.path.clone()); |
| 990 | 6× | } |
| 991 | | } |
| 992 | 2× | candidates.push((c.change_id, *h, paths.into_iter().collect())); |
| 993 | | } |
| 994 | 1× | for d in self.semantic_deps(&snap.wc.change_id, &touched, &candidates)? { |
| 995 | 1× | if snap.base.contains(&d) { |
| 996 | 1× | deps.insert(d); |
| 997 | 1× | } |
| 998 | | } |
| 999 | 649× | } |
| 1000 | | |
| 1001 | | |
| 1002 | | |
| 1003 | 650× | deps.extend(snap.contributors.iter().copied()); |
| 1004 | 650× | for d in extra_deps { |
| 1005 | 2× | if !snap.base.contains(d) { |
| 1006 | 0× | return fail(format!( |
| 1007 | | "--depends-on {d}: not in the working copy's base state" |
| 1008 | | )); |
| 1009 | 2× | } |
| 1010 | 2× | deps.insert(*d); |
| 1011 | | } |
| 1012 | 650× | let mut change = Change { |
| 1013 | | version: crate::objects::CHANGE_VERSION, |
| 1014 | 650× | change_id: if partial { |
| 1015 | 6× | ChangeId::random() |
| 1016 | | } else { |
| 1017 | 644× | snap.wc.change_id |
| 1018 | | }, |
| 1019 | 650× | deps: vec![], |
| 1020 | 650× | author: self.author(), |
| 1021 | 650× | hlc: Hlc::now(), |
| 1022 | 650× | message: message.trim_end().to_string(), |
| 1023 | 650× | tree_ops, |
| 1024 | 650× | edits, |
| 1025 | 650× | kind: if snap.resolves { |
| 1026 | 7× | ChangeKind::Resolution |
| 1027 | | } else { |
| 1028 | 643× | ChangeKind::Normal |
| 1029 | | }, |
| 1030 | | }; |
| 1031 | 650× | if partial { |
| 1032 | | |
| 1033 | | |
| 1034 | 6× | deps = referenced_changes(&change, &snap.base); |
| 1035 | 6× | for d in extra_deps { |
| 1036 | 0× | deps.insert(*d); |
| 1037 | 0× | } |
| 1038 | 644× | } |
| 1039 | 650× | change.deps = deps.into_iter().collect(); |
| 1040 | 650× | change.normalize(); |
| 1041 | 650× | let signing = crate::timing::phase("commit: sign and store"); |
| 1042 | 650× | let hash = self.store_signed(&change)?; |
| 1043 | 649× | drop(signing); |
| 1044 | 649× | let extending = crate::timing::phase("commit: extend the state"); |
| 1045 | |
|
| 1046 | | |
| 1047 | | |
| 1048 | | |
| 1049 | | |
| 1050 | 649× | let old_heads = self.heads_of(&op.view, &self.config.bookmark)?; |
| 1051 | 649× | let old_base = snap.base.maximal(&snap.wc.deps); |
| 1052 | 649× | let mut all: Vec<ChangeHash> = old_heads.clone(); |
| 1053 | 649× | all.extend(old_base.iter().copied()); |
| 1054 | 649× | all.push(hash); |
| 1055 | | |
| 1056 | | |
| 1057 | 649× | let mut st = if old_heads.iter().all(|h| snap.base.contains(h)) { |
| 1058 | 610× | snap.base |
| 1059 | | } else { |
| 1060 | 39× | self.load_state(&all)? |
| 1061 | | }; |
| 1062 | 649× | st.add_hashed(hash, change)?; |
| 1063 | 649× | let mut with_new = old_heads.clone(); |
| 1064 | 649× | with_new.push(hash); |
| 1065 | 649× | let new_heads = st.maximal(&with_new); |
| 1066 | | |
| 1067 | 649× | let since = self |
| 1068 | 649× | .latest_checkpoint(&self.config.bookmark)? |
| 1069 | 649× | .map(|(_, cp)| cp.count) |
| 1070 | 649× | .unwrap_or(0); |
| 1071 | 649× | if self.config.checkpoint_interval > 0 |
| 1072 | 649× | && (st.len() as u64).saturating_sub(since) >= self.config.checkpoint_interval |
| 1073 | | { |
| 1074 | 2× | let cp_state = self.load_state_full(&new_heads)?; |
| 1075 | 2× | self.write_checkpoint(&self.config.bookmark, &cp_state, &new_heads)?; |
| 1076 | 647× | } |
| 1077 | 649× | let mut wc_base = old_base.clone(); |
| 1078 | 649× | wc_base.push(hash); |
| 1079 | 649× | let wc_base = st.maximal(&wc_base); |
| 1080 | 649× | drop(extending); |
| 1081 | |
|
| 1082 | | |
| 1083 | | |
| 1084 | | |
| 1085 | | |
| 1086 | 649× | let keying = crate::timing::phase("commit: graph keys of the files"); |
| 1087 | 649× | let committed_keys = |
| 1088 | 649× | (!partial).then(|| self.committed_graph_keys(&st, &changed_paths, &snap.attributes)); |
| 1089 | 649× | drop(keying); |
| 1090 | 649× | let recording = crate::timing::phase("commit: record"); |
| 1091 | 649× | let wc = self.new_working_copy(wc_base, ""); |
| 1092 | 649× | let wc_hash = self.store.put_change(&wc)?; |
| 1093 | 649× | let mut view = op.view.clone(); |
| 1094 | 649× | view.bookmarks.insert( |
| 1095 | 649× | self.config.bookmark.clone(), |
| 1096 | 649× | self.store.put_state(&StateObject::new(new_heads))?, |
| 1097 | | ); |
| 1098 | 649× | view.workspaces.insert(self.workspace, wc_hash); |
| 1099 | 649× | self.record_op(&[op_ref], &format!("oxy commit -m {message:?}"), view)?; |
| 1100 | 644× | drop(recording); |
| 1101 | 644× | let rewriting = crate::timing::phase("commit: manifest"); |
| 1102 | 644× | if partial { |
| 1103 | 6× | |
| 1104 | 6× | self.drop_manifest(); |
| 1105 | 6× | } else { |
| 1106 | 638× | let conflicted = snap.base_snapshot.is_conflicted() && !snap.resolves; |
| 1107 | | |
| 1108 | | |
| 1109 | | |
| 1110 | 638× | let keys = if conflicted { |
| 1111 | 0× | HashMap::new() |
| 1112 | | } else { |
| 1113 | 638× | committed_keys.unwrap_or_default() |
| 1114 | | }; |
| 1115 | 638× | match pending { |
| 1116 | 430× | Some(m) => self.write_pending_manifest(m, conflicted, keys)?, |
| 1117 | 208× | None => self.rewrite_manifest(conflicted, false, &keys)?, |
| 1118 | | } |
| 1119 | | } |
| 1120 | 644× | drop(rewriting); |
| 1121 | 644× | let packing = crate::timing::phase("commit: autopack"); |
| 1122 | 644× | self.maybe_autopack()?; |
| 1123 | 644× | drop(packing); |
| 1124 | 644× | let _hook = crate::timing::phase("commit: hook"); |
| 1125 | 644× | let _ = self.run_hook( |
| 1126 | 644× | "post-commit", |
| 1127 | 644× | &format!( |
| 1128 | 644× | "{{\"event\":\"post-commit\",\"change_id\":{},\"hash\":{},\"message\":{}}}", |
| 1129 | 644× | json_str(&snap.wc.change_id.to_hex()), |
| 1130 | 644× | json_str(&hash.to_hex()), |
| 1131 | 644× | json_str(message) |
| 1132 | 644× | ), |
| 1133 | 644× | ); |
| 1134 | 644× | Ok((hash, partial)) |
| 1135 | 654× | } |
| 1136 | | } |
| 1137 | |
|
| 1138 | |
|
| 1139 | | #[derive(Clone, Debug)] |
| 1140 | | pub enum Selection { |
| 1141 | | All, |
| 1142 | | |
| 1143 | | Paths(Vec<String>), |
| 1144 | | |
| 1145 | | Hunks(Vec<usize>), |
| 1146 | | } |
| 1147 | |
|
| 1148 | |
|
| 1149 | | #[derive(Clone, Debug)] |
| 1150 | | pub struct Hunk { |
| 1151 | | pub index: usize, |
| 1152 | | pub path: String, |
| 1153 | | |
| 1154 | | pub text: String, |
| 1155 | | } |
| 1156 | |
|
| 1157 | |
|
| 1158 | |
|
| 1159 | 4× | fn select_paths( |
| 1160 | 4× | wc: &Change, |
| 1161 | 4× | base_snapshot: &Snapshot, |
| 1162 | 4× | paths: &[String], |
| 1163 | 4× | ) -> (Vec<TreeOp>, Vec<Edit>) { |
| 1164 | 13× | let selected = |p: &str| { |
| 1165 | 13× | paths |
| 1166 | 13× | .iter() |
| 1167 | 13× | .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))) |
| 1168 | 13× | }; |
| 1169 | 4× | let by_node = base_snapshot.tree.by_node(); |
| 1170 | 4× | let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 1171 | 4× | for op in &wc.tree_ops { |
| 1172 | | if let TreeOp::AddNode { |
| 1173 | 1× | node, |
| 1174 | 1× | parent, |
| 1175 | 1× | name, |
| 1176 | 1× | kind, |
| 1177 | | .. |
| 1178 | 1× | } = op |
| 1179 | 1× | { |
| 1180 | 1× | new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 1181 | 1× | } |
| 1182 | | } |
| 1183 | 4× | let mut nodes: BTreeSet<NodeId> = BTreeSet::new(); |
| 1184 | 13× | for n in wc |
| 1185 | 4× | .tree_ops |
| 1186 | 4× | .iter() |
| 1187 | 4× | .map(|o| o.node()) |
| 1188 | 12× | .chain(wc.edits.iter().map(|e| e.node())) |
| 1189 | | { |
| 1190 | 13× | if path_of_node(&n, &by_node, &new_names).is_some_and(|p| selected(&p)) { |
| 1191 | 8× | nodes.insert(n); |
| 1192 | 8× | } |
| 1193 | | } |
| 1194 | | |
| 1195 | 4× | let mut changed = true; |
| 1196 | 8× | while changed { |
| 1197 | 4× | changed = false; |
| 1198 | 4× | for (n, (parent, _, _)) in &new_names { |
| 1199 | 1× | if nodes.contains(n) && new_names.contains_key(parent) && nodes.insert(*parent) { |
| 1200 | 0× | changed = true; |
| 1201 | 1× | } |
| 1202 | | } |
| 1203 | | } |
| 1204 | 4× | let ops = wc |
| 1205 | 4× | .tree_ops |
| 1206 | 4× | .iter() |
| 1207 | 4× | .filter(|o| nodes.contains(&o.node())) |
| 1208 | 4× | .cloned() |
| 1209 | 4× | .collect(); |
| 1210 | 4× | let edits = wc |
| 1211 | 4× | .edits |
| 1212 | 4× | .iter() |
| 1213 | 12× | .filter(|e| nodes.contains(&e.node())) |
| 1214 | 4× | .cloned() |
| 1215 | 4× | .collect(); |
| 1216 | 4× | (ops, edits) |
| 1217 | 4× | } |
| 1218 | |
|
| 1219 | |
|
| 1220 | |
|
| 1221 | |
|
| 1222 | 5× | fn select_hunks(wc: &Change, indices: &[usize]) -> (Vec<TreeOp>, Vec<Edit>) { |
| 1223 | 5× | let n_ops = wc.tree_ops.len(); |
| 1224 | 5× | let mut ops_sel = vec![false; n_ops]; |
| 1225 | 5× | let mut edits_sel = vec![false; wc.edits.len()]; |
| 1226 | 5× | for &i in indices { |
| 1227 | 4× | if i < n_ops { |
| 1228 | 1× | ops_sel[i] = true; |
| 1229 | 3× | } else if i - n_ops < wc.edits.len() { |
| 1230 | 3× | edits_sel[i - n_ops] = true; |
| 1231 | 3× | } |
| 1232 | | } |
| 1233 | | |
| 1234 | 5× | let mut per_node: HashMap<NodeId, u32> = HashMap::new(); |
| 1235 | 5× | let mut insert_ranges: Vec<Option<(NodeId, u32, u32)>> = Vec::new(); |
| 1236 | 10× | for e in &wc.edits { |
| 1237 | 10× | insert_ranges.push(match e { |
| 1238 | 10× | Edit::Insert { node, lines, .. } => { |
| 1239 | 10× | let start = per_node.entry(*node).or_default(); |
| 1240 | 10× | let r = (*node, *start, *start + lines.len() as u32); |
| 1241 | 10× | *start += lines.len() as u32; |
| 1242 | 10× | Some(r) |
| 1243 | | } |
| 1244 | 0× | _ => None, |
| 1245 | | }); |
| 1246 | | } |
| 1247 | 5× | let add_index: HashMap<NodeId, usize> = wc |
| 1248 | 5× | .tree_ops |
| 1249 | 5× | .iter() |
| 1250 | 5× | .enumerate() |
| 1251 | 8× | .filter_map(|(i, o)| { |
| 1252 | 8× | if let TreeOp::AddNode { node, .. } = o { |
| 1253 | 8× | Some((*node, i)) |
| 1254 | | } else { |
| 1255 | 0× | None |
| 1256 | | } |
| 1257 | 8× | }) |
| 1258 | 5× | .collect(); |
| 1259 | 5× | let insert_for = |node: &NodeId, index: u32| -> Option<usize> { |
| 1260 | 0× | insert_ranges |
| 1261 | 0× | .iter() |
| 1262 | 0× | .position(|r| matches!(r, Some((n, a, b)) if n == node && *a <= index && index < *b)) |
| 1263 | 0× | }; |
| 1264 | | |
| 1265 | 5× | let mut changed = true; |
| 1266 | 13× | while changed { |
| 1267 | 8× | changed = false; |
| 1268 | 17× | for i in 0..n_ops { |
| 1269 | 17× | if !ops_sel[i] { |
| 1270 | 11× | continue; |
| 1271 | 6× | } |
| 1272 | 6× | if let TreeOp::AddNode { node, parent, .. } = &wc.tree_ops[i] { |
| 1273 | 6× | if let Some(&p) = add_index.get(parent) { |
| 1274 | 4× | if !ops_sel[p] { |
| 1275 | 2× | ops_sel[p] = true; |
| 1276 | 2× | changed = true; |
| 1277 | 2× | } |
| 1278 | 2× | } |
| 1279 | | |
| 1280 | | |
| 1281 | 12× | for (j, e) in wc.edits.iter().enumerate() { |
| 1282 | 12× | if e.node() == *node && !edits_sel[j] { |
| 1283 | 1× | edits_sel[j] = true; |
| 1284 | 1× | changed = true; |
| 1285 | 11× | } |
| 1286 | | } |
| 1287 | 0× | } |
| 1288 | | } |
| 1289 | 16× | for j in 0..wc.edits.len() { |
| 1290 | 16× | if !edits_sel[j] { |
| 1291 | 9× | continue; |
| 1292 | 7× | } |
| 1293 | 7× | let e = &wc.edits[j]; |
| 1294 | 7× | if let Some(&a) = add_index.get(&e.node()) { |
| 1295 | 5× | if !ops_sel[a] { |
| 1296 | 1× | ops_sel[a] = true; |
| 1297 | 1× | changed = true; |
| 1298 | 4× | } |
| 1299 | 2× | } |
| 1300 | | if let Edit::Insert { |
| 1301 | 7× | node, |
| 1302 | 7× | after, |
| 1303 | 7× | before, |
| 1304 | | .. |
| 1305 | 7× | } = e |
| 1306 | | { |
| 1307 | 14× | for r in [after, before] { |
| 1308 | 14× | if let AtomRef::Atom { change, index } = r { |
| 1309 | 2× | if change.is_zero() { |
| 1310 | 0× | if let Some(k) = insert_for(node, *index) { |
| 1311 | 0× | if !edits_sel[k] { |
| 1312 | 0× | edits_sel[k] = true; |
| 1313 | 0× | changed = true; |
| 1314 | 0× | } |
| 1315 | 0× | } |
| 1316 | 2× | } |
| 1317 | 12× | } |
| 1318 | | } |
| 1319 | 0× | } |
| 1320 | | } |
| 1321 | | } |
| 1322 | | |
| 1323 | 5× | let mut remap: HashMap<(NodeId, u32), u32> = HashMap::new(); |
| 1324 | 5× | let mut new_counts: HashMap<NodeId, u32> = HashMap::new(); |
| 1325 | 10× | for (j, r) in insert_ranges.iter().enumerate() { |
| 1326 | 10× | if let (true, Some((node, a, b))) = (edits_sel[j], r) { |
| 1327 | 4× | let next = new_counts.entry(*node).or_default(); |
| 1328 | 4× | for old in *a..*b { |
| 1329 | 4× | remap.insert((*node, old), *next); |
| 1330 | 4× | *next += 1; |
| 1331 | 4× | } |
| 1332 | 6× | } |
| 1333 | | } |
| 1334 | 8× | let map_ref = |r: AtomRef, node: &NodeId| -> AtomRef { |
| 1335 | 2× | match r { |
| 1336 | 2× | AtomRef::Atom { change, index } if change.is_zero() => AtomRef::Atom { |
| 1337 | 0× | change, |
| 1338 | 0× | index: remap.get(&(*node, index)).copied().unwrap_or(index), |
| 1339 | 0× | }, |
| 1340 | 8× | other => other, |
| 1341 | | } |
| 1342 | 8× | }; |
| 1343 | 5× | let ops = wc |
| 1344 | 5× | .tree_ops |
| 1345 | 5× | .iter() |
| 1346 | 5× | .enumerate() |
| 1347 | 8× | .filter(|(i, _)| ops_sel[*i]) |
| 1348 | 5× | .map(|(_, o)| o.clone()) |
| 1349 | 5× | .collect(); |
| 1350 | 5× | let edits = wc |
| 1351 | 5× | .edits |
| 1352 | 5× | .iter() |
| 1353 | 5× | .enumerate() |
| 1354 | 10× | .filter(|(j, _)| edits_sel[*j]) |
| 1355 | 5× | .map(|(_, e)| match e { |
| 1356 | | Edit::Insert { |
| 1357 | 4× | node, |
| 1358 | 4× | after, |
| 1359 | 4× | before, |
| 1360 | 4× | lines, |
| 1361 | 4× | } => Edit::Insert { |
| 1362 | 4× | node: *node, |
| 1363 | 4× | after: map_ref(*after, node), |
| 1364 | 4× | before: map_ref(*before, node), |
| 1365 | 4× | lines: lines.clone(), |
| 1366 | 4× | }, |
| 1367 | 0× | other => other.clone(), |
| 1368 | 4× | }) |
| 1369 | 5× | .collect(); |
| 1370 | 5× | (ops, edits) |
| 1371 | 5× | } |
| 1372 | |
|
| 1373 | |
|
| 1374 | 7× | pub fn render_hunks(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<Hunk> { |
| 1375 | 7× | let by_node = base_snapshot.tree.by_node(); |
| 1376 | 7× | let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 1377 | 8× | for op in &change.tree_ops { |
| 1378 | | if let TreeOp::AddNode { |
| 1379 | 8× | node, |
| 1380 | 8× | parent, |
| 1381 | 8× | name, |
| 1382 | 8× | kind, |
| 1383 | | .. |
| 1384 | 8× | } = op |
| 1385 | 8× | { |
| 1386 | 8× | new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 1387 | 8× | } |
| 1388 | | } |
| 1389 | 7× | let mut out = Vec::new(); |
| 1390 | 8× | for (i, op) in change.tree_ops.iter().enumerate() { |
| 1391 | 8× | let one = Change { |
| 1392 | 8× | tree_ops: vec![op.clone()], |
| 1393 | 8× | edits: vec![], |
| 1394 | 8× | ..change.clone() |
| 1395 | 8× | }; |
| 1396 | 8× | let path = path_of_node(&op.node(), &by_node, &new_names).unwrap_or_default(); |
| 1397 | 8× | out.push(Hunk { |
| 1398 | 8× | index: i, |
| 1399 | 8× | path, |
| 1400 | 8× | text: render_change(&one, base, base_snapshot), |
| 1401 | 8× | }); |
| 1402 | 8× | } |
| 1403 | 13× | for (j, e) in change.edits.iter().enumerate() { |
| 1404 | 13× | let one = Change { |
| 1405 | 13× | tree_ops: vec![], |
| 1406 | 13× | edits: vec![e.clone()], |
| 1407 | 13× | ..change.clone() |
| 1408 | 13× | }; |
| 1409 | 13× | let path = path_of_node(&e.node(), &by_node, &new_names).unwrap_or_default(); |
| 1410 | 13× | out.push(Hunk { |
| 1411 | 13× | index: change.tree_ops.len() + j, |
| 1412 | 13× | path, |
| 1413 | 13× | text: render_change(&one, base, base_snapshot), |
| 1414 | 13× | }); |
| 1415 | 13× | } |
| 1416 | 7× | out |
| 1417 | 7× | } |
| 1418 | |
|
| 1419 | |
|
| 1420 | | #[derive(Clone, Debug)] |
| 1421 | | pub struct FileDiff { |
| 1422 | | pub path: String, |
| 1423 | | |
| 1424 | | pub kind: String, |
| 1425 | | pub hunks: Vec<DiffHunk>, |
| 1426 | | |
| 1427 | | pub from: Option<String>, |
| 1428 | | } |
| 1429 | |
|
| 1430 | |
|
| 1431 | |
|
| 1432 | | #[derive(Clone, Debug, Default)] |
| 1433 | | pub struct DiffHunk { |
| 1434 | | pub base_line: u32, |
| 1435 | | pub added: Vec<Vec<u8>>, |
| 1436 | | pub removed: Vec<(u32, Vec<u8>)>, |
| 1437 | | } |
| 1438 | |
|
| 1439 | | impl Repo { |
| 1440 | | |
| 1441 | 26× | pub fn file_at(&self, path: &str, heads: Option<&[ChangeHash]>) -> Result<Vec<u8>> { |
| 1442 | | |
| 1443 | | |
| 1444 | | |
| 1445 | | |
| 1446 | 26× | let base = if self.is_bare() { |
| 1447 | 5× | None |
| 1448 | | } else { |
| 1449 | 21× | Some(self.working_copy()?.1.deps) |
| 1450 | | }; |
| 1451 | 26× | let same = |a: &[ChangeHash], b: &[ChangeHash]| { |
| 1452 | 1× | let (mut a, mut b) = (a.to_vec(), b.to_vec()); |
| 1453 | 1× | a.sort(); |
| 1454 | 1× | b.sort(); |
| 1455 | 1× | a == b |
| 1456 | 1× | }; |
| 1457 | 26× | let mut state = match (heads, base) { |
| 1458 | 1× | (Some(h), Some(b)) if same(h, &b) => self.load_state_indexed(&b)?, |
| 1459 | 5× | (Some(h), _) => self.load_state_history(h)?, |
| 1460 | 20× | (None, Some(b)) => self.load_state_indexed(&b)?, |
| 1461 | 0× | (None, None) => return fail("this is a bare repository: it has no working copy"), |
| 1462 | | }; |
| 1463 | 26× | let view = state.tree().view(); |
| 1464 | 26× | let Some(node) = view.entries.get(path).map(|e| e.node) else { |
| 1465 | 0× | return fail(format!("{path} is not in that state")); |
| 1466 | | }; |
| 1467 | 26× | self.load_graphs_of(&mut state, &[node])?; |
| 1468 | 25× | let snapshot = state.materialize_paths(view, [path]); |
| 1469 | 25× | let Some(f) = snapshot.files.get(path) else { |
| 1470 | 0× | return fail(format!("{path} is not in that state")); |
| 1471 | | }; |
| 1472 | 25× | match &f.content { |
| 1473 | 0× | FileContent::Dir => fail(format!("{path} is a directory")), |
| 1474 | 25× | FileContent::Text { bytes, .. } => Ok(bytes.clone()), |
| 1475 | 0× | FileContent::Opaque { blobs } => match blobs.first() { |
| 1476 | 0× | Some(b) => self.read_blob(b), |
| 1477 | 0× | None => Ok(vec![]), |
| 1478 | | }, |
| 1479 | | } |
| 1480 | 26× | } |
| 1481 | |
|
| 1482 | | |
| 1483 | | |
| 1484 | | |
| 1485 | 2× | pub fn discard(&self, paths: &[String]) -> Result<usize> { |
| 1486 | 2× | let _wc = self.lock_working_copy()?; |
| 1487 | 2× | let snap = self.snapshot_status("oxy discard")?; |
| 1488 | 7× | let under = |p: &str| { |
| 1489 | 7× | paths |
| 1490 | 7× | .iter() |
| 1491 | 7× | .any(|x| p == x || p.starts_with(&format!("{}/", x.trim_end_matches('/')))) |
| 1492 | 7× | }; |
| 1493 | 2× | let attrs = Repo::state_attributes(&snap.base_snapshot); |
| 1494 | 2× | let mut n = 0; |
| 1495 | 3× | for s in &snap.statuses { |
| 1496 | 3× | if !under(&s.path) { |
| 1497 | 1× | continue; |
| 1498 | 2× | } |
| 1499 | 2× | match snap.base_snapshot.files.get(&s.path) { |
| 1500 | | None => { |
| 1501 | | |
| 1502 | 1× | let full = self.root.join(&s.path); |
| 1503 | 1× | if full.is_dir() { |
| 1504 | 0× | let _ = fs::remove_dir_all(&full); |
| 1505 | 1× | } else { |
| 1506 | 1× | let _ = fs::remove_file(&full); |
| 1507 | 1× | } |
| 1508 | 1× | n += 1; |
| 1509 | | } |
| 1510 | 1× | Some(f) => { |
| 1511 | 1× | match &f.content { |
| 1512 | 0× | FileContent::Dir => fs::create_dir_all(self.root.join(&s.path))?, |
| 1513 | 1× | FileContent::Text { bytes, .. } => { |
| 1514 | 1× | let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&s.path).eol |
| 1515 | 1× | == Some(crate::attributes::Eol::Crlf) |
| 1516 | | { |
| 1517 | 0× | crate::attributes::to_crlf(bytes).into() |
| 1518 | | } else { |
| 1519 | 1× | bytes.as_slice().into() |
| 1520 | | }; |
| 1521 | 1× | self.write_file(&s.path, &on_disk, f.mode.exec)?; |
| 1522 | | } |
| 1523 | 0× | FileContent::Opaque { blobs } => { |
| 1524 | 0× | let bytes = match blobs.first() { |
| 1525 | 0× | Some(b) => self.read_blob(b)?, |
| 1526 | 0× | None => vec![], |
| 1527 | | }; |
| 1528 | 0× | if f.kind == NodeKind::Symlink { |
| 1529 | 0× | self.write_symlink(&s.path, &bytes)?; |
| 1530 | 0× | } else if f.kind == NodeKind::File { |
| 1531 | 0× | self.write_file(&s.path, &bytes, f.mode.exec)?; |
| 1532 | 0× | } |
| 1533 | | } |
| 1534 | | } |
| 1535 | 1× | n += 1; |
| 1536 | | } |
| 1537 | | } |
| 1538 | | } |
| 1539 | | |
| 1540 | 4× | for (p, f) in &snap.base_snapshot.files { |
| 1541 | 4× | if under(p) |
| 1542 | 1× | && !self.root.join(p).exists() |
| 1543 | 0× | && f.kind != NodeKind::Dir |
| 1544 | 0× | && f.kind != NodeKind::Subrepo |
| 1545 | 0× | && !snap.statuses.iter().any(|s| s.path == *p) |
| 1546 | | { |
| 1547 | 0× | let bytes = match &f.content { |
| 1548 | 0× | FileContent::Text { bytes, .. } => bytes.clone(), |
| 1549 | 0× | FileContent::Opaque { blobs } => blobs |
| 1550 | 0× | .first() |
| 1551 | 0× | .map(|b| self.read_blob(b)) |
| 1552 | 0× | .transpose()? |
| 1553 | 0× | .unwrap_or_default(), |
| 1554 | 0× | FileContent::Dir => continue, |
| 1555 | | }; |
| 1556 | 0× | if f.kind == NodeKind::Symlink { |
| 1557 | 0× | self.write_symlink(p, &bytes)? |
| 1558 | | } else { |
| 1559 | 0× | self.write_file(p, &bytes, f.mode.exec)? |
| 1560 | | } |
| 1561 | 0× | n += 1; |
| 1562 | 4× | } |
| 1563 | | } |
| 1564 | 2× | self.drop_manifest(); |
| 1565 | 2× | Ok(n) |
| 1566 | 2× | } |
| 1567 | |
|
| 1568 | | |
| 1569 | | |
| 1570 | | |
| 1571 | 1× | pub fn discard_hunks(&self, indices: &[usize]) -> Result<usize> { |
| 1572 | 1× | let _wc = self.lock_working_copy()?; |
| 1573 | 1× | let (snap, hunks) = self.hunks()?; |
| 1574 | 1× | let keep: Vec<usize> = hunks |
| 1575 | 1× | .iter() |
| 1576 | 1× | .map(|h| h.index) |
| 1577 | 1× | .filter(|i| !indices.contains(i)) |
| 1578 | 1× | .collect(); |
| 1579 | 1× | let (ops, edits) = select_hunks(&snap.wc, &keep); |
| 1580 | 1× | let dropped_nodes: BTreeSet<NodeId> = hunks |
| 1581 | 1× | .iter() |
| 1582 | 1× | .filter(|h| indices.contains(&h.index)) |
| 1583 | 1× | .map(|h| { |
| 1584 | 1× | if h.index < snap.wc.tree_ops.len() { |
| 1585 | 0× | snap.wc.tree_ops[h.index].node() |
| 1586 | | } else { |
| 1587 | 1× | snap.wc.edits[h.index - snap.wc.tree_ops.len()].node() |
| 1588 | | } |
| 1589 | 1× | }) |
| 1590 | 1× | .collect(); |
| 1591 | 1× | let mut partial = snap.wc.clone(); |
| 1592 | 1× | partial.tree_ops = ops; |
| 1593 | 1× | partial.edits = edits; |
| 1594 | 1× | partial.normalize(); |
| 1595 | 1× | let mut st = snap.base.clone(); |
| 1596 | 1× | st.add_unchecked(partial); |
| 1597 | | |
| 1598 | | |
| 1599 | 1× | self.load_all_graphs(&mut st)?; |
| 1600 | 1× | let target = st.materialize(); |
| 1601 | 1× | let by_node_target = target.tree.by_node(); |
| 1602 | 1× | let by_node_base = snap.base_snapshot.tree.by_node(); |
| 1603 | 1× | let attrs = Repo::state_attributes(&target); |
| 1604 | 1× | let mut n = 0; |
| 1605 | 1× | for node in dropped_nodes { |
| 1606 | 1× | match by_node_target.get(&node) { |
| 1607 | 1× | Some(e) => { |
| 1608 | 1× | let f = &target.files[&e.path]; |
| 1609 | 1× | match &f.content { |
| 1610 | 0× | FileContent::Dir => {} |
| 1611 | 1× | FileContent::Text { bytes, .. } => { |
| 1612 | 1× | let on_disk: std::borrow::Cow<[u8]> = if attrs.lookup(&e.path).eol |
| 1613 | 1× | == Some(crate::attributes::Eol::Crlf) |
| 1614 | | { |
| 1615 | 0× | crate::attributes::to_crlf(bytes).into() |
| 1616 | | } else { |
| 1617 | 1× | bytes.as_slice().into() |
| 1618 | | }; |
| 1619 | 1× | self.write_file(&e.path, &on_disk, f.mode.exec)?; |
| 1620 | | } |
| 1621 | 0× | FileContent::Opaque { blobs } => { |
| 1622 | 0× | let bytes = blobs |
| 1623 | 0× | .first() |
| 1624 | 0× | .map(|b| self.read_blob(b)) |
| 1625 | 0× | .transpose()? |
| 1626 | 0× | .unwrap_or_default(); |
| 1627 | 0× | if f.kind == NodeKind::Symlink { |
| 1628 | 0× | self.write_symlink(&e.path, &bytes)? |
| 1629 | | } else { |
| 1630 | 0× | self.write_file(&e.path, &bytes, f.mode.exec)? |
| 1631 | | } |
| 1632 | | } |
| 1633 | | } |
| 1634 | | |
| 1635 | 1× | if let Some(old_path) = snap |
| 1636 | 1× | .statuses |
| 1637 | 1× | .iter() |
| 1638 | 1× | .find(|s| { |
| 1639 | 1× | s.status == ChangeStatus::Renamed && !target.files.contains_key(&s.path) |
| 1640 | 1× | }) |
| 1641 | 1× | .map(|s| s.path.clone()) |
| 1642 | 0× | { |
| 1643 | 0× | let _ = fs::remove_file(self.root.join(old_path)); |
| 1644 | 1× | } |
| 1645 | 1× | n += 1; |
| 1646 | | } |
| 1647 | | None => { |
| 1648 | | |
| 1649 | 0× | if let Some(added) = snap.statuses.iter().find(|s| { |
| 1650 | 0× | s.status == ChangeStatus::Added |
| 1651 | 0× | && !target.files.contains_key(&s.path) |
| 1652 | 0× | && !by_node_base.contains_key(&node) |
| 1653 | 0× | }) { |
| 1654 | 0× | let full = self.root.join(&added.path); |
| 1655 | 0× | if full.is_dir() { |
| 1656 | 0× | let _ = fs::remove_dir_all(&full); |
| 1657 | 0× | } else { |
| 1658 | 0× | let _ = fs::remove_file(&full); |
| 1659 | 0× | } |
| 1660 | 0× | n += 1; |
| 1661 | 0× | } |
| 1662 | | } |
| 1663 | | } |
| 1664 | | } |
| 1665 | 1× | self.drop_manifest(); |
| 1666 | 1× | Ok(n) |
| 1667 | 1× | } |
| 1668 | |
|
| 1669 | | |
| 1670 | | |
| 1671 | 5× | pub fn diff_files(&self, change: Option<&ChangeHash>) -> Result<Vec<FileDiff>> { |
| 1672 | 5× | let (c, base, base_snapshot) = match change { |
| 1673 | 2× | Some(h) => { |
| 1674 | 2× | let c = self.store.get_change(h)?; |
| 1675 | 2× | let (base, snapshot) = self.change_base(&c)?; |
| 1676 | 2× | (c, base, snapshot) |
| 1677 | | } |
| 1678 | 3× | None => self.working_copy_with_base()?, |
| 1679 | | }; |
| 1680 | 5× | Ok(diff_files_of(&c, &base, &base_snapshot)) |
| 1681 | 5× | } |
| 1682 | |
|
| 1683 | | |
| 1684 | | |
| 1685 | | |
| 1686 | | |
| 1687 | | |
| 1688 | | |
| 1689 | | |
| 1690 | 655× | pub(crate) fn snapshot_from_cache(&self) -> Result<Option<SnapshotResult>> { |
| 1691 | 655× | let Some(statuses) = self.status_by_manifest()? else { |
| 1692 | 440× | return Ok(None); |
| 1693 | | }; |
| 1694 | 215× | if statuses.is_empty() { |
| 1695 | 2× | return Ok(None); |
| 1696 | 213× | } |
| 1697 | 213× | let _t = crate::timing::phase("snapshot: from the status cache"); |
| 1698 | 213× | let (wc_hash, wc) = self.working_copy()?; |
| 1699 | 213× | let base = self.load_state_indexed(&wc.deps)?; |
| 1700 | 213× | let (base, base_snapshot) = self.render_base_for(&wc, base)?; |
| 1701 | 213× | let attribute_files: Vec<(String, Vec<u8>)> = match self.read_manifest()? { |
| 1702 | 213× | Some((_, _, m)) => m |
| 1703 | 213× | .entries |
| 1704 | 213× | .keys() |
| 1705 | 672× | .filter(|p| is_attributes(p)) |
| 1706 | 213× | .filter_map(|p| fs::read(self.root.join(p)).ok().map(|b| (p.clone(), b))) |
| 1707 | 213× | .collect(), |
| 1708 | 0× | None => Vec::new(), |
| 1709 | | }; |
| 1710 | 213× | let attributes = crate::attributes::Attributes::from_files( |
| 1711 | 213× | attribute_files |
| 1712 | 213× | .iter() |
| 1713 | 213× | .map(|(p, b)| (p.as_str(), b.as_slice())), |
| 1714 | | ); |
| 1715 | 213× | Ok(Some(SnapshotResult { |
| 1716 | 213× | wc_hash, |
| 1717 | 213× | wc, |
| 1718 | 213× | base, |
| 1719 | 213× | base_snapshot, |
| 1720 | 213× | statuses, |
| 1721 | 213× | changed: false, |
| 1722 | 213× | resolves: false, |
| 1723 | 213× | contributors: Vec::new(), |
| 1724 | 213× | attributes, |
| 1725 | 213× | untracked_nested: self.untracked_nested.borrow().clone(), |
| 1726 | 213× | manifest: None, |
| 1727 | 213× | })) |
| 1728 | 655× | } |
| 1729 | |
|
| 1730 | | |
| 1731 | | |
| 1732 | | |
| 1733 | | |
| 1734 | | |
| 1735 | | |
| 1736 | 4× | pub fn working_copy_with_base(&self) -> Result<(Change, State, Snapshot)> { |
| 1737 | 4× | if self.status_by_manifest()?.is_some() { |
| 1738 | 2× | let (_, wc) = self.working_copy()?; |
| 1739 | 2× | let base = self.load_state_indexed(&wc.deps)?; |
| 1740 | 2× | let (base, snapshot) = self.tree_base_for(&wc, base)?; |
| 1741 | 2× | return Ok((wc, base, snapshot)); |
| 1742 | 2× | } |
| 1743 | 2× | let snap = self.snapshot_status("oxy diff")?; |
| 1744 | 2× | Ok((snap.wc, snap.base, snap.base_snapshot)) |
| 1745 | 4× | } |
| 1746 | |
|
| 1747 | | |
| 1748 | | |
| 1749 | 5× | pub fn log_for_path(&self, path: &str) -> Result<Vec<LogEntry>> { |
| 1750 | 5× | let heads = self.bookmark_heads()?; |
| 1751 | 5× | if self.is_bare() { |
| 1752 | 1× | return self.log_for_path_in(path, &heads); |
| 1753 | 4× | } |
| 1754 | 4× | let entries = self.log()?; |
| 1755 | 4× | let (_, wc) = self.working_copy()?; |
| 1756 | | |
| 1757 | | |
| 1758 | | |
| 1759 | 4× | let node_in = |state: State| state.tree().view().entries.get(path).map(|e| e.node); |
| 1760 | 4× | let node = match node_in(self.load_state_indexed(&wc.deps)?) { |
| 1761 | 4× | Some(n) => Some(n), |
| 1762 | 0× | None => node_in(self.load_state_history(&heads)?), |
| 1763 | | }; |
| 1764 | 4× | let Some(node) = node else { |
| 1765 | 0× | return fail(format!("{path} is not tracked")); |
| 1766 | | }; |
| 1767 | 4× | Ok(self.entries_touching(entries, node)) |
| 1768 | 5× | } |
| 1769 | |
|
| 1770 | | |
| 1771 | | |
| 1772 | 2× | pub fn log_for_path_in(&self, path: &str, heads: &[ChangeHash]) -> Result<Vec<LogEntry>> { |
| 1773 | 2× | self.log_for_path_of(&self.load_state_history(heads)?, path, None) |
| 1774 | 2× | } |
| 1775 | |
|
| 1776 | | |
| 1777 | | |
| 1778 | | |
| 1779 | 5× | pub fn log_for_path_of( |
| 1780 | 5× | &self, |
| 1781 | 5× | state: &State, |
| 1782 | 5× | path: &str, |
| 1783 | 5× | limit: Option<usize>, |
| 1784 | 5× | ) -> Result<Vec<LogEntry>> { |
| 1785 | 5× | let Some(node) = state.tree().view().entries.get(path).map(|e| e.node) else { |
| 1786 | 1× | return fail(format!("{path} is not in that state")); |
| 1787 | | }; |
| 1788 | 4× | Ok(self.entries_touching_up_to(self.log_of(state), node, limit)) |
| 1789 | 5× | } |
| 1790 | |
|
| 1791 | 4× | fn entries_touching(&self, entries: Vec<LogEntry>, node: NodeId) -> Vec<LogEntry> { |
| 1792 | 4× | self.entries_touching_up_to(entries, node, None) |
| 1793 | 4× | } |
| 1794 | |
|
| 1795 | 8× | fn entries_touching_up_to( |
| 1796 | 8× | &self, |
| 1797 | 8× | entries: Vec<LogEntry>, |
| 1798 | 8× | node: NodeId, |
| 1799 | 8× | limit: Option<usize>, |
| 1800 | 8× | ) -> Vec<LogEntry> { |
| 1801 | 8× | let mut touched = self.touched(); |
| 1802 | 8× | if limit.is_none() { |
| 1803 | 7× | let hashes: Vec<ChangeHash> = entries.iter().map(|e| e.hash).collect(); |
| 1804 | 7× | touched.fill(self, &hashes); |
| 1805 | 1× | } |
| 1806 | 8× | let wanted = HashSet::from([node]); |
| 1807 | 8× | let mut out = Vec::new(); |
| 1808 | 28× | for e in entries { |
| 1809 | 28× | if limit.is_some_and(|n| out.len() >= n) { |
| 1810 | 1× | break; |
| 1811 | 27× | } |
| 1812 | 27× | if touched.touches(self, &e.hash, &wanted) { |
| 1813 | 18× | out.push(e); |
| 1814 | 18× | } |
| 1815 | | } |
| 1816 | 8× | touched.save(); |
| 1817 | 8× | out |
| 1818 | 8× | } |
| 1819 | | } |
| 1820 | |
|
| 1821 | |
|
| 1822 | 5× | pub fn diff_files_of(change: &Change, base: &State, base_snapshot: &Snapshot) -> Vec<FileDiff> { |
| 1823 | 5× | let by_node = base_snapshot.tree.by_node(); |
| 1824 | 5× | let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 1825 | 5× | for op in &change.tree_ops { |
| 1826 | | if let TreeOp::AddNode { |
| 1827 | 2× | node, |
| 1828 | 2× | parent, |
| 1829 | 2× | name, |
| 1830 | 2× | kind, |
| 1831 | | .. |
| 1832 | 3× | } = op |
| 1833 | 2× | { |
| 1834 | 2× | new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 1835 | 2× | } |
| 1836 | | } |
| 1837 | 9× | let path_of = |n: &NodeId| -> String { |
| 1838 | 9× | path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8))) |
| 1839 | 9× | }; |
| 1840 | 5× | let mut files: BTreeMap<NodeId, FileDiff> = BTreeMap::new(); |
| 1841 | 5× | let mut order: Vec<NodeId> = Vec::new(); |
| 1842 | 14× | let mut entry = |files: &mut BTreeMap<NodeId, FileDiff>, n: NodeId, kind: &str| { |
| 1843 | 14× | if let std::collections::btree_map::Entry::Vacant(v) = files.entry(n) { |
| 1844 | 9× | order.push(n); |
| 1845 | 9× | v.insert(FileDiff { |
| 1846 | 9× | path: path_of(&n), |
| 1847 | 9× | kind: kind.to_string(), |
| 1848 | 9× | hunks: vec![], |
| 1849 | 9× | from: None, |
| 1850 | 9× | }); |
| 1851 | 9× | } |
| 1852 | 14× | }; |
| 1853 | 5× | for op in &change.tree_ops { |
| 1854 | 3× | match op { |
| 1855 | 2× | TreeOp::AddNode { node, kind, .. } => { |
| 1856 | 2× | if *kind != NodeKind::Dir { |
| 1857 | 2× | entry(&mut files, *node, "added"); |
| 1858 | 2× | } |
| 1859 | | } |
| 1860 | 1× | TreeOp::RemoveNode { node, .. } => { |
| 1861 | 1× | entry(&mut files, *node, "removed"); |
| 1862 | 1× | if let Some(e) = by_node.get(node) { |
| 1863 | 1× | files.get_mut(node).unwrap().path = e.path.clone(); |
| 1864 | 1× | } |
| 1865 | | } |
| 1866 | | TreeOp::Move { |
| 1867 | 0× | node, parent, name, .. |
| 1868 | | } => { |
| 1869 | 0× | entry(&mut files, *node, "renamed"); |
| 1870 | 0× | let f = files.get_mut(node).unwrap(); |
| 1871 | 0× | f.from = by_node.get(node).map(|e| e.path.clone()); |
| 1872 | 0× | let dir = if parent.is_root() { |
| 1873 | 0× | String::new() |
| 1874 | | } else { |
| 1875 | 0× | path_of_node(parent, &by_node, &new_names).unwrap_or_default() |
| 1876 | | }; |
| 1877 | 0× | f.path = if dir.is_empty() { |
| 1878 | 0× | name.clone() |
| 1879 | | } else { |
| 1880 | 0× | format!("{dir}/{name}") |
| 1881 | | }; |
| 1882 | | } |
| 1883 | 0× | TreeOp::SetMode { node, .. } => entry(&mut files, *node, "mode"), |
| 1884 | | } |
| 1885 | | } |
| 1886 | | |
| 1887 | 5× | let mut line_of: HashMap<NodeId, HashMap<(ChangeHash, u32), u32>> = HashMap::new(); |
| 1888 | 11× | let mut lines_for = |n: &NodeId| -> HashMap<(ChangeHash, u32), u32> { |
| 1889 | 11× | line_of |
| 1890 | 11× | .entry(*n) |
| 1891 | 11× | .or_insert_with(|| { |
| 1892 | 8× | base.live_lines(n) |
| 1893 | 8× | .into_iter() |
| 1894 | 8× | .enumerate() |
| 1895 | 11× | .map(|(i, (k, _))| (k, i as u32 + 1)) |
| 1896 | 8× | .collect() |
| 1897 | 8× | }) |
| 1898 | 11× | .clone() |
| 1899 | 11× | }; |
| 1900 | 11× | for e in &change.edits { |
| 1901 | 11× | let n = e.node(); |
| 1902 | 11× | let is_new = new_names.contains_key(&n); |
| 1903 | 11× | entry(&mut files, n, if is_new { "added" } else { "modified" }); |
| 1904 | 11× | let f = files.get_mut(&n).unwrap(); |
| 1905 | 11× | match e { |
| 1906 | 9× | Edit::Insert { after, lines, .. } => { |
| 1907 | 9× | let map = lines_for(&n); |
| 1908 | 9× | let base_line = match after { |
| 1909 | 3× | AtomRef::Start => 0, |
| 1910 | 0× | AtomRef::End => map.len() as u32, |
| 1911 | 6× | AtomRef::Atom { change, index } if change.is_zero() => { |
| 1912 | 0× | f.hunks.last().map(|h| h.base_line).unwrap_or(0) |
| 1913 | | } |
| 1914 | 6× | AtomRef::Atom { change, index } => { |
| 1915 | 6× | map.get(&(*change, *index)).copied().unwrap_or(0) |
| 1916 | | } |
| 1917 | | }; |
| 1918 | 9× | f.hunks.push(DiffHunk { |
| 1919 | 9× | base_line, |
| 1920 | 9× | added: lines.to_vec(), |
| 1921 | 9× | removed: vec![], |
| 1922 | 9× | }); |
| 1923 | | } |
| 1924 | 2× | Edit::Delete { atoms, .. } => { |
| 1925 | 2× | let map = lines_for(&n); |
| 1926 | 2× | let mut removed = Vec::new(); |
| 1927 | 2× | for r in atoms { |
| 1928 | 2× | for i in r.from..r.to { |
| 1929 | 2× | let line = map.get(&(r.change, i)).copied().unwrap_or(0); |
| 1930 | 2× | let text = base |
| 1931 | 2× | .file(&n) |
| 1932 | 2× | .and_then(|g| g.content(&(r.change, i))) |
| 1933 | 2× | .unwrap_or(b"") |
| 1934 | 2× | .to_vec(); |
| 1935 | 2× | removed.push((line, text)); |
| 1936 | | } |
| 1937 | | } |
| 1938 | 2× | let base_line = removed |
| 1939 | 2× | .first() |
| 1940 | 2× | .map(|(l, _)| l.saturating_sub(1)) |
| 1941 | 2× | .unwrap_or(0); |
| 1942 | 2× | f.hunks.push(DiffHunk { |
| 1943 | 2× | base_line, |
| 1944 | 2× | added: vec![], |
| 1945 | 2× | removed, |
| 1946 | 2× | }); |
| 1947 | | } |
| 1948 | | Edit::Replace { .. } => { |
| 1949 | 0× | if !is_new { |
| 1950 | 0× | f.kind = "replaced".into(); |
| 1951 | 0× | } |
| 1952 | | } |
| 1953 | | } |
| 1954 | | } |
| 1955 | 9× | order.into_iter().filter_map(|n| files.remove(&n)).collect() |
| 1956 | 5× | } |
| 1957 | |
|
| 1958 | |
|
| 1959 | |
|
| 1960 | |
|
| 1961 | 59× | fn seeded_node(seed: &[u8; 32], path: &str, kind: NodeKind) -> NodeId { |
| 1962 | 59× | let mut h = blake3::Hasher::new(); |
| 1963 | 59× | h.update(b"oxy node 1\0"); |
| 1964 | 59× | h.update(seed); |
| 1965 | 59× | h.update(&[match kind { |
| 1966 | 45× | NodeKind::File => 1, |
| 1967 | 13× | NodeKind::Dir => 2, |
| 1968 | 0× | NodeKind::Symlink => 3, |
| 1969 | 1× | NodeKind::Subrepo => 4, |
| 1970 | | }]); |
| 1971 | 59× | h.update(path.as_bytes()); |
| 1972 | 59× | let mut id = [0u8; 16]; |
| 1973 | 59× | id.copy_from_slice(&h.finalize().as_bytes()[..16]); |
| 1974 | 59× | if id == [0; 16] { |
| 1975 | 0× | id[15] = 1; |
| 1976 | 59× | } |
| 1977 | 59× | NodeId(id) |
| 1978 | 59× | } |
| 1979 | |
|
| 1980 | | struct ChangeBuilder<'a> { |
| 1981 | | base: &'a State, |
| 1982 | | base_snapshot: &'a Snapshot, |
| 1983 | | tree_ops: Vec<TreeOp>, |
| 1984 | | edits: Vec<Edit>, |
| 1985 | | statuses: Vec<PathStatus>, |
| 1986 | | dir_nodes: HashMap<String, NodeId>, |
| 1987 | | reuse: HashMap<String, (NodeId, NodeKind)>, |
| 1988 | | |
| 1989 | | contributors: BTreeSet<ChangeHash>, |
| 1990 | | resolves: bool, |
| 1991 | | attrs: crate::attributes::Attributes, |
| 1992 | | blobs: HashMap<String, ObjectRef>, |
| 1993 | | |
| 1994 | | resolve: HashSet<String>, |
| 1995 | | |
| 1996 | | node_seed: Option<[u8; 32]>, |
| 1997 | | |
| 1998 | | |
| 1999 | | dead_dirs: Option<HashMap<(NodeId, String), (NodeId, crate::tree::OpKey)>>, |
| 2000 | | |
| 2001 | | |
| 2002 | | diffs: HashMap<String, Vec<Edit>>, |
| 2003 | | } |
| 2004 | |
|
| 2005 | | impl<'a> ChangeBuilder<'a> { |
| 2006 | 1953× | fn new( |
| 2007 | 1953× | base: &'a State, |
| 2008 | 1953× | base_snapshot: &'a Snapshot, |
| 2009 | 1953× | reuse: HashMap<String, (NodeId, NodeKind)>, |
| 2010 | 1953× | ) -> Self { |
| 2011 | 1953× | let mut dir_nodes: HashMap<String, NodeId> = HashMap::new(); |
| 2012 | 1953× | dir_nodes.insert(String::new(), NodeId::ROOT); |
| 2013 | 17684× | for (p, e) in base_snapshot.tree.entries.iter() { |
| 2014 | 17684× | if e.kind == NodeKind::Dir { |
| 2015 | 545× | dir_nodes.insert(p.clone(), e.node); |
| 2016 | 17139× | } |
| 2017 | | } |
| 2018 | 1953× | ChangeBuilder { |
| 2019 | 1953× | base, |
| 2020 | 1953× | base_snapshot, |
| 2021 | 1953× | tree_ops: vec![], |
| 2022 | 1953× | edits: vec![], |
| 2023 | 1953× | statuses: vec![], |
| 2024 | 1953× | dir_nodes, |
| 2025 | 1953× | reuse, |
| 2026 | 1953× | contributors: BTreeSet::new(), |
| 2027 | 1953× | resolves: false, |
| 2028 | 1953× | attrs: crate::attributes::Attributes::default(), |
| 2029 | 1953× | blobs: HashMap::new(), |
| 2030 | 1953× | resolve: HashSet::new(), |
| 2031 | 1953× | node_seed: None, |
| 2032 | 1953× | dead_dirs: None, |
| 2033 | 1953× | diffs: HashMap::new(), |
| 2034 | 1953× | } |
| 2035 | 1953× | } |
| 2036 | |
|
| 2037 | | |
| 2038 | | |
| 2039 | | |
| 2040 | 1953× | fn diff_in_parallel(&mut self, entries: &BTreeMap<String, FsEntry>) { |
| 2041 | | use rayon::prelude::*; |
| 2042 | 1953× | let files = &self.base_snapshot.files; |
| 2043 | 1953× | let edited: Vec<(&String, &FsEntry, &crate::state::FileState)> = entries |
| 2044 | 1953× | .iter() |
| 2045 | 18695× | .filter_map(|(p, e)| { |
| 2046 | 18695× | let x = files.get(p)?; |
| 2047 | 12664× | (x.kind == e.kind && !e.unchanged && !self.opaque_for(p, e)).then_some((p, e, x)) |
| 2048 | 18695× | }) |
| 2049 | 1953× | .collect(); |
| 2050 | | |
| 2051 | 1953× | if edited.len() < 16 { |
| 2052 | 1943× | return; |
| 2053 | 10× | } |
| 2054 | 10× | let this = &*self; |
| 2055 | 10× | let attrs = &this.attrs; |
| 2056 | 10× | let diffs: Vec<(String, Vec<Edit>)> = edited |
| 2057 | 10× | .par_iter() |
| 2058 | 5560× | .filter_map(|(p, e, x)| { |
| 2059 | | let FileContent::Text { |
| 2060 | 5560× | bytes, |
| 2061 | 5560× | materialized, |
| 2062 | 5560× | } = &x.content |
| 2063 | | else { |
| 2064 | 0× | return None; |
| 2065 | | }; |
| 2066 | 5560× | if materialized.is_conflicted() { |
| 2067 | 0× | return None; |
| 2068 | 5560× | } |
| 2069 | 5560× | let disk_text = if attrs.lookup(p).eol.is_some() { |
| 2070 | 0× | std::sync::Arc::new(crate::attributes::normalize_eol(&e.content)) |
| 2071 | | } else { |
| 2072 | 5560× | e.content.clone() |
| 2073 | | }; |
| 2074 | 5560× | if *bytes == *disk_text { |
| 2075 | 5480× | return None; |
| 2076 | 80× | } |
| 2077 | 80× | let keys = materialized.live_atoms(); |
| 2078 | 80× | let lines = crate::diff::split_lines(bytes); |
| 2079 | 80× | (keys.len() == lines.len()).then(|| { |
| 2080 | 80× | let edits = crate::diff::text_edits_of(x.node, &keys, &lines, &disk_text); |
| 2081 | 80× | ((*p).clone(), edits) |
| 2082 | 80× | }) |
| 2083 | 5560× | }) |
| 2084 | 10× | .collect(); |
| 2085 | 10× | self.diffs = diffs.into_iter().collect(); |
| 2086 | 1953× | } |
| 2087 | |
|
| 2088 | 1953× | fn with_attributes(mut self, attrs: crate::attributes::Attributes) -> Self { |
| 2089 | 1953× | self.attrs = attrs; |
| 2090 | 1953× | self |
| 2091 | 1953× | } |
| 2092 | |
|
| 2093 | | |
| 2094 | | |
| 2095 | | |
| 2096 | 132× | fn blob_of( |
| 2097 | 132× | &mut self, |
| 2098 | 132× | repo: &Repo, |
| 2099 | 132× | path: &str, |
| 2100 | 132× | e: &FsEntry, |
| 2101 | 132× | nocompress: bool, |
| 2102 | 132× | ) -> Result<ObjectRef> { |
| 2103 | 132× | let b = match e.blob { |
| 2104 | 0× | Some(b) => b, |
| 2105 | 132× | None => repo.store_blob_for(&e.content, nocompress)?, |
| 2106 | | }; |
| 2107 | 132× | self.blobs.insert(path.to_string(), b); |
| 2108 | 132× | Ok(b) |
| 2109 | 132× | } |
| 2110 | |
|
| 2111 | 22868× | fn opaque_for(&self, path: &str, e: &FsEntry) -> bool { |
| 2112 | 22868× | if e.blob.is_some() { |
| 2113 | 8× | return true; |
| 2114 | 22860× | } |
| 2115 | 22860× | if e.kind == NodeKind::Symlink || e.kind == NodeKind::Subrepo { |
| 2116 | 79× | return true; |
| 2117 | 22781× | } |
| 2118 | 22781× | match self.attrs.lookup(path).binary { |
| 2119 | 12× | Some(true) => true, |
| 2120 | 0× | Some(false) => is_binary(&e.content), |
| 2121 | 22769× | None => is_binary(&e.content), |
| 2122 | | } |
| 2123 | 22868× | } |
| 2124 | |
|
| 2125 | | |
| 2126 | 14325× | fn stored_text( |
| 2127 | 14325× | &self, |
| 2128 | 14325× | path: &str, |
| 2129 | 14325× | content: &std::sync::Arc<Vec<u8>>, |
| 2130 | 14325× | ) -> std::sync::Arc<Vec<u8>> { |
| 2131 | 14325× | if self.attrs.lookup(path).eol.is_some() { |
| 2132 | 7× | std::sync::Arc::new(crate::attributes::normalize_eol(content)) |
| 2133 | | } else { |
| 2134 | 14318× | content.clone() |
| 2135 | | } |
| 2136 | 14325× | } |
| 2137 | |
|
| 2138 | | |
| 2139 | | |
| 2140 | 6155× | fn new_node(&self, path: &str, kind: NodeKind) -> NodeId { |
| 2141 | 6155× | if let Some(n) = self.reused_node(path, kind) { |
| 2142 | 139× | return n; |
| 2143 | 6016× | } |
| 2144 | 6016× | match &self.node_seed { |
| 2145 | 59× | Some(seed) => seeded_node(seed, path, kind), |
| 2146 | 5957× | None => NodeId::random(), |
| 2147 | | } |
| 2148 | 6155× | } |
| 2149 | |
|
| 2150 | 6155× | fn reused_node(&self, path: &str, kind: NodeKind) -> Option<NodeId> { |
| 2151 | 6155× | self.reuse |
| 2152 | 6155× | .get(path) |
| 2153 | 6155× | .filter(|(_, k)| *k == kind) |
| 2154 | 6155× | .map(|(n, _)| *n) |
| 2155 | 6155× | } |
| 2156 | |
|
| 2157 | | |
| 2158 | 15× | fn revivable_dir( |
| 2159 | 15× | &mut self, |
| 2160 | 15× | parent: NodeId, |
| 2161 | 15× | name: &str, |
| 2162 | 15× | ) -> Option<(NodeId, crate::tree::OpKey)> { |
| 2163 | 15× | let base = self.base; |
| 2164 | 15× | self.dead_dirs |
| 2165 | 15× | .get_or_insert_with(|| base.tree().dead_dirs()) |
| 2166 | 15× | .get(&(parent, name.to_string())) |
| 2167 | 15× | .copied() |
| 2168 | 15× | } |
| 2169 | |
|
| 2170 | 6032× | fn ensure_dir(&mut self, path: &str) -> NodeId { |
| 2171 | 6032× | if let Some(n) = self.dir_nodes.get(path) { |
| 2172 | 5881× | return *n; |
| 2173 | 151× | } |
| 2174 | 151× | let mut parent = NodeId::ROOT; |
| 2175 | 151× | let mut prefix = String::new(); |
| 2176 | 170× | for comp in path.split('/') { |
| 2177 | 170× | if !prefix.is_empty() { |
| 2178 | 19× | prefix.push('/'); |
| 2179 | 151× | } |
| 2180 | 170× | prefix.push_str(comp); |
| 2181 | 170× | let known = self.dir_nodes.get(&prefix).copied(); |
| 2182 | 170× | let revive = match known { |
| 2183 | 13× | Some(_) => None, |
| 2184 | 157× | None if self.node_seed.is_some() => self.revivable_dir(parent, comp), |
| 2185 | 142× | None => None, |
| 2186 | | }; |
| 2187 | 170× | parent = match (known, revive) { |
| 2188 | 13× | (Some(n), _) => n, |
| 2189 | 2× | (None, Some((node, old))) => { |
| 2190 | | |
| 2191 | | |
| 2192 | | |
| 2193 | | |
| 2194 | 2× | self.tree_ops.push(TreeOp::Move { |
| 2195 | 2× | node, |
| 2196 | 2× | old: OpRef { |
| 2197 | 2× | change: old.0, |
| 2198 | 2× | index: old.1, |
| 2199 | 2× | }, |
| 2200 | 2× | parent, |
| 2201 | 2× | name: comp.to_string(), |
| 2202 | 2× | }); |
| 2203 | 2× | self.dir_nodes.insert(prefix.clone(), node); |
| 2204 | 2× | node |
| 2205 | | } |
| 2206 | | (None, None) => { |
| 2207 | 155× | let node = self.new_node(&prefix, NodeKind::Dir); |
| 2208 | 155× | self.tree_ops.push(TreeOp::AddNode { |
| 2209 | 155× | node, |
| 2210 | 155× | kind: NodeKind::Dir, |
| 2211 | 155× | mode: Mode::default(), |
| 2212 | 155× | parent, |
| 2213 | 155× | name: comp.to_string(), |
| 2214 | 155× | }); |
| 2215 | 155× | self.dir_nodes.insert(prefix.clone(), node); |
| 2216 | 155× | node |
| 2217 | | } |
| 2218 | | }; |
| 2219 | | } |
| 2220 | 151× | parent |
| 2221 | 6032× | } |
| 2222 | |
|
| 2223 | 6000× | fn add_path(&mut self, repo: &Repo, path: &str, e: &FsEntry) -> Result<()> { |
| 2224 | 6000× | let (dir, name) = split_path(path); |
| 2225 | 6000× | let parent = self.ensure_dir(dir); |
| 2226 | 6000× | let node = self.new_node(path, e.kind); |
| 2227 | 6000× | let opaque = self.opaque_for(path, e); |
| 2228 | 6000× | let nocompress = self.attrs.lookup(path).nocompress; |
| 2229 | 6000× | self.tree_ops.push(TreeOp::AddNode { |
| 2230 | 6000× | node, |
| 2231 | 6000× | kind: e.kind, |
| 2232 | 6000× | mode: Mode { |
| 2233 | 6000× | exec: e.exec, |
| 2234 | 6000× | opaque, |
| 2235 | 6000× | }, |
| 2236 | 6000× | parent, |
| 2237 | 6000× | name: name.to_string(), |
| 2238 | 6000× | }); |
| 2239 | 6000× | if opaque { |
| 2240 | 96× | let blob = self.blob_of(repo, path, e, nocompress)?; |
| 2241 | 96× | self.edits.push(Edit::Replace { |
| 2242 | 96× | node, |
| 2243 | 96× | old: None, |
| 2244 | 96× | new: Some(blob), |
| 2245 | 96× | }); |
| 2246 | 5904× | } else if !e.content.is_empty() { |
| 2247 | 5875× | let content = self.stored_text(path, &e.content); |
| 2248 | 5875× | self.edits.push(Edit::Insert { |
| 2249 | 5875× | node, |
| 2250 | 5875× | after: crate::objects::AtomRef::Start, |
| 2251 | 5875× | before: crate::objects::AtomRef::End, |
| 2252 | 5875× | lines: crate::lines::Lines::split_shared(content), |
| 2253 | 5875× | }); |
| 2254 | 5875× | } |
| 2255 | 6000× | self.statuses.push(PathStatus { |
| 2256 | 6000× | path: path.to_string(), |
| 2257 | 6000× | status: ChangeStatus::Added, |
| 2258 | 6000× | }); |
| 2259 | 6000× | Ok(()) |
| 2260 | 6000× | } |
| 2261 | |
|
| 2262 | | |
| 2263 | 32× | fn move_path( |
| 2264 | 32× | &mut self, |
| 2265 | 32× | repo: &Repo, |
| 2266 | 32× | old: &str, |
| 2267 | 32× | new: &str, |
| 2268 | 32× | e: &FsEntry, |
| 2269 | 32× | expected: &crate::state::FileState, |
| 2270 | 32× | ) -> Result<()> { |
| 2271 | 32× | let placement = self.base_snapshot.tree.entries[old].placement; |
| 2272 | 32× | let (dir, name) = split_path(new); |
| 2273 | 32× | let parent = self.ensure_dir(dir); |
| 2274 | 32× | self.tree_ops.push(TreeOp::Move { |
| 2275 | 32× | node: expected.node, |
| 2276 | 32× | old: op_ref(placement), |
| 2277 | 32× | parent, |
| 2278 | 32× | name: name.to_string(), |
| 2279 | 32× | }); |
| 2280 | 32× | for k in self.same_place(&expected.node, placement) { |
| 2281 | 0× | self.tree_ops.push(TreeOp::RemoveNode { |
| 2282 | 0× | node: expected.node, |
| 2283 | 0× | placement: op_ref(k), |
| 2284 | 0× | }); |
| 2285 | 0× | } |
| 2286 | 32× | self.update_path(repo, old, e, expected)?; |
| 2287 | | |
| 2288 | 32× | self.statuses.retain(|s| s.path != old); |
| 2289 | 32× | self.statuses.push(PathStatus { |
| 2290 | 32× | path: new.to_string(), |
| 2291 | 32× | status: ChangeStatus::Renamed, |
| 2292 | 32× | }); |
| 2293 | 32× | Ok(()) |
| 2294 | 32× | } |
| 2295 | |
|
| 2296 | | |
| 2297 | | |
| 2298 | | |
| 2299 | | |
| 2300 | | |
| 2301 | 88× | fn retire_stray_placements(&mut self) { |
| 2302 | 88× | let mut retired: HashSet<(ChangeHash, u32)> = HashSet::new(); |
| 2303 | 88× | let mut placed: BTreeSet<NodeId> = BTreeSet::new(); |
| 2304 | 99× | for op in &self.tree_ops { |
| 2305 | 99× | match op { |
| 2306 | 19× | TreeOp::Move { node, old, .. } => { |
| 2307 | 19× | retired.insert((old.change, old.index)); |
| 2308 | 19× | placed.insert(*node); |
| 2309 | 19× | } |
| 2310 | 21× | TreeOp::RemoveNode { placement, .. } => { |
| 2311 | 21× | retired.insert((placement.change, placement.index)); |
| 2312 | 21× | } |
| 2313 | 59× | TreeOp::AddNode { .. } | TreeOp::SetMode { .. } => {} |
| 2314 | | } |
| 2315 | | } |
| 2316 | | |
| 2317 | | |
| 2318 | 88× | let mut kept: HashMap<NodeId, (ChangeHash, u32)> = HashMap::new(); |
| 2319 | 241× | for e in self.base_snapshot.tree.entries.values() { |
| 2320 | 241× | if !retired.contains(&e.placement) { |
| 2321 | 204× | kept.insert(e.node, e.placement); |
| 2322 | 204× | } |
| 2323 | | } |
| 2324 | | |
| 2325 | | |
| 2326 | 88× | let multi: BTreeSet<NodeId> = self |
| 2327 | 88× | .base_snapshot |
| 2328 | 88× | .tree |
| 2329 | 88× | .conflicts |
| 2330 | 88× | .iter() |
| 2331 | 88× | .filter_map(|c| match c { |
| 2332 | 1× | crate::tree::TreeConflict::MultiPlacement { node, .. } => Some(*node), |
| 2333 | 6× | _ => None, |
| 2334 | 7× | }) |
| 2335 | 88× | .collect(); |
| 2336 | 88× | let mut extra = Vec::new(); |
| 2337 | | |
| 2338 | | |
| 2339 | | |
| 2340 | 88× | for c in &self.base_snapshot.tree.conflicts { |
| 2341 | 7× | let crate::tree::TreeConflict::Orphan { node, .. } = c else { |
| 2342 | 6× | continue; |
| 2343 | | }; |
| 2344 | 1× | if kept.contains_key(node) || placed.contains(node) { |
| 2345 | 0× | continue; |
| 2346 | 1× | } |
| 2347 | 1× | for (k, _) in self.base.tree().live_placements(node) { |
| 2348 | 1× | if retired.insert(k) { |
| 2349 | 1× | extra.push(TreeOp::RemoveNode { |
| 2350 | 1× | node: *node, |
| 2351 | 1× | placement: op_ref(k), |
| 2352 | 1× | }); |
| 2353 | 1× | } |
| 2354 | | } |
| 2355 | | } |
| 2356 | 88× | for node in placed |
| 2357 | 88× | .iter() |
| 2358 | 88× | .chain(multi.iter().filter(|n| kept.contains_key(*n))) |
| 2359 | | { |
| 2360 | 19× | for (k, _) in self.base.tree().live_placements(node) { |
| 2361 | 18× | if retired.contains(&k) || kept.get(node) == Some(&k) { |
| 2362 | 17× | continue; |
| 2363 | 1× | } |
| 2364 | 1× | retired.insert(k); |
| 2365 | 1× | extra.push(TreeOp::RemoveNode { |
| 2366 | 1× | node: *node, |
| 2367 | 1× | placement: op_ref(k), |
| 2368 | 1× | }); |
| 2369 | | } |
| 2370 | | } |
| 2371 | 88× | if !extra.is_empty() { |
| 2372 | 2× | self.resolves = true; |
| 2373 | 2× | self.tree_ops.extend(extra); |
| 2374 | 86× | } |
| 2375 | 88× | } |
| 2376 | |
|
| 2377 | | |
| 2378 | | |
| 2379 | | |
| 2380 | | |
| 2381 | | |
| 2382 | 164× | fn same_place(&self, node: &NodeId, k: (ChangeHash, u32)) -> Vec<(ChangeHash, u32)> { |
| 2383 | 164× | let tree = self.base.tree(); |
| 2384 | 164× | let Some(shown) = tree.placement(&k) else { |
| 2385 | 0× | return Vec::new(); |
| 2386 | | }; |
| 2387 | 164× | tree.live_placements(node) |
| 2388 | 164× | .into_iter() |
| 2389 | 166× | .filter(|(o, p)| *o != k && p.parent == shown.parent && p.name == shown.name) |
| 2390 | 164× | .map(|(o, _)| o) |
| 2391 | 164× | .collect() |
| 2392 | 164× | } |
| 2393 | |
|
| 2394 | 132× | fn remove_path(&mut self, path: &str, expected: &crate::state::FileState) { |
| 2395 | 132× | let entry = &self.base_snapshot.tree.entries[path]; |
| 2396 | 132× | let placement = entry.placement; |
| 2397 | 132× | self.tree_ops.push(TreeOp::RemoveNode { |
| 2398 | 132× | node: expected.node, |
| 2399 | 132× | placement: op_ref(placement), |
| 2400 | 132× | }); |
| 2401 | 132× | for k in self.same_place(&expected.node, placement) { |
| 2402 | 1× | self.tree_ops.push(TreeOp::RemoveNode { |
| 2403 | 1× | node: expected.node, |
| 2404 | 1× | placement: op_ref(k), |
| 2405 | 1× | }); |
| 2406 | 1× | } |
| 2407 | 132× | if path.contains(".oxy-conflict-") || expected.is_conflicted() { |
| 2408 | 7× | self.resolves = true; |
| 2409 | 125× | } |
| 2410 | 132× | self.statuses.push(PathStatus { |
| 2411 | 132× | path: path.to_string(), |
| 2412 | 132× | status: ChangeStatus::Removed, |
| 2413 | 132× | }); |
| 2414 | 132× | } |
| 2415 | |
|
| 2416 | 12696× | fn update_path( |
| 2417 | 12696× | &mut self, |
| 2418 | 12696× | repo: &Repo, |
| 2419 | 12696× | path: &str, |
| 2420 | 12696× | e: &FsEntry, |
| 2421 | 12696× | expected: &crate::state::FileState, |
| 2422 | 12696× | ) -> Result<()> { |
| 2423 | 12696× | if expected.kind != e.kind { |
| 2424 | 1× | self.remove_path(path, expected); |
| 2425 | 1× | self.statuses.pop(); |
| 2426 | 1× | self.add_path(repo, path, e)?; |
| 2427 | 1× | self.statuses.pop(); |
| 2428 | 1× | self.statuses.push(PathStatus { |
| 2429 | 1× | path: path.to_string(), |
| 2430 | 1× | status: ChangeStatus::Modified, |
| 2431 | 1× | }); |
| 2432 | 1× | return Ok(()); |
| 2433 | 12695× | } |
| 2434 | 12695× | let entry = &self.base_snapshot.tree.entries[path]; |
| 2435 | 12695× | let node = expected.node; |
| 2436 | 12695× | let mode_atom = entry.mode_atom.map(op_ref); |
| 2437 | 12695× | if e.unchanged { |
| 2438 | | |
| 2439 | | |
| 2440 | 4245× | if e.exec != expected.mode.exec { |
| 2441 | 3× | if let Some(old) = mode_atom { |
| 2442 | 3× | self.tree_ops.push(TreeOp::SetMode { |
| 2443 | 3× | node, |
| 2444 | 3× | old, |
| 2445 | 3× | mode: Mode { |
| 2446 | 3× | exec: e.exec, |
| 2447 | 3× | opaque: false, |
| 2448 | 3× | }, |
| 2449 | 3× | }); |
| 2450 | 3× | self.statuses.push(PathStatus { |
| 2451 | 3× | path: path.to_string(), |
| 2452 | 3× | status: ChangeStatus::ModeChanged, |
| 2453 | 3× | }); |
| 2454 | 3× | } |
| 2455 | 4242× | } |
| 2456 | 4245× | return Ok(()); |
| 2457 | 8450× | } |
| 2458 | 8450× | let mut status: Option<ChangeStatus> = None; |
| 2459 | 8450× | let wants_opaque = self.opaque_for(path, e); |
| 2460 | 8450× | let nocompress = self.attrs.lookup(path).nocompress; |
| 2461 | 8450× | let disk_text = self.stored_text(path, &e.content); |
| 2462 | 8450× | match &expected.content { |
| 2463 | 0× | FileContent::Dir => {} |
| 2464 | | FileContent::Text { |
| 2465 | 8226× | bytes, |
| 2466 | 8226× | materialized, |
| 2467 | | } => { |
| 2468 | 8226× | if materialized.is_conflicted() { |
| 2469 | 33× | if bytes == &*disk_text { |
| 2470 | 6× | return Ok(()); |
| 2471 | 27× | } |
| 2472 | 27× | if has_conflict_markers(&e.content) { |
| 2473 | 4× | self.statuses.push(PathStatus { |
| 2474 | 4× | path: path.to_string(), |
| 2475 | 4× | status: ChangeStatus::ConflictEdited, |
| 2476 | 4× | }); |
| 2477 | 4× | return Ok(()); |
| 2478 | 23× | } |
| 2479 | | |
| 2480 | | |
| 2481 | | |
| 2482 | | |
| 2483 | 23× | let regions = self.base.conflict_regions(&node); |
| 2484 | 23× | let mut edits = text_edits(node, ®ions.plain, &disk_text); |
| 2485 | 23× | if !regions.region_atoms.is_empty() { |
| 2486 | 23× | edits.insert( |
| 2487 | 23× | 0, |
| 2488 | 23× | Edit::Delete { |
| 2489 | 23× | node, |
| 2490 | 23× | atoms: ranges(®ions.region_atoms), |
| 2491 | 23× | }, |
| 2492 | 23× | ); |
| 2493 | 23× | } |
| 2494 | 23× | self.edits.extend(edits); |
| 2495 | 23× | self.contributors.extend(regions.contributors); |
| 2496 | 23× | self.resolves = true; |
| 2497 | 23× | if e.exec != expected.mode.exec { |
| 2498 | 0× | if let Some(old) = mode_atom { |
| 2499 | 0× | self.tree_ops.push(TreeOp::SetMode { |
| 2500 | 0× | node, |
| 2501 | 0× | old, |
| 2502 | 0× | mode: Mode { |
| 2503 | 0× | exec: e.exec, |
| 2504 | 0× | opaque: false, |
| 2505 | 0× | }, |
| 2506 | 0× | }); |
| 2507 | 0× | } |
| 2508 | 23× | } |
| 2509 | 23× | self.statuses.push(PathStatus { |
| 2510 | 23× | path: path.to_string(), |
| 2511 | 23× | status: ChangeStatus::Resolved, |
| 2512 | 23× | }); |
| 2513 | 23× | return Ok(()); |
| 2514 | 8193× | } |
| 2515 | 8193× | if wants_opaque { |
| 2516 | | |
| 2517 | 2× | if let Some(old) = mode_atom { |
| 2518 | 2× | self.tree_ops.push(TreeOp::SetMode { |
| 2519 | 2× | node, |
| 2520 | 2× | old, |
| 2521 | 2× | mode: Mode { |
| 2522 | 2× | exec: e.exec, |
| 2523 | 2× | opaque: true, |
| 2524 | 2× | }, |
| 2525 | 2× | }); |
| 2526 | 2× | } |
| 2527 | 2× | let live: Vec<_> = repo_live_keys(self.base, &node); |
| 2528 | 2× | if !live.is_empty() { |
| 2529 | 2× | self.edits.push(Edit::Delete { |
| 2530 | 2× | node, |
| 2531 | 2× | atoms: ranges(&live), |
| 2532 | 2× | }); |
| 2533 | 2× | } |
| 2534 | 2× | let blob = self.blob_of(repo, path, e, nocompress)?; |
| 2535 | 2× | self.edits.push(Edit::Replace { |
| 2536 | 2× | node, |
| 2537 | 2× | old: None, |
| 2538 | 2× | new: Some(blob), |
| 2539 | 2× | }); |
| 2540 | 2× | status = Some(ChangeStatus::Modified); |
| 2541 | 8191× | } else if bytes != &*disk_text { |
| 2542 | 778× | let mut laps = crate::timing::Laps::start(); |
| 2543 | | |
| 2544 | | |
| 2545 | 778× | let edits = match self.diffs.remove(path) { |
| 2546 | 80× | Some(edits) => edits, |
| 2547 | | None => { |
| 2548 | 698× | let keys = materialized.live_atoms(); |
| 2549 | 698× | let lines = crate::diff::split_lines(bytes); |
| 2550 | 698× | if keys.len() == lines.len() { |
| 2551 | 698× | crate::diff::text_edits_of(node, &keys, &lines, &disk_text) |
| 2552 | | } else { |
| 2553 | 0× | text_edits(node, &self.base.live_lines(&node), &disk_text) |
| 2554 | | } |
| 2555 | | } |
| 2556 | | }; |
| 2557 | 778× | laps.lap("build: text diff"); |
| 2558 | 778× | if !edits.is_empty() { |
| 2559 | 778× | self.edits.extend(edits); |
| 2560 | 778× | status = Some(ChangeStatus::Modified); |
| 2561 | 778× | } |
| 2562 | 7413× | } |
| 2563 | 8193× | let switched_to_opaque = wants_opaque; |
| 2564 | 8193× | if !switched_to_opaque && e.exec != expected.mode.exec { |
| 2565 | 4× | if let Some(old) = mode_atom { |
| 2566 | 4× | self.tree_ops.push(TreeOp::SetMode { |
| 2567 | 4× | node, |
| 2568 | 4× | old, |
| 2569 | 4× | mode: Mode { |
| 2570 | 4× | exec: e.exec, |
| 2571 | 4× | opaque: false, |
| 2572 | 4× | }, |
| 2573 | 4× | }); |
| 2574 | 4× | status.get_or_insert(ChangeStatus::ModeChanged); |
| 2575 | 4× | } |
| 2576 | 8189× | } |
| 2577 | | } |
| 2578 | 224× | FileContent::Opaque { blobs } => { |
| 2579 | 224× | let new_ref = e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)); |
| 2580 | 224× | if e.kind == NodeKind::File && wants_opaque { |
| 2581 | 191× | |
| 2582 | 191× | self.blobs.insert(path.to_string(), new_ref); |
| 2583 | 191× | } |
| 2584 | 224× | if blobs.len() > 1 && self.resolve.contains(path) && e.kind == expected.kind { |
| 2585 | | |
| 2586 | | |
| 2587 | | |
| 2588 | 2× | let chosen = self.blob_of(repo, path, e, nocompress)?; |
| 2589 | 4× | for b in blobs { |
| 2590 | 4× | self.edits.push(Edit::Replace { |
| 2591 | 4× | node, |
| 2592 | 4× | old: Some(*b), |
| 2593 | 4× | new: Some(chosen), |
| 2594 | 4× | }); |
| 2595 | 4× | } |
| 2596 | 2× | self.contributors |
| 2597 | 2× | .extend(self.base.blob_contributors(&node, blobs)); |
| 2598 | 2× | self.resolves = true; |
| 2599 | 2× | self.statuses.push(PathStatus { |
| 2600 | 2× | path: path.to_string(), |
| 2601 | 2× | status: ChangeStatus::Resolved, |
| 2602 | 2× | }); |
| 2603 | 2× | return Ok(()); |
| 2604 | 222× | } |
| 2605 | 222× | if blobs.len() > 1 { |
| 2606 | 2× | if !blobs.contains(&new_ref) { |
| 2607 | 0× | self.statuses.push(PathStatus { |
| 2608 | 0× | path: path.to_string(), |
| 2609 | 0× | status: ChangeStatus::ConflictEdited, |
| 2610 | 0× | }); |
| 2611 | 2× | } |
| 2612 | 2× | return Ok(()); |
| 2613 | 220× | } |
| 2614 | 220× | let current = blobs.first().copied(); |
| 2615 | 220× | if current != Some(new_ref) { |
| 2616 | 34× | if e.kind == NodeKind::File && !wants_opaque { |
| 2617 | | |
| 2618 | 2× | if let Some(old) = mode_atom { |
| 2619 | 2× | self.tree_ops.push(TreeOp::SetMode { |
| 2620 | 2× | node, |
| 2621 | 2× | old, |
| 2622 | 2× | mode: Mode { |
| 2623 | 2× | exec: e.exec, |
| 2624 | 2× | opaque: false, |
| 2625 | 2× | }, |
| 2626 | 2× | }); |
| 2627 | 2× | } |
| 2628 | 2× | self.edits.push(Edit::Replace { |
| 2629 | 2× | node, |
| 2630 | 2× | old: current, |
| 2631 | 2× | new: None, |
| 2632 | 2× | }); |
| 2633 | 2× | if !e.content.is_empty() { |
| 2634 | 2× | self.edits.push(Edit::Insert { |
| 2635 | 2× | node, |
| 2636 | 2× | after: crate::objects::AtomRef::Start, |
| 2637 | 2× | before: crate::objects::AtomRef::End, |
| 2638 | 2× | lines: crate::lines::Lines::split(&e.content), |
| 2639 | 2× | }); |
| 2640 | 2× | } |
| 2641 | | } else { |
| 2642 | 32× | let blob = self.blob_of(repo, path, e, nocompress)?; |
| 2643 | 32× | self.edits.push(Edit::Replace { |
| 2644 | 32× | node, |
| 2645 | 32× | old: current, |
| 2646 | 32× | new: Some(blob), |
| 2647 | 32× | }); |
| 2648 | | |
| 2649 | | |
| 2650 | | |
| 2651 | 32× | if e.kind == NodeKind::File && e.exec != expected.mode.exec { |
| 2652 | 2× | if let Some(old) = mode_atom { |
| 2653 | 2× | self.tree_ops.push(TreeOp::SetMode { |
| 2654 | 2× | node, |
| 2655 | 2× | old, |
| 2656 | 2× | mode: Mode { |
| 2657 | 2× | exec: e.exec, |
| 2658 | 2× | opaque: true, |
| 2659 | 2× | }, |
| 2660 | 2× | }); |
| 2661 | 2× | } |
| 2662 | 30× | } |
| 2663 | | } |
| 2664 | 34× | status = Some(ChangeStatus::Modified); |
| 2665 | 186× | } else if e.kind == NodeKind::File && e.exec != expected.mode.exec { |
| 2666 | 0× | if let Some(old) = mode_atom { |
| 2667 | 0× | self.tree_ops.push(TreeOp::SetMode { |
| 2668 | 0× | node, |
| 2669 | 0× | old, |
| 2670 | 0× | mode: Mode { |
| 2671 | 0× | exec: e.exec, |
| 2672 | 0× | opaque: true, |
| 2673 | 0× | }, |
| 2674 | 0× | }); |
| 2675 | 0× | status = Some(ChangeStatus::ModeChanged); |
| 2676 | 0× | } |
| 2677 | 186× | } |
| 2678 | | } |
| 2679 | | } |
| 2680 | 8413× | if let Some(s) = status { |
| 2681 | 818× | self.statuses.push(PathStatus { |
| 2682 | 818× | path: path.to_string(), |
| 2683 | 818× | status: s, |
| 2684 | 818× | }); |
| 2685 | 7595× | } |
| 2686 | 8413× | Ok(()) |
| 2687 | 12696× | } |
| 2688 | | } |
| 2689 | |
|
| 2690 | 2× | fn repo_live_keys(base: &State, node: &NodeId) -> Vec<crate::graph::AtomKey> { |
| 2691 | 2× | base.live_lines(node).into_iter().map(|(k, _)| k).collect() |
| 2692 | 2× | } |
| 2693 | |
|
| 2694 | |
|
| 2695 | |
|
| 2696 | 2041× | pub fn referenced_changes(change: &Change, base: &State) -> BTreeSet<ChangeHash> { |
| 2697 | 2041× | let mut out = BTreeSet::new(); |
| 2698 | 2041× | let opref = |r: &OpRef, out: &mut BTreeSet<ChangeHash>| { |
| 2699 | 193× | if !r.change.is_zero() { |
| 2700 | 193× | out.insert(r.change); |
| 2701 | 193× | } |
| 2702 | 193× | }; |
| 2703 | 2041× | let mut nodes: Vec<NodeId> = Vec::new(); |
| 2704 | 6560× | for op in &change.tree_ops { |
| 2705 | 6560× | match op { |
| 2706 | 6367× | TreeOp::AddNode { parent, .. } => nodes.push(*parent), |
| 2707 | 147× | TreeOp::RemoveNode { node, placement } => { |
| 2708 | 147× | nodes.push(*node); |
| 2709 | 147× | opref(placement, &mut out); |
| 2710 | 147× | } |
| 2711 | | TreeOp::Move { |
| 2712 | 34× | node, old, parent, .. |
| 2713 | 34× | } => { |
| 2714 | 34× | nodes.push(*node); |
| 2715 | 34× | nodes.push(*parent); |
| 2716 | 34× | opref(old, &mut out); |
| 2717 | 34× | } |
| 2718 | 12× | TreeOp::SetMode { node, old, .. } => { |
| 2719 | 12× | nodes.push(*node); |
| 2720 | 12× | opref(old, &mut out); |
| 2721 | 12× | } |
| 2722 | | } |
| 2723 | | } |
| 2724 | 8079× | for e in &change.edits { |
| 2725 | 8079× | nodes.push(e.node()); |
| 2726 | 134× | match e { |
| 2727 | 7420× | Edit::Insert { after, before, .. } => { |
| 2728 | 14840× | for r in [after, before] { |
| 2729 | 14840× | if let crate::objects::AtomRef::Atom { change, .. } = r { |
| 2730 | 2134× | if !change.is_zero() { |
| 2731 | 2132× | out.insert(*change); |
| 2732 | 2132× | } |
| 2733 | 12706× | } |
| 2734 | | } |
| 2735 | | } |
| 2736 | 525× | Edit::Delete { atoms, .. } => { |
| 2737 | | |
| 2738 | | |
| 2739 | | |
| 2740 | | |
| 2741 | 525× | let graph = base.file(&e.node()); |
| 2742 | 1074× | for r in atoms { |
| 2743 | 1074× | if !r.change.is_zero() { |
| 2744 | 1073× | out.insert(r.change); |
| 2745 | 1073× | } |
| 2746 | 1074× | if let Some(g) = graph { |
| 2747 | 1097× | for i in r.from..r.to { |
| 2748 | 1097× | out.extend( |
| 2749 | 1097× | g.live_successor_changes(&(r.change, i)) |
| 2750 | 1097× | .into_iter() |
| 2751 | 1097× | .filter(|c| !c.is_zero()), |
| 2752 | | ); |
| 2753 | | } |
| 2754 | 0× | } |
| 2755 | | } |
| 2756 | | } |
| 2757 | | |
| 2758 | | |
| 2759 | | Edit::Replace { |
| 2760 | 39× | node, |
| 2761 | 39× | old: Some(old), |
| 2762 | | .. |
| 2763 | | } => { |
| 2764 | 39× | out.extend( |
| 2765 | 39× | base.live_replaces(node) |
| 2766 | 39× | .into_iter() |
| 2767 | 46× | .filter(|(c, b)| b == old && !c.is_zero()) |
| 2768 | 39× | .map(|(c, _)| c), |
| 2769 | | ); |
| 2770 | | } |
| 2771 | 95× | Edit::Replace { .. } => {} |
| 2772 | | } |
| 2773 | | } |
| 2774 | 14673× | for n in nodes { |
| 2775 | 14673× | if let Some(h) = base.tree().introducer(&n) { |
| 2776 | 2169× | out.insert(h); |
| 2777 | 12504× | } |
| 2778 | | } |
| 2779 | 2041× | out |
| 2780 | 2041× | } |
| 2781 | |
|
| 2782 | |
|
| 2783 | 1276× | pub fn added_node_paths( |
| 2784 | 1276× | change: &Change, |
| 2785 | 1276× | base_snapshot: &Snapshot, |
| 2786 | 1276× | ) -> HashMap<String, (NodeId, NodeKind)> { |
| 2787 | 1276× | let by_node = base_snapshot.tree.by_node(); |
| 2788 | 1276× | let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 2789 | 1276× | for op in &change.tree_ops { |
| 2790 | | if let TreeOp::AddNode { |
| 2791 | 79× | node, |
| 2792 | 79× | parent, |
| 2793 | 79× | name, |
| 2794 | 79× | kind, |
| 2795 | | .. |
| 2796 | 102× | } = op |
| 2797 | 79× | { |
| 2798 | 79× | new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 2799 | 79× | } |
| 2800 | | } |
| 2801 | 1276× | let mut out = HashMap::new(); |
| 2802 | 1276× | for (node, (_, _, kind)) in &new_names { |
| 2803 | 79× | if let Some(path) = path_of_node(node, &by_node, &new_names) { |
| 2804 | 79× | out.insert(path, (*node, *kind)); |
| 2805 | 79× | } |
| 2806 | | } |
| 2807 | 1276× | out |
| 2808 | 1276× | } |
| 2809 | |
|
| 2810 | 188× | fn path_of_node( |
| 2811 | 188× | n: &NodeId, |
| 2812 | 188× | by_node: &HashMap<NodeId, &crate::tree::TreeEntry>, |
| 2813 | 188× | new_names: &HashMap<NodeId, (NodeId, String, NodeKind)>, |
| 2814 | 188× | ) -> Option<String> { |
| 2815 | 188× | if let Some(e) = by_node.get(n) { |
| 2816 | 57× | return Some(e.path.clone()); |
| 2817 | 131× | } |
| 2818 | 131× | let mut parts = Vec::new(); |
| 2819 | 131× | let mut cur = *n; |
| 2820 | | loop { |
| 2821 | 261× | if cur.is_root() { |
| 2822 | 117× | break; |
| 2823 | 144× | } |
| 2824 | 144× | if let Some(e) = by_node.get(&cur) { |
| 2825 | 0× | if !e.path.is_empty() { |
| 2826 | 0× | parts.push(e.path.clone()); |
| 2827 | 0× | } |
| 2828 | 0× | break; |
| 2829 | 144× | } |
| 2830 | 144× | let (parent, name, _) = new_names.get(&cur)?; |
| 2831 | 130× | parts.push(name.clone()); |
| 2832 | 130× | cur = *parent; |
| 2833 | | } |
| 2834 | 117× | parts.reverse(); |
| 2835 | 117× | Some(parts.join("/")) |
| 2836 | 188× | } |
| 2837 | |
|
| 2838 | |
|
| 2839 | 28× | pub fn render_change(change: &Change, base: &State, base_snapshot: &Snapshot) -> String { |
| 2840 | 28× | let by_node = base_snapshot.tree.by_node(); |
| 2841 | 28× | let mut new_names: HashMap<NodeId, (NodeId, String, NodeKind)> = HashMap::new(); |
| 2842 | 28× | for op in &change.tree_ops { |
| 2843 | | if let TreeOp::AddNode { |
| 2844 | 15× | node, |
| 2845 | 15× | parent, |
| 2846 | 15× | name, |
| 2847 | 15× | kind, |
| 2848 | | .. |
| 2849 | 22× | } = op |
| 2850 | 15× | { |
| 2851 | 15× | new_names.insert(*node, (*parent, name.clone(), *kind)); |
| 2852 | 15× | } |
| 2853 | | } |
| 2854 | 66× | let path_of = |n: &NodeId| -> String { |
| 2855 | 66× | path_of_node(n, &by_node, &new_names).unwrap_or_else(|| format!("<node {}>", n.short(8))) |
| 2856 | 66× | }; |
| 2857 | |
|
| 2858 | 28× | let mut out = String::new(); |
| 2859 | 28× | for op in &change.tree_ops { |
| 2860 | 22× | match op { |
| 2861 | | TreeOp::AddNode { |
| 2862 | 15× | node, kind, mode, .. |
| 2863 | | } => { |
| 2864 | 15× | out.push_str(&format!( |
| 2865 | | "A {} ({:?}{}{})\n", |
| 2866 | 15× | path_of(node), |
| 2867 | | kind, |
| 2868 | 15× | if mode.exec { ", exec" } else { "" }, |
| 2869 | 15× | if mode.opaque { ", opaque" } else { "" } |
| 2870 | | )); |
| 2871 | | } |
| 2872 | 5× | TreeOp::RemoveNode { node, .. } => out.push_str(&format!("D {}\n", path_of(node))), |
| 2873 | | TreeOp::Move { |
| 2874 | 2× | node, parent, name, .. |
| 2875 | | } => { |
| 2876 | 2× | let to = if parent.is_root() { |
| 2877 | 0× | name.clone() |
| 2878 | | } else { |
| 2879 | 2× | format!("{}/{name}", path_of(parent)) |
| 2880 | | }; |
| 2881 | 2× | out.push_str(&format!("R {} -> {to}\n", path_of(node))) |
| 2882 | | } |
| 2883 | 0× | TreeOp::SetMode { node, mode, .. } => { |
| 2884 | 0× | out.push_str(&format!( |
| 2885 | 0× | "M {} mode: exec={} opaque={}\n", |
| 2886 | 0× | path_of(node), |
| 2887 | 0× | mode.exec, |
| 2888 | 0× | mode.opaque |
| 2889 | 0× | )); |
| 2890 | 0× | } |
| 2891 | | } |
| 2892 | | } |
| 2893 | | |
| 2894 | | |
| 2895 | 28× | let with_op: HashSet<NodeId> = change.tree_ops.iter().map(|o| o.node()).collect(); |
| 2896 | 28× | let mut listed: HashSet<NodeId> = HashSet::new(); |
| 2897 | 32× | for e in &change.edits { |
| 2898 | 32× | let n = e.node(); |
| 2899 | 32× | if !with_op.contains(&n) && listed.insert(n) { |
| 2900 | 18× | out.push_str(&format!("M {}\n", path_of(&n))); |
| 2901 | 18× | } |
| 2902 | | } |
| 2903 | 28× | let mut last_node: Option<NodeId> = None; |
| 2904 | 32× | for e in &change.edits { |
| 2905 | 32× | let n = e.node(); |
| 2906 | 32× | if last_node != Some(n) { |
| 2907 | 24× | out.push_str(&format!("--- {}\n", path_of(&n))); |
| 2908 | 24× | last_node = Some(n); |
| 2909 | 24× | } |
| 2910 | 32× | match e { |
| 2911 | 24× | Edit::Insert { lines, .. } => { |
| 2912 | 29× | for l in lines.iter() { |
| 2913 | 29× | out.push('+'); |
| 2914 | 29× | out.push_str(&String::from_utf8_lossy(l)); |
| 2915 | 29× | if !l.ends_with(b"\n") { |
| 2916 | 0× | out.push('\n'); |
| 2917 | 29× | } |
| 2918 | | } |
| 2919 | | } |
| 2920 | 7× | Edit::Delete { atoms, .. } => { |
| 2921 | 7× | for r in atoms { |
| 2922 | 7× | for i in r.from..r.to { |
| 2923 | 7× | let content = base |
| 2924 | 7× | .file(&n) |
| 2925 | 7× | .and_then(|g| g.content(&(r.change, i))) |
| 2926 | 7× | .unwrap_or(b"<unknown>\n"); |
| 2927 | 7× | out.push('-'); |
| 2928 | 7× | out.push_str(&String::from_utf8_lossy(content)); |
| 2929 | 7× | if !content.ends_with(b"\n") { |
| 2930 | 0× | out.push('\n'); |
| 2931 | 7× | } |
| 2932 | | } |
| 2933 | | } |
| 2934 | | } |
| 2935 | 1× | Edit::Replace { old, new, .. } => { |
| 2936 | 1× | out.push_str(&format!( |
| 2937 | | " opaque content {} -> {}\n", |
| 2938 | 1× | old.map(|o| o.short(12)).unwrap_or_else(|| "none".into()), |
| 2939 | 1× | new.map(|o| o.short(12)).unwrap_or_else(|| "none".into()) |
| 2940 | | )); |
| 2941 | | } |
| 2942 | | } |
| 2943 | | } |
| 2944 | 28× | out |
| 2945 | 28× | } |
| 2946 | |
|
| 2947 | |
|
| 2948 | |
|
| 2949 | |
|
| 2950 | | const RENAME_THRESHOLD: u32 = 50; |
| 2951 | | const RENAME_LIMIT: usize = 10_000; |
| 2952 | |
|
| 2953 | |
|
| 2954 | |
|
| 2955 | |
|
| 2956 | | #[derive(Default)] |
| 2957 | | struct Scope<'a> { |
| 2958 | | seed: Option<[u8; 32]>, |
| 2959 | | dir_needed: Option<&'a dyn Fn(&str) -> bool>, |
| 2960 | | } |
| 2961 | |
|
| 2962 | 1953× | fn detect_renames( |
| 2963 | 1953× | base_snapshot: &Snapshot, |
| 2964 | 1953× | fs_entries: &BTreeMap<String, FsEntry>, |
| 2965 | 1953× | in_scope: impl Fn(&str) -> bool, |
| 2966 | 1953× | ) -> Vec<(String, String)> { |
| 2967 | 1953× | let removed: Vec<(&String, &crate::state::FileState)> = base_snapshot |
| 2968 | 1953× | .files |
| 2969 | 1953× | .iter() |
| 2970 | 13223× | .filter(|(p, f)| { |
| 2971 | 13223× | f.kind != NodeKind::Dir |
| 2972 | 12810× | && !fs_entries.contains_key(*p) |
| 2973 | 146× | && in_scope(p) |
| 2974 | 136× | && !f.is_conflicted() |
| 2975 | 13223× | }) |
| 2976 | 1953× | .collect(); |
| 2977 | 1953× | let added: Vec<(&String, &FsEntry)> = fs_entries |
| 2978 | 1953× | .iter() |
| 2979 | 18695× | .filter(|(p, e)| e.kind != NodeKind::Dir && !base_snapshot.files.contains_key(*p)) |
| 2980 | 1953× | .collect(); |
| 2981 | 1953× | if removed.is_empty() || added.is_empty() { |
| 2982 | 1898× | return vec![]; |
| 2983 | 55× | } |
| 2984 | 55× | let exact_only = removed.len() * added.len() > RENAME_LIMIT; |
| 2985 | 69× | let old_content = |f: &crate::state::FileState| -> Option<Vec<u8>> { |
| 2986 | 69× | match &f.content { |
| 2987 | 65× | FileContent::Text { bytes, .. } => Some(bytes.clone()), |
| 2988 | 4× | _ => None, |
| 2989 | | } |
| 2990 | 69× | }; |
| 2991 | 55× | let mut scored: Vec<(u32, usize, usize)> = Vec::new(); |
| 2992 | 69× | for (ri, (_, f)) in removed.iter().enumerate() { |
| 2993 | 69× | let old = old_content(f); |
| 2994 | 69× | let old_blob = match &f.content { |
| 2995 | 4× | FileContent::Opaque { blobs } if blobs.len() == 1 => Some(blobs[0]), |
| 2996 | 65× | _ => None, |
| 2997 | | }; |
| 2998 | 88× | for (ai, (_, e)) in added.iter().enumerate() { |
| 2999 | 88× | if e.kind != f.kind { |
| 3000 | 0× | continue; |
| 3001 | 88× | } |
| 3002 | 88× | let score = match (&old, old_blob) { |
| 3003 | 78× | (Some(o), _) => { |
| 3004 | 78× | if exact_only { |
| 3005 | 0× | if *o == *e.content { |
| 3006 | 0× | 100 |
| 3007 | | } else { |
| 3008 | 0× | 0 |
| 3009 | | } |
| 3010 | | } else { |
| 3011 | 78× | similarity(o, &e.content) |
| 3012 | | } |
| 3013 | | } |
| 3014 | 10× | (None, Some(b)) if e.blob.unwrap_or_else(|| Repo::blob_ref(&e.content)) == b => 100, |
| 3015 | 7× | _ => 0, |
| 3016 | | }; |
| 3017 | 88× | if score >= RENAME_THRESHOLD { |
| 3018 | 38× | scored.push((score, ri, ai)); |
| 3019 | 50× | } |
| 3020 | | } |
| 3021 | | } |
| 3022 | 55× | scored.sort_by(|a, b| { |
| 3023 | 9× | b.0.cmp(&a.0) |
| 3024 | 9× | .then(removed[a.1].0.cmp(removed[b.1].0)) |
| 3025 | 9× | .then(added[a.2].0.cmp(added[b.2].0)) |
| 3026 | 9× | }); |
| 3027 | 55× | let mut used_r = vec![false; removed.len()]; |
| 3028 | 55× | let mut used_a = vec![false; added.len()]; |
| 3029 | 55× | let mut out = Vec::new(); |
| 3030 | 55× | for (_, ri, ai) in scored { |
| 3031 | 38× | if used_r[ri] || used_a[ai] { |
| 3032 | 6× | continue; |
| 3033 | 32× | } |
| 3034 | 32× | used_r[ri] = true; |
| 3035 | 32× | used_a[ai] = true; |
| 3036 | 32× | out.push((removed[ri].0.clone(), added[ai].0.clone())); |
| 3037 | | } |
| 3038 | 55× | out |
| 3039 | 1953× | } |
| 3040 | |
|
| 3041 | |
|
| 3042 | |
|
| 3043 | 82× | pub fn similarity(a: &[u8], b: &[u8]) -> u32 { |
| 3044 | 82× | if a == b { |
| 3045 | 22× | return 100; |
| 3046 | 60× | } |
| 3047 | 60× | if a.is_empty() || b.is_empty() { |
| 3048 | 2× | return 0; |
| 3049 | 58× | } |
| 3050 | 58× | let mut counts: HashMap<&[u8], usize> = HashMap::new(); |
| 3051 | 117× | for l in split_lines(a) { |
| 3052 | 117× | *counts.entry(l).or_default() += 1; |
| 3053 | 117× | } |
| 3054 | 58× | let mut shared = 0usize; |
| 3055 | 111× | for l in split_lines(b) { |
| 3056 | 111× | if let Some(n) = counts.get_mut(l) { |
| 3057 | 44× | if *n > 0 { |
| 3058 | 44× | *n -= 1; |
| 3059 | 44× | shared += l.len(); |
| 3060 | 44× | } |
| 3061 | 67× | } |
| 3062 | | } |
| 3063 | 58× | (shared * 100 / a.len().max(b.len())) as u32 |
| 3064 | 82× | } |
| 3065 | |
|
| 3066 | | #[cfg(test)] |
| 3067 | | mod join_tests { |
| 3068 | | use super::*; |
| 3069 | |
|
| 3070 | | #[test] |
| 3071 | 1× | fn missing_from_finds_what_the_other_lacks() { |
| 3072 | 1× | let a: BTreeMap<String, u8> = [("a", 1), ("b", 2), ("c/d", 3), ("e", 4)] |
| 3073 | 1× | .into_iter() |
| 3074 | 4× | .map(|(k, v)| (k.to_string(), v)) |
| 3075 | 1× | .collect(); |
| 3076 | 1× | let b: BTreeMap<String, ()> = [("0", ()), ("b", ()), ("c", ()), ("e", ())] |
| 3077 | 1× | .into_iter() |
| 3078 | 4× | .map(|(k, v)| (k.to_string(), v)) |
| 3079 | 1× | .collect(); |
| 3080 | 1× | let got: Vec<&str> = missing_from(&a, &b) |
| 3081 | 1× | .iter() |
| 3082 | 2× | .map(|(k, _)| k.as_str()) |
| 3083 | 1× | .collect(); |
| 3084 | 1× | assert_eq!(got, vec!["a", "c/d"]); |
| 3085 | 1× | } |
| 3086 | |
|
| 3087 | | #[test] |
| 3088 | 1× | fn join_pairs_every_key_in_order() { |
| 3089 | 1× | let a: BTreeMap<String, u8> = [("a", 1), ("b/c", 2), ("d", 3)] |
| 3090 | 1× | .into_iter() |
| 3091 | 3× | .map(|(k, v)| (k.to_string(), v)) |
| 3092 | 1× | .collect(); |
| 3093 | 1× | let b: BTreeMap<String, char> = [("b", 'x'), ("b/c", 'y'), ("e", 'z')] |
| 3094 | 1× | .into_iter() |
| 3095 | 3× | .map(|(k, v)| (k.to_string(), v)) |
| 3096 | 1× | .collect(); |
| 3097 | 1× | let got: Vec<(String, Option<u8>, Option<char>)> = join(&a, &b) |
| 3098 | 5× | .map(|(k, x, y)| (k.clone(), x.copied(), y.copied())) |
| 3099 | 1× | .collect(); |
| 3100 | 1× | let want = vec![ |
| 3101 | 1× | ("a".to_string(), Some(1), None), |
| 3102 | 1× | ("b".to_string(), None, Some('x')), |
| 3103 | 1× | ("b/c".to_string(), Some(2), Some('y')), |
| 3104 | 1× | ("d".to_string(), Some(3), None), |
| 3105 | 1× | ("e".to_string(), None, Some('z')), |
| 3106 | | ]; |
| 3107 | 1× | assert_eq!(got, want); |
| 3108 | 1× | } |
| 3109 | | } |