-
Notifications
You must be signed in to change notification settings - Fork 173
deps: Replace vergen with a new support crate #2905
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| [package] | ||
| name = "build_rs_git_info" | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
|
|
||
| [dependencies] | ||
| anyhow.workspace = true | ||
|
|
||
| [lints] | ||
| workspace = true |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||||||||||
| // Copyright (c) Microsoft Corporation. | ||||||||||||||
| // Licensed under the MIT License. | ||||||||||||||
|
|
||||||||||||||
| //! Build-script helper that emits `BUILD_GIT_SHA` and `BUILD_GIT_BRANCH` | ||||||||||||||
| //! cargo environment variables by invoking the `git` CLI. | ||||||||||||||
|
|
||||||||||||||
| use std::process::Command; | ||||||||||||||
|
|
||||||||||||||
| fn git_output(args: &[&str]) -> anyhow::Result<String> { | ||||||||||||||
| let output = Command::new("git").args(args).output()?; | ||||||||||||||
|
|
||||||||||||||
| if !output.status.success() { | ||||||||||||||
| anyhow::bail!( | ||||||||||||||
| "git {:?} failed with code {:?}: {}", | ||||||||||||||
| args, | ||||||||||||||
| output.status.code(), | ||||||||||||||
| String::from_utf8_lossy(&output.stderr) | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| let output = String::from_utf8(output.stdout).unwrap().trim().to_owned(); | ||||||||||||||
| Ok(output) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| fn git_path(args: &[&str]) -> anyhow::Result<std::path::PathBuf> { | ||||||||||||||
| let output = git_output(args)?; | ||||||||||||||
| Ok(std::path::absolute(&output)?) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// Emit git information as `cargo:rustc-env` variables so they are available via | ||||||||||||||
| /// `env!()` / `option_env!()` in the consuming crate. | ||||||||||||||
| pub fn emit_git_info() -> anyhow::Result<()> { | ||||||||||||||
| // Always rerun when HEAD changes (e.g. branch switch). | ||||||||||||||
| let head_path = git_path(&["rev-parse", "--git-path", "HEAD"])?; | ||||||||||||||
| println!("cargo:rerun-if-changed={}", head_path.display()); | ||||||||||||||
|
|
||||||||||||||
| // If HEAD is a symbolic ref (i.e. points at a branch), also watch the | ||||||||||||||
| // branch ref file so we rebuild when new commits land on that branch. | ||||||||||||||
| if let Ok(head_ref) = git_output(&["symbolic-ref", "HEAD"]) { | ||||||||||||||
| // e.g. refs/heads/main → .git/refs/heads/main (or the worktree equivalent) | ||||||||||||||
| let ref_path = git_path(&["rev-parse", "--git-path", &head_ref])?; | ||||||||||||||
| println!("cargo:rerun-if-changed={}", ref_path.display()); | ||||||||||||||
| } | ||||||||||||||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| // Also watch the packed-refs file so ref updates are tracked even when | |
| // branch refs are packed and the loose ref files no longer exist. | |
| let packed_refs_path = git_path(&["rev-parse", "--git-path", "packed-refs"])?; | |
| println!("cargo:rerun-if-changed={}", packed_refs_path.display()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chatted with @smalis-msft offline. He experimented, and found that this is incorrect. This code will trigger a rebuild if that file doesn't exist.
Uh oh!
There was an error while loading. Please reload this page.