Skip to content

Commit 11a28ec

Browse files
Add crate version and commit hash to ldk-server
We will now log the version number and commit hash when starting up. Also will give the full commit hash when doing ldk-server --version to better help debugging things. Co-authored-by: Leo Nash <hello@leonash.net>
1 parent 1310ba2 commit 11a28ec

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

ldk-server/build.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
use std::env;
11+
use std::path::Path;
12+
use std::process::Command;
13+
14+
fn main() {
15+
println!("cargo:rerun-if-changed=build.rs");
16+
println!("cargo:rerun-if-env-changed=GIT_HASH");
17+
18+
// Re-run the build script whenever the checked-out commit changes so the
19+
// embedded hash never goes stale. HEAD lives in the (possibly worktree-local)
20+
// git dir, while branch refs and packed-refs live in the common git dir.
21+
if let Some(git_dir) = git_output(&["rev-parse", "--git-dir"]) {
22+
watch_path(&Path::new(&git_dir).join("HEAD"));
23+
}
24+
if let Some(common_dir) = git_output(&["rev-parse", "--git-common-dir"]) {
25+
let common_dir = Path::new(&common_dir);
26+
watch_path(&common_dir.join("packed-refs"));
27+
// If HEAD points at a ref, watch that ref file too (it changes on commit).
28+
// Watch it even when it does not exist yet: packed refs become loose
29+
// files on the next commit, and Cargo can detect that creation.
30+
if let Some(ref_path) = git_output(&["symbolic-ref", "-q", "HEAD"]) {
31+
watch_path(&common_dir.join(ref_path));
32+
}
33+
}
34+
35+
let git_hash = git_output(&["rev-parse", "HEAD"])
36+
.or_else(|| env::var("GIT_HASH").ok())
37+
.unwrap_or_else(|| "unknown".to_string());
38+
println!("cargo:rustc-env=GIT_HASH={git_hash}");
39+
}
40+
41+
/// Runs `git` with the given args, returning the trimmed stdout on success or
42+
/// `None` if git is unavailable, exits non-zero, or produces no output.
43+
fn git_output(args: &[&str]) -> Option<String> {
44+
let output = Command::new("git").args(args).output().ok()?;
45+
if !output.status.success() {
46+
return None;
47+
}
48+
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
49+
if stdout.is_empty() {
50+
None
51+
} else {
52+
Some(stdout)
53+
}
54+
}
55+
56+
fn watch_path(path: &Path) {
57+
println!("cargo:rerun-if-changed={}", path.display());
58+
}

ldk-server/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use crate::util::tls::get_or_generate_tls_config;
5757
use crate::util::{systemd, write_new};
5858

5959
const API_KEY_FILE: &str = "api_key";
60+
const FULL_VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("GIT_HASH"), ")");
6061

6162
pub fn get_default_data_dir() -> Option<PathBuf> {
6263
#[cfg(target_os = "macos")]
@@ -240,7 +241,7 @@ fn main() {
240241
let (event_sender, _) = broadcast::channel::<EventEnvelope>(1024);
241242
let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
242243

243-
info!("Starting up...");
244+
info!("Starting ldk-server version {FULL_VERSION}");
244245
match node.start() {
245246
Ok(()) => {},
246247
Err(e) => {

ldk-server/src/util/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ impl TryFrom<&LSPSClientTomlConfig> for LSPSClientConfig {
742742

743743
#[derive(Parser, Debug)]
744744
#[command(
745-
version,
745+
version = crate::FULL_VERSION,
746746
about = "LDK Server Configuration",
747747
long_about = None,
748748
override_usage = "ldk-server [config_path]"

0 commit comments

Comments
 (0)