From c1349bff0f49639ff5954b322d6c48fa95102a73 Mon Sep 17 00:00:00 2001 From: Ibrahim Rahhal Date: Sat, 20 Sep 2025 15:55:50 +0300 Subject: [PATCH] Improveing discovery, report errors better --- src/scanners/blast.rs | 17 ++++++++++++++--- src/utils/generic.rs | 28 +++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/scanners/blast.rs b/src/scanners/blast.rs index f743773..9066b28 100644 --- a/src/scanners/blast.rs +++ b/src/scanners/blast.rs @@ -20,9 +20,20 @@ pub fn run( out_format: Option, out_file: Option, ) { - if *only_uncommitted && !utils::generic::is_git_repo("./") { - eprintln!("This is not a git repository. Without a git repository Corgea CLI can't determine which files have been modified or added thus only a full scan is possible."); - std::process::exit(1); + if *only_uncommitted { + match utils::generic::is_git_repo("./") { + Ok(false) => { + eprintln!("This is not a git repository. Without a git repository Corgea CLI can't determine which files have been modified or added thus only a full scan is possible."); + std::process::exit(1); + }, + Err(e) => { + eprintln!("Error checking git repository information: {}. Without a git repository Corgea CLI can't determine which files have been modified or added thus only a full scan is possible.", e); + std::process::exit(1); + }, + Ok(true) => { + // Continue with the git repo logic + } + } } println!( "\nScanning with BLAST 🚀🚀🚀" diff --git a/src/utils/generic.rs b/src/utils/generic.rs index 6f87e7b..f3d566e 100644 --- a/src/utils/generic.rs +++ b/src/utils/generic.rs @@ -132,16 +132,26 @@ pub fn create_path_if_not_exists>(path: P) -> io::Result<()> { Ok(()) } -pub fn get_repo_path(dir: &str) -> Result { - let repo = match Repository::discover(dir) { - Ok(repo) => repo, - Err(e) => return Err(e), - }; - Ok(repo.path().to_string_lossy().to_string()) -} -pub fn is_git_repo(dir: &str) -> bool { - get_repo_path(dir).is_ok() +pub fn is_git_repo(dir: &str) -> Result { + let git_path = Path::new(dir).join(".git"); + if git_path.exists() { + return Ok(true); + } + + // Fall back to the more expensive discover method for cases like: + // - We're in a subdirectory of a git repo + // - .git is a file (worktrees, submodules) + match Repository::discover(dir) { + Ok(_) => Ok(true), + Err(e) => { + if e.code() == git2::ErrorCode::NotFound { + Ok(false) + } else { + Err(e) + } + } + } } pub fn delete_directory>(path: P) -> io::Result<()> {