From d4da66d0fc94ab2e62abd3cbb0d4221b422f7753 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Mon, 24 May 2021 19:39:57 -0400 Subject: Support custom commit line regex when parsing git output (#558) Fixes #174 --- src/cli.rs | 4 ++++ src/config.rs | 12 ++++++++++++ src/delta.rs | 2 +- src/options/set.rs | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index d097a878..40629bca 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -357,6 +357,10 @@ pub struct Opt { /// (underline), 'ol' (overline), or the combination 'ul ol'. pub commit_decoration_style: String, + /// The regular expression used to identify the commit line when parsing git output. + #[structopt(long = "commit-regex", default_value = r"^commit ")] + pub commit_regex: String, + #[structopt(long = "file-style", default_value = "blue")] /// Style (foreground, background, attributes) for the file section. See STYLES section. The /// style 'omit' can be used to remove the file section from the output. diff --git a/src/config.rs b/src/config.rs index fbad2e41..8d72fb33 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,6 +23,7 @@ pub struct Config { pub background_color_extends_to_terminal_width: bool, pub commit_style: Style, pub color_only: bool, + pub commit_regex: Regex, pub cwd_relative_to_repo_root: Option, pub decorations_width: cli::Width, pub diff_stat_align_width: usize, @@ -136,6 +137,16 @@ impl From for Config { .map(|s| s.parse::().unwrap_or(0.0)) .unwrap_or(0.0); + let commit_regex = Regex::new(&opt.commit_regex).unwrap_or_else(|_| { + eprintln!( + "Invalid commit-regex: {}. \ + The value must be a valid Rust regular expression. \ + See https://docs.rs/regex.", + opt.commit_regex + ); + process::exit(1); + }); + let tokenization_regex = Regex::new(&opt.tokenization_regex).unwrap_or_else(|_| { eprintln!( "Invalid word-diff-regex: {}. \ @@ -185,6 +196,7 @@ impl From for Config { .background_color_extends_to_terminal_width, commit_style, color_only: opt.color_only, + commit_regex, cwd_relative_to_repo_root: std::env::var("GIT_PREFIX").ok(), decorations_width: opt.computed.decorations_width, diff_stat_align_width: opt.diff_stat_align_width, diff --git a/src/delta.rs b/src/delta.rs index cfafe3dc..81202bf8 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -115,7 +115,7 @@ impl<'a> StateMachine<'a> { self.source = detect_source(&line); } - let mut handled_line = if line.starts_with("commit ") { + let mut handled_line = if self.config.commit_regex.is_match(line) { self.handle_commit_meta_header_line()? } else if (self.state == State::CommitMeta || self.state == State::Unknown) && line.starts_with(' ') diff --git a/src/options/set.rs b/src/options/set.rs index 6a1a7ad0..b27907e0 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -127,6 +127,7 @@ pub fn set_options( [ color_only, commit_decoration_style, + commit_regex, commit_style, diff_stat_align_width, file_added_label, -- cgit v1.2.3