use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fs;
use std::io::IsTerminal;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, OnceLock, mpsc};
use std::thread;
use std::time::{Duration, Instant};
#[cfg(unix)]
use rustix::process::{Pid, Signal, kill_process_group};
use tempfile::NamedTempFile;
use crate::error::{DaloError, DaloResult};
const GIT_LOCAL_TIMEOUT: Duration = Duration::from_secs(60);
const GIT_NETWORK_TIMEOUT: Duration = Duration::from_secs(300);
const GIT_POLL_INTERVAL: Duration = Duration::from_millis(1);
const GIT_TIMEOUT_ENV: &str = "DALO_GIT_TIMEOUT_SECS";
const MAX_CONCURRENT_FETCHES: usize = 4;
const GIT_ALLOWED_PROTOCOLS: &str = "https:ssh:git:file";
type SshPreflightCache = Mutex<BTreeMap<(String, PathBuf), Arc<OnceLock<bool>>>>;
static SSH_PREFLIGHT_CACHE: OnceLock<SshPreflightCache> = OnceLock::new();
pub fn init_repo(path: &Path) -> DaloResult<()> {
run_git(path, &["init", "-q"]).map(|_| ())
}
pub fn clone_repo(url: &str, destination: &Path) -> DaloResult<()> {
validate_remote_url(url)?;
let cwd = destination.parent().unwrap_or_else(|| Path::new("."));
preflight_clone_source(url, cwd)?;
let destination_arg = destination.to_string_lossy().into_owned();
print_network_progress(&format!(
"Cloning repository `{}`...",
display_git_value(url)
));
run_git_network(cwd, &["clone", "--quiet", "--", url, &destination_arg]).map(|_| ())
}
#[cfg(test)]
pub(crate) fn acquire_pinned_commit(url: &str, destination: &Path, commit: &str) -> DaloResult<()> {
validate_remote_url(url)?;
if !matches!(commit.len(), 40 | 64)
|| !commit
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
{
return Err(DaloError::InvalidArgument {
reason: "pinned acquisition requires a full lowercase Git commit ID".to_owned(),
});
}
let cwd = destination.parent().unwrap_or_else(|| Path::new("."));
preflight_clone_source(url, cwd)?;
fs::create_dir(destination)?;
if commit.len() == 64 {
run_git(destination, &["init", "-q", "--object-format=sha256"])?;
} else {
run_git(
destination,
&["-c", "init.defaultObjectFormat=sha1", "init", "-q"],
)?;
}
run_git(destination, &["remote", "add", "--", "origin", url])?;
print_network_progress(&format!(
"Fetching pinned commit `{commit}` from `{}`...",
display_git_value(url)
));
run_git_network(
destination,
&["fetch", "--quiet", "--depth=1", "--no-tags", "origin", commit],
)
.map_err(|error| DaloError::CheckFailed {
reason: format!(
"could not acquire pinned commit {commit} with depth 1; the server must support shallow fetches of this exact commit; no full-history fallback was attempted: {error}"
),
})?;
let fetched = rev_parse(destination, "FETCH_HEAD^{commit}")?;
if fetched != commit {
return Err(DaloError::CheckFailed {
reason: "fetched project commit differs from declaration".to_owned(),
});
}
checkout_detached(destination, commit)
}
pub(crate) fn acquire_pinned_project_commit(
url: &str,
destination: &Path,
commit: &str,
requested_path: Option<&str>,
) -> DaloResult<Option<PathBuf>> {
validate_remote_url(url)?;
validate_full_pinned_commit(commit)?;
let cwd = destination.parent().unwrap_or_else(|| Path::new("."));
preflight_clone_source(url, cwd)?;
fs::create_dir(destination)?;
init_pinned_repository(destination, commit)?;
run_git(destination, &["remote", "add", "--", "origin", url])?;
configure_partial_clone_remote(destination)?;
print_network_progress(&format!(
"Fetching scoped project commit `{commit}` from `{}`...",
display_git_value(url)
));
run_git_network(
destination,
&[
"fetch",
"--quiet",
"--depth=1",
"--no-tags",
"--filter=blob:none",
"origin",
commit,
],
)
.map_err(|error| DaloError::CheckFailed {
reason: format!(
"could not acquire pinned commit {commit} with a blob-filtered depth-1 fetch; the server must support shallow fetches of this exact commit and Git blob filtering; no full-history fallback was attempted (including an unfiltered fallback): {error}"
),
})?;
let fetched = rev_parse(destination, "FETCH_HEAD^{commit}")?;
if fetched != commit {
return Err(DaloError::CheckFailed {
reason: "fetched project commit differs from declaration".to_owned(),
});
}
let tree_paths = project_tree_paths(destination, commit)?;
let scope = resolve_project_skill_scope_from_paths(&tree_paths, requested_path)?;
configure_project_sparse_checkout(destination, scope.as_deref())?;
checkout_detached(destination, commit)?;
if scope.is_some() {
compact_project_sparse_index(destination)?;
}
if scope.is_some()
&& has_paths_outside_scope(&tree_paths, scope.as_deref())
&& !run_git(
destination,
&["rev-list", "--objects", "--missing=print", commit],
)?
.lines()
.any(|line| line.starts_with('?'))
{
return Err(DaloError::CheckFailed {
reason: "the Git server ignored blob filtering, so this source would retain the full repository; use a server that supports Git partial-clone filtering or set an explicit repository-root path if a full checkout is intended".to_owned(),
});
}
Ok(scope)
}
pub(crate) fn resolve_project_skill_scope(
checkout: &Path,
requested_path: Option<&str>,
) -> DaloResult<Option<PathBuf>> {
validate_remote_url(&remote_url(checkout, "origin")?)?;
let commit = rev_parse_head(checkout)?;
let tree_paths = project_tree_paths(checkout, &commit)?;
resolve_project_skill_scope_from_paths(&tree_paths, requested_path)
}
pub(crate) fn validate_project_skill_path(value: &str) -> DaloResult<Option<PathBuf>> {
if matches!(value, "." | "/") {
return Ok(None);
}
let bytes = value.as_bytes();
let has_windows_drive_prefix =
bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':';
if value.is_empty()
|| value.contains('\\')
|| value.starts_with('/')
|| has_windows_drive_prefix
{
return Err(DaloError::InvalidArgument {
reason: format!(
"project source path `{value}` must be a repository-relative directory; use `.` or `/` for the repository root"
),
});
}
let components = value.split('/').collect::<Vec<_>>();
if components
.iter()
.any(|component| component.is_empty() || *component == "." || *component == "..")
{
return Err(DaloError::InvalidArgument {
reason: format!(
"project source path `{value}` must not contain empty, `.` or `..` path components"
),
});
}
Ok(Some(components.iter().collect()))
}
fn init_pinned_repository(destination: &Path, commit: &str) -> DaloResult<()> {
if commit.len() == 64 {
run_git(destination, &["init", "-q", "--object-format=sha256"])?;
} else {
run_git(
destination,
&["-c", "init.defaultObjectFormat=sha1", "init", "-q"],
)?;
}
Ok(())
}
fn configure_partial_clone_remote(destination: &Path) -> DaloResult<()> {
run_git(
destination,
&["config", "core.repositoryformatversion", "1"],
)?;
run_git(
destination,
&["config", "extensions.partialClone", "origin"],
)?;
run_git(destination, &["config", "remote.origin.promisor", "true"])?;
run_git(
destination,
&["config", "remote.origin.partialclonefilter", "blob:none"],
)?;
Ok(())
}
pub(crate) fn validate_full_pinned_commit(commit: &str) -> DaloResult<()> {
if matches!(commit.len(), 40 | 64)
&& commit
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
{
Ok(())
} else {
Err(DaloError::InvalidArgument {
reason: "pinned acquisition requires a full lowercase Git commit ID".to_owned(),
})
}
}
pub(crate) fn mark_scoped_catalog_checkout(checkout: &Path, scope: &str) -> DaloResult<()> {
run_git(
checkout,
&["config", "--local", "dalo.scopedCatalogPath", scope],
)
.map(|_| ())
}
pub(crate) fn scoped_catalog_checkout_path(checkout: &Path) -> DaloResult<Option<String>> {
match run_git(checkout, &["config", "--get", "dalo.scopedCatalogPath"]) {
Ok(path) => Ok(Some(path.trim().to_owned())),
Err(DaloError::CommandFailed { status, .. }) if status == "1" => Ok(None),
Err(error) => Err(error),
}
}
pub(crate) fn scoped_catalog_checkout_matches(
checkout: &Path,
commit: &str,
scope: &str,
) -> DaloResult<bool> {
let sparse = match run_git(checkout, &["config", "--bool", "core.sparseCheckout"]) {
Ok(value) => value.trim() == "true",
Err(DaloError::CommandFailed { status, .. }) if status == "1" => false,
Err(error) => return Err(error),
};
Ok(rev_parse_head(checkout)? == commit
&& scoped_catalog_checkout_path(checkout)?.as_deref() == Some(scope)
&& run_git(checkout, &["rev-parse", "--is-shallow-repository"])?.trim() == "true"
&& sparse
&& run_git(checkout, &["sparse-checkout", "list"])?.trim()
== project_sparse_pattern(Path::new(scope))
&& checkout.join(scope).is_dir())
}
fn project_tree_paths(checkout: &Path, commit: &str) -> DaloResult<Vec<String>> {
let output = run_git(checkout, &["ls-tree", "-r", "--name-only", "-z", commit])?;
Ok(output
.split('\0')
.filter(|path| !path.is_empty())
.map(str::to_owned)
.collect())
}
fn resolve_project_skill_scope_from_paths(
paths: &[String],
requested_path: Option<&str>,
) -> DaloResult<Option<PathBuf>> {
if let Some(requested_path) = requested_path {
let Some(scope) = validate_project_skill_path(requested_path)? else {
return Ok(None);
};
let prefix = format!("{}/", scope.to_string_lossy());
if !paths.iter().any(|path| path.starts_with(&prefix)) {
return Err(DaloError::InvalidArgument {
reason: format!(
"project source path `{requested_path}` does not name a tracked directory in this commit; Dalo will not fall back to a full repository checkout"
),
});
}
return Ok(Some(scope));
}
let skill_directories = paths
.iter()
.filter(|path| path.rsplit('/').next() == Some("SKILL.md"))
.filter(|path| !path.split('/').any(|component| component.starts_with('.')))
.map(|path| {
path.split('/')
.take(path.split('/').count().saturating_sub(1))
.map(str::to_owned)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let Some(first) = skill_directories.first() else {
return Err(DaloError::InvalidArgument {
reason: "no `SKILL.md` files were found outside dot-folders; set `source.path = \".\"` to use the repository root or point `source.path` at the skill directory".to_owned(),
});
};
let mut common = first.clone();
for candidate in skill_directories.iter().skip(1) {
let shared = common
.iter()
.zip(candidate)
.take_while(|(left, right)| left == right)
.count();
common.truncate(shared);
}
if common.is_empty() {
Ok(None)
} else {
Ok(Some(common.iter().collect()))
}
}
pub(crate) fn configure_project_sparse_checkout(
checkout: &Path,
scope: Option<&Path>,
) -> DaloResult<()> {
let Some(scope) = scope else {
match run_git(checkout, &["config", "--bool", "core.sparseCheckout"]) {
Ok(enabled) if enabled.trim() == "true" => {
run_git(checkout, &["sparse-checkout", "disable"])?;
}
Ok(_) => {}
Err(DaloError::CommandFailed { status, .. }) if status == "1" => {}
Err(error) => return Err(error),
}
return Ok(());
};
run_git(checkout, &["sparse-checkout", "init", "--no-cone"])?;
let pattern = project_sparse_pattern(scope);
run_git(
checkout,
&["sparse-checkout", "set", "--no-cone", "--", &pattern],
)?;
Ok(())
}
fn project_sparse_pattern(scope: &Path) -> String {
let mut escaped_scope = String::new();
for character in scope.to_string_lossy().chars() {
if matches!(character, '*' | '?' | '[' | '\\') {
escaped_scope.push('\\');
}
escaped_scope.push(character);
}
format!("/{escaped_scope}/")
}
pub(crate) fn compact_project_sparse_index(checkout: &Path) -> DaloResult<()> {
run_git(checkout, &["update-index", "--index-version=4"]).map(|_| ())
}
fn has_paths_outside_scope(paths: &[String], scope: Option<&Path>) -> bool {
let Some(scope) = scope else {
return false;
};
let prefix = format!("{}/", scope.to_string_lossy());
paths.iter().any(|path| !path.starts_with(&prefix))
}
pub fn preflight_clone_source(location: &str, cwd: &Path) -> DaloResult<()> {
preflight_local_clone_source(location, cwd)
}
pub fn is_worktree(path: &Path) -> DaloResult<bool> {
match run_git(path, &["rev-parse", "--is-inside-work-tree"]) {
Ok(output) => Ok(output.trim() == "true"),
Err(DaloError::CommandFailed { status, .. }) if status == "128" => Ok(false),
Err(error) => Err(error),
}
}
pub(crate) fn worktree_root(path: &Path) -> DaloResult<PathBuf> {
let root = run_git(path, &["rev-parse", "--show-toplevel"])?;
Ok(fs::canonicalize(root.trim())?)
}
pub(crate) fn remote_branch_commit(path: &Path, branch: &str) -> DaloResult<Option<String>> {
validate_manifest_revision(branch)?;
let reference = format!("refs/remotes/origin/{branch}");
match run_git(path, &["show-ref", "--verify", "--quiet", &reference]) {
Ok(_) => rev_parse(path, &reference).map(Some),
Err(DaloError::CommandFailed { status, .. }) if status == "1" => Ok(None),
Err(error) => Err(error),
}
}
pub fn validate_remote_url(url: &str) -> DaloResult<()> {
if url_has_forbidden_userinfo(url) || !uses_allowed_git_transport(url) {
return Err(DaloError::UnsafeRemoteUrl);
}
Ok(())
}
#[must_use]
pub fn display_remote_url(url: &str) -> String {
redact_url_userinfo(url)
}
pub fn pull_ff_only(path: &Path) -> DaloResult<()> {
print_network_progress(&format!("Refreshing source `{}`...", path.display()));
run_git_network(path, &["pull", "--ff-only", "--quiet"]).map(|_| ())
}
pub fn remote_url(path: &Path, remote: &str) -> DaloResult<String> {
run_git(path, &["remote", "get-url", remote]).map(|output| output.trim().to_owned())
}
pub fn status_paths(path: &Path) -> DaloResult<Vec<PathBuf>> {
let output = run_git(
path,
&["status", "--porcelain=v1", "-z", "--untracked-files=all"],
)?;
let mut records = output.split('\0').filter(|record| !record.is_empty());
let mut paths = Vec::new();
while let Some(record) = records.next() {
if record.len() < 4 || record.as_bytes()[2] != b' ' {
return Err(DaloError::StateError {
reason: "Git returned an invalid working-tree status record".to_owned(),
});
}
let status = &record[..2];
paths.push(PathBuf::from(&record[3..]));
if status.contains('R') || status.contains('C') {
let previous = records.next().ok_or_else(|| DaloError::StateError {
reason: "Git returned an incomplete rename status record".to_owned(),
})?;
paths.push(PathBuf::from(previous));
}
}
Ok(paths)
}
pub fn create_branch(path: &Path, branch: &str, base: &str) -> DaloResult<()> {
validate_manifest_revision(branch)?;
validate_manifest_revision(base)?;
run_git(path, &["switch", "--create", branch, base]).map(|_| ())
}
pub fn add_and_commit(path: &Path, relative: &Path, message: &str) -> DaloResult<()> {
if relative.as_os_str().is_empty()
|| relative
.components()
.any(|component| !matches!(component, std::path::Component::Normal(_)))
|| message.is_empty()
|| message.chars().any(char::is_control)
{
return Err(DaloError::InvalidArgument {
reason: "invalid promotion path or commit message".to_owned(),
});
}
let relative = relative
.to_str()
.ok_or_else(|| DaloError::InvalidArgument {
reason: "promotion path is not UTF-8".to_owned(),
})?;
run_git(path, &["add", "--", relative])?;
run_git(path, &["commit", "--message", message]).map(|_| ())
}
pub fn push_branch_with_gh_auth(path: &Path, remote_url: &str, branch: &str) -> DaloResult<()> {
validate_remote_url(remote_url)?;
validate_manifest_revision(branch)?;
run_git_network(
path,
&[
"-c",
"credential.helper=",
"-c",
"credential.helper=!gh auth git-credential",
"push",
"--",
remote_url,
branch,
],
)
.map(|_| ())
}
pub fn is_dirty(path: &Path) -> DaloResult<bool> {
let output = run_git(path, &["status", "--porcelain=v2", "--untracked-files=no"])?;
Ok(!output.trim().is_empty())
}
pub(crate) fn has_checkout_local_content(path: &Path) -> DaloResult<bool> {
let output = run_git(
path,
&[
"status",
"--porcelain=v1",
"--untracked-files=all",
"--ignored=matching",
],
)?;
Ok(!output.trim().is_empty())
}
pub fn is_tracked_file(repo: &Path, file: &Path) -> DaloResult<bool> {
let relative = file
.strip_prefix(repo)
.map_err(|_| DaloError::InvalidArgument {
reason: format!(
"path `{}` is outside source checkout `{}`",
file.display(),
repo.display()
),
})?;
let relative = relative
.to_str()
.ok_or_else(|| DaloError::InvalidArgument {
reason: format!("source-relative path `{}` is not UTF-8", relative.display()),
})?;
match run_git(repo, &["ls-files", "--error-unmatch", "--", relative]) {
Ok(_) => Ok(true),
Err(DaloError::CommandFailed { status, .. }) if status == "1" => Ok(false),
Err(error) => Err(error),
}
}
pub fn rev_parse_head(path: &Path) -> DaloResult<String> {
run_git(path, &["rev-parse", "HEAD"]).map(|output| output.trim().to_owned())
}
pub fn read_file_at_commit(repo: &Path, commit: &str, relative_path: &Path) -> DaloResult<String> {
if !matches!(commit.len(), 40 | 64)
|| !commit.bytes().all(|byte| byte.is_ascii_hexdigit())
|| relative_path.as_os_str().is_empty()
|| relative_path
.components()
.any(|component| !matches!(component, std::path::Component::Normal(_)))
{
return Err(DaloError::StateError {
reason: "instruction lock contains invalid Git provenance".to_owned(),
});
}
let relative = relative_path
.to_str()
.ok_or_else(|| DaloError::StateError {
reason: format!(
"instruction source path `{}` is not UTF-8",
relative_path.display()
),
})?;
let object = format!("{commit}:{relative}");
run_git(repo, &["show", &object])
}
pub fn rev_parse(path: &Path, revision: &str) -> DaloResult<String> {
run_git(path, &["rev-parse", revision]).map(|output| output.trim().to_owned())
}
pub fn fetch(path: &Path) -> DaloResult<()> {
print_network_progress(&format!(
"Checking upstream drift for `{}`...",
path.display()
));
run_git_network(path, &["fetch", "--quiet", "origin", "HEAD"]).map(|_| ())
}
pub fn validate_tracking_branch(branch: &str) -> DaloResult<()> {
validate_manifest_revision(branch)?;
if branch == "HEAD"
|| branch.starts_with("refs/")
|| run_git(
Path::new("."),
&["check-ref-format", &format!("refs/heads/{branch}")],
)
.is_err()
{
return Err(DaloError::StateError {
reason: format!(
"invalid tracking branch `{branch}`; use a short branch name such as `main`"
),
});
}
Ok(())
}
pub fn fetch_tracking_branch(path: &Path, branch: &str) -> DaloResult<String> {
validate_tracking_branch(branch)?;
print_network_progress(&format!("Checking catalog branch `{branch}`..."));
run_git_network(
path,
&[
"fetch",
"--quiet",
"--no-tags",
"origin",
&format!("refs/heads/{branch}"),
],
)?;
rev_parse(path, "FETCH_HEAD^{commit}")
}
pub fn fetch_upstream(path: &Path) -> DaloResult<()> {
print_network_progress(&format!(
"Staging source refresh for `{}`...",
path.display()
));
run_git_network(path, &["fetch", "--quiet"]).map(|_| ())
}
pub(crate) fn fetch_upstreams_bounded(paths: &[PathBuf]) -> Vec<DaloResult<()>> {
for path in paths {
print_network_progress(&format!(
"Staging source refresh for `{}`...",
path.display()
));
}
run_bounded(paths, MAX_CONCURRENT_FETCHES, |path| {
run_git_network(path, &["fetch", "--quiet"]).map(|_| ())
})
}
fn run_bounded<T, F>(items: &[T], limit: usize, operation: F) -> Vec<DaloResult<()>>
where
T: Sync,
F: Fn(&T) -> DaloResult<()> + Sync,
{
if items.is_empty() {
return Vec::new();
}
let worker_count = limit.max(1).min(items.len());
let next = AtomicUsize::new(0);
let (sender, receiver) = mpsc::channel();
thread::scope(|scope| {
for _ in 0..worker_count {
let sender = sender.clone();
let next = &next;
let operation = &operation;
scope.spawn(move || {
loop {
let index = next.fetch_add(1, Ordering::Relaxed);
let Some(item) = items.get(index) else {
break;
};
if sender.send((index, operation(item))).is_err() {
break;
}
}
});
}
});
drop(sender);
let mut results = receiver.into_iter().collect::<Vec<_>>();
results.sort_by_key(|(index, _)| *index);
results.into_iter().map(|(_, result)| result).collect()
}
pub fn resolve_manifest_revision(path: &Path, revision: &str) -> DaloResult<String> {
validate_manifest_revision(revision)?;
let remote = format!("refs/remotes/origin/{revision}^{{commit}}");
match rev_parse(path, &remote) {
Ok(commit) => Ok(commit),
Err(_) => {
let requested = format!("{revision}^{{commit}}");
rev_parse(path, &requested)
}
}
}
pub(crate) fn revision_is_missing(path: &Path, revision: &str) -> bool {
revision_is_missing_with_program("git", path, revision)
}
fn revision_is_missing_with_program(program: &str, path: &Path, revision: &str) -> bool {
let is_hex_object_id =
(4..=40).contains(&revision.len()) && revision.bytes().all(|byte| byte.is_ascii_hexdigit());
if is_hex_object_id {
let disambiguate = format!("--disambiguate={revision}");
return run_git_program(
program,
path,
&["rev-parse", &disambiguate],
GIT_LOCAL_TIMEOUT,
)
.is_ok_and(|output| output.trim().is_empty());
}
[
format!("refs/remotes/origin/{revision}"),
format!("refs/heads/{revision}"),
format!("refs/tags/{revision}"),
]
.iter()
.all(|reference| {
matches!(
run_git_program(
program,
path,
&["show-ref", "--verify", "--quiet", reference],
GIT_LOCAL_TIMEOUT,
),
Err(DaloError::CommandFailed { status, .. }) if status == "1"
)
})
}
pub fn validate_manifest_revision(revision: &str) -> DaloResult<()> {
let has_operator = revision.contains(['^', '~', ':', '?', '*', '[', '\\']);
if revision.is_empty()
|| revision.starts_with('-')
|| revision
.chars()
.any(|character| character.is_whitespace() || character.is_control())
|| revision.contains("..")
|| revision.contains("@{")
|| has_operator
{
Err(DaloError::CheckFailed {
reason: format!("invalid manifest Git revision `{revision}`"),
})
} else {
Ok(())
}
}
pub fn checkout_detached(path: &Path, commit: &str) -> DaloResult<()> {
run_git(
path,
&["checkout", "--detach", "--force", "--quiet", commit],
)
.map(|_| ())
}
pub fn revision_count(path: &Path, from: &str, to: &str) -> DaloResult<usize> {
let range = format!("{from}..{to}");
let output = run_git(path, &["rev-list", "--count", &range])?;
output.trim().parse().map_err(|error| {
DaloError::Io(std::io::Error::other(format!(
"git returned an invalid revision count `{}`: {error}",
output.trim()
)))
})
}
pub fn fast_forward_to(path: &Path, revision: &str) -> DaloResult<()> {
run_git(path, &["merge", "--ff-only", "--quiet", revision]).map(|_| ())
}
pub fn reset_hard_to(path: &Path, revision: &str) -> DaloResult<()> {
run_git(path, &["reset", "--hard", "--quiet", revision]).map(|_| ())
}
pub fn add_detached_worktree(repo: &Path, dest: &Path, commit: &str) -> DaloResult<()> {
let dest_arg = dest.to_string_lossy().into_owned();
run_git(
repo,
&["worktree", "add", "--detach", "--quiet", &dest_arg, commit],
)
.map(|_| ())
}
pub fn remove_worktree(repo: &Path, dest: &Path) -> DaloResult<()> {
let dest_arg = dest.to_string_lossy().into_owned();
run_git(repo, &["worktree", "remove", "--force", &dest_arg]).map(|_| ())
}
pub fn prune_worktrees(repo: &Path) -> DaloResult<()> {
run_git(repo, &["worktree", "prune"]).map(|_| ())
}
fn run_git(path: &Path, args: &[&str]) -> DaloResult<String> {
run_git_program("git", path, args, git_timeout(GIT_LOCAL_TIMEOUT))
}
fn run_git_network(path: &Path, args: &[&str]) -> DaloResult<String> {
run_git_program_with_ssh_preflight("git", path, args, git_timeout(GIT_NETWORK_TIMEOUT), true)
}
fn run_git_program(
program: &str,
path: &Path,
args: &[&str],
timeout: Duration,
) -> DaloResult<String> {
run_git_program_with_ssh_preflight(program, path, args, timeout, false)
}
fn run_git_program_with_ssh_preflight(
program: &str,
path: &Path,
args: &[&str],
timeout: Duration,
preflight_core_ssh_command: bool,
) -> DaloResult<String> {
let ssh_command_env = std::env::var_os("GIT_SSH_COMMAND");
run_git_program_with_ssh_preflight_and_env(
program,
path,
args,
timeout,
preflight_core_ssh_command,
ssh_command_env.as_deref(),
)
}
fn run_git_program_with_ssh_preflight_and_env(
program: &str,
path: &Path,
args: &[&str],
timeout: Duration,
preflight_core_ssh_command: bool,
ssh_command_env: Option<&OsStr>,
) -> DaloResult<String> {
let core_ssh_command_configured = preflight_core_ssh_command
&& ssh_command_env.is_none()
&& cached_has_core_ssh_command(program, path, timeout.min(git_timeout(GIT_LOCAL_TIMEOUT)));
run_git_program_with_options(
program,
path,
args,
timeout,
ssh_command_env,
core_ssh_command_configured,
)
}
fn run_git_program_with_options(
program: &str,
path: &Path,
args: &[&str],
timeout: Duration,
ssh_command_env: Option<impl AsRef<OsStr>>,
core_ssh_command_configured: bool,
) -> DaloResult<String> {
let stdout = NamedTempFile::new()?;
let stderr = NamedTempFile::new()?;
let mut command = Command::new(program);
command
.args(args)
.current_dir(path)
.env("GIT_ALLOW_PROTOCOL", GIT_ALLOWED_PROTOCOLS)
.env("GIT_PROTOCOL_FROM_USER", "0")
.env("GIT_TERMINAL_PROMPT", "0")
.stdin(Stdio::null())
.stdout(Stdio::from(stdout.reopen()?))
.stderr(Stdio::from(stderr.reopen()?));
#[cfg(test)]
command
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env_remove("GIT_CONFIG_COUNT")
.env_remove("GIT_CONFIG_PARAMETERS");
configure_ssh_command(
&mut command,
ssh_command_env.as_ref(),
core_ssh_command_configured,
);
#[cfg(unix)]
command.process_group(0);
let mut child = command.spawn()?;
let start = Instant::now();
let status = loop {
match child.try_wait() {
Ok(Some(status)) => break status,
Ok(None) => {}
Err(error) => {
terminate_git_process(&mut child);
return Err(DaloError::Io(error));
}
}
let elapsed = start.elapsed();
if elapsed >= timeout {
terminate_git_process(&mut child);
return Err(DaloError::CommandFailed {
program: program.to_owned(),
args: display_git_args(args),
cwd: path.to_path_buf(),
status: format!("timed out after {}", format_duration(timeout)),
stderr: humanize_git_failure(args, &timeout_stderr(&stderr)),
});
}
thread::sleep(GIT_POLL_INTERVAL.min(timeout - elapsed));
};
let stdout_text = read_tempfile_lossy(&stdout);
let stderr_text = read_tempfile_lossy(&stderr).trim().to_owned();
if status.success() {
return Ok(stdout_text);
}
Err(DaloError::CommandFailed {
program: program.to_owned(),
args: display_git_args(args),
cwd: path.to_path_buf(),
status: status
.code()
.map_or_else(|| "signal".to_owned(), |code| code.to_string()),
stderr: humanize_git_failure(args, &stderr_text),
})
}
fn terminate_git_process(child: &mut Child) {
#[cfg(unix)]
{
let process_group = Pid::from_child(child);
if kill_process_group(process_group, Signal::KILL).is_ok() {
let _ = child.wait();
return;
}
}
let _ = child.kill();
let _ = child.wait();
}
fn print_network_progress(message: &str) {
if std::io::stderr().is_terminal() {
eprintln!("{message}");
}
}
fn humanize_git_failure(args: &[&str], stderr: &str) -> String {
let raw = redact_urls_in_text(stderr.trim());
let Some(summary) = git_failure_summary(args) else {
return raw;
};
if raw.is_empty() {
return summary;
}
format!("{summary}\n\nGit said: {raw}")
}
fn git_failure_summary(args: &[&str]) -> Option<String> {
match args {
["clone", .., "--", url, _destination] if looks_like_remote_location(url) => {
Some(format!(
"Could not clone repository `{}`. Check the URL, network/proxy access, and repository permissions.",
display_git_value(url)
))
}
["clone", .., "--", url, _destination] => Some(format!(
"Could not clone local repository `{}`. Check that the path is readable and points to a Git repository.",
display_git_value(url)
)),
["pull", ..] => Some(
"Could not refresh this source. Check network/proxy access, repository permissions, and whether the tracking branch can fast-forward."
.to_owned(),
),
["fetch", ..] => Some(
"Could not check the repository for upstream changes. Check network/proxy access and repository permissions."
.to_owned(),
),
_ => None,
}
}
fn display_git_args(args: &[&str]) -> String {
args.iter()
.map(|arg| display_git_value(arg))
.collect::<Vec<_>>()
.join(" ")
}
fn preflight_local_clone_source(location: &str, cwd: &Path) -> DaloResult<()> {
let path = Path::new(location);
let local_path = if path.is_absolute() {
path.to_path_buf()
} else {
cwd.join(path)
};
if looks_like_remote_location(location) && !local_path.exists() {
return Ok(());
}
let metadata = match fs::metadata(&local_path) {
Ok(metadata) => metadata,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Err(DaloError::InvalidArgument {
reason: format!(
"local source path `{}` does not exist",
display_git_value(location)
),
});
}
Err(error) => return Err(DaloError::Io(error)),
};
if metadata.is_file() {
return Ok(());
}
let has_worktree_metadata = local_path.join(".git").try_exists()?;
let has_bare_metadata = local_path.join("HEAD").is_file()
&& local_path.join("objects").is_dir()
&& local_path.join("refs").is_dir();
if !metadata.is_dir() || (!has_worktree_metadata && !has_bare_metadata) {
return Err(DaloError::InvalidArgument {
reason: format!(
"local source path `{}` is not a Git repository (missing .git)",
display_git_value(location)
),
});
}
Ok(())
}
pub(crate) fn looks_like_remote_location(location: &str) -> bool {
if Path::new(location).is_absolute() {
return false;
}
if location.contains("://") {
return true;
}
let Some(colon) = location.find(':') else {
return false;
};
colon > 0
&& !location[..colon].contains('/')
&& location
.get(colon + 1..)
.is_some_and(|suffix| !suffix.is_empty())
}
fn display_git_value(value: &str) -> String {
redact_url_userinfo(value)
.chars()
.fold(String::new(), |mut escaped, character| {
if character.is_control() {
escaped.extend(character.escape_default());
} else {
escaped.push(character);
}
escaped
})
}
fn uses_allowed_git_transport(url: &str) -> bool {
let Some((scheme, remainder)) = url.split_once("://") else {
return !looks_like_remote_location(url) || is_scp_style_git_url(url);
};
match scheme.to_ascii_lowercase().as_str() {
"https" | "ssh" | "git" => has_url_authority(remainder),
"file" => !remainder.is_empty(),
_ => false,
}
}
fn has_url_authority(remainder: &str) -> bool {
remainder
.split(['/', '?', '#'])
.next()
.is_some_and(|authority| !authority.is_empty())
}
fn is_scp_style_git_url(url: &str) -> bool {
let Some((authority, path)) = url.split_once(':') else {
return false;
};
if authority.is_empty()
|| path.is_empty()
|| path.starts_with(':')
|| path.contains(':')
|| path
.chars()
.any(|character| character.is_whitespace() || character.is_control())
{
return false;
}
let (user, host) = authority
.split_once('@')
.map_or((None, authority), |(user, host)| (Some(user), host));
user.is_none_or(|user| {
!user.is_empty()
&& user
.chars()
.all(|character| character.is_ascii_alphanumeric() || "._-".contains(character))
}) && !host.is_empty()
&& !host.starts_with('-')
&& host
.chars()
.all(|character| character.is_ascii_alphanumeric() || ".-".contains(character))
}
fn url_has_forbidden_userinfo(url: &str) -> bool {
let Some(scheme_end) = url.find("://") else {
return false;
};
let authority = &url[scheme_end + 3..];
let authority_end = authority
.find(|character: char| ['/', '?', '#'].contains(&character))
.unwrap_or(authority.len());
let authority = &authority[..authority_end];
let Some(userinfo_end) = authority.rfind('@') else {
return false;
};
!url[..scheme_end].eq_ignore_ascii_case("ssh")
|| authority[..userinfo_end].is_empty()
|| authority[..userinfo_end].contains(':')
}
fn redact_url_userinfo(url: &str) -> String {
let Some(scheme_end) = url.find("://") else {
return url.to_owned();
};
let authority_start = scheme_end + 3;
let authority = &url[authority_start..];
let authority_end = authority
.find(|character: char| ['/', '?', '#'].contains(&character))
.unwrap_or(authority.len());
let Some(userinfo_end) = authority[..authority_end].rfind('@') else {
return url.to_owned();
};
format!(
"{}***@{}",
&url[..authority_start],
&authority[userinfo_end + 1..]
)
}
fn redact_urls_in_text(text: &str) -> String {
text.split_whitespace()
.map(redact_url_userinfo)
.collect::<Vec<_>>()
.join(" ")
}
fn configure_ssh_command(
command: &mut Command,
ssh_command_env: Option<&impl AsRef<OsStr>>,
core_ssh_command_configured: bool,
) {
if let Some(value) = ssh_command_env {
command.env("GIT_SSH_COMMAND", value.as_ref());
} else if !core_ssh_command_configured {
command.env("GIT_SSH_COMMAND", "ssh -oBatchMode=yes");
} else {
command.env_remove("GIT_SSH_COMMAND");
}
}
fn has_core_ssh_command(program: &str, path: &Path, timeout: Duration) -> bool {
run_git_program_with_options(
program,
path,
&["config", "--get", "core.sshCommand"],
timeout,
Option::<&str>::None,
true,
)
.is_ok()
}
fn cached_has_core_ssh_command(program: &str, path: &Path, timeout: Duration) -> bool {
let comparable_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
let cache = SSH_PREFLIGHT_CACHE.get_or_init(|| Mutex::new(BTreeMap::new()));
let cell = {
let mut cache = cache
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
cache
.entry((program.to_owned(), comparable_path))
.or_insert_with(|| Arc::new(OnceLock::new()))
.clone()
};
*cell.get_or_init(|| has_core_ssh_command(program, path, timeout))
}
fn git_timeout(default: Duration) -> Duration {
std::env::var(GIT_TIMEOUT_ENV)
.ok()
.and_then(|value| value.parse::<u64>().ok())
.filter(|seconds| *seconds > 0)
.map(Duration::from_secs)
.unwrap_or(default)
}
fn read_tempfile_lossy(file: &NamedTempFile) -> String {
fs::read(file.path())
.map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
.unwrap_or_default()
}
fn timeout_stderr(stderr: &NamedTempFile) -> String {
let text = read_tempfile_lossy(stderr).trim().to_owned();
if text.is_empty() {
"git command timed out; terminal prompts are disabled".to_owned()
} else {
text
}
}
fn format_duration(duration: Duration) -> String {
if duration.as_millis() < 1_000 {
format!("{}ms", duration.as_millis())
} else {
format!("{}s", duration.as_secs())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
#[test]
fn run_git_program_should_report_missing_binary() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let error = run_git_program(
"dalo-definitely-missing-git-binary",
temp_dir.path(),
&["--version"],
Duration::from_millis(10),
)
.expect_err("missing binary should fail");
assert!(matches!(error, DaloError::Io(_)));
}
#[test]
fn run_git_program_should_disable_interactive_prompts() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git",
"#!/bin/sh\nprintf 'prompt=%s ssh=%s protocols=%s from_user=%s global=%s nosystem=%s count=%s parameters=%s\\n' \"$GIT_TERMINAL_PROMPT\" \"$GIT_SSH_COMMAND\" \"$GIT_ALLOW_PROTOCOL\" \"$GIT_PROTOCOL_FROM_USER\" \"$GIT_CONFIG_GLOBAL\" \"$GIT_CONFIG_NOSYSTEM\" \"${GIT_CONFIG_COUNT:-unset}\" \"${GIT_CONFIG_PARAMETERS:-unset}\" >&2\nexit 2\n",
);
let error = run_git_program_with_options(
fake_git.to_str().expect("script path should be utf-8"),
temp_dir.path(),
&["pull"],
Duration::from_secs(3),
Option::<&str>::None,
false,
)
.expect_err("fake git should fail");
let DaloError::CommandFailed { stderr, .. } = error else {
panic!("expected command failure, got: {error}");
};
assert!(stderr.contains("prompt=0"));
assert!(stderr.contains("BatchMode=yes"));
assert!(stderr.contains("protocols=https:ssh:git:file"));
assert!(stderr.contains("from_user=0"));
assert!(stderr.contains("global=/dev/null"));
assert!(stderr.contains("nosystem=1"));
assert!(stderr.contains("count=unset"));
assert!(stderr.contains("parameters=unset"));
}
#[test]
fn run_git_program_should_preserve_user_ssh_command() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git",
"#!/bin/sh\nprintf 'ssh=%s\\n' \"$GIT_SSH_COMMAND\" >&2\nexit 2\n",
);
let error = run_git_program_with_options(
fake_git.to_str().expect("script path should be utf-8"),
temp_dir.path(),
&["fetch"],
Duration::from_secs(3),
Some("ssh -i deploy-key -oBatchMode=yes"),
false,
)
.expect_err("fake git should fail");
let DaloError::CommandFailed { stderr, .. } = error else {
panic!("expected command failure, got: {error}");
};
assert!(stderr.contains("ssh=ssh -i deploy-key -oBatchMode=yes"));
}
#[test]
fn run_git_program_should_not_override_core_ssh_command() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git",
"#!/bin/sh\nprintf \"ssh=${GIT_SSH_COMMAND:-unset}\\n\" >&2\nexit 2\n",
);
let error = run_git_program_with_options(
fake_git.to_str().expect("script path should be utf-8"),
temp_dir.path(),
&["fetch"],
Duration::from_secs(3),
Option::<&str>::None,
true,
)
.expect_err("fake git should fail");
let DaloError::CommandFailed { stderr, .. } = error else {
panic!("expected command failure, got: {error}");
};
assert!(stderr.contains("ssh=unset"));
assert!(!stderr.contains("BatchMode=yes"));
}
#[test]
fn run_git_program_should_timeout_hung_command() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git",
"#!/bin/sh\nwhile :; do :; done\n",
);
let error = run_git_program(
fake_git.to_str().expect("script path should be utf-8"),
temp_dir.path(),
&["pull"],
Duration::from_millis(10),
)
.expect_err("hung command should time out");
let DaloError::CommandFailed { status, stderr, .. } = error else {
panic!("expected command failure, got: {error}");
};
assert!(status.contains("timed out after"));
assert!(stderr.contains("terminal prompts are disabled"));
}
#[test]
fn run_git_program_should_skip_ssh_preflight_for_local_commands() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git",
"#!/bin/sh\nif [ \"$1\" = config ]; then : > preflight-called; exit 1; fi\nprintf 'ok\\n'\n",
);
let output = run_git_program(
fake_git.to_str().expect("script path should be utf-8"),
temp_dir.path(),
&["status"],
Duration::from_secs(3),
)
.expect("local command should run");
assert_eq!(output.trim(), "ok");
assert!(!temp_dir.path().join("preflight-called").exists());
}
#[test]
fn run_git_program_should_terminate_helper_processes_on_timeout() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let helper_activity_file = temp_dir.path().join("helper-activity");
let fake_git = write_executable(
temp_dir.path(),
"fake-git",
"#!/bin/sh\nwhile :; do printf . >> \"$1\"; sleep 0.01; done &\nwhile :; do sleep 1; done\n",
);
let helper_activity_file_arg = helper_activity_file
.to_str()
.expect("helper activity path should be utf-8");
let spawn_deadline = Instant::now() + Duration::from_secs(2);
let error = loop {
match run_git_program_with_options(
fake_git.to_str().expect("script path should be utf-8"),
temp_dir.path(),
&[helper_activity_file_arg],
Duration::from_secs(3),
Option::<&str>::None,
false,
) {
Err(DaloError::Io(io_error))
if io_error.kind() == std::io::ErrorKind::ExecutableFileBusy
&& Instant::now() < spawn_deadline =>
{
thread::sleep(Duration::from_millis(50));
}
Err(error) => break error,
Ok(output) => panic!("hung command should time out, got output {output:?}"),
}
};
let DaloError::CommandFailed { status, .. } = error else {
panic!("hung command should time out with a command failure, got: {error}");
};
assert!(
status.contains("timed out after"),
"expected a timeout status, got {status:?}"
);
let quiet_window = Duration::from_millis(100);
let quiet_deadline = Instant::now() + Duration::from_secs(2);
let mut activity = fs::read(&helper_activity_file)
.expect("helper should record activity before the timeout");
loop {
thread::sleep(quiet_window);
let now =
fs::read(&helper_activity_file).expect("helper activity should remain readable");
if now == activity {
break;
}
assert!(
Instant::now() < quiet_deadline,
"timeout should terminate the helper process; it kept writing for 2s"
);
activity = now;
}
}
#[test]
fn core_ssh_preflight_should_share_the_git_timeout() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git",
"#!/bin/sh\nif [ \"$1\" = config ]; then while :; do :; done; fi\nprintf 'ok\\n'\n",
);
let start = Instant::now();
let output = run_git_program_with_ssh_preflight(
fake_git.to_str().expect("script path should be utf-8"),
temp_dir.path(),
&["--version"],
Duration::from_secs(1),
true,
)
.expect("main command should run after the timed-out preflight");
assert_eq!(output.trim(), "ok");
assert!(start.elapsed() < Duration::from_secs(5));
}
#[test]
fn core_ssh_preflight_should_run_once_per_repository() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git-once",
"#!/bin/sh\nprintf '%s prompt=%s ssh=%s\\n' \"$1\" \"$GIT_TERMINAL_PROMPT\" \"$GIT_SSH_COMMAND\" >> invocations\n[ \"$1\" != config ]\n",
);
let program = fake_git.to_str().expect("script path should be utf-8");
for _ in 0..2 {
run_git_program_with_ssh_preflight_and_env(
program,
temp_dir.path(),
&["fetch"],
Duration::from_secs(3),
true,
None,
)
.expect("network command should run");
}
let invocations = fs::read_to_string(temp_dir.path().join("invocations"))
.expect("shim log should be readable");
assert_eq!(invocations.matches("config ").count(), 1);
assert_eq!(invocations.matches("fetch ").count(), 2);
assert!(invocations.lines().all(|line| line.contains("prompt=0")));
assert!(
invocations
.lines()
.filter(|line| line.starts_with("fetch "))
.all(|line| line.contains("BatchMode=yes"))
);
}
#[test]
fn explicit_ssh_command_should_skip_the_redundant_preflight() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git-explicit-ssh",
"#!/bin/sh\nprintf '%s ssh=%s\\n' \"$1\" \"$GIT_SSH_COMMAND\" >> invocations\n[ \"$1\" != config ]\n",
);
run_git_program_with_ssh_preflight_and_env(
fake_git.to_str().expect("script path should be utf-8"),
temp_dir.path(),
&["fetch"],
Duration::from_secs(3),
true,
Some(OsStr::new("ssh -i explicit-key -oBatchMode=yes")),
)
.expect("network command should preserve the explicit SSH policy");
let invocations = fs::read_to_string(temp_dir.path().join("invocations"))
.expect("shim log should be readable");
assert!(!invocations.contains("config "));
assert!(invocations.contains("fetch ssh=ssh -i explicit-key -oBatchMode=yes"));
}
#[test]
fn bounded_git_shim_should_cap_concurrency_and_preserve_result_order() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git-parallel",
"#!/bin/sh\nsleep 0.03\ncase \"$PWD\" in *job-1|*job-5) exit 7;; esac\n",
);
let jobs = (0..7)
.map(|index| {
let path = temp_dir.path().join(format!("job-{index}"));
fs::create_dir(&path).expect("job directory should be created");
path
})
.collect::<Vec<_>>();
let active = AtomicUsize::new(0);
let maximum = AtomicUsize::new(0);
let results = run_bounded(&jobs, 3, |path| {
let current = active.fetch_add(1, Ordering::SeqCst) + 1;
maximum.fetch_max(current, Ordering::SeqCst);
let result = run_git_program_with_options(
fake_git.to_str().expect("script path should be utf-8"),
path,
&["fetch"],
Duration::from_secs(3),
Option::<&str>::None,
false,
)
.map(|_| ());
active.fetch_sub(1, Ordering::SeqCst);
result
});
assert!(maximum.load(Ordering::SeqCst) >= 2);
assert!(maximum.load(Ordering::SeqCst) <= 3);
assert_eq!(results.len(), jobs.len());
for (index, result) in results.iter().enumerate() {
assert_eq!(result.is_err(), matches!(index, 1 | 5));
}
assert_eq!(active.load(Ordering::SeqCst), 0);
}
#[test]
fn git_poll_interval_should_not_add_a_50ms_latency_floor() {
assert!(GIT_POLL_INTERVAL < Duration::from_millis(50));
}
#[test]
fn humanize_git_failure_should_explain_clone_errors() {
let message = humanize_git_failure(
&[
"clone",
"--quiet",
"--",
"https://example.invalid/repo.git",
"/tmp/checkout",
],
"fatal: unable to access repository",
);
assert!(message.contains("Could not clone repository"));
assert!(message.contains("https://example.invalid/repo.git"));
assert!(message.contains("Git said: fatal: unable to access repository"));
}
#[test]
fn humanize_git_failure_should_not_give_network_advice_for_local_clones() {
let message = humanize_git_failure(
&["clone", "--quiet", "--", "/tmp/team", "/tmp/checkout"],
"Schwerwiegend: kein Git-Repository",
);
assert!(message.contains("Could not clone local repository `/tmp/team`"));
assert!(message.contains("path is readable"));
assert!(!message.contains("network/proxy"));
}
#[test]
fn local_clone_preflight_should_report_missing_and_non_repository_paths() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let missing = temp_dir.path().join("missing");
let error = preflight_local_clone_source(&missing.to_string_lossy(), temp_dir.path())
.expect_err("missing local source should fail preflight");
assert_eq!(
error.to_string(),
format!("local source path `{}` does not exist", missing.display())
);
let plain_directory = temp_dir.path().join("plain");
fs::create_dir(&plain_directory).expect("plain directory should be created");
let error =
preflight_local_clone_source(&plain_directory.to_string_lossy(), temp_dir.path())
.expect_err("non-repository local source should fail preflight");
assert_eq!(
error.to_string(),
format!(
"local source path `{}` is not a Git repository (missing .git)",
plain_directory.display()
)
);
}
#[test]
fn local_clone_preflight_should_allow_git_bundle_files() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let source = temp_dir.path().join("source");
fs::create_dir(&source).expect("source directory should be created");
init_repo(&source).expect("source repository should initialize");
fs::write(source.join("README.md"), "# Bundle\n")
.expect("bundle content should be written");
run_git(&source, &["add", "README.md"]).expect("bundle content should be staged");
run_git(
&source,
&[
"-c",
"user.name=Dalo Test",
"-c",
"user.email=dalo@example.invalid",
"-c",
"commit.gpgSign=false",
"commit",
"-qm",
"bundle fixture",
],
)
.expect("bundle source should commit");
let bundle = temp_dir.path().join("source.bundle");
let bundle_arg = bundle.to_string_lossy().into_owned();
run_git(&source, &["bundle", "create", &bundle_arg, "--all"])
.expect("bundle should be created");
preflight_local_clone_source(&bundle_arg, temp_dir.path())
.expect("valid Git bundle should pass local preflight");
clone_repo(&bundle_arg, &temp_dir.path().join("clone"))
.expect("valid Git bundle should remain cloneable");
}
#[test]
fn local_clone_preflight_should_escape_control_characters() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let missing = temp_dir.path().join("missing\nrepo");
let error = preflight_local_clone_source(&missing.to_string_lossy(), temp_dir.path())
.expect_err("missing local source should fail preflight");
let message = error.to_string();
assert!(message.contains("missing\\nrepo"));
assert!(!message.contains("missing\nrepo"));
}
#[test]
fn humanize_git_failure_should_redact_url_userinfo() {
let secret_url = "https://octo:token-value@example.invalid/repo.git";
let message = humanize_git_failure(
&["clone", "--quiet", "--", secret_url, "/tmp/checkout"],
&format!("fatal: unable to access '{secret_url}': denied"),
);
assert!(message.contains("https://***@example.invalid/repo.git"));
assert!(!message.contains("token-value"));
}
#[test]
fn validate_remote_url_should_allow_supported_transports() {
for location in [
"/tmp/repo",
"./repo",
"https://example.invalid/repo.git",
"ssh://git@example.invalid/repo.git",
"git://example.invalid/repo.git",
"file:///tmp/repo",
"git@example.invalid:owner/repo.git",
"example.invalid:owner/repo.git",
] {
assert!(
validate_remote_url(location).is_ok(),
"expected `{location}` to be accepted"
);
}
}
#[test]
fn validate_remote_url_should_reject_unsafe_transports_and_userinfo() {
for location in [
"ext::sh -c 'touch /tmp/dalo-rce'",
"ext::sh",
"http://example.invalid/repo.git",
"git+ssh://example.invalid/repo.git",
"rsync://example.invalid/repo.git",
"https://octo:token-value@example.invalid/repo.git",
"ssh://octo:token-value@example.invalid/repo.git",
"git@example.invalid:owner:repo.git",
"git@-example.invalid:owner/repo.git",
] {
assert!(
matches!(
validate_remote_url(location),
Err(DaloError::UnsafeRemoteUrl)
),
"expected `{location}` to be rejected"
);
}
}
#[test]
fn clone_repo_should_reject_unsafe_transport_before_spawning_git() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let destination = temp_dir.path().join("checkout");
let error = clone_repo("ext::sh -c 'touch /tmp/dalo-rce'", &destination)
.expect_err("unsafe transport should be rejected before cloning");
assert!(matches!(error, DaloError::UnsafeRemoteUrl));
assert!(!destination.exists());
}
#[test]
fn pinned_acquisition_should_reject_invalid_input_before_creating_checkout() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let destination = temp.path().join("checkout");
for commit in ["main", "--upload-pack=bad", &"A".repeat(40)] {
assert!(
acquire_pinned_commit("https://example.invalid/repo.git", &destination, commit)
.is_err()
);
assert!(!destination.exists());
}
assert!(matches!(
acquire_pinned_commit("ext::sh", &destination, &"0".repeat(40)),
Err(DaloError::UnsafeRemoteUrl)
));
assert!(!destination.exists());
}
#[test]
fn pinned_project_fetch_configures_partial_clone_before_fetch() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let destination = temp.path().join("checkout");
fs::create_dir(&destination).expect("checkout should be created");
run_git(&destination, &["init", "-q"]).expect("repository should initialize");
run_git(
&destination,
&[
"remote",
"add",
"--",
"origin",
"https://example.invalid/repo.git",
],
)
.expect("origin should be configured");
configure_partial_clone_remote(&destination)
.expect("partial-clone configuration should succeed before fetching");
assert_eq!(
run_git(
&destination,
&["config", "--get", "core.repositoryformatversion"]
)
.expect("repository format version should be configured")
.trim(),
"1"
);
assert_eq!(
run_git(
&destination,
&["config", "--get", "extensions.partialClone"]
)
.expect("partial-clone extension should be configured")
.trim(),
"origin"
);
assert_eq!(
run_git(&destination, &["config", "--get", "remote.origin.promisor"])
.expect("origin should be marked as a promisor remote")
.trim(),
"true"
);
assert_eq!(
run_git(
&destination,
&["config", "--get", "remote.origin.partialclonefilter"],
)
.expect("origin's partial-clone filter should be configured")
.trim(),
"blob:none"
);
}
#[test]
fn project_skill_scope_should_infer_common_visible_parent_and_validate_paths() {
let paths = vec![
".agents/skills/internal/SKILL.md".to_owned(),
"skills/one/SKILL.md".to_owned(),
"skills/two/SKILL.md".to_owned(),
"skills/readme.md".to_owned(),
];
assert_eq!(
resolve_project_skill_scope_from_paths(&paths, None).unwrap(),
Some(PathBuf::from("skills"))
);
assert_eq!(
resolve_project_skill_scope_from_paths(&paths, Some("skills")).unwrap(),
Some(PathBuf::from("skills"))
);
assert_eq!(
resolve_project_skill_scope_from_paths(&["SKILL.md".to_owned()], None).unwrap(),
None,
"a root-level skill makes the repository root the inferred scope"
);
assert_eq!(
validate_project_skill_path(".").unwrap(),
None,
"dot explicitly selects the repository root"
);
assert_eq!(
validate_project_skill_path("/").unwrap(),
None,
"slash explicitly selects the repository root"
);
for invalid in [
"",
"/skills",
"C:/skills",
"skills/../other",
"skills//other",
"skills\\other",
] {
assert!(
validate_project_skill_path(invalid).is_err(),
"`{invalid}` should be rejected"
);
}
}
#[test]
fn pinned_acquisition_should_preserve_supported_commit_formats() {
let temp = tempfile::tempdir().expect("tempdir should be created");
for format in ["sha1", "sha256"] {
let source = temp.path().join(format);
fs::create_dir(&source).expect("source should be created");
let initialized = if format == "sha1" {
run_git(
&source,
&["-c", "init.defaultObjectFormat=sha1", "init", "-q"],
)
} else {
run_git(&source, &["init", "-q", "--object-format=sha256"])
};
if let Err(DaloError::CommandFailed { status, stderr, .. }) = &initialized
&& format == "sha256"
&& status == "129"
&& stderr.contains("unknown option")
&& stderr.contains("object-format")
{
eprintln!("Git lacks SHA-256 support; SHA-1 acquisition was verified");
continue;
}
initialized.expect("source should initialize");
fs::write(source.join("SKILL.md"), "# Pinned skill\n")
.expect("skill should be written");
run_git(&source, &["add", "."]).expect("skill should stage");
run_git(
&source,
&[
"-c",
"user.name=Dalo Test",
"-c",
"user.email=dalo@example.invalid",
"-c",
"commit.gpgSign=false",
"commit",
"-qm",
"pinned fixture",
],
)
.expect("source should commit");
let commit = rev_parse_head(&source).expect("source HEAD should resolve");
assert_eq!(commit.len(), if format == "sha256" { 64 } else { 40 });
let checkout = temp.path().join(format!("checkout-{format}"));
acquire_pinned_commit(
source.to_str().expect("UTF-8 source path"),
&checkout,
&commit,
)
.expect("pin should restore");
assert_eq!(
rev_parse_head(&checkout).expect("HEAD should resolve"),
commit
);
assert_eq!(
fs::read(checkout.join("SKILL.md")).unwrap(),
b"# Pinned skill\n"
);
assert_eq!(
run_git(&checkout, &["rev-list", "--count", "HEAD"])
.unwrap()
.trim(),
"1"
);
}
}
#[test]
fn is_dirty_should_ignore_untracked_files_but_report_tracked_changes() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let repo = temp.path().join("repo");
fs::create_dir_all(&repo).expect("repo dir should be created");
run_git(&repo, &["init", "-q"]).expect("repo should initialize");
fs::write(repo.join("SKILL.md"), "# Skill\n").expect("skill should be written");
run_git(&repo, &["add", "."]).expect("files should stage");
run_git(
&repo,
&[
"-c",
"commit.gpgsign=false",
"-c",
"user.email=test@example.com",
"-c",
"user.name=Test User",
"commit",
"-m",
"initial",
"-q",
],
)
.expect("initial commit should succeed");
fs::write(repo.join(".DS_Store"), b"junk").expect("untracked file should be written");
assert!(!is_dirty(&repo).expect("status should run"));
fs::write(repo.join("SKILL.md"), "# Changed\n").expect("tracked file should change");
assert!(is_dirty(&repo).expect("status should run"));
}
#[test]
fn validate_manifest_revision_should_reject_revision_expressions() {
for accepted in [
"0123456789abcdef0123456789abcdef01234567",
"v1.0.0",
"main",
"release/2024",
] {
assert!(
validate_manifest_revision(accepted).is_ok(),
"expected `{accepted}` to be accepted"
);
}
for rejected in [
"", "-x", "a b", "main^", "HEAD~1", "a..b", "HEAD@{1}", "a:b", "a*",
] {
assert!(
validate_manifest_revision(rejected).is_err(),
"expected `{rejected}` to be rejected"
);
}
}
#[test]
fn humanize_git_failure_should_explain_tracking_refresh_errors() {
let message =
humanize_git_failure(&["pull", "--ff-only", "--quiet"], "fatal: not possible");
assert!(message.contains("Could not refresh this source"));
assert!(message.contains("Git said: fatal: not possible"));
}
#[test]
fn revision_is_missing_should_not_classify_ambiguous_hash_as_missing() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(
temp_dir.path(),
"fake-git",
"#!/bin/sh\nprintf '%s\\n' 0123456789abcdef0123456789abcdef01234567 0123456789abcdef0123456789abcdef01234568\n",
);
assert!(!revision_is_missing_with_program(
fake_git.to_str().expect("fake git path should be utf-8"),
temp_dir.path(),
"0123",
));
}
#[test]
fn revision_is_missing_should_not_classify_other_git_failures_as_missing() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let fake_git = write_executable(temp_dir.path(), "fake-git", "#!/bin/sh\nexit 2\n");
assert!(!revision_is_missing_with_program(
fake_git.to_str().expect("fake git path should be utf-8"),
temp_dir.path(),
"does-not-exist",
));
}
#[test]
fn clone_repo_should_treat_dash_prefixed_url_as_repository_path() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let repo = temp_dir.path().join("--upload-pack=touch-owned");
let destination = temp_dir.path().join("checkout");
fs::create_dir_all(&repo).expect("repo dir should be created");
run_git_program(
"git",
&repo,
&["init", "-q", "--bare"],
Duration::from_secs(5),
)
.expect("bare repo should be initialized");
clone_repo(
repo.file_name()
.expect("repo should have file name")
.to_str()
.expect("repo name should be utf-8"),
&destination,
)
.expect("dash-prefixed repo path should clone after -- separator");
assert!(destination.join(".git").is_dir());
}
fn write_executable(dir: &Path, name: &str, body: &str) -> PathBuf {
let path = dir.join(name);
let mut writer = Command::new("sh")
.args(["-c", "exec cat > \"$1\"", "dalo-test-script-writer"])
.arg(&path)
.stdin(Stdio::piped())
.spawn()
.expect("script writer should start");
let mut stdin = writer
.stdin
.take()
.expect("script writer should have stdin");
stdin
.write_all(body.as_bytes())
.expect("script body should be written");
drop(stdin);
let status = writer.wait().expect("script writer should finish");
assert!(status.success(), "script writer should succeed: {status}");
let mut permissions = fs::metadata(&path)
.expect("script metadata should be readable")
.permissions();
permissions.set_mode(0o755);
fs::set_permissions(&path, permissions).expect("script should be executable");
path
}
}