Simplify --version info

This commit is contained in:
Nick Cameron 2017-05-25 16:39:44 +12:00
parent 393ba27311
commit a7b8dcc60d
4 changed files with 30 additions and 72 deletions

22
Cargo.lock generated
View file

@ -18,7 +18,6 @@ dependencies = [
"term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -123,15 +122,6 @@ name = "rustc-serialize"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "same-file"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde"
version = "1.0.7"
@ -289,16 +279,6 @@ name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "walkdir"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.2.8"
@ -326,7 +306,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b"
"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db"
"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
"checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"
"checksum serde 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c0c3d79316a6051231925504f6ef893d45088e8823c77a8331a3dcf427ee9087"
"checksum serde_derive 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0019cd5b9f0529a1a0e145a912e9a2d60c325c58f7f260fc36c71976e9d76aee"
"checksum serde_derive_internals 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "021c338d22c7e30f957a6ab7e388cb6098499dda9fd4ba1661ee074ca7a180d1"
@ -346,6 +325,5 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

View file

@ -34,9 +34,6 @@ log = "0.3"
env_logger = "0.4"
getopts = "0.2"
[build-dependencies]
walkdir = "1.0.3"
[target.'cfg(unix)'.dependencies]
libc = "0.2.11"

View file

@ -1,57 +1,50 @@
extern crate walkdir;
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use walkdir::WalkDir;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("git_info.rs");
let mut f = File::create(&dest_path).unwrap();
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
writeln!(f,
"const COMMIT_HASH: Option<&'static str> = {:?};",
git_head_sha1())
.unwrap();
writeln!(f,
"const WORKTREE_CLEAN: Option<bool> = {:?};",
git_tree_is_clean())
File::create(out_dir.join("commit-info.txt"))
.unwrap()
.write_all(commit_info().as_bytes())
.unwrap();
}
// cargo:rerun-if-changed requires one entry per individual file.
for entry in WalkDir::new("src") {
let entry = entry.unwrap();
println!("cargo:rerun-if-changed={}", entry.path().display());
// 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()) {
(Some(hash), Some(date)) => format!(" ({} {})", hash.trim_right(), date),
_ => String::new(),
}
}
// Returns `None` if git is not available.
fn git_head_sha1() -> Option<String> {
fn commit_hash() -> Option<String> {
Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("HEAD")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|mut s| {
let len = s.trim_right().len();
s.truncate(len);
s
})
.and_then(|r| String::from_utf8(r.stdout).ok())
}
// Returns `None` if git is not available.
fn git_tree_is_clean() -> Option<bool> {
fn commit_date() -> Option<String> {
Command::new("git")
.arg("status")
.arg("--porcelain")
.arg("--untracked-files=no")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()
.ok()
.map(|o| o.stdout.is_empty())
.and_then(|r| String::from_utf8(r.stdout).ok())
}

View file

@ -29,12 +29,6 @@ use std::str::FromStr;
use getopts::{Matches, Options};
// Include git commit hash and worktree status; contents are like
// const COMMIT_HASH: Option<&'static str> = Some("c31a366");
// const WORKTREE_CLEAN: Option<bool> = Some(false);
// with `None` if running git failed, eg if it is not installed.
include!(concat!(env!("OUT_DIR"), "/git_info.rs"));
type FmtError = Box<error::Error + Send + Sync>;
type FmtResult<T> = std::result::Result<T, FmtError>;
@ -365,13 +359,9 @@ fn print_usage(opts: &Options, reason: &str) {
}
fn print_version() {
println!("{} ({}{})",
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"),
COMMIT_HASH.unwrap_or("git commit unavailable"),
match WORKTREE_CLEAN {
Some(false) => " worktree dirty",
_ => "",
});
println!("{}-nightly{}",
env!("CARGO_PKG_VERSION"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")))
}
fn determine_operation(matches: &Matches) -> FmtResult<Operation> {