From bff2b4c1a5854376d555219f1fe8d4c61c364b51 Mon Sep 17 00:00:00 2001 From: Timon Schelling Date: Thu, 28 Aug 2025 10:25:33 +0000 Subject: [PATCH 1/3] Prevent panic if env GRAPHITE_GIT_COMMIT_HASH is less than 8 chars long --- editor/src/application.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/src/application.rs b/editor/src/application.rs index fdb8c60334..e3c7c24d09 100644 --- a/editor/src/application.rs +++ b/editor/src/application.rs @@ -51,7 +51,7 @@ pub fn commit_info_localized(localized_commit_date: &str) -> String { {}", GRAPHITE_RELEASE_SERIES, GRAPHITE_GIT_COMMIT_BRANCH, - &GRAPHITE_GIT_COMMIT_HASH[..8], + GRAPHITE_GIT_COMMIT_HASH.get(..8).unwrap_or(GRAPHITE_GIT_COMMIT_HASH), localized_commit_date ) } From 6d640d2c5cc8d0b4e6d23798d46e787bbb84012a Mon Sep 17 00:00:00 2001 From: Timon Schelling Date: Thu, 28 Aug 2025 10:27:36 +0000 Subject: [PATCH 2/3] Improve release info build script --- editor/build.rs | 71 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/editor/build.rs b/editor/build.rs index 7fc95d9c54..c096a72e28 100644 --- a/editor/build.rs +++ b/editor/build.rs @@ -1,24 +1,61 @@ +use std::env; use std::process::Command; const GRAPHITE_RELEASE_SERIES: &str = "Alpha 4"; fn main() { - // Execute a Git command for its stdout. Early exit if it fails for any of the possible reasons. - let try_git_command = |args: &[&str]| -> Option { - let git_output = Command::new("git").args(args).output().ok()?; - let maybe_empty = String::from_utf8(git_output.stdout).ok()?; - let command_result = (!maybe_empty.is_empty()).then_some(maybe_empty)?; - Some(command_result) - }; - // Execute a Git command for its output. Return "unknown" if it fails for any of the possible reasons. - let git_command = |args| -> String { try_git_command(args).unwrap_or_else(|| String::from("unknown")) }; - - // Rather than printing to any terminal, these commands set environment variables in the Cargo toolchain. - // They are accessed with the `env!("...")` macro in the codebase. - println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_DATE={}", git_command(&["log", "-1", "--format=%cd"])); - println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_HASH={}", git_command(&["rev-parse", "HEAD"])); - let branch = std::env::var("GITHUB_HEAD_REF").unwrap_or_default(); - let branch = if branch.is_empty() { git_command(&["name-rev", "--name-only", "HEAD"]) } else { branch }; - println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_BRANCH={branch}"); + // Instruct Cargo to rerun this build script if any of these environment variables change. + println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_DATE"); + println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_HASH"); + println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_BRANCH"); + println!("cargo:rerun-if-env-changed=GITHUB_HEAD_REF"); + + // Instruct Cargo to rerun this build script if the Git HEAD or refs change. + println!("cargo:rerun-if-changed=.git/HEAD"); + println!("cargo:rerun-if-changed=.git/refs/heads"); + + // Try to get the commit information from the environment (e.g. set by CI), otherwise fall back to Git commands. + let commit_date = env_or_else_trim("GRAPHITE_GIT_COMMIT_DATE", || git_or_unknown(&["log", "-1", "--format=%cI"])); + let commit_hash = env_or_else_trim("GRAPHITE_GIT_COMMIT_HASH", || git_or_unknown(&["rev-parse", "HEAD"])); + let commit_branch = env_or_else_trim("GRAPHITE_GIT_COMMIT_BRANCH", || { + let gh = env::var("GITHUB_HEAD_REF").unwrap_or_default(); + if !gh.trim().is_empty() { + gh.trim().to_string() + } else { + git_or_unknown(&["rev-parse", "--abbrev-ref", "HEAD"]) + } + }); + + // Instruct Cargo to set environment variables for compile time. + // They are accessed with the `env!("GRAPHITE_*")` macro in the codebase. + println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_DATE={commit_date}"); + println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_HASH={commit_hash}"); + println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_BRANCH={commit_branch}"); println!("cargo:rustc-env=GRAPHITE_RELEASE_SERIES={GRAPHITE_RELEASE_SERIES}"); } + +// Get an environment variable, or if it is not set or empty, use the provided fallback function. +// Returns a trimmed string. +fn env_or_else_trim(key: &str, fallback: impl FnOnce() -> String) -> String { + match env::var(key) { + Ok(v) if !v.trim().is_empty() => v.trim().to_string(), + _ => fallback().trim().to_string(), + } +} + +// Execute a Git command for its output. Return "unknown" if it fails for any of the possible reasons. +fn git_or_unknown(args: &[&str]) -> String { + git(args).unwrap_or_else(|| "unknown".to_string()) +} + +/// Run a git command and capture trimmed stdout. +/// Returns None if git is missing, exits with error, or stdout is empty/non-UTF8. +fn git(args: &[&str]) -> Option { + let output = Command::new("git").args(args).output().ok()?; + if !output.status.success() { + return None; + } + let s = String::from_utf8(output.stdout).ok()?; + let t = s.trim(); + if t.is_empty() { None } else { Some(t.to_string()) } +} From 452e87f97aa658df9745cbe2489ef598961405f4 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Fri, 5 Sep 2025 00:15:53 -0700 Subject: [PATCH 3/3] Nits --- editor/build.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/editor/build.rs b/editor/build.rs index c096a72e28..3e0a531581 100644 --- a/editor/build.rs +++ b/editor/build.rs @@ -15,9 +15,9 @@ fn main() { println!("cargo:rerun-if-changed=.git/refs/heads"); // Try to get the commit information from the environment (e.g. set by CI), otherwise fall back to Git commands. - let commit_date = env_or_else_trim("GRAPHITE_GIT_COMMIT_DATE", || git_or_unknown(&["log", "-1", "--format=%cI"])); - let commit_hash = env_or_else_trim("GRAPHITE_GIT_COMMIT_HASH", || git_or_unknown(&["rev-parse", "HEAD"])); - let commit_branch = env_or_else_trim("GRAPHITE_GIT_COMMIT_BRANCH", || { + let commit_date = env_or_else("GRAPHITE_GIT_COMMIT_DATE", || git_or_unknown(&["log", "-1", "--format=%cI"])); + let commit_hash = env_or_else("GRAPHITE_GIT_COMMIT_HASH", || git_or_unknown(&["rev-parse", "HEAD"])); + let commit_branch = env_or_else("GRAPHITE_GIT_COMMIT_BRANCH", || { let gh = env::var("GITHUB_HEAD_REF").unwrap_or_default(); if !gh.trim().is_empty() { gh.trim().to_string() @@ -34,16 +34,15 @@ fn main() { println!("cargo:rustc-env=GRAPHITE_RELEASE_SERIES={GRAPHITE_RELEASE_SERIES}"); } -// Get an environment variable, or if it is not set or empty, use the provided fallback function. -// Returns a trimmed string. -fn env_or_else_trim(key: &str, fallback: impl FnOnce() -> String) -> String { +/// Get an environment variable, or if it is not set or empty, use the provided fallback function. Returns a string with trimmed whitespace. +fn env_or_else(key: &str, fallback: impl FnOnce() -> String) -> String { match env::var(key) { Ok(v) if !v.trim().is_empty() => v.trim().to_string(), _ => fallback().trim().to_string(), } } -// Execute a Git command for its output. Return "unknown" if it fails for any of the possible reasons. +/// Execute a Git command to obtain its output. Return "unknown" if it fails for any of the possible reasons. fn git_or_unknown(args: &[&str]) -> String { git(args).unwrap_or_else(|| "unknown".to_string()) }