From 1c77814f2c7a799d262c49389cf5f3560cde3fe6 Mon Sep 17 00:00:00 2001 From: iamkroot Date: Sun, 30 Oct 2022 13:33:54 +0530 Subject: [PATCH] git: Use open_from_env before discover This matches git behaviour --- src/fs/feature/git.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/fs/feature/git.rs b/src/fs/feature/git.rs index adc9a66bf..0e36012e4 100644 --- a/src/fs/feature/git.rs +++ b/src/fs/feature/git.rs @@ -162,18 +162,23 @@ impl GitRepo { /// Returns the original buffer if none is found. fn discover(path: PathBuf) -> Result { info!("Searching for Git repository above {:?}", path); - let repo = match git2::Repository::discover(&path) { + // Search with GIT_DIR env variable first if set + let repo = match git2::Repository::open_from_env() { Ok(r) => r, Err(e) => { - error!("Error discovering Git repositories: {:?}", e); - match git2::Repository::open_from_env() { - Ok(r) => r, - Err(e) => { - // anything other than NotFound implies GIT_DIR was set and we got actual error - if e.code() != git2::ErrorCode::NotFound { - error!("Error opening Git repo from env using GIT_DIR: {:?}", e); + // anything other than NotFound implies GIT_DIR was set and we got actual error + if e.code() != git2::ErrorCode::NotFound { + error!("Error opening Git repo from env using GIT_DIR: {:?}", e); + return Err(path); + } else { + // nothing found, search using discover + match git2::Repository::discover(&path) { + Ok(r) => r, + Err(e) => { + error!("Error discovering Git repositories: {:?}", e); + return Err(path); + } - return Err(path); } } }