diff --git a/Cargo.lock b/Cargo.lock index 8afba7c1..38ccd997 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1561,9 +1561,9 @@ checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0" [[package]] name = "log" -version = "0.4.33" +version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" [[package]] name = "loom" @@ -2130,6 +2130,7 @@ dependencies = [ name = "radicle-ci-adapter" version = "0.1.0" dependencies = [ + "log", "nonempty 0.12.0", "radicle", "serde", @@ -2138,6 +2139,7 @@ dependencies = [ "thiserror 2.0.18", "time", "uuid", + "walkdir", ] [[package]] diff --git a/adapterlib/Cargo.toml b/adapterlib/Cargo.toml index befba6a0..af17379b 100644 --- a/adapterlib/Cargo.toml +++ b/adapterlib/Cargo.toml @@ -11,6 +11,7 @@ repository = "https://radicle.network/nodes/radicle.liw.fi/rad:zwTxygwuz5LDGBq25 categories = ["development-tools::build-utils"] [dependencies] +log = "0.4.34" nonempty = "0.12.0" radicle = { workspace = true } serde = { workspace = true } @@ -18,6 +19,7 @@ serde_json = { workspace = true } thiserror = { workspace = true } time = { workspace = true } uuid = { workspace = true } +walkdir = "2.5.0" [dev-dependencies] tempfile = { version = "3.27.0" } diff --git a/adapterlib/src/copy_tree.rs b/adapterlib/src/copy_tree.rs new file mode 100644 index 00000000..4265cfcf --- /dev/null +++ b/adapterlib/src/copy_tree.rs @@ -0,0 +1,144 @@ +//! Copy a directory tree. + +use std::path::{Path, PathBuf, StripPrefixError}; + +use log::trace; +use walkdir::WalkDir; + +pub fn copy_tree(src: impl AsRef, dest: impl AsRef) -> Result<(), CopyTreeError> { + let src = src.as_ref(); + let dest = dest.as_ref(); + + if !src.exists() { + return Err(CopyTreeError::source_missing(src)); + } + if dest.exists() { + return Err(CopyTreeError::target_exists(dest)); + } + + mkdir(dest)?; + + for entry in WalkDir::new(src).into_iter().skip(1) { + let entry = entry.map_err(|source| CopyTreeError::walkdir(src, source))?; + trace!("source: {}", entry.path().display()); + + let dest_path = dest.join( + entry + .path() + .strip_prefix(src) + .map_err(|source| CopyTreeError::strip_prefix(entry.path(), src, source))?, + ); + + if entry.file_type().is_file() { + copy_file(entry.path(), &dest_path)?; + } else if entry.file_type().is_dir() { + mkdir(&dest_path)? + } else if entry.file_type().is_symlink() { + copy_symlink(entry.path(), &dest_path)?; + } else { + return Err(CopyTreeError::file_type(entry.path())); + } + } + + Ok(()) +} + +fn mkdir(path: &Path) -> Result<(), CopyTreeError> { + std::fs::create_dir(path).map_err(|source| CopyTreeError::mkdir(path, source))?; + trace!("mkdir {}", path.display()); + Ok(()) +} + +fn copy_file(src: &Path, dest: &Path) -> Result<(), CopyTreeError> { + std::fs::copy(src, dest).map_err(|source| CopyTreeError::copy_file(src, dest, source))?; + trace!("copy {} to {}", src.display(), dest.display()); + Ok(()) +} + +fn copy_symlink(src: &Path, dest: &Path) -> Result<(), CopyTreeError> { + let target = std::fs::read_link(src).map_err(|source| CopyTreeError::read_link(src, source))?; + std::os::unix::fs::symlink(&target, dest) + .map_err(|source| CopyTreeError::symlink(dest, &target, source))?; + trace!("symlink {} -> {}", dest.display(), target.display()); + Ok(()) +} + +#[derive(Debug, thiserror::Error)] +pub enum CopyTreeError { + #[error("source directory does not exist: {0}")] + SourceMissing(PathBuf), + + #[error("target directory exists: {0}")] + TargetExists(PathBuf), + + #[error("failed to create directory {0}")] + Mkdir(PathBuf, #[source] std::io::Error), + + #[error("error scanning source tree {0}")] + WalkDir(PathBuf, #[source] walkdir::Error), + + #[error("can't handle unknown type of file {0}")] + FileType(PathBuf), + + #[error("failed to copy file {0} to {1}")] + CopyFile(PathBuf, PathBuf, #[source] std::io::Error), + + #[error("can't remove prefix {0} from {1}")] + StripPrefix(PathBuf, PathBuf, #[source] StripPrefixError), + + #[error("failed to read what symlink points to: {0}")] + ReadLink(PathBuf, #[source] std::io::Error), + + #[error("failed to create symlink {0} -> {1}")] + Symlink(PathBuf, PathBuf, #[source] std::io::Error), +} + +impl CopyTreeError { + fn source_missing(path: impl Into) -> Self { + Self::SourceMissing(path.into()) + } + + fn target_exists(path: impl Into) -> Self { + Self::TargetExists(path.into()) + } + + fn mkdir(path: impl Into, source: std::io::Error) -> Self { + Self::Mkdir(path.into(), source) + } + + fn walkdir(path: impl Into, source: walkdir::Error) -> Self { + Self::WalkDir(path.into(), source) + } + + fn file_type(path: impl Into) -> Self { + Self::FileType(path.into()) + } + + fn copy_file( + src: impl Into, + dest: impl Into, + source: std::io::Error, + ) -> Self { + Self::CopyFile(src.into(), dest.into(), source) + } + + fn strip_prefix( + prefix: impl Into, + path: impl Into, + source: StripPrefixError, + ) -> Self { + Self::StripPrefix(prefix.into(), path.into(), source) + } + + fn read_link(path: impl Into, source: std::io::Error) -> Self { + Self::ReadLink(path.into(), source) + } + + fn symlink( + link: impl Into, + target: impl Into, + source: std::io::Error, + ) -> Self { + Self::Symlink(link.into(), target.into(), source) + } +} diff --git a/adapterlib/src/lib.rs b/adapterlib/src/lib.rs index 4a19d2b9..b78ed571 100644 --- a/adapterlib/src/lib.rs +++ b/adapterlib/src/lib.rs @@ -27,6 +27,10 @@ pub use radicle::{ prelude::{NodeId, RepoId}, }; +use crate::copy_tree::{CopyTreeError, copy_tree}; + +mod copy_tree; + /// The type of a run identifier. For maximum generality, this is a /// string rather than an integer. /// @@ -596,6 +600,10 @@ pub enum AdapterLibError { /// Admin log error. #[error(transparent)] AdminLog(#[from] LogError), + + /// Can't copy source files from directory. + #[error("failed to copy source tree from {0}")] + CopyTree(PathBuf, #[source] CopyTreeError), } impl AdapterLibError { @@ -674,6 +682,35 @@ pub fn get_sources( Ok(()) } +/// Get sources from the local node or directory specified in an +/// environment variable. +/// +/// If `RADICLE_CI_ADAPTER_SOURCE_DIR` is set, copy sources from +/// that directory into `src`. Otherwise get the sources from +/// the local node. +pub fn get_sources_from_node_or_dir( + adminlog: &mut AdminLog, + dry_run: bool, + repoid: RepoId, + commit: Oid, + src: &Path, +) -> Result<(), AdapterLibError> { + if let Ok(dir) = std::env::var("RADICLE_CI_ADAPTER_SOURCE_DIR") { + let dir = Path::new(&dir); + copy_tree(dir, src) + .map_err(|source| AdapterLibError::CopyTree(dir.to_path_buf(), source))?; + } else { + let profile = Profile::load().map_err(AdapterLibError::Profile)?; + let storage = profile.storage.path(); + let repo_path = storage.join(repoid.canonical()); + + git_clone(adminlog, dry_run, &repo_path, src)?; + git_checkout(adminlog, dry_run, commit, src)?; + } + + Ok(()) +} + /// Run `git clone` for the repository. fn git_clone( adminlog: &mut AdminLog,