diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-06-15 16:47:38 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-06-15 16:50:48 -0400 |
commit | 55e6a0135318a76d66428bc07268187ed2fabdaf (patch) | |
tree | 85a1a6a28bca619e500d1f74b3d2360b1e70f391 /src/git_config.rs | |
parent | acd79ba5c8b856288fa84382bcbbd94a92b31384 (diff) |
Read git config when we are not in a git repo
Diffstat (limited to 'src/git_config.rs')
-rw-r--r-- | src/git_config.rs | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/src/git_config.rs b/src/git_config.rs index 14de33b6..4d353f06 100644 --- a/src/git_config.rs +++ b/src/git_config.rs @@ -11,21 +11,23 @@ pub struct GitConfig { impl GitConfig { pub fn try_create() -> Option<Self> { - match std::env::current_dir() { - Ok(dir) => match git2::Repository::discover(dir) { - Ok(repo) => match repo.config() { - Ok(mut config) => { - let config = config.snapshot().unwrap_or_else(|err| { - eprintln!("Failed to read git config: {}", err); - process::exit(1) - }); - Some(Self { config }) - } - Err(_) => None, - }, - Err(_) => None, - }, - Err(_) => None, + let repo = match std::env::current_dir() { + Ok(dir) => git2::Repository::discover(dir).ok(), + _ => None, + }; + let config = match repo { + Some(repo) => repo.config().ok(), + None => git2::Config::open_default().ok(), + }; + match config { + Some(mut config) => { + let config = config.snapshot().unwrap_or_else(|err| { + eprintln!("Failed to read git config: {}", err); + process::exit(1) + }); + Some(Self { config }) + } + None => None, } } |