use crate::cli::global::GlobalFlags;
use crate::cmd::output::WritePhase;
use crate::cmd::output::execute_via_engine;
use crate::cmd::write_dispatch::{WriteMessages, execute_write};
use crate::exit;
use crate::plan::Operation;
use anyhow::Context;
use clap::Args;
use serde::Serialize;
use std::fs;
#[derive(Debug, Args)]
#[command(after_help = "\
EXAMPLES:
patchloom rename old_config.json config.json --apply
patchloom rename src/utils.rs src/helpers.rs --apply --force")]
pub struct RenameArgs {
pub from: String,
pub to: String,
#[arg(long)]
pub force: bool,
#[command(flatten)]
pub write: crate::cli::global::WriteFlags,
}
#[derive(Debug, Serialize)]
struct RenameOutput {
ok: bool,
from: String,
#[serde(skip_serializing_if = "Option::is_none")]
to: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
diff: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
applied: Option<bool>,
}
pub fn run(args: RenameArgs, global: &GlobalFlags) -> anyhow::Result<u8> {
crate::verbose!(
"rename: from={}, to={}, force={}",
args.from,
args.to,
args.force
);
let cwd = global.resolve_cwd()?;
global.check_paths_contained(&cwd, [args.from.as_str(), args.to.as_str()])?;
let src = cwd.join(&args.from);
let dst = cwd.join(&args.to);
if !src.exists() {
anyhow::bail!("source file not found: {}", args.from);
}
if !src.is_file() {
anyhow::bail!("source is not a file: {}", args.from);
}
if dst.exists() && !dst.is_file() {
anyhow::bail!("destination is not a file: {}", args.to);
}
let is_case_only_change = src != dst
&& src.parent() == dst.parent()
&& src.file_name().map(|n| n.to_ascii_lowercase())
== dst.file_name().map(|n| n.to_ascii_lowercase());
if !is_case_only_change
&& (src == dst
|| matches!(
(src.canonicalize(), dst.canonicalize()),
(Ok(ref s), Ok(ref d)) if s == d
))
{
let output = RenameOutput {
ok: true,
from: args.from.clone(),
to: Some(args.to.clone()),
diff: None,
applied: None,
};
if !global.emit_json(&output)? && !global.quiet {
println!("source and destination are the same: {}", args.from);
}
return Ok(exit::SUCCESS);
}
if !args.force && !is_case_only_change && dst.exists() {
anyhow::bail!("destination already exists: {}", args.to);
}
let is_binary = {
use std::io::Read;
let mut file = fs::File::open(&src)?;
let mut buf = [0u8; 8192];
let n = file.read(&mut buf)?;
crate::files::is_binary(&buf[..n])
};
if is_binary {
return run_binary_rename(&args, global, &cwd, &src, &dst);
}
if is_case_only_change {
return run_binary_rename(&args, global, &cwd, &src, &dst);
}
let op = Operation::FileRename {
from: args.from.clone(),
to: args.to.clone(),
force: args.force,
};
let check_msg = format!("would rename {} -> {}", args.from, args.to);
let apply_msg = format!("renamed {} -> {}", args.from, args.to);
execute_via_engine(
op,
global,
|phase, diff| RenameOutput {
ok: true,
from: args.from.clone(),
to: Some(args.to.clone()),
diff,
applied: match phase {
WritePhase::Confirmed(a) => Some(a),
_ => None,
},
},
&check_msg,
&apply_msg,
)
}
fn run_binary_rename(
args: &RenameArgs,
global: &GlobalFlags,
cwd: &std::path::Path,
src: &std::path::Path,
dst: &std::path::Path,
) -> anyhow::Result<u8> {
if global.trim_trailing_whitespace
|| global.ensure_final_newline
|| global.normalize_eol.is_some()
{
anyhow::bail!(
"cannot apply write policy to binary file: {}",
src.display()
);
}
let check_msg = format!("would rename {} -> {} (binary)", args.from, args.to);
let apply_msg = format!("renamed {} -> {} (binary)", args.from, args.to);
let post_msg = format!("renamed {} -> {}", args.from, args.to);
execute_write(
global,
cwd,
|phase, _diff| RenameOutput {
ok: true,
from: args.from.clone(),
to: Some(args.to.clone()),
diff: None,
applied: match phase {
WritePhase::Confirmed(a) => Some(a),
_ => None,
},
},
None::<&dyn Fn(bool) -> String>,
|| {
let mut backup = crate::backup::BackupSession::new(cwd)?;
backup.save_before_delete(src)?;
backup.save_before_write(dst)?;
if let Some(parent) = dst.parent()
&& !parent.as_os_str().is_empty()
&& !parent.exists()
{
fs::create_dir_all(parent)?;
}
backup.finalize()?;
rename_or_copy(src, dst)?;
Ok(())
},
WriteMessages {
check: &check_msg,
apply: &apply_msg,
post_confirm: Some(&post_msg),
},
)
}
fn rename_or_copy(src: &std::path::Path, dst: &std::path::Path) -> anyhow::Result<()> {
match fs::rename(src, dst) {
Ok(()) => Ok(()),
Err(e) if is_cross_device(&e) => {
fs::copy(src, dst).with_context(|| {
format!("cross-device copy {} -> {}", src.display(), dst.display())
})?;
fs::remove_file(src).with_context(|| {
format!("removing source after cross-device copy: {}", src.display())
})?;
Ok(())
}
Err(e) => Err(e.into()),
}
}
fn is_cross_device(e: &std::io::Error) -> bool {
#[cfg(unix)]
{
e.raw_os_error() == Some(libc::EXDEV)
}
#[cfg(windows)]
{
e.raw_os_error() == Some(17)
}
#[cfg(not(any(unix, windows)))]
{
let _ = e;
false
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn rename_moves_file() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("old.txt");
let dst = dir.path().join("new.txt");
fs::write(&src, "hello\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
assert!(!src.exists());
assert!(dst.exists());
assert_eq!(fs::read_to_string(&dst).unwrap(), "hello\n");
}
#[test]
fn rename_fails_if_dst_exists() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("old.txt");
let dst = dir.path().join("new.txt");
fs::write(&src, "hello\n").unwrap();
fs::write(&dst, "existing\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let err = run(args, &global).unwrap_err();
assert!(err.to_string().contains("already exists"));
}
#[test]
fn rename_force_rejects_directory_destination() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("old.txt");
let dst = dir.path().join("folder");
fs::write(&src, "hello\n").unwrap();
fs::create_dir(&dst).unwrap();
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: true,
write: Default::default(),
};
let err = run(args, &GlobalFlags::test_with_cwd(dir.path())).unwrap_err();
assert!(err.to_string().contains("destination is not a file"));
}
#[test]
fn rename_force_overwrites_dst() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("old.txt");
let dst = dir.path().join("new.txt");
fs::write(&src, "new content\n").unwrap();
fs::write(&dst, "old content\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: true,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
assert!(!src.exists());
assert_eq!(fs::read_to_string(&dst).unwrap(), "new content\n");
}
#[test]
fn rename_check_reports_changes() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("old.txt");
let dst = dir.path().join("new.txt");
fs::write(&src, "hello\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.check = true;
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::CHANGES_DETECTED);
assert!(src.exists());
assert_eq!(
fs::read_to_string(&src).unwrap(),
"hello\n",
"--check must not modify source content"
);
assert!(!dst.exists(), "--check must not create destination file");
}
#[test]
fn rename_fails_if_src_missing() {
let dir = TempDir::new().unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: dir.path().join("nope.txt").to_string_lossy().into_owned(),
to: dir.path().join("dst.txt").to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let err = run(args, &global).unwrap_err();
assert!(err.to_string().contains("not found"));
}
#[test]
fn rename_same_path_is_noop() {
let dir = TempDir::new().unwrap();
let file = dir.path().join("same.txt");
fs::write(&file, "hello\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: file.to_string_lossy().into_owned(),
to: file.to_string_lossy().into_owned(),
force: true,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
assert!(file.exists(), "file should still exist");
assert_eq!(fs::read_to_string(&file).unwrap(), "hello\n");
}
#[test]
fn rename_same_file_via_different_path_is_noop() {
let dir = TempDir::new().unwrap();
let file = dir.path().join("same.txt");
fs::write(&file, "hello\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: file.to_string_lossy().into_owned(),
to: dir.path().join("./same.txt").to_string_lossy().into_owned(),
force: true,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
assert!(file.exists(), "file must not be deleted");
assert_eq!(fs::read_to_string(&file).unwrap(), "hello\n");
}
#[test]
fn rename_binary_file() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("image.bin");
let dst = dir.path().join("moved.bin");
fs::write(&src, b"\x00\x01\x02\xff\xfe").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
assert!(!src.exists());
assert_eq!(fs::read(&dst).unwrap(), b"\x00\x01\x02\xff\xfe");
}
#[test]
fn rename_with_contain_rejects_parent_escape_on_to() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("inside.txt"), "keep\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
global.contain = true;
let args = RenameArgs {
from: "inside.txt".into(),
to: "../escape-renamed.txt".into(),
force: false,
write: Default::default(),
};
let err = run(args, &global).unwrap_err();
let msg = err.to_string();
assert!(
msg.contains("escapes") || msg.contains("rejected"),
"expected containment error, got: {msg}"
);
assert!(
dir.path().join("inside.txt").exists(),
"source must remain when containment rejects destination"
);
assert!(!dir.path().join("../escape-renamed.txt").exists());
}
#[test]
fn rename_with_contain_rejects_parent_escape_on_from() {
let dir = TempDir::new().unwrap();
let outside = dir.path().parent().unwrap().join(format!(
"patchloom-rename-from-escape-{}.txt",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis()
));
fs::write(&outside, "outside\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
global.contain = true;
let args = RenameArgs {
from: format!("../{}", outside.file_name().unwrap().to_string_lossy()),
to: "pulled-in.txt".into(),
force: false,
write: Default::default(),
};
let err = run(args, &global).unwrap_err();
let msg = err.to_string();
assert!(
msg.contains("escapes") || msg.contains("rejected"),
"expected containment error, got: {msg}"
);
assert!(outside.exists(), "outside source must not be moved");
assert!(!dir.path().join("pulled-in.txt").exists());
let _ = fs::remove_file(&outside);
}
#[test]
fn rename_binary_with_contain_rejects_parent_escape() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("image.bin"), b"\x00\x01\x02\xff\xfe").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
global.contain = true;
let args = RenameArgs {
from: "image.bin".into(),
to: "../escaped.bin".into(),
force: false,
write: Default::default(),
};
let err = run(args, &global).unwrap_err();
let msg = err.to_string();
assert!(
msg.contains("escapes") || msg.contains("rejected"),
"expected containment error on binary rename escape, got: {msg}"
);
assert!(dir.path().join("image.bin").exists());
assert!(!dir.path().join("../escaped.bin").exists());
}
#[test]
fn rename_with_contain_allows_in_workspace_relative_path() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("old.txt"), "hello\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
global.contain = true;
let args = RenameArgs {
from: "old.txt".into(),
to: "new.txt".into(),
force: false,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
assert!(!dir.path().join("old.txt").exists());
assert_eq!(
fs::read_to_string(dir.path().join("new.txt")).unwrap(),
"hello\n"
);
}
#[test]
fn rename_without_contain_allows_parent_escape() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("inside.txt"), "move me\n").unwrap();
let name = format!(
"patchloom-rename-escape-{}.txt",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis()
);
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: "inside.txt".into(),
to: format!("../{name}"),
force: false,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
let outside = dir.path().parent().unwrap().join(&name);
assert_eq!(fs::read_to_string(&outside).unwrap(), "move me\n");
let _ = fs::remove_file(&outside);
}
#[test]
fn rename_creates_parent_dirs() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("old.txt");
let dst = dir.path().join("sub").join("dir").join("new.txt");
fs::write(&src, "hello\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
assert!(!src.exists());
assert_eq!(fs::read_to_string(&dst).unwrap(), "hello\n");
}
#[test]
fn rename_fails_if_src_is_directory() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("folder");
let dst = dir.path().join("new.txt");
fs::create_dir(&src).unwrap();
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let err = run(args, &GlobalFlags::test_with_cwd(dir.path())).unwrap_err();
assert!(err.to_string().contains("source is not a file"));
}
#[test]
fn rename_creates_backup_before_mutation() {
let dir = TempDir::new().unwrap();
let src = dir.path().join("original.txt");
let dst = dir.path().join("moved.txt");
fs::write(&src, "backup me\n").unwrap();
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let code = run(args, &global).unwrap();
assert_eq!(code, exit::SUCCESS);
assert!(!src.exists(), "source should be gone after rename");
assert_eq!(fs::read_to_string(&dst).unwrap(), "backup me\n");
let backup_dir = dir.path().join(".patchloom/backups");
assert!(
backup_dir.exists(),
"backup directory should exist after rename --apply"
);
let sessions: Vec<_> = fs::read_dir(&backup_dir)
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| e.path().is_dir())
.collect();
assert!(
!sessions.is_empty(),
"at least one backup session should be created"
);
}
#[cfg(unix)]
#[test]
fn rename_unreadable_file_propagates_io_error() {
use std::os::unix::fs::PermissionsExt;
let dir = TempDir::new().unwrap();
let src = dir.path().join("unreadable.txt");
let dst = dir.path().join("dest.txt");
fs::write(&src, "hello\n").unwrap();
fs::set_permissions(&src, fs::Permissions::from_mode(0o000)).unwrap();
if fs::read_to_string(&src).is_ok() {
fs::set_permissions(&src, fs::Permissions::from_mode(0o644)).unwrap();
return;
}
let mut global = GlobalFlags::test_with_cwd(dir.path());
global.apply = true;
let args = RenameArgs {
from: src.to_string_lossy().into_owned(),
to: dst.to_string_lossy().into_owned(),
force: false,
write: Default::default(),
};
let result = run(args, &global);
assert!(result.is_err(), "expected error, got Ok: {result:?}");
let err_msg = result.unwrap_err().to_string();
assert!(
!err_msg.contains("binary"),
"should not misclassify permission error as binary: {err_msg}"
);
fs::set_permissions(&src, fs::Permissions::from_mode(0o644)).unwrap();
}
}