From 295ea6d940f3c29b1f2f29a992acfb64186a829b Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 24 Jan 2022 15:28:04 -0800 Subject: [PATCH] Use local git info for version. --- build.rs | 24 ++++++++++++++++ src/bin/cargo/cli.rs | 10 +++---- src/cargo/core/features.rs | 3 +- src/cargo/version.rs | 57 ++++++++++++++------------------------ 4 files changed, 50 insertions(+), 44 deletions(-) diff --git a/build.rs b/build.rs index 68865b58fcf..33d664ce825 100644 --- a/build.rs +++ b/build.rs @@ -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()) +} diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 58dcd183ca9..879e898d5a9 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -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); diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index 2d563773075..61452542a51 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -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")) } diff --git a/src/cargo/version.rs b/src/cargo/version.rs index 6dbb9d1c100..9829fd72e74 100644 --- a/src/cargo/version.rs +++ b/src/cargo/version.rs @@ -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, - /// 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, + /// The release channel we were built for (stable/beta/nightly/dev). + /// + /// `None` if not built via rustuild. + pub release_channel: Option, + /// Information about the Git repository we may have been built from. + /// + /// `None` if not built from a git repo. + pub commit_info: Option, } 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, } }