use anyhow::{bail, Result};
use std::path::Path;
pub fn execute(path: &Path, source: &str, destination: &str) -> Result<()> {
let repo = crate::ops::open_repo(path)?;
let src_full = path.join(source);
let dst_full = path.join(destination);
if !src_full.exists() {
bail!("bad source, source={}, destination={}", source, destination);
}
if dst_full.exists() {
bail!(
"destination exists, source={}, destination={}",
source,
destination
);
}
if let Some(parent) = dst_full.parent() {
if !parent.exists() {
std::fs::create_dir_all(parent)?;
}
}
std::fs::rename(&src_full, &dst_full)?;
let mut index = repo.index()?;
index.remove_path(Path::new(source))?;
index.add_path(Path::new(destination))?;
index.write()?;
println!("Renamed {} -> {}", source, destination);
Ok(())
}