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