| 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 |
pub conflicted: bool, |
| 10 |
|
| 11 |
pub notes: Vec<String>, |
| 12 |
|
| 13 |
pub lacks: usize, |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
const CHECKOUT_BATCH: usize = 2048; |
| 18 |
|
| 19 |
impl Repo { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
pub fn checkout( |
| 24 |
&self, |
| 25 |
heads: &[ChangeHash], |
| 26 |
discard: bool, |
| 27 |
command: &str, |
| 28 |
) -> Result<CheckoutReport> { |
| 29 |
self.checkout_keeping(heads, discard, command, &BTreeSet::new()) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
pub(crate) fn checkout_keeping( |
| 35 |
&self, |
| 36 |
heads: &[ChangeHash], |
| 37 |
discard: bool, |
| 38 |
command: &str, |
| 39 |
keep_subrepos: &BTreeSet<String>, |
| 40 |
) -> Result<CheckoutReport> { |
| 41 |
let _wc = self.lock_working_copy()?; |
| 42 |
let _all = crate::timing::phase("checkout"); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
let changed_paths: Vec<String> = if self.clean_by_manifest()? { |
| 49 |
Vec::new() |
| 50 |
} else { |
| 51 |
let snap = self.snapshot_status(command)?; |
| 52 |
self.refuse_dirty(&snap, discard)?; |
| 53 |
snap.statuses.iter().map(|s| s.path.clone()).collect() |
| 54 |
}; |
| 55 |
let (_, wc_now) = self.working_copy()?; |
| 56 |
let old_manifest = self.read_manifest()?.map(|(_, _, m)| m).unwrap_or_default(); |
| 57 |
let changed: HashSet<&str> = changed_paths.iter().map(String::as_str).collect(); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
let mut target = self.load_state_indexed(heads)?; |
| 62 |
let heads = target.maximal(heads); |
| 63 |
let tv = crate::timing::phase("checkout: target view"); |
| 64 |
let tview = target.tree().view(); |
| 65 |
drop(tv); |
| 66 |
let mut report = CheckoutReport::default(); |
| 67 |
|
| 68 |
report.notes.extend( |
| 69 |
self.platform |
| 70 |
.problems(tview.entries.keys().map(String::as_str)), |
| 71 |
); |
| 72 |
report.conflicted = tview.is_conflicted(); |
| 73 |
report.lacks = self.count_lacking(&wc_now.deps, &target)?; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
let mut dirs_to_prune: BTreeSet<PathBuf> = BTreeSet::new(); |
| 78 |
let gone: BTreeSet<&str> = old_manifest |
| 79 |
.entries |
| 80 |
.keys() |
| 81 |
.map(String::as_str) |
| 82 |
.chain(changed.iter().copied()) |
| 83 |
.filter(|p| !tview.entries.contains_key(*p)) |
| 84 |
.collect(); |
| 85 |
for path in gone { |
| 86 |
let dir = self.root.join(path); |
| 87 |
if dir.join(OXY_DIR).exists() || dir.join(".git").exists() { |
| 88 |
report |
| 89 |
.notes |
| 90 |
.push(format!("{path}: nested repository left in place")); |
| 91 |
continue; |
| 92 |
} |
| 93 |
let full = self.safe_path(path)?; |
| 94 |
if fs::symlink_metadata(&full).is_err() { |
| 95 |
continue; |
| 96 |
} |
| 97 |
fs::remove_file(&full)?; |
| 98 |
report.removed += 1; |
| 99 |
if let Some(parent) = full.parent() { |
| 100 |
dirs_to_prune.insert(parent.to_path_buf()); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
let plan = crate::timing::phase("checkout: plan"); |
| 106 |
let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 107 |
let hex = |b: &[u8]| *blake3::hash(b).as_bytes(); |
| 108 |
|
| 109 |
let mut texts: Vec<(String, NodeId, Mode)> = Vec::new(); |
| 110 |
for (path, e) in tview.entries.iter() { |
| 111 |
if e.kind == NodeKind::Dir || !self.in_scope(path) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
let exec_ok = |
| 117 |
|m: &prompt::ManifestEntry| !self.platform.exec_bits || m.exec == e.mode.exec; |
| 118 |
let entry = old_manifest |
| 119 |
.entries |
| 120 |
.get(path) |
| 121 |
.filter(|m| !changed.contains(path.as_str()) && exec_ok(m)); |
| 122 |
if e.kind == NodeKind::File && !e.mode.opaque { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
let key = target.graph_key(&e.node); |
| 127 |
let proven = |m: &&prompt::ManifestEntry| { |
| 128 |
!is_attributes(path) && key.is_some() && m.graph == key |
| 129 |
}; |
| 130 |
match entry.filter(proven) { |
| 131 |
Some(m) => { |
| 132 |
entries.insert( |
| 133 |
path.clone(), |
| 134 |
FsEntry { |
| 135 |
content_hash: Some(m.hash), |
| 136 |
graph_key: m.graph, |
| 137 |
unchanged: true, |
| 138 |
stat: Some((m.size, m.mtime_ns)), |
| 139 |
..FsEntry::new(NodeKind::File, e.mode.exec, Vec::new()) |
| 140 |
}, |
| 141 |
); |
| 142 |
} |
| 143 |
None => texts.push((path.clone(), e.node, e.mode)), |
| 144 |
} |
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
let blobs = target.live_blobs(&e.node); |
| 149 |
if blobs.len() > 1 { |
| 150 |
report.conflicted = true; |
| 151 |
} |
| 152 |
let Some(blob) = blobs.first().copied() else { |
| 153 |
continue; |
| 154 |
}; |
| 155 |
if e.kind == NodeKind::Subrepo { |
| 156 |
if keep_subrepos.contains(path) { |
| 157 |
continue; |
| 158 |
} |
| 159 |
let pin = crate::subrepo::Pin::decode(&self.read_blob(&blob)?)?; |
| 160 |
let sync = self.sync_subrepo(path, &pin, false)?; |
| 161 |
if matches!( |
| 162 |
sync, |
| 163 |
SubrepoSync::Updated | SubrepoSync::KeptBookmark { .. } |
| 164 |
) { |
| 165 |
report.written += 1; |
| 166 |
} |
| 167 |
report.notes.extend(sync.note(path)); |
| 168 |
continue; |
| 169 |
} |
| 170 |
if let Some(m) = entry |
| 171 |
.filter(|m| e.kind == NodeKind::File && blobs.len() == 1 && m.blob == Some(blob)) |
| 172 |
{ |
| 173 |
entries.insert( |
| 174 |
path.clone(), |
| 175 |
FsEntry { |
| 176 |
content_hash: Some(m.hash), |
| 177 |
blob: (e.kind == NodeKind::File).then_some(blob), |
| 178 |
stat: Some((m.size, m.mtime_ns)), |
| 179 |
..FsEntry::new(e.kind, e.mode.exec, Vec::new()) |
| 180 |
}, |
| 181 |
); |
| 182 |
continue; |
| 183 |
} |
| 184 |
let bytes = self.read_blob(&blob)?; |
| 185 |
let full = self.root.join(path); |
| 186 |
if e.kind == NodeKind::Symlink { |
| 187 |
let same = fs::read_link(&full) |
| 188 |
.is_ok_and(|t| t.to_string_lossy().as_bytes() == bytes.as_slice()); |
| 189 |
if !same { |
| 190 |
self.write_symlink(path, &bytes)?; |
| 191 |
report.written += 1; |
| 192 |
} |
| 193 |
entries.insert( |
| 194 |
path.clone(), |
| 195 |
FsEntry { |
| 196 |
content_hash: Some(hex(&bytes)), |
| 197 |
..FsEntry::new(NodeKind::Symlink, false, Vec::new()) |
| 198 |
}, |
| 199 |
); |
| 200 |
} else { |
| 201 |
if !self.same_on_disk(&full, &bytes, e.mode.exec) { |
| 202 |
self.write_file(path, &bytes, e.mode.exec)?; |
| 203 |
report.written += 1; |
| 204 |
} |
| 205 |
entries.insert( |
| 206 |
path.clone(), |
| 207 |
FsEntry { |
| 208 |
content_hash: Some(hex(&bytes)), |
| 209 |
blob: (blobs.len() == 1).then_some(blob), |
| 210 |
..FsEntry::new(NodeKind::File, e.mode.exec, Vec::new()) |
| 211 |
}, |
| 212 |
); |
| 213 |
} |
| 214 |
for (i, b) in blobs.iter().enumerate().skip(1) { |
| 215 |
self.write_file( |
| 216 |
&format!("{path}.oxy-conflict-{}", i + 1), |
| 217 |
&self.read_blob(b)?, |
| 218 |
e.mode.exec, |
| 219 |
)?; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
drop(plan); |
| 224 |
let _render = crate::timing::phase("checkout: render and write"); |
| 225 |
|
| 226 |
|
| 227 |
texts.sort_by_key(|(p, _, _)| !is_attributes(p)); |
| 228 |
let keys: HashMap<NodeId, [u8; 32]> = texts |
| 229 |
.iter() |
| 230 |
.filter_map(|(_, n, _)| Some((*n, target.graph_key(n)?))) |
| 231 |
.collect(); |
| 232 |
let mut attrs = crate::attributes::Attributes::default(); |
| 233 |
let attr_count = texts.iter().filter(|(p, _, _)| is_attributes(p)).count(); |
| 234 |
let mut done = 0usize; |
| 235 |
let mut batches: Vec<&[(String, NodeId, Mode)]> = Vec::new(); |
| 236 |
if attr_count > 0 { |
| 237 |
batches.push(&texts[..attr_count]); |
| 238 |
} |
| 239 |
batches.extend(texts[attr_count..].chunks(CHECKOUT_BATCH)); |
| 240 |
for batch in batches { |
| 241 |
use rayon::prelude::*; |
| 242 |
let nodes: Vec<NodeId> = batch.iter().map(|(_, n, _)| *n).collect(); |
| 243 |
{ |
| 244 |
let _t = crate::timing::phase("checkout: build graphs"); |
| 245 |
self.load_graphs_of(&mut target, &nodes)?; |
| 246 |
} |
| 247 |
let rendered: Vec<(Vec<u8>, bool)> = { |
| 248 |
let _t = crate::timing::phase("checkout: render"); |
| 249 |
nodes |
| 250 |
.par_iter() |
| 251 |
.map(|n| { |
| 252 |
let (bytes, m) = target.render_file(n); |
| 253 |
(bytes, m.is_conflicted()) |
| 254 |
}) |
| 255 |
.collect() |
| 256 |
}; |
| 257 |
target.drop_graphs(&nodes); |
| 258 |
let _t = crate::timing::phase("checkout: write"); |
| 259 |
if done == 0 && attr_count > 0 { |
| 260 |
attrs = crate::attributes::Attributes::from_files( |
| 261 |
batch |
| 262 |
.iter() |
| 263 |
.zip(&rendered) |
| 264 |
.map(|((p, _, _), (b, _))| (p.as_str(), b.as_slice())), |
| 265 |
); |
| 266 |
} |
| 267 |
done += batch.len(); |
| 268 |
for ((path, node, mode), (bytes, conflicted)) in batch.iter().zip(rendered) { |
| 269 |
let a = attrs.lookup(path); |
| 270 |
let crlf = a.eol == Some(crate::attributes::Eol::Crlf); |
| 271 |
let on_disk: Vec<u8> = if crlf { |
| 272 |
crate::attributes::to_crlf(&bytes) |
| 273 |
} else { |
| 274 |
bytes |
| 275 |
}; |
| 276 |
if !self.same_on_disk(&self.root.join(path), &on_disk, mode.exec) { |
| 277 |
self.write_file(path, &on_disk, mode.exec)?; |
| 278 |
report.written += 1; |
| 279 |
} |
| 280 |
report.conflicted |= conflicted; |
| 281 |
entries.insert( |
| 282 |
path.clone(), |
| 283 |
FsEntry { |
| 284 |
content_hash: Some(hex(&on_disk)), |
| 285 |
graph_key: (!conflicted && a.eol.is_none()) |
| 286 |
.then(|| keys.get(node).copied()) |
| 287 |
.flatten(), |
| 288 |
..FsEntry::new(NodeKind::File, mode.exec, Vec::new()) |
| 289 |
}, |
| 290 |
); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
for (path, e) in &entries { |
| 296 |
if e.kind == NodeKind::File && attrs.lookup(path).lockable && !self.is_locked(path) { |
| 297 |
let _ = set_readonly(&self.root.join(path), true); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
for d in dirs_to_prune.iter().rev() { |
| 302 |
let mut d = d.clone(); |
| 303 |
while d != self.root |
| 304 |
&& fs::read_dir(&d) |
| 305 |
.map(|mut i| i.next().is_none()) |
| 306 |
.unwrap_or(false) |
| 307 |
{ |
| 308 |
fs::remove_dir(&d)?; |
| 309 |
let Some(p) = d.parent() else { break }; |
| 310 |
d = p.to_path_buf(); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
drop(_render); |
| 315 |
let _t = crate::timing::phase("checkout: record and manifest"); |
| 316 |
let (op_ref, op) = self.head_op()?; |
| 317 |
let wc = self.new_working_copy(heads.clone(), ""); |
| 318 |
let wc_hash = self.store.put_change(&wc)?; |
| 319 |
let mut view = op.view.clone(); |
| 320 |
view.workspaces.insert(self.workspace, wc_hash); |
| 321 |
self.record_op(&[op_ref], command, view)?; |
| 322 |
self.write_manifest_entries(report.conflicted, false, &entries, &HashMap::new())?; |
| 323 |
let _ = self.run_hook( |
| 324 |
"post-checkout", |
| 325 |
&format!( |
| 326 |
"{{\"event\":\"post-checkout\",\"heads\":{},\"written\":{},\"removed\":{}}}", |
| 327 |
json_array(heads.iter().map(|h| json_str(&h.to_hex()))), |
| 328 |
report.written, |
| 329 |
report.removed |
| 330 |
), |
| 331 |
); |
| 332 |
Ok(report) |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
fn count_lacking(&self, from: &[ChangeHash], to: &State) -> Result<usize> { |
| 338 |
let mut seen: HashSet<ChangeHash> = HashSet::new(); |
| 339 |
let mut stack: Vec<ChangeHash> = from.to_vec(); |
| 340 |
while let Some(h) = stack.pop() { |
| 341 |
if to.contains(&h) || !seen.insert(h) { |
| 342 |
continue; |
| 343 |
} |
| 344 |
stack.extend(self.store.get_change_lazy(&h)?.0.deps); |
| 345 |
} |
| 346 |
Ok(seen.len()) |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
fn refuse_dirty(&self, snap: &SnapshotResult, discard: bool) -> Result<()> { |
| 351 |
if discard || (snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty()) { |
| 352 |
return Ok(()); |
| 353 |
} |
| 354 |
let list: Vec<String> = snap |
| 355 |
.statuses |
| 356 |
.iter() |
| 357 |
.take(8) |
| 358 |
.map(|s| format!("{} {}", status_word(s.status), s.path)) |
| 359 |
.collect(); |
| 360 |
fail(format!( |
| 361 |
"the working copy has changes ({}{}); commit them, take them along with --carry, or drop them with --discard", |
| 362 |
list.join(", "), |
| 363 |
if snap.statuses.len() > 8 { ", …" } else { "" } |
| 364 |
)) |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
fn same_on_disk(&self, full: &Path, bytes: &[u8], exec: bool) -> bool { |
| 370 |
match fs::symlink_metadata(full) { |
| 371 |
Ok(m) if m.is_file() && m.len() == bytes.len() as u64 => { |
| 372 |
(!self.platform.exec_bits || is_executable(&m) == exec) |
| 373 |
&& fs::read(full).is_ok_and(|b| b == bytes) |
| 374 |
} |
| 375 |
_ => false, |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
fn safe_path(&self, rel: &str) -> Result<PathBuf> { |
| 382 |
let mut full = self.root.clone(); |
| 383 |
let comps: Vec<&str> = rel.split('/').collect(); |
| 384 |
for (i, comp) in comps.iter().enumerate() { |
| 385 |
if comp.is_empty() || *comp == "." || *comp == ".." || comp.contains('\\') { |
| 386 |
return fail(format!("refusing unsafe path {rel:?}")); |
| 387 |
} |
| 388 |
if i == 0 && *comp == OXY_DIR { |
| 389 |
return fail(format!("refusing to write inside {OXY_DIR}: {rel:?}")); |
| 390 |
} |
| 391 |
if i + 1 < comps.len() { |
| 392 |
full.push(comp); |
| 393 |
if let Ok(m) = fs::symlink_metadata(&full) { |
| 394 |
if m.file_type().is_symlink() { |
| 395 |
return fail(format!( |
| 396 |
"refusing to write through symlink {}", |
| 397 |
full.display() |
| 398 |
)); |
| 399 |
} |
| 400 |
} |
| 401 |
} else { |
| 402 |
full.push(comp); |
| 403 |
} |
| 404 |
} |
| 405 |
Ok(full) |
| 406 |
} |
| 407 |
|
| 408 |
pub(crate) fn write_file(&self, rel: &str, bytes: &[u8], exec: bool) -> Result<()> { |
| 409 |
let full = self.safe_path(rel)?; |
| 410 |
if let Some(parent) = full.parent() { |
| 411 |
fs::create_dir_all(parent)?; |
| 412 |
} |
| 413 |
if fs::symlink_metadata(&full).is_ok_and(|m| m.file_type().is_symlink() || m.is_dir()) { |
| 414 |
remove_any(&full)?; |
| 415 |
} |
| 416 |
if fs::metadata(&full).is_ok_and(|m| m.permissions().readonly()) { |
| 417 |
set_readonly(&full, false)?; |
| 418 |
} |
| 419 |
fs::write(&full, bytes)?; |
| 420 |
set_executable(&full, exec)?; |
| 421 |
Ok(()) |
| 422 |
} |
| 423 |
|
| 424 |
pub(crate) fn write_symlink(&self, rel: &str, target: &[u8]) -> Result<()> { |
| 425 |
let full = self.safe_path(rel)?; |
| 426 |
if let Some(parent) = full.parent() { |
| 427 |
fs::create_dir_all(parent)?; |
| 428 |
} |
| 429 |
if fs::symlink_metadata(&full).is_ok() { |
| 430 |
remove_any(&full)?; |
| 431 |
} |
| 432 |
#[cfg(unix)] |
| 433 |
{ |
| 434 |
std::os::unix::fs::symlink(String::from_utf8_lossy(target).as_ref(), &full)?; |
| 435 |
Ok(()) |
| 436 |
} |
| 437 |
#[cfg(not(unix))] |
| 438 |
{ |
| 439 |
|
| 440 |
fs::write(&full, target)?; |
| 441 |
Ok(()) |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
fn status_word(s: ChangeStatus) -> &'static str { |
| 449 |
match s { |
| 450 |
ChangeStatus::Added => "added", |
| 451 |
ChangeStatus::Modified => "modified", |
| 452 |
ChangeStatus::Removed => "removed", |
| 453 |
ChangeStatus::Renamed => "renamed", |
| 454 |
ChangeStatus::ModeChanged => "mode changed", |
| 455 |
ChangeStatus::ConflictEdited => "conflict edited", |
| 456 |
ChangeStatus::Resolved => "resolved", |
| 457 |
} |
| 458 |
} |