From 5cbc3972644a321442b9640cfcc50f7ef014a6c9 Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Sat, 22 Aug 2020 20:42:55 +0200
Subject: Execute `git diff` to output diffs
I want to enable colour diff output. At first, I thought about doing
this by getting the diff colours from the Git config, then outputting
the ANSI escape codes manually with the text.
However, I then thought it would be nice to enable other diff features
like `--word-diff`, and it would be too much trouble to re-implement
that manually, and then I'd have to take word diff colouring into
account.
Decided that the optimal way to get both colours and extra diff
features like word diffing would be to use `git diff` directly under the
hood.
We shell out to `git diff`, and give it the original file as a base, and
a new blob object that we create in the repository on the right side.
This allows us to compare the file using a command in the following
format:
$ git diff :src/server.rs
Ostensibly, the temporary blob object I create for the right side
version should be freed automatically by `git2`.
Add a new `sugdiff` command to output diffs. This will replace
`sugpatch`. Since we're working with diffs, I think a name with "diff"
sounds more logical. The previous version, "patch" sounds too much like
what "apply" does, because of the Unix `patch` command.
---
github-suggestion/src/suggestion.rs | 41 +++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
(limited to 'github-suggestion')
diff --git a/github-suggestion/src/suggestion.rs b/github-suggestion/src/suggestion.rs
index 367757a..3f124ce 100644
--- a/github-suggestion/src/suggestion.rs
+++ b/github-suggestion/src/suggestion.rs
@@ -16,6 +16,7 @@
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::Path;
+use std::process::Command;
use git2::{Patch, Repository};
use regex::Regex;
@@ -46,6 +47,9 @@ pub enum Error {
#[error("{0} is not valid UTF-8")]
InvalidUtf8(String),
+ #[error("unable to convert from UTF-8: {0}")]
+ FromUtf8(#[from] std::str::Utf8Error),
+
#[error("Regex error: {0}")]
Regex(#[from] regex::Error),
}
@@ -123,6 +127,43 @@ impl Suggestion {
)
}
+ pub fn diff_command(&self) -> Result<(), Error> {
+ let repo = Repository::open(".")?;
+ let commit = repo.find_commit(self.commit.parse()?)?;
+
+ let path = Path::new(&self.path);
+
+ let object = commit
+ .tree()?
+ .get_path(path)?
+ .to_object(&repo)?;
+
+ let blob = object.as_blob()
+ .ok_or_else(|| Error::GitObjectNotBlob(object.id()))?;
+
+ let blob_reader = BufReader::new(blob.content());
+ let mut new = BufWriter::new(Vec::new());
+ self.apply_to(blob_reader, &mut new)?;
+ let new_buffer = new.into_inner()
+ .map_err(|e| Error::BufWriter {
+ source: e,
+ message: "unable to read right side of patch".to_owned(),
+ })?;
+
+ let patched_blob = repo.blob(&new_buffer)?;
+
+ Command::new("git")
+ .arg("diff")
+ .arg(format!("{}:{}", commit.id(), self.path))
+ .arg(patched_blob.to_string())
+ .spawn()
+ .unwrap();
+
+ // Maybe: Return blob
+
+ Ok(())
+ }
+
/// Extract suggestion code from a comment body.
fn suggestion_with_line_ending(
&self,
--
cgit v1.2.3