diff --git a/src/modules/git_branch.rs b/src/modules/git_branch.rs index 0402e139..e6cadb81 100644 --- a/src/modules/git_branch.rs +++ b/src/modules/git_branch.rs @@ -27,12 +27,13 @@ pub fn module<'a>(context: &'a Context) -> Option> { let repo = context.get_repo().ok()?; - let repo_root = repo.root.as_ref()?; - let git_repo = Repository::open(repo_root).ok()?; - let is_detached = git_repo.head_detached().ok()?; - if config.only_attached && is_detached { - return None; - }; + if let Some(repo_root) = repo.root.as_ref() { + let git_repo = Repository::open(repo_root).ok()?; + let is_detached = git_repo.head_detached().ok()?; + if config.only_attached && is_detached { + return None; + } + } let branch_name = repo.branch.as_ref()?; let mut graphemes: Vec<&str> = branch_name.graphemes(true).collect(); @@ -343,6 +344,33 @@ mod tests { repo_dir.close() } + #[test] + fn test_works_in_bare_repo() -> io::Result<()> { + let repo_dir = tempfile::tempdir()?; + + Command::new("git") + .args(&["init", "--bare"]) + .current_dir(&repo_dir) + .output()?; + + Command::new("git") + .args(&["symbolic-ref", "HEAD", "refs/heads/main"]) + .current_dir(&repo_dir) + .output()?; + + let actual = ModuleRenderer::new("git_branch") + .path(&repo_dir.path()) + .collect(); + + let expected = Some(format!( + "on {} ", + Color::Purple.bold().paint(format!("\u{e0a0} {}", "main")), + )); + + assert_eq!(expected, actual); + repo_dir.close() + } + // This test is not possible until we switch to `git status --porcelain` // where we can mock the env for the specific git process. This is because // git2 does not care about our mocking and when we set the real `GIT_DIR`