Skip to content

Commit

Permalink
Merge the different GitConfig constructors for a config file
Browse files Browse the repository at this point in the history
The `try_create_from_path` function and the `from_path` function for
tests can be merged into a single function.
  • Loading branch information
nickelc committed Mar 10, 2023
1 parent 1642135 commit d9df684
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::ffi::OsString;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use bat::assets::HighlightingAssets;
use clap::{ColorChoice, CommandFactory, FromArgMatches, Parser};
Expand Down Expand Up @@ -1142,7 +1142,8 @@ impl Opt {

if let Some(path) = matches.get_one::<String>("config") {
if !path.is_empty() {
final_config = Some(GitConfig::try_create_from_path(&env, path));
let path = Path::new(path);
final_config = Some(GitConfig::from_path(&env, path, true));
}
}

Expand Down
45 changes: 14 additions & 31 deletions src/git_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,31 @@ impl GitConfig {
}
}

#[cfg(not(test))]
pub fn try_create_from_path(env: &DeltaEnv, path: &String) -> Self {
use crate::fatal;
#[cfg(test)]
pub fn try_create(_env: &DeltaEnv) -> Option<Self> {
unreachable!("GitConfig::try_create() is not available when testing");
}

let config = git2::Config::open(Path::new(path));
pub fn from_path(env: &DeltaEnv, path: &Path, honor_env_var: bool) -> Self {
use crate::fatal;

match config {
match git2::Config::open(path) {
Ok(mut config) => {
let config = config.snapshot().unwrap_or_else(|err| {
fatal(format!("Failed to read git config: {err}"));
});

Self {
config,
config_from_env_var: parse_config_from_env_var(env),
config_from_env_var: if honor_env_var {
parse_config_from_env_var(env)
} else {
HashMap::new()
},
repo: None,
enabled: true,
#[cfg(test)]
path: path.into(),
}
}
Err(e) => {
Expand All @@ -89,31 +97,6 @@ impl GitConfig {
}
}

#[cfg(test)]
pub fn try_create(_env: &DeltaEnv) -> Option<Self> {
unreachable!("GitConfig::try_create() is not available when testing");
}

#[cfg(test)]
pub fn try_create_from_path(_env: &DeltaEnv, _path: &String) -> Self {
unreachable!("GitConfig::try_create_from_path() is not available when testing");
}

#[cfg(test)]
pub fn from_path(env: &DeltaEnv, path: &Path, honor_env_var: bool) -> Self {
Self {
config: git2::Config::open(path).unwrap(),
config_from_env_var: if honor_env_var {
parse_config_from_env_var(env)
} else {
HashMap::new()
},
repo: None,
enabled: true,
path: path.into(),
}
}

pub fn get<T>(&self, key: &str) -> Option<T>
where
T: GitConfigGet,
Expand Down

0 comments on commit d9df684

Please sign in to comment.