From 74c34e885e948b8141972d2b2e28da378daeacf7 Mon Sep 17 00:00:00 2001
From: Charles Hall <charles@computer.surgery>
Date: Fri, 22 Mar 2024 19:50:57 -0400
Subject: [PATCH] allow including extra info in `--version` output

Signed-off-by: strawberry <strawberry@puppygock.gay>
---
 Cargo.toml  |  1 +
 flake.nix   |  1 +
 src/clap.rs | 16 +++++++++++++++-
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/Cargo.toml b/Cargo.toml
index d6075dd7..d350438f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -218,6 +218,7 @@ features = [
   "help",
   "usage",
   "error-context",
+  "string"
 ]
 
 [dependencies.futures-util]
diff --git a/flake.nix b/flake.nix
index 66b2598a..16ac7e5d 100644
--- a/flake.nix
+++ b/flake.nix
@@ -73,6 +73,7 @@
       ];
 
       env = pkgs: {
+        CONDUIT_VERSION_EXTRA = self.shortRev or self.dirtyShortRev;
         ROCKSDB_INCLUDE_DIR = "${rocksdb' pkgs}/include";
         ROCKSDB_LIB_DIR = "${rocksdb' pkgs}/lib";
       }
diff --git a/src/clap.rs b/src/clap.rs
index 446398c7..f1748b71 100644
--- a/src/clap.rs
+++ b/src/clap.rs
@@ -4,9 +4,23 @@ use std::path::PathBuf;
 
 use clap::Parser;
 
+/// Returns the current version of the crate with extra info if supplied
+///
+/// Set the environment variable `CONDUIT_VERSION_EXTRA` to any UTF-8 string to
+/// include it in parenthesis after the SemVer version. A common value are git
+/// commit hashes.
+fn version() -> String {
+	let cargo_pkg_version = env!("CARGO_PKG_VERSION");
+
+	match option_env!("CONDUIT_VERSION_EXTRA") {
+		Some(x) => format!("{} ({})", cargo_pkg_version, x),
+		None => cargo_pkg_version.to_owned(),
+	}
+}
+
 /// Commandline arguments
 #[derive(Parser, Debug)]
-#[clap(version, about, long_about = None)]
+#[clap(version = version(), about, long_about = None)]
 pub struct Args {
 	#[arg(short, long)]
 	/// Optional argument to the path of a conduwuit config TOML file