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
This commit is contained in:
William Howard 2023-11-29 06:26:35 +00:00 committed by GitHub
parent 5267c464eb
commit ac4a839103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 0 deletions

View File

@ -8,6 +8,7 @@ use crate::modules;
use crate::utils; use crate::utils;
use clap::Parser; use clap::Parser;
use gix::{ use gix::{
repository::Kind,
sec::{self as git_sec, trust::DefaultForLevel}, sec::{self as git_sec, trust::DefaultForLevel},
state as git_state, Repository, ThreadSafeRepository, state as git_state, Repository, ThreadSafeRepository,
}; };
@ -351,6 +352,7 @@ impl<'a> Context<'a> {
state: repository.state(), state: repository.state(),
remote, remote,
fs_monitor_value_is_true, 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`. /// Contains `true` if the value of `core.fsmonitor` is set to `true`.
/// If not `true`, `fsmonitor` is explicitly disabled in git commands. /// If not `true`, `fsmonitor` is explicitly disabled in git commands.
fs_monitor_value_is_true: bool, fs_monitor_value_is_true: bool,
// Kind of repository, work tree or bare
pub kind: Kind,
} }
impl Repo { impl Repo {

View File

@ -34,6 +34,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
// Return None if not in git repository // Return None if not in git repository
let repo = context.get_repo().ok()?; 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 let Some(git_status) = git_status_wsl(context, &config) {
if git_status.is_empty() { if git_status.is_empty() {
return None; return None;
@ -1166,6 +1171,21 @@ mod tests {
repo_dir.close() 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<()> { fn ahead(repo_dir: &Path) -> io::Result<()> {
File::create(repo_dir.join("readme.md"))?.sync_all()?; File::create(repo_dir.join("readme.md"))?.sync_all()?;

View File

@ -166,6 +166,7 @@ impl<'a> ModuleRenderer<'a> {
pub enum FixtureProvider { pub enum FixtureProvider {
Fossil, Fossil,
Git, Git,
GitBare,
Hg, Hg,
Pijul, Pijul,
} }
@ -229,6 +230,16 @@ pub fn fixture_repo(provider: FixtureProvider) -> io::Result<TempDir> {
Ok(path) 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 => { FixtureProvider::Hg => {
let path = tempfile::tempdir()?; let path = tempfile::tempdir()?;