(source: &Path, destination: &Path)
| 834 | } |
| 835 | |
| 836 | fn replace_worktree(source: &Path, destination: &Path) -> Result<(), SplitSnapshotError> { |
| 837 | for entry in std::fs::read_dir(destination).map_err(RepositoryError::Io)? { |
| 838 | let entry = entry.map_err(RepositoryError::Io)?; |
| 839 | if entry.file_name() != ".atomic" { |
| 840 | remove_path(&entry.path())?; |
| 841 | } |
| 842 | } |
| 843 | for entry in walkdir::WalkDir::new(source).follow_links(false) { |
| 844 | let entry = |
| 845 | entry.map_err(|error| RepositoryError::Io(std::io::Error::other(error.to_string())))?; |
| 846 | let relative = entry.path().strip_prefix(source).map_err(|error| { |
| 847 | RepositoryError::InvalidOperation { |
| 848 | message: error.to_string(), |
| 849 | } |
| 850 | })?; |
| 851 | if relative.as_os_str().is_empty() |
| 852 | || matches!(relative.components().next(), Some(Component::Normal(first)) if first == ".atomic" || first == ".git") |
| 853 | { |
| 854 | continue; |
| 855 | } |
| 856 | let target = destination.join(relative); |
| 857 | let metadata = std::fs::symlink_metadata(entry.path()).map_err(RepositoryError::Io)?; |
| 858 | if metadata.file_type().is_dir() { |
| 859 | std::fs::create_dir_all(&target).map_err(RepositoryError::Io)?; |
| 860 | } else if metadata.file_type().is_symlink() { |
| 861 | let bytes = crate::content_filter::read_working_bytes(entry.path()) |
| 862 | .map_err(RepositoryError::Io)?; |
| 863 | create_symlink(&target, &bytes)?; |
| 864 | } else if metadata.file_type().is_file() { |
| 865 | if let Some(parent) = target.parent() { |
| 866 | std::fs::create_dir_all(parent).map_err(RepositoryError::Io)?; |
| 867 | } |
| 868 | std::fs::copy(entry.path(), &target).map_err(RepositoryError::Io)?; |
| 869 | } else { |
| 870 | return Err(SplitSnapshotError::Refused( |
| 871 | SnapshotSplitRefusal::UnsupportedUnit { |
| 872 | path: relative.display().to_string(), |
| 873 | reason: "unsupported filesystem entry kind".to_string(), |
| 874 | }, |
| 875 | )); |
| 876 | } |
| 877 | #[cfg(unix)] |
| 878 | if !metadata.file_type().is_symlink() { |
| 879 | use std::os::unix::fs::PermissionsExt; |
| 880 | std::fs::set_permissions( |
| 881 | &target, |
| 882 | std::fs::Permissions::from_mode(metadata.permissions().mode()), |
| 883 | ) |
| 884 | .map_err(RepositoryError::Io)?; |
| 885 | } |
| 886 | } |
| 887 | Ok(()) |
| 888 | } |
| 889 | |
| 890 | fn refresh_scratch_git_index( |
| 891 | source_root: &Path, |
no test coverage detected