-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1701 from kinnison/kinnison/better-git-version
Switch to git-testament rather than old build.rs
- Loading branch information
Showing
4 changed files
with
58 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,6 @@ | ||
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 target = env::var("TARGET").unwrap(); | ||
println!("cargo:rustc-env=TARGET={}", target); | ||
|
||
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(); | ||
|
||
println!("cargo:rerun-if-changed=build.rs"); | ||
} | ||
|
||
// 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_end(), date), | ||
_ => String::new(), | ||
} | ||
} | ||
|
||
fn commit_hash() -> Result<String, Ignore> { | ||
Ok(String::from_utf8( | ||
Command::new("git") | ||
.args(&["rev-parse", "--short=9", "HEAD"]) | ||
.output()? | ||
.stdout, | ||
)?) | ||
} | ||
|
||
fn commit_date() -> Result<String, Ignore> { | ||
Ok(String::from_utf8( | ||
Command::new("git") | ||
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"]) | ||
.output()? | ||
.stdout, | ||
)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters