|
1 | 1 | use std::path::Path;
|
| 2 | +use std::process::Command; |
2 | 3 | use std::{env, fs};
|
3 | 4 |
|
4 | 5 | use toml::Value;
|
5 | 6 |
|
| 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 | + |
6 | 62 | fn main() {
|
7 | 63 | let toml_content =
|
8 | 64 | fs::read_to_string("../versions.toml").expect("Failed to read versions.toml");
|
@@ -34,6 +90,40 @@ fn main() {
|
34 | 90 | }
|
35 | 91 | }
|
36 | 92 |
|
| 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 | + |
37 | 127 | let out_dir = env::var_os("OUT_DIR").unwrap();
|
38 | 128 | let dest_path = Path::new(&out_dir).join("versions.rs");
|
39 | 129 | fs::write(&dest_path, rust_code).expect("Failed to write generated code");
|
|
0 commit comments