Skip to content

Commit fd416d4

Browse files
authored
Merge pull request #5579 from stacks-network/feat/build-time-git-consts
feat: add build-time git info
2 parents e250a95 + 3035074 commit fd416d4

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

stacks-common/build.rs

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

45
use toml::Value;
56

7+
/// Given a [Command], run it and return the output as a string,
8+
/// returning `None` if the command fails.
9+
fn run_git_command(command: &mut Command) -> Option<String> {
10+
command
11+
.output()
12+
.map(|output| String::from_utf8(output.stdout).ok())
13+
.unwrap_or(None)
14+
.map(|s| s.trim().to_string())
15+
}
16+
17+
fn current_git_hash() -> Option<String> {
18+
option_env!("GIT_COMMIT").map(String::from).or_else(|| {
19+
run_git_command(
20+
Command::new("git")
21+
.arg("log")
22+
.arg("-1")
23+
.arg("--pretty=format:%h")
24+
.current_dir(env!("CARGO_MANIFEST_DIR")),
25+
)
26+
})
27+
}
28+
29+
fn current_git_branch() -> Option<String> {
30+
option_env!("GIT_BRANCH").map(String::from).or_else(|| {
31+
run_git_command(
32+
Command::new("git")
33+
.arg("rev-parse")
34+
.arg("--abbrev-ref")
35+
.arg("HEAD"),
36+
)
37+
})
38+
}
39+
40+
fn is_working_tree_clean() -> bool {
41+
Command::new("git")
42+
.arg("diff")
43+
.arg("--quiet")
44+
.arg("--exit-code")
45+
.current_dir(env!("CARGO_MANIFEST_DIR"))
46+
.status()
47+
.map(|status| status.code() == Some(0))
48+
.unwrap_or(true)
49+
}
50+
651
fn main() {
752
let toml_file = "../versions.toml";
853
let toml_content = fs::read_to_string(toml_file).expect("Failed to read versions.toml");
@@ -24,6 +69,29 @@ fn main() {
2469
));
2570
}
2671

72+
let git_commit = current_git_hash();
73+
rust_code.push_str(&format!(
74+
"pub const GIT_COMMIT: Option<&'static str> = {git_commit:?};\n",
75+
));
76+
if let Some(git_commit) = git_commit {
77+
println!("cargo:rustc-env=GIT_COMMIT={}", git_commit);
78+
}
79+
80+
let git_branch = current_git_branch();
81+
rust_code.push_str(&format!(
82+
"pub const GIT_BRANCH: Option<&'static str> = {git_branch:?};\n",
83+
));
84+
if let Some(git_branch) = git_branch {
85+
println!("cargo:rustc-env=GIT_BRANCH={}", git_branch);
86+
}
87+
88+
let is_clean = if is_working_tree_clean() { "" } else { "+" };
89+
rust_code.push_str(&format!(
90+
"pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\"{}\");\n",
91+
is_clean
92+
));
93+
println!("cargo:rustc-env=GIT_TREE_CLEAN={}", is_clean);
94+
2795
let out_dir = env::var_os("OUT_DIR").unwrap();
2896
let dest_path = Path::new(&out_dir).join("versions.rs");
2997
fs::write(&dest_path, rust_code).expect("Failed to write generated code");

stackslib/src/lib.rs

Lines changed: 7 additions & 7 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<&str> = option_env!("GIT_BRANCH");
75-
const GIT_COMMIT: Option<&str> = option_env!("GIT_COMMIT");
76-
const GIT_TREE_CLEAN: Option<&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: &str = "debug";
@@ -82,9 +82,9 @@ const BUILD_TYPE: &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.unwrap_or("");
86-
let git_commit = GIT_COMMIT.unwrap_or("");
87-
let git_tree_clean = GIT_TREE_CLEAN.unwrap_or("");
85+
let git_branch = GIT_BRANCH_ENV.unwrap_or_else(|| GIT_BRANCH.unwrap_or(""));
86+
let git_commit = GIT_COMMIT_ENV.unwrap_or_else(|| GIT_COMMIT.unwrap_or(""));
87+
let git_tree_clean = GIT_TREE_CLEAN_ENV.unwrap_or_else(|| GIT_TREE_CLEAN.unwrap_or(""));
8888

8989
format!(
9090
"{} {} ({}:{}{}, {} build, {} [{}])",

0 commit comments

Comments
 (0)