Skip to content

Commit bc459f5

Browse files
committed
Add a feature flag to include git commit hash to the version string
Signed-off-by: Guvenc Gulce <[email protected]>
1 parent 6cd2bf1 commit bc459f5

File tree

9 files changed

+50
-6
lines changed

9 files changed

+50
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ all: clippy
99
cargo build --target=x86_64-unknown-linux-musl --all
1010

1111
release: clippy
12-
cargo build --release --target=x86_64-unknown-linux-musl --all
12+
cargo build --release --features git-version --target=x86_64-unknown-linux-musl --all
1313

1414
run: all
1515
sudo ./target/debug/feos --ipam $(IPAM)

feos/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ prost = { workspace = true }
5656
hyper-util = { workspace = true }
5757
once_cell = { workspace = true }
5858
tower = { workspace = true }
59-
tempfile = { workspace = true }
59+
tempfile = { workspace = true }
60+
61+
[features]
62+
git-version = ["feos-utils/git-version"]

feos/services/host-service/src/worker/info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ pub async fn handle_get_version_info(
325325
let result = fs::read_to_string(path)
326326
.await
327327
.map(|kernel_version| {
328-
let feos_version = env!("CARGO_PKG_VERSION").to_string();
328+
let feos_version = feos_utils::version::full_version_string();
329329
GetVersionInfoResponse {
330330
kernel_version: kernel_version.trim().to_string(),
331331
feos_version,

feos/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use image_service::IMAGE_SERVICE_SOCKET;
99
use log::{error, info, warn};
1010
use nix::unistd::Uid;
1111
use setup::*;
12-
use std::env;
1312
use tokio::{fs, net::UnixListener, sync::mpsc};
1413
use tokio_stream::wrappers::UnixListenerStream;
1514
use tonic::transport::Server;
@@ -25,7 +24,7 @@ pub async fn run_server(restarted_after_upgrade: bool) -> Result<()> {
2524
╚═╝ ╚══════╝ ╚═════╝ ╚══════╝
2625
v{}
2726
",
28-
env!("CARGO_PKG_VERSION")
27+
feos_utils::version::full_version_string()
2928
);
3029

3130
let log_handle = feos_utils::feos_logger::Builder::new()

feos/utils/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ netlink-packet-route = { workspace = true }
1515
pnet = { workspace = true }
1616
rtnetlink = { workspace = true }
1717
socket2 = { workspace = true }
18-
libc = { workspace = true }
18+
libc = { workspace = true }
19+
20+
[build-dependencies]
21+
cc = "1.0"
22+
23+
[features]
24+
default = []
25+
git-version = []

feos/utils/build.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::process::Command;
5+
6+
fn main() {
7+
println!("cargo:rerun-if-changed=.git/HEAD");
8+
9+
let output = Command::new("git")
10+
.args(["rev-parse", "--short", "HEAD"])
11+
.output();
12+
13+
if let Ok(output) = output {
14+
if output.status.success() {
15+
let commit_hash = String::from_utf8_lossy(&output.stdout).trim().to_string();
16+
println!("cargo:rustc-env=GIT_COMMIT_HASH={commit_hash}");
17+
}
18+
}
19+
}

feos/utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub mod feos_logger;
55
pub mod filesystem;
66
pub mod host;
77
pub mod network;
8+
pub mod version;

feos/utils/src/version.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#[cfg(not(feature = "git-version"))]
5+
pub fn full_version_string() -> String {
6+
env!("CARGO_PKG_VERSION").to_string()
7+
}
8+
9+
#[cfg(feature = "git-version")]
10+
pub fn full_version_string() -> String {
11+
let version = env!("CARGO_PKG_VERSION");
12+
let commit_hash = option_env!("GIT_COMMIT_HASH").unwrap_or("???????");
13+
format!("{} ({})", version, commit_hash)
14+
}

0 commit comments

Comments
 (0)