mirror of
https://github.com/Noratrieb/does-it-build.git
synced 2026-01-14 10:25:01 +01:00
49 lines
1.2 KiB
Rust
49 lines
1.2 KiB
Rust
fn main() {
|
|
// Always rerun.
|
|
|
|
let version = if let Ok(commit) = try_get_commit() {
|
|
match has_no_changes() {
|
|
Ok(true) => commit,
|
|
Ok(false) => format!("{commit} (*)"),
|
|
Err(_) => format!("{commit} (?)"),
|
|
}
|
|
} else {
|
|
"unknown".into()
|
|
};
|
|
|
|
println!("cargo:rustc-env=GIT_COMMIT={version}");
|
|
let version_short = if version.len() > 16 {
|
|
&version[..16]
|
|
} else {
|
|
&version
|
|
};
|
|
println!("cargo:rustc-env=GIT_COMMIT_SHORT={version_short}");
|
|
}
|
|
|
|
fn try_get_commit() -> color_eyre::Result<String> {
|
|
if let Ok(overridden) = std::env::var("DOES_IT_BUILD_OVERRIDE_VERSION") {
|
|
return Ok(overridden);
|
|
}
|
|
|
|
let stdout = std::process::Command::new("git")
|
|
.arg("rev-parse")
|
|
.arg("HEAD")
|
|
.output()?
|
|
.stdout;
|
|
|
|
let stdout = String::from_utf8(stdout)?;
|
|
|
|
Ok(stdout.trim()[0..8].to_owned())
|
|
}
|
|
|
|
fn has_no_changes() -> color_eyre::Result<bool> {
|
|
if std::env::var("DOES_IT_BUILD_OVERRIDE_VERSION").is_ok() {
|
|
return Ok(true);
|
|
}
|
|
|
|
Ok(std::process::Command::new("git")
|
|
.args(["diff", "--no-ext-diff", "--quiet", "--exit-code"])
|
|
.output()?
|
|
.status
|
|
.success())
|
|
}
|