Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge the different GitConfig constructors for a config file #1342

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a matter of style in languages like Rust with convenient enums/ADTs, do you think there's an argument for creating an enum so that this call site looks something like

GitConfig::from_path(&env, path, HonorGitConfigParametersEnvVar.Yes)

making it clear what the semantics of the boolean is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think more idiomatic would be a builder.

GitConfig::from_path(&env, path).ignore_git_config_parameters(true);

Personally, I don't mind a boolean parameter because of my intellisense setup that shows the signature, doc comments and autocompletes this function to GitConfig::from_path(&env, path, honor_env_var).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, fair enough! Feel free to introduce builders.

}
}

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