-
-
Notifications
You must be signed in to change notification settings - Fork 894
Allow the release info build script to work without requiring Git to be installed #3105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bff2b4c
Prevent panic if env GRAPHITE_GIT_COMMIT_HASH is less than 8 chars long
timon-schelling 6d640d2
Improve release info build script
timon-schelling 252c762
Merge branch 'master' into improve-release-info-build-script
timon-schelling 7e1cb0e
Merge branch 'master' into improve-release-info-build-script
Keavon 452e87f
Nits
Keavon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,60 @@ | ||
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<String> { | ||
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("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() | ||
} 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 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 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()) | ||
} | ||
|
||
/// 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<String> { | ||
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()) } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
--abbrev-ref
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returns a commit hash otherwise. But I could also use name-rev like you did before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does it return if not a commit hash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Branch name. (Rev short name if you want to use git wording)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should aim to always have a commit hash so it's possible to actually look up which commit a build link belongs to. (This is also something we don't do on https://editor.graphite.rs but should do.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commit hash is collected in the statement above. There rev-parse without --abbrev-ref.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of this second one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I wasn't looking closely enough. Disregard my last two comments. Anyway, I think we're good to merge.