Skip to content

Commit

Permalink
Auto merge of #3604 - froydnj:rich-version-info, r=alexcrichton
Browse files Browse the repository at this point in the history
implement `cargo --version --verbose`

As suggested in #3584.  This is a bit underwhelming, and I'm unsure if some of the complexity in froydnj/cargo@775c900 is really warranted, but this series gets the job done.  Sample output when building with `configure` and `make`:

```
froydnj@hawkeye:~/src/cargo.git$ target/x86_64-unknown-linux-gnu/release/cargo --version
cargo-0.17.0-dev (ae4a4d8 2017-01-27)
froydnj@hawkeye:~/src/cargo.git$ target/x86_64-unknown-linux-gnu/release/cargo --version --verbose
cargo-0.17.0-dev (ae4a4d8 2017-01-27)
release: 0.17.0
commit-hash: ae4a4d8fc55bf7eca3d974f953dc61729e4a40db
commit-date: 2017-01-27
```
  • Loading branch information
bors committed Jan 29, 2017
2 parents 51ae993 + 50e1c1a commit 8d53838
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 21 deletions.
32 changes: 22 additions & 10 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,22 @@ include config.mk
export PATH := $(dir $(CFG_RUSTC)):$(PATH)

ifeq ($(CFG_RELEASE_CHANNEL),stable)
CFG_RELEASE=$(CFG_RELEASE_NUM)
CFG_PACKAGE_VERS=$(CFG_RELEASE_NUM)
else ifeq ($(CFG_RELEASE_CHANNEL),beta)
CFG_RELEASE=$(CFG_RELEASE_NUM)-beta$(CFG_PRERELEASE_VERSION)
CFG_PACKAGE_VERS=beta
else ifeq ($(CFG_RELEASE_CHANNEL),nightly)
CFG_RELEASE=$(CFG_RELEASE_NUM)-nightly
CFG_PACKAGE_VERS=nightly
else ifeq ($(CFG_RELEASE_CHANNEL),dev)
CFG_RELEASE=$(CFG_RELEASE_NUM)-dev
CFG_PACKAGE_VERS=$(CFG_RELEASE_NUM)-dev
endif

# allow build systems to use a constant date instead of the current one
CFG_BUILD_DATE = $(shell SOURCE_DATE_EPOCH="$${SOURCE_DATE_EPOCH:-$$(date +%s)}" ; date -u -d "@$$SOURCE_DATE_EPOCH" +%F 2>/dev/null || date -u -r "$$SOURCE_DATE_EPOCH" +%F 2>/dev/null || date -u +%F)

ifeq ($(wildcard $(CFG_SRC_DIR)/.git),)
CFG_VERSION = $(CFG_RELEASE) (built $(CFG_BUILD_DATE))
else
CFG_VER_DATE = $(shell git --git-dir='$(CFG_SRC_DIR).git' log -1 --date=short --pretty=format:'%cd')
CFG_VER_HASH = $(shell git --git-dir='$(CFG_SRC_DIR).git' rev-parse --short HEAD)
CFG_VERSION = $(CFG_RELEASE) ($(CFG_VER_HASH) $(CFG_VER_DATE))
ifneq ($(wildcard $(CFG_SRC_DIR)/.git),)
CFG_COMMIT_DATE = $(shell git --git-dir='$(CFG_SRC_DIR).git' log -1 --date=short --pretty=format:'%cd')
CFG_SHORT_COMMIT_HASH = $(shell git --git-dir='$(CFG_SRC_DIR).git' rev-parse --short HEAD)
CFG_COMMIT_HASH = $(shell git --git-dir='$(CFG_SRC_DIR).git' rev-parse HEAD)
endif
PKG_NAME = cargo-$(CFG_PACKAGE_VERS)

Expand Down Expand Up @@ -58,7 +52,25 @@ endif

S := $(CFG_SRC_DIR)/

CFG_RELEASE_PARTS := $(subst ., ,$(CFG_RELEASE_NUM))
CFG_VERSION_MAJOR := $(word 1,$(CFG_RELEASE_PARTS))
CFG_VERSION_MINOR := $(word 2,$(CFG_RELEASE_PARTS))
CFG_VERSION_PATCH := $(word 3,$(CFG_RELEASE_PARTS))

export CFG_VERSION
export CFG_VERSION_MAJOR
export CFG_VERSION_MINOR
export CFG_VERSION_PATCH
ifneq ($(CFG_PRERELEASE_VERSION),)
export CFG_PRERELEASE_VERSION
endif
ifneq ($(CFG_COMMIT_HASH),)
export CFG_COMMIT_HASH
export CFG_COMMIT_DATE
export CFG_SHORT_COMMIT_HASH
endif
export CFG_BUILD_DATE
export CFG_RELEASE_CHANNEL
export CFG_DISABLE_CROSS_TESTS

ifeq ($(OS),Windows_NT)
Expand Down
13 changes: 12 additions & 1 deletion src/bin/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,18 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
let _token = cargo::util::job::setup();

if flags.flag_version {
println!("{}", cargo::version());
let version = cargo::version();
println!("{}", version);
if flags.flag_verbose > 0{
println!("release: {}.{}.{}",
version.major, version.minor, version.patch);
if let Some(ref cfg) = version.cfg_info {
if let Some(ref ci) = cfg.commit_info {
println!("commit-hash: {}", ci.commit_hash);
println!("commit-date: {}", ci.commit_date);
}
}
}
return Ok(None)
}

Expand Down
107 changes: 98 additions & 9 deletions src/cargo/lib.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern crate toml;
extern crate url;

use std::env;
use std::fmt;
use std::io;
use rustc_serialize::{Decodable, Encodable};
use rustc_serialize::json;
Expand All @@ -49,6 +50,62 @@ pub mod ops;
pub mod sources;
pub mod util;

pub struct CommitInfo {
pub short_commit_hash: String,
pub commit_hash: String,
pub commit_date: String,
}

pub struct CfgInfo {
// Information about the git repository we may have been built from.
pub commit_info: Option<CommitInfo>,
// The date that the build was performed.
pub build_date: String,
// The release channel we were built for.
pub release_channel: String,
}

pub struct VersionInfo {
pub major: String,
pub minor: String,
pub patch: String,
pub pre_release: Option<String>,
// Information that's only available when we were built with
// configure/make, rather than cargo itself.
pub cfg_info: Option<CfgInfo>,
}

impl fmt::Display for VersionInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "cargo-{}.{}.{}",
self.major, self.minor, self.patch)?;
match self.cfg_info.as_ref().map(|ci| &ci.release_channel) {
Some(channel) => {
if channel != "stable" {
write!(f, "-{}", channel)?;
let empty = String::from("");
write!(f, "{}", self.pre_release.as_ref().unwrap_or(&empty))?;
}
},
None => (),
};

if let Some(ref cfg) = self.cfg_info {
match cfg.commit_info {
Some(ref ci) => {
write!(f, " ({} {})",
ci.short_commit_hash, ci.commit_date)?;
},
None => {
write!(f, " (built {})",
cfg.build_date)?;
}
}
};
Ok(())
}
}

pub fn execute_main_without_stdin<T, V>(
exec: fn(T, &Config) -> CliResult<Option<V>>,
options_first: bool,
Expand Down Expand Up @@ -208,15 +265,47 @@ fn handle_cause(mut cargo_err: &CargoError, shell: &mut MultiShell) -> bool {
}
}

pub fn version() -> String {
format!("cargo {}", match option_env!("CFG_VERSION") {
Some(s) => s.to_string(),
None => format!("{}.{}.{}{}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR"),
env!("CARGO_PKG_VERSION_PATCH"),
option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""))
})
pub fn version() -> VersionInfo {
macro_rules! env_str {
($name:expr) => { env!($name).to_string() }
}
macro_rules! option_env_str {
($name:expr) => { option_env!($name).map(|s| s.to_string()) }
}
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 {
major: option_env_str!("CFG_VERSION_MAJOR").unwrap(),
minor: option_env_str!("CFG_VERSION_MINOR").unwrap(),
patch: option_env_str!("CFG_VERSION_PATCH").unwrap(),
pre_release: option_env_str!("CFG_PRERELEASE_VERSION"),
cfg_info: Some(CfgInfo {
build_date: option_env_str!("CFG_BUILD_DATE").unwrap(),
release_channel: option_env_str!("CFG_RELEASE_CHANNEL").unwrap(),
commit_info: commit_info,
}),
}
},
// We are being compiled by Cargo itself.
None => {
VersionInfo {
major: env_str!("CARGO_PKG_VERSION_MAJOR"),
minor: env_str!("CARGO_PKG_VERSION_MINOR"),
patch: env_str!("CARGO_PKG_VERSION_PATCH"),
pre_release: option_env_str!("CARGO_PKG_VERSION_PRE"),
cfg_info: None,
}
}
}
}

fn flags_from_args<T>(usage: &str, args: &[String], options_first: bool) -> CliResult<T>
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub fn http_handle(config: &Config) -> CargoResult<Easy> {
handle.connect_timeout(Duration::new(30, 0))?;
handle.low_speed_limit(10 /* bytes per second */)?;
handle.low_speed_time(Duration::new(30, 0))?;
handle.useragent(&version())?;
handle.useragent(&version().to_string())?;
if let Some(proxy) = http_proxy(config)? {
handle.proxy(&proxy)?;
}
Expand Down

0 comments on commit 8d53838

Please sign in to comment.