Skip to content

Commit

Permalink
Auto merge of #10323 - ehuss:local-git-info, r=alexcrichton
Browse files Browse the repository at this point in the history
Use local git info for version.

#10178 caused an unintended change where cargo is being built twice in rust-lang/rust's CI.  It is being built once as a CLI, and a second time for RLS.  The cause is the `CFG_COMMIT_HASH` environment variable changes between those two builds (it is set for the tool being built).

The solution here is to grab the git information from cargo's own build script. This is guaranteed to always be in the `src/tools/cargo` directory for both tools.

This should help save a minute or two in the dist builders.
  • Loading branch information
bors committed Jan 25, 2022
2 parents 9978fd7 + 295ea6d commit b5e2e39
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
24 changes: 24 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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={}",
Expand Down Expand Up @@ -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())
}
10 changes: 4 additions & 6 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
57 changes: 21 additions & 36 deletions src/cargo/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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,
}
}

0 comments on commit b5e2e39

Please sign in to comment.