mirror of
https://github.com/openai/codex.git
synced 2026-05-16 09:12:54 +00:00
Move the build git SHA display value into codex-utils-cli so codex --version, TUI headers, status lines, and status cards render the same version string. (cherry picked from commit 9eeaac2f278e7babd768c1818aeb8f95e0022ebf) (cherry picked from commit 97e4866de8026afdbe9f40c8fadf294d422b27df)
52 lines
1.4 KiB
Rust
52 lines
1.4 KiB
Rust
use std::path::Path;
|
|
use std::process::Command;
|
|
|
|
fn main() {
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
|
|
let manifest_dir = match std::env::var("CARGO_MANIFEST_DIR") {
|
|
Ok(manifest_dir) => manifest_dir,
|
|
Err(err) => panic!("CLI utils build script needs CARGO_MANIFEST_DIR: {err}"),
|
|
};
|
|
let manifest_dir = Path::new(&manifest_dir);
|
|
|
|
let git_sha = git_stdout(manifest_dir, &["rev-parse", "--short=12", "HEAD"])
|
|
.unwrap_or_else(|| "unknown".to_string());
|
|
println!("cargo:rustc-env=CODEX_BUILD_GIT_SHA={git_sha}");
|
|
|
|
if let Some(head_path) = git_path(manifest_dir, "HEAD") {
|
|
println!("cargo:rerun-if-changed={head_path}");
|
|
}
|
|
|
|
if let Some(head_ref) = git_stdout(manifest_dir, &["symbolic-ref", "-q", "HEAD"])
|
|
&& let Some(ref_path) = git_path(manifest_dir, &head_ref)
|
|
{
|
|
println!("cargo:rerun-if-changed={ref_path}");
|
|
}
|
|
}
|
|
|
|
fn git_path(manifest_dir: &Path, path: &str) -> Option<String> {
|
|
git_stdout(manifest_dir, &["rev-parse", "--git-path", path])
|
|
}
|
|
|
|
fn git_stdout(manifest_dir: &Path, args: &[&str]) -> Option<String> {
|
|
let output = Command::new("git")
|
|
.arg("-C")
|
|
.arg(manifest_dir)
|
|
.args(args)
|
|
.output()
|
|
.ok()?;
|
|
|
|
if !output.status.success() {
|
|
return None;
|
|
}
|
|
|
|
let stdout = String::from_utf8(output.stdout).ok()?;
|
|
let stdout = stdout.trim().to_string();
|
|
if stdout.is_empty() {
|
|
return None;
|
|
}
|
|
|
|
Some(stdout)
|
|
}
|