summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-11-06 11:14:47 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-11-06 13:27:12 +0100
commit1d3cd3a1abd1755177cc94e5e263ed785203c937 (patch)
treed2a9baf8e59f26dc50a23abc341cb5fa57058513 /src/util
parentee54d7c98a6d6f769298e54dad6af1e70697503c (diff)
Fix: Implementation of get_repo_head_commit()
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/util')
-rw-r--r--src/util/git.rs21
1 files 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<bool> {
}
pub fn get_repo_head_commit_hash(p: &Path) -> Result<String> {
- 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)
}