| 1 | |
|
| 2 | |
|
| 3 | | use super::*; |
| 4 | |
|
| 5 | | #[derive(Debug, Default, Clone, PartialEq, Eq)] |
| 6 | | pub struct CheckoutReport { |
| 7 | | pub written: usize, |
| 8 | | pub removed: usize, |
| 9 | | |
| 10 | | pub removed_paths: Vec<String>, |
| 11 | | pub conflicted: bool, |
| 12 | | |
| 13 | | pub notes: Vec<String>, |
| 14 | | |
| 15 | | pub lacks: usize, |
| 16 | | } |
| 17 | |
|
| 18 | |
|
| 19 | | const CHECKOUT_BATCH: usize = 2048; |
| 20 | |
|
| 21 | | impl Repo { |
| 22 | | |
| 23 | | |
| 24 | | |
| 25 | 542× | pub fn checkout( |
| 26 | 542× | &self, |
| 27 | 542× | heads: &[ChangeHash], |
| 28 | 542× | discard: bool, |
| 29 | 542× | command: &str, |
| 30 | 542× | ) -> Result<CheckoutReport> { |
| 31 | 542× | self.checkout_keeping(heads, discard, command, &BTreeSet::new()) |
| 32 | 542× | } |
| 33 | |
|
| 34 | | |
| 35 | | |
| 36 | 547× | pub(crate) fn checkout_keeping( |
| 37 | 547× | &self, |
| 38 | 547× | heads: &[ChangeHash], |
| 39 | 547× | discard: bool, |
| 40 | 547× | command: &str, |
| 41 | 547× | keep_subrepos: &BTreeSet<String>, |
| 42 | 547× | ) -> Result<CheckoutReport> { |
| 43 | 547× | let _wc = self.lock_working_copy()?; |
| 44 | 547× | let _all = crate::timing::phase("checkout"); |
| 45 | | |
| 46 | | |
| 47 | | |
| 48 | | |
| 49 | | |
| 50 | 547× | let changed_paths: Vec<String> = if self.clean_by_manifest()? { |
| 51 | 221× | Vec::new() |
| 52 | | } else { |
| 53 | 326× | let snap = self.snapshot_status(command)?; |
| 54 | 326× | self.refuse_dirty(&snap, discard)?; |
| 55 | 324× | snap.statuses.iter().map(|s| s.path.clone()).collect() |
| 56 | | }; |
| 57 | 545× | let (_, wc_now) = self.working_copy()?; |
| 58 | 545× | let old_manifest = self.read_manifest()?.map(|(_, _, m)| m).unwrap_or_default(); |
| 59 | 545× | let changed: HashSet<&str> = changed_paths.iter().map(String::as_str).collect(); |
| 60 | | |
| 61 | | |
| 62 | | |
| 63 | 545× | let mut target = self.load_state_indexed(heads)?; |
| 64 | 545× | let heads = target.maximal(heads); |
| 65 | 545× | let tv = crate::timing::phase("checkout: target view"); |
| 66 | 545× | let tview = self.tree_view(&mut target)?; |
| 67 | 545× | drop(tv); |
| 68 | 545× | let mut report = CheckoutReport::default(); |
| 69 | | |
| 70 | 545× | report.notes.extend( |
| 71 | 545× | self.platform |
| 72 | 545× | .problems(tview.entries.keys().map(String::as_str)), |
| 73 | | ); |
| 74 | 545× | report.conflicted = tview.is_conflicted(); |
| 75 | 545× | report.lacks = self.count_lacking(&wc_now.deps, &target)?; |
| 76 | |
|
| 77 | | |
| 78 | | |
| 79 | 545× | let mut dirs_to_prune: BTreeSet<PathBuf> = BTreeSet::new(); |
| 80 | 545× | let gone: BTreeSet<&str> = old_manifest |
| 81 | 545× | .entries |
| 82 | 545× | .keys() |
| 83 | 545× | .map(String::as_str) |
| 84 | 545× | .chain(changed.iter().copied()) |
| 85 | 7768× | .filter(|p| !tview.entries.contains_key(*p)) |
| 86 | 545× | .collect(); |
| 87 | 3072× | for path in gone { |
| 88 | 3072× | let dir = self.root.join(path); |
| 89 | 3072× | if dir.join(OXY_DIR).exists() || dir.join(".git").exists() { |
| 90 | 2× | report |
| 91 | 2× | .notes |
| 92 | 2× | .push(format!("{path}: nested repository left in place")); |
| 93 | 2× | continue; |
| 94 | 3070× | } |
| 95 | 3070× | let full = self.safe_path(path)?; |
| 96 | 3070× | if fs::symlink_metadata(&full).is_err() { |
| 97 | 1× | continue; |
| 98 | 3069× | } |
| 99 | 3069× | fs::remove_file(&full)?; |
| 100 | 3069× | report.removed += 1; |
| 101 | 3069× | if report.removed_paths.len() < 5 { |
| 102 | 119× | report.removed_paths.push(path.to_string()); |
| 103 | 2950× | } |
| 104 | 3069× | if let Some(parent) = full.parent() { |
| 105 | 3069× | dirs_to_prune.insert(parent.to_path_buf()); |
| 106 | 3069× | } |
| 107 | | } |
| 108 | |
|
| 109 | | |
| 110 | 545× | let plan = crate::timing::phase("checkout: plan"); |
| 111 | 545× | let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 112 | 5917× | let hex = |b: &[u8]| *blake3::hash(b).as_bytes(); |
| 113 | | |
| 114 | 545× | let mut texts: Vec<(String, NodeId, Mode)> = Vec::new(); |
| 115 | 10611× | for (path, e) in tview.entries.iter() { |
| 116 | 10611× | if e.kind == NodeKind::Dir || !self.in_scope(path) { |
| 117 | 213× | continue; |
| 118 | 10398× | } |
| 119 | | |
| 120 | | |
| 121 | 10398× | let exec_ok = |
| 122 | 4656× | |m: &prompt::ManifestEntry| !self.platform.exec_bits || m.exec == e.mode.exec; |
| 123 | 10398× | let entry = old_manifest |
| 124 | 10398× | .entries |
| 125 | 10398× | .get(path) |
| 126 | 10398× | .filter(|m| !changed.contains(path.as_str()) && exec_ok(m)); |
| 127 | 10398× | if e.kind == NodeKind::File && !e.mode.opaque { |
| 128 | | |
| 129 | | |
| 130 | | |
| 131 | 10348× | let key = target.graph_key(&e.node); |
| 132 | 10348× | let proven = |m: &&prompt::ManifestEntry| { |
| 133 | 4628× | !is_attributes(path) && key.is_some() && m.graph == key |
| 134 | 4628× | }; |
| 135 | 10348× | match entry.filter(proven) { |
| 136 | 4452× | Some(m) => { |
| 137 | 4452× | entries.insert( |
| 138 | 4452× | path.clone(), |
| 139 | 4452× | FsEntry { |
| 140 | 4452× | content_hash: Some(m.hash), |
| 141 | 4452× | graph_key: m.graph, |
| 142 | 4452× | unchanged: true, |
| 143 | 4452× | stat: Some((m.size, m.mtime_ns)), |
| 144 | 4452× | ..FsEntry::new(NodeKind::File, e.mode.exec, Vec::new()) |
| 145 | 4452× | }, |
| 146 | 4452× | ); |
| 147 | 4452× | } |
| 148 | 5896× | None => texts.push((path.clone(), e.node, e.mode)), |
| 149 | | } |
| 150 | 10348× | continue; |
| 151 | 50× | } |
| 152 | | |
| 153 | 50× | let blobs = target.live_blobs(&e.node); |
| 154 | 50× | if blobs.len() > 1 { |
| 155 | 1× | report.conflicted = true; |
| 156 | 49× | } |
| 157 | 50× | let Some(blob) = blobs.first().copied() else { |
| 158 | 0× | continue; |
| 159 | | }; |
| 160 | 50× | if e.kind == NodeKind::Subrepo { |
| 161 | 10× | if keep_subrepos.contains(path) { |
| 162 | 1× | continue; |
| 163 | 9× | } |
| 164 | 9× | let pin = crate::subrepo::Pin::decode(&self.read_blob(&blob)?)?; |
| 165 | 9× | let sync = self.sync_subrepo(path, &pin, false)?; |
| 166 | 8× | if matches!( |
| 167 | 9× | sync, |
| 168 | | SubrepoSync::Updated | SubrepoSync::KeptBookmark { .. } |
| 169 | 1× | ) { |
| 170 | 1× | report.written += 1; |
| 171 | 8× | } |
| 172 | 9× | report.notes.extend(sync.note(path)); |
| 173 | 9× | continue; |
| 174 | 40× | } |
| 175 | 40× | if let Some(m) = entry |
| 176 | 40× | .filter(|m| e.kind == NodeKind::File && blobs.len() == 1 && m.blob == Some(blob)) |
| 177 | | { |
| 178 | 17× | entries.insert( |
| 179 | 17× | path.clone(), |
| 180 | 17× | FsEntry { |
| 181 | 17× | content_hash: Some(m.hash), |
| 182 | 17× | blob: (e.kind == NodeKind::File).then_some(blob), |
| 183 | 17× | stat: Some((m.size, m.mtime_ns)), |
| 184 | 17× | ..FsEntry::new(e.kind, e.mode.exec, Vec::new()) |
| 185 | 17× | }, |
| 186 | | ); |
| 187 | 17× | continue; |
| 188 | 23× | } |
| 189 | 23× | let bytes = self.read_blob(&blob)?; |
| 190 | 23× | let full = self.root.join(path); |
| 191 | 23× | if e.kind == NodeKind::Symlink { |
| 192 | 0× | let same = fs::read_link(&full) |
| 193 | 0× | .is_ok_and(|t| t.to_string_lossy().as_bytes() == bytes.as_slice()); |
| 194 | 0× | if !same { |
| 195 | 0× | self.write_symlink(path, &bytes)?; |
| 196 | 0× | report.written += 1; |
| 197 | 0× | } |
| 198 | 0× | entries.insert( |
| 199 | 0× | path.clone(), |
| 200 | 0× | FsEntry { |
| 201 | 0× | content_hash: Some(hex(&bytes)), |
| 202 | 0× | ..FsEntry::new(NodeKind::Symlink, false, Vec::new()) |
| 203 | 0× | }, |
| 204 | | ); |
| 205 | | } else { |
| 206 | 23× | if !self.same_on_disk(&full, &bytes, e.mode.exec) { |
| 207 | 22× | self.write_file(path, &bytes, e.mode.exec)?; |
| 208 | 22× | report.written += 1; |
| 209 | 1× | } |
| 210 | 23× | entries.insert( |
| 211 | 23× | path.clone(), |
| 212 | 23× | FsEntry { |
| 213 | 23× | content_hash: Some(hex(&bytes)), |
| 214 | 23× | blob: (blobs.len() == 1).then_some(blob), |
| 215 | 23× | ..FsEntry::new(NodeKind::File, e.mode.exec, Vec::new()) |
| 216 | 23× | }, |
| 217 | | ); |
| 218 | | } |
| 219 | 23× | for (i, b) in blobs.iter().enumerate().skip(1) { |
| 220 | 1× | self.write_file( |
| 221 | 1× | &format!("{path}.oxy-conflict-{}", i + 1), |
| 222 | 1× | &self.read_blob(b)?, |
| 223 | 1× | e.mode.exec, |
| 224 | 0× | )?; |
| 225 | | } |
| 226 | | } |
| 227 | |
|
| 228 | 545× | drop(plan); |
| 229 | 545× | let _render = crate::timing::phase("checkout: render and write"); |
| 230 | | |
| 231 | | |
| 232 | 10996× | texts.sort_by_key(|(p, _, _)| !is_attributes(p)); |
| 233 | 545× | let keys: HashMap<NodeId, [u8; 32]> = texts |
| 234 | 545× | .iter() |
| 235 | 5896× | .filter_map(|(_, n, _)| Some((*n, target.graph_key(n)?))) |
| 236 | 545× | .collect(); |
| 237 | 545× | let mut attrs = crate::attributes::Attributes::default(); |
| 238 | 5896× | let attr_count = texts.iter().filter(|(p, _, _)| is_attributes(p)).count(); |
| 239 | 545× | let mut done = 0usize; |
| 240 | 545× | let mut batches: Vec<&[(String, NodeId, Mode)]> = Vec::new(); |
| 241 | 545× | if attr_count > 0 { |
| 242 | 2× | batches.push(&texts[..attr_count]); |
| 243 | 543× | } |
| 244 | 545× | batches.extend(texts[attr_count..].chunks(CHECKOUT_BATCH)); |
| 245 | 545× | for batch in batches { |
| 246 | | use rayon::prelude::*; |
| 247 | 401× | let nodes: Vec<NodeId> = batch.iter().map(|(_, n, _)| *n).collect(); |
| 248 | | |
| 249 | 401× | let single: Vec<Option<crate::objects::TextRef>> = |
| 250 | 5896× | nodes.iter().map(|n| target.single_text(n)).collect(); |
| 251 | 401× | let graphs: Vec<NodeId> = nodes |
| 252 | 401× | .iter() |
| 253 | 401× | .zip(&single) |
| 254 | 5896× | .filter(|(_, s)| s.is_none()) |
| 255 | 401× | .map(|(n, _)| *n) |
| 256 | 401× | .collect(); |
| 257 | 401× | let mut texts = { |
| 258 | 401× | let _t = crate::timing::phase("checkout: read single texts"); |
| 259 | 401× | let wanted: Vec<crate::objects::TextRef> = |
| 260 | 401× | single.iter().flatten().copied().collect(); |
| 261 | 401× | self.single_texts(&wanted)?.into_iter() |
| 262 | | }; |
| 263 | | { |
| 264 | 401× | let _t = crate::timing::phase("checkout: build graphs"); |
| 265 | 401× | self.load_graphs_of(&mut target, &graphs)?; |
| 266 | | } |
| 267 | 401× | let rendered: Vec<(Vec<u8>, bool)> = { |
| 268 | 401× | let _t = crate::timing::phase("checkout: render"); |
| 269 | 401× | let mut out: Vec<Option<(Vec<u8>, bool)>> = single |
| 270 | 401× | .iter() |
| 271 | 5896× | .map(|s| s.as_ref().and_then(|_| texts.next().map(|t| (t, false)))) |
| 272 | 401× | .collect(); |
| 273 | 401× | let from_graphs: Vec<(Vec<u8>, bool)> = graphs |
| 274 | 401× | .par_iter() |
| 275 | 401× | .map(|n| { |
| 276 | 339× | let (bytes, m) = target.render_file(n); |
| 277 | 339× | (bytes, m.is_conflicted()) |
| 278 | 339× | }) |
| 279 | 401× | .collect(); |
| 280 | 401× | let mut from_graphs = from_graphs.into_iter(); |
| 281 | 5896× | for o in out.iter_mut().filter(|o| o.is_none()) { |
| 282 | 339× | *o = from_graphs.next(); |
| 283 | 339× | } |
| 284 | 401× | out.into_iter() |
| 285 | 5896× | .map(|o| o.expect("every file rendered")) |
| 286 | 401× | .collect() |
| 287 | | }; |
| 288 | | #[cfg(test)] |
| 289 | | { |
| 290 | 203× | let singles: Vec<NodeId> = nodes |
| 291 | 203× | .iter() |
| 292 | 203× | .zip(&single) |
| 293 | 5345× | .filter(|(_, s)| s.is_some()) |
| 294 | 203× | .map(|(n, _)| *n) |
| 295 | 203× | .collect(); |
| 296 | 203× | self.load_graphs_of(&mut target, &singles)?; |
| 297 | 5345× | for (n, (bytes, _)) in nodes.iter().zip(&rendered) { |
| 298 | 5345× | assert_eq!( |
| 299 | | *bytes, |
| 300 | 5345× | target.render_file(n).0, |
| 301 | | "a single text is its file's rendering" |
| 302 | | ); |
| 303 | | } |
| 304 | | } |
| 305 | 401× | target.drop_graphs(&nodes); |
| 306 | 401× | let _t = crate::timing::phase("checkout: write"); |
| 307 | 401× | if done == 0 && attr_count > 0 { |
| 308 | 2× | attrs = crate::attributes::Attributes::from_files( |
| 309 | 2× | batch |
| 310 | 2× | .iter() |
| 311 | 2× | .zip(&rendered) |
| 312 | 2× | .map(|((p, _, _), (b, _))| (p.as_str(), b.as_slice())), |
| 313 | | ); |
| 314 | 399× | } |
| 315 | 401× | done += batch.len(); |
| 316 | 401× | let on_disk: Vec<Vec<u8>> = batch |
| 317 | 401× | .iter() |
| 318 | 401× | .zip(rendered.iter()) |
| 319 | 5896× | .map(|((path, _, _), (bytes, _))| { |
| 320 | 5896× | if attrs.lookup(path).eol == Some(crate::attributes::Eol::Crlf) { |
| 321 | 2× | crate::attributes::to_crlf(bytes) |
| 322 | | } else { |
| 323 | 5894× | bytes.clone() |
| 324 | | } |
| 325 | 5896× | }) |
| 326 | 401× | .collect(); |
| 327 | | |
| 328 | | |
| 329 | 401× | let mut dirs: BTreeSet<&str> = BTreeSet::new(); |
| 330 | 5896× | for (path, _, _) in batch { |
| 331 | 5896× | if let Some((dir, _)) = path.rsplit_once('/') { |
| 332 | 5253× | dirs.insert(dir); |
| 333 | 5253× | } |
| 334 | | } |
| 335 | 401× | for dir in dirs { |
| 336 | 162× | let full = self.safe_path(dir)?; |
| 337 | 161× | if !full.is_dir() { |
| 338 | 129× | if fs::symlink_metadata(&full).is_ok() { |
| 339 | 0× | remove_any(&full)?; |
| 340 | 129× | } |
| 341 | 129× | fs::create_dir_all(&full)?; |
| 342 | 32× | } |
| 343 | | } |
| 344 | 5894× | for (path, _, _) in batch { |
| 345 | 5894× | check_path_name(path)?; |
| 346 | | } |
| 347 | 400× | let (root, exec_bits) = (&self.root, self.platform.exec_bits); |
| 348 | | |
| 349 | | |
| 350 | | |
| 351 | | |
| 352 | 400× | let plans: Vec<Disk> = batch |
| 353 | 400× | .par_iter() |
| 354 | 400× | .zip(on_disk.par_iter()) |
| 355 | 5894× | .map(|((path, _, mode), bytes)| { |
| 356 | 5894× | compare_on_disk(&root.join(path), bytes, mode.exec, exec_bits) |
| 357 | 5894× | }) |
| 358 | 400× | .collect(); |
| 359 | 400× | let fresh: Vec<usize> = (0..batch.len()) |
| 360 | 5894× | .filter(|&i| plans[i] == Disk::Absent) |
| 361 | 400× | .collect(); |
| 362 | 400× | fresh |
| 363 | 400× | .par_iter() |
| 364 | 5708× | .map(|&i| write_file_at(&root.join(&batch[i].0), &on_disk[i], batch[i].2.exec)) |
| 365 | 400× | .collect::<std::io::Result<()>>()?; |
| 366 | 5894× | for (i, plan) in plans.iter().enumerate() { |
| 367 | 5894× | if *plan == Disk::Differs { |
| 368 | 158× | write_file_at(&root.join(&batch[i].0), &on_disk[i], batch[i].2.exec)?; |
| 369 | 5736× | } |
| 370 | | } |
| 371 | 5894× | report.written += plans.iter().filter(|p| **p != Disk::Same).count(); |
| 372 | 5894× | for (((path, node, mode), (_, conflicted)), on_disk) in |
| 373 | 400× | batch.iter().zip(rendered).zip(on_disk) |
| 374 | | { |
| 375 | 5894× | let a = attrs.lookup(path); |
| 376 | 5894× | report.conflicted |= conflicted; |
| 377 | 5894× | entries.insert( |
| 378 | 5894× | path.clone(), |
| 379 | | FsEntry { |
| 380 | 5894× | content_hash: Some(hex(&on_disk)), |
| 381 | 5894× | graph_key: (!conflicted && a.eol.is_none()) |
| 382 | 5894× | .then(|| keys.get(node).copied()) |
| 383 | 5894× | .flatten(), |
| 384 | 5894× | ..FsEntry::new(NodeKind::File, mode.exec, Vec::new()) |
| 385 | | }, |
| 386 | | ); |
| 387 | | } |
| 388 | | } |
| 389 | | |
| 390 | | |
| 391 | 10386× | for (path, e) in &entries { |
| 392 | 10386× | if e.kind == NodeKind::File && attrs.lookup(path).lockable && !self.is_locked(path) { |
| 393 | 2× | let _ = set_readonly(&self.root.join(path), true); |
| 394 | 10384× | } |
| 395 | | } |
| 396 | | |
| 397 | 544× | for d in dirs_to_prune.iter().rev() { |
| 398 | 75× | let mut d = d.clone(); |
| 399 | 93× | while d != self.root |
| 400 | 21× | && fs::read_dir(&d) |
| 401 | 21× | .map(|mut i| i.next().is_none()) |
| 402 | 21× | .unwrap_or(false) |
| 403 | | { |
| 404 | 18× | fs::remove_dir(&d)?; |
| 405 | 18× | let Some(p) = d.parent() else { break }; |
| 406 | 18× | d = p.to_path_buf(); |
| 407 | | } |
| 408 | | } |
| 409 | |
|
| 410 | 544× | drop(_render); |
| 411 | | |
| 412 | | |
| 413 | 544× | crate::timing::report_totals(); |
| 414 | 544× | let recording = crate::timing::phase("checkout: record"); |
| 415 | 544× | let (op_ref, op) = self.head_op()?; |
| 416 | 544× | let wc = self.new_working_copy(heads.clone(), ""); |
| 417 | 544× | let wc_hash = self.store.put_change(&wc)?; |
| 418 | 544× | let mut view = op.view.clone(); |
| 419 | 544× | view.workspaces.insert(self.workspace, wc_hash); |
| 420 | 544× | self.record_op(&[op_ref], command, view)?; |
| 421 | 544× | drop(recording); |
| 422 | 544× | let _t = crate::timing::phase("checkout: manifest"); |
| 423 | | |
| 424 | | |
| 425 | | |
| 426 | | |
| 427 | | |
| 428 | 544× | if !old_manifest.dirs.is_empty() { |
| 429 | 544× | let mut dirs: BTreeSet<String> = |
| 430 | 639× | old_manifest.dirs.iter().map(|(d, _)| d.clone()).collect(); |
| 431 | 544× | dirs.insert(String::new()); |
| 432 | 10386× | for p in entries.keys() { |
| 433 | 10386× | let mut at = p.as_str(); |
| 434 | 10516× | while let Some((parent, _)) = at.rsplit_once('/') { |
| 435 | 9495× | if !dirs.insert(parent.to_string()) { |
| 436 | 9365× | break; |
| 437 | 130× | } |
| 438 | 130× | at = parent; |
| 439 | | } |
| 440 | | } |
| 441 | | use rayon::prelude::*; |
| 442 | 544× | let root = &self.root; |
| 443 | 544× | let dirs: Vec<(String, u128)> = dirs |
| 444 | 544× | .into_par_iter() |
| 445 | 769× | .filter_map(|d| { |
| 446 | 769× | let m = fs::metadata(root.join(&d)).ok().filter(|m| m.is_dir())?; |
| 447 | 751× | Some((d, prompt::mtime_ns(&m))) |
| 448 | 769× | }) |
| 449 | 544× | .collect(); |
| 450 | 544× | let mut dirs = dirs; |
| 451 | 544× | dirs.sort_unstable(); |
| 452 | 544× | *self.walked_dirs.borrow_mut() = Some((dirs, old_manifest.nested.clone())); |
| 453 | 0× | } |
| 454 | 544× | self.write_manifest_entries(report.conflicted, false, &entries, &HashMap::new())?; |
| 455 | 544× | let _ = self.run_hook( |
| 456 | 544× | "post-checkout", |
| 457 | 544× | &format!( |
| 458 | | "{{\"event\":\"post-checkout\",\"heads\":{},\"written\":{},\"removed\":{}}}", |
| 459 | 853× | json_array(heads.iter().map(|h| json_str(&h.to_hex()))), |
| 460 | | report.written, |
| 461 | | report.removed |
| 462 | | ), |
| 463 | | ); |
| 464 | 544× | Ok(report) |
| 465 | 547× | } |
| 466 | |
|
| 467 | | |
| 468 | | |
| 469 | 545× | fn count_lacking(&self, from: &[ChangeHash], to: &State) -> Result<usize> { |
| 470 | 545× | let mut seen: HashSet<ChangeHash> = HashSet::new(); |
| 471 | 545× | let mut stack: Vec<ChangeHash> = from.to_vec(); |
| 472 | 1022× | while let Some(h) = stack.pop() { |
| 473 | 477× | if to.contains(&h) || !seen.insert(h) { |
| 474 | 384× | continue; |
| 475 | 93× | } |
| 476 | 93× | stack.extend(self.store.get_change_lazy(&h)?.0.deps); |
| 477 | | } |
| 478 | 545× | Ok(seen.len()) |
| 479 | 545× | } |
| 480 | |
|
| 481 | | |
| 482 | 326× | pub(crate) fn refuse_dirty(&self, snap: &SnapshotResult, discard: bool) -> Result<()> { |
| 483 | 326× | if discard || (snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty()) { |
| 484 | 324× | return Ok(()); |
| 485 | 2× | } |
| 486 | 2× | let list: Vec<String> = snap |
| 487 | 2× | .statuses |
| 488 | 2× | .iter() |
| 489 | 2× | .take(8) |
| 490 | 3× | .map(|s| format!("{} {}", status_word(s.status), s.path)) |
| 491 | 2× | .collect(); |
| 492 | 2× | fail(format!( |
| 493 | | "the working copy has changes ({}{}); commit them, take them along with --carry, or drop them with --discard", |
| 494 | 2× | list.join(", "), |
| 495 | 2× | if snap.statuses.len() > 8 { ", …" } else { "" } |
| 496 | | )) |
| 497 | 326× | } |
| 498 | |
|
| 499 | | |
| 500 | | |
| 501 | 23× | fn same_on_disk(&self, full: &Path, bytes: &[u8], exec: bool) -> bool { |
| 502 | 23× | match fs::symlink_metadata(full) { |
| 503 | 5× | Ok(m) if m.is_file() && m.len() == bytes.len() as u64 => { |
| 504 | 4× | (!self.platform.exec_bits || is_executable(&m) == exec) |
| 505 | 4× | && fs::read(full).is_ok_and(|b| b == bytes) |
| 506 | | } |
| 507 | 19× | _ => false, |
| 508 | | } |
| 509 | 23× | } |
| 510 | |
|
| 511 | | |
| 512 | | |
| 513 | 3262× | fn safe_path(&self, rel: &str) -> Result<PathBuf> { |
| 514 | 3262× | let mut full = self.root.clone(); |
| 515 | 3262× | let comps: Vec<&str> = rel.split('/').collect(); |
| 516 | 6277× | for (i, comp) in comps.iter().enumerate() { |
| 517 | 6277× | if comp.is_empty() || *comp == "." || *comp == ".." || comp.contains('\\') { |
| 518 | 0× | return fail(format!("refusing unsafe path {rel:?}")); |
| 519 | 6277× | } |
| 520 | 6277× | if i == 0 && *comp == OXY_DIR { |
| 521 | 0× | return fail(format!("refusing to write inside {OXY_DIR}: {rel:?}")); |
| 522 | 6277× | } |
| 523 | 6277× | if i + 1 < comps.len() { |
| 524 | 3016× | full.push(comp); |
| 525 | 3016× | if let Ok(m) = fs::symlink_metadata(&full) { |
| 526 | 3015× | if m.file_type().is_symlink() { |
| 527 | 1× | return fail(format!( |
| 528 | | "refusing to write through symlink {}", |
| 529 | 1× | full.display() |
| 530 | | )); |
| 531 | 3014× | } |
| 532 | 1× | } |
| 533 | 3261× | } else { |
| 534 | 3261× | full.push(comp); |
| 535 | 3261× | } |
| 536 | | } |
| 537 | 3261× | Ok(full) |
| 538 | 3262× | } |
| 539 | |
|
| 540 | 30× | pub(crate) fn write_file(&self, rel: &str, bytes: &[u8], exec: bool) -> Result<()> { |
| 541 | 30× | let full = self.safe_path(rel)?; |
| 542 | 30× | if let Some(parent) = full.parent() { |
| 543 | 30× | fs::create_dir_all(parent)?; |
| 544 | 0× | } |
| 545 | 30× | if fs::symlink_metadata(&full).is_ok_and(|m| m.file_type().is_symlink() || m.is_dir()) { |
| 546 | 0× | remove_any(&full)?; |
| 547 | 30× | } |
| 548 | 30× | if fs::metadata(&full).is_ok_and(|m| m.permissions().readonly()) { |
| 549 | 1× | set_readonly(&full, false)?; |
| 550 | 29× | } |
| 551 | 30× | fs::write(&full, bytes)?; |
| 552 | 30× | set_executable(&full, exec)?; |
| 553 | 30× | Ok(()) |
| 554 | 30× | } |
| 555 | |
|
| 556 | 0× | pub(crate) fn write_symlink(&self, rel: &str, target: &[u8]) -> Result<()> { |
| 557 | 0× | let full = self.safe_path(rel)?; |
| 558 | 0× | if let Some(parent) = full.parent() { |
| 559 | 0× | fs::create_dir_all(parent)?; |
| 560 | 0× | } |
| 561 | 0× | if fs::symlink_metadata(&full).is_ok() { |
| 562 | 0× | remove_any(&full)?; |
| 563 | 0× | } |
| 564 | | #[cfg(unix)] |
| 565 | | { |
| 566 | 0× | std::os::unix::fs::symlink(String::from_utf8_lossy(target).as_ref(), &full)?; |
| 567 | 0× | Ok(()) |
| 568 | | } |
| 569 | | #[cfg(not(unix))] |
| 570 | | { |
| 571 | | |
| 572 | | fs::write(&full, target)?; |
| 573 | | Ok(()) |
| 574 | | } |
| 575 | 0× | } |
| 576 | | } |
| 577 | |
|
| 578 | |
|
| 579 | |
|
| 580 | 3× | fn status_word(s: ChangeStatus) -> &'static str { |
| 581 | 3× | match s { |
| 582 | 0× | ChangeStatus::Added => "added", |
| 583 | 2× | ChangeStatus::Modified => "modified", |
| 584 | 1× | ChangeStatus::Removed => "removed", |
| 585 | 0× | ChangeStatus::Renamed => "renamed", |
| 586 | 0× | ChangeStatus::ModeChanged => "mode changed", |
| 587 | 0× | ChangeStatus::ConflictEdited => "conflict edited", |
| 588 | 0× | ChangeStatus::Resolved => "resolved", |
| 589 | | } |
| 590 | 3× | } |
| 591 | |
|
| 592 | |
|
| 593 | |
|
| 594 | 5894× | fn check_path_name(rel: &str) -> Result<()> { |
| 595 | 11147× | for (i, comp) in rel.split('/').enumerate() { |
| 596 | 11147× | if comp.is_empty() || comp == "." || comp == ".." || comp.contains('\\') { |
| 597 | 0× | return fail(format!("refusing unsafe path {rel:?}")); |
| 598 | 11147× | } |
| 599 | 11147× | if i == 0 && comp == OXY_DIR { |
| 600 | 0× | return fail(format!("refusing to write inside {OXY_DIR}: {rel:?}")); |
| 601 | 11147× | } |
| 602 | | } |
| 603 | 5894× | Ok(()) |
| 604 | 5894× | } |
| 605 | |
|
| 606 | |
|
| 607 | | #[derive(Clone, Copy, PartialEq, Eq)] |
| 608 | | enum Disk { |
| 609 | | Same, |
| 610 | | Differs, |
| 611 | | Absent, |
| 612 | | } |
| 613 | |
|
| 614 | |
|
| 615 | |
|
| 616 | 5894× | fn compare_on_disk(full: &Path, bytes: &[u8], exec: bool, exec_bits: bool) -> Disk { |
| 617 | 5894× | match fs::symlink_metadata(full) { |
| 618 | 28× | Ok(m) |
| 619 | 186× | if m.is_file() |
| 620 | 186× | && m.len() == bytes.len() as u64 |
| 621 | 47× | && (!exec_bits || is_executable(&m) == exec) |
| 622 | 46× | && fs::read(full).is_ok_and(|b| b == bytes) => |
| 623 | | { |
| 624 | 28× | Disk::Same |
| 625 | | } |
| 626 | 158× | Ok(_) => Disk::Differs, |
| 627 | 5708× | Err(_) => Disk::Absent, |
| 628 | | } |
| 629 | 5894× | } |
| 630 | |
|
| 631 | |
|
| 632 | 5866× | fn write_file_at(full: &Path, bytes: &[u8], exec: bool) -> std::io::Result<()> { |
| 633 | 5866× | match fs::symlink_metadata(full) { |
| 634 | 158× | Ok(m) if m.is_file() => { |
| 635 | 158× | if m.permissions().readonly() { |
| 636 | 0× | set_readonly(full, false)?; |
| 637 | 158× | } |
| 638 | | } |
| 639 | 0× | Ok(_) => remove_any(full)?, |
| 640 | 5708× | Err(_) => {} |
| 641 | | } |
| 642 | 5866× | fs::write(full, bytes)?; |
| 643 | 5866× | set_executable(full, exec) |
| 644 | 5866× | } |