feat: add new function get_sources_from_node_or_dir

This is a complement of the get_sources function. I don't want to extend
the existing function to avoid unplesant surprises. If the new function
works well in practice, I'll phase out the old one over time.
This commit is contained in:
Lars Wirzenius
2026-09-01 08:21:46 +03:00
parent 71360e526f
commit 71ff4e8ec1
4 changed files with 187 additions and 2 deletions
Generated
+4 -2
View File
@@ -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]]
+2
View File
@@ -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" }
+144
View File
@@ -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<Path>, dest: impl AsRef<Path>) -> 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<PathBuf>) -> Self {
Self::SourceMissing(path.into())
}
fn target_exists(path: impl Into<PathBuf>) -> Self {
Self::TargetExists(path.into())
}
fn mkdir(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
Self::Mkdir(path.into(), source)
}
fn walkdir(path: impl Into<PathBuf>, source: walkdir::Error) -> Self {
Self::WalkDir(path.into(), source)
}
fn file_type(path: impl Into<PathBuf>) -> Self {
Self::FileType(path.into())
}
fn copy_file(
src: impl Into<PathBuf>,
dest: impl Into<PathBuf>,
source: std::io::Error,
) -> Self {
Self::CopyFile(src.into(), dest.into(), source)
}
fn strip_prefix(
prefix: impl Into<PathBuf>,
path: impl Into<PathBuf>,
source: StripPrefixError,
) -> Self {
Self::StripPrefix(prefix.into(), path.into(), source)
}
fn read_link(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
Self::ReadLink(path.into(), source)
}
fn symlink(
link: impl Into<PathBuf>,
target: impl Into<PathBuf>,
source: std::io::Error,
) -> Self {
Self::Symlink(link.into(), target.into(), source)
}
}
+37
View File
@@ -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,