Skip to content

Commit

Permalink
always add commit hash to version.build for /status endpoint (#12722
Browse files Browse the repository at this point in the history
)

fix #12624

After locally adding a tag `5.5.5`


before changes:

```
curl -s localhost:3030/status | jq -Mr .version
{
  "version": "trunk",
  "build": "5.5.5",
  "rustc_version": "1.83.0"
}
```

after changes:

```
curl -s localhost:3030/status | jq -Mr .version
{
  "version": "trunk",
  "build": "5.5.5-0-g1dd1b0a92",
  "rustc_version": "1.83.0"
}
```

---------

Co-authored-by: Adam Chudaś <[email protected]>
  • Loading branch information
qiweiii and staffik authored Feb 11, 2025
1 parent b411263 commit fb95d7b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::LazyLock;
pub struct Version {
pub version: String,
pub build: String,
pub commit: String,
#[serde(default)]
pub rustc_version: String,
}
Expand Down
31 changes: 18 additions & 13 deletions neard/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,9 @@ fn command(prog: &str, args: &[&str], cwd: Option<std::path::PathBuf>) -> Result
}
}

/// Returns version read from git repository or ‘unknown’ if could not be
/// determined.
///
/// Uses `git describe --always --dirty=-modified` to get the version. For
/// builds on release tags this will return that tag. In other cases the
/// version will describe the commit by including its hash. If the working
/// directory isn’t clean, the version will include `-modified` suffix.
fn get_git_version() -> Result<String> {
/// Returns version info (build, commit) read from git repository or (unknown, unknown) if could
/// not be determined.
fn get_git_version() -> Result<(String, String)> {
// Figure out git directory. Don’t just assume it’s ../.git because that
// doesn’t work with git work trees so use `git rev-parse --git-dir` instead.
let pkg_dir = std::path::PathBuf::from(env("CARGO_MANIFEST_DIR")?);
Expand All @@ -66,7 +61,7 @@ fn get_git_version() -> Result<String> {
// version as unknown.
println!("cargo:warning=unable to determine git version (not in git repository?)");
println!("cargo:warning={}", msg);
return Ok("unknown".to_string());
return Ok(("unknown".to_string(), "unknown".to_string()));
}
};

Expand All @@ -85,11 +80,19 @@ fn get_git_version() -> Result<String> {
// * --match=[0-9]* → only consider tags starting with a digit; this
// prevents tags such as `crates-0.14.0` from being considered
let args = &["describe", "--always", "--dirty=-modified", "--tags", "--match=[0-9]*"];
let out = command("git", args, None)?;
match String::from_utf8_lossy(&out) {
let build = command("git", args, None)?;
let build_str = match String::from_utf8_lossy(&build) {
std::borrow::Cow::Borrowed(version) => Ok(version.trim().to_string()),
std::borrow::Cow::Owned(version) => Err(anyhow!("git: invalid output: {}", version)),
}
};

let commit = command("git", &["rev-parse", "HEAD"], None)?;
let commit_hash = match String::from_utf8_lossy(&commit) {
std::borrow::Cow::Borrowed(version) => Ok(version.trim().to_string()),
std::borrow::Cow::Owned(version) => Err(anyhow!("git: invalid output: {}", version)),
};

Ok((build_str?, commit_hash?))
}

/// Get features enabled using the --features flag.
Expand Down Expand Up @@ -131,7 +134,9 @@ fn try_main() -> Result<()> {
};
println!("cargo:rustc-env=NEARD_VERSION={}", version);

println!("cargo:rustc-env=NEARD_BUILD={}", get_git_version()?);
println!("cargo:rustc-env=NEARD_BUILD={}", get_git_version()?.0);

println!("cargo:rustc-env=NEARD_COMMIT={}", get_git_version()?.1);

println!("cargo:rustc-env=NEARD_RUSTC_VERSION={}", rustc_version::version()?);

Expand Down
1 change: 1 addition & 0 deletions neard/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl NeardCmd {
target: "neard",
version = crate::NEARD_VERSION,
build = crate::NEARD_BUILD,
commit = crate::NEARD_COMMIT,
latest_protocol = near_primitives::version::PROTOCOL_VERSION
);

Expand Down
12 changes: 10 additions & 2 deletions neard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@ use std::time::Duration;

static NEARD_VERSION: &str = env!("NEARD_VERSION");
static NEARD_BUILD: &str = env!("NEARD_BUILD");
static NEARD_COMMIT: &str = env!("NEARD_COMMIT");
static RUSTC_VERSION: &str = env!("NEARD_RUSTC_VERSION");
static NEARD_FEATURES: &str = env!("NEARD_FEATURES");

static NEARD_VERSION_STRING: LazyLock<String> = LazyLock::new(|| {
format!(
"(release {}) (build {}) (rustc {}) (protocol {}) (db {})\nfeatures: [{}]",
NEARD_VERSION, NEARD_BUILD, RUSTC_VERSION, PROTOCOL_VERSION, DB_VERSION, NEARD_FEATURES
"(release {}) (build {}) (commit {}) (rustc {}) (protocol {}) (db {})\nfeatures: [{}]",
NEARD_VERSION,
NEARD_BUILD,
NEARD_COMMIT,
RUSTC_VERSION,
PROTOCOL_VERSION,
DB_VERSION,
NEARD_FEATURES
)
});

fn neard_version() -> Version {
Version {
version: NEARD_VERSION.to_string(),
build: NEARD_BUILD.to_string(),
commit: NEARD_COMMIT.to_string(),
rustc_version: RUSTC_VERSION.to_string(),
}
}
Expand Down

0 comments on commit fb95d7b

Please sign in to comment.