on master help sign in
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.

commit4081ace4b90e049dce7867e4ec42cdeda6eb3009 authorMartin Nordholts <enselic@gmail.com> dateSat 23 Oct 2021, 16:32 (5 years ago) parentdde770a
2 files changed+17−1
CHANGELOG.md+1 src/bin/bat/clap_app.rs+16−1
CHANGELOG.md+1
@@ -20,6 +20,7 @@
2020
2121- Add PowerShell completion, see #1826 (@rashil2000)
2222- Minimum supported Rust version (MSRV) bumped to 1.46
23+- Include git hash in `bat -V` and `bat --version` output if present. See #1921 (@Enselic)
2324
2425## Syntaxes
2526
src/bin/bat/clap_app.rs+16−1
@@ -2,6 +2,21 @@ use clap::{crate_name, crate_version, App as ClapApp, AppSettings, Arg, ArgGroup
22use std::env;
33use std::path::Path;
44
5+lazy_static::lazy_static! {
6+ static ref VERSION: String = {
7+ #[cfg(feature = "bugreport")]
8+ let git_version = bugreport::git_version!(fallback = "");
9+ #[cfg(not(feature = "bugreport"))]
10+ let git_version = "";
11+
12+ if git_version.is_empty() {
13+ crate_version!().to_string()
14+ } else {
15+ format!("{} ({})", crate_version!(), git_version)
16+ }
17+ };
18+}
19+
520pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
621 let clap_color_setting = if interactive_output && env::var_os("NO_COLOR").is_none() {
722 AppSettings::ColoredHelp
@@ -10,7 +25,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
1025 };
1126
1227 let mut app = ClapApp::new(crate_name!())
13− .version(crate_version!())
28+ .version(VERSION.as_str())
1429 .global_setting(clap_color_setting)
1530 .global_setting(AppSettings::DeriveDisplayOrder)
1631 .global_setting(AppSettings::UnifiedHelpMessage)