From 4e11abdf9b0b28dfda2ec4904ddf0a82e524a847 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 30 Mar 2020 10:37:29 -0700 Subject: [PATCH] Move git changes support behind a feature --- Cargo.toml | 3 +++ ci/script.bash | 1 + src/decorations.rs | 4 ++++ src/diff.rs | 2 ++ src/printer.rs | 34 ++++++++++++++++++++-------------- src/style.rs | 1 + 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d7ba56b2..c9bd3ad9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,10 +23,12 @@ application = [ "atty", "clap", "dirs", + "git", "lazy_static", "liquid", "wild", ] +git = ["git2"] # Support indicating git modifications [dependencies] atty = { version = "0.2.14", optional = true } @@ -44,6 +46,7 @@ globset = "0.4" [dependencies.git2] version = "0.13" +optional = true default-features = false [dependencies.syntect] diff --git a/ci/script.bash b/ci/script.bash index 7f72ed34..d2055bb2 100755 --- a/ci/script.bash +++ b/ci/script.bash @@ -15,3 +15,4 @@ fi # Check bat-as-a-library, which has a smaller set of dependencies cargo check --target "$TARGET" --verbose --lib --no-default-features +cargo check --target "$TARGET" --verbose --lib --no-default-features --features git diff --git a/src/decorations.rs b/src/decorations.rs index 787a847b..1b44d05a 100644 --- a/src/decorations.rs +++ b/src/decorations.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "git")] use crate::diff::LineChange; use crate::printer::{Colors, InteractivePrinter}; use ansi_term::Style; @@ -68,6 +69,7 @@ impl Decoration for LineNumberDecoration { } } +#[cfg(feature = "git")] pub struct LineChangesDecoration { cached_none: DecorationText, cached_added: DecorationText, @@ -76,6 +78,7 @@ pub struct LineChangesDecoration { cached_modified: DecorationText, } +#[cfg(feature = "git")] impl LineChangesDecoration { #[inline] fn generate_cached(style: Style, text: &str) -> DecorationText { @@ -96,6 +99,7 @@ impl LineChangesDecoration { } } +#[cfg(feature = "git")] impl Decoration for LineChangesDecoration { fn generate( &self, diff --git a/src/diff.rs b/src/diff.rs index bf0c1f30..bd08468a 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "git")] + use std::collections::HashMap; use std::ffi::OsStr; use std::fs; diff --git a/src/printer.rs b/src/printer.rs index 3924cd62..b269219c 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -21,11 +21,11 @@ use unicode_width::UnicodeWidthChar; use crate::assets::HighlightingAssets; use crate::config::Config; -use crate::decorations::{ - Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration, -}; -use crate::diff::get_git_diff; -use crate::diff::LineChanges; +use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration}; +#[cfg(feature = "git")] +use crate::decorations::LineChangesDecoration; +#[cfg(feature = "git")] +use crate::diff::{get_git_diff, LineChanges}; use crate::errors::*; use crate::inputfile::{InputFile, InputFileReader}; use crate::line_range::RangeCheckResult; @@ -100,6 +100,7 @@ pub struct InteractivePrinter<'a> { panel_width: usize, ansi_prefix_sgr: String, content_type: Option, + #[cfg(feature = "git")] pub line_changes: Option, highlighter: Option>, syntax_set: &'a SyntaxSet, @@ -130,8 +131,11 @@ impl<'a> InteractivePrinter<'a> { decorations.push(Box::new(LineNumberDecoration::new(&colors))); } - if config.style_components.changes() { - decorations.push(Box::new(LineChangesDecoration::new(&colors))); + #[cfg(feature = "git")] + { + if config.style_components.changes() { + decorations.push(Box::new(LineChangesDecoration::new(&colors))); + } } let mut panel_width: usize = @@ -153,6 +157,7 @@ impl<'a> InteractivePrinter<'a> { panel_width = 0; } + #[cfg(feature = "git")] let mut line_changes = None; let highlighter = if reader @@ -162,14 +167,14 @@ impl<'a> InteractivePrinter<'a> { None } else { // Get the Git modifications - line_changes = if config.style_components.changes() { - match file { - InputFile::Ordinary(filename) => get_git_diff(filename), - _ => None, + #[cfg(feature = "git")] + { + if config.style_components.changes() { + if let InputFile::Ordinary(filename) = file { + line_changes = get_git_diff(filename); + } } - } else { - None - }; + } // Determine the type of syntax for highlighting let syntax = assets.get_syntax(config.language, file, reader, &config.syntax_mapping); @@ -183,6 +188,7 @@ impl<'a> InteractivePrinter<'a> { decorations, content_type: reader.content_type, ansi_prefix_sgr: String::new(), + #[cfg(feature = "git")] line_changes, highlighter, syntax_set: &assets.syntax_set, diff --git a/src/style.rs b/src/style.rs index a906553a..1821e495 100644 --- a/src/style.rs +++ b/src/style.rs @@ -68,6 +68,7 @@ impl StyleComponents { StyleComponents(components.iter().cloned().collect()) } + #[cfg(feature = "git")] pub fn changes(&self) -> bool { self.0.contains(&StyleComponent::Changes) }