From 1d3cd3a1abd1755177cc94e5e263ed785203c937 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 6 Nov 2020 11:14:47 +0100 Subject: Fix: Implementation of get_repo_head_commit() Signed-off-by: Matthias Beyer --- src/util/git.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/util/git.rs b/src/util/git.rs index 30b2245..3926c14 100644 --- a/src/util/git.rs +++ b/src/util/git.rs @@ -1,6 +1,8 @@ use std::path::Path; +use anyhow::anyhow; use anyhow::Result; +use anyhow::Context; use anyhow::Error; use git2::Repository; @@ -11,12 +13,17 @@ pub fn repo_is_clean(p: &Path) -> Result { } pub fn get_repo_head_commit_hash(p: &Path) -> Result { - let r = Repository::open(p)?; - let hash = r.head()? - .peel(git2::ObjectType::Commit)? - .id() - .as_bytes() - .to_vec(); + let r = Repository::open(p) + .with_context(|| anyhow!("Opening repository at {}", p.display()))?; - String::from_utf8(hash).map_err(Error::from) + let s = r.head() + .with_context(|| anyhow!("Getting HEAD from repository at {}", p.display()))? + .shorthand() + .ok_or_else(|| { + anyhow!("Failed to get commit hash: Not valid UTF8") + })? + .to_owned(); + + trace!("Found git commit hash = {}", s); + Ok(s) } -- cgit v1.2.3