Skip to content

Commit

Permalink
Read git config entries at start-up
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Jul 22, 2020
1 parent 8233a75 commit 142fdbd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use syntect::parsing::SyntaxSet;
use crate::bat::assets::HighlightingAssets;
use crate::bat::output::PagingMode;
use crate::git_config::GitConfig;
use crate::git_config_entry::GitConfigEntry;
use crate::options;

#[derive(StructOpt, Clone, Default)]
Expand Down Expand Up @@ -517,6 +518,9 @@ pub struct Opt {

#[structopt(skip)]
pub computed: ComputedValues,

#[structopt(skip)]
pub git_config_entries: HashMap<String, GitConfigEntry>,
}

#[derive(Default, Clone, Debug)]
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::process;

Expand All @@ -13,6 +14,7 @@ use crate::color;
use crate::delta::State;
use crate::env;
use crate::features::side_by_side;
use crate::git_config_entry::GitConfigEntry;
use crate::style::Style;

pub struct Config {
Expand All @@ -25,6 +27,7 @@ pub struct Config {
pub file_removed_label: String,
pub file_renamed_label: String,
pub file_style: Style,
pub git_config_entries: HashMap<String, GitConfigEntry>,
pub keep_plus_minus_markers: bool,
pub hunk_header_style: Style,
pub max_buffered_lines: usize,
Expand Down Expand Up @@ -134,6 +137,7 @@ impl From<cli::Opt> for Config {
file_removed_label: opt.file_removed_label,
file_renamed_label: opt.file_renamed_label,
file_style,
git_config_entries: opt.git_config_entries,
keep_plus_minus_markers: opt.keep_plus_minus_markers,
hunk_header_style,
max_buffered_lines: 32,
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod edits;
mod env;
mod features;
mod git_config;
mod git_config_entry;
mod options;
mod paint;
mod parse;
Expand All @@ -37,7 +38,7 @@ use crate::bat::output::{OutputType, PagingMode};
use crate::delta::delta;
use crate::options::theme::is_light_syntax_theme;

mod errors {
pub mod errors {
error_chain! {
foreign_links {
Io(::std::io::Error);
Expand Down
32 changes: 32 additions & 0 deletions src/options/set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet, VecDeque};
use std::process;
use std::str::FromStr;

use console::Term;
use structopt::clap;
Expand All @@ -11,8 +12,10 @@ use crate::config;
use crate::env;
use crate::features;
use crate::git_config;
use crate::git_config_entry::{self, GitConfigEntry};
use crate::options::option_value::{OptionValue, ProvenancedOptionValue};
use crate::options::theme;
use crate::style::Style;

macro_rules! set_options {
([$( $field_ident:ident ),* ],
Expand Down Expand Up @@ -68,6 +71,7 @@ pub fn set_options(
if opt.no_gitconfig {
git_config.enabled = false;
}
set_git_config_entries(opt, git_config);
}

let option_names = cli::Opt::get_option_names();
Expand Down Expand Up @@ -493,6 +497,34 @@ fn set_widths(opt: &mut cli::Opt) {
background_color_extends_to_terminal_width;
}

fn set_git_config_entries(opt: &mut cli::Opt, git_config: &mut git_config::GitConfig) {
// Styles
for key in &["color.diff.old", "color.diff.new"] {
if let Some(style_string) = git_config.get::<String>(key) {
opt.git_config_entries.insert(
key.to_string(),
GitConfigEntry::Style(Style::from_str(
&style_string,
None,
None,
opt.computed.true_color,
false,
)),
);
}
}

// Strings
for key in &["remote.origin.url"] {
if let Some(string) = git_config.get::<String>(key) {
if let Ok(repo) = git_config_entry::GitRemoteRepo::from_str(&string) {
opt.git_config_entries
.insert(key.to_string(), GitConfigEntry::GitRemote(repo));
}
}
}
}

#[cfg(test)]
pub mod tests {
use std::fs::remove_file;
Expand Down

0 comments on commit 142fdbd

Please sign in to comment.