Skip to content

Commit c081d0a

Browse files
Allow the release info build script to work without requiring Git to be installed (#3105)
* Prevent panic if env GRAPHITE_GIT_COMMIT_HASH is less than 8 chars long * Improve release info build script * Nits --------- Co-authored-by: Keavon Chambers <[email protected]>
1 parent 5d441c2 commit c081d0a

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

editor/build.rs

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,60 @@
1+
use std::env;
12
use std::process::Command;
23

34
const GRAPHITE_RELEASE_SERIES: &str = "Alpha 4";
45

56
fn main() {
6-
// Execute a Git command for its stdout. Early exit if it fails for any of the possible reasons.
7-
let try_git_command = |args: &[&str]| -> Option<String> {
8-
let git_output = Command::new("git").args(args).output().ok()?;
9-
let maybe_empty = String::from_utf8(git_output.stdout).ok()?;
10-
let command_result = (!maybe_empty.is_empty()).then_some(maybe_empty)?;
11-
Some(command_result)
12-
};
13-
// Execute a Git command for its output. Return "unknown" if it fails for any of the possible reasons.
14-
let git_command = |args| -> String { try_git_command(args).unwrap_or_else(|| String::from("unknown")) };
15-
16-
// Rather than printing to any terminal, these commands set environment variables in the Cargo toolchain.
17-
// They are accessed with the `env!("...")` macro in the codebase.
18-
println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_DATE={}", git_command(&["log", "-1", "--format=%cd"]));
19-
println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_HASH={}", git_command(&["rev-parse", "HEAD"]));
20-
let branch = std::env::var("GITHUB_HEAD_REF").unwrap_or_default();
21-
let branch = if branch.is_empty() { git_command(&["name-rev", "--name-only", "HEAD"]) } else { branch };
22-
println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_BRANCH={branch}");
7+
// Instruct Cargo to rerun this build script if any of these environment variables change.
8+
println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_DATE");
9+
println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_HASH");
10+
println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_BRANCH");
11+
println!("cargo:rerun-if-env-changed=GITHUB_HEAD_REF");
12+
13+
// Instruct Cargo to rerun this build script if the Git HEAD or refs change.
14+
println!("cargo:rerun-if-changed=.git/HEAD");
15+
println!("cargo:rerun-if-changed=.git/refs/heads");
16+
17+
// Try to get the commit information from the environment (e.g. set by CI), otherwise fall back to Git commands.
18+
let commit_date = env_or_else("GRAPHITE_GIT_COMMIT_DATE", || git_or_unknown(&["log", "-1", "--format=%cI"]));
19+
let commit_hash = env_or_else("GRAPHITE_GIT_COMMIT_HASH", || git_or_unknown(&["rev-parse", "HEAD"]));
20+
let commit_branch = env_or_else("GRAPHITE_GIT_COMMIT_BRANCH", || {
21+
let gh = env::var("GITHUB_HEAD_REF").unwrap_or_default();
22+
if !gh.trim().is_empty() {
23+
gh.trim().to_string()
24+
} else {
25+
git_or_unknown(&["rev-parse", "--abbrev-ref", "HEAD"])
26+
}
27+
});
28+
29+
// Instruct Cargo to set environment variables for compile time.
30+
// They are accessed with the `env!("GRAPHITE_*")` macro in the codebase.
31+
println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_DATE={commit_date}");
32+
println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_HASH={commit_hash}");
33+
println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_BRANCH={commit_branch}");
2334
println!("cargo:rustc-env=GRAPHITE_RELEASE_SERIES={GRAPHITE_RELEASE_SERIES}");
2435
}
36+
37+
/// Get an environment variable, or if it is not set or empty, use the provided fallback function. Returns a string with trimmed whitespace.
38+
fn env_or_else(key: &str, fallback: impl FnOnce() -> String) -> String {
39+
match env::var(key) {
40+
Ok(v) if !v.trim().is_empty() => v.trim().to_string(),
41+
_ => fallback().trim().to_string(),
42+
}
43+
}
44+
45+
/// Execute a Git command to obtain its output. Return "unknown" if it fails for any of the possible reasons.
46+
fn git_or_unknown(args: &[&str]) -> String {
47+
git(args).unwrap_or_else(|| "unknown".to_string())
48+
}
49+
50+
/// Run a git command and capture trimmed stdout.
51+
/// Returns None if git is missing, exits with error, or stdout is empty/non-UTF8.
52+
fn git(args: &[&str]) -> Option<String> {
53+
let output = Command::new("git").args(args).output().ok()?;
54+
if !output.status.success() {
55+
return None;
56+
}
57+
let s = String::from_utf8(output.stdout).ok()?;
58+
let t = s.trim();
59+
if t.is_empty() { None } else { Some(t.to_string()) }
60+
}

editor/src/application.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn commit_info_localized(localized_commit_date: &str) -> String {
5151
{}",
5252
GRAPHITE_RELEASE_SERIES,
5353
GRAPHITE_GIT_COMMIT_BRANCH,
54-
&GRAPHITE_GIT_COMMIT_HASH[..8],
54+
GRAPHITE_GIT_COMMIT_HASH.get(..8).unwrap_or(GRAPHITE_GIT_COMMIT_HASH),
5555
localized_commit_date
5656
)
5757
}

0 commit comments

Comments
 (0)