on master help sign in
Move git changes support behind a feature
commit4e11abdf9b0b28dfda2ec4904ddf0a82e524a847 authorDavid Tolnay <dtolnay@gmail.com> dateMon 30 Mar 2020, 10:37 (7 years ago) parent570805b
6 files changed+31−14
Cargo.toml+3 ci/script.bash+1 src/decorations.rs+4 src/diff.rs+2 src/printer.rs+20−14 src/style.rs+1
Cargo.toml+3
@@ -23,10 +23,12 @@ application = [
2323 "atty",
2424 "clap",
2525 "dirs",
26+ "git",
2627 "lazy_static",
2728 "liquid",
2829 "wild",
2930]
31+git = ["git2"] # Support indicating git modifications
3032
3133[dependencies]
3234atty = { version = "0.2.14", optional = true }
@@ -44,6 +46,7 @@ globset = "0.4"
4446
4547[dependencies.git2]
4648version = "0.13"
49+optional = true
4750default-features = false
4851
4952[dependencies.syntect]
ci/script.bash+1
@@ -15,3 +15,4 @@ fi
1515
1616# Check bat-as-a-library, which has a smaller set of dependencies
1717cargo check --target "$TARGET" --verbose --lib --no-default-features
18+cargo check --target "$TARGET" --verbose --lib --no-default-features --features git
src/decorations.rs+4
@@ -1,3 +1,4 @@
1+#[cfg(feature = "git")]
12use crate::diff::LineChange;
23use crate::printer::{Colors, InteractivePrinter};
34use ansi_term::Style;
@@ -68,6 +69,7 @@ impl Decoration for LineNumberDecoration {
6869 }
6970}
7071
72+#[cfg(feature = "git")]
7173pub struct LineChangesDecoration {
7274 cached_none: DecorationText,
7375 cached_added: DecorationText,
@@ -76,6 +78,7 @@ pub struct LineChangesDecoration {
7678 cached_modified: DecorationText,
7779}
7880
81+#[cfg(feature = "git")]
7982impl LineChangesDecoration {
8083 #[inline]
8184 fn generate_cached(style: Style, text: &str) -> DecorationText {
@@ -96,6 +99,7 @@ impl LineChangesDecoration {
9699 }
97100}
98101
102+#[cfg(feature = "git")]
99103impl Decoration for LineChangesDecoration {
100104 fn generate(
101105 &self,
src/diff.rs+2
@@ -1,3 +1,5 @@
1+#![cfg(feature = "git")]
2+
13use std::collections::HashMap;
24use std::ffi::OsStr;
35use std::fs;
src/printer.rs+20−14
@@ -21,11 +21,11 @@ use unicode_width::UnicodeWidthChar;
2121
2222use crate::assets::HighlightingAssets;
2323use crate::config::Config;
24−use crate::decorations::{
25− Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration,
26−};
27−use crate::diff::get_git_diff;
28−use crate::diff::LineChanges;
24+use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration};
25+#[cfg(feature = "git")]
26+use crate::decorations::LineChangesDecoration;
27+#[cfg(feature = "git")]
28+use crate::diff::{get_git_diff, LineChanges};
2929use crate::errors::*;
3030use crate::inputfile::{InputFile, InputFileReader};
3131use crate::line_range::RangeCheckResult;
@@ -100,6 +100,7 @@ pub struct InteractivePrinter<'a> {
100100 panel_width: usize,
101101 ansi_prefix_sgr: String,
102102 content_type: Option<ContentType>,
103+ #[cfg(feature = "git")]
103104 pub line_changes: Option<LineChanges>,
104105 highlighter: Option<HighlightLines<'a>>,
105106 syntax_set: &'a SyntaxSet,
@@ -130,8 +131,11 @@ impl<'a> InteractivePrinter<'a> {
130131 decorations.push(Box::new(LineNumberDecoration::new(&colors)));
131132 }
132133
133− if config.style_components.changes() {
134− decorations.push(Box::new(LineChangesDecoration::new(&colors)));
134+ #[cfg(feature = "git")]
135+ {
136+ if config.style_components.changes() {
137+ decorations.push(Box::new(LineChangesDecoration::new(&colors)));
138+ }
135139 }
136140
137141 let mut panel_width: usize =
@@ -153,6 +157,7 @@ impl<'a> InteractivePrinter<'a> {
153157 panel_width = 0;
154158 }
155159
160+ #[cfg(feature = "git")]
156161 let mut line_changes = None;
157162
158163 let highlighter = if reader
@@ -162,14 +167,14 @@ impl<'a> InteractivePrinter<'a> {
162167 None
163168 } else {
164169 // Get the Git modifications
165− line_changes = if config.style_components.changes() {
166− match file {
167− InputFile::Ordinary(filename) => get_git_diff(filename),
168− _ => None,
170+ #[cfg(feature = "git")]
171+ {
172+ if config.style_components.changes() {
173+ if let InputFile::Ordinary(filename) = file {
174+ line_changes = get_git_diff(filename);
175+ }
169176 }
170− } else {
171− None
172− };
177+ }
173178
174179 // Determine the type of syntax for highlighting
175180 let syntax = assets.get_syntax(config.language, file, reader, &config.syntax_mapping);
@@ -183,6 +188,7 @@ impl<'a> InteractivePrinter<'a> {
183188 decorations,
184189 content_type: reader.content_type,
185190 ansi_prefix_sgr: String::new(),
191+ #[cfg(feature = "git")]
186192 line_changes,
187193 highlighter,
188194 syntax_set: &assets.syntax_set,
src/style.rs+1
@@ -68,6 +68,7 @@ impl StyleComponents {
6868 StyleComponents(components.iter().cloned().collect())
6969 }
7070
71+ #[cfg(feature = "git")]
7172 pub fn changes(&self) -> bool {
7273 self.0.contains(&StyleComponent::Changes)
7374 }