pub trait GitDiff {
fn git_diff(repo: &git2::Repository) -> crate::error::ReviseResult<String> {
let mut opts = git2::DiffOptions::new(); let head = repo.head()?.peel_to_tree()?; let diff = repo.diff_tree_to_workdir(Some(&head), Some(&mut opts))?; let mut content = String::new(); diff.foreach(
&mut |_, _| true,
None,
None,
Some(&mut |_, _, line| {
let prefix = match line.origin() {
'+' => "+",
'-' => "-",
_ => " ",
};
content.push_str(prefix);
content.push_str(&String::from_utf8_lossy(line.content()));
content.push('\n');
true
}),
)?;
Ok(content)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_git_diff() {
struct GItDiffImpl;
impl GitDiff for GItDiffImpl {}
let _ = GItDiffImpl::git_diff(&git2::Repository::open(".").unwrap())
.unwrap();
}
}