use crate::error::{Result, ToriiError};
use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug, Default)]
pub struct ExportOpts {
pub output_dir: Option<PathBuf>,
pub stdout: bool,
pub cover_letter: bool,
}
pub fn export(repo_path: &Path, range: &str, opts: &ExportOpts) -> Result<()> {
let mut args = vec!["format-patch".to_string()];
if let Some(dir) = &opts.output_dir {
args.push("-o".to_string());
args.push(dir.to_string_lossy().to_string());
}
if opts.stdout {
args.push("--stdout".to_string());
}
if opts.cover_letter {
args.push("--cover-letter".to_string());
}
args.push(range.to_string());
println!("📨 patch export range={range}");
let status = Command::new("git")
.args(&args)
.current_dir(repo_path)
.status()
.map_err(|e| ToriiError::Subprocess { tool: "git".into(), message: format!("invoke git format-patch: {e}") })?;
if !status.success() {
return Err(ToriiError::Subprocess { tool: "git".into(), message: format!(
"git format-patch exited with {status}"
) });
}
Ok(())
}
#[derive(Debug, Default)]
pub struct ApplyOpts {
pub three_way: bool,
pub abort: bool,
pub continue_: bool,
pub skip: bool,
}
pub fn apply(repo_path: &Path, files: &[PathBuf], opts: &ApplyOpts) -> Result<()> {
let mut args = vec!["am".to_string()];
if opts.three_way {
args.push("--3way".to_string());
}
if opts.abort {
args.push("--abort".to_string());
} else if opts.continue_ {
args.push("--continue".to_string());
} else if opts.skip {
args.push("--skip".to_string());
} else {
if files.is_empty() {
return Err(ToriiError::Usage(
"patch apply needs at least one file, or --abort / --continue / --skip".into(),
));
}
for f in files {
args.push(f.to_string_lossy().to_string());
}
}
println!("📨 patch apply ({} arg(s))", args.len() - 1);
let status = Command::new("git")
.args(&args)
.current_dir(repo_path)
.status()
.map_err(|e| ToriiError::Subprocess { tool: "git".into(), message: format!("invoke git am: {e}") })?;
if !status.success() {
return Err(ToriiError::Subprocess { tool: "git".into(), message: format!(
"git am exited with {status} — resolve and run `torii patch apply --continue`"
) });
}
Ok(())
}