use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Component, Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::audit::{self, AuditReport};
use crate::catalog::{self, SourceLock};
use crate::config::UserConfig;
use crate::error::{DaloError, DaloResult};
use crate::git;
use crate::inventory::InventoryWarning;
use crate::materialize::MaterializeOperationKind;
use crate::store::{self, ApprovalsFile, StorePaths};
pub const MAX_SOURCE_ID_LENGTH: usize = 128;
pub const SOURCE_ID_REQUIREMENTS: &str = "source ids must be at most 128 characters; they must be non-empty, not `.`/`..`, and only contain `[A-Za-z0-9._-]`";
pub const SOURCE_NAMESPACE_REQUIREMENTS: &str = "source namespaces must be portable skill names: lowercase ASCII letters, digits, `-`, `_`, or `.`, without leading or trailing dots";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SourceKind {
Local,
Team,
Catalog,
}
impl SourceKind {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Local => "local",
Self::Team => "team",
Self::Catalog => "catalog",
}
}
}
impl std::fmt::Display for SourceKind {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SourceConfig {
pub id: String,
pub kind: SourceKind,
pub path: PathBuf,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub subpath: Option<PathBuf>,
pub priority: i32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub namespace: Option<String>,
pub enabled: bool,
pub trusted: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub update_policy: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub selection: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub declared_by: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub declared_ref: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceAddReport {
pub source: SourceConfig,
pub dry_run: bool,
pub audits: Vec<AuditReport>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub inventory_warnings: Vec<InventoryWarning>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TrackingSourceRefreshFailure {
pub id: String,
pub path: PathBuf,
pub reason: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceListReport {
pub sources: Vec<SourceListEntry>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceListEntry {
#[serde(flatten)]
pub source: SourceConfig,
pub provenance: SourceProvenance,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SourceManagement {
Direct,
TeamManifest,
}
impl SourceManagement {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Direct => "direct",
Self::TeamManifest => "team_manifest",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceProvenance {
pub management: SourceManagement,
#[serde(skip_serializing_if = "Option::is_none")]
pub declared_by: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub requested_ref: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub resolved_commit: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub checkout_commit: Option<String>,
}
#[derive(Debug, Default)]
pub(crate) struct SourceHeadCache {
commits: BTreeMap<PathBuf, Result<String, String>>,
}
impl SourceHeadCache {
pub(crate) fn resolve(&mut self, source: &SourceConfig) -> Option<String> {
self.resolve_result(source).ok()
}
pub(crate) fn resolve_result(&mut self, source: &SourceConfig) -> Result<String, String> {
self.commits
.entry(source.path.clone())
.or_insert_with(|| git::rev_parse_head(&source.path).map_err(|error| error.to_string()))
.clone()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourcePriorityReport {
pub source: SourceConfig,
pub changed: bool,
pub dry_run: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceNamespaceReport {
pub source: SourceConfig,
pub changed: bool,
pub dry_run: bool,
}
#[derive(Debug, Clone)]
pub struct SourceRemovalPlan {
pub original_config: UserConfig,
pub original_source_lock: SourceLock,
pub original_approvals: ApprovalsFile,
pub config: UserConfig,
pub source_lock: SourceLock,
pub approvals: ApprovalsFile,
pub report: SourceRemoveReport,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceRemoveReport {
pub source_id: String,
pub checkout_path: PathBuf,
pub cascaded_sources: Vec<String>,
pub cascaded_checkout_paths: Vec<PathBuf>,
pub kept_checkout: bool,
pub removed_approvals: usize,
pub removed_catalog_lock: bool,
pub reconciled_links: Vec<SourceRemoveLink>,
pub deactivated_skills: Vec<String>,
pub deactivated_instruction_packs: Vec<crate::instructions::InstructionRemovalOperation>,
pub cleanup_warnings: Vec<String>,
pub affected_paths: Vec<PathBuf>,
pub dry_run: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceRemoveLink {
pub kind: MaterializeOperationKind,
pub path: PathBuf,
}
pub fn plan_remove_source(
paths: &StorePaths,
id: &str,
keep_checkout: bool,
dry_run: bool,
) -> DaloResult<SourceRemovalPlan> {
let original_config = store::read_config(paths)?;
let source = original_config
.sources
.iter()
.find(|source| source.id == id)
.cloned()
.ok_or_else(|| {
DaloError::unknown_source(
id,
original_config
.sources
.iter()
.map(|candidate| candidate.id.clone())
.collect(),
)
})?;
if let Some(team) = &source.declared_by {
return Err(DaloError::StateError {
reason: format!(
"source `{id}` is managed by `{team}`; edit `{}` in that team repository",
crate::team_manifest::TEAM_MANIFEST_FILE
),
});
}
if source.kind == SourceKind::Local {
return Err(DaloError::InvalidSourceId {
id: id.to_owned(),
reason: "the built-in local source cannot be removed".to_owned(),
});
}
let original_source_lock = catalog::read_source_lock(paths)?;
let original_approvals = store::read_approvals(paths)?;
let mut removed_sources = original_config
.sources
.iter()
.filter(|candidate| candidate.id == id || candidate.declared_by.as_deref() == Some(id))
.cloned()
.collect::<Vec<_>>();
removed_sources.sort_by(|left, right| left.id.cmp(&right.id));
let removed_source_ids = removed_sources
.iter()
.map(|candidate| candidate.id.clone())
.collect::<BTreeSet<_>>();
let mut config = original_config.clone();
config
.sources
.retain(|candidate| !removed_source_ids.contains(&candidate.id));
sort_sources(&mut config.sources);
let mut source_lock = original_source_lock.clone();
let before_catalogs = source_lock.catalogs.len();
source_lock
.catalogs
.retain(|entry| !removed_source_ids.contains(&entry.source_id));
let removed_catalog_lock = source_lock.catalogs.len() != before_catalogs;
let mut approvals = original_approvals.clone();
let before_approvals = approvals.approvals.len();
approvals.approvals.retain(|approval| {
!removed_source_ids.iter().any(|source_id| {
approval.value.starts_with(&format!("{source_id}:"))
|| approval.scope == "source" && approval.value == *source_id
})
});
let removed_approvals = before_approvals - approvals.approvals.len();
let mut affected_paths = vec![
paths.config_file.clone(),
paths.source_lock_file.clone(),
paths.approvals_file.clone(),
paths.lock_file.clone(),
paths.state_file.clone(),
];
let cascaded = removed_sources
.iter()
.filter(|candidate| candidate.id != id)
.cloned()
.collect::<Vec<_>>();
if !keep_checkout {
affected_paths.extend(removed_sources.iter().map(|candidate| {
candidate
.path
.parent()
.map_or_else(|| candidate.path.clone(), Path::to_path_buf)
}));
}
Ok(SourceRemovalPlan {
original_config,
original_source_lock,
original_approvals,
config,
source_lock,
approvals,
report: SourceRemoveReport {
source_id: id.to_owned(),
checkout_path: source.path,
cascaded_sources: cascaded.iter().map(|source| source.id.clone()).collect(),
cascaded_checkout_paths: cascaded.iter().map(|source| source.path.clone()).collect(),
kept_checkout: keep_checkout,
removed_approvals,
removed_catalog_lock,
reconciled_links: Vec::new(),
deactivated_skills: Vec::new(),
deactivated_instruction_packs: Vec::new(),
cleanup_warnings: Vec::new(),
affected_paths,
dry_run,
},
})
}
pub fn sort_sources(sources: &mut [SourceConfig]) {
sources.sort_by(|left, right| {
left.priority
.cmp(&right.priority)
.then_with(|| left.id.cmp(&right.id))
});
}
pub fn add_team_source(
paths: &StorePaths,
id: &str,
url: &str,
namespace: Option<&str>,
dry_run: bool,
) -> DaloResult<SourceAddReport> {
add_team_source_scoped(paths, id, url, namespace, None, None, dry_run)
}
pub fn add_team_source_scoped(
paths: &StorePaths,
id: &str,
url: &str,
namespace: Option<&str>,
revision: Option<&str>,
subpath: Option<&Path>,
dry_run: bool,
) -> DaloResult<SourceAddReport> {
add_team_source_with_config_writer(
paths,
id,
url,
SourceAddOptions {
namespace,
revision,
subpath,
},
dry_run,
store::write_config,
)
}
#[derive(Clone, Copy, Default)]
struct SourceAddOptions<'a> {
namespace: Option<&'a str>,
revision: Option<&'a str>,
subpath: Option<&'a Path>,
}
#[must_use]
pub fn resolve_source_location(location: &str, cwd: &Path) -> String {
let path = Path::new(location);
if path.is_absolute() {
return location.to_owned();
}
let local_path = normalize_local_path(&cwd.join(path));
if git::looks_like_remote_location(location) && !local_path.exists() {
return location.to_owned();
}
local_path.to_string_lossy().into_owned()
}
fn normalize_local_path(path: &Path) -> PathBuf {
let mut normalized = PathBuf::new();
for component in path.components() {
match component {
Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
Component::RootDir => normalized.push(Path::new("/")),
Component::CurDir => {}
Component::ParentDir => {
if normalized.file_name().is_some() {
normalized.pop();
} else if !normalized.has_root() {
normalized.push("..");
}
}
Component::Normal(part) => normalized.push(part),
}
}
normalized
}
fn add_team_source_with_config_writer<F>(
paths: &StorePaths,
id: &str,
url: &str,
options: SourceAddOptions<'_>,
dry_run: bool,
write_config: F,
) -> DaloResult<SourceAddReport>
where
F: FnOnce(&StorePaths, &UserConfig) -> DaloResult<()>,
{
add_team_source_with_config_writer_and_cloner(
paths,
id,
url,
options,
dry_run,
write_config,
git::clone_repo,
)
}
fn add_team_source_with_config_writer_and_cloner<F, C>(
paths: &StorePaths,
id: &str,
url: &str,
options: SourceAddOptions<'_>,
dry_run: bool,
write_config: F,
clone_repo: C,
) -> DaloResult<SourceAddReport>
where
F: FnOnce(&StorePaths, &UserConfig) -> DaloResult<()>,
C: FnOnce(&str, &std::path::Path) -> DaloResult<()>,
{
if !is_valid_source_id(id) {
return Err(DaloError::InvalidSourceId {
id: id.to_owned(),
reason: SOURCE_ID_REQUIREMENTS.to_owned(),
});
}
let namespace = validate_source_namespace(options.namespace)?;
let subpath = options.subpath.map(validate_source_subpath).transpose()?;
if let Some(revision) = options.revision {
git::validate_manifest_revision(revision)?;
}
git::validate_remote_url(url)?;
let mut config = store::read_config(paths)?;
if config.sources.iter().any(|source| source.id == id) {
return Err(DaloError::SourceAlreadyExists {
source_id: id.to_owned(),
});
}
let checkout = paths.sources_dir.join(id).join("checkout");
let priority = config
.sources
.iter()
.map(|source| source.priority)
.max()
.unwrap_or(0)
+ 10;
let source = SourceConfig {
id: id.to_owned(),
kind: SourceKind::Team,
path: checkout.clone(),
subpath: subpath.clone(),
priority,
namespace,
enabled: true,
trusted: true,
url: Some(url.to_owned()),
branch: options.revision.map(str::to_owned),
update_policy: Some(
if options.revision.is_some() {
"pin"
} else {
"track"
}
.to_owned(),
),
selection: Vec::new(),
declared_by: None,
declared_ref: None,
};
if dry_run {
return Ok(SourceAddReport {
source,
dry_run: true,
audits: Vec::new(),
inventory_warnings: Vec::new(),
});
}
clone_source_checkout_with(url, &checkout, clone_repo)?;
let checkout_result = (|| -> DaloResult<PathBuf> {
if let Some(revision) = options.revision {
let commit = git::resolve_manifest_revision(&checkout, revision)?;
git::checkout_detached(&checkout, &commit)?;
}
scoped_source_root(&checkout, subpath.as_deref())
})();
let source_root = checkout_result.inspect_err(|_| {
let _ = std::fs::remove_dir_all(&checkout);
remove_empty_source_dir(&checkout);
})?;
let (audits, inventory_warnings) =
audit_source_checkout(paths, id, &source_root).inspect_err(|_| {
let _ = std::fs::remove_dir_all(&checkout);
remove_empty_source_dir(&checkout);
})?;
finish_team_source(paths, &mut config, source.clone(), write_config).inspect_err(|_| {
let _ = std::fs::remove_dir_all(&checkout);
remove_empty_source_dir(&checkout);
})?;
Ok(SourceAddReport {
source,
dry_run: false,
audits,
inventory_warnings,
})
}
pub fn validate_source_namespace(namespace: Option<&str>) -> DaloResult<Option<String>> {
let Some(namespace) = namespace else {
return Ok(None);
};
let namespace = namespace.trim();
if crate::inventory::is_valid_slot_name(namespace) {
Ok(Some(namespace.to_owned()))
} else {
Err(DaloError::InvalidArgument {
reason: SOURCE_NAMESPACE_REQUIREMENTS.to_owned(),
})
}
}
pub fn validate_source_subpath(subpath: &Path) -> DaloResult<PathBuf> {
let mut normalized = PathBuf::new();
for component in subpath.components() {
match component {
Component::Normal(part) => normalized.push(part),
_ => {
return Err(DaloError::InvalidArgument {
reason: format!(
"source subpath `{}` must be relative and contain no `.` or `..` components",
subpath.display()
),
});
}
}
}
if normalized.as_os_str().is_empty() {
return Err(DaloError::InvalidArgument {
reason: "source subpath must name a directory inside the checkout".to_owned(),
});
}
Ok(normalized)
}
pub fn scoped_source_root(checkout: &Path, subpath: Option<&Path>) -> DaloResult<PathBuf> {
let Some(subpath) = subpath else {
return Ok(checkout.to_path_buf());
};
let subpath = validate_source_subpath(subpath)?;
let checkout_canonical = fs::canonicalize(checkout)?;
let candidate = checkout.join(&subpath);
let candidate_canonical =
fs::canonicalize(&candidate).map_err(|error| DaloError::CheckFailed {
reason: format!(
"source subpath `{}` is unavailable: {error}",
subpath.display()
),
})?;
if !candidate_canonical.starts_with(&checkout_canonical) || !candidate_canonical.is_dir() {
return Err(DaloError::CheckFailed {
reason: format!(
"source subpath `{}` must resolve to a directory inside the checkout",
subpath.display()
),
});
}
Ok(candidate_canonical)
}
fn audit_source_checkout(
paths: &StorePaths,
source_id: &str,
source_root: &Path,
) -> DaloResult<(Vec<AuditReport>, Vec<InventoryWarning>)> {
let inventory = crate::inventory::scan_source(source_id, source_root)?;
let audits = inventory
.skills
.iter()
.map(|skill| {
audit::audit_skill(
paths,
&skill.source_ref,
&skill.path,
&audit::AuditOptions {
exclude_root_source_metadata: store::comparable_path(&skill.path)
== store::comparable_path(source_root),
..audit::AuditOptions::default()
},
)
})
.collect::<DaloResult<Vec<_>>>()?;
Ok((audits, inventory.warnings))
}
pub fn clone_source_checkout(url: &str, checkout: &Path) -> DaloResult<()> {
clone_source_checkout_with(url, checkout, git::clone_repo)
}
pub(crate) fn clone_scoped_catalog_checkout(
url: &str,
checkout: &Path,
commit: &str,
scope: &str,
) -> DaloResult<()> {
clone_source_checkout_with(url, checkout, |url, temporary| {
git::acquire_pinned_project_commit(url, temporary, commit, Some(scope))?;
git::mark_scoped_catalog_checkout(temporary, scope)
})
}
fn clone_source_checkout_with<C>(url: &str, checkout: &Path, clone_repo: C) -> DaloResult<()>
where
C: FnOnce(&str, &Path) -> DaloResult<()>,
{
if checkout.exists() {
if checkout.join(".git").exists() {
return Err(DaloError::SourceCheckoutExists {
path: checkout.to_path_buf(),
reason: "restore its source config or move/remove the checkout before retrying"
.to_owned(),
});
}
std::fs::remove_dir_all(checkout)?;
}
let Some(parent) = checkout.parent() else {
return Err(DaloError::InvalidStorePath {
path: checkout.to_path_buf(),
reason: "source checkout has no parent directory".to_owned(),
});
};
std::fs::create_dir_all(parent)?;
remove_interrupted_clone_dirs(parent)?;
let temporary_checkout =
checkout.with_file_name(format!(".checkout-tmp-{}", std::process::id()));
clone_repo(url, &temporary_checkout).inspect_err(|_| {
let _ = std::fs::remove_dir_all(&temporary_checkout);
remove_empty_source_dir(checkout);
})?;
std::fs::rename(&temporary_checkout, checkout).inspect_err(|_| {
let _ = std::fs::remove_dir_all(&temporary_checkout);
remove_empty_source_dir(checkout);
})?;
Ok(())
}
fn remove_interrupted_clone_dirs(parent: &Path) -> DaloResult<()> {
for entry in std::fs::read_dir(parent)? {
let entry = entry?;
if !entry
.file_name()
.to_string_lossy()
.starts_with(".checkout-tmp-")
{
continue;
}
let metadata = std::fs::symlink_metadata(entry.path())?;
if metadata.file_type().is_dir() && !metadata.file_type().is_symlink() {
std::fs::remove_dir_all(entry.path())?;
} else {
std::fs::remove_file(entry.path())?;
}
}
Ok(())
}
fn remove_empty_source_dir(checkout: &Path) {
if let Some(parent) = checkout.parent() {
let _ = std::fs::remove_dir(parent);
}
}
#[must_use]
pub fn is_valid_source_id(value: &str) -> bool {
if value.is_empty() || value.len() > MAX_SOURCE_ID_LENGTH || value == "." || value == ".." {
return false;
}
value.chars().all(|character| {
character.is_ascii_alphanumeric()
|| character == '-'
|| character == '_'
|| character == '.'
})
}
fn finish_team_source(
paths: &StorePaths,
config: &mut UserConfig,
source: SourceConfig,
write_config: impl FnOnce(&StorePaths, &UserConfig) -> DaloResult<()>,
) -> DaloResult<()> {
config.sources.push(source);
sort_sources(&mut config.sources);
write_config(paths, config)?;
Ok(())
}
#[must_use = "the source list report should be rendered or inspected"]
pub fn list_sources(paths: &StorePaths) -> DaloResult<SourceListReport> {
let mut sources = store::read_config(paths)?.sources;
sort_sources(&mut sources);
let source_lock = catalog::read_source_lock(paths).ok();
let sources = sources
.into_iter()
.map(|source| SourceListEntry {
provenance: source_provenance(&source, source_lock.as_ref()),
source,
})
.collect();
Ok(SourceListReport { sources })
}
#[must_use]
pub fn source_provenance(
source: &SourceConfig,
source_lock: Option<&SourceLock>,
) -> SourceProvenance {
source_provenance_with_head_cache(source, source_lock, &mut SourceHeadCache::default())
}
pub(crate) fn source_provenance_with_head_cache(
source: &SourceConfig,
source_lock: Option<&SourceLock>,
head_cache: &mut SourceHeadCache,
) -> SourceProvenance {
let checkout_commit = (source.kind != SourceKind::Local)
.then(|| head_cache.resolve(source))
.flatten();
let resolved_commit = match source.kind {
SourceKind::Catalog => source_lock
.and_then(|lock| lock.catalog(&source.id))
.map(|lock| lock.commit.clone()),
SourceKind::Team => checkout_commit.clone(),
SourceKind::Local => None,
};
SourceProvenance {
management: if source.declared_by.is_some() {
SourceManagement::TeamManifest
} else {
SourceManagement::Direct
},
declared_by: source.declared_by.clone(),
origin_url: source.url.as_deref().map(git::display_remote_url),
requested_ref: source
.declared_ref
.clone()
.or_else(|| source.branch.clone()),
resolved_commit,
checkout_commit,
}
}
pub fn set_source_priority(
paths: &StorePaths,
id: &str,
priority: i32,
dry_run: bool,
) -> DaloResult<SourcePriorityReport> {
let mut config = store::read_config(paths)?;
let Some(source) = config.sources.iter_mut().find(|source| source.id == id) else {
return Err(DaloError::unknown_source(
id,
config
.sources
.iter()
.map(|candidate| candidate.id.clone())
.collect(),
));
};
if let Some(team) = &source.declared_by {
return Err(DaloError::StateError {
reason: format!(
"source `{id}` is managed by `{team}`; edit `{}` in that team repository",
crate::team_manifest::TEAM_MANIFEST_FILE
),
});
}
if source.kind == SourceKind::Local {
return Err(DaloError::LocalSourcePriorityFixed {
source_id: id.to_owned(),
});
}
let changed = source.priority != priority;
source.priority = priority;
let source = source.clone();
if changed && !dry_run {
sort_sources(&mut config.sources);
store::write_config(paths, &config)?;
}
Ok(SourcePriorityReport {
source,
changed,
dry_run,
})
}
pub fn set_source_namespace(
paths: &StorePaths,
id: &str,
namespace: Option<&str>,
dry_run: bool,
) -> DaloResult<SourceNamespaceReport> {
let namespace = validate_source_namespace(namespace)?;
let mut config = store::read_config(paths)?;
let Some(source) = config.sources.iter_mut().find(|source| source.id == id) else {
return Err(DaloError::unknown_source(
id,
config
.sources
.iter()
.map(|candidate| candidate.id.clone())
.collect(),
));
};
if let Some(team) = &source.declared_by {
return Err(DaloError::StateError {
reason: format!(
"source `{id}` is managed by `{team}`; edit `{}` in that team repository",
crate::team_manifest::TEAM_MANIFEST_FILE
),
});
}
if source.kind == SourceKind::Local && namespace.is_some() {
return Err(DaloError::StateError {
reason: format!(
"source `{id}` is the local source; its namespace is fixed and cannot be changed"
),
});
}
let changed = source.namespace != namespace;
source.namespace = namespace;
let source = source.clone();
if changed && !dry_run {
store::write_config(paths, &config)?;
}
Ok(SourceNamespaceReport {
source,
changed,
dry_run,
})
}
pub fn refresh_tracking_team_sources(
paths: &StorePaths,
) -> DaloResult<Vec<TrackingSourceRefreshFailure>> {
let config = store::read_config(paths)?;
refresh_tracking_team_sources_from_config(paths, &config)
}
pub fn refresh_tracking_team_sources_from_config(
paths: &StorePaths,
config: &UserConfig,
) -> DaloResult<Vec<TrackingSourceRefreshFailure>> {
let (fetch_paths, candidates) = prepare_tracking_refreshes(config, git::is_dirty)?;
let fetch_errors = git::fetch_upstreams_bounded(&fetch_paths)
.into_iter()
.map(|result| result.err().map(|error| error.to_string()))
.collect::<Vec<_>>();
let mut failures = Vec::new();
for candidate in candidates {
let (source, fetch_index) = match candidate {
PreparedTrackingSource::InspectionFailure { source, reason } => {
failures.push(TrackingSourceRefreshFailure {
id: source.id.clone(),
path: source.path.clone(),
reason: format!("could not inspect tracking checkout: {reason}"),
});
continue;
}
PreparedTrackingSource::Fetch {
source,
fetch_index,
} => (source, fetch_index),
};
if let Some(error) = &fetch_errors[fetch_index] {
failures.push(TrackingSourceRefreshFailure {
id: source.id.clone(),
path: source.path.clone(),
reason: format!("could not refresh tracking source: {error}"),
});
continue;
}
if let Err(error) = audit_and_fast_forward_fetched_source(paths, source) {
if matches!(error, DaloError::AuditBlocked { .. }) {
return Err(error);
}
failures.push(TrackingSourceRefreshFailure {
id: source.id.clone(),
path: source.path.clone(),
reason: format!("could not refresh tracking source: {error}"),
});
}
}
Ok(failures)
}
enum PreparedTrackingSource<'a> {
InspectionFailure {
source: &'a SourceConfig,
reason: String,
},
Fetch {
source: &'a SourceConfig,
fetch_index: usize,
},
}
fn prepare_tracking_refreshes<'a, D>(
config: &'a UserConfig,
mut inspect_dirty: D,
) -> DaloResult<(Vec<PathBuf>, Vec<PreparedTrackingSource<'a>>)>
where
D: FnMut(&Path) -> DaloResult<bool>,
{
let tracking_sources = config
.sources
.iter()
.filter(|source| {
source.enabled
&& source.kind == SourceKind::Team
&& source.update_policy.as_deref() == Some("track")
})
.collect::<Vec<_>>();
let mut inspections = BTreeMap::new();
let mut fetch_paths = Vec::new();
let mut fetch_index_by_path = BTreeMap::new();
let mut prepared = Vec::new();
for source in tracking_sources {
let comparable_path = store::comparable_path(&source.path);
let inspection = inspections
.entry(comparable_path.clone())
.or_insert_with(|| inspect_dirty(&source.path).map_err(|error| error.to_string()));
match inspection {
Ok(true) => {
return Err(DaloError::DirtySource {
source_id: source.id.clone(),
path: source.path.clone(),
});
}
Ok(false) => {}
Err(reason) => {
prepared.push(PreparedTrackingSource::InspectionFailure {
source,
reason: reason.clone(),
});
continue;
}
}
let fetch_index = *fetch_index_by_path
.entry(comparable_path)
.or_insert_with(|| {
let index = fetch_paths.len();
fetch_paths.push(source.path.clone());
index
});
prepared.push(PreparedTrackingSource::Fetch {
source,
fetch_index,
});
}
Ok((fetch_paths, prepared))
}
fn audit_and_fast_forward_fetched_source(
paths: &StorePaths,
source: &SourceConfig,
) -> DaloResult<()> {
let upstream = git::rev_parse(&source.path, "@{upstream}")?;
let incoming = git::revision_count(&source.path, "HEAD", &upstream)?;
if incoming == 0 {
return Ok(());
}
if git::revision_count(&source.path, &upstream, "HEAD")? != 0 {
return Err(DaloError::TrackingSourceNotFastForward {
source_id: source.id.clone(),
path: source.path.clone(),
});
}
let staging_root = paths.sources_dir.join(".audit-staging");
fs::create_dir_all(&staging_root)?;
let staging_path = staging_root.join(format!("{}-{upstream}", source.id));
cleanup_obsolete_staging_worktrees(source, &staging_root, &staging_path)?;
let staging_matches = staging_path.exists()
&& git::rev_parse_head(&staging_path).is_ok_and(|commit| commit == upstream);
if !staging_matches {
if staging_path.exists() {
let _ = git::remove_worktree(&source.path, &staging_path);
let _ = fs::remove_dir_all(&staging_path);
git::prune_worktrees(&source.path)?;
}
git::add_detached_worktree(&source.path, &staging_path, &upstream)?;
}
let audit_result = (|| -> DaloResult<()> {
let source_root = scoped_source_root(&staging_path, source.subpath.as_deref())?;
let inventory = crate::inventory::scan_source(&source.id, &source_root)?;
let mut blocked = Vec::new();
for skill in inventory.skills {
let report = audit::audit_skill(
paths,
&skill.source_ref,
&skill.path,
&audit::AuditOptions {
exclude_root_source_metadata: store::comparable_path(&skill.path)
== store::comparable_path(&source_root),
..audit::AuditOptions::default()
},
)?;
if report.is_blocking() {
blocked.push((skill.source_ref, skill.path, audit_finding_summary(&report)));
}
}
if blocked.is_empty() {
Ok(())
} else {
Err(DaloError::AuditBlocked {
reason: format!(
"staged security audit blocked upstream commit {upstream} ({})",
blocked
.iter()
.map(|(source_ref, path, finding_summary)| format!(
"{source_ref} ({finding_summary}); inspect with `dalo audit {}`; then, only if the risk is accepted, add `--accept-risk <reason>`",
crate::error::shell_quote_path(path)
))
.collect::<Vec<_>>()
.join(", ")
),
})
}
})();
if let Err(error) = audit_result {
if matches!(error, DaloError::AuditBlocked { .. }) {
return Err(error);
}
if let Err(cleanup) = git::remove_worktree(&source.path, &staging_path) {
return Err(DaloError::Io(std::io::Error::other(format!(
"{error}; additionally failed to remove staged audit worktree: {cleanup}"
))));
}
return Err(error);
}
git::remove_worktree(&source.path, &staging_path)?;
let _ = fs::remove_dir(&staging_root);
git::fast_forward_to(&source.path, &upstream)
}
fn audit_finding_summary(report: &AuditReport) -> String {
let finding_count = report.static_findings.len()
+ report
.agent_review
.as_ref()
.map_or(0, |review| review.findings.len());
let finding_word = if finding_count == 1 {
"finding"
} else {
"findings"
};
let max_severity = report
.max_severity
.expect("a blocking audit report has a maximum severity");
format!(
"{finding_count} {finding_word}, max {}",
max_severity.as_str()
)
}
fn cleanup_obsolete_staging_worktrees(
source: &SourceConfig,
staging_root: &Path,
keep: &Path,
) -> DaloResult<()> {
for entry in fs::read_dir(staging_root)? {
let entry = entry?;
let path = entry.path();
if path == keep || !staging_entry_belongs_to_source(&entry.file_name(), &source.id) {
continue;
}
if git::remove_worktree(&source.path, &path).is_err() {
fs::remove_dir_all(&path)?;
}
}
git::prune_worktrees(&source.path)
}
pub(crate) fn staging_entry_belongs_to_source(name: &std::ffi::OsStr, source_id: &str) -> bool {
let Some(name) = name.to_str() else {
return false;
};
let Some(commit) = name
.strip_prefix(source_id)
.and_then(|suffix| suffix.strip_prefix('-'))
else {
return false;
};
matches!(commit.len(), 40 | 64) && commit.bytes().all(|byte| byte.is_ascii_hexdigit())
}
#[cfg(test)]
fn refresh_tracking_team_sources_with<D, P>(
config: &UserConfig,
mut is_dirty: D,
mut pull_ff_only: P,
) -> DaloResult<Vec<TrackingSourceRefreshFailure>>
where
D: FnMut(&Path) -> DaloResult<bool>,
P: FnMut(&Path) -> DaloResult<()>,
{
let mut failures = Vec::new();
for source in config.sources.iter().filter(|source| {
source.enabled
&& source.kind == SourceKind::Team
&& source.update_policy.as_deref() == Some("track")
}) {
match is_dirty(&source.path) {
Ok(true) => {
return Err(DaloError::DirtySource {
source_id: source.id.clone(),
path: source.path.clone(),
});
}
Ok(false) => {}
Err(error) => {
failures.push(TrackingSourceRefreshFailure {
id: source.id.clone(),
path: source.path.clone(),
reason: format!("could not inspect tracking checkout: {error}"),
});
continue;
}
}
if let Err(error) = pull_ff_only(&source.path) {
failures.push(TrackingSourceRefreshFailure {
id: source.id.clone(),
path: source.path.clone(),
reason: format!("could not refresh tracking source: {error}"),
});
}
}
Ok(failures)
}
#[cfg(test)]
mod tests {
use std::path::Path;
use std::process::Command;
use super::*;
use proptest::prelude::*;
#[test]
fn scoped_source_root_should_reject_symlinks_outside_checkout() {
let temp = tempfile::tempdir().unwrap();
let checkout = temp.path().join("checkout");
let outside = temp.path().join("outside");
std::fs::create_dir_all(&checkout).unwrap();
std::fs::create_dir_all(&outside).unwrap();
std::os::unix::fs::symlink(&outside, checkout.join("skills")).unwrap();
assert!(scoped_source_root(&checkout, Some(Path::new("skills"))).is_err());
}
#[test]
fn is_valid_source_id_should_reject_traversal_and_slash_ids() {
assert!(!is_valid_source_id(".."));
assert!(!is_valid_source_id("a/b"));
assert!(!is_valid_source_id(""));
assert!(!is_valid_source_id(&"a".repeat(MAX_SOURCE_ID_LENGTH + 1)));
}
#[test]
fn is_valid_source_id_should_accept_plain_id() {
assert!(is_valid_source_id("company"));
}
#[test]
fn staging_entry_match_should_disambiguate_dash_prefixed_source_ids() {
let commit = "0123456789abcdef0123456789abcdef01234567";
let sha256_commit = format!("{commit}0123456789abcdef01234567");
assert!(staging_entry_belongs_to_source(
std::ffi::OsStr::new(&format!("team-{commit}")),
"team"
));
assert!(!staging_entry_belongs_to_source(
std::ffi::OsStr::new(&format!("team-eu-{commit}")),
"team"
));
assert!(staging_entry_belongs_to_source(
std::ffi::OsStr::new(&format!("team-eu-{commit}")),
"team-eu"
));
assert!(staging_entry_belongs_to_source(
std::ffi::OsStr::new(&format!("team-{sha256_commit}")),
"team"
));
assert!(!staging_entry_belongs_to_source(
std::ffi::OsStr::new("team-not-a-commit"),
"team"
));
}
proptest! {
#[test]
fn valid_source_ids_should_stay_single_path_components(value in "\\PC{0,64}") {
if is_valid_source_id(&value) {
prop_assert!(!value.is_empty());
prop_assert_ne!(value.as_str(), ".");
prop_assert_ne!(value.as_str(), "..");
prop_assert!(!value.contains('/'));
let portable = value.chars().all(|character| {
character.is_ascii_alphanumeric()
|| character == '-'
|| character == '_'
|| character == '.'
});
prop_assert!(portable);
}
}
}
#[test]
fn add_team_source_should_reject_traversal_id_without_cloning() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp_dir.path().join("store");
store::init_store(store_root.clone(), false).expect("init should succeed");
let paths = StorePaths::new(store_root);
let error = add_team_source(
&paths,
"../../evil",
"https://example.invalid/repo.git",
None,
false,
)
.expect_err("traversal id should be rejected");
assert!(matches!(error, DaloError::InvalidSourceId { .. }));
assert!(
!paths
.sources_dir
.join("../../evil")
.join("checkout")
.exists()
);
assert!(
std::fs::read_dir(&paths.sources_dir)
.expect("sources dir should exist")
.next()
.is_none()
);
}
#[test]
fn add_team_source_should_reject_url_userinfo_before_cloning_or_persisting() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp_dir.path().join("store");
store::init_store(store_root.clone(), false).expect("init should succeed");
let paths = StorePaths::new(store_root);
let error = add_team_source(
&paths,
"company",
"https://octo:token-value@example.invalid/repo.git",
None,
false,
)
.expect_err("credential-bearing URL should be rejected");
assert!(matches!(error, DaloError::UnsafeRemoteUrl));
assert!(!paths.sources_dir.join("company").exists());
let config = store::read_config(&paths).expect("config should remain readable");
assert!(!config.sources.iter().any(|source| source.id == "company"));
}
#[test]
fn add_team_source_should_remove_checkout_when_persisting_fails() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp_dir.path().join("store");
let repo = temp_dir.path().join("team-repo");
create_git_repo(&repo);
store::init_store(store_root.clone(), false).expect("init should succeed");
let paths = StorePaths::new(store_root);
let checkout = paths.sources_dir.join("company").join("checkout");
let error = add_team_source_with_config_writer(
&paths,
"company",
&repo.to_string_lossy(),
SourceAddOptions::default(),
false,
|_, _| Err(DaloError::Io(std::io::Error::other("persist failed"))),
)
.expect_err("add should fail when config cannot be recorded");
assert!(matches!(error, DaloError::Io(_)));
assert!(!checkout.exists());
let config = store::read_config(&paths).expect("config should remain readable");
assert!(!config.sources.iter().any(|source| source.id == "company"));
}
#[test]
fn add_team_source_should_remove_checkout_when_clone_fails() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp_dir.path().join("store");
store::init_store(store_root.clone(), false).expect("init should succeed");
let paths = StorePaths::new(store_root);
let checkout = paths.sources_dir.join("company").join("checkout");
let error = add_team_source_with_config_writer_and_cloner(
&paths,
"company",
"https://example.invalid/repo.git",
SourceAddOptions::default(),
false,
store::write_config,
|_, checkout| {
std::fs::create_dir_all(checkout.join(".git"))?;
std::fs::write(checkout.join("PARTIAL"), "left by clone")?;
Err(DaloError::CommandFailed {
program: "git".to_owned(),
args: "clone".to_owned(),
cwd: checkout
.parent()
.unwrap_or_else(|| std::path::Path::new("."))
.to_path_buf(),
status: "timed out after 1s".to_owned(),
stderr: "git command timed out".to_owned(),
})
},
)
.expect_err("clone failure should fail source add");
assert!(matches!(error, DaloError::CommandFailed { .. }));
assert!(!checkout.exists());
let config = store::read_config(&paths).expect("config should remain readable");
assert!(!config.sources.iter().any(|source| source.id == "company"));
}
#[test]
fn add_team_source_should_replace_an_orphaned_legacy_checkout() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp_dir.path().join("store");
let repo = temp_dir.path().join("team-repo");
create_git_repo(&repo);
store::init_store(store_root.clone(), false).expect("init should succeed");
let paths = StorePaths::new(store_root);
let checkout = paths.sources_dir.join("company").join("checkout");
std::fs::create_dir_all(&checkout).expect("legacy checkout should be created");
std::fs::write(checkout.join("PARTIAL"), "interrupted clone")
.expect("legacy marker should be written");
let report = add_team_source(&paths, "company", &repo.to_string_lossy(), None, false)
.expect("orphaned checkout should not block a retry");
assert!(report.source.path.join(".git").is_dir());
assert!(!report.source.path.join("PARTIAL").exists());
}
#[test]
fn add_team_source_should_not_remove_an_unconfigured_git_checkout() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp_dir.path().join("store");
store::init_store(store_root.clone(), false).expect("init should succeed");
let paths = StorePaths::new(store_root);
let checkout = paths.sources_dir.join("company").join("checkout");
std::fs::create_dir_all(checkout.join(".git")).expect("git checkout should be created");
std::fs::write(checkout.join("LOCAL"), "keep this work")
.expect("local work marker should be written");
let error = add_team_source_with_config_writer_and_cloner(
&paths,
"company",
"https://example.invalid/company.git",
SourceAddOptions::default(),
false,
store::write_config,
|_, _| panic!("a preserved checkout must not be replaced"),
)
.expect_err("unconfigured Git checkout should require explicit recovery");
assert!(matches!(error, DaloError::SourceCheckoutExists { .. }));
assert!(checkout.join("LOCAL").is_file());
assert!(error.to_string().contains("restore its source config"));
}
#[test]
fn resolve_source_location_should_absolutize_local_paths_and_preserve_remotes() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let cwd = temp_dir.path().join("project");
let colon_path = cwd.join("team:skills");
std::fs::create_dir_all(&colon_path).expect("local colon path should be created");
assert_eq!(resolve_source_location(".", &cwd), cwd.to_string_lossy());
assert_eq!(
resolve_source_location("../skills", &cwd),
temp_dir.path().join("skills").to_string_lossy()
);
assert_eq!(
resolve_source_location("team:skills", &cwd),
colon_path.to_string_lossy()
);
assert_eq!(
resolve_source_location("https://example.invalid/repo.git", &cwd),
"https://example.invalid/repo.git"
);
assert_eq!(
resolve_source_location("git@example.invalid:org/repo.git", &cwd),
"git@example.invalid:org/repo.git"
);
}
#[test]
fn clone_source_checkout_should_sweep_interrupted_clone_dirs() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let repo = temp_dir.path().join("team-repo");
create_git_repo(&repo);
let source_dir = temp_dir.path().join("store/sources/company");
let checkout = source_dir.join("checkout");
for name in [".checkout-tmp-111", ".checkout-tmp-222"] {
std::fs::create_dir_all(source_dir.join(name))
.expect("interrupted clone dir should be created");
}
clone_source_checkout(&repo.to_string_lossy(), &checkout)
.expect("source clone should succeed");
assert!(checkout.join(".git").is_dir());
assert!(!source_dir.join(".checkout-tmp-111").exists());
assert!(!source_dir.join(".checkout-tmp-222").exists());
}
#[test]
fn tracking_refresh_plan_should_deduplicate_checkouts_and_preserve_source_order() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let shared = temp_dir.path().join("shared");
let broken = temp_dir.path().join("broken");
fs::create_dir(&shared).expect("shared checkout should exist");
fs::create_dir(&broken).expect("broken checkout should exist");
let source = |id: &str, path: PathBuf| SourceConfig {
id: id.to_owned(),
kind: SourceKind::Team,
path,
subpath: None,
priority: 10,
namespace: None,
enabled: true,
trusted: true,
url: Some(format!("https://example.invalid/{id}.git")),
branch: None,
update_policy: Some("track".to_owned()),
selection: Vec::new(),
declared_by: None,
declared_ref: None,
};
let config = UserConfig {
version: 1,
settings: crate::config::Settings {
autosync: false,
sync_interval: None,
},
sources: vec![
source("first", shared.clone()),
source("broken", broken.clone()),
source("second", shared.clone()),
],
plugins: crate::config::PluginConfig::default(),
plugin_policy: Vec::new(),
project_selections: None,
};
let mut inspections = BTreeMap::new();
let (fetch_paths, prepared) = prepare_tracking_refreshes(&config, |path| {
*inspections.entry(path.to_path_buf()).or_insert(0_usize) += 1;
if path == broken {
Err(DaloError::Io(std::io::Error::other("inspection failed")))
} else {
Ok(false)
}
})
.expect("inspection failures should degrade individual sources");
assert_eq!(fetch_paths, vec![shared.clone()]);
assert_eq!(inspections.get(&shared), Some(&1));
assert_eq!(inspections.get(&broken), Some(&1));
assert!(matches!(
&prepared[0],
PreparedTrackingSource::Fetch {
source,
fetch_index: 0
} if source.id == "first"
));
assert!(matches!(
&prepared[1],
PreparedTrackingSource::InspectionFailure { source, reason }
if source.id == "broken" && reason.contains("inspection failed")
));
assert!(matches!(
&prepared[2],
PreparedTrackingSource::Fetch {
source,
fetch_index: 0
} if source.id == "second"
));
}
#[test]
fn refresh_tracking_sources_should_degrade_pull_failures_but_keep_going() {
let source_path = PathBuf::from("/store/sources/company/checkout");
let config = UserConfig {
version: 1,
settings: crate::config::Settings {
autosync: false,
sync_interval: None,
},
sources: vec![SourceConfig {
id: "company".to_owned(),
kind: SourceKind::Team,
path: source_path.clone(),
subpath: None,
priority: 10,
namespace: None,
enabled: true,
trusted: true,
url: Some("https://example.invalid/company.git".to_owned()),
branch: None,
update_policy: Some("track".to_owned()),
selection: Vec::new(),
declared_by: None,
declared_ref: None,
}],
plugins: crate::config::PluginConfig::default(),
plugin_policy: Vec::new(),
project_selections: None,
};
let failures = refresh_tracking_team_sources_with(
&config,
|_| Ok(false),
|_| Err(DaloError::Io(std::io::Error::other("network unavailable"))),
)
.expect("an unavailable remote should be represented as a degraded source");
assert_eq!(failures.len(), 1);
assert_eq!(failures[0].id, "company");
assert_eq!(failures[0].path, source_path);
assert!(failures[0].reason.contains("network unavailable"));
}
#[test]
fn refresh_tracking_sources_should_still_stop_for_a_dirty_checkout() {
let config = UserConfig {
version: 1,
settings: crate::config::Settings {
autosync: false,
sync_interval: None,
},
sources: vec![SourceConfig {
id: "company".to_owned(),
kind: SourceKind::Team,
path: PathBuf::from("/store/sources/company/checkout"),
subpath: None,
priority: 10,
namespace: None,
enabled: true,
trusted: true,
url: Some("https://example.invalid/company.git".to_owned()),
branch: None,
update_policy: Some("track".to_owned()),
selection: Vec::new(),
declared_by: None,
declared_ref: None,
}],
plugins: crate::config::PluginConfig::default(),
plugin_policy: Vec::new(),
project_selections: None,
};
let error = refresh_tracking_team_sources_with(&config, |_| Ok(true), |_| Ok(()))
.expect_err("dirty checkouts must still block sync");
assert!(matches!(error, DaloError::DirtySource { .. }));
}
#[test]
fn source_provenance_should_distinguish_manifest_management_and_redact_origin() {
let source = SourceConfig {
id: "company.marketing".to_owned(),
kind: SourceKind::Catalog,
path: PathBuf::from("/missing/checkout"),
subpath: None,
priority: 11,
namespace: None,
enabled: true,
trusted: false,
url: Some("https://user:secret@example.com/marketing.git".to_owned()),
branch: None,
update_policy: Some("manifest".to_owned()),
selection: vec!["copy".to_owned()],
declared_by: Some("company".to_owned()),
declared_ref: Some("main".to_owned()),
};
let lock = SourceLock {
schema_version: catalog::SOURCE_LOCK_SCHEMA_VERSION,
catalogs: vec![catalog::CatalogLock {
source_id: source.id.clone(),
commit: "0123456789abcdef".to_owned(),
selected: Vec::new(),
inventory: Vec::new(),
}],
};
let provenance = source_provenance(&source, Some(&lock));
assert_eq!(provenance.management, SourceManagement::TeamManifest);
assert_eq!(provenance.declared_by.as_deref(), Some("company"));
assert_eq!(
provenance.origin_url.as_deref(),
Some("https://***@example.com/marketing.git")
);
let origin = provenance
.origin_url
.as_deref()
.expect("origin should exist");
assert!(!origin.contains("user"));
assert!(!origin.contains("secret"));
assert_eq!(provenance.requested_ref.as_deref(), Some("main"));
assert_eq!(
provenance.resolved_commit.as_deref(),
Some("0123456789abcdef")
);
assert!(provenance.checkout_commit.is_none());
}
#[test]
fn add_team_source_should_persist_source_without_leftovers_on_success() {
let temp_dir = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp_dir.path().join("store");
let repo = temp_dir.path().join("team-repo");
create_git_repo(&repo);
store::init_store(store_root.clone(), false).expect("init should succeed");
let paths = StorePaths::new(store_root);
let report = add_team_source(&paths, "company", &repo.to_string_lossy(), None, false)
.expect("add should succeed against a local repo");
assert!(report.source.path.join(".git").is_dir());
let second = add_team_source(&paths, "company", &repo.to_string_lossy(), None, false)
.expect_err("a second add of the same id should be a clean duplicate error");
assert!(matches!(second, DaloError::SourceAlreadyExists { .. }));
}
fn create_git_repo(repo: &Path) {
std::fs::create_dir_all(repo).expect("repo dir should be created");
run_git(repo, &["init", "-q"]);
run_git(repo, &["config", "user.email", "test@example.com"]);
run_git(repo, &["config", "user.name", "Test"]);
std::fs::write(repo.join("README.md"), "# Repo\n").expect("file should be written");
run_git(repo, &["add", "."]);
run_git(
repo,
&[
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"-m",
"initial",
],
);
}
fn run_git(repo: &Path, args: &[&str]) {
let status = Command::new("git")
.current_dir(repo)
.args(args)
.status()
.expect("git should run");
assert!(status.success(), "git {args:?} should succeed");
}
#[test]
fn source_namespace_should_use_portable_skill_name_rules() {
assert_eq!(
validate_source_namespace(Some("company-tools")).expect("valid namespace"),
Some("company-tools".to_owned())
);
assert_eq!(
validate_source_namespace(None).expect("missing namespace is allowed"),
None
);
assert!(validate_source_namespace(Some("Company Tools")).is_err());
assert!(validate_source_namespace(Some(".company")).is_err());
}
}