diff options
author | Teddy Wing | 2020-08-22 20:42:55 +0200 |
---|---|---|
committer | Teddy Wing | 2020-08-22 21:18:34 +0200 |
commit | 5cbc3972644a321442b9640cfcc50f7ef014a6c9 (patch) | |
tree | 42870a32fcf84345793956e62b118f0eaeca773a | |
parent | 95790ebf124823538a1cbeaf6a3b02a8b6ebbe4d (diff) | |
download | git-suggestion-5cbc3972644a321442b9640cfcc50f7ef014a6c9.tar.bz2 |
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 <BASE>:src/server.rs <TEMPORARY_HASH>
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.
-rw-r--r-- | github-suggestion/src/suggestion.rs | 41 | ||||
-rw-r--r-- | src/bin/git-sugdiff.rs | 48 |
2 files changed, 89 insertions, 0 deletions
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, diff --git a/src/bin/git-sugdiff.rs b/src/bin/git-sugdiff.rs new file mode 100644 index 0000000..c8ecf40 --- /dev/null +++ b/src/bin/git-sugdiff.rs @@ -0,0 +1,48 @@ +// Copyright (c) 2020 Teddy Wing +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + + +use std::env; +use std::process; + +use exitcode; + +use github_suggestion_cli::{gseprintln, for_suggestion}; +use github_suggestion_cli::config::Config; + + +fn main() { + let args: Vec<_> = env::args().collect(); + + let config = match Config::get( + &args, + "usage: git sugpatch [options] <suggestion>...", + ) { + Ok(c) => c, + Err(e) => { + gseprintln!(e); + + process::exit(exitcode::CONFIG); + }, + }; + + for_suggestion( + &config, + |suggestion| { + // TODO: Needs to work for multiple suggestions at once + suggestion.diff_command().unwrap(); + }, + ); +} |