Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
34 lines
836 B
Rust
34 lines
836 B
Rust
use std::env::current_dir;
|
|
|
|
use git2::Repository;
|
|
|
|
#[derive(Debug)]
|
|
pub struct GitCommit {
|
|
pub commit_sha: String,
|
|
}
|
|
|
|
impl GitCommit {
|
|
pub fn new() -> anyhow::Result<GitCommit> {
|
|
let repo = Repository::open(current_dir().expect("having current_dir available")).map_err(
|
|
|e| {
|
|
log::debug!("{}", e);
|
|
anyhow::anyhow!("could not open repository")
|
|
},
|
|
)?;
|
|
let head_ref = repo
|
|
.head()
|
|
.map_err(|e| {
|
|
log::warn!("{}", e);
|
|
anyhow::anyhow!("could not get HEAD")
|
|
})?
|
|
.target()
|
|
.ok_or(anyhow::anyhow!(
|
|
"could not extract head -> target to commit_sha"
|
|
))?;
|
|
|
|
Ok(Self {
|
|
commit_sha: head_ref.to_string(),
|
|
})
|
|
}
|
|
}
|