forked from 1b-agent/opencadstudio-upstream
Show the exact build in About, Copy Info, REPORT and --version
Builds from main reported only the release version (2026.36) even when HEAD was hundreds of commits past the tag. build.rs now also emits OCS_TAG_DISTANCE, OCS_COMMIT_DATE, OCS_BUILD_METADATA and OCS_FULL_VERSION: a clean checkout of the release tag stays "2026.36", anything else becomes "2026.36+194.gef189d77", with ".dirty" appended for uncommitted changes and only the hash when no tag can be counted from (shallow clone, tarball). OCS_APP_VERSION is unchanged so the update check, donation gate, User-Agents and window title keep working. The About dialog gains Build / Commit date / Profile cards, Copy Info, the REPORT issue template and recovery reports carry the full version, and `-V`/`--version` print it instead of Cargo's 2026.36.0. Release workflows check out full history so release binaries see the tag. Also fixes the build being marked dirty when there is no repository: diff-index exits 128 there, not 1. Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
1 parent
58c9fcb748
commit
5f161bb037
31 files changed
+204
-18
No files matched your search
@@ -14,32 +14,78 @@ fn main() {
|
||||
version.clone()
|
||||
};
|
||||
println!("cargo:rustc-env=OCS_APP_VERSION={app_version}");
|
||||
// Rerun on commit and branch switch only. Watching `.git/index` as well
|
||||
// would keep the dirty flag fresh, but Cargo recompiles the crate every
|
||||
// time this script reruns, and `git status` rewrites the index.
|
||||
println!("cargo:rerun-if-changed=.git/HEAD");
|
||||
if let Ok(head) = std::fs::read_to_string(".git/HEAD") {
|
||||
if let Some(reference) = head.trim().strip_prefix("ref: ") {
|
||||
println!("cargo:rerun-if-changed=.git/{reference}");
|
||||
}
|
||||
}
|
||||
let revision = std::process::Command::new("git")
|
||||
.args(["rev-parse", "--short=12", "HEAD"])
|
||||
.output()
|
||||
.ok()
|
||||
.filter(|output| output.status.success())
|
||||
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
let revision = git_output(&["rev-parse", "--short=12", "HEAD"])
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
// diff-index exits 1 for differences and 128 when there is no repository,
|
||||
// so only a real difference marks the build dirty.
|
||||
let dirty = std::process::Command::new("git")
|
||||
.args(["diff-index", "--quiet", "HEAD", "--"])
|
||||
.status()
|
||||
.ok()
|
||||
.is_some_and(|status| !status.success());
|
||||
.is_some_and(|status| status.code() == Some(1));
|
||||
let commit = revision.clone();
|
||||
let revision = if dirty {
|
||||
format!("{revision}-dirty")
|
||||
} else {
|
||||
revision
|
||||
};
|
||||
println!("cargo:rustc-env=OCS_GIT_REV={revision}");
|
||||
|
||||
// Builds from the main branch report how far past the release tag they
|
||||
// are. Release tags are `v` + the display version (scripts/release.py),
|
||||
// so counting first-parent commits from that tag ties the distance to
|
||||
// the version the app already claims. Shallow clones and source
|
||||
// tarballs have no tag to count from and fall back to the hash alone.
|
||||
let tag_distance = git_output(&[
|
||||
"rev-list",
|
||||
"--count",
|
||||
"--first-parent",
|
||||
&format!("v{app_version}..HEAD"),
|
||||
])
|
||||
.and_then(|value| value.parse::<u64>().ok());
|
||||
println!(
|
||||
"cargo:rustc-env=OCS_TAG_DISTANCE={}",
|
||||
tag_distance.map_or_else(|| "unknown".to_string(), |n| n.to_string())
|
||||
);
|
||||
let commit_date = git_output(&["log", "-1", "--format=%cs", "HEAD"])
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
println!("cargo:rustc-env=OCS_COMMIT_DATE={commit_date}");
|
||||
|
||||
// Semver-style build metadata: empty for a clean build on the release
|
||||
// tag, `+N.g<hash>` for N first-parent commits past it, `+g<hash>` when
|
||||
// the distance is unknown, and `.dirty` appended for uncommitted changes.
|
||||
let short_hash = (commit != "unknown").then(|| commit[..commit.len().min(8)].to_string());
|
||||
let mut metadata_parts: Vec<String> = Vec::new();
|
||||
match (tag_distance, &short_hash) {
|
||||
(Some(0), _) => {}
|
||||
(Some(n), Some(hash)) => {
|
||||
metadata_parts.push(n.to_string());
|
||||
metadata_parts.push(format!("g{hash}"));
|
||||
}
|
||||
(Some(n), None) => metadata_parts.push(n.to_string()),
|
||||
(None, Some(hash)) => metadata_parts.push(format!("g{hash}")),
|
||||
(None, None) => {}
|
||||
}
|
||||
if dirty {
|
||||
metadata_parts.push("dirty".to_string());
|
||||
}
|
||||
let build_metadata = if metadata_parts.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
format!("+{}", metadata_parts.join("."))
|
||||
};
|
||||
println!("cargo:rustc-env=OCS_BUILD_METADATA={build_metadata}");
|
||||
let full_version = format!("{app_version}{build_metadata}");
|
||||
println!("cargo:rustc-env=OCS_FULL_VERSION={full_version}");
|
||||
let profile = std::env::var("PROFILE").unwrap_or_else(|_| "unknown".to_string());
|
||||
println!("cargo:rustc-env=OCS_BUILD_PROFILE={profile}");
|
||||
if std::env::var("TARGET").ok().as_deref() == Some("x86_64-pc-windows-msvc")
|
||||
@@ -73,7 +119,7 @@ fn main() {
|
||||
if Path::new("packaging/windows/AppIcon.ico").exists() {
|
||||
let mut res = winresource::WindowsResource::new();
|
||||
res.set_icon("packaging/windows/AppIcon.ico");
|
||||
res.set("ProductVersion", &app_version);
|
||||
res.set("ProductVersion", &full_version);
|
||||
res.set("FileVersion", &app_version);
|
||||
if let Err(e) = res.compile() {
|
||||
println!("cargo:warning=failed to embed Windows icon: {e}");
|
||||
@@ -81,3 +127,16 @@ fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `git` with `args` and returns trimmed stdout, or `None` when git is
|
||||
/// missing, fails (no repository, unknown tag), or prints nothing.
|
||||
fn git_output(args: &[&str]) -> Option<String> {
|
||||
std::process::Command::new("git")
|
||||
.args(args)
|
||||
.output()
|
||||
.ok()
|
||||
.filter(|output| output.status.success())
|
||||
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
}
|
||||
Reference in new issue
Block a user