pub mod apply;
mod build;
pub(crate) mod confine;
pub mod content;
mod directories;
pub(crate) mod discover;
pub(crate) mod hooks;
pub mod inspect;
pub mod journal;
pub mod model;
pub(crate) mod observe;
mod plan;
mod preview;
pub mod progress;
mod prune;
mod rebuild;
pub mod reconcile;
pub mod recovery;
mod request;
mod resolve;
pub(crate) mod scan;
pub(crate) mod stage;
pub mod state;
mod target;
pub(crate) mod transitive;
mod verify;
#[cfg(test)]
mod tests;
pub use hooks::{HookOutcome, HookScope, HookStatus};
pub use plan::{plan_target, project_workspace};
#[cfg(test)]
use crate::projection::build::projected_artifact_keys;
use crate::projection::build::projected_artifacts;
use crate::projection::model::{ArtifactRelativePath, Projection};
#[cfg(test)]
use crate::sync::model::ReconciliationPolicy;
pub(crate) use preview::offered_leaves;
pub use preview::{
BindingWarnings, PreviewCollision, PreviewEntry, PreviewFile, PreviewTargetPlan,
PreviewWarning, SyncState, preview_targets,
};
pub use progress::{
ArtifactId, FetchId, FetchOutcome, Phase, ProgressSink, Severity, SilentSink, SyncSummary,
};
pub use rebuild::{RebuildReport, rebuild_registry, rebuild_registry_with};
pub use request::{
AppliedChange, Concurrency, ConflictPolicy, DeclineAll, HookPolicy, LockSet, MovedPinPolicy,
PrunePolicy, SkippedChange, SourcePolicy, SyncOptions, SyncReport, SyncRequest, SyncStatus,
SyncWarning, TrustPrompt, TrustRequest,
};
pub use verify::{
OverlayFinding, UntrustedHookFinding, VerifyMismatch, VerifyReason, VerifyReport, verify,
};
#[cfg(feature = "bench")]
pub use resolve::resolve_sources_for_bench;
#[cfg(test)]
use prune::prune_projected;
pub(crate) use prune::{orphan_artifact_path, orphan_records};
use request::SyncEvents;
use resolve::{ResolvedSourceMap, resolve_sources};
pub use stage::{StageRequest, StagedArtifact, StagedFile, stage_artifact};
#[cfg(test)]
use target::deploy_reconciled_target;
pub(crate) use target::record_artifact_path;
use target::{
Reconciliation, TargetRun, deploy_reconciled_target_report, resolve_conflicts,
resolve_conflicts_reusing,
};
#[cfg(test)]
use {
crate::config::LayoutKind, crate::lock::LockedSource,
crate::projection::diagnostic::ProjectionWarning, crate::sync::inspect::check_artifact_state,
};
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use crate::config::{
Config, DeployMode, ParsedSource, PreDeployOnFail, Protocol, SourceMode, merge_configs,
};
use crate::error::{Error, Result};
use crate::lock::{Lock, carry_shadowed_pins, merge_locks, split_locks};
use crate::source::{
SourceName, SourceStore, WorktreeAdminId, WorktreeDeployRequest, WorktreeMirrorGuard,
WorktreeRemoveRequest, is_local_path, remove_missing_mirror_worktree_gitlink,
};
use crate::sync::state::{ArtifactKey, ArtifactRecord, Ejection, StateStore};
use journal::Journal;
use recovery::recovery_sweep;
#[cfg(test)]
#[expect(
clippy::struct_excessive_bools,
reason = "legacy test fixtures exercise the old boolean mapping into SyncOptions"
)]
pub struct SyncInput<'a> {
pub base_config: &'a Config,
pub local_config: Option<&'a Config>,
pub base_lock: Option<Lock>,
pub local_lock: Option<Lock>,
pub force: bool,
pub interactive: bool,
pub prune: bool,
pub no_hooks: bool,
pub no_transitive_hooks: bool,
pub frozen: bool,
pub lockless: bool,
pub fast_forward: bool,
pub resolver: Option<&'a dyn ConflictResolver>,
pub jobs: Option<usize>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Resolution {
Skip,
Overwrite,
Eject,
Abort,
}
pub use model::ConflictKind;
#[derive(Debug, Clone)]
pub struct Conflict {
pub target: String,
pub source: String,
pub artifact: String,
pub kind: ConflictKind,
}
pub trait ConflictResolver {
fn resolve(&self, conflict: &Conflict) -> Resolution;
}
#[cfg(test)]
pub struct SyncOutput {
pub base_lock: Lock,
pub local_lock: Option<Lock>,
pub had_failures: bool,
pub deploy_failures: bool,
pub hook_results: Vec<hooks::HookOutcome>,
pub stripped_transitive_hooks: usize,
}
struct SyncRunInput<'a> {
base_config: &'a Config,
local_config: Option<&'a Config>,
locks: LockSet,
options: SyncOptions,
resolver: Option<&'a dyn ConflictResolver>,
sink: &'a dyn ProgressSink,
trust_prompt: Option<&'a dyn TrustPrompt>,
lockless: bool,
}
impl SyncRunInput<'_> {
fn refresh_sources(&self) -> bool {
matches!(self.options.source_policy, SourcePolicy::Refresh)
}
fn frozen(&self) -> bool {
matches!(self.options.source_policy, SourcePolicy::Frozen)
}
fn fast_forward(&self) -> bool {
matches!(self.options.moved_pin_policy, MovedPinPolicy::FastForward)
}
fn jobs(&self) -> Option<usize> {
self.options.concurrency.jobs.map(std::num::NonZero::get)
}
}
trait RunOptions {
fn options(&self) -> SyncOptions;
fn resolver(&self) -> Option<&dyn ConflictResolver>;
fn lockless(&self) -> bool;
fn sink(&self) -> &dyn ProgressSink {
progress::SILENT
}
fn trust_prompt(&self) -> Option<&dyn TrustPrompt> {
None
}
fn interactive(&self) -> bool {
matches!(
self.options().conflict_policy,
ConflictPolicy::ResolveInteractively
)
}
fn prune(&self) -> bool {
matches!(self.options().prune_policy, PrunePolicy::RemoveOrphans)
}
fn hooks_enabled(&self) -> bool {
!matches!(self.options().hook_policy, HookPolicy::None)
}
fn transitive_hooks_enabled(&self) -> bool {
matches!(self.options().hook_policy, HookPolicy::All)
}
}
impl RunOptions for SyncRunInput<'_> {
fn options(&self) -> SyncOptions {
self.options
}
fn resolver(&self) -> Option<&dyn ConflictResolver> {
self.resolver
}
fn lockless(&self) -> bool {
self.lockless
}
fn sink(&self) -> &dyn ProgressSink {
self.sink
}
fn trust_prompt(&self) -> Option<&dyn TrustPrompt> {
self.trust_prompt
}
}
#[cfg(test)]
impl RunOptions for SyncInput<'_> {
fn options(&self) -> SyncOptions {
self.run_input().options
}
fn resolver(&self) -> Option<&dyn ConflictResolver> {
self.resolver
}
fn lockless(&self) -> bool {
self.lockless
}
}
struct SyncExecution {
report: SyncReport,
deploy_failures: bool,
}
#[cfg(test)]
trait TestSyncInvocation {
type Output;
fn run_input(&self) -> SyncRunInput<'_>;
fn finish(execution: SyncExecution) -> Self::Output;
}
#[cfg(test)]
impl TestSyncInvocation for SyncRequest<'_> {
type Output = SyncReport;
fn run_input(&self) -> SyncRunInput<'_> {
SyncRunInput {
base_config: self.base_config,
local_config: self.local_config,
locks: self.locks.clone(),
options: self.options,
resolver: self.resolver,
sink: self.sink,
trust_prompt: self.trust_prompt,
lockless: false,
}
}
fn finish(execution: SyncExecution) -> Self::Output {
execution.report
}
}
#[cfg(test)]
impl TestSyncInvocation for SyncInput<'_> {
type Output = SyncOutput;
fn run_input(&self) -> SyncRunInput<'_> {
let source_policy = if self.frozen {
SourcePolicy::Frozen
} else {
SourcePolicy::Locked
};
let conflict_policy = if self.force {
ConflictPolicy::Overwrite
} else if self.interactive {
ConflictPolicy::ResolveInteractively
} else {
ConflictPolicy::Refuse
};
let hook_policy = if self.no_hooks {
HookPolicy::None
} else if self.no_transitive_hooks {
HookPolicy::NoTransitive
} else {
HookPolicy::All
};
SyncRunInput {
base_config: self.base_config,
local_config: self.local_config,
locks: LockSet {
base: self.base_lock.clone(),
local: self.local_lock.clone(),
},
options: SyncOptions {
source_policy,
conflict_policy,
prune_policy: if self.prune {
PrunePolicy::RemoveOrphans
} else {
PrunePolicy::KeepOrphans
},
hook_policy,
moved_pin_policy: if self.fast_forward {
MovedPinPolicy::FastForward
} else {
MovedPinPolicy::Seal
},
concurrency: Concurrency {
jobs: self.jobs.and_then(std::num::NonZeroUsize::new),
},
},
resolver: self.resolver,
sink: progress::SILENT,
trust_prompt: None,
lockless: self.lockless,
}
}
fn finish(execution: SyncExecution) -> Self::Output {
compatibility_output(execution)
}
}
#[cfg(test)]
fn compatibility_output(execution: SyncExecution) -> SyncOutput {
let SyncExecution {
report,
deploy_failures,
} = execution;
let stripped_transitive_hooks = report
.warnings
.iter()
.find_map(|w| match w {
SyncWarning::UntrustedTransitiveHooks { count } => Some(*count),
_ => None,
})
.unwrap_or_default();
let LockSet { base, local } = report.locks;
SyncOutput {
base_lock: base.expect("sync execution always returns a base lock"),
local_lock: local,
had_failures: report.status == SyncStatus::Failed,
deploy_failures,
hook_results: report.hook_outcomes,
stripped_transitive_hooks,
}
}
pub(super) fn target_parent(path: &Path) -> PathBuf {
match path.parent() {
Some(parent) if !parent.as_os_str().is_empty() => parent.to_path_buf(),
_ => PathBuf::from("."),
}
}
pub(super) fn effective_protocol(source: &ParsedSource, config: &Config) -> Protocol {
source
.protocol()
.or(config.protocol)
.unwrap_or(Protocol::Https)
}
pub(crate) fn resolved_remotes(
config: &Config,
parsed: &BTreeMap<String, ParsedSource>,
) -> Result<BTreeMap<String, String>> {
let mut remotes = BTreeMap::new();
for (name, source) in parsed {
let remote = if source.mode() == SourceMode::Url {
source
.source_url()
.ok_or_else(|| Error::Config(format!("source `{name}`: missing url")))?
.to_owned()
} else {
let protocol = effective_protocol(source, config);
source
.resolved_remote(&config.hosts, protocol)
.map_err(|e| Error::Config(format!("source `{name}`: {e}")))?
};
remotes.insert(name.clone(), remote);
}
Ok(remotes)
}
pub(super) fn remote_for<'a>(remotes: &'a BTreeMap<String, String>, name: &str) -> Result<&'a str> {
remotes
.get(name)
.map(String::as_str)
.ok_or_else(|| Error::Config(format!("no resolved remote for source `{name}`")))
}
pub(super) fn nonce() -> u64 {
static COUNTER: AtomicU64 = AtomicU64::new(0);
COUNTER.fetch_add(1, Ordering::Relaxed)
}
pub(super) fn trusted_preimages(effective_lock: Option<&Lock>) -> BTreeSet<String> {
let Some(lock) = effective_lock else {
return BTreeSet::new();
};
lock.trusted_hooks
.iter()
.map(|h| h.preimage.clone())
.collect()
}
fn take_hook_candidates(
graph: &mut transitive::ResolvedGraph,
events: &mut SyncEvents,
) -> Vec<transitive::TransitiveHookCandidate> {
for diagnostic in std::mem::take(&mut graph.hook_diagnostics) {
events.push_warning(SyncWarning::MalformedTransitiveHooks {
target: diagnostic.target,
detail: diagnostic.detail,
});
}
std::mem::take(&mut graph.hook_candidates)
}
fn record_candidate_hooks(
base_lock: &mut Lock,
candidates: &[transitive::TransitiveHookCandidate],
) {
base_lock.candidate_hooks = candidates
.iter()
.map(crate::lock::CandidateHookRecord::from)
.collect();
}
struct TransitiveHookDecision {
outcomes: Vec<hooks::HookOutcome>,
stripped: usize,
}
fn decide_transitive_hooks(
base_lock: &mut Lock,
candidates: &[transitive::TransitiveHookCandidate],
effective_lock: Option<&Lock>,
prompt: Option<&dyn TrustPrompt>,
) -> Result<TransitiveHookDecision> {
let trusted = trusted_preimages(effective_lock);
let runs: Vec<hooks::TransitiveHookRun<'_>> = candidates
.iter()
.map(|c| hooks::TransitiveHookRun {
dep_instance: &c.dep_instance,
hook_id: &c.hook_id,
command: &c.command,
preimage: &c.preimage,
target_path: &c.target_path,
source: &c.source,
commit: &c.commit,
})
.collect();
let (outcomes, approvals) = match prompt {
Some(prompt) => hooks::dispatch_transitive_hooks(&runs, &trusted, prompt)?,
None => hooks::dispatch_transitive_hooks(&runs, &trusted, &request::DeclineAll)?,
};
let now = chrono::Utc::now().to_rfc3339();
for approval in approvals {
base_lock.trusted_hooks.push(crate::lock::TrustedHook {
dep_instance: approval.dep_instance,
hook_id: approval.hook_id,
preimage: approval.preimage,
approved_at: now.clone(),
source: approval.source,
commit: approval.commit,
});
}
let ran = outcomes.len();
Ok(TransitiveHookDecision {
outcomes,
stripped: candidates.len() - ran,
})
}
struct DeployAll<'a, R> {
config: &'a Config,
parsed: &'a BTreeMap<String, ParsedSource>,
remotes: &'a BTreeMap<String, String>,
projection: &'a Projection,
protected: &'a confine::ProtectedPathSet,
content_store: Option<&'a Path>,
input: &'a dyn RunOptions,
resolved_sources: &'a ResolvedSourceMap,
backend: &'a dyn SourceStore,
registry: &'a R,
journal: &'a Journal,
}
impl<R: StateStore> DeployAll<'_, R> {
fn records(&self) -> Result<Vec<ArtifactRecord>> {
Ok(self.registry.all_artifacts()?)
}
}
struct DeployProtocol<'a, R> {
workspace: DeployAll<'a, R>,
fast_forward_drops: &'a [FastForwardDrop],
}
struct ApplyRun<'a> {
had_failures: bool,
pre_deploy: Vec<hooks::HookOutcome>,
aborted: bool,
changes: model::ChangeSet,
events: SyncEvents<'a>,
}
fn target_run<'a, R>(
ctx: &DeployAll<'a, R>,
target_name: &'a str,
target: &'a crate::config::Target,
) -> TargetRun<'a> {
TargetRun {
parsed: ctx.parsed,
target_name,
target,
remotes: ctx.remotes,
resolved_sources: ctx.resolved_sources,
vars: &ctx.config.vars,
protected: ctx.protected,
content_store: ctx.content_store,
}
}
#[derive(Default)]
struct PreDeployRun {
outcomes: Vec<hooks::HookOutcome>,
skipped_targets: BTreeSet<String>,
aborted: bool,
ran: bool,
}
fn run_pre_deploy<R>(ctx: &DeployAll<'_, R>) -> Result<PreDeployRun> {
let mut run = PreDeployRun::default();
if !ctx.input.hooks_enabled() {
return Ok(run);
}
for (target_name, target) in &ctx.config.targets {
let Some(hooks) = &target.hooks else {
continue;
};
if hooks.pre_deploy.is_none() {
continue;
}
let outcomes = hooks::dispatch_pre_deploy(hooks, target_name, &target.expanded_path())?;
run.ran |= !outcomes.is_empty();
let failed = outcomes
.iter()
.any(|outcome| outcome.status == hooks::HookStatus::Failure);
run.outcomes.extend(outcomes);
if !failed {
continue;
}
match hooks.pre_deploy_on_fail {
PreDeployOnFail::Abort => {
run.aborted = true;
break;
}
PreDeployOnFail::Skip => {
run.skipped_targets.insert(target_name.clone());
}
}
}
Ok(run)
}
fn refuse_lockless_mutation(
input: &dyn RunOptions,
registry: &dyn StateStore,
observed: &model::ObservedProjectState<ArtifactRecord>,
changeset: &model::ChangeSet,
) -> Result<()> {
let refresh_pending = observed.artifacts.iter().any(|entry| {
matches!(
entry.observation,
model::ObservedArtifact::Managed(model::ManagedArtifact {
condition: model::ManagedCondition::MetadataChangedButContentClean { .. },
..
})
) && !changeset.changes.iter().any(|change| {
matches!(
change,
model::SyncChange::RewriteOverlay {
target,
source,
artifact,
} if target == &entry.target && source == &entry.source && artifact == &entry.artifact
)
})
});
let other_mutation = changeset
.changes
.iter()
.any(|change| !matches!(change, model::SyncChange::RewriteOverlay { .. }));
if input.lockless() && (other_mutation || refresh_pending) {
return Err(readonly_state_error(registry));
}
Ok(())
}
fn sweep_history_worktrees<R>(ctx: &DeployAll<'_, R>) -> Result<()>
where
R: StateStore,
{
let mut swept = BTreeSet::new();
for record in ctx.records()? {
let Some((address, admin_id)) = observe::history_address(&record)? else {
continue;
};
let identity = format!(
"{}:{}",
address.cache_git_root.display(),
address.key.as_str()
);
if swept.contains(&identity) {
continue;
}
let source = SourceName::trusted(record.source.clone());
match ctx.backend.lock_worktree_mirror_at(&source, &address) {
Ok(guard) => {
guard.sweep_worktrees()?;
swept.insert(identity);
}
Err(error) => {
let Some(target) = ctx.config.targets.get(&record.key.target) else {
return Err(error.into());
};
let path = prune::removal_path(target, &record, ctx.protected)?;
if !remove_missing_mirror_worktree_gitlink(&address, &admin_id, &path)? {
return Err(error.into());
}
}
}
}
Ok(())
}
fn observe_reconciliation<R>(
ctx: &DeployAll<'_, R>,
policy: model::ReconciliationPolicy,
) -> Result<(
model::ObservedProjectState<state::ArtifactRecord>,
Vec<model::ObservedEntry<state::ArtifactRecord>>,
model::ChangeSet,
)>
where
R: StateStore,
{
ctx.input.sink().phase_started(Phase::Observe);
let observed = observe::observe_workspace(ctx, ctx.projection)?;
let retirements = observe::observe_history_retirements(ctx)?;
let mut changeset = reconcile::reconcile(ctx.projection, &observed, &policy)
.map_err(|error| Error::Sync(error.to_string()))?;
reconcile::add_history_retirement_conflicts(&mut changeset, &retirements, policy);
ctx.input.sink().phase_finished(Phase::Observe);
Ok((observed, retirements, changeset))
}
fn apply_target_changes<'a, R>(
ctx: &DeployAll<'a, R>,
fast_forward_drops: &[FastForwardDrop],
) -> Result<ApplyRun<'a>>
where
R: StateStore,
{
let options = ctx.input.options();
let policy = (&options).into();
if ctx.input.prune() {
sweep_history_worktrees(ctx)?;
}
let (initial_observed, initial_retirements, initial_changeset) =
observe_reconciliation(ctx, policy)?;
let registry: &dyn StateStore = ctx.registry;
refuse_lockless_mutation(ctx.input, registry, &initial_observed, &initial_changeset)?;
let initial_decisions = resolve_conflicts(
&initial_changeset,
ctx.input.resolver(),
ctx.input.interactive(),
)?;
let PreDeployRun {
outcomes,
skipped_targets,
aborted,
ran,
} = run_pre_deploy(ctx)?;
if aborted {
return Ok(ApplyRun {
had_failures: false,
pre_deploy: outcomes,
aborted: true,
changes: initial_changeset,
events: SyncEvents::new(ctx.input.sink()),
});
}
let (observed, changeset, retirements, decisions) = if ran {
let (observed, retirements, changeset) = observe_reconciliation(ctx, policy)?;
refuse_lockless_mutation(ctx.input, registry, &observed, &changeset)?;
let decisions = resolve_conflicts_reusing(
&changeset,
&initial_decisions,
&skipped_targets,
ctx.input.resolver(),
ctx.input.interactive(),
)?;
(observed, changeset, retirements, decisions)
} else {
(
initial_observed,
initial_changeset,
initial_retirements,
initial_decisions,
)
};
let reconciliation = Reconciliation::new(&changeset, &observed, &retirements, decisions);
let mut run = ApplyRun {
had_failures: !skipped_targets.is_empty(),
pre_deploy: outcomes,
aborted: false,
changes: changeset.clone(),
events: SyncEvents::new(ctx.input.sink()),
};
apply_fast_forward_drops(
fast_forward_drops,
ctx.backend,
ctx.registry,
ctx.config,
ctx.protected,
&skipped_targets,
&mut run.events,
)?;
ctx.input.sink().phase_started(Phase::Apply);
run.had_failures |=
deploy_reconciled_targets(ctx, &reconciliation, &mut run.events, &skipped_targets)?;
ctx.input.sink().phase_finished(Phase::Apply);
if !run.had_failures {
ctx.input.sink().phase_started(Phase::Prune);
prune::apply_reconciled_removals(
&changeset.changes,
&observed,
ctx.projection,
ctx,
&mut run.events,
)?;
ctx.input.sink().phase_finished(Phase::Prune);
} else if ctx.input.prune() && run.had_failures {
run.events
.push_warning(SyncWarning::PruneSkippedAfterFailures);
}
Ok(run)
}
fn deploy_reconciled_targets<R>(
ctx: &DeployAll<'_, R>,
reconciliation: &Reconciliation<'_>,
events: &mut SyncEvents,
skipped_targets: &BTreeSet<String>,
) -> Result<bool>
where
R: StateStore,
{
let mut had_failures = false;
for (target_name, target) in &ctx.config.targets {
if skipped_targets.contains(target_name) {
continue;
}
let Some(target_projection) = ctx
.projection
.targets
.iter()
.find(|target_projection| &target_projection.target == target_name)
else {
continue;
};
had_failures |= deploy_reconciled_target_report(
target_run(ctx, target_name, target),
target_projection,
reconciliation,
ctx.backend,
ctx.registry,
ctx.journal,
events,
)?;
}
Ok(had_failures)
}
#[derive(Clone, Debug)]
pub(super) struct Placement {
pub target: String,
pub physical: PathBuf,
identity: PathBuf,
parent_entries: Vec<PathBuf>,
}
impl Placement {
pub fn new(target: &str, path: &Path) -> Result<Self> {
let physical = confine::normalize_physical_entry(path)?;
let identity = confine::fold_path(&physical);
let parent_entries = path
.ancestors()
.skip(1)
.map(|parent| {
confine::normalize_physical_entry(parent)
.map(|physical| confine::fold_path(&physical))
})
.collect::<Result<Vec<_>>>()?;
Ok(Self {
target: target.to_owned(),
physical,
identity,
parent_entries,
})
}
pub fn overlaps(&self, other: &Self) -> bool {
self.identity.starts_with(&other.identity)
|| other.identity.starts_with(&self.identity)
|| self.parent_entries.contains(&other.identity)
|| other.parent_entries.contains(&self.identity)
}
}
fn projected_placements(projection: &Projection, config: &Config) -> Result<Vec<Placement>> {
let cwd = std::env::current_dir()
.map_err(|e| Error::Sync(format!("resolve current dir for overlap check: {e}")))?;
let mut placements = Vec::new();
for target_projection in &projection.targets {
let Some(target) = config.targets.get(&target_projection.target) else {
continue;
};
let root = cwd.join(target.expanded_path());
for binding in &target_projection.bindings {
for item in projected_artifacts(binding) {
placements.push(Placement::new(
&target_projection.target,
&root.join(item.destination.as_str()),
)?);
}
}
}
Ok(placements)
}
fn reject_cross_target_overlap(projection: &Projection, config: &Config) -> Result<()> {
let placements = projected_placements(projection, config)?;
for (i, first) in placements.iter().enumerate() {
for second in &placements[i + 1..] {
if first.target != second.target && first.overlaps(second) {
return Err(cross_target_overlap_diagnostic(
&first.target,
&second.target,
&first.physical,
&second.physical,
));
}
}
}
Ok(())
}
fn cross_target_overlap_diagnostic(
first_target: &str,
second_target: &str,
first_path: &Path,
second_path: &Path,
) -> Error {
crate::diagnostic::SelectionDiagnostic {
entry: format!("{first_target} / {second_target}"),
matched_against: "the physical deploy destinations across all targets".to_owned(),
why: "two targets project artifacts onto overlapping physical paths".to_owned(),
did_you_mean: None,
remedy: "give each target a disjoint deploy path, or narrow their sources".to_owned(),
debug_hint: Some("phora preview".to_owned()),
details: vec![
format!("target `{first_target}`: {}", first_path.display()),
format!("target `{second_target}`: {}", second_path.display()),
],
}
.sync()
}
fn notify_orphans<R: StateStore>(deploy: &DeployAll<'_, R>, events: &mut SyncEvents) -> Result<()> {
let count = orphan_records(deploy.config, deploy.registry)?.len();
if count > 0 {
events.push_warning(SyncWarning::OrphanedRecords { count });
}
Ok(())
}
fn sweep_target_parents(
config: &Config,
journal: &Journal,
registry: &dyn StateStore,
) -> Result<()> {
let mut swept_parents: BTreeSet<PathBuf> = BTreeSet::new();
for target in config.targets.values() {
let parent = match &target.confine {
Some(anchor) => anchor.clone(),
None => target_parent(&target.expanded_path()),
};
if swept_parents.insert(parent.clone()) {
recovery_sweep(&parent, journal, registry)?;
}
}
Ok(())
}
fn effective_lock(input: &SyncRunInput<'_>) -> Option<Lock> {
match (&input.locks.base, &input.locks.local) {
(Some(base), local) => Some(merge_locks(base, local.as_ref())),
(None, Some(local)) => Some(local.clone()),
(None, None) => None,
}
}
fn local_source_names(input: &SyncRunInput<'_>) -> BTreeSet<String> {
input
.local_config
.map(|config| config.sources.keys().cloned().collect())
.unwrap_or_default()
}
fn merged_config(input: &SyncRunInput<'_>) -> Config {
merge_configs(input.base_config.clone(), input.local_config.cloned())
}
fn open_sync_journal(lockless: bool, registry: &dyn StateStore) -> Result<Journal> {
if lockless {
Ok(Journal::open_readonly(®istry.journal_root()))
} else {
Journal::open(®istry.journal_root())
}
}
fn readonly_state_error(registry: &dyn StateStore) -> Error {
let journal_root = registry.journal_root();
let state_root = journal_root.parent().unwrap_or(&journal_root);
state::readonly_root_error(state_root).into()
}
enum FastForwardDrop {
Remove { record: ArtifactRecord },
KeepLive { record: ArtifactRecord },
}
impl FastForwardDrop {
fn record(&self) -> &ArtifactRecord {
match self {
Self::Remove { record, .. } | Self::KeepLive { record, .. } => record,
}
}
}
fn plan_fast_forward_drops(
projection: &Projection,
config: &Config,
registry: &dyn StateStore,
protected: &confine::ProtectedPathSet,
drops: Vec<ArtifactRecord>,
lockless: bool,
) -> Result<Vec<FastForwardDrop>> {
if drops.is_empty() {
return Ok(Vec::new());
}
if lockless {
return Err(readonly_state_error(registry));
}
let expected_paths = prune::expected_live_paths(projection, config);
let mut plan = Vec::new();
for record in drops {
let Some(target) = config.targets.get(&record.key.target) else {
continue;
};
let dst = prune::removal_destination(target, &record);
let path = prune::removal_path(target, &record, protected).map_err(|error| {
Error::Sync(format!(
"fast-forward refuses out-of-anchor {}: {error}; eject it instead",
dst.display()
))
})?;
if prune::overlaps_live_dest(&path, &expected_paths, &record.key.target) {
plan.push(FastForwardDrop::KeepLive { record });
} else {
plan.push(FastForwardDrop::Remove { record });
}
}
Ok(plan)
}
fn projected_artifact_count(projection: &Projection) -> usize {
projection
.targets
.iter()
.flat_map(|target| target.bindings.iter())
.map(|binding| binding.artifacts.len())
.sum()
}
fn project_sync_workspace(
config: &Config,
parsed: &BTreeMap<String, ParsedSource>,
remotes: &BTreeMap<String, String>,
backend: &dyn SourceStore,
resolved_commits: &BTreeMap<(String, String), String>,
resolved_sources: &ResolvedSourceMap,
) -> Result<Projection> {
let projection = project_workspace(
config,
parsed,
remotes,
backend,
resolved_commits,
resolved_sources,
)?;
reject_cross_target_overlap(&projection, config)?;
Ok(projection)
}
#[cfg(not(test))]
pub fn sync<R>(
request: &SyncRequest<'_>,
backend: &dyn SourceStore,
registry: &R,
) -> Result<SyncReport>
where
R: StateStore,
{
let input = request_run_input(request, false);
sync_core(&input, backend, registry).map(|execution| execution.report)
}
#[cfg(test)]
fn sync<I, R>(input: &I, backend: &dyn SourceStore, registry: &R) -> Result<I::Output>
where
I: TestSyncInvocation,
R: StateStore,
{
let run_input = input.run_input();
sync_core(&run_input, backend, registry).map(I::finish)
}
pub(crate) fn sync_opened<R>(
request: &SyncRequest<'_>,
backend: &dyn SourceStore,
registry: &R,
lockless: bool,
) -> Result<SyncReport>
where
R: StateStore,
{
let input = request_run_input(request, lockless);
sync_core(&input, backend, registry).map(|execution| execution.report)
}
fn request_run_input<'a>(request: &'a SyncRequest<'a>, lockless: bool) -> SyncRunInput<'a> {
SyncRunInput {
base_config: request.base_config,
local_config: request.local_config,
locks: request.locks.clone(),
options: request.options,
resolver: request.resolver,
sink: request.sink,
trust_prompt: request.trust_prompt,
lockless,
}
}
fn sync_core<R>(
input: &SyncRunInput<'_>,
backend: &dyn SourceStore,
registry: &R,
) -> Result<SyncExecution>
where
R: StateStore,
{
let started = std::time::Instant::now();
let mut events = SyncEvents::new(input.sink());
let mut effective_config = merged_config(input);
effective_config.validate()?;
for source in effective_config.unbound_sources() {
events.push_warning(SyncWarning::UnboundSource {
source: source.to_owned(),
});
}
let pre_sync_outcomes = run_pre_sync(input, &effective_config)?;
if pre_sync_outcomes
.iter()
.any(|o| o.status == hooks::HookStatus::Failure)
{
return Ok(aborted_before_deploy_phase(
input
.locks
.base
.clone()
.unwrap_or_else(|| split_locks(Vec::new(), &BTreeSet::new()).0),
input.locks.local.clone(),
pre_sync_outcomes,
events,
started,
));
}
let mut parsed = effective_config.parsed_sources()?;
let builds = build::run(input, &effective_config, &parsed, backend, &mut events)?;
let build_failed = builds.failed;
let mut remotes = resolved_remotes(&effective_config, &parsed)?;
let effective_lock = effective_lock(input);
input.sink().phase_started(Phase::Compose);
let mut graph = transitive::resolve_transitive_graph(
&effective_config,
&parsed,
backend,
input.frozen(),
if input.refresh_sources() {
None
} else {
effective_lock.as_ref()
},
)?;
let hook_candidates = take_hook_candidates(&mut graph, &mut events);
let import_refs = graph.import_refs.clone();
let instances = graph.inject(&mut effective_config, &mut parsed, &mut remotes);
input.sink().phase_finished(Phase::Compose);
let workspace = SyncWorkspace {
config: effective_config,
parsed,
remotes,
instances,
import_refs,
hook_candidates,
builds,
};
let mut execution = sync_workspace(
input,
workspace,
backend,
registry,
pre_sync_outcomes,
events,
started,
)?;
if build_failed {
execution.report.status = SyncStatus::Failed;
}
Ok(execution)
}
struct SyncWorkspace {
config: Config,
parsed: BTreeMap<String, ParsedSource>,
remotes: BTreeMap<String, String>,
instances: BTreeMap<String, String>,
import_refs: Vec<resolve::ImportResolution>,
hook_candidates: Vec<transitive::TransitiveHookCandidate>,
builds: build::Builds,
}
#[expect(
clippy::too_many_lines,
reason = "the ordered resolve, project, and apply pipeline"
)]
fn sync_workspace<R: StateStore>(
input: &SyncRunInput<'_>,
workspace: SyncWorkspace,
backend: &dyn SourceStore,
registry: &R,
pre_sync_outcomes: Vec<hooks::HookOutcome>,
mut events: SyncEvents<'_>,
started: std::time::Instant,
) -> Result<SyncExecution> {
let SyncWorkspace {
config: effective_config,
parsed,
remotes,
instances,
import_refs,
hook_candidates,
builds,
} = workspace;
let effective_lock = effective_lock(input);
for warning in validate_link_mode(input.base_config, &parsed, &remotes)? {
events.push_warning(SyncWarning::LinkPathNotPortable {
source: warning.source,
path: warning.path,
});
}
let local_names = local_source_names(input);
let compat_registry: &dyn StateStore = registry;
let journal = open_sync_journal(input.lockless, compat_registry)?;
let cwd = std::env::current_dir()
.map_err(|e| Error::Sync(format!("resolve current dir for confinement: {e}")))?;
let protected = confine::ProtectedPathSet::resolve(&effective_config.paths, &cwd)?;
let content_store =
crate::paths::cache_root_for(effective_config.paths.cache.as_deref(), &cwd)?
.join("content");
sweep_target_parents(&effective_config, &journal, compat_registry)?;
let recorded_after_recovery = live_recorded_artifacts(compat_registry)?;
input.sink().phase_started(Phase::Resolve);
let mut routed = resolve_sources(
&effective_config,
&parsed,
&remotes,
&instances,
&import_refs,
effective_lock.as_ref(),
backend,
input.refresh_sources(),
input.frozen(),
input.jobs(),
input.sink(),
)?;
builds.route(&mut routed);
input.sink().phase_finished(Phase::Resolve);
let (mut base_lock, local_lock) = split_locks(routed.locks, &local_names);
if let Some(prior) = &input.locks.base {
let shadowed = local_names
.iter()
.filter(|name| input.base_config.sources.contains_key(*name))
.cloned()
.collect();
carry_shadowed_pins(&mut base_lock, prior, &shadowed);
}
base_lock.trusted_hooks = effective_lock
.as_ref()
.map(|lock| lock.trusted_hooks.clone())
.unwrap_or_default();
record_candidate_hooks(&mut base_lock, &hook_candidates);
collect_ref_transitions(
&effective_config,
&parsed,
effective_lock.as_ref(),
&routed.commits,
&mut events,
);
input.sink().phase_started(Phase::Project);
let mut projection = project_sync_workspace(
&effective_config,
&parsed,
&remotes,
backend,
&routed.commits,
&routed.resolved,
)?;
input.sink().phase_finished(Phase::Project);
input
.sink()
.artifacts_planned(projected_artifact_count(&projection));
for warning in std::mem::take(&mut projection.warnings) {
events.push_warning(SyncWarning::Projection(warning));
}
let pending_fast_forward_drops = validate_sealed_offer(
&effective_config,
&parsed,
&projection,
&recorded_after_recovery,
input.fast_forward(),
input.prune(),
)?;
let fast_forward_drops = plan_fast_forward_drops(
&projection,
&effective_config,
compat_registry,
&protected,
pending_fast_forward_drops,
input.lockless,
)?;
let deploy = DeployProtocol {
workspace: DeployAll {
config: &effective_config,
parsed: &parsed,
remotes: &remotes,
projection: &projection,
resolved_sources: &routed.resolved,
protected: &protected,
content_store: Some(&content_store),
input,
backend,
registry,
journal: &journal,
},
fast_forward_drops: &fast_forward_drops,
};
deploy_and_run_hooks(
&deploy,
base_lock,
local_lock,
&hook_candidates,
effective_lock.as_ref(),
pre_sync_outcomes,
events,
started,
)
}
#[expect(
clippy::too_many_arguments,
reason = "the deploy phase threads both locks, both hook sets, the accumulator, and the run clock"
)]
fn deploy_and_run_hooks<R>(
protocol: &DeployProtocol<'_, R>,
mut base_lock: Lock,
local_lock: Option<Lock>,
hook_candidates: &[transitive::TransitiveHookCandidate],
effective_lock: Option<&Lock>,
pre_sync_outcomes: Vec<hooks::HookOutcome>,
events: SyncEvents,
started: std::time::Instant,
) -> Result<SyncExecution>
where
R: StateStore,
{
let DeployProtocol {
workspace: deploy,
fast_forward_drops,
} = protocol;
let mut run = apply_target_changes(deploy, fast_forward_drops)?;
run.events.applied.splice(0..0, events.applied);
run.events.skipped.splice(0..0, events.skipped);
run.events.warnings.splice(0..0, events.warnings);
let mut early_hooks = pre_sync_outcomes;
early_hooks.extend(run.pre_deploy);
if run.aborted {
return Ok(aborted_before_deploy_phase(
base_lock,
local_lock,
early_hooks,
run.events,
started,
));
}
let mut had_failures = run.had_failures;
let registry: &dyn StateStore = deploy.registry;
if !deploy.input.prune() {
notify_orphans(deploy, &mut run.events)?;
}
deploy.input.sink().phase_started(Phase::Hooks);
let (hook_results, stripped_transitive_hooks) = run_all_hooks(
deploy.input,
deploy.config,
registry,
&mut base_lock,
hook_candidates,
effective_lock,
early_hooks,
)?;
deploy.input.sink().phase_finished(Phase::Hooks);
for outcome in &hook_results {
deploy.input.sink().hook_finished(outcome);
}
if stripped_transitive_hooks > 0 {
run.events
.push_warning(SyncWarning::UntrustedTransitiveHooks {
count: stripped_transitive_hooks,
});
}
let deploy_failures = had_failures;
had_failures |= hook_results
.iter()
.any(|o| o.status == hooks::HookStatus::Failure);
let summary = summarize(
deploy.config.targets.len(),
run.events.unchanged,
&run.events.applied,
&run.events.skipped,
&hook_results,
started.elapsed(),
);
deploy.input.sink().finished(&summary);
Ok(SyncExecution {
report: SyncReport {
locks: LockSet {
base: Some(base_lock),
local: local_lock,
},
changes: run.changes,
applied: run.events.applied,
skipped: run.events.skipped,
warnings: run.events.warnings,
hook_outcomes: hook_results,
status: if had_failures {
SyncStatus::Failed
} else {
SyncStatus::Success
},
},
deploy_failures,
})
}
fn summarize(
targets: usize,
unchanged: usize,
applied: &[AppliedChange],
skipped: &[SkippedChange],
hooks: &[hooks::HookOutcome],
elapsed: std::time::Duration,
) -> SyncSummary {
let mut summary = SyncSummary {
targets,
unchanged,
elapsed,
..SyncSummary::default()
};
for change in applied {
match change {
AppliedChange::Deployed { .. } => summary.deployed += 1,
AppliedChange::Overwritten { .. } => summary.overwritten += 1,
AppliedChange::OverlayRewritten { .. } => summary.overlays_rewritten += 1,
AppliedChange::Ejected { .. } => summary.ejected += 1,
AppliedChange::Removed { .. } => summary.removed += 1,
}
}
for change in skipped {
match change {
SkippedChange::Conflict { .. } => summary.conflicts += 1,
SkippedChange::Failed { .. } | SkippedChange::ReadonlyOverlayRewrite { .. } => {
summary.failures += 1;
}
}
}
summary.hooks_run = hooks.len();
summary.hooks_failed = hooks
.iter()
.filter(|outcome| outcome.status == hooks::HookStatus::Failure)
.count();
summary
}
fn aborted_before_deploy_phase(
base_lock: Lock,
local_lock: Option<Lock>,
hook_results: Vec<hooks::HookOutcome>,
events: SyncEvents,
started: std::time::Instant,
) -> SyncExecution {
let summary = summarize(
0,
events.unchanged,
&events.applied,
&events.skipped,
&hook_results,
started.elapsed(),
);
events.sink().finished(&summary);
SyncExecution {
report: SyncReport {
locks: LockSet {
base: Some(base_lock),
local: local_lock,
},
changes: model::ChangeSet::default(),
applied: events.applied,
skipped: events.skipped,
warnings: events.warnings,
hook_outcomes: hook_results,
status: SyncStatus::Failed,
},
deploy_failures: false,
}
}
fn run_pre_sync(input: &SyncRunInput<'_>, config: &Config) -> Result<Vec<hooks::HookOutcome>> {
if !input.hooks_enabled() {
return Ok(Vec::new());
}
let target_names = config
.targets
.keys()
.map(String::as_str)
.collect::<Vec<_>>()
.join(" ");
hooks::dispatch_pre_sync(config, &target_names)
}
fn run_all_hooks(
input: &dyn RunOptions,
config: &Config,
registry: &dyn StateStore,
base_lock: &mut Lock,
hook_candidates: &[transitive::TransitiveHookCandidate],
effective_lock: Option<&Lock>,
early_hooks: Vec<hooks::HookOutcome>,
) -> Result<(Vec<hooks::HookOutcome>, usize)> {
let mut hook_results = early_hooks;
if input.hooks_enabled() {
hook_results.append(&mut hooks::dispatch_hooks(
config,
registry,
input.lockless(),
)?);
}
let mut stripped = 0;
if input.transitive_hooks_enabled() {
let mut decision = decide_transitive_hooks(
base_lock,
hook_candidates,
effective_lock,
input.interactive().then(|| input.trust_prompt()).flatten(),
)?;
stripped = decision.stripped;
hook_results.append(&mut decision.outcomes);
}
Ok((hook_results, stripped))
}
struct BindingOffer<'projection> {
selection: crate::projection::offer::OfferSelection,
projected: &'projection crate::projection::model::BindingProjection,
deploy_mode: DeployMode,
ref_label: String,
live_root: Option<PathBuf>,
}
#[derive(Clone, Copy)]
struct SealedRecordPolicy {
immutable_copy: bool,
commit_differs: bool,
mode_switched: bool,
}
impl SealedRecordPolicy {
fn classify(record: &ArtifactRecord, offer: &BindingOffer<'_>) -> Self {
Self {
immutable_copy: !record.linked && offer.deploy_mode == DeployMode::Copy,
commit_differs: record.commit != offer.projected.commit,
mode_switched: record.linked != (offer.deploy_mode == DeployMode::Link),
}
}
fn same_snapshot_transition(self) -> bool {
self.immutable_copy && !self.commit_differs
}
fn fast_forward_actionable(self) -> bool {
self.immutable_copy && self.commit_differs
}
}
fn validate_sealed_offer(
config: &Config,
parsed: &BTreeMap<String, ParsedSource>,
projection: &Projection,
recorded: &[ArtifactRecord],
fast_forward: bool,
prune: bool,
) -> Result<Vec<ArtifactRecord>> {
use crate::projection::offer::OfferSelection;
let mut offers: BTreeMap<(String, String), BindingOffer<'_>> = BTreeMap::new();
for (target_name, target) in &config.targets {
let Some(target_projection) = projection
.targets
.iter()
.find(|projected| projected.target == *target_name)
else {
continue;
};
for binding in target.resolve_sources(parsed) {
let Some(source) = parsed.get(binding.source) else {
continue;
};
let Some(projected_binding) = target_projection
.bindings
.iter()
.find(|projected| projected.identity == binding.identity)
else {
continue;
};
let offer = source.offer();
let selection =
OfferSelection::compile(offer.includes(), offer.excludes(), offer.root())?;
offers.insert(
(target_name.clone(), binding.identity.to_owned()),
BindingOffer {
selection,
projected: projected_binding,
deploy_mode: source.deploy_mode(),
ref_label: binding.effective_ref.to_string(),
live_root: live_source_root(config, source)?,
},
);
}
}
let mut dropped = Vec::new();
for record in recorded {
let key = &record.key;
let Some(offer) = offers.get(&(key.target.clone(), key.source.clone())) else {
continue;
};
let artifact = &key.artifact;
if offer
.projected
.artifacts
.iter()
.any(|projected| projected.materialization.published_key() == artifact)
{
continue;
}
let policy = SealedRecordPolicy::classify(record, offer);
if policy.same_snapshot_transition() || (prune && policy.mode_switched) {
continue;
}
let source_paths = record_source_paths(offer.projected, record)?;
let has_current_attribution = source_paths.iter().any(|source_path| {
offer
.projected
.attribution
.offered_leaves
.iter()
.any(|offered| paths_overlap(offered, source_path))
});
let selection_admits_candidate = source_paths
.iter()
.any(|source_path| offer.selection.admits_published(source_path));
if has_current_attribution || (policy.immutable_copy && !selection_admits_candidate) {
continue;
}
if prune
&& selection_admits_candidate
&& config.targets.get(&key.target).is_some_and(|target| {
dangling_file_link(record, target, offer.live_root.as_deref(), &source_paths)
})
{
continue;
}
if fast_forward && policy.fast_forward_actionable() {
dropped.push(record.clone());
continue;
}
let deploy_path = config.targets.get(&key.target).map(|target| {
target::record_artifact_path(target, record)
.display()
.to_string()
});
return Err(sealed_offer_diagnostic(&SealedOffer {
target: &key.target,
source: &key.source,
artifact,
deploy_path: deploy_path.as_deref(),
recorded_commit: &record.commit,
resolved_commit: &offer.projected.commit,
resolved_ref: &offer.ref_label,
fast_forward_actionable: policy.fast_forward_actionable(),
}));
}
Ok(dropped)
}
fn live_source_root(config: &Config, source: &ParsedSource) -> Result<Option<PathBuf>> {
if source.deploy_mode() != DeployMode::Link {
return Ok(None);
}
let remote = source.resolved_remote(&config.hosts, crate::config::Protocol::Https)?;
let path = PathBuf::from(remote).join(source.offer().root().unwrap_or(Path::new("")));
let absolute = std::path::absolute(path)
.map_err(|error| Error::Sync(format!("resolve live source: {error}")))?;
confine::normalize_physical(&absolute).map(Some)
}
fn dangling_file_link(
record: &ArtifactRecord,
target: &crate::config::Target,
source_root: Option<&Path>,
source_paths: &BTreeSet<String>,
) -> bool {
if !record.linked || record.kind != crate::sync::state::RecordKind::File {
return false;
}
let Some(root) = source_root else {
return false;
};
let destination = prune::removal_destination(target, record);
let Ok(link) = std::fs::read_link(&destination) else {
return false;
};
let actual = destination.parent().unwrap_or(Path::new(".")).join(link);
source_paths.iter().any(|source| {
let expected = root.join(source);
matches!(std::fs::symlink_metadata(&expected), Err(error) if error.kind() == std::io::ErrorKind::NotFound)
&& confine::normalize_physical_entry(&actual).ok()
.zip(confine::normalize_physical_entry(&expected).ok())
.is_some_and(|(actual, expected)| actual == expected)
})
}
fn record_source_paths(
binding: &crate::projection::model::BindingProjection,
record: &ArtifactRecord,
) -> Result<BTreeSet<String>> {
let artifact = record.key.artifact.as_str();
let mut paths = BTreeSet::from([artifact.to_owned()]);
if !record.linked && record.kind == crate::sync::state::RecordKind::Dir {
for file in &record.files {
let relative = persisted_manifest_relative_path(&file.path)?;
paths.insert(format!("{artifact}/{relative}"));
}
}
for resolved in &binding.attribution.resolved_takes {
if paths.iter().any(|path| paths_overlap(path, &resolved.dest)) {
paths.insert(resolved.source.clone());
}
}
if binding.attribution.copy_template_suffix && !record.linked && record.vars_digest.is_some() {
paths.extend(
paths
.iter()
.filter(|path| path.strip_suffix(".tmpl").is_none())
.map(|path| format!("{path}.tmpl"))
.collect::<Vec<_>>(),
);
}
Ok(paths)
}
fn persisted_manifest_relative_path(path: &Path) -> Result<ArtifactRelativePath> {
let relative = path.to_str().ok_or_else(|| {
Error::Sync(format!(
"invalid persisted manifest data path {}: path is not UTF-8",
path.display()
))
})?;
ArtifactRelativePath::new(relative).map_err(|error| {
Error::Sync(format!(
"invalid persisted manifest data path {}: {error}",
path.display()
))
})
}
fn paths_overlap(first: &str, second: &str) -> bool {
first == second
|| first
.strip_prefix(second)
.is_some_and(|rest| rest.starts_with('/'))
|| second
.strip_prefix(first)
.is_some_and(|rest| rest.starts_with('/'))
}
fn apply_fast_forward_drops(
drops: &[FastForwardDrop],
backend: &dyn SourceStore,
registry: &dyn StateStore,
config: &Config,
protected: &confine::ProtectedPathSet,
skipped_targets: &BTreeSet<String>,
events: &mut SyncEvents,
) -> Result<()> {
for drop in drops {
let record = drop.record();
if skipped_targets.contains(&record.key.target) {
continue;
}
let target = config.targets.get(&record.key.target).ok_or_else(|| {
Error::Sync(format!(
"fast-forward target `{}` disappeared before removal",
record.key.target
))
})?;
let path = prune::removal_path(target, record, protected).map_err(|error| {
Error::Sync(format!(
"fast-forward refuses out-of-anchor {}: {error}; eject it instead",
record.key.artifact
))
})?;
match drop {
FastForwardDrop::KeepLive { .. } => {
events.push_warning(SyncWarning::FastForwardKeptLive {
source: record.key.source.clone(),
artifact: record.key.artifact.clone(),
path,
});
}
FastForwardDrop::Remove { .. } => {
events.push_warning(SyncWarning::FastForwardDropped {
source: record.key.source.clone(),
artifact: record.key.artifact.clone(),
});
remove_orphan_path(&path).map_err(|error| {
Error::Sync(format!("fast-forward prune {}: {error}", path.display()))
})?;
detach_history_overlay(record, &path, backend)?;
}
}
registry.remove_artifact(&record.key)?;
}
Ok(())
}
#[cfg(test)]
fn prune_fast_forward_drops_report(
projection: &Projection,
config: &Config,
registry: &dyn StateStore,
protected: &confine::ProtectedPathSet,
drops: &[ArtifactRecord],
lockless: bool,
events: &mut SyncEvents,
) -> Result<()> {
let plan = plan_fast_forward_drops(
projection,
config,
registry,
protected,
drops.to_vec(),
lockless,
)?;
let backend = crate::source::GitBackend::new(PathBuf::new());
apply_fast_forward_drops(
&plan,
&backend,
registry,
config,
protected,
&BTreeSet::new(),
events,
)
}
struct SealedOffer<'a> {
target: &'a str,
source: &'a str,
artifact: &'a str,
deploy_path: Option<&'a str>,
recorded_commit: &'a str,
resolved_commit: &'a str,
resolved_ref: &'a str,
fast_forward_actionable: bool,
}
fn sealed_offer_diagnostic(ctx: &SealedOffer<'_>) -> Error {
let &SealedOffer {
target,
source,
artifact,
deploy_path,
recorded_commit,
resolved_commit,
resolved_ref,
fast_forward_actionable,
} = ctx;
let now = pin_label(resolved_ref, resolved_commit);
let mut details = vec![
format!("binding: source `{source}` → target `{target}`"),
format!(
"pin: recorded {} → now {now}",
short_commit(recorded_commit)
),
];
if let Some(path) = deploy_path {
details.push(format!("path: {path}"));
}
let remedy = if fast_forward_actionable {
"re-sync with `--fast-forward` to drop it, or eject it before removing it".to_string()
} else {
"restore the artifact to the source's offer, or eject it before removing it".to_string()
};
crate::diagnostic::SelectionDiagnostic {
entry: format!("{target}:{source}:{artifact}"),
matched_against: format!("the current offer of source `{source}` in target `{target}`"),
why: "a recorded artifact is no longer in the source's offer".to_string(),
did_you_mean: None,
remedy,
debug_hint: Some(format!("phora explain {target} {source} {artifact}")),
details,
}
.sync()
}
fn short_commit(commit: &str) -> &str {
commit.get(..8).unwrap_or(commit)
}
fn live_recorded_artifacts(registry: &dyn StateStore) -> Result<Vec<ArtifactRecord>> {
let recorded = registry.all_artifacts()?;
let ejected = crate::sync::state::ejected_index(registry, &recorded)?;
Ok(recorded
.into_iter()
.filter(|r| {
!ejected.contains(&(
r.key.target.clone(),
r.key.source.clone(),
r.key.artifact.clone(),
))
})
.collect())
}
fn pin_label(ref_label: &str, commit: &str) -> String {
if ref_label.is_empty() {
short_commit(commit).to_string()
} else {
format!("{ref_label} ({})", short_commit(commit))
}
}
fn collect_ref_transitions(
config: &Config,
parsed: &BTreeMap<String, ParsedSource>,
effective_lock: Option<&Lock>,
resolved_commits: &BTreeMap<(String, String), String>,
events: &mut SyncEvents,
) {
for transition in ref_transitions(config, parsed, effective_lock, resolved_commits) {
events.push_warning(SyncWarning::ReferenceMoved {
source: transition.source,
target: transition.target,
from: transition.from,
to: transition.to,
});
}
}
#[derive(Debug)]
struct RefTransition {
source: String,
target: String,
from: String,
to: String,
}
#[cfg(test)]
fn ref_transition_lines(
config: &Config,
parsed: &BTreeMap<String, ParsedSource>,
effective_lock: Option<&Lock>,
resolved_commits: &BTreeMap<(String, String), String>,
) -> Vec<String> {
ref_transitions(config, parsed, effective_lock, resolved_commits)
.into_iter()
.map(|transition| {
format!(
"phora: {} → {}: {} → {}",
transition.source, transition.target, transition.from, transition.to
)
})
.collect()
}
fn ref_transitions(
config: &Config,
parsed: &BTreeMap<String, ParsedSource>,
effective_lock: Option<&Lock>,
resolved_commits: &BTreeMap<(String, String), String>,
) -> Vec<RefTransition> {
let mut transitions = Vec::new();
for (target_name, target) in &config.targets {
for binding in target.resolve_sources(parsed) {
let Some(source) = parsed.get(binding.source) else {
continue;
};
let encoded_ref = crate::lock::encode_ref(&binding.effective_ref);
let Some(new_commit) = resolved_commits.get(&(binding.source.to_owned(), encoded_ref))
else {
continue;
};
let discriminator =
crate::lock::ref_discriminator(&binding.effective_ref, &source.refspec());
let Some(old) = effective_lock
.and_then(|lock| lock.find_entry(binding.source, discriminator.as_deref()))
else {
continue;
};
if &old.commit == new_commit {
continue;
}
let from = pin_label(&old.resolved, &old.commit);
let to = pin_label(&binding.effective_ref.to_string(), new_commit);
transitions.push(RefTransition {
source: binding.source.to_owned(),
target: target_name.clone(),
from,
to,
});
}
}
transitions
}
pub(crate) fn inject_composed_graph(
config: &mut Config,
parsed: &mut BTreeMap<String, ParsedSource>,
remotes: &mut BTreeMap<String, String>,
backend: &dyn SourceStore,
lock: Option<&Lock>,
) -> Vec<transitive::ComposedTarget> {
let Ok(graph) = transitive::resolve_transitive_graph(config, parsed, backend, true, lock)
else {
return Vec::new();
};
let composed = graph.targets.clone();
graph.inject(config, parsed, remotes);
composed
}
#[derive(Debug)]
struct LinkModeWarning {
source: String,
path: PathBuf,
}
impl LinkModeWarning {
#[cfg(test)]
fn contains(&self, needle: &str) -> bool {
self.source.contains(needle) || self.path.to_string_lossy().contains(needle)
}
}
fn validate_link_mode(
base: &Config,
effective: &BTreeMap<String, ParsedSource>,
remotes: &BTreeMap<String, String>,
) -> Result<Vec<LinkModeWarning>> {
let mut warnings = Vec::new();
for (name, source) in effective {
if source.deploy_mode() != DeployMode::Link {
continue;
}
let git = remote_for(remotes, name)?;
if !is_local_path(git) {
return Err(Error::Config(format!(
"source `{name}`: deploy = \"link\" requires a local filesystem path, \
not a remote URL `{git}`"
)));
}
if base.sources.contains_key(name) && Path::new(git).is_absolute() {
warnings.push(LinkModeWarning {
source: name.clone(),
path: PathBuf::from(git),
});
}
}
Ok(warnings)
}
pub(super) struct StagingGuard<'a> {
staging_base: &'a Path,
staging: &'a Path,
armed: bool,
}
impl<'a> StagingGuard<'a> {
pub(super) fn new(staging_base: &'a Path, staging: &'a Path) -> Self {
Self {
staging_base,
staging,
armed: true,
}
}
pub(super) fn disarm(&mut self) {
self.armed = false;
}
}
impl Drop for StagingGuard<'_> {
fn drop(&mut self) {
if !self.armed {
return;
}
let _ = remove_orphan_path(self.staging);
let _ = std::fs::remove_dir(self.staging_base);
}
}
pub(super) fn remove_orphan_path(path: &Path) -> std::io::Result<()> {
match std::fs::symlink_metadata(path) {
Ok(meta) if meta.is_dir() => std::fs::remove_dir_all(path),
Ok(_) => std::fs::remove_file(path),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
}
}
pub fn eject(
config: &Config,
registry: &dyn StateStore,
artifact: &str,
source: &str,
target: &str,
backend: &dyn SourceStore,
) -> Result<Option<PathBuf>> {
let target_config = config
.targets
.get(target)
.ok_or_else(|| Error::Config(format!("unknown target: {target}")))?;
let key = ArtifactKey {
target: target.to_owned(),
source: source.to_owned(),
artifact: artifact.to_owned(),
};
let record = registry.artifact(&key)?.ok_or_else(|| {
Error::StateStore(format!(
"{source}/{artifact} is not managed in target {target}"
))
})?;
let deploy_root = prune::removal_destination(target_config, &record);
eject_artifact(&key, Some(&record), &deploy_root, backend, registry)
}
fn standalone_clone_blocks_uneject(
target: &crate::config::Target,
record: &ArtifactRecord,
) -> Result<()> {
let deploy_root = prune::removal_destination(target, record);
if record.history && deploy_root.join(".git").is_dir() {
return Err(Error::Sync(format!(
"{} is a standalone clone; move its .git away before unejecting",
deploy_root.display()
)));
}
Ok(())
}
pub(super) fn eject_artifact(
key: &ArtifactKey,
record: Option<&ArtifactRecord>,
deploy_root: &Path,
backend: &dyn SourceStore,
registry: &dyn StateStore,
) -> Result<Option<PathBuf>> {
let clone = match record {
Some(record) => eject_history_overlay(record, deploy_root, backend)?,
None => None,
};
let mut ejected = registry.ejections(&key.target)?;
if !ejected
.iter()
.any(|entry| entry.source == key.source && entry.artifact == key.artifact)
{
ejected.push(Ejection {
source: key.source.clone(),
artifact: key.artifact.clone(),
ejected_at: chrono::Utc::now().to_rfc3339(),
});
registry.save_ejections(&key.target, &ejected)?;
}
Ok(clone)
}
fn eject_history_overlay(
record: &ArtifactRecord,
deploy_root: &Path,
backend: &dyn SourceStore,
) -> Result<Option<PathBuf>> {
let Some((guard, admin_id)) = lock_history_overlay(record, deploy_root, backend)? else {
return Ok(None);
};
let request = WorktreeDeployRequest {
admin_id: admin_id.clone(),
deploy_root: deploy_root.to_path_buf(),
commit: record.commit.parse()?,
};
if guard.eject_worktree(&request)? {
return Ok(Some(deploy_root.to_path_buf()));
}
guard.remove_worktree(&WorktreeRemoveRequest {
admin_id,
deploy_root: deploy_root.to_path_buf(),
})?;
Ok(None)
}
pub(super) fn detach_history_overlay(
record: &ArtifactRecord,
deploy_root: &Path,
backend: &dyn SourceStore,
) -> Result<()> {
let Some((guard, admin_id)) = lock_history_overlay(record, deploy_root, backend)? else {
return Ok(());
};
guard.remove_worktree(&WorktreeRemoveRequest {
admin_id,
deploy_root: deploy_root.to_path_buf(),
})?;
Ok(())
}
fn lock_history_overlay(
record: &ArtifactRecord,
deploy_root: &Path,
backend: &dyn SourceStore,
) -> Result<Option<(WorktreeMirrorGuard, WorktreeAdminId)>> {
let Some((address, admin_id)) = observe::history_address(record)? else {
return Ok(None);
};
if remove_missing_mirror_worktree_gitlink(&address, &admin_id, deploy_root)? {
return Ok(None);
}
let source = SourceName::trusted(record.source.clone());
match backend.lock_worktree_mirror_at(&source, &address) {
Ok(guard) => Ok(Some((guard, admin_id))),
Err(_) if remove_missing_mirror_worktree_gitlink(&address, &admin_id, deploy_root)? => {
Ok(None)
}
Err(error) => Err(error.into()),
}
}
pub fn uneject(
config: &Config,
registry: &dyn StateStore,
artifact: &str,
source: &str,
target: &str,
) -> Result<()> {
let Some(target_config) = config.targets.get(target) else {
return Err(Error::Config(format!("unknown target: {target}")));
};
let key = ArtifactKey {
target: target.to_owned(),
source: source.to_owned(),
artifact: artifact.to_owned(),
};
if let Some(record) = registry.artifact(&key)? {
standalone_clone_blocks_uneject(target_config, &record)?;
}
let mut ejected = registry.ejections(target)?;
ejected.retain(|e| !(e.source == source && e.artifact == artifact));
Ok(registry.save_ejections(target, &ejected)?)
}