From 8d90baf0eb299710585c12bb9629b20fd696c3ff Mon Sep 17 00:00:00 2001 From: Hendrik Rombach Date: Tue, 7 Apr 2020 12:01:40 +0200 Subject: [PATCH] fix: implement fallback branch_name for bare git repos (#1035) closes #686 --- src/modules/git_branch.rs | 5 ++++- src/modules/git_status.rs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/modules/git_branch.rs b/src/modules/git_branch.rs index 56414b19..4b58c33d 100644 --- a/src/modules/git_branch.rs +++ b/src/modules/git_branch.rs @@ -30,7 +30,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { }; let repo = context.get_repo().ok()?; - let branch_name = repo.branch.as_ref()?; + // bare repos don't have a branch name, so `repo.branch.as_ref` would return None, + // but git treats "master" as the default branch name + let default_branch = String::from("master"); + let branch_name = repo.branch.as_ref().unwrap_or(&default_branch); let truncated_graphemes = get_graphemes(&branch_name, len); // The truncation symbol should only be added if we truncated let truncated_and_symbol = if len < graphemes_len(&branch_name) { diff --git a/src/modules/git_status.rs b/src/modules/git_status.rs index 0778bbce..b689b070 100644 --- a/src/modules/git_status.rs +++ b/src/modules/git_status.rs @@ -23,7 +23,10 @@ use std::collections::HashMap; /// - `✘` — A file's deletion has been added to the staging area pub fn module<'a>(context: &'a Context) -> Option> { let repo = context.get_repo().ok()?; - let branch_name = repo.branch.as_ref()?; + // bare repos don't have a branch name, so `repo.branch.as_ref` would return None, + // but git treats "master" as the default branch name + let default_branch = String::from("master"); + let branch_name = repo.branch.as_ref().unwrap_or(&default_branch); let repo_root = repo.root.as_ref()?; let mut repository = Repository::open(repo_root).ok()?;