MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / write_tree

Function write_tree

atomic-repository/src/repository/revise/replay.rs:440–512  ·  view source on GitHub ↗
(
    repo: &Repository,
    before: &Tree,
    after: &Tree,
    stage: bool,
)

Source from the content-addressed store, hash-verified

438}
439
440fn write_tree(
441 repo: &Repository,
442 before: &Tree,
443 after: &Tree,
444 stage: bool,
445) -> Result<(), RepositoryError> {
446 let wc = repo.require_working_copy_id()?;
447 let mut removed: Vec<_> = before.values().collect();
448 removed.sort_by_key(|file| std::cmp::Reverse(file.path.len()));
449 for file in removed {
450 let path = file_path(repo.root(), &file.path)?;
451 if fs::symlink_metadata(&path).is_ok() {
452 if file.kind == ViewEntryKind::Directory {
453 fs::remove_dir(path)?;
454 } else {
455 fs::remove_file(path)?;
456 }
457 }
458 }
459 for file in after.values() {
460 let path = file_path(repo.root(), &file.path)?;
461 fs::create_dir_all(path.parent().unwrap())?;
462 if file.kind == ViewEntryKind::Directory {
463 fs::create_dir_all(&path)?;
464 } else if file.kind == ViewEntryKind::Symlink {
465 #[cfg(unix)]
466 {
467 use std::os::unix::ffi::OsStringExt;
468 std::os::unix::fs::symlink(
469 std::ffi::OsString::from_vec(file.bytes.clone()),
470 &path,
471 )?;
472 }
473 #[cfg(not(unix))]
474 {
475 return Err(invalid("symlink revision is unsupported on this platform"));
476 }
477 } else {
478 fs::write(&path, &file.bytes)?;
479 #[cfg(unix)]
480 {
481 use std::os::unix::fs::PermissionsExt;
482 fs::set_permissions(&path, fs::Permissions::from_mode(file.mode.into()))?;
483 }
484 }
485 }
486 // Attribute files must all exist before converting canonical bytes back
487 // to recorder input, including nested .gitattributes and Git driver policy.
488 let filter = GitAttributesFilter::for_repository(repo.root());
489 for file in after
490 .values()
491 .filter(|file| file.kind == ViewEntryKind::File)
492 {
493 let bytes = filter
494 .smudge(Path::new(&file.path), &file.bytes)
495 .map_err(|e| invalid(e.to_string()))?
496 .bytes;
497 fs::write(file_path(repo.root(), &file.path)?, bytes)?;

Callers 2

record_treeFunction · 0.70
planFunction · 0.70

Calls 15

file_pathFunction · 0.85
set_permissionsFunction · 0.85
parentMethod · 0.80
invalidFunction · 0.70
getMethod · 0.65
writeFunction · 0.50
collectMethod · 0.45
lenMethod · 0.45
rootMethod · 0.45
unwrapMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected