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