Skip to content

Commit

Permalink
perf(git_status): avoid running in bare repos (#5581)
Browse files Browse the repository at this point in the history
* fix: git_status bare repo handling

* perform the git_status bare repo check earlier

* Adjusted test
  • Loading branch information
wjhoward committed Nov 29, 2023
1 parent 5267c46 commit ac4a839
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/context.rs
Expand Up @@ -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,
};
Expand Down Expand Up @@ -351,6 +352,7 @@ impl<'a> Context<'a> {
state: repository.state(),
remote,
fs_monitor_value_is_true,
kind: repository.kind(),
})
})
}
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions src/modules/git_status.rs
Expand Up @@ -34,6 +34,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
// 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;
Expand Down Expand Up @@ -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()?;

Expand Down
11 changes: 11 additions & 0 deletions src/test/mod.rs
Expand Up @@ -166,6 +166,7 @@ impl<'a> ModuleRenderer<'a> {
pub enum FixtureProvider {
Fossil,
Git,
GitBare,
Hg,
Pijul,
}
Expand Down Expand Up @@ -229,6 +230,16 @@ pub fn fixture_repo(provider: FixtureProvider) -> io::Result<TempDir> {

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()?;

Expand Down

0 comments on commit ac4a839

Please sign in to comment.