feat: add build sha to parser.c header comment

This commit is contained in:
dundargoc 2024-10-24 12:44:37 +02:00 committed by dundargoc
parent 74481399df
commit dc4e232e6e
3 changed files with 41 additions and 2 deletions

View file

@ -60,6 +60,8 @@ fn web_playground_files_present() -> bool {
paths.iter().all(|p| Path::new(p).exists())
}
// When updating this function, don't forget to also update generate/build.rs which has a
// near-identical function.
fn read_git_sha() -> Option<String> {
let crate_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());

32
cli/generate/build.rs Normal file
View file

@ -0,0 +1,32 @@
use std::{env, path::PathBuf, process::Command};
fn main() {
if let Some(git_sha) = read_git_sha() {
println!("cargo:rustc-env=BUILD_SHA={git_sha}");
}
}
// This is copied from the build.rs in parent directory. This should be updated if the
// parent build.rs gets fixes.
fn read_git_sha() -> Option<String> {
let crate_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
if !crate_path
.parent()?
.parent()
.map_or(false, |p| p.join(".git").is_dir())
{
return None;
}
Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(crate_path)
.output()
.map_or(None, |output| {
if !output.status.success() {
return None;
}
Some(String::from_utf8_lossy(&output.stdout).to_string())
})
}

View file

@ -19,8 +19,9 @@ use super::{
const SMALL_STATE_THRESHOLD: usize = 64;
const ABI_VERSION_MIN: usize = 14;
const ABI_VERSION_MAX: usize = tree_sitter::LANGUAGE_VERSION;
const BUILD_VERSION: &str = env!("CARGO_PKG_VERSION");
const ABI_VERSION_WITH_METADATA: usize = 15;
const BUILD_VERSION: &str = env!("CARGO_PKG_VERSION");
const BUILD_SHA: Option<&'static str> = option_env!("BUILD_SHA");
macro_rules! add {
($this: tt, $($arg: tt)*) => {{
@ -280,9 +281,13 @@ impl Generator {
}
fn add_header(&mut self) {
let version = BUILD_SHA.map_or_else(
|| BUILD_VERSION.to_string(),
|build_sha| format!("{BUILD_VERSION} ({build_sha})"),
);
add_line!(
self,
"// Automatically generated by tree-sitter v{BUILD_VERSION}"
"/* Automatically generated by tree-sitter v{version} */",
);
add_line!(self, "");
}