summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoreth3lbert <eth3lbert+dev@gmail.com>2023-08-18 23:10:47 +0800
committerGitHub <noreply@github.com>2023-08-18 08:10:47 -0700
commitb6d8ecbd4f3f863b2c587ff367f5409787f7c2f8 (patch)
treeecbdb79ffa408a6a91766c9b905af8b924d94770
parent803a3a673ce6e26f6e92167c7309589cf5456202 (diff)
feat: display commit info in --version (#558)
This improves --version output for #554.
-rw-r--r--build.rs26
-rw-r--r--src/main.rs1
-rw-r--r--src/options.rs5
-rw-r--r--src/version.rs50
4 files changed, 79 insertions, 3 deletions
diff --git a/build.rs b/build.rs
index a0ee98766..06a81db8c 100644
--- a/build.rs
+++ b/build.rs
@@ -7,7 +7,7 @@
#![allow(clippy::if_same_then_else)]
use rayon::prelude::*;
-use std::path::PathBuf;
+use std::{path::PathBuf, process::Command};
use version_check as rustc;
struct TreeSitterParser {
@@ -363,4 +363,28 @@ fn main() {
}
parsers.par_iter().for_each(|p| p.build());
+ commit_info();
+}
+
+fn commit_info() {
+ if !PathBuf::from(".git").exists() {
+ return;
+ }
+
+ let output = match Command::new("git")
+ .arg("log")
+ .arg("-1")
+ .arg("--date=short")
+ .arg("--format=%H %h %cd")
+ .output()
+ {
+ Ok(output) if output.status.success() => output,
+ _ => return,
+ };
+ let stdout = String::from_utf8(output.stdout).unwrap();
+ let mut parts = stdout.split_whitespace();
+ let mut next = || parts.next().unwrap();
+ println!("cargo:rustc-env=DFT_COMMIT_HASH={}", next());
+ println!("cargo:rustc-env=DFT_COMMIT_SHORT_HASH={}", next());
+ println!("cargo:rustc-env=DFT_COMMIT_DATE={}", next())
}
diff --git a/src/main.rs b/src/main.rs
index 08daceffe..19f915797 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -36,6 +36,7 @@ mod options;
mod parse;
mod positions;
mod summary;
+mod version;
#[macro_use]
extern crate log;
diff --git a/src/options.rs b/src/options.rs
index 9dbeb34b1..1f36a1202 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -2,7 +2,7 @@
use std::{env, ffi::OsStr, path::Path, path::PathBuf};
-use clap::{crate_authors, crate_description, crate_version, Arg, Command};
+use clap::{crate_authors, crate_description, Arg, Command};
use const_format::formatcp;
use crossterm::tty::IsTty;
@@ -10,6 +10,7 @@ use crate::{
display::style::BackgroundColor,
exit_codes::EXIT_BAD_ARGUMENTS,
parse::guess_language::{language_override_from_name, LanguageOverride},
+ version::VERSION,
};
pub const DEFAULT_BYTE_LIMIT: usize = 1_000_000;
@@ -83,7 +84,7 @@ impl Default for DiffOptions {
fn app() -> clap::Command<'static> {
Command::new("Difftastic")
.override_usage(USAGE)
- .version(crate_version!())
+ .version(VERSION.as_str())
.about(crate_description!())
.author(crate_authors!())
.after_long_help(concat!(
diff --git a/src/version.rs b/src/version.rs
new file mode 100644
index 000000000..8391e2674
--- /dev/null
+++ b/src/version.rs
@@ -0,0 +1,50 @@
+use std::fmt;
+
+use lazy_static::lazy_static;
+
+pub struct CommitInfo {
+ pub short_commit_hash: &'static str,
+ pub commit_hash: &'static str,
+ pub commit_date: &'static str,
+}
+
+pub struct VersionInfo {
+ pub version: &'static str,
+ pub commit_info: Option<CommitInfo>,
+}
+
+impl fmt::Display for VersionInfo {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", self.version)?;
+
+ if let Some(ref ci) = &self.commit_info {
+ write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?;
+ }
+ Ok(())
+ }
+}
+
+lazy_static! {
+ pub static ref VERSION: String = version().to_string();
+}
+
+pub const fn version() -> VersionInfo {
+ let version = env!("CARGO_PKG_VERSION");
+ let commit_info = match (
+ option_env!("DFT_COMMIT_SHORT_HASH"),
+ option_env!("DFT_COMMIT_HASH"),
+ option_env!("DFT_COMMIT_DATE"),
+ ) {
+ (Some(short_commit_hash), Some(commit_hash), Some(commit_date)) => Some(CommitInfo {
+ short_commit_hash,
+ commit_hash,
+ commit_date,
+ }),
+ _ => None,
+ };
+
+ VersionInfo {
+ version,
+ commit_info,
+ }
+}