From 42566db2ec5d5bfb42043e95b1ddff117557e8bc Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Fri, 8 Jan 2021 07:19:42 -0500 Subject: Create git_config module --- src/cli.rs | 3 +- src/config.rs | 2 +- src/features/hyperlinks.rs | 2 +- src/git_config.rs | 101 ------------------------------------- src/git_config/git_config.rs | 101 +++++++++++++++++++++++++++++++++++++ src/git_config/git_config_entry.rs | 39 ++++++++++++++ src/git_config_entry.rs | 39 -------------- src/main.rs | 1 - src/options/set.rs | 19 ++++--- 9 files changed, 152 insertions(+), 155 deletions(-) delete mode 100644 src/git_config.rs create mode 100644 src/git_config/git_config.rs create mode 100644 src/git_config/git_config_entry.rs delete mode 100644 src/git_config_entry.rs diff --git a/src/cli.rs b/src/cli.rs index 743b72f3..41f6eb89 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -11,8 +11,7 @@ use syntect::parsing::SyntaxSet; use crate::bat_utils::assets::HighlightingAssets; use crate::bat_utils::output::PagingMode; -use crate::git_config::GitConfig; -use crate::git_config_entry::GitConfigEntry; +use crate::git_config::{GitConfig, GitConfigEntry}; use crate::options; #[derive(StructOpt, Clone, Default)] diff --git a/src/config.rs b/src/config.rs index d7828952..23b7b97d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,7 +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::git_config::GitConfigEntry; use crate::style::{self, Style}; pub struct Config { diff --git a/src/features/hyperlinks.rs b/src/features/hyperlinks.rs index 3bb5e70c..d59f5e9b 100644 --- a/src/features/hyperlinks.rs +++ b/src/features/hyperlinks.rs @@ -5,7 +5,7 @@ use regex::{Captures, Regex}; use crate::config::Config; use crate::features::OptionValueFunction; -use crate::git_config_entry::{GitConfigEntry, GitRemoteRepo}; +use crate::git_config::{GitConfigEntry, GitRemoteRepo}; pub fn make_feature() -> Vec<(String, OptionValueFunction)> { builtin_feature!([ diff --git a/src/git_config.rs b/src/git_config.rs deleted file mode 100644 index ab831b31..00000000 --- a/src/git_config.rs +++ /dev/null @@ -1,101 +0,0 @@ -#[cfg(test)] -use std::path::Path; -use std::process; - -pub struct GitConfig { - config: git2::Config, - pub enabled: bool, - pub repo: Option, -} - -impl GitConfig { - pub fn try_create() -> Option { - let repo = match std::env::current_dir() { - Ok(dir) => git2::Repository::discover(dir).ok(), - _ => None, - }; - let config = match &repo { - Some(repo) => repo.config().ok(), - None => git2::Config::open_default().ok(), - }; - match config { - Some(mut config) => { - let config = config.snapshot().unwrap_or_else(|err| { - eprintln!("Failed to read git config: {}", err); - process::exit(1) - }); - Some(Self { - config, - repo, - enabled: true, - }) - } - None => None, - } - } - - #[cfg(test)] - pub fn from_path(path: &Path) -> Self { - Self { - config: git2::Config::open(path).unwrap(), - repo: None, - enabled: true, - } - } - - pub fn get(&self, key: &str) -> Option - where - T: GitConfigGet, - { - if self.enabled { - T::git_config_get(key, self) - } else { - None - } - } -} - -pub trait GitConfigGet { - fn git_config_get(key: &str, git_config: &GitConfig) -> Option - where - Self: Sized; -} - -impl GitConfigGet for String { - fn git_config_get(key: &str, git_config: &GitConfig) -> Option { - git_config.config.get_string(key).ok() - } -} - -impl GitConfigGet for Option { - fn git_config_get(key: &str, git_config: &GitConfig) -> Option { - match git_config.config.get_string(key) { - Ok(value) => Some(Some(value)), - _ => None, - } - } -} - -impl GitConfigGet for bool { - fn git_config_get(key: &str, git_config: &GitConfig) -> Option { - git_config.config.get_bool(key).ok() - } -} - -impl GitConfigGet for usize { - fn git_config_get(key: &str, git_config: &GitConfig) -> Option { - match git_config.config.get_i64(key) { - Ok(value) => Some(value as usize), - _ => None, - } - } -} - -impl GitConfigGet for f64 { - fn git_config_get(key: &str, git_config: &GitConfig) -> Option { - match git_config.config.get_string(key) { - Ok(value) => value.parse::().ok(), - _ => None, - } - } -} diff --git a/src/git_config/git_config.rs b/src/git_config/git_config.rs new file mode 100644 index 00000000..ab831b31 --- /dev/null +++ b/src/git_config/git_config.rs @@ -0,0 +1,101 @@ +#[cfg(test)] +use std::path::Path; +use std::process; + +pub struct GitConfig { + config: git2::Config, + pub enabled: bool, + pub repo: Option, +} + +impl GitConfig { + pub fn try_create() -> Option { + let repo = match std::env::current_dir() { + Ok(dir) => git2::Repository::discover(dir).ok(), + _ => None, + }; + let config = match &repo { + Some(repo) => repo.config().ok(), + None => git2::Config::open_default().ok(), + }; + match config { + Some(mut config) => { + let config = config.snapshot().unwrap_or_else(|err| { + eprintln!("Failed to read git config: {}", err); + process::exit(1) + }); + Some(Self { + config, + repo, + enabled: true, + }) + } + None => None, + } + } + + #[cfg(test)] + pub fn from_path(path: &Path) -> Self { + Self { + config: git2::Config::open(path).unwrap(), + repo: None, + enabled: true, + } + } + + pub fn get(&self, key: &str) -> Option + where + T: GitConfigGet, + { + if self.enabled { + T::git_config_get(key, self) + } else { + None + } + } +} + +pub trait GitConfigGet { + fn git_config_get(key: &str, git_config: &GitConfig) -> Option + where + Self: Sized; +} + +impl GitConfigGet for String { + fn git_config_get(key: &str, git_config: &GitConfig) -> Option { + git_config.config.get_string(key).ok() + } +} + +impl GitConfigGet for Option { + fn git_config_get(key: &str, git_config: &GitConfig) -> Option { + match git_config.config.get_string(key) { + Ok(value) => Some(Some(value)), + _ => None, + } + } +} + +impl GitConfigGet for bool { + fn git_config_get(key: &str, git_config: &GitConfig) -> Option { + git_config.config.get_bool(key).ok() + } +} + +impl GitConfigGet for usize { + fn git_config_get(key: &str, git_config: &GitConfig) -> Option { + match git_config.config.get_i64(key) { + Ok(value) => Some(value as usize), + _ => None, + } + } +} + +impl GitConfigGet for f64 { + fn git_config_get(key: &str, git_config: &GitConfig) -> Option { + match git_config.config.get_string(key) { + Ok(value) => value.parse::().ok(), + _ => None, + } + } +} diff --git a/src/git_config/git_config_entry.rs b/src/git_config/git_config_entry.rs new file mode 100644 index 00000000..2764e2bd --- /dev/null +++ b/src/git_config/git_config_entry.rs @@ -0,0 +1,39 @@ +use std::path::PathBuf; +use std::result::Result; +use std::str::FromStr; + +use lazy_static::lazy_static; +use regex::Regex; + +use crate::errors::*; + +#[derive(Clone, Debug)] +pub enum GitConfigEntry { + Style(String), + GitRemote(GitRemoteRepo), + Path(PathBuf), +} + +#[derive(Clone, Debug)] +pub enum GitRemoteRepo { + GitHubRepo(String), +} + +lazy_static! { + static ref GITHUB_REMOTE_URL: Regex = Regex::new(r"github\.com[:/]([^/]+)/(.+)\.git").unwrap(); +} + +impl FromStr for GitRemoteRepo { + type Err = Error; + fn from_str(s: &str) -> Result { + if let Some(caps) = GITHUB_REMOTE_URL.captures(s) { + Ok(Self::GitHubRepo(format!( + "{user}/{repo}", + user = caps.get(1).unwrap().as_str(), + repo = caps.get(2).unwrap().as_str() + ))) + } else { + Err("Not a GitHub repo.".into()) + } + } +} diff --git a/src/git_config_entry.rs b/src/git_config_entry.rs deleted file mode 100644 index 2764e2bd..00000000 --- a/src/git_config_entry.rs +++ /dev/null @@ -1,39 +0,0 @@ -use std::path::PathBuf; -use std::result::Result; -use std::str::FromStr; - -use lazy_static::lazy_static; -use regex::Regex; - -use crate::errors::*; - -#[derive(Clone, Debug)] -pub enum GitConfigEntry { - Style(String), - GitRemote(GitRemoteRepo), - Path(PathBuf), -} - -#[derive(Clone, Debug)] -pub enum GitRemoteRepo { - GitHubRepo(String), -} - -lazy_static! { - static ref GITHUB_REMOTE_URL: Regex = Regex::new(r"github\.com[:/]([^/]+)/(.+)\.git").unwrap(); -} - -impl FromStr for GitRemoteRepo { - type Err = Error; - fn from_str(s: &str) -> Result { - if let Some(caps) = GITHUB_REMOTE_URL.captures(s) { - Ok(Self::GitHubRepo(format!( - "{user}/{repo}", - user = caps.get(1).unwrap().as_str(), - repo = caps.get(2).unwrap().as_str() - ))) - } else { - Err("Not a GitHub repo.".into()) - } - } -} diff --git a/src/main.rs b/src/main.rs index cad56b93..5757fbab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,6 @@ mod env; mod features; mod format; mod git_config; -mod git_config_entry; mod hunk_header; mod options; mod paint; diff --git a/src/options/set.rs b/src/options/set.rs index eb74a31d..354eeea6 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -13,8 +13,7 @@ use crate::config; use crate::env; use crate::errors::*; use crate::features; -use crate::git_config; -use crate::git_config_entry::{self, GitConfigEntry}; +use crate::git_config::{GitConfig, GitConfigEntry, GitRemoteRepo}; use crate::options::option_value::{OptionValue, ProvenancedOptionValue}; use crate::options::theme; @@ -64,7 +63,7 @@ macro_rules! set_options { pub fn set_options( opt: &mut cli::Opt, - git_config: &mut Option, + git_config: &mut Option, arg_matches: &clap::ArgMatches, assets: HighlightingAssets, ) { @@ -204,7 +203,7 @@ pub fn set_options( #[allow(non_snake_case)] fn set__light__dark__syntax_theme__options( opt: &mut cli::Opt, - git_config: &mut Option, + git_config: &mut Option, arg_matches: &clap::ArgMatches, option_names: &HashMap<&str, &str>, ) { @@ -291,7 +290,7 @@ fn set__light__dark__syntax_theme__options( fn gather_features( opt: &cli::Opt, builtin_features: &HashMap, - git_config: &Option, + git_config: &Option, ) -> Vec { let mut features = VecDeque::new(); @@ -372,7 +371,7 @@ fn gather_features_recursively( features: &mut VecDeque, builtin_features: &HashMap, opt: &cli::Opt, - git_config: &git_config::GitConfig, + git_config: &GitConfig, ) { if builtin_features.contains_key(feature) { gather_builtin_features_recursively(feature, features, builtin_features, opt); @@ -408,7 +407,7 @@ fn gather_builtin_features_from_flags_in_gitconfig( features: &mut VecDeque, builtin_features: &HashMap, opt: &cli::Opt, - git_config: &git_config::GitConfig, + git_config: &GitConfig, ) { for child_feature in builtin_features.keys() { if let Some(value) = @@ -510,7 +509,7 @@ fn parse_paging_mode(paging_mode_string: &str) -> PagingMode { fn set_widths( opt: &mut cli::Opt, - git_config: &mut Option, + git_config: &mut Option, arg_matches: &clap::ArgMatches, option_names: &HashMap<&str, &str>, ) { @@ -571,7 +570,7 @@ fn is_truecolor_terminal() -> bool { .unwrap_or(false) } -fn set_git_config_entries(opt: &mut cli::Opt, git_config: &mut git_config::GitConfig) { +fn set_git_config_entries(opt: &mut cli::Opt, git_config: &mut GitConfig) { // Styles for key in &["color.diff.old", "color.diff.new"] { if let Some(style_string) = git_config.get::(key) { @@ -583,7 +582,7 @@ fn set_git_config_entries(opt: &mut cli::Opt, git_config: &mut git_config::GitCo // Strings for key in &["remote.origin.url"] { if let Some(string) = git_config.get::(key) { - if let Ok(repo) = git_config_entry::GitRemoteRepo::from_str(&string) { + if let Ok(repo) = GitRemoteRepo::from_str(&string) { opt.git_config_entries .insert(key.to_string(), GitConfigEntry::GitRemote(repo)); } -- cgit v1.2.3