Skip to content

Commit d39e545

Browse files
committed
feat: add build-time git info
1 parent be946b6 commit d39e545

File tree

2 files changed

+97
-9
lines changed

2 files changed

+97
-9
lines changed

stacks-common/build.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,64 @@
11
use std::path::Path;
2+
use std::process::Command;
23
use std::{env, fs};
34

45
use toml::Value;
56

7+
fn current_git_hash() -> Option<String> {
8+
if option_env!("GIT_COMMIT") == None {
9+
let commit = Command::new("git")
10+
.arg("log")
11+
.arg("-1")
12+
.arg("--pretty=format:%h") // Abbreviated commit hash
13+
.current_dir(env!("CARGO_MANIFEST_DIR"))
14+
.output();
15+
16+
if let Ok(commit) = commit {
17+
if let Ok(commit) = String::from_utf8(commit.stdout) {
18+
return Some(commit.trim().to_string());
19+
}
20+
}
21+
} else {
22+
return option_env!("GIT_COMMIT").map(String::from);
23+
}
24+
25+
None
26+
}
27+
28+
fn current_git_branch() -> Option<String> {
29+
if option_env!("GIT_BRANCH") == None {
30+
let commit = Command::new("git")
31+
.arg("rev-parse")
32+
.arg("--abbrev-ref")
33+
.arg("HEAD")
34+
.output();
35+
if let Ok(commit) = commit {
36+
if let Ok(commit) = String::from_utf8(commit.stdout) {
37+
return Some(commit.trim().to_string());
38+
}
39+
}
40+
} else {
41+
return option_env!("GIT_BRANCH").map(String::from);
42+
}
43+
44+
None
45+
}
46+
47+
fn is_working_tree_clean() -> bool {
48+
let status = Command::new("git")
49+
.arg("diff")
50+
.arg("--quiet")
51+
.arg("--exit-code")
52+
.current_dir(env!("CARGO_MANIFEST_DIR"))
53+
.status();
54+
55+
if let Ok(status) = status {
56+
status.code() == Some(0)
57+
} else {
58+
true
59+
}
60+
}
61+
662
fn main() {
763
let toml_content =
864
fs::read_to_string("../versions.toml").expect("Failed to read versions.toml");
@@ -34,6 +90,40 @@ fn main() {
3490
}
3591
}
3692

93+
if let Some(git) = current_git_hash() {
94+
// println!("git commit: {}", git);
95+
rust_code.push_str(&format!(
96+
"pub const GIT_COMMIT: Option<&'static str> = Some(\"{}\");\n",
97+
git
98+
));
99+
println!("cargo:rustc-env=GIT_COMMIT={}", git);
100+
} else {
101+
rust_code.push_str(&format!(
102+
"pub const GIT_COMMIT: Option<&'static str> = None;\n"
103+
));
104+
}
105+
if let Some(git) = current_git_branch() {
106+
rust_code.push_str(&format!(
107+
"pub const GIT_BRANCH: Option<&'static str> = Some(\"{}\");\n",
108+
git
109+
));
110+
println!("cargo:rustc-env=GIT_BRANCH={}", git);
111+
} else {
112+
rust_code.push_str(&format!(
113+
"pub const GIT_BRANCH: Option<&'static str> = None;\n"
114+
));
115+
}
116+
if !is_working_tree_clean() {
117+
rust_code.push_str(&format!(
118+
"pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\"\");\n"
119+
));
120+
println!("cargo:rustc-env=GIT_TREE_CLEAN=+");
121+
} else {
122+
rust_code.push_str(&format!(
123+
"pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\"+\");\n"
124+
));
125+
}
126+
37127
let out_dir = env::var_os("OUT_DIR").unwrap();
38128
let dest_path = Path::new(&out_dir).join("versions.rs");
39129
fs::write(&dest_path, rust_code).expect("Failed to write generated code");

stackslib/src/lib.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extern crate stacks_common;
4545
#[macro_use]
4646
pub extern crate clarity;
4747

48-
use stacks_common::versions::STACKS_NODE_VERSION;
48+
use stacks_common::versions::{GIT_BRANCH, GIT_COMMIT, GIT_TREE_CLEAN, STACKS_NODE_VERSION};
4949
pub use stacks_common::{address, codec, types, util};
5050

5151
#[macro_use]
@@ -71,9 +71,9 @@ pub mod deps;
7171
pub mod monitoring;
7272

7373
// set via _compile-time_ envars
74-
const GIT_BRANCH: Option<&'static str> = option_env!("GIT_BRANCH");
75-
const GIT_COMMIT: Option<&'static str> = option_env!("GIT_COMMIT");
76-
const GIT_TREE_CLEAN: Option<&'static str> = option_env!("GIT_TREE_CLEAN");
74+
const GIT_BRANCH_ENV: Option<&'static str> = option_env!("GIT_BRANCH");
75+
const GIT_COMMIT_ENV: Option<&'static str> = option_env!("GIT_COMMIT");
76+
const GIT_TREE_CLEAN_ENV: Option<&'static str> = option_env!("GIT_TREE_CLEAN");
7777

7878
#[cfg(debug_assertions)]
7979
const BUILD_TYPE: &'static str = "debug";
@@ -82,11 +82,9 @@ const BUILD_TYPE: &'static str = "release";
8282

8383
pub fn version_string(pkg_name: &str, pkg_version: Option<&str>) -> String {
8484
let pkg_version = pkg_version.unwrap_or(STACKS_NODE_VERSION);
85-
let git_branch = GIT_BRANCH
86-
.map(|x| format!("{}", x))
87-
.unwrap_or("".to_string());
88-
let git_commit = GIT_COMMIT.unwrap_or("");
89-
let git_tree_clean = GIT_TREE_CLEAN.unwrap_or("");
85+
let git_branch = GIT_BRANCH_ENV.unwrap_or(GIT_BRANCH.unwrap_or(""));
86+
let git_commit = GIT_COMMIT_ENV.unwrap_or(GIT_COMMIT.unwrap_or(""));
87+
let git_tree_clean = GIT_TREE_CLEAN_ENV.unwrap_or(GIT_TREE_CLEAN.unwrap_or(""));
9088

9189
format!(
9290
"{} {} ({}:{}{}, {} build, {} [{}])",

0 commit comments

Comments
 (0)