use std::io::IsTerminal;
use std::num::NonZeroUsize;
use std::path::Path;
use crate::config::Config;
use crate::error::{Error, Result};
use crate::lock::{Lock, merge_locks};
use crate::paths::{cache_root_for, state_root_for};
use crate::sync::state::ProjectId;
use crate::sync::state::{FileStateStore, StateError, StateStore};
use crate::sync::{
Concurrency, ConflictPolicy, ConflictResolver, HookPolicy, LockSet, MovedPinPolicy,
PrunePolicy, SkippedChange, SourcePolicy, SyncOptions, SyncReport, SyncRequest, SyncWarning,
sync_opened,
};
use super::json::JsonSink;
use super::progress::{ProgressMode, TtySink};
use super::{
CliOutcome, DropSources, TtyResolver, build_router, drop_sources, load_config,
load_local_config, open_project_registry,
};
fn open_sync_registry(cwd: &Path, config: &Config) -> Result<FileStateStore> {
let projects_base = state_root_for(config.paths.state.as_deref(), cwd)?.join("projects");
let project = ProjectId::for_path(cwd)?;
Ok(FileStateStore::open(projects_base.join(project.as_str()))?)
}
#[expect(
clippy::fn_params_excessive_bools,
clippy::too_many_arguments,
reason = "independent CLI run flags, not a state machine"
)]
pub(super) fn run_sync(
prune: bool,
force: bool,
no_hooks: bool,
no_transitive_hooks: bool,
frozen: bool,
fast_forward: bool,
no_progress: bool,
json: bool,
drop: Option<DropSources>,
jobs: Option<usize>,
) -> Result<CliOutcome> {
let cwd = std::env::current_dir()?;
let base = load_config()?;
let local = load_local_config(&cwd)?;
let (mut base_lock, mut local_lock) = load_locks(&cwd)?;
if let Some(drop) = drop {
drop_sources(base_lock.as_mut(), &drop);
drop_sources(local_lock.as_mut(), &drop);
}
let effective = crate::config::merge_configs(base.clone(), local.clone());
let cache_git = cache_root_for(effective.paths.cache.as_deref(), &cwd)?.join("git");
let backend = build_router(&effective, cache_git)?;
let registry = open_sync_registry(&cwd, &effective)?;
let guard = match registry.acquire_lock() {
Ok(guard) => Some(guard),
Err(StateError::ReadOnly(_)) if frozen => None,
Err(e) => return Err(e.into()),
};
let lockless = guard.is_none();
let _guard = guard;
if !lockless && let Some(advisory) = registry.lock_advisory() {
eprintln!("{advisory}");
}
let interactive = std::io::stdin().is_terminal();
let resolver = TtyResolver;
let trust_prompt = super::TtyTrustPrompt;
let reporter = Reporter::select(json, no_progress);
let source_policy = if frozen {
SourcePolicy::Frozen
} else {
SourcePolicy::Locked
};
let conflict_policy = if force {
ConflictPolicy::Overwrite
} else if interactive {
ConflictPolicy::ResolveInteractively
} else {
ConflictPolicy::Refuse
};
let hook_policy = if no_hooks {
HookPolicy::None
} else if no_transitive_hooks {
HookPolicy::NoTransitive
} else {
HookPolicy::All
};
let jobs = jobs
.map(|jobs| {
NonZeroUsize::new(jobs)
.ok_or_else(|| Error::Config("--jobs must be greater than zero".to_owned()))
})
.transpose()?;
let request = SyncRequest {
base_config: &base,
local_config: local.as_ref(),
locks: LockSet {
base: base_lock,
local: local_lock,
},
options: SyncOptions {
source_policy,
conflict_policy,
prune_policy: if prune {
PrunePolicy::RemoveOrphans
} else {
PrunePolicy::KeepOrphans
},
hook_policy,
moved_pin_policy: if fast_forward {
MovedPinPolicy::FastForward
} else {
MovedPinPolicy::Seal
},
concurrency: Concurrency { jobs },
},
resolver: interactive.then_some(&resolver as &dyn ConflictResolver),
sink: reporter.sink(),
trust_prompt: interactive.then_some(&trust_prompt as &dyn crate::sync::TrustPrompt),
};
let out = sync_opened(&request, &backend, ®istry, lockless).inspect_err(|error| {
request.sink.aborted(&error.to_string());
})?;
let outcome = finish_sync(&cwd, &out, interactive, &reporter)?;
if outcome == CliOutcome::Success && !lockless {
super::cache::auto_prune(&effective.paths, &cwd);
}
Ok(outcome)
}
struct StrippedHookNotice {
message: String,
fail: bool,
}
fn stripped_hook_notice(stripped: usize, interactive: bool) -> Option<StrippedHookNotice> {
(stripped > 0).then(|| StrippedHookNotice {
message: format!(
"phora: {stripped} untrusted transitive hook(s) were stripped and not run — affected \
artifacts are deployed but NOT post-processed and may be incomplete\n\
phora: run `phora trust <name>` to inspect and approve {stripped} hook(s)"
),
fail: interactive,
})
}
enum Reporter {
Tty { sink: Box<TtySink>, live: bool },
Json(JsonSink),
}
impl Reporter {
fn select(json: bool, no_progress: bool) -> Self {
if json {
return Self::Json(JsonSink::new());
}
let mode = ProgressMode::resolve(no_progress);
Self::Tty {
sink: Box::new(TtySink::new(mode)),
live: mode == ProgressMode::Live,
}
}
fn sink(&self) -> &dyn crate::sync::ProgressSink {
match self {
Self::Tty { sink, .. } => sink.as_ref(),
Self::Json(sink) => sink,
}
}
fn is_json(&self) -> bool {
matches!(self, Self::Json(_))
}
fn summary(&self) -> Option<crate::sync::SyncSummary> {
match self {
Self::Tty { sink, live: true } => sink.summary(),
Self::Tty { live: false, .. } | Self::Json(_) => None,
}
}
}
fn finish_sync(
cwd: &Path,
out: &SyncReport,
interactive: bool,
reporter: &Reporter,
) -> Result<CliOutcome> {
let pre_sync_failed = out.hook_outcomes.iter().any(|hook| {
hook.scope == crate::sync::HookScope::PreSync
&& hook.status == crate::sync::HookStatus::Failure
});
if !pre_sync_failed {
let base_lock = out
.locks
.base
.as_ref()
.ok_or_else(|| Error::Lock("sync completed without a base lock".to_owned()))?;
write_locks(cwd, base_lock, out.locks.local.as_ref())?;
}
render_sync_warnings(out);
for applied in &out.applied {
match applied {
crate::sync::AppliedChange::Removed {
source, artifact, ..
} => eprintln!("phora: pruning orphaned {source}:{artifact}"),
crate::sync::AppliedChange::OverlayRewritten {
source, artifact, ..
} => eprintln!("phora: rewrote history overlay {source}:{artifact}"),
_ => {}
}
}
for skipped in &out.skipped {
match skipped {
SkippedChange::Failed {
source,
artifact,
message,
..
} => eprintln!("phora: failed to deploy {source}:{artifact}: {message}"),
SkippedChange::ReadonlyOverlayRewrite {
source, artifact, ..
} => eprintln!(
"phora: skipped history overlay rewrite {source}:{artifact}: read-only state"
),
SkippedChange::Conflict { .. } => {}
}
}
let report = super::render::render_hook_report(&out.hook_outcomes);
let stripped = out
.warnings
.iter()
.find_map(|warning| match warning {
SyncWarning::UntrustedTransitiveHooks { count } => Some(*count),
_ => None,
})
.unwrap_or(0);
if let Some(notice) = stripped_hook_notice(stripped, interactive) {
if !report.is_empty() {
eprint!("{report}");
}
eprintln!("{}", notice.message);
if notice.fail {
return Ok(CliOutcome::Failure);
}
}
if out.status == crate::sync::SyncStatus::Failed {
if !report.is_empty() {
eprint!("{report}");
}
let hooks_failed = out
.hook_outcomes
.iter()
.any(|o| o.status == crate::sync::HookStatus::Failure);
let deploy_failures = out
.skipped
.iter()
.any(|skipped| matches!(skipped, SkippedChange::Failed { .. }));
let message = match (deploy_failures, hooks_failed) {
(true, true) => "phora: some artifacts failed to deploy and one or more hooks failed",
(true, false) => "phora: some artifacts failed to deploy",
(false, _) => "phora: one or more hooks failed",
};
eprintln!("{message}");
return Ok(CliOutcome::Failure);
}
if reporter.is_json() {
if !report.is_empty() {
eprint!("{report}");
}
return Ok(CliOutcome::Success);
}
if !report.is_empty() {
print!("{report}");
}
match reporter.summary() {
Some(summary) => print!("{}", super::render::format_sync_summary(&summary)),
None => println!("sync complete"),
}
Ok(CliOutcome::Success)
}
fn render_sync_warnings(out: &SyncReport) {
for warning in &out.warnings {
if let Some(rendered) = super::render::format_sync_warning(warning) {
eprintln!("{rendered}");
}
}
}
pub(super) fn run_rebuild_registry() -> Result<()> {
let cwd = std::env::current_dir()?;
let base = load_config()?;
let local = load_local_config(&cwd)?;
let mut config = crate::config::merge_configs(base, local);
config.validate()?;
let registry = open_project_registry(&config)?;
let _guard = registry.acquire_lock()?;
let (base_lock, local_lock) = load_locks(&cwd)?;
let lock = match base_lock {
Some(base) => merge_locks(&base, local_lock.as_ref()),
None => local_lock
.ok_or_else(|| Error::Lock("no lock file found; run sync first".to_owned()))?,
};
let cache_git = cache_root_for(config.paths.cache.as_deref(), &cwd)?.join("git");
let backend = build_router(&config, cache_git)?;
let mut parsed = config.parsed_sources()?;
let mut remotes = crate::sync::resolved_remotes(&config, &parsed)?;
crate::sync::inject_composed_graph(
&mut config,
&mut parsed,
&mut remotes,
&backend,
Some(&lock),
);
let report =
crate::sync::rebuild_registry_with(&config, &parsed, &remotes, &lock, &backend, ®istry)?;
println!("reconstructed {}", report.reconstructed.len());
if !report.modified.is_empty() {
let modified: Vec<String> = report
.modified
.iter()
.map(|k| format!("{}/{}", k.source, k.artifact))
.collect();
println!("modified [{}]", modified.join(", "));
}
if !report.foreign.is_empty() {
let foreign: Vec<String> = report
.foreign
.iter()
.map(|p| p.display().to_string())
.collect();
println!("foreign [{}]", foreign.join(", "));
}
Ok(())
}
pub(super) fn run_update(
source: Option<&str>,
fast_forward: bool,
prune: bool,
) -> Result<CliOutcome> {
let drop = source.map_or(DropSources::All, |s| DropSources::One(s.to_owned()));
run_sync(
prune,
false,
false,
false,
false,
fast_forward,
false,
false,
Some(drop),
None,
)
}
pub fn write_locks(dir: &Path, base: &Lock, local: Option<&Lock>) -> Result<()> {
let base_path = dir.join("phora.lock");
let base_text =
toml::to_string(base).map_err(|e| Error::Lock(format!("serialize phora.lock: {e}")))?;
std::fs::write(&base_path, base_text)
.map_err(|e| Error::Lock(format!("write {}: {e}", base_path.display())))?;
let local_path = dir.join("phora.local.lock");
match local {
Some(local) => {
let local_text = toml::to_string(local)
.map_err(|e| Error::Lock(format!("serialize phora.local.lock: {e}")))?;
std::fs::write(&local_path, local_text)
.map_err(|e| Error::Lock(format!("write {}: {e}", local_path.display())))?;
}
None => match std::fs::remove_file(&local_path) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => {
return Err(Error::Lock(format!(
"remove stale {}: {e}",
local_path.display()
)));
}
},
}
Ok(())
}
pub fn load_locks(dir: &Path) -> Result<(Option<Lock>, Option<Lock>)> {
Ok((
read_lock(&dir.join("phora.lock"))?,
read_lock(&dir.join("phora.local.lock"))?,
))
}
fn read_lock(path: &Path) -> Result<Option<Lock>> {
match std::fs::read_to_string(path) {
Ok(text) => toml::from_str(&text)
.map(Some)
.map_err(|e| Error::Lock(format!("parse {}: {e}", path.display()))),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(Error::Lock(format!("read {}: {e}", path.display()))),
}
}
#[cfg(test)]
mod tests {
use super::stripped_hook_notice;
#[test]
fn no_stripped_hooks_yields_no_notice() {
assert!(stripped_hook_notice(0, true).is_none());
assert!(stripped_hook_notice(0, false).is_none());
}
#[test]
fn stripped_hooks_under_a_tty_fail_the_command() {
let notice = stripped_hook_notice(2, true).expect("a notice when hooks are stripped");
assert!(
notice.fail,
"a TTY sync must fail so a human acts on the stripped hooks"
);
assert!(notice.message.contains("phora trust") && notice.message.contains("approve"));
assert!(notice.message.contains("incomplete"));
}
#[test]
fn stripped_hooks_under_non_tty_surface_but_do_not_fail() {
let notice = stripped_hook_notice(1, false).expect("a notice when hooks are stripped");
assert!(
!notice.fail,
"non-TTY/CI must stay green; the gap is surfaced, not fatal"
);
assert!(notice.message.contains("phora trust") && notice.message.contains("approve"));
}
}