diff options
author | Dan Davison <dandavison7@gmail.com> | 2021-04-24 11:26:49 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-24 11:26:49 -0400 |
commit | eef33b6b33053db3dc07fb92d9a6eb55c089f5fe (patch) | |
tree | 1beacef990820126d97cfcea14e7e89b871931a2 | |
parent | 9f2f44d8777352a2abaec9b6f643cc56593bbe38 (diff) | |
parent | 567bdf6775bce0f9b7ab6fc12cc319927aca12e2 (diff) |
Merge pull request #573 from dandavison/support-new-style-git-config-parameters
Support new GIT_CONFIG_PARAMETERS format
-rw-r--r-- | src/git_config/mod.rs | 91 |
1 files changed, 67 insertions, 24 deletions
diff --git a/src/git_config/mod.rs b/src/git_config/mod.rs index 13b494b9..1b2ea0a6 100644 --- a/src/git_config/mod.rs +++ b/src/git_config/mod.rs @@ -80,14 +80,38 @@ fn parse_config_from_env_var() -> HashMap<String, String> { } lazy_static! { - static ref GIT_CONFIG_PARAMETERS_REGEX: Regex = - Regex::new(r"'(delta\.[a-z-]+)=([^']+)'").unwrap(); + static ref GIT_CONFIG_PARAMETERS_REGEX: Regex = Regex::new( + r"(?x) + (?: # Non-capturing group containing union + '(delta\.[a-z-]+)=([^']+)' # Git <2.31.0 format + | + '(delta\.[a-z-]+)'='([^']+)' # Git ≥2.31.0 format + ) + " + ) + .unwrap(); } fn parse_config_from_env_var_value(s: &str) -> HashMap<String, String> { GIT_CONFIG_PARAMETERS_REGEX .captures_iter(s) - .map(|captures| (captures[1].to_string(), captures[2].to_string())) + .map(|captures| { + let (i, j) = match ( + captures.get(1), + captures.get(2), + captures.get(3), + captures.get(4), + ) { + (Some(_), Some(_), None, None) => (1, 2), + (None, None, Some(_), Some(_)) => (3, 4), + _ => (0, 0), + }; + if (i, j) == (0, 0) { + ("".to_string(), "".to_string()) + } else { + (captures[i].to_string(), captures[j].to_string()) + } + }) .collect() } @@ -167,32 +191,51 @@ mod tests { // [core] // pager = env | grep GIT_CONFIG_PARAMETERS - let config = parse_config_from_env_var_value("'user.name=xxx'"); - assert!(config.is_empty()); + // We test multiple formats because the format of the value stored by + // git in this environment variable has changed in recent versions of + // Git. See + // https://github.com/git/git/blob/311531c9de557d25ac087c1637818bd2aad6eb3a/Documentation/RelNotes/2.31.0.txt#L127-L130 + + for env_var_value in &["'user.name=xxx'", "'user.name'='xxx'"] { + let config = parse_config_from_env_var_value(env_var_value); + assert!(config.is_empty()); + } - let config = parse_config_from_env_var_value("'delta.plus-style=green'"); - assert_eq!(config["delta.plus-style"], "green"); + for env_var_value in &["'delta.plus-style=green'", "'delta.plus-style'='green'"] { + let config = parse_config_from_env_var_value(env_var_value); + assert_eq!(config["delta.plus-style"], "green"); + } - let config = parse_config_from_env_var_value( + for env_var_value in &[ r##"'user.name=xxx' 'delta.hunk-header-line-number-style=red "#067a00"'"##, - ); - assert_eq!( - config["delta.hunk-header-line-number-style"], - r##"red "#067a00""## - ); + r##"'user.name'='xxx' 'delta.hunk-header-line-number-style'='red "#067a00"'"##, + ] { + let config = parse_config_from_env_var_value(env_var_value); + assert_eq!( + config["delta.hunk-header-line-number-style"], + r##"red "#067a00""## + ); + } - let config = - parse_config_from_env_var_value(r##"'user.name=xxx' 'delta.side-by-side=false'"##); - assert_eq!(config["delta.side-by-side"], "false"); + for env_var_value in &[ + r##"'user.name=xxx' 'delta.side-by-side=false'"##, + r##"'user.name'='xxx' 'delta.side-by-side'='false'"##, + ] { + let config = parse_config_from_env_var_value(env_var_value); + assert_eq!(config["delta.side-by-side"], "false"); + } - let config = parse_config_from_env_var_value( + for env_var_value in &[ r##"'delta.plus-style=green' 'delta.side-by-side=false' 'delta.hunk-header-line-number-style=red "#067a00"'"##, - ); - assert_eq!(config["delta.plus-style"], "green"); - assert_eq!(config["delta.side-by-side"], "false"); - assert_eq!( - config["delta.hunk-header-line-number-style"], - r##"red "#067a00""## - ); + r##"'delta.plus-style'='green' 'delta.side-by-side'='false' 'delta.hunk-header-line-number-style'='red "#067a00"'"##, + ] { + let config = parse_config_from_env_var_value(env_var_value); + assert_eq!(config["delta.plus-style"], "green"); + assert_eq!(config["delta.side-by-side"], "false"); + assert_eq!( + config["delta.hunk-header-line-number-style"], + r##"red "#067a00""## + ); + } } } |