Skip to content

The headless browser for AI agents and web scraping

build: read the release tag from source tarballs

Floyd Maan52126c40283a
diff --git a/.git_archival.txt b/.git_archival.txt
new file mode 100644
index 00000000..c824a33e
--- /dev/null
+++ b/.git_archival.txt
@@ -0,0 +1 @@
+describe-name: $Format:%(describe:tags=true,match=v[0-9]*)$
diff --git a/.gitattributes b/.gitattributes
index dfe07704..4e984f0a 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,2 +1,3 @@
# Auto detect text files and perform LF normalization
* text=auto
+.git_archival.txt export-subst
diff --git a/crates/obscura-cli/build.rs b/crates/obscura-cli/build.rs
index e3fc13bb..9edb86c5 100644
--- a/crates/obscura-cli/build.rs
+++ b/crates/obscura-cli/build.rs
@@ -21,6 +21,27 @@ fn github_tag_version() -> Option<String> {
.map(normalize_version)
}
+/// Source tarballs (GitHub's `/archive/refs/tags/vX.Y.Z.tar.gz`, which Homebrew
+/// and nixpkgs build from) have neither git metadata nor GITHUB_* env, so the
+/// tag is otherwise unknown. `.git_archival.txt` is marked `export-subst`, so
+/// `git archive` (and GitHub's tarball endpoint) expands its placeholder to the
+/// tag; in a git checkout the placeholder stays unexpanded and is ignored.
+fn archive_version() -> Option<String> {
+ let manifest_dir = env::var("CARGO_MANIFEST_DIR").ok()?;
+ let path = std::path::Path::new(&manifest_dir).join("../../.git_archival.txt");
+ if !path.exists() {
+ return None;
+ }
+ println!("cargo:rerun-if-changed={}", path.display());
+ std::fs::read_to_string(&path)
+ .ok()?
+ .lines()
+ .find_map(|line| line.strip_prefix("describe-name:"))
+ .map(str::trim)
+ .filter(|value| !value.is_empty() && !value.contains("$Format"))
+ .map(|value| normalize_version(value.to_string()))
+}
+
/// Local builds are not cut from a tag, so the bare workspace version cannot
/// tell two checkouts apart. Append the commit when git is there to answer;
/// tarball and minimal-image builds without git keep the bare version.
@@ -49,6 +70,7 @@ fn main() {
let version = env_value("OBSCURA_VERSION")
.map(normalize_version)
.or_else(github_tag_version)
+ .or_else(archive_version)
.unwrap_or_else(local_version);
println!("cargo:rustc-env=OBSCURA_BUILD_VERSION={version}");
diff --git a/crates/obscura-mcp/build.rs b/crates/obscura-mcp/build.rs
index e3fc13bb..9edb86c5 100644
--- a/crates/obscura-mcp/build.rs
+++ b/crates/obscura-mcp/build.rs
@@ -21,6 +21,27 @@ fn github_tag_version() -> Option<String> {
.map(normalize_version)
}
+/// Source tarballs (GitHub's `/archive/refs/tags/vX.Y.Z.tar.gz`, which Homebrew
+/// and nixpkgs build from) have neither git metadata nor GITHUB_* env, so the
+/// tag is otherwise unknown. `.git_archival.txt` is marked `export-subst`, so
+/// `git archive` (and GitHub's tarball endpoint) expands its placeholder to the
+/// tag; in a git checkout the placeholder stays unexpanded and is ignored.
+fn archive_version() -> Option<String> {
+ let manifest_dir = env::var("CARGO_MANIFEST_DIR").ok()?;
+ let path = std::path::Path::new(&manifest_dir).join("../../.git_archival.txt");
+ if !path.exists() {
+ return None;
+ }
+ println!("cargo:rerun-if-changed={}", path.display());
+ std::fs::read_to_string(&path)
+ .ok()?
+ .lines()
+ .find_map(|line| line.strip_prefix("describe-name:"))
+ .map(str::trim)
+ .filter(|value| !value.is_empty() && !value.contains("$Format"))
+ .map(|value| normalize_version(value.to_string()))
+}
+
/// Local builds are not cut from a tag, so the bare workspace version cannot
/// tell two checkouts apart. Append the commit when git is there to answer;
/// tarball and minimal-image builds without git keep the bare version.
@@ -49,6 +70,7 @@ fn main() {
let version = env_value("OBSCURA_VERSION")
.map(normalize_version)
.or_else(github_tag_version)
+ .or_else(archive_version)
.unwrap_or_else(local_version);
println!("cargo:rustc-env=OBSCURA_BUILD_VERSION={version}");