use std::path::PathBuf;
use gitpoulpi_core::domain::{
CommitStat, DiffFileList, DiffTarget, FileDiff, FileStatus, Oid, RepoRef,
};
use gitpoulpi_core::Result;
use crate::process::{run, run_bytes};
fn run_text(root: &std::path::Path, args: &[&str]) -> Result<String> {
let bytes = run_bytes(root, args)?;
gitpoulpi_core::domain::decode_text(&bytes)
.map(std::borrow::Cow::into_owned)
.ok_or_else(|| gitpoulpi_core::Error::Backend("git output was not utf-8".to_string()))
}
const EMPTY_TREE: &str = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
pub fn diff(repo: &RepoRef, target: DiffTarget) -> Result<Vec<FileDiff>> {
diff_inner(repo, target, None)
}
pub fn diff_paths(repo: &RepoRef, target: DiffTarget, paths: &[PathBuf]) -> Result<Vec<FileDiff>> {
if paths.is_empty() {
return Ok(Vec::new());
}
diff_inner(repo, target, Some(paths))
}
fn pathspec_tail(paths: Option<&[PathBuf]>) -> Vec<String> {
let Some(paths) = paths else {
return Vec::new();
};
let mut tail = vec!["--".to_string()];
tail.extend(
paths
.iter()
.map(|p| format!(":(literal){}", p.to_string_lossy().replace('\\', "/"))),
);
tail
}
fn diff_inner(
repo: &RepoRef,
target: DiffTarget,
paths: Option<&[PathBuf]>,
) -> Result<Vec<FileDiff>> {
let tail = pathspec_tail(paths);
let args: Vec<String> = match target {
DiffTarget::WorkingTreeVsIndex => vec![
"diff".into(),
"--no-color".into(),
"--no-ext-diff".into(),
"--no-textconv".into(),
"-M".into(),
"-U3".into(),
],
DiffTarget::IndexVsHead => {
vec![
"diff".into(),
"--no-color".into(),
"--no-ext-diff".into(),
"--no-textconv".into(),
"-M".into(),
"-U3".into(),
"--cached".into(),
]
}
DiffTarget::CommitVsParent(oid) => {
let hex = oid.to_hex();
let parent = format!("{hex}^");
let first = with_tail(commit_diff_args(&parent, &hex), &tail);
let first: Vec<&str> = first.iter().map(String::as_str).collect();
match run_text(&repo.root, &first) {
Ok(raw) => return Ok(gitpoulpi_core::diff_text::parse_unified(&raw)),
Err(_) => with_tail(commit_diff_args(EMPTY_TREE, &hex), &tail),
}
}
DiffTarget::CommitRange(from, to) => {
compare_args(&[from.to_hex().as_str(), to.to_hex().as_str()])
}
DiffTarget::CommitVsWorkingTree(oid) => compare_args(&[oid.to_hex().as_str()]),
DiffTarget::CommitSpan(oldest, newest) => {
let (oldest, newest) = (oldest.to_hex(), newest.to_hex());
let parent = format!("{oldest}^");
let first = with_tail(compare_args(&[parent.as_str(), newest.as_str()]), &tail);
let first: Vec<&str> = first.iter().map(String::as_str).collect();
match run_text(&repo.root, &first) {
Ok(raw) => return Ok(gitpoulpi_core::diff_text::parse_unified(&raw)),
Err(_) => compare_args(&[EMPTY_TREE, newest.as_str()]),
}
}
};
let mut args = args;
if !matches!(target, DiffTarget::CommitVsParent(_)) {
args.extend(tail);
}
let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
let raw = run_text(&repo.root, &args_ref)?;
Ok(gitpoulpi_core::diff_text::parse_unified(&raw))
}
fn commit_diff_args<'a>(old: &'a str, new: &'a str) -> [&'a str; 8] {
[
"diff",
"--no-color",
"--no-ext-diff",
"--no-textconv",
"-M",
"-U3",
old,
new,
]
}
fn with_tail(args: impl IntoIterator<Item = impl ToString>, tail: &[String]) -> Vec<String> {
let mut all: Vec<String> = args.into_iter().map(|a| a.to_string()).collect();
all.extend(tail.iter().cloned());
all
}
fn compare_args(revs: &[&str]) -> Vec<String> {
let mut args: Vec<String> = [
"diff",
"--no-color",
"--no-ext-diff",
"--no-textconv",
"-M",
"-U3",
]
.iter()
.map(|a| a.to_string())
.collect();
args.extend(revs.iter().map(|r| r.to_string()));
args
}
pub fn commit_stats(repo: &RepoRef, ids: &[Oid]) -> Result<Vec<CommitStat>> {
if ids.is_empty() {
return Ok(Vec::new());
}
let hexes: Vec<String> = ids.iter().map(|o| o.to_hex()).collect();
let mut args: Vec<&str> = vec![
"log",
"--no-walk=unsorted",
"--no-color",
"--no-ext-diff",
"--no-textconv",
"--no-color",
"--root",
"--diff-merges=first-parent",
"--numstat",
"--format=%x01%H",
];
args.extend(hexes.iter().map(String::as_str));
let raw = run(&repo.root, &args)?;
let mut by_id: std::collections::HashMap<String, (u32, u32, u32)> =
std::collections::HashMap::new();
for chunk in raw.split('\u{1}').skip(1) {
let mut lines = chunk.lines();
let Some(hash) = lines.next() else { continue };
let (mut files, mut ins, mut del) = (0, 0, 0);
for line in lines {
let mut cols = line.splitn(3, '\t');
let (Some(a), Some(d), Some(_)) = (cols.next(), cols.next(), cols.next()) else {
continue;
};
files += 1;
ins += a.parse::<u32>().unwrap_or(0);
del += d.parse::<u32>().unwrap_or(0);
}
by_id.insert(hash.trim().to_string(), (files, ins, del));
}
Ok(ids
.iter()
.zip(&hexes)
.map(|(&id, hex)| {
let (files, insertions, deletions) = by_id.get(hex).copied().unwrap_or_default();
CommitStat {
id,
files,
insertions,
deletions,
}
})
.collect())
}
pub fn diff_files(repo: &RepoRef, target: DiffTarget) -> Result<DiffFileList> {
let base = [
"diff",
"--no-color",
"--no-ext-diff",
"--no-textconv",
"--name-status",
"-M",
"-z",
];
let with = |extra: &[&str]| -> Vec<String> {
base.iter()
.chain(extra.iter())
.map(|a| a.to_string())
.collect()
};
let run_owned = |args: Vec<String>| {
let refs: Vec<&str> = args.iter().map(String::as_str).collect();
run(&repo.root, &refs)
};
let raw = match target {
DiffTarget::WorkingTreeVsIndex => run_owned(with(&[]))?,
DiffTarget::IndexVsHead => run_owned(with(&["--cached"]))?,
DiffTarget::CommitVsParent(oid) => {
let hex = oid.to_hex();
match run_owned(with(&[&format!("{hex}^"), &hex])) {
Ok(raw) => raw,
Err(_) => run_owned(with(&[EMPTY_TREE, &hex]))?,
}
}
DiffTarget::CommitRange(from, to) => run_owned(with(&[&from.to_hex(), &to.to_hex()]))?,
DiffTarget::CommitVsWorkingTree(oid) => run_owned(with(&[&oid.to_hex()]))?,
DiffTarget::CommitSpan(oldest, newest) => {
let (oldest, newest) = (oldest.to_hex(), newest.to_hex());
match run_owned(with(&[&format!("{oldest}^"), &newest])) {
Ok(raw) => raw,
Err(_) => run_owned(with(&[EMPTY_TREE, &newest]))?,
}
}
};
Ok(DiffFileList {
files: parse_name_status(&raw),
hunks_included: false,
})
}
fn parse_name_status(raw: &str) -> Vec<FileDiff> {
let mut files = Vec::new();
let mut fields = raw.split('\0');
while let Some(code) = fields.next() {
if code.is_empty() {
continue;
}
let renamed = code.starts_with('R') || code.starts_with('C');
let status = match code.chars().next() {
Some('A') => FileStatus::Added,
Some('D') => FileStatus::Deleted,
Some('R') | Some('C') => FileStatus::Renamed,
_ => FileStatus::Modified,
};
let mut path = fields.next().unwrap_or_default();
let mut from = None;
if renamed {
from = Some(PathBuf::from(path));
path = fields.next().unwrap_or(path);
}
files.push(FileDiff {
renamed_from: from,
similarity: if renamed {
code[1..].parse().ok()
} else {
None
},
path: PathBuf::from(path),
status,
hunks: Vec::new(),
});
}
files
}