use std::fs;
use std::path::Path;
use std::process::Command;
use crate::util::output;
use crate::util::t;
use crate::Build;
#[derive(Clone, Default)]
pub enum GitInfo {
#[default]
Absent,
Present(Option<Info>),
RecordedForTarball(Info),
}
#[derive(Clone)]
pub struct Info {
pub commit_date: String,
pub sha: String,
pub short_sha: String,
}
impl GitInfo {
pub fn new(omit_git_hash: bool, dir: &Path) -> GitInfo {
if !dir.join(".git").exists() {
match read_commit_info_file(dir) {
Some(info) => return GitInfo::RecordedForTarball(info),
None => return GitInfo::Absent,
}
}
match Command::new("git").arg("rev-parse").current_dir(dir).output() {
Ok(ref out) if out.status.success() => {}
_ => return GitInfo::Absent,
}
if omit_git_hash {
return GitInfo::Present(None);
}
let ver_date = output(
Command::new("git")
.current_dir(dir)
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--pretty=format:%cd"),
);
let ver_hash = output(Command::new("git").current_dir(dir).arg("rev-parse").arg("HEAD"));
let short_ver_hash = output(
Command::new("git").current_dir(dir).arg("rev-parse").arg("--short=9").arg("HEAD"),
);
GitInfo::Present(Some(Info {
commit_date: ver_date.trim().to_string(),
sha: ver_hash.trim().to_string(),
short_sha: short_ver_hash.trim().to_string(),
}))
}
pub fn info(&self) -> Option<&Info> {
match self {
GitInfo::Absent => None,
GitInfo::Present(info) => info.as_ref(),
GitInfo::RecordedForTarball(info) => Some(info),
}
}
pub fn sha(&self) -> Option<&str> {
self.info().map(|s| &s.sha[..])
}
pub fn sha_short(&self) -> Option<&str> {
self.info().map(|s| &s.short_sha[..])
}
pub fn commit_date(&self) -> Option<&str> {
self.info().map(|s| &s.commit_date[..])
}
pub fn version(&self, build: &Build, num: &str) -> String {
let mut version = build.release(num);
if let Some(ref inner) = self.info() {
version.push_str(" (");
version.push_str(&inner.short_sha);
version.push(' ');
version.push_str(&inner.commit_date);
version.push(')');
}
version
}
pub fn is_managed_git_subrepository(&self) -> bool {
match self {
GitInfo::Absent | GitInfo::RecordedForTarball(_) => false,
GitInfo::Present(_) => true,
}
}
pub fn is_from_tarball(&self) -> bool {
match self {
GitInfo::Absent | GitInfo::Present(_) => false,
GitInfo::RecordedForTarball(_) => true,
}
}
}
pub fn read_commit_info_file(root: &Path) -> Option<Info> {
if let Ok(contents) = fs::read_to_string(root.join("git-commit-info")) {
let mut lines = contents.lines();
let sha = lines.next();
let short_sha = lines.next();
let commit_date = lines.next();
let info = match (commit_date, sha, short_sha) {
(Some(commit_date), Some(sha), Some(short_sha)) => Info {
commit_date: commit_date.to_owned(),
sha: sha.to_owned(),
short_sha: short_sha.to_owned(),
},
_ => panic!("the `git-commit-info` file is malformed"),
};
Some(info)
} else {
None
}
}
pub fn write_commit_info_file(root: &Path, info: &Info) {
let commit_info = format!("{}\n{}\n{}\n", info.sha, info.short_sha, info.commit_date);
t!(fs::write(root.join("git-commit-info"), &commit_info));
}
pub fn write_commit_hash_file(root: &Path, sha: &str) {
t!(fs::write(root.join("git-commit-hash"), sha));
}