summaryrefslogtreecommitdiffstats
path: root/src/context.rs
diff options
context:
space:
mode:
authorWalther Chen <hwchen@users.noreply.github.com>2020-07-05 13:22:14 -0400
committerGitHub <noreply@github.com>2020-07-05 13:22:14 -0400
commit489838e6a24ea1c08be6abe56d066724a1d59abd (patch)
tree8f813216bb1c5d0c096eeacce500e0fad555198d /src/context.rs
parent021d82a224ffe6d3bfc4802fe0178c2f0a92f113 (diff)
feat: git branch: read from HEAD on newly initialized repo (#1336)
* Git branch: read from HEAD on newly init repo On a newly initialized git repo, there are no branches created until a commit is made. Previously, starship handled this by having a default branch "master" for when branch `head` could not be read. However, if a user wants to set a different default branch name, that name won't appear on starship until a commit is made to the branch. If git2 provides a way to read the default branch name, we can use that, but at the moment it's not obvious how. For the moment, we can directly read `.git/HEAD`, which contains the name of the default branch head reference. This commit implements this strategy. Closes #1327 * update git_branch test from unborn master to unborn default * cargo fmt
Diffstat (limited to 'src/context.rs')
-rw-r--r--src/context.rs15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/context.rs b/src/context.rs
index 27feac0f7..17a73c688 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -321,8 +321,19 @@ fn get_current_branch(repository: &Repository) -> Option<String> {
Err(e) => {
return if e.code() == UnbornBranch {
// HEAD should only be an unborn branch if the repository is fresh,
- // in that case assume "master"
- Some(String::from("master"))
+ // in that case read directly from `.git/HEAD`
+ let mut head_path = repository.path().to_path_buf();
+ head_path.push("HEAD");
+
+ // get first line, then last path segment
+ fs::read_to_string(&head_path)
+ .ok()?
+ .lines()
+ .next()?
+ .trim()
+ .split('/')
+ .last()
+ .map(|r| r.to_owned())
} else {
None
};