forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose version info in CLI tool with build-time obtained git hash (pa…
…ritytech#787) * version info with built-time obtained git hash * clippy * rerun-if-changed properly and handle git command failing * cargo fmt
- Loading branch information
Showing
5 changed files
with
55 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::{ | ||
borrow::Cow, | ||
process::Command, | ||
}; | ||
|
||
fn main() { | ||
// Make git hash available via GIT_HASH build-time env var: | ||
output_git_short_hash(); | ||
} | ||
|
||
fn output_git_short_hash() { | ||
let output = Command::new("git") | ||
.args(["rev-parse", "--short=11", "HEAD"]) | ||
.output(); | ||
|
||
let git_hash = match output { | ||
Ok(o) if o.status.success() => { | ||
let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned(); | ||
Cow::from(sha) | ||
} | ||
Ok(o) => { | ||
println!("cargo:warning=Git command failed with status: {}", o.status); | ||
Cow::from("unknown") | ||
} | ||
Err(err) => { | ||
println!("cargo:warning=Failed to execute git command: {}", err); | ||
Cow::from("unknown") | ||
} | ||
}; | ||
|
||
println!("cargo:rustc-env=GIT_HASH={}", git_hash); | ||
println!("cargo:rerun-if-changed=../.git/HEAD"); | ||
println!("cargo:rerun-if-changed=../.git/refs"); | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
} |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
pub mod codegen; | ||
pub mod compatibility; | ||
pub mod metadata; | ||
pub mod version; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use clap::Parser as ClapParser; | ||
|
||
/// Prints version information | ||
#[derive(Debug, ClapParser)] | ||
pub struct Opts {} | ||
|
||
pub fn run(_opts: Opts) -> color_eyre::Result<()> { | ||
let git_hash = env!("GIT_HASH"); | ||
println!( | ||
"{} {}-{}", | ||
clap::crate_name!(), | ||
clap::crate_version!(), | ||
git_hash | ||
); | ||
Ok(()) | ||
} |
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