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.print(git2::DiffFormat::Patch, |_, _, l| {
let diff = std::str::from_utf8(l.content()).unwrap();
content += diff;
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();
}
}