1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use std::path::PathBuf;
15 use std::process::Command;
18 println!("cargo:rerun-if-changed=.git/HEAD");
19 println!("cargo:rerun-if-env-changed=CFG_RELEASE_CHANNEL");
21 let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
23 File::create(out_dir.join("commit-info.txt"))
25 .write_all(commit_info().as_bytes())
29 // Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
30 // (git not installed or if this is not a git repository) just return an empty string.
31 fn commit_info() -> String {
32 match (channel(), commit_hash(), commit_date()) {
33 (channel, Some(hash), Some(date)) => {
34 format!("{} ({} {})", channel, hash.trim_right(), date)
40 fn channel() -> String {
41 if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") {
48 fn commit_hash() -> Option<String> {
50 .args(&["rev-parse", "--short", "HEAD"])
53 .and_then(|r| String::from_utf8(r.stdout).ok())
56 fn commit_date() -> Option<String> {
58 .args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
61 .and_then(|r| String::from_utf8(r.stdout).ok())