diff --git a/src/git.rs b/src/git.rs index 9137345..37c8bfe 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,6 +1,7 @@ use crate::build::{ConstType, ConstVal, ShadowConst}; use crate::ci::CiType; use crate::err::*; +use crate::{DateTime, Format}; use std::collections::BTreeMap; use std::io::{BufReader, Read}; use std::path::Path; @@ -104,13 +105,10 @@ impl Git { } fn init(&mut self, path: &Path, std_env: &BTreeMap) -> SdResult<()> { - // check git status - let x = command_git_clean(); - self.update_bool(GIT_CLEAN, x); - - let x = command_git_status_file(); - self.update_str(GIT_STATUS_FILE, x); + // First, try executing using the git command. + self.init_git()?; + // If the git2 feature is enabled, then replace the corresponding values with git2. self.init_git2(path)?; // use command branch @@ -133,6 +131,30 @@ impl Git { Ok(()) } + fn init_git(&mut self) -> SdResult<()> { + // check git status + let x = command_git_clean(); + self.update_bool(GIT_CLEAN, x); + + let x = command_git_status_file(); + self.update_str(GIT_STATUS_FILE, x); + + let git_info = command_git_head(); + + self.update_str(COMMIT_EMAIL, git_info.email); + self.update_str(COMMIT_AUTHOR, git_info.author); + self.update_str(SHORT_COMMIT, git_info.short_commit); + self.update_str(COMMIT_HASH, git_info.commit); + + let time_stamp = git_info.date.parse::()?; + let date_time = DateTime::timestamp_2_utc(time_stamp); + self.update_str(COMMIT_DATE, date_time.human_format()); + self.update_str(COMMIT_DATE_2822, date_time.to_rfc2822()); + self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339()); + + Ok(()) + } + #[allow(unused_variables)] fn init_git2(&mut self, path: &Path) -> SdResult<()> { #[cfg(feature = "git2")] @@ -322,7 +344,7 @@ pub mod git2_mod { use std::path::Path; pub fn git_repo>(path: P) -> Result { - git2::Repository::discover(path) + Repository::discover(path) } pub fn git2_current_branch(repo: &Repository) -> Option { @@ -398,6 +420,33 @@ pub fn git_status_file() -> String { } } +struct GitHeadInfo { + commit: String, + short_commit: String, + email: String, + author: String, + date: String, +} + +fn command_git_head() -> GitHeadInfo { + let cli = |args: &[&str]| { + Command::new("git") + .args(args) + .output() + .map(|x| String::from_utf8(x.stdout).ok()) + .map(|x| x.map(|x| x.trim().to_string())) + .unwrap_or_default() + .unwrap_or_default() + }; + GitHeadInfo { + commit: cli(&["rev-parse", "HEAD"]), + short_commit: cli(&["rev-parse", "--short", "HEAD"]), + author: cli(&["log", "-1", "--pretty=format:%an"]), + email: cli(&["log", "-1", "--pretty=format:%ae"]), + date: cli(&["show", "--pretty=format:%ct", "--date=raw", "-s"]), + } +} + /// Command exec git current tag fn command_current_tag() -> Option { Command::new("git")