Skip to content

Commit

Permalink
Add git sha to version information (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored Oct 7, 2022
1 parent 740f921 commit ddfc06c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ version = "0.0.4"
edition = "2021"
rust-version = "1.64"
autobins = false
build = "build.rs"

[build-dependencies]
serde = "1.0.82"
serde_derive = "1.0.82"
serde_json = "1.0.82"

[[bin]]
name = "soroban"
Expand Down
43 changes: 43 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use serde_derive::{Deserialize, Serialize};
use std::{fs::read_to_string, process::Command, str};

fn main() {
println!("cargo:rerun-if-changed=.");

let mut git_sha: Option<String> = None;

if let Ok(vcs_info) = read_to_string(".cargo_vcs_info.json") {
let vcs_info: Result<CargoVcsInfo, _> = serde_json::from_str(&vcs_info);
if let Ok(vcs_info) = vcs_info {
git_sha = Some(vcs_info.git.sha1);
}
}

if git_sha.is_none() {
if let Ok(git_describe) = Command::new("git")
.arg("describe")
.arg("--always")
.arg("--exclude='*'")
.arg("--long")
.arg("--dirty")
.output()
.map(|o| o.stdout)
{
git_sha = str::from_utf8(&git_describe).ok().map(str::to_string);
}
}

if let Some(git_sha) = git_sha {
println!("cargo:rustc-env=GIT_SHA={}", git_sha);
}
}

#[derive(Serialize, Deserialize, Default)]
struct CargoVcsInfo {
git: CargoVcsInfoGit,
}

#[derive(Serialize, Deserialize, Default)]
struct CargoVcsInfoGit {
sha1: String,
}
10 changes: 8 additions & 2 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ use clap::Parser;
use soroban_env_host::meta;
use std::fmt::Debug;

const GIT_SHA: Option<&str> = option_env!("GIT_SHA");

#[derive(Parser, Debug)]
pub struct Cmd;

impl Cmd {
#[allow(clippy::unused_self)]
pub fn run(&self) {
println!("soroban-cli {}", env!("CARGO_PKG_VERSION"),);
println!("soroban-env interface {}", meta::INTERFACE_VERSION);
println!(
"soroban-cli {} ({})",
env!("CARGO_PKG_VERSION"),
GIT_SHA.unwrap_or_default(),
);
println!("soroban-env-interface-version: {}", meta::INTERFACE_VERSION);
}
}

0 comments on commit ddfc06c

Please sign in to comment.