Skip to content

Commit

Permalink
fix(git_branch): Make Git branch module support bare repositories (st…
Browse files Browse the repository at this point in the history
…arship#2522)

Co-authored-by: Kim Christensen <[email protected]>
  • Loading branch information
kichristensen and kichristensen authored Apr 1, 2021
1 parent cba98bd commit 70b397a
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/modules/git_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {

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();
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 70b397a

Please sign in to comment.