use std::{path::Path, time::Duration};
use similar::TextDiff;
#[derive(Clone, Copy, Debug)]
pub(super) enum FileContents<'a> {
Created(&'a str),
Modified { old: &'a str, new: &'a str },
Deleted(&'a str),
}
const PATCH_DIFF_DEADLINE: Duration = Duration::from_millis(250);
pub(super) fn file_patch(path: &Path, contents: FileContents<'_>) -> Option<String> {
let path = plain_absolute(path)?;
let (mode_line, from, to, old, new) = match contents {
FileContents::Created(new) => ("new file mode 100644\n", "/dev/null", path, "", new),
FileContents::Modified { old, new } => ("", path, path, old, new),
FileContents::Deleted(old) => ("deleted file mode 100644\n", path, "/dev/null", old, ""),
};
let hunks = TextDiff::configure()
.timeout(PATCH_DIFF_DEADLINE)
.diff_lines(old, new)
.unified_diff()
.header(from, to)
.to_string();
if hunks.is_empty() && mode_line.is_empty() {
return None;
}
Some(format!("diff --git {path} {path}\n{mode_line}{hunks}"))
}
fn plain_absolute(path: &Path) -> Option<&str> {
let text = path.to_str()?;
let needs_quoting = text
.chars()
.any(|c| c == '"' || c == '\\' || c.is_control() || !c.is_ascii());
(path.is_absolute() && !needs_quoting).then_some(text)
}
#[cfg(test)]
#[path = "git_patch_tests.rs"]
mod tests;