use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use crate::config::{
Config, DeployMode, LayoutConfig, ParsedSource, SourceMode, Target, TemplateOptIn,
};
use crate::error::{Error, Result};
use crate::lock::{Lock, encode_ref, ref_discriminator};
use crate::projection::model::Materialization;
use crate::source::{
Commit, ResolvePolicy, ResolveRequest, ResolvedSource, RevisionSpec, SourceLocation,
SourcePath, SourceStore,
};
use crate::sync::state::{
ArtifactKey, ArtifactRecord, ManifestFile, NewArtifactRecord, NewHistoryRecord, RecordKind,
StateStore,
};
use super::plan::plan_target;
use super::resolve::ResolvedSourceMap;
use super::scan::link_target_bytes;
use super::stage::{StageRequest, stage_artifact};
use super::target::{history_deployment, project_root};
use super::{StagingGuard, nonce, remote_for, resolved_remotes};
use crate::projection::model::{BindingProjection, ProjectedArtifact, TargetProjection};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RebuildReport {
pub reconstructed: Vec<ArtifactKey>,
pub modified: Vec<ArtifactKey>,
pub foreign: Vec<std::path::PathBuf>,
pub forgotten: Vec<ArtifactKey>,
}
pub fn rebuild_registry(
config: &Config,
lock: &Lock,
backend: &dyn SourceStore,
registry: &dyn StateStore,
) -> Result<RebuildReport> {
let parsed = config.parsed_sources()?;
let remotes = resolved_remotes(config, &parsed)?;
rebuild_registry_with(config, &parsed, &remotes, lock, backend, registry)
}
pub fn rebuild_registry_with(
config: &Config,
parsed: &BTreeMap<String, ParsedSource>,
remotes: &BTreeMap<String, String>,
lock: &Lock,
backend: &dyn SourceStore,
registry: &dyn StateStore,
) -> Result<RebuildReport> {
let configured = config;
let locked_config = only_locked_bindings(config, parsed, lock);
let config = &locked_config;
let (resolved_commits, resolved_sources) =
locked_sources(config, lock, parsed, remotes, backend)?;
let mut report = RebuildReport::default();
for (target_name, target) in &config.targets {
let plan = plan_target(
target_name,
target,
parsed,
remotes,
backend,
&resolved_commits,
&resolved_sources,
)?;
let mut managed_dests: BTreeSet<PathBuf> = BTreeSet::new();
for binding in &plan.bindings {
managed_dests.extend(
binding
.artifacts
.iter()
.map(|item| target.expanded_path().join(item.destination.as_str())),
);
let source = parsed.get(&binding.source).ok_or_else(|| {
Error::Config(format!(
"target references undefined source: {}",
binding.source
))
})?;
rebuild_binding(
&BindingRun {
config,
parsed,
target_name,
target,
backend,
resolved_sources: &resolved_sources,
registry,
projection: &plan,
binding,
source,
},
&mut report,
)?;
}
report.foreign.extend(scan_foreign(target, &managed_dests)?);
}
forget_vanished_orphans(configured, backend, registry, &mut report)?;
Ok(report)
}
fn forget_vanished_orphans(
config: &Config,
backend: &dyn SourceStore,
registry: &dyn StateStore,
report: &mut RebuildReport,
) -> Result<()> {
for record in registry.all_artifacts()? {
if !super::prune::is_orphan(config, &record) {
continue;
}
let Some(path) = super::prune::recorded_destination(config, &record) else {
continue;
};
if std::fs::symlink_metadata(&path).is_ok() {
continue;
}
super::detach_history_overlay(&record, &path, backend)?;
registry.remove_artifact(&record.key)?;
report.forgotten.push(record.key);
}
Ok(())
}
fn locked_entry<'l>(
lock: &'l Lock,
source: &crate::config::ParsedSource,
binding: &crate::config::ResolvedBinding<'_>,
) -> Option<&'l crate::lock::LockedSource> {
let discriminator = ref_discriminator(&binding.effective_ref, &source.refspec());
lock.find_entry(binding.source, discriminator.as_deref())
}
fn only_locked_bindings(
config: &Config,
parsed: &BTreeMap<String, crate::config::ParsedSource>,
lock: &Lock,
) -> Config {
let mut config = config.clone();
for target in config.targets.values_mut() {
let unlocked: BTreeSet<String> = target
.resolve_sources(parsed)
.into_iter()
.filter(|binding| {
parsed
.get(binding.source)
.is_some_and(|source| locked_entry(lock, source, binding).is_none())
})
.map(|binding| binding.identity.to_owned())
.collect();
if let Some(bindings) = target.sources.as_mut() {
bindings.retain(|identity, _| !unlocked.contains(identity));
}
}
config
}
type LockedSourceMaps = (BTreeMap<(String, String), String>, ResolvedSourceMap);
fn locked_sources(
config: &Config,
lock: &Lock,
parsed: &BTreeMap<String, crate::config::ParsedSource>,
remotes: &BTreeMap<String, String>,
store: &dyn SourceStore,
) -> Result<LockedSourceMaps> {
let mut commits = BTreeMap::new();
let mut resolved_sources = ResolvedSourceMap::new();
for target in config.targets.values() {
for binding in target.resolve_sources(parsed) {
let source = parsed.get(binding.source).ok_or_else(|| {
Error::Config(format!(
"target references undefined source: {}",
binding.source
))
})?;
let Some(locked) = locked_entry(lock, source, &binding) else {
continue;
};
commits.insert(
(
binding.source.to_owned(),
encode_ref(&binding.effective_ref),
),
locked.commit.clone(),
);
let resolved_key = (binding.source.to_owned(), locked.commit.clone());
if resolved_sources.contains_key(&resolved_key) {
continue;
}
let remote = remote_for(remotes, binding.source)?;
let (location, revision) = match source.deploy_mode() {
DeployMode::Link => (
SourceLocation::Worktree {
root: remote.into(),
follow_symlinks: source.export_policy(false).allow_symlinks,
},
RevisionSpec::Default,
),
DeployMode::Copy if source.mode() == SourceMode::Url => (
SourceLocation::Url {
url: remote.to_owned(),
},
RevisionSpec::Commit(locked.commit.parse::<Commit>()?),
),
DeployMode::Copy => (
SourceLocation::Git {
url: remote.to_owned(),
},
RevisionSpec::Commit(locked.commit.parse::<Commit>()?),
),
};
let resolved = store.resolve(
&ResolveRequest {
name: crate::source::SourceName::trusted(binding.source),
location,
revision,
},
ResolvePolicy::CachedOnly,
)?;
resolved_sources.insert(resolved_key, resolved);
}
}
Ok((commits, resolved_sources))
}
struct BindingRun<'a> {
config: &'a Config,
parsed: &'a BTreeMap<String, crate::config::ParsedSource>,
target_name: &'a str,
target: &'a Target,
backend: &'a dyn SourceStore,
resolved_sources: &'a ResolvedSourceMap,
registry: &'a dyn StateStore,
projection: &'a TargetProjection,
binding: &'a BindingProjection,
source: &'a crate::config::ParsedSource,
}
fn rebuild_binding(run: &BindingRun<'_>, report: &mut RebuildReport) -> Result<()> {
let resolved = run
.resolved_sources
.get(&(run.binding.source.clone(), run.binding.commit.clone()))
.ok_or_else(|| {
Error::Sync(format!(
"no resolved snapshot for {} at {}",
run.binding.source, run.binding.commit
))
})?;
let template_opt_in = run
.target
.resolve_sources(run.parsed)
.into_iter()
.find(|b| b.identity == run.binding.identity)
.map_or(TemplateOptIn::SuffixOnly, |b| b.template_opt_in);
let deploy_root = run.target.deploy_root();
for item in &run.binding.artifacts {
let policy = run.source.export_policy(matches!(
item.materialization,
Materialization::WholeRoot { .. }
));
let published_key = item.materialization.published_key().to_owned();
let key = ArtifactKey {
target: run.target_name.to_owned(),
source: run.binding.identity.clone(),
artifact: published_key.clone(),
};
let artifact_dst = run.target.expanded_path().join(item.destination.as_str());
if run.source.deploy_mode() == DeployMode::Link {
rebuild_linked(
run.registry,
&run.binding.source,
&policy,
&run.target.layout(),
record_kind(&item.materialization),
key.clone(),
deploy_root.clone(),
&artifact_dst,
)?;
} else {
rebuild_one(RebuildOne {
backend: run.backend,
registry: run.registry,
underlying_source: &run.binding.source,
root: run.source.offer().root(),
commit: &run.binding.commit,
resolved,
policy: &policy,
projection: run.projection,
item,
artifact_dst: &artifact_dst,
layout: run.target.layout(),
key: key.clone(),
report,
template_opt_in: &template_opt_in,
vars: &run.config.vars,
deploy_root: deploy_root.clone(),
})?;
}
report.reconstructed.push(key);
}
Ok(())
}
fn record_kind(materialization: &Materialization) -> RecordKind {
match materialization {
Materialization::CollapsedDir { .. } | Materialization::WholeRoot { .. } => RecordKind::Dir,
Materialization::Leaf(_) => RecordKind::File,
}
}
struct RebuildOne<'a> {
backend: &'a dyn SourceStore,
registry: &'a dyn StateStore,
underlying_source: &'a str,
root: Option<&'a Path>,
commit: &'a str,
resolved: &'a ResolvedSource,
policy: &'a crate::source::ExportPolicy,
projection: &'a TargetProjection,
item: &'a ProjectedArtifact,
artifact_dst: &'a Path,
layout: LayoutConfig,
key: ArtifactKey,
report: &'a mut RebuildReport,
template_opt_in: &'a TemplateOptIn,
vars: &'a BTreeMap<String, String>,
deploy_root: String,
}
fn rebuilt_history(
backend: &dyn SourceStore,
resolved: &ResolvedSource,
item: &ProjectedArtifact,
artifact_dst: &Path,
key: &ArtifactKey,
commit: &str,
) -> Result<Option<NewHistoryRecord>> {
if !matches!(&item.materialization, Materialization::WholeRoot { .. }) {
return Ok(None);
}
let guard = backend.lock_worktree_mirror(&resolved.name, resolved.snapshot.mirror())?;
let (history, _) = history_deployment(
&project_root()?,
artifact_dst,
&key.target,
&key.source,
&guard.address,
commit,
)?;
Ok(Some(history))
}
fn rebuild_one(args: RebuildOne<'_>) -> Result<()> {
let RebuildOne {
backend,
registry,
underlying_source,
root,
commit,
resolved,
policy,
projection,
item,
artifact_dst,
layout,
key,
report,
template_opt_in,
vars,
deploy_root,
} = args;
let key_label = key.artifact.replace('/', "_");
let staging_base = std::env::temp_dir().join("phora-rebuild");
let staging = staging_base.join(format!("{key_label}-{}-{}", std::process::id(), nonce()));
let _guard = StagingGuard::new(&staging_base, &staging);
let staged = stage_artifact(
&StageRequest {
artifact: item,
target: projection,
variables: vars,
},
root,
policy,
&staging,
None,
template_opt_in,
|repo_relative| {
let path = SourcePath::new(&repo_relative.to_string_lossy().replace('\\', "/"))?;
let entry = backend.read(&resolved.snapshot, &path)?;
Ok((entry.bytes, entry.meta.kind))
},
)?;
let manifest_base = match &item.materialization {
Materialization::CollapsedDir { .. } | Materialization::WholeRoot { .. } => {
artifact_dst.to_path_buf()
}
Materialization::Leaf(_) => artifact_dst
.parent()
.map_or_else(|| artifact_dst.to_path_buf(), Path::to_path_buf),
};
let mut modified = false;
let mut files = Vec::with_capacity(staged.files.len());
for sf in staged.files {
let path = PathBuf::from(sf.destination.as_str());
let on_disk = manifest_base.join(&path);
let (size, mtime) = match disk_hash(&on_disk)? {
Some(actual) if actual.kind == sf.kind && actual.hash == sf.blake3 => {
(actual.size, actual.mtime)
}
_ => {
modified = true;
(sf.size, sf.mtime)
}
};
files.push(ManifestFile {
path,
kind: sf.kind,
size,
mtime,
blake3: sf.blake3,
});
}
let history = rebuilt_history(backend, resolved, item, artifact_dst, &key, commit)?;
let record = ArtifactRecord::projected(NewArtifactRecord {
key: key.clone(),
underlying_source,
commit,
digest: staged.digest,
layout: layout.kind.label().to_owned(),
kind: record_kind(&item.materialization),
allow_symlinks: policy.allow_symlinks,
preserve_executable: policy.preserve_executable,
files,
history,
vars_digest: staged.vars_digest,
deploy_root: Some(deploy_root),
layout_separator: layout.persisted_separator(),
});
registry.put_artifact(&record)?;
if modified {
report.modified.push(key);
}
Ok(())
}
#[expect(
clippy::too_many_arguments,
reason = "a linked record threads its source, policy, layout, kind, key, root, and destination"
)]
fn rebuild_linked(
registry: &dyn StateStore,
underlying_source: &str,
policy: &crate::source::ExportPolicy,
layout: &LayoutConfig,
kind: RecordKind,
key: ArtifactKey,
deploy_root: String,
artifact_dst: &Path,
) -> Result<()> {
let link_target = std::fs::read_link(artifact_dst).ok();
let record = ArtifactRecord {
version: 1,
key,
source: underlying_source.to_owned(),
commit: "link".to_owned(),
digest: "link:".to_owned(),
projected_at: chrono::Utc::now().to_rfc3339(),
layout: layout.kind.label().to_owned(),
kind,
allow_symlinks: policy.allow_symlinks,
preserve_executable: policy.preserve_executable,
files: vec![],
directories: None,
linked: true,
history: false,
worktree_admin_id: None,
mirror_key: None,
cache_git_root: None,
vars_digest: None,
deploy_root: Some(deploy_root),
layout_separator: layout.persisted_separator(),
link_target: link_target.map(|target| target.to_string_lossy().into_owned()),
};
registry.put_artifact(&record)?;
Ok(())
}
struct DiskHash {
kind: crate::sync::state::ManifestEntryKind,
hash: String,
size: u64,
mtime: u64,
}
fn disk_hash(path: &Path) -> Result<Option<DiskHash>> {
let meta = match std::fs::symlink_metadata(path) {
Ok(meta) => meta,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(Error::Sync(format!("stat {}: {e}", path.display()))),
};
let kind = if meta.file_type().is_symlink() {
crate::sync::state::ManifestEntryKind::Link
} else if meta.is_file() {
crate::sync::state::ManifestEntryKind::File
} else {
return Ok(None);
};
let content = match kind {
crate::sync::state::ManifestEntryKind::File => std::fs::read(path),
crate::sync::state::ManifestEntryKind::Link => link_target_bytes(path),
}
.map_err(|e| Error::Sync(format!("read {}: {e}", path.display())))?;
let mtime = filetime::FileTime::from_last_modification_time(&meta).unix_seconds();
Ok(Some(DiskHash {
kind,
hash: blake3::hash(&content).to_hex().to_string(),
size: content.len() as u64,
mtime: u64::try_from(mtime).unwrap_or(0),
}))
}
fn scan_foreign(target: &Target, managed_dests: &BTreeSet<PathBuf>) -> Result<Vec<PathBuf>> {
let target_path = target.expanded_path();
let entries = match std::fs::read_dir(&target_path) {
Ok(rd) => rd,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => {
return Err(Error::Sync(format!(
"read target dir {}: {e}",
target_path.display()
)));
}
};
let mut foreign = Vec::new();
for entry in entries {
let entry =
entry.map_err(|e| Error::Sync(format!("read {}: {e}", target_path.display())))?;
if entry.file_name().to_string_lossy().starts_with('.') {
continue;
}
let path = entry.path();
if managed_dests.contains(&path) || is_managed_ancestor(&path, managed_dests) {
continue;
}
foreign.push(path);
}
foreign.sort();
Ok(foreign)
}
fn is_managed_ancestor(dir: &Path, managed_dests: &BTreeSet<PathBuf>) -> bool {
managed_dests.iter().any(|dest| dest.starts_with(dir))
}