use gix::date::time::format::ISO8601_STRICT;
use std::collections::HashMap;
use std::path::Path;
pub fn fetch_repo_vars(dir: &Path) -> HashMap<String, String> {
let mut result: HashMap<String, String> = HashMap::new();
result.insert("commit_sha".to_string(), String::new());
result.insert("commit_sha_short".to_string(), String::new());
result.insert("commit_date".to_string(), String::new());
result.insert("commit_author_name".to_string(), String::new());
result.insert("commit_author_email".to_string(), String::new());
result.insert("commit_committer_name".to_string(), String::new());
result.insert("commit_committer_email".to_string(), String::new());
result.insert("branch".to_string(), String::new());
if let Ok(repo) = gix::discover(dir) {
if let Ok(head) = repo.head() {
if head.is_detached() {
result.insert("branch".to_string(), "none".to_string());
}
if let Some(head_id) = head.id() {
let long_sha = head_id.to_hex();
result.insert("commit_sha".to_string(), long_sha.to_string());
if let Ok(short_sha) = head_id.shorten() {
result.insert("commit_sha_short".to_string(), short_sha.to_string());
result.insert("branch".to_string(), short_sha.to_string());
}
if let Ok(head_obj) = head_id.object() {
if let Ok(commit) = head_obj.try_into_commit() {
if let Ok(commit_time) = commit.time() {
result.insert(
"commit_date".to_string(),
commit_time.format(ISO8601_STRICT),
);
}
if let Ok(commit_author) = commit.author() {
result.insert(
"commit_author_name".to_string(),
commit_author.name.to_string(),
);
result.insert(
"commit_author_email".to_string(),
commit_author.email.to_string(),
);
}
if let Ok(commit_committer) = commit.committer() {
result.insert(
"commit_committer_name".to_string(),
commit_committer.name.to_string(),
);
result.insert(
"commit_committer_email".to_string(),
commit_committer.email.to_string(),
);
}
}
}
}
}
if let Ok(Some(head_ref)) = repo.head_name() {
result.insert("branch".to_string(), head_ref.shorten().to_string());
}
}
result
}