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 let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
20 File::create(out_dir.join("commit-info.txt"))
22 .write_all(commit_info().as_bytes())
26 // Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
27 // (git not installed or if this is not a git repository) just return an empty string.
28 fn commit_info() -> String {
29 match (commit_hash(), commit_date()) {
30 (Some(hash), Some(date)) => format!(" ({} {})", hash.trim_right(), date),
35 fn commit_hash() -> Option<String> {
37 .args(&["rev-parse", "--short", "HEAD"])
40 .and_then(|r| String::from_utf8(r.stdout).ok())
43 fn commit_date() -> Option<String> {
45 .args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
48 .and_then(|r| String::from_utf8(r.stdout).ok())