We specify an "abbreviated" length of 64 characters to force the full hash to be always used for reproducibility. The reason we specify 64 and not 40 is to be compatible with sha256 commit hashes.
40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
use std::env;
|
|
use std::fs;
|
|
use std::path::Path;
|
|
use std::process::Command;
|
|
|
|
fn main() {
|
|
let keywords_expansion = std::include_str!("git-keywords-expansion.text");
|
|
|
|
let described;
|
|
if keywords_expansion.starts_with("$Format:") {
|
|
// Running in a git repo
|
|
let output = Command::new("git")
|
|
.arg("describe")
|
|
.arg("--always")
|
|
.arg("--abbrev=64")
|
|
.output()
|
|
.expect("Failed to execute git describe --always --abbrev=64");
|
|
described = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
|
} else {
|
|
let (hash, describe_output) = keywords_expansion
|
|
.split_once('\t')
|
|
.expect("git-keywords-expansion.text must have two tab-separated fields");
|
|
let describe_output = describe_output.trim();
|
|
|
|
if describe_output.is_empty() {
|
|
described = hash.to_string();
|
|
} else {
|
|
described = describe_output.to_string();
|
|
}
|
|
}
|
|
|
|
let ident_string = format!("@(#) $Version: kato {described} $\n");
|
|
|
|
let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR must be set foor build.rs");
|
|
let ident_file = Path::new(&out_dir).join("ident.text");
|
|
|
|
fs::write(&ident_file, ident_string).expect("failed to write ident.text");
|
|
|
|
println!("cargo::rerun-if-changed=git-keywords-expansion.text");
|
|
}
|