2023-01-18 22:31:33 +00:00
|
|
|
use std::{env, fs::File, io::Write, path::Path, process::Command};
|
2019-07-21 17:45:31 +00:00
|
|
|
|
|
|
|
fn main() {
|
2020-11-24 01:08:57 +00:00
|
|
|
// If these env variables exist then we are building on nix, use them as hash
|
|
|
|
// and tag.
|
|
|
|
if let (Some(hash), Some(tag)) = (option_env!("NIX_GIT_HASH"), option_env!("NIX_GIT_TAG")) {
|
2020-06-17 11:06:40 +00:00
|
|
|
create_hash_file(hash);
|
2020-11-24 01:08:57 +00:00
|
|
|
create_tag_file(tag);
|
|
|
|
} else {
|
|
|
|
// Get the current githash
|
|
|
|
// Note: It will compare commits. As long as the commits do not diverge from the
|
|
|
|
// server no version change will be detected.
|
|
|
|
match Command::new("git")
|
2022-09-08 19:51:02 +00:00
|
|
|
.args([
|
2020-11-24 01:08:57 +00:00
|
|
|
"log",
|
|
|
|
"-n",
|
|
|
|
"1",
|
|
|
|
"--pretty=format:%h/%cd",
|
|
|
|
"--date=format:%Y-%m-%d-%H:%M",
|
|
|
|
"--abbrev=8",
|
|
|
|
])
|
|
|
|
.output()
|
|
|
|
{
|
|
|
|
Ok(output) => match String::from_utf8(output.stdout) {
|
|
|
|
Ok(hash) => {
|
|
|
|
create_hash_file(&hash);
|
|
|
|
},
|
|
|
|
Err(e) => panic!("failed to convert git output to UTF-8: {}", e),
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-11-24 01:08:57 +00:00
|
|
|
Err(e) => panic!("failed to retrieve current git commit hash: {}", e),
|
|
|
|
}
|
2020-09-16 10:50:55 +00:00
|
|
|
|
2020-11-24 01:08:57 +00:00
|
|
|
// Get the current githash
|
|
|
|
// Note: It will compare commits. As long as the commits do not diverge from the
|
|
|
|
// server no version change will be detected.
|
|
|
|
match Command::new("git")
|
2022-09-08 19:51:02 +00:00
|
|
|
.args(["describe", "--exact-match", "--tags", "HEAD"])
|
2020-11-24 01:08:57 +00:00
|
|
|
.output()
|
|
|
|
{
|
|
|
|
Ok(output) => match String::from_utf8(output.stdout) {
|
|
|
|
Ok(tag) => {
|
|
|
|
create_tag_file(&tag);
|
|
|
|
},
|
|
|
|
Err(e) => panic!("failed to convert git output to UTF-8: {}", e),
|
2020-09-16 10:50:55 +00:00
|
|
|
},
|
2020-11-24 01:08:57 +00:00
|
|
|
Err(e) => panic!("failed to retrieve current git commit hash: {}", e),
|
|
|
|
}
|
2020-09-16 10:50:55 +00:00
|
|
|
}
|
2019-07-21 17:45:31 +00:00
|
|
|
}
|
2020-06-17 11:06:40 +00:00
|
|
|
|
|
|
|
fn create_hash_file(hash: &str) {
|
|
|
|
let mut target = File::create(
|
|
|
|
Path::new(&env::var("OUT_DIR").expect("failed to query OUT_DIR environment variable"))
|
|
|
|
.join("githash"),
|
|
|
|
)
|
|
|
|
.expect("failed to create git hash file!");
|
|
|
|
target
|
|
|
|
.write_all(hash.trim().as_bytes())
|
|
|
|
.expect("failed to write to file!");
|
|
|
|
}
|
2020-09-16 10:50:55 +00:00
|
|
|
|
|
|
|
fn create_tag_file(tag: &str) {
|
|
|
|
let mut target = File::create(
|
|
|
|
Path::new(&env::var("OUT_DIR").expect("failed to query OUT_DIR environment variable"))
|
|
|
|
.join("gittag"),
|
|
|
|
)
|
|
|
|
.expect("failed to create git tag file!");
|
|
|
|
target
|
|
|
|
.write_all(tag.trim().as_bytes())
|
|
|
|
.expect("failed to write to file!");
|
|
|
|
}
|