summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Nordholts <enselic@gmail.com>2021-10-23 16:32:39 +0200
committerMartin Nordholts <enselic@gmail.com>2021-10-24 11:36:55 +0200
commit4081ace4b90e049dce7867e4ec42cdeda6eb3009 (patch)
tree22ecb874c82a870393c67cb7ecb5ec9e12ac3232
parentdde770aa210ab9eeb5469e152cec6fcaab374d84 (diff)
Include git hash in `bat -V` and `bat --version` output if present
I had to use a `lazy_static` due to that the clap API that only accepts a reference to a version string. And, in our code, only a 'static reference to a version string. Code could probably be refactored to accept a "normal" reference, but that would be a major undertaking.
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/bin/bat/clap_app.rs17
2 files changed, 17 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b0ab9534..6e2fc2df 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@
- Add PowerShell completion, see #1826 (@rashil2000)
- Minimum supported Rust version (MSRV) bumped to 1.46
+- Include git hash in `bat -V` and `bat --version` output if present. See #1921 (@Enselic)
## Syntaxes
diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs
index 5e25b01a..dc74e574 100644
--- a/src/bin/bat/clap_app.rs
+++ b/src/bin/bat/clap_app.rs
@@ -2,6 +2,21 @@ use clap::{crate_name, crate_version, App as ClapApp, AppSettings, Arg, ArgGroup
use std::env;
use std::path::Path;
+lazy_static::lazy_static! {
+ static ref VERSION: String = {
+ #[cfg(feature = "bugreport")]
+ let git_version = bugreport::git_version!(fallback = "");
+ #[cfg(not(feature = "bugreport"))]
+ let git_version = "";
+
+ if git_version.is_empty() {
+ crate_version!().to_string()
+ } else {
+ format!("{} ({})", crate_version!(), git_version)
+ }
+ };
+}
+
pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
let clap_color_setting = if interactive_output && env::var_os("NO_COLOR").is_none() {
AppSettings::ColoredHelp
@@ -10,7 +25,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
};
let mut app = ClapApp::new(crate_name!())
- .version(crate_version!())
+ .version(VERSION.as_str())
.global_setting(clap_color_setting)
.global_setting(AppSettings::DeriveDisplayOrder)
.global_setting(AppSettings::UnifiedHelpMessage)