refactor: put the dry-run and quiet checks on Ctx

This commit is contained in:
2026-09-09 12:32:55 +00:00
parent d9439253fd
commit b40dfe6124
10 changed files with 253 additions and 217 deletions

View File

@@ -11,9 +11,7 @@ use anyhow::{Context, Result, anyhow, bail};
use self::store::{Leads, Repo, ignored, leads, resolve, symlink_metadata_opt, tracked};
use crate::ctx::Ctx;
use crate::fsops::{
ensure_private_parent, move_path, place_link, prune_empty, remove_file, rename, suffixed,
};
use crate::fsops::suffixed;
use crate::output::{line, note, warning};
const BACKUP_SUFFIX: &str = ".ahab-bak";
@@ -137,10 +135,10 @@ fn restore_one(ctx: &Ctx, repo: &Repo, path: &Path, report: &Report) -> Result<(
bail!("{} is in the way; move it aside", aside.display());
}
rename(ctx, &src, &aside)?;
ctx.fs().rename(&src, &aside)?;
if let Err(e) = move_path(ctx, &stored, &src) {
rename(ctx, &aside, &src).with_context(|| {
if let Err(e) = ctx.fs().move_path(&stored, &src) {
ctx.fs().rename(&aside, &src).with_context(|| {
format!(
"could not put the link at {} back after failing to restore it",
src.display()
@@ -150,8 +148,8 @@ fn restore_one(ctx: &Ctx, repo: &Repo, path: &Path, report: &Report) -> Result<(
return Err(e);
}
remove_file(ctx, &aside)?;
prune_empty(ctx, stored.parent(), &repo.base);
ctx.fs().remove_file(&aside)?;
ctx.fs().prune_empty(stored.parent(), &repo.base);
report.line("restored", &rel);
Ok(())
@@ -295,7 +293,7 @@ fn link_one(ctx: &Ctx, repo: &Repo, path: &Path, force: bool, report: &Report) -
stays_in_store(repo, &rel)?;
// before anything is moved in, so the tree it lands in is never briefly
// readable by anyone else
ensure_private_parent(ctx, &repo.base, &target)?;
ctx.fs().ensure_private_parent(&repo.base, &target)?;
if tracked(ctx, repo, &rel)? {
return Err(anyhow!(
@@ -363,7 +361,7 @@ fn link_one(ctx: &Ctx, repo: &Repo, path: &Path, force: bool, report: &Report) -
return Err(needs_force(&target));
}
place_link(ctx, &src, &target)?;
ctx.fs().place_link(&src, &target)?;
report.line("repointed", &rel);
Ok(())
}
@@ -381,10 +379,10 @@ fn link_one(ctx: &Ctx, repo: &Repo, path: &Path, force: bool, report: &Report) -
));
}
rename(ctx, &src, &backup)?;
ctx.fs().rename(&src, &backup)?;
report.line("saved", &suffixed(&rel, BACKUP_SUFFIX));
place_link(ctx, &src, &target)?;
ctx.fs().place_link(&src, &target)?;
report.line("linked", &rel);
Ok(())
}
@@ -399,7 +397,7 @@ fn link_one(ctx: &Ctx, repo: &Repo, path: &Path, force: bool, report: &Report) -
if !force {
return Err(needs_force(&target));
}
place_link(ctx, &src, &target)?;
ctx.fs().place_link(&src, &target)?;
report.line("linked", &rel);
Ok(())
}
@@ -416,10 +414,10 @@ fn link_one(ctx: &Ctx, repo: &Repo, path: &Path, force: bool, report: &Report) -
// but no link placed, the store holds a path nothing points at and the working
// tree has lost it altogether, which is the one outcome worse than failing
fn move_and_link(ctx: &Ctx, src: &Path, target: &Path) -> Result<()> {
move_path(ctx, src, target)?;
ctx.fs().move_path(src, target)?;
if let Err(e) = place_link(ctx, src, target) {
move_path(ctx, target, src).with_context(|| {
if let Err(e) = ctx.fs().place_link(src, target) {
ctx.fs().move_path(target, src).with_context(|| {
format!(
"could not put {} back after failing to link it to {}",
src.display(),