-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #163 Handle git dependency in build.rs * warning litteral * println Co-authored-by: jacek-casper <[email protected]> --------- Co-authored-by: jacek-casper <[email protected]>
- Loading branch information
1 parent
6b61f16
commit 7049b8e
Showing
1 changed file
with
25 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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,21 +1,35 @@ | ||
use std::io; | ||
use std::process::Command; | ||
|
||
const GIT_HASH_ENV_VAR: &str = "GIT_SHA_SHORT"; | ||
|
||
fn main() { | ||
//Build command to retrieve the short git commit hash | ||
let git_process_output = Command::new("git") | ||
match get_git_commit_hash() { | ||
// If the git commit hash is retrieved successfully, set the environment variable | ||
Ok(git_hash) => { | ||
println!("cargo:rustc-env={GIT_HASH_ENV_VAR}={git_hash}"); | ||
} | ||
// If there's an error retrieving the git commit hash, print a note and set the environment variable to "unknown" | ||
Err(e) => { | ||
println!("cargo:warning=Note: Failed to get git commit hash: {}", e); | ||
println!("cargo:rustc-env={GIT_HASH_ENV_VAR}=unknown"); | ||
} | ||
} | ||
} | ||
|
||
fn get_git_commit_hash() -> Result<String, io::Error> { | ||
// Build the command to retrieve the short git commit hash | ||
let output = Command::new("git") | ||
.arg("rev-parse") | ||
.arg("--short") | ||
.arg("HEAD") | ||
.output() | ||
.expect("Failed to retrieve short git commit hash"); | ||
|
||
//Parse the raw output into a string, we still need to remove the newline character | ||
let git_hash_raw = | ||
String::from_utf8(git_process_output.stdout).expect("Failed to convert git hash to string"); | ||
//Remove the newline character from the short git commit hash | ||
let git_hash = git_hash_raw.trim_end_matches('\n'); | ||
.output()?; | ||
|
||
println!("cargo:rustc-env={}={}", GIT_HASH_ENV_VAR, git_hash); | ||
if output.status.success() { | ||
// Parse the raw output into a string and trim the newline character | ||
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string()) | ||
} else { | ||
// Return an error if the command failed | ||
Err(io::Error::new(io::ErrorKind::Other, "Git command failed")) | ||
} | ||
} |