| 1 |
mod remote; |
| 2 |
|
| 3 |
pub use remote::GitRemoteRepo; |
| 4 |
|
| 5 |
use crate::env::DeltaEnv; |
| 6 |
use regex::Regex; |
| 7 |
use std::cell::OnceCell; |
| 8 |
use std::collections::HashMap; |
| 9 |
use std::path::Path; |
| 10 |
|
| 11 |
use lazy_static::lazy_static; |
| 12 |
|
| 13 |
pub struct GitConfig { |
| 14 |
config: git2::Config, |
| 15 |
config_from_env_var: HashMap<String, String>, |
| 16 |
pub enabled: bool, |
| 17 |
repo: Option<git2::Repository>, |
| 18 |
remote_url: OnceCell<Option<GitRemoteRepo>>, |
| 19 |
// To make GitConfig cloneable when testing (in turn to make Config cloneable): |
| 20 |
#[cfg(test)] |
| 21 |
path: std::path::PathBuf, |
| 22 |
} |
| 23 |
|
| 24 |
#[cfg(test)] |
| 25 |
impl Clone for GitConfig { |
| 26 |
fn clone(&self) -> Self { |
| 27 |
assert!(self.repo.is_none()); |
| 28 |
GitConfig { |
| 29 |
// Assumes no test modifies the file pointed to by `path` |
| 30 |
config: git2::Config::open(&self.path).unwrap(), |
| 31 |
config_from_env_var: self.config_from_env_var.clone(), |
| 32 |
enabled: self.enabled, |
| 33 |
repo: None, |
| 34 |
remote_url: OnceCell::new(), |
| 35 |
path: self.path.clone(), |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl GitConfig { |
| 41 |
#[cfg(not(test))] |
| 42 |
pub fn try_create(env: &DeltaEnv) -> Option<Self> { |
| 43 |
use crate::fatal; |
| 44 |
|
| 45 |
let repo = match &env.current_dir { |
| 46 |
Some(dir) => git2::Repository::discover(dir).ok(), |
| 47 |
_ => None, |
| 48 |
}; |
| 49 |
let config = match &repo { |
| 50 |
Some(repo) => repo.config().ok(), |
| 51 |
None => git2::Config::open_default().ok(), |
| 52 |
}; |
| 53 |
match config { |
| 54 |
Some(mut config) => { |
| 55 |
let config = config.snapshot().unwrap_or_else(|err| { |
| 56 |
fatal(format!("Failed to read git config: {err}")); |
| 57 |
}); |
| 58 |
Some(Self { |
| 59 |
config, |
| 60 |
config_from_env_var: parse_config_from_env_var(env), |
| 61 |
repo, |
| 62 |
enabled: true, |
| 63 |
remote_url: OnceCell::new(), |
| 64 |
}) |
| 65 |
} |
| 66 |
None => None, |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
#[cfg(test)] |
| 71 |
pub fn try_create(_env: &DeltaEnv) -> Option<Self> { |
| 72 |
// Do not read local git configs when testing |
| 73 |
None |
| 74 |
} |
| 75 |
|
| 76 |
#[cfg(test)] |
| 77 |
pub fn for_testing() -> Option<Self> { |
| 78 |
Some(GitConfig { |
| 79 |
config: git2::Config::new().unwrap(), |
| 80 |
config_from_env_var: HashMap::new(), |
| 81 |
enabled: true, |
| 82 |
repo: None, |
| 83 |
remote_url: OnceCell::new(), |
| 84 |
path: std::path::PathBuf::from("/invalid_null.git"), |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
pub fn from_path(env: &DeltaEnv, path: &Path, honor_env_var: bool) -> Self { |
| 89 |
use crate::fatal; |
| 90 |
|
| 91 |
match git2::Config::open(path) { |
| 92 |
Ok(mut config) => { |
| 93 |
let config = config.snapshot().unwrap_or_else(|err| { |
| 94 |
fatal(format!("Failed to read git config: {err}")); |
| 95 |
}); |
| 96 |
|
| 97 |
Self { |
| 98 |
config, |
| 99 |
config_from_env_var: if honor_env_var { |
| 100 |
parse_config_from_env_var(env) |
| 101 |
} else { |
| 102 |
HashMap::new() |
| 103 |
}, |
| 104 |
repo: None, |
| 105 |
enabled: true, |
| 106 |
remote_url: OnceCell::new(), |
| 107 |
#[cfg(test)] |
| 108 |
path: path.into(), |
| 109 |
} |
| 110 |
} |
| 111 |
Err(e) => { |
| 112 |
fatal(format!("Failed to read git config: {}", e.message())); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
pub fn get<T>(&self, key: &str) -> Option<T> |
| 118 |
where |
| 119 |
T: GitConfigGet, |
| 120 |
{ |
| 121 |
if self.enabled { |
| 122 |
T::git_config_get(key, self) |
| 123 |
} else { |
| 124 |
None |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
#[cfg(test)] |
| 129 |
fn get_remote_url_impl(&self) -> Option<GitRemoteRepo> { |
| 130 |
GitRemoteRepo::for_testing() |
| 131 |
} |
| 132 |
|
| 133 |
#[cfg(not(test))] |
| 134 |
fn get_remote_url_impl(&self) -> Option<GitRemoteRepo> { |
| 135 |
use std::str::FromStr; |
| 136 |
self.repo |
| 137 |
.as_ref()? |
| 138 |
.find_remote("origin") |
| 139 |
.ok()? |
| 140 |
.url() |
| 141 |
.and_then(|url| GitRemoteRepo::from_str(url).ok()) |
| 142 |
} |
| 143 |
|
| 144 |
pub fn get_remote_url(&self) -> &Option<GitRemoteRepo> { |
| 145 |
self.remote_url.get_or_init(|| self.get_remote_url_impl()) |
| 146 |
} |
| 147 |
|
| 148 |
pub fn for_each<F>(&self, regex: &str, mut f: F) |
| 149 |
where |
| 150 |
F: FnMut(&str, Option<&str>), |
| 151 |
{ |
| 152 |
let mut entries = self.config.entries(Some(regex)).unwrap(); |
| 153 |
while let Some(entry) = entries.next() { |
| 154 |
let entry = entry.unwrap(); |
| 155 |
let name = entry.name().unwrap(); |
| 156 |
f(name, entry.value()); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
fn parse_config_from_env_var(env: &DeltaEnv) -> HashMap<String, String> { |
| 162 |
if let Some(s) = &env.git_config_parameters { |
| 163 |
parse_config_from_env_var_value(s) |
| 164 |
} else { |
| 165 |
HashMap::new() |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
lazy_static! { |
| 170 |
static ref GIT_CONFIG_PARAMETERS_REGEX: Regex = Regex::new( |
| 171 |
r"(?x) |
| 172 |
(?: # Non-capturing group containing union |
| 173 |
'(delta\.[a-z-]+)=([^']+)' # Git <2.31.0 format |
| 174 |
| |
| 175 |
'(delta\.[a-z-]+)'='([^']+)' # Git ≥2.31.0 format |
| 176 |
) |
| 177 |
" |
| 178 |
) |
| 179 |
.unwrap(); |
| 180 |
} |
| 181 |
|
| 182 |
fn parse_config_from_env_var_value(s: &str) -> HashMap<String, String> { |
| 183 |
GIT_CONFIG_PARAMETERS_REGEX |
| 184 |
.captures_iter(s) |
| 185 |
.map(|captures| { |
| 186 |
let (i, j) = match ( |
| 187 |
captures.get(1), |
| 188 |
captures.get(2), |
| 189 |
captures.get(3), |
| 190 |
captures.get(4), |
| 191 |
) { |
| 192 |
(Some(_), Some(_), None, None) => (1, 2), |
| 193 |
(None, None, Some(_), Some(_)) => (3, 4), |
| 194 |
_ => (0, 0), |
| 195 |
}; |
| 196 |
if (i, j) == (0, 0) { |
| 197 |
("".to_string(), "".to_string()) |
| 198 |
} else { |
| 199 |
(captures[i].to_string(), captures[j].to_string()) |
| 200 |
} |
| 201 |
}) |
| 202 |
.collect() |
| 203 |
} |
| 204 |
|
| 205 |
pub trait GitConfigGet { |
| 206 |
fn git_config_get(key: &str, git_config: &GitConfig) -> Option<Self> |
| 207 |
where |
| 208 |
Self: Sized; |
| 209 |
} |
| 210 |
|
| 211 |
impl GitConfigGet for String { |
| 212 |
fn git_config_get(key: &str, git_config: &GitConfig) -> Option<Self> { |
| 213 |
match git_config.config_from_env_var.get(key) { |
| 214 |
Some(val) => Some(val.to_string()), |
| 215 |
None => git_config.config.get_string(key).ok(), |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
impl GitConfigGet for Option<String> { |
| 221 |
fn git_config_get(key: &str, git_config: &GitConfig) -> Option<Self> { |
| 222 |
match git_config.config_from_env_var.get(key) { |
| 223 |
Some(val) => Some(Some(val.to_string())), |
| 224 |
None => match git_config.config.get_string(key) { |
| 225 |
Ok(val) => Some(Some(val)), |
| 226 |
_ => None, |
| 227 |
}, |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
impl GitConfigGet for bool { |
| 233 |
fn git_config_get(key: &str, git_config: &GitConfig) -> Option<Self> { |
| 234 |
match git_config.config_from_env_var.get(key).map(|s| s.as_str()) { |
| 235 |
Some("true") => Some(true), |
| 236 |
Some("false") => Some(false), |
| 237 |
_ => git_config.config.get_bool(key).ok(), |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
impl GitConfigGet for usize { |
| 243 |
fn git_config_get(key: &str, git_config: &GitConfig) -> Option<Self> { |
| 244 |
if let Some(s) = git_config.config_from_env_var.get(key) { |
| 245 |
if let Ok(n) = s.parse::<usize>() { |
| 246 |
return Some(n); |
| 247 |
} |
| 248 |
} |
| 249 |
match git_config.config.get_i64(key) { |
| 250 |
Ok(value) => Some(value as usize), |
| 251 |
_ => None, |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
impl GitConfigGet for f64 { |
| 257 |
fn git_config_get(key: &str, git_config: &GitConfig) -> Option<Self> { |
| 258 |
if let Some(s) = git_config.config_from_env_var.get(key) { |
| 259 |
if let Ok(n) = s.parse::<f64>() { |
| 260 |
return Some(n); |
| 261 |
} |
| 262 |
} |
| 263 |
match git_config.config.get_string(key) { |
| 264 |
Ok(value) => value.parse::<f64>().ok(), |
| 265 |
_ => None, |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
#[cfg(test)] |
| 271 |
mod tests { |
| 272 |
|
| 273 |
use super::parse_config_from_env_var_value; |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn test_parse_config_from_env_var_value() { |
| 277 |
// To generate test cases, use git -c ... with |
| 278 |
// [core] |
| 279 |
// pager = env | grep GIT_CONFIG_PARAMETERS |
| 280 |
|
| 281 |
// We test multiple formats because the format of the value stored by |
| 282 |
// git in this environment variable has changed in recent versions of |
| 283 |
// Git. See |
| 284 |
// https://github.com/git/git/blob/311531c9de557d25ac087c1637818bd2aad6eb3a/Documentation/RelNotes/2.31.0.txt#L127-L130 |
| 285 |
|
| 286 |
for env_var_value in &["'user.name=xxx'", "'user.name'='xxx'"] { |
| 287 |
let config = parse_config_from_env_var_value(env_var_value); |
| 288 |
assert!(config.is_empty()); |
| 289 |
} |
| 290 |
|
| 291 |
for env_var_value in &["'delta.plus-style=green'", "'delta.plus-style'='green'"] { |
| 292 |
let config = parse_config_from_env_var_value(env_var_value); |
| 293 |
assert_eq!(config["delta.plus-style"], "green"); |
| 294 |
} |
| 295 |
|
| 296 |
for env_var_value in &[ |
| 297 |
r##"'user.name=xxx' 'delta.hunk-header-line-number-style=red "#067a00"'"##, |
| 298 |
r##"'user.name'='xxx' 'delta.hunk-header-line-number-style'='red "#067a00"'"##, |
| 299 |
] { |
| 300 |
let config = parse_config_from_env_var_value(env_var_value); |
| 301 |
assert_eq!( |
| 302 |
config["delta.hunk-header-line-number-style"], |
| 303 |
r##"red "#067a00""## |
| 304 |
); |
| 305 |
} |
| 306 |
|
| 307 |
for env_var_value in &[ |
| 308 |
r##"'user.name=xxx' 'delta.side-by-side=false'"##, |
| 309 |
r##"'user.name'='xxx' 'delta.side-by-side'='false'"##, |
| 310 |
] { |
| 311 |
let config = parse_config_from_env_var_value(env_var_value); |
| 312 |
assert_eq!(config["delta.side-by-side"], "false"); |
| 313 |
} |
| 314 |
|
| 315 |
for env_var_value in &[ |
| 316 |
r##"'delta.plus-style=green' 'delta.side-by-side=false' 'delta.hunk-header-line-number-style=red "#067a00"'"##, |
| 317 |
r##"'delta.plus-style'='green' 'delta.side-by-side'='false' 'delta.hunk-header-line-number-style'='red "#067a00"'"##, |
| 318 |
] { |
| 319 |
let config = parse_config_from_env_var_value(env_var_value); |
| 320 |
assert_eq!(config["delta.plus-style"], "green"); |
| 321 |
assert_eq!(config["delta.side-by-side"], "false"); |
| 322 |
assert_eq!( |
| 323 |
config["delta.hunk-header-line-number-style"], |
| 324 |
r##"red "#067a00""## |
| 325 |
); |
| 326 |
} |
| 327 |
} |
| 328 |
} |