use std::path::Path;
use gitpoulpi_core::domain::{PatchApplied, PatchCreated, PatchSource, RepoRef};
use gitpoulpi_core::{Error, Result};
use crate::process::{run, run_bytes, run_bytes_with_stdin};
fn check_dest(dest: &Path) -> Result<()> {
if dest.exists() {
return Err(Error::Backend(format!("{} already exists", dest.display())));
}
match dest.parent() {
Some(parent) if !parent.as_os_str().is_empty() && !parent.exists() => Err(Error::Backend(
format!("{} has no parent directory", parent.display()),
)),
_ => Ok(()),
}
}
fn short_hex(hex: &str) -> &str {
&hex[..hex.len().min(8)]
}
fn parent_count(repo: &RepoRef, oid: &gitpoulpi_core::domain::Oid) -> Result<usize> {
let raw = run(
&repo.root,
&["rev-list", "--parents", "-n", "1", &oid.to_hex()],
)
.map_err(|_| Error::Backend(format!("{} is not a known commit", oid.to_hex())))?;
Ok(raw.split_whitespace().count().saturating_sub(1))
}
pub fn patch_create(repo: &RepoRef, source: &PatchSource, dest: &Path) -> Result<PatchCreated> {
check_dest(dest)?;
let (bytes, commits): (Vec<u8>, usize) = match source {
PatchSource::Commits(oids) => {
if oids.is_empty() {
return Err(Error::Backend("no commits given".into()));
}
let mut buf = Vec::new();
for oid in oids {
if parent_count(repo, oid)? > 1 {
return Err(Error::Backend(format!(
"{} is a merge commit and cannot become a patch",
oid.to_hex()
)));
}
let one = run_bytes(
&repo.root,
&["format-patch", "-1", "--stdout", "--binary", &oid.to_hex()],
)?;
buf.extend_from_slice(&one);
}
(buf, oids.len())
}
PatchSource::Range { base, tip } => {
let range = format!("{}..{}", base.to_hex(), tip.to_hex());
let count_raw = run(&repo.root, &["rev-list", "--no-merges", "--count", &range])?;
let commits: usize = count_raw.trim().parse().unwrap_or(0);
if commits == 0 {
return Err(Error::Backend(format!(
"{range} has no non-merge commits to turn into a patch"
)));
}
let bytes = run_bytes(
&repo.root,
&[
"format-patch",
&range,
"--stdout",
"--binary",
"--no-merges",
],
)?;
(bytes, commits)
}
PatchSource::CommitPaths { commit, paths } => {
if paths.is_empty() {
return Err(Error::Backend("no paths given".into()));
}
if parent_count(repo, commit)? > 1 {
return Err(Error::Backend(format!(
"{} is a merge commit and cannot become a patch",
commit.to_hex()
)));
}
let hex = commit.to_hex();
let mut touched: Vec<&str> = vec![
"diff-tree",
"--no-commit-id",
"--name-only",
"-r",
&hex,
"--",
];
touched.extend(paths.iter().map(String::as_str));
if run(&repo.root, &touched)?.trim().is_empty() {
return Err(Error::Backend(format!(
"{} did not change the given paths",
short_hex(&hex)
)));
}
let mut args: Vec<&str> =
vec!["format-patch", "-1", "--stdout", "--binary", &hex, "--"];
args.extend(paths.iter().map(String::as_str));
(run_bytes(&repo.root, &args)?, 1)
}
PatchSource::WorkingTree { paths } => {
run(&repo.root, &["rev-parse", "--verify", "HEAD"])
.map_err(|_| Error::Backend("HEAD has no commits yet".into()))?;
let mut args: Vec<&str> = vec!["diff", "HEAD", "--binary"];
if !paths.is_empty() {
args.push("--");
args.extend(paths.iter().map(String::as_str));
}
let bytes = run_bytes(&repo.root, &args)?;
(bytes, 0)
}
};
if bytes.is_empty() {
return Err(Error::Backend("the patch would be empty".into()));
}
std::fs::write(dest, &bytes)
.map_err(|e| Error::Backend(format!("failed to write {}: {e}", dest.display())))?;
Ok(PatchCreated {
path: dest.to_path_buf(),
commits,
bytes: bytes.len() as u64,
})
}
fn parse_numstat_z(raw: &str) -> Vec<String> {
raw.split('\0')
.filter(|record| !record.is_empty())
.filter_map(|record| record.rsplit('\t').next())
.map(str::to_string)
.collect()
}
pub fn patch_apply(repo: &RepoRef, patch: &Path) -> Result<PatchApplied> {
if !patch.is_file() {
return Err(Error::Backend(format!("{} is not a file", patch.display())));
}
let patch_arg = patch.to_string_lossy().to_string();
let numstat = run(
&repo.root,
&["apply", "--check", "--numstat", "-z", &patch_arg],
)?;
let files = parse_numstat_z(&numstat);
if files.is_empty() {
return Err(Error::Backend("the patch applies no changes".into()));
}
run(&repo.root, &["apply", "--whitespace=nowarn", &patch_arg])?;
Ok(PatchApplied { files })
}
pub fn apply_commit_to_worktree(
repo: &RepoRef,
commit: gitpoulpi_core::domain::Oid,
) -> Result<PatchApplied> {
if parent_count(repo, &commit)? > 1 {
return Err(Error::ApplyRefused(
gitpoulpi_core::domain::ApplyRefusal::MergeCommit,
));
}
let diff = run_bytes(
&repo.root,
&[
"diff-tree",
"-p",
"--root",
"--binary",
"--no-renames",
"--no-ext-diff",
"--no-textconv",
"--no-color",
"--no-commit-id",
"--src-prefix=a/",
"--dst-prefix=b/",
"-r",
&commit.to_hex(),
],
)?;
if diff.is_empty() {
return Err(Error::Backend(format!(
"{} changes nothing to apply",
short_hex(&commit.to_hex())
)));
}
let numstat = run_bytes_with_stdin(
&repo.root,
&["apply", "--check", "--numstat", "-z", "-"],
&diff,
)?;
let files = parse_numstat_z(&String::from_utf8_lossy(&numstat));
if files.is_empty() {
return Err(Error::Backend("the commit applies no changes".into()));
}
run_bytes_with_stdin(&repo.root, &["apply", "--whitespace=nowarn", "-"], &diff)?;
Ok(PatchApplied { files })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_numstat_z_records_into_paths() {
let raw = "3\t1\tsrc/a.rs\0-\t-\tassets/logo.png\0";
assert_eq!(
parse_numstat_z(raw),
vec!["src/a.rs".to_string(), "assets/logo.png".to_string()]
);
}
#[test]
fn empty_numstat_is_no_files() {
assert!(parse_numstat_z("").is_empty());
}
}