Skip to content

Commit

Permalink
Do not handle --word-diff or --color-words output
Browse files Browse the repository at this point in the history
Fixes #440
Ref #152
  • Loading branch information
dandavison committed Nov 29, 2021
1 parent 0f66f75 commit b0e0283
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
20 changes: 19 additions & 1 deletion src/handlers/hunk.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
use lazy_static::lazy_static;

use crate::cli;
use crate::delta::{State, StateMachine};
use crate::style;
use crate::utils::process::{self, CallingProcess};
use unicode_segmentation::UnicodeSegmentation;

lazy_static! {
static ref IS_WORD_DIFF: bool = match process::calling_process().as_deref() {
Some(
CallingProcess::GitDiff(cmd_line)
| CallingProcess::GitShow(cmd_line, _)
| CallingProcess::GitLog(cmd_line)
| CallingProcess::GitReflog(cmd_line),
) =>
cmd_line.long_options.contains("--word-diff")
|| cmd_line.long_options.contains("--word-diff-regex")
|| cmd_line.long_options.contains("--color-words"),
_ => false,
};
}

impl<'a> StateMachine<'a> {
#[inline]
fn test_hunk_line(&self) -> bool {
matches!(
self.state,
State::HunkHeader(_, _) | State::HunkZero | State::HunkMinus(_) | State::HunkPlus(_)
)
) && !&*IS_WORD_DIFF
}

/// Handle a hunk line, i.e. a minus line, a plus line, or an unchanged line.
Expand Down
21 changes: 18 additions & 3 deletions src/utils/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ use lazy_static::lazy_static;

#[derive(Clone, Debug, PartialEq)]
pub enum CallingProcess {
GitDiff(CommandLine),
GitShow(CommandLine, Option<String>), // element 2 is file extension
GitLog(CommandLine),
GitReflog(CommandLine),
GitGrep(CommandLine),
OtherGrep, // rg, grep, ag, ack, etc
}
// TODO: Git blame is currently handled differently

#[derive(Clone, Debug, PartialEq)]
pub struct CommandLine {
Expand Down Expand Up @@ -91,10 +95,12 @@ pub fn describe_calling_process(args: &[String]) -> ProcessArgs<CallingProcess>
match args.next() {
Some(command) => match Path::new(command).file_stem() {
Some(s) if s.to_str().map(|s| is_git_binary(s)).unwrap_or(false) => {
let mut args = args.skip_while(|s| *s != "grep" && *s != "show");
let mut args = args.skip_while(|s| {
*s != "diff" && *s != "show" && *s != "log" && *s != "reflog" && *s != "grep"
});
match args.next() {
Some("grep") => {
ProcessArgs::Args(CallingProcess::GitGrep(parse_command_line(args)))
Some("diff") => {
ProcessArgs::Args(CallingProcess::GitDiff(parse_command_line(args)))
}
Some("show") => {
let command_line = parse_command_line(args);
Expand All @@ -110,6 +116,15 @@ pub fn describe_calling_process(args: &[String]) -> ProcessArgs<CallingProcess>
};
ProcessArgs::Args(CallingProcess::GitShow(command_line, extension))
}
Some("log") => {
ProcessArgs::Args(CallingProcess::GitLog(parse_command_line(args)))
}
Some("reflog") => {
ProcessArgs::Args(CallingProcess::GitReflog(parse_command_line(args)))
}
Some("grep") => {
ProcessArgs::Args(CallingProcess::GitGrep(parse_command_line(args)))
}
_ => {
// It's git, but not a subcommand that we parse. Don't
// look at any more processes.
Expand Down

0 comments on commit b0e0283

Please sign in to comment.