Skip to content

Commit

Permalink
Merge pull request #347 from japaric/hash
Browse files Browse the repository at this point in the history
add commit hash to the output of -V
  • Loading branch information
brson committed Apr 21, 2016
2 parents cdd9322 + 213d809 commit d4988a6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ keywords = ["multirust", "install", "proxy"]

license = "MIT OR Apache-2.0"

build = "build.rs"

[dependencies]
rustup-dist = { path = "src/rustup-dist" }
rustup-utils = { path = "src/rustup-utils" }
Expand Down
51 changes: 51 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

struct Ignore;

impl<E> From<E> for Ignore
where E: Error
{
fn from(_: E) -> Ignore {
Ignore
}
}

fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

File::create(out_dir.join("commit-info.txt"))
.unwrap()
.write_all(commit_info().as_bytes())
.unwrap();
}

// Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
// (git not installed or if this is not a git repository) just return an empty string.
fn commit_info() -> String {
match (commit_hash(), commit_date()) {
(Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim_right(), date),
_ => String::new(),
}
}

fn commit_hash() -> Result<String, Ignore> {
Ok(try!(String::from_utf8(try!(Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output())
.stdout)))
}

fn commit_date() -> Result<String, Ignore> {
Ok(try!(String::from_utf8(try!(Command::new("git")
.args(&["log",
"-1",
"--date=short",
"--pretty=format:%cd"])
.output())
.stdout)))
}
2 changes: 1 addition & 1 deletion src/rustup-cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub fn list_overrides(cfg: &Cfg) -> Result<()> {


pub fn version() -> &'static str {
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
concat!(env!("CARGO_PKG_VERSION"), include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")))
}

fn split_override<T: FromStr>(s: &str, separator: char) -> Option<(T, T)> {
Expand Down

0 comments on commit d4988a6

Please sign in to comment.