summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaolo Insogna <paolo@cowtech.it>2023-03-05 23:32:11 +0100
committerGitHub <noreply@github.com>2023-03-05 17:32:11 -0500
commit3a09926e4576542e4fac04943dc6781eb026656e (patch)
tree33ebceccf36f3a772d084028b834839166f2deb7
parent5e4faabfd36b3fae69dc15b2f39f18196bd5154c (diff)
Added config option. (#1324)
* feat: Added config option.
-rw-r--r--src/cli.rs32
-rw-r--r--src/git_config/mod.rs31
-rw-r--r--src/options/get.rs2
-rw-r--r--src/options/set.rs1
4 files changed, 63 insertions, 3 deletions
diff --git a/src/cli.rs b/src/cli.rs
index fc959e2c..af8be13a 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -55,6 +55,23 @@ A feature name may not contain whitespace. You can activate multiple features:
If more than one feature sets the same option, the last one wins.
+If an option is present in the [delta] section, then features are not considered at all.
+
+If you want an option to be fully overridable by a feature and also have a non default value when
+no features are used, then you need to define a \"default\" feature and include it in the main
+delta configuration.
+
+For instance:
+
+[delta]
+feature = default-feature
+
+[delta \"default-feature\"]
+width = 123
+
+At this point, you can override features set in the command line or in the environment variables
+and the \"last one wins\" rules will apply as expected.
+
STYLES
------
@@ -276,6 +293,10 @@ pub struct Opt {
/// intended for other tools that use delta.
pub color_only: bool,
+ #[arg(long = "config", default_value = "", value_name = "PATH")]
+ /// Load the config file at PATH instead of ~/.gitconfig.
+ pub config: String,
+
#[arg(
long = "commit-decoration-style",
default_value = "",
@@ -1135,7 +1156,16 @@ impl Opt {
git_config: Option<GitConfig>,
assets: HighlightingAssets,
) -> Self {
- Self::from_clap_and_git_config(env, Self::command().get_matches(), git_config, assets)
+ let mut final_config = git_config;
+ let matches = Self::command().get_matches();
+
+ if let Some(path) = matches.get_one::<String>("config") {
+ if !path.is_empty() {
+ final_config = Some(GitConfig::try_create_from_path(&env, path));
+ }
+ }
+
+ Self::from_clap_and_git_config(env, matches, final_config, assets)
}
pub fn from_iter_and_git_config<I>(
diff --git a/src/git_config/mod.rs b/src/git_config/mod.rs
index 60fb0a62..6e40a70e 100644
--- a/src/git_config/mod.rs
+++ b/src/git_config/mod.rs
@@ -5,7 +5,6 @@ pub use git_config_entry::{GitConfigEntry, GitRemoteRepo};
use crate::env::DeltaEnv;
use regex::Regex;
use std::collections::HashMap;
-#[cfg(test)]
use std::path::Path;
use std::str::FromStr;
@@ -65,12 +64,42 @@ impl GitConfig {
}
}
+ #[cfg(not(test))]
+ pub fn try_create_from_path(env: &DeltaEnv, path: &String) -> Self {
+ use crate::fatal;
+
+ let config = git2::Config::open(Path::new(path));
+
+ match config {
+ 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),
+ repo: None,
+ enabled: true,
+ }
+ }
+ Err(e) => {
+ fatal(format!("Failed to read git config: {}", e.message()));
+ }
+ }
+ }
+
#[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(),
diff --git a/src/options/get.rs b/src/options/get.rs
index 2ee3ee1b..974b9a83 100644
--- a/src/options/get.rs
+++ b/src/options/get.rs
@@ -9,7 +9,7 @@ use ProvenancedOptionValue::*;
// Look up a value of type `T` associated with `option name`. The search rules are:
//
// 1. If there is a value associated with `option_name` in the main [delta] git config
-// section, then stop searching and return that value.
+// section, then stop searching and return that value (steps 2 and 3 are not executed at all).
//
// 2. For each feature in the ordered list of enabled features:
//
diff --git a/src/options/set.rs b/src/options/set.rs
index 27aff042..d078a19d 100644
--- a/src/options/set.rs
+++ b/src/options/set.rs
@@ -135,6 +135,7 @@ pub fn set_options(
blame_timestamp_format,
blame_timestamp_output_format,
color_only,
+ config,
commit_decoration_style,
commit_regex,
commit_style,