summaryrefslogtreecommitdiffstats
path: root/src/context.rs
diff options
context:
space:
mode:
authorHendrik Rombach <h.rombach1@gmail.com>2020-05-06 11:19:53 +0200
committerGitHub <noreply@github.com>2020-05-06 11:19:53 +0200
commit108193103d45d2ebf3bbaab577ffb6ce83133eac (patch)
tree8d48c8c4c41f91ab56eb7c448946377a7b6e8e9b /src/context.rs
parent4607e21fa6fa7165a8c820452e9ed214c6bd93c7 (diff)
fix(git): show branch name in fresh repo (unborn master) (#1093)
* fix: branch_name in fresh repo (unborn master) * test: add test for unborn branch_name fix
Diffstat (limited to 'src/context.rs')
-rw-r--r--src/context.rs16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/context.rs b/src/context.rs
index bd8579588..049306e5c 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -3,7 +3,7 @@ use crate::module::Module;
use crate::modules;
use clap::ArgMatches;
-use git2::{Repository, RepositoryState};
+use git2::{ErrorCode::UnbornBranch, Repository, RepositoryState};
use once_cell::sync::OnceCell;
use std::collections::{HashMap, HashSet};
use std::env;
@@ -312,7 +312,19 @@ impl<'a> ScanDir<'a> {
}
fn get_current_branch(repository: &Repository) -> Option<String> {
- let head = repository.head().ok()?;
+ let head = match repository.head() {
+ Ok(reference) => reference,
+ 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"))
+ } else {
+ None
+ };
+ }
+ };
+
let shorthand = head.shorthand();
shorthand.map(std::string::ToString::to_string)