Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (41 sloc) 1.37 KB
use std::process::Command;
/// Same as `set_env`, but using `name` as environment variable.
///
/// You can, for example, override the `CARGO_PKG_VERSION` using in
/// your `build.rs` script:
///
/// ```
/// extern crate git_version;
/// fn main() { git_version::set_env_with_name("CARGO_PKG_VERSION"); }
/// ```
use std::env;
fn set_env_with_name(name: &str) {
let ver = match Command::new("git")
.args(&["describe", "--tags", "--dirty=-modified"])
.output()
{
Ok(cmd) => {
assert!(cmd.status.success());
match std::str::from_utf8(&cmd.stdout[..]) {
Ok(v) => v.trim().to_owned(),
Err(_) => match env::var(name) {
Ok(val) => val.trim().to_owned(),
Err(_) => "invalid-git-version".to_owned(),
}
}
}
Err(_) => match env::var(name) {
Ok(val) => val.trim().to_owned(),
Err(_) => "no-git-version".to_owned(),
}
};
println!("cargo:rustc-env={}={}", name, ver);
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");
println!("cargo:rerun-if-changed=.git");
println!("cargo:rerun-if-env-changed={}", name);
}
fn main() {
env::set_var("RUST_BACKTRACE", "1");
set_env_with_name("GIT_VERSION");
}