Drop vergen_git2 dependency (#36439)

Manually call `git rev-parse --short HEAD` to retrieve the commit hash,
instead of using Emitter from vergen_git2. This helps remove
dependencies and shorten compile time.

Testing: It doesn't require tests because it is a refactoring for
removing dependencies
Fixes: #36435

Signed-off-by: Kingsley Yung <kingsley@kkoyung.dev>
This commit is contained in:
Kingsley Yung 2025-04-10 18:39:44 +08:00 committed by GitHub
parent c16ca22970
commit e6595619e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 125 deletions

View file

@ -22,7 +22,6 @@ bench = false
# since build-scripts can't detect the cargo target os at build-time, we
# must unconditionally add these dependencies. See https://github.com/rust-lang/cargo/issues/4932
[build-dependencies]
vergen-git2 = { version = "1.0.5", features = ["build"] }
# MacOS only
cc = "1.2"

View file

@ -6,18 +6,20 @@ use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;
use vergen_git2::{Emitter, Git2Builder};
fn emit_git_sha() -> Result<(), String> {
let git_options = Git2Builder::default()
.sha(true /* short */)
.build()
fn git_sha() -> Result<String, String> {
let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.map_err(|e| e.to_string())?;
Emitter::default()
.add_instructions(&git_options)
.and_then(|emitter| emitter.fail_on_error().emit())
.map_err(|e| e.to_string())
if output.status.success() {
let hash = String::from_utf8(output.stdout).map_err(|e| e.to_string())?;
Ok(hash.trim().to_owned())
} else {
let stderr = String::from_utf8(output.stderr).map_err(|e| e.to_string())?;
Err(stderr)
}
}
fn main() -> Result<(), Box<dyn Error>> {
@ -72,12 +74,15 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rustc-link-search=native={}", out.display());
}
if let Err(error) = emit_git_sha() {
println!(
"cargo:warning=Could not generate git version information: {:?}",
error
);
println!("cargo:rustc-env=VERGEN_GIT_SHA=nogit");
match git_sha() {
Ok(hash) => println!("cargo:rustc-env=GIT_SHA={}", hash),
Err(error) => {
println!(
"cargo:warning=Could not generate git version information: {:?}",
error
);
println!("cargo:rustc-env=GIT_SHA=nogit");
},
}
// On MacOS, all dylib dependencies are shipped along with the binary

View file

@ -97,11 +97,7 @@ pub fn init_tracing(filter_directives: Option<&str>) {
}
pub fn servo_version() -> String {
format!(
"Servo {}-{}",
env!("CARGO_PKG_VERSION"),
env!("VERGEN_GIT_SHA")
)
format!("Servo {}-{}", env!("CARGO_PKG_VERSION"), env!("GIT_SHA"))
}
/// Plumbs tracing spans into HiTrace, with the following caveats: