Skip to content

Adding --verbose flag to check commit hash #4568

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions compiler-cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::process::Command;

fn main() {
// Try to get the current Git commit hash (short format) for use in --version --verbose
let commit_hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok() // if the command fails to launch
.filter(|output| output.status.success()) // if the command fails (e.g. not a git repo)
.map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "unknown".to_string());

println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of shelling out to run a program each time. We could do something like this perhaps

const GIT_SHA: &str = if include_str!("../../.git/HEAD") == "ref: refs/heads/main" {
  include_str!("../../.git/refs/heads/main")
} else {
  include_str!("../../.git/HEAD")
}

}
Loading
Loading