use std::{ io::Write, path::PathBuf, process::{Command, Stdio}, }; use anyhow::{Context, Result, bail}; use clap::Parser; use crate::parse::{FileDiff, Patch}; mod parse; #[derive(Parser)] struct Cli { /// The file to pull the patch to rollup from. patch_files: Vec, /// Which repository to use as a target for the rollup /// /// Needs to be clean. #[arg(short, long)] to: PathBuf, #[arg(short, long)] debug: bool, } macro_rules! git { (@_base for $finalizer:ident in $to:expr; $($args:expr),*) => { Command::new("git") .args([ "-C", $to.display().to_string().as_str(), $($args),* ]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .$finalizer() .with_context(|| format!("Failed to run {}", git!(@_fmt_cmd $($args),*)))? }; (@_handle_status $output:expr; $($args:expr),*) => {{ if !$output.status.success() { bail!("{} failed!", git!(@_fmt_cmd $($args),*)) } if !$output.stderr.is_empty() { let stderr = String::from_utf8($output.stderr).expect("git should always produce utf8"); bail!("{} returned some errors:\n{stderr}", git!(@_fmt_cmd $($args),*)); } }}; (@_fmt_cmd $($args:expr),*) => {{ let all = ["git", $($args),*].join(" "); format!("`{}`", all) }}; (in $to:expr; $($args:expr),*) => {{ let output = git!(@_base for output in $to; $($args),*); git!(@_handle_status output; $($args),*); let stdout = String::from_utf8(output.stdout).expect("git should always produce utf8"); let stderr = String::from_utf8(output.stderr).expect("git should always produce utf8"); (stdout, stderr) }}; (@set_stdin_to $stdin:expr; in $to:expr; $($args:expr),*) => {{ let mut child = git!(@_base for spawn in $to; $($args),*); let mut stdin = child.stdin.take().expect("is piped"); write!(stdin, "{}", $stdin)?; drop(stdin); let output = child.wait_with_output() .with_context(|| format!("Failed to run `{}`", git!(@_fmt_cmd $($args),*)))?; git!(@_handle_status output; $($args),*); let stdout = String::from_utf8(output.stdout).expect("git should always produce utf8"); let stderr = String::from_utf8(output.stderr).expect("git should always produce utf8"); (stdout, stderr) }}; } fn main() -> Result<()> { let args = Cli::parse(); let mut patches = vec![]; for file in &args.patch_files { patches.push(parse::parse_file(file, args.debug)?); } eprintln!("Successfully parsed all {} patch files.", patches.len()); { let (stdout, _) = git!(in args.to; "status", "--porcelain"); if !stdout.is_empty() { bail!( "`--to` (`{}`) workspace is dirty, refusing to continue.", args.to.display() ); } } for (idx, patch) in patches.iter().enumerate() { eprintln!( "Applying {}. patch: `{}`... ", idx + 1, args.patch_files[idx].display() ); let mut final_diffs = vec![]; for file_diff in &patch.files { match file_diff { FileDiff::Add { path, .. } => { if args.to.join(path).exists() { bail!("A file was added, that already existed. This should be impossible.") } final_diffs.push(file_diff.to_owned()); // eprintln!(" > Keeping add of `{}`", path.display()); } FileDiff::Remove { path, .. } => { if args.to.join(path).exists() { final_diffs.push(file_diff.to_owned()); // eprintln!(" > Keeping remove of `{}`", path.display()); } else { eprintln!( " > Dropping remove of `{}`, as the file does not exist in the rollup target.", path.display() ); } } FileDiff::RenameNoChange { from, to, .. } | FileDiff::RenameWithChange { from, to, .. } => { if args.to.join(from).exists() { if args.to.join(to).exists() { bail!( "A rename (`{}` -> `{}`) tried to rename a path to an already existing path. This should be impossible.", from.display(), to.display() ) } final_diffs.push(file_diff.to_owned()); // eprintln!(" > Keeping rename of `{}` to `{}`", from.display(), to.display()); } else { eprintln!( " > Dropping rename from `{}` to `{}`, as the from file does not exist in the rollup target.", from.display(), to.display() ); } } FileDiff::Change { path, .. } => { if args.to.join(path).exists() { final_diffs.push(file_diff.to_owned()); } else { eprintln!( " > Dropping change to `{}`, as the file does not exist in the rollup target.", path.display() ); } } } } if final_diffs.is_empty() { eprintln!(" -> Patch is empty after dropping, skipping it.",); continue; } let filtered_patch = Patch { files: final_diffs, ..patch.to_owned() }; git!(@set_stdin_to filtered_patch; in args.to; "apply", "-", "--whitespace=nowarn"); git!(in args.to; "add", "."); git!(@set_stdin_to filtered_patch.commit_message; in args.to; "commit", "--file=-", format!("--date={}", filtered_patch.date).as_str(), "--quiet"); } Ok(()) }