use crate::checkout;
use crate::config::EffectiveConfig;
use crate::edge::Edge;
use crate::hooks::Hooks;
use crate::install;
use crate::lifecycle::Plugins;
use crate::log::{self, Level, Log};
use crate::op::Phase;
use crate::plugin::Subprocess;
use crate::registry::Registry;
use crate::verb::Verb;
use crate::wire::OpContext;
use crate::LANDING_BRANCH;
use std::io;
use std::path::Path;
pub fn adopt(edge: &Edge, landing: &Path, store: &Path, actor: &str, center: &str) -> io::Result<()> {
fetch_config(edge, landing, store, actor, center)?;
install_local(edge, landing)
}
fn fetch_config(edge: &Edge, landing: &Path, store: &Path, actor: &str, center: &str) -> io::Result<()> {
let cfg = EffectiveConfig::resolve(landing, &edge.xdg.user_config())?;
let level = Level::parse(edge.log_level.as_deref().unwrap_or(&cfg.log_level))?;
let binding = checkout::binding(landing, store, &edge.invocation_path, Some(center.to_string()), false, LANDING_BRANCH.to_string());
let pre = Hooks::effective(landing, &edge.xdg.user_config())?
.resolve(&Registry::at(landing), Verb::Install.token(), "pre");
if pre.is_empty() {
return Err(io::Error::other(
"prime --install: no install.pre plugin (e.g. the tracker) is installed to fetch the center's config",
));
}
let log = Log::new(edge.xdg.clone_dir(&edge.invocation_path).op_log(), level, Verb::Install, log::wall);
let plugins = Subprocess::new(OpContext::diffless(actor.to_string(), binding), &log, edge.depth);
for plugin in &pre {
plugins.run(plugin, Verb::Install, Phase::Pre, landing, None)?;
}
Ok(())
}
pub(crate) fn install_local(edge: &Edge, landing: &Path) -> io::Result<()> {
let clone = edge.xdg.clone_dir(&edge.invocation_path);
let (binding, level) = checkout::bind(edge, landing, &clone.store(), None, None)?;
let log = Log::new(clone.op_log(), level, Verb::Install, log::wall);
let plugins = Subprocess::new(OpContext::diffless(edge.default_actor.clone(), binding), &log, edge.depth);
let chain = install::Chain { plugins: &plugins, log: &log, pre: Vec::new(), post: Vec::new() };
let summary = install::seal_copy(&clone, install::DEFAULT_PATH, "FETCH_HEAD", landing, &chain, &edge.default_actor)?;
install::bind_referenced(landing, edge, &std::collections::BTreeMap::new())?;
println!("install: {summary}");
Ok(())
}
#[cfg(test)]
#[path = "adopt_tests.rs"]
mod tests;