Use local git info for version.

This commit is contained in:
Eric Huss 2022-01-24 15:28:04 -08:00
parent 58790d3b5e
commit 295ea6d940
4 changed files with 50 additions and 44 deletions

View File

@ -2,8 +2,10 @@ use flate2::{Compression, GzBuilder};
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::process::Command;
fn main() {
commit_info();
compress_man();
println!(
"cargo:rustc-env=RUST_HOST_TARGET={}",
@ -41,3 +43,25 @@ fn compress_man() {
let encoder = ar.into_inner().unwrap();
encoder.finish().unwrap();
}
fn commit_info() {
if !Path::new(".git").exists() {
return;
}
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.output()
{
Ok(output) if output.status.success() => output,
_ => return,
};
let stdout = String::from_utf8(output.stdout).unwrap();
let mut parts = stdout.split_whitespace();
let mut next = || parts.next().unwrap();
println!("cargo:rustc-env=CARGO_COMMIT_HASH={}", next());
println!("cargo:rustc-env=CARGO_COMMIT_SHORT_HASH={}", next());
println!("cargo:rustc-env=CARGO_COMMIT_DATE={}", next())
}

View File

@ -169,12 +169,10 @@ pub fn get_version_string(is_verbose: bool) -> String {
let version = cargo::version();
let mut version_string = format!("cargo {}\n", version);
if is_verbose {
version_string.push_str(&format!("release: {}\n", version.version,));
if let Some(ref cfg) = version.cfg_info {
if let Some(ref ci) = cfg.commit_info {
version_string.push_str(&format!("commit-hash: {}\n", ci.commit_hash));
version_string.push_str(&format!("commit-date: {}\n", ci.commit_date));
}
version_string.push_str(&format!("release: {}\n", version.version));
if let Some(ref ci) = version.commit_info {
version_string.push_str(&format!("commit-hash: {}\n", ci.commit_hash));
version_string.push_str(&format!("commit-date: {}\n", ci.commit_date));
}
writeln!(version_string, "host: {}", env!("RUST_HOST_TARGET")).unwrap();
add_libgit2(&mut version_string);

View File

@ -993,7 +993,6 @@ pub fn channel() -> String {
}
}
crate::version()
.cfg_info
.map(|c| c.release_channel)
.release_channel
.unwrap_or_else(|| String::from("dev"))
}

View File

@ -9,31 +9,26 @@ pub struct CommitInfo {
pub commit_date: String,
}
/// Information provided by the outer build system (rustbuild aka bootstrap).
pub struct CfgInfo {
/// Information about the Git repository we may have been built from.
pub commit_info: Option<CommitInfo>,
/// The release channel we were built for (stable/beta/nightly/dev).
pub release_channel: String,
}
/// Cargo's version.
pub struct VersionInfo {
/// Cargo's version, such as "1.57.0", "1.58.0-beta.1", "1.59.0-nightly", etc.
pub version: String,
/// Information that's only available when we were built with
/// rustbuild, rather than Cargo itself.
pub cfg_info: Option<CfgInfo>,
/// The release channel we were built for (stable/beta/nightly/dev).
///
/// `None` if not built via rustuild.
pub release_channel: Option<String>,
/// Information about the Git repository we may have been built from.
///
/// `None` if not built from a git repo.
pub commit_info: Option<CommitInfo>,
}
impl fmt::Display for VersionInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.version)?;
if let Some(ref cfg) = self.cfg_info {
if let Some(ref ci) = cfg.commit_info {
write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?;
}
if let Some(ref ci) = self.commit_info {
write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?;
};
Ok(())
}
@ -70,26 +65,16 @@ pub fn version() -> VersionInfo {
format!("1.{}.{}", minor, patch)
});
match option_env!("CFG_RELEASE_CHANNEL") {
// We have environment variables set up from configure/make.
Some(_) => {
let commit_info = option_env!("CFG_COMMIT_HASH").map(|s| CommitInfo {
commit_hash: s.to_string(),
short_commit_hash: option_env_str!("CFG_SHORT_COMMIT_HASH").unwrap(),
commit_date: option_env_str!("CFG_COMMIT_DATE").unwrap(),
});
VersionInfo {
version,
cfg_info: Some(CfgInfo {
release_channel: option_env_str!("CFG_RELEASE_CHANNEL").unwrap(),
commit_info,
}),
}
}
// We are being compiled by Cargo itself.
None => VersionInfo {
version,
cfg_info: None,
},
let release_channel = option_env_str!("CFG_RELEASE_CHANNEL");
let commit_info = option_env_str!("CARGO_COMMIT_HASH").map(|commit_hash| CommitInfo {
short_commit_hash: option_env_str!("CARGO_COMMIT_SHORT_HASH").unwrap(),
commit_hash,
commit_date: option_env_str!("CARGO_COMMIT_DATE").unwrap(),
});
VersionInfo {
version,
release_channel,
commit_info,
}
}