From ac4a83910357d69950ca304a3fb41d1d39bc3592 Mon Sep 17 00:00:00 2001 From: William Howard Date: Wed, 29 Nov 2023 06:26:35 +0000 Subject: [PATCH] perf(git_status): avoid running in bare repos (#5581) * fix: git_status bare repo handling * perform the git_status bare repo check earlier * Adjusted test --- src/context.rs | 5 +++++ src/modules/git_status.rs | 20 ++++++++++++++++++++ src/test/mod.rs | 11 +++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/context.rs b/src/context.rs index 0a498c99..56074469 100644 --- a/src/context.rs +++ b/src/context.rs @@ -8,6 +8,7 @@ use crate::modules; use crate::utils; use clap::Parser; use gix::{ + repository::Kind, sec::{self as git_sec, trust::DefaultForLevel}, state as git_state, Repository, ThreadSafeRepository, }; @@ -351,6 +352,7 @@ impl<'a> Context<'a> { state: repository.state(), remote, fs_monitor_value_is_true, + kind: repository.kind(), }) }) } @@ -641,6 +643,9 @@ pub struct Repo { /// Contains `true` if the value of `core.fsmonitor` is set to `true`. /// If not `true`, `fsmonitor` is explicitly disabled in git commands. fs_monitor_value_is_true: bool, + + // Kind of repository, work tree or bare + pub kind: Kind, } impl Repo { diff --git a/src/modules/git_status.rs b/src/modules/git_status.rs index e54ba700..614e6f66 100644 --- a/src/modules/git_status.rs +++ b/src/modules/git_status.rs @@ -34,6 +34,11 @@ pub fn module<'a>(context: &'a Context) -> Option> { // Return None if not in git repository let repo = context.get_repo().ok()?; + if repo.kind.is_bare() { + log::debug!("This is a bare repository, git_status is not applicable"); + return None; + } + if let Some(git_status) = git_status_wsl(context, &config) { if git_status.is_empty() { return None; @@ -1166,6 +1171,21 @@ mod tests { repo_dir.close() } + #[test] + fn doesnt_generate_git_status_for_bare_repo() -> io::Result<()> { + let repo_dir = fixture_repo(FixtureProvider::GitBare)?; + + create_added(repo_dir.path())?; + + let actual = ModuleRenderer::new("git_status") + .path(repo_dir.path()) + .collect(); + + assert_eq!(None, actual); + + repo_dir.close() + } + fn ahead(repo_dir: &Path) -> io::Result<()> { File::create(repo_dir.join("readme.md"))?.sync_all()?; diff --git a/src/test/mod.rs b/src/test/mod.rs index dd26ea82..9cbb749d 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -166,6 +166,7 @@ impl<'a> ModuleRenderer<'a> { pub enum FixtureProvider { Fossil, Git, + GitBare, Hg, Pijul, } @@ -229,6 +230,16 @@ pub fn fixture_repo(provider: FixtureProvider) -> io::Result { Ok(path) } + FixtureProvider::GitBare => { + let path = tempfile::tempdir()?; + gix::ThreadSafeRepository::init( + &path, + gix::create::Kind::Bare, + gix::create::Options::default(), + ) + .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; + Ok(path) + } FixtureProvider::Hg => { let path = tempfile::tempdir()?;