feat: display commit info in --version (#558)

This improves --version output for #554.
id_arena_for_vertex
eth3lbert 2023-08-18 23:10:47 +07:00 committed by GitHub
parent 803a3a673c
commit b6d8ecbd4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 3 deletions

@ -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())
}

@ -36,6 +36,7 @@ mod options;
mod parse;
mod positions;
mod summary;
mod version;
#[macro_use]
extern crate log;

@ -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!(

@ -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,
}
}