Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show commit in version if not building from tagged release commit #3793

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,35 @@ fn git_commit() -> Option<String> {
.stdout,
)
.ok()
.map(|branch| branch.into())
.map(|commit| commit.into())
}

fn cargo_version() -> String {
let cargo_version = env!("CARGO_PKG_VERSION").into();

let Some(output) = Command::new("git")
.args(["describe", "--tags", "--exact-match"])
.output()
.ok()
else {
return cargo_version;
};

str::from_utf8(&output.stdout)
.ok()
.and_then(|tag| {
if tag.is_empty() {
Some(format!(
"{}-{}",
cargo_version,
git_commit().unwrap_or_default()
))
} else {
assert_eq!(tag, cargo_version);
None
}
})
.unwrap_or(env!("CARGO_PKG_VERSION").into())
}

fn main() {
Expand All @@ -33,4 +61,5 @@ fn main() {
"cargo:rustc-env=GIT_COMMIT={}",
git_commit().unwrap_or_default()
);
println!("cargo:rustc-env=CARGO_PKG_VERSION={}", cargo_version());
}
Loading