use std::env::current_dir; use git2::Repository; #[derive(Debug)] pub struct GitCommit { pub commit_sha: String, } impl GitCommit { pub fn new() -> anyhow::Result { 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(), }) } }