use crate::template::GitRunner;
use crate::workspace;
use std::path::Path;
const REASON: &str = "a root agent forks off the ref you name (ARCH §2.3, §7.2)";
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(
"pass --from <ref> or --config <name>, not both — a start forks off exactly one \
ref (ARCH §2.3); --config <name> is the head of config/<name>, --from <ref> is \
any ref at all"
)]
Conflict,
#[error(transparent)]
UnknownRef(#[from] workspace::UnknownRef),
#[error(transparent)]
UnknownLineage(#[from] workspace::UnknownLineage),
}
pub fn resolve(
repo: &Path,
from: Option<&str>,
config: Option<&str>,
git: &dyn GitRunner,
) -> Result<String, Error> {
match (from, config) {
(Some(_), Some(_)) => Err(Error::Conflict),
(Some(rev), None) => {
workspace::require_ref(repo, rev, REASON, git)?;
Ok(rev.to_owned())
}
(None, name) => {
let name = name.unwrap_or(workspace::DEFAULT_CONFIG_NAME);
workspace::require_lineage(repo, name, git)?;
Ok(workspace::config_ref(name))
}
}
}
#[cfg(test)]
mod tests;