From 9f4919e9af897c48c292c5646ab1b122ac922d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Katona?= Date: Mon, 29 Aug 2022 09:10:54 +0200 Subject: [PATCH] fix status crash with empty head repo --- src/vcs/project.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/vcs/project.rs b/src/vcs/project.rs index e0b990d..27c6fd0 100644 --- a/src/vcs/project.rs +++ b/src/vcs/project.rs @@ -67,9 +67,25 @@ impl Project { &self.path } + pub fn current_ref(&self) -> Result, Error> { + match self.repo.current_ref() { + Ok(val) => Ok(Some(val)), + Err(err) => { + eprintln!("{}: {}\n", self.name(), err); + Ok(None) + } + } + } + pub fn current_profile(&self) -> Result, Error> { - let current_ref = self.repo.current_ref()?; - if let Some(val) = self.profile_map.iter().find(|v| v.1 == ¤t_ref) { + // repo.current_ref can fail if repo has no head yet (e.g. post init) + let current_ref = self.current_ref()?; + + if let Some(val) = self + .profile_map + .iter() + .find(|v| Some(v.1) == current_ref.as_ref()) + { return Ok(Some(val.0.clone())); } @@ -127,7 +143,9 @@ impl Project { let title = format!( "{}: {} [{}]", self.name().bold().with(PROJECT_COLOR), - self.repo.current_ref()?.with(REFS_COLOR), + self.current_ref()? + .unwrap_or_else(|| "???".into()) + .with(REFS_COLOR), format!("{}", self.path().display()).italic(), ); let mut root = Tree::new(title);