Skip to content

Commit ac4a839

Browse files
authored
perf(git_status): avoid running in bare repos (starship#5581)
* fix: git_status bare repo handling * perform the git_status bare repo check earlier * Adjusted test
1 parent 5267c46 commit ac4a839

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::modules;
88
use crate::utils;
99
use clap::Parser;
1010
use gix::{
11+
repository::Kind,
1112
sec::{self as git_sec, trust::DefaultForLevel},
1213
state as git_state, Repository, ThreadSafeRepository,
1314
};
@@ -351,6 +352,7 @@ impl<'a> Context<'a> {
351352
state: repository.state(),
352353
remote,
353354
fs_monitor_value_is_true,
355+
kind: repository.kind(),
354356
})
355357
})
356358
}
@@ -641,6 +643,9 @@ pub struct Repo {
641643
/// Contains `true` if the value of `core.fsmonitor` is set to `true`.
642644
/// If not `true`, `fsmonitor` is explicitly disabled in git commands.
643645
fs_monitor_value_is_true: bool,
646+
647+
// Kind of repository, work tree or bare
648+
pub kind: Kind,
644649
}
645650

646651
impl Repo {

src/modules/git_status.rs

+20
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
3434
// Return None if not in git repository
3535
let repo = context.get_repo().ok()?;
3636

37+
if repo.kind.is_bare() {
38+
log::debug!("This is a bare repository, git_status is not applicable");
39+
return None;
40+
}
41+
3742
if let Some(git_status) = git_status_wsl(context, &config) {
3843
if git_status.is_empty() {
3944
return None;
@@ -1166,6 +1171,21 @@ mod tests {
11661171
repo_dir.close()
11671172
}
11681173

1174+
#[test]
1175+
fn doesnt_generate_git_status_for_bare_repo() -> io::Result<()> {
1176+
let repo_dir = fixture_repo(FixtureProvider::GitBare)?;
1177+
1178+
create_added(repo_dir.path())?;
1179+
1180+
let actual = ModuleRenderer::new("git_status")
1181+
.path(repo_dir.path())
1182+
.collect();
1183+
1184+
assert_eq!(None, actual);
1185+
1186+
repo_dir.close()
1187+
}
1188+
11691189
fn ahead(repo_dir: &Path) -> io::Result<()> {
11701190
File::create(repo_dir.join("readme.md"))?.sync_all()?;
11711191

src/test/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl<'a> ModuleRenderer<'a> {
166166
pub enum FixtureProvider {
167167
Fossil,
168168
Git,
169+
GitBare,
169170
Hg,
170171
Pijul,
171172
}
@@ -229,6 +230,16 @@ pub fn fixture_repo(provider: FixtureProvider) -> io::Result<TempDir> {
229230

230231
Ok(path)
231232
}
233+
FixtureProvider::GitBare => {
234+
let path = tempfile::tempdir()?;
235+
gix::ThreadSafeRepository::init(
236+
&path,
237+
gix::create::Kind::Bare,
238+
gix::create::Options::default(),
239+
)
240+
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
241+
Ok(path)
242+
}
232243
FixtureProvider::Hg => {
233244
let path = tempfile::tempdir()?;
234245

0 commit comments

Comments
 (0)