From 8d020342f0c555c7d6f98179b8165fae246c6da6 Mon Sep 17 00:00:00 2001 From: Jonas Maier Date: Sat, 9 May 2026 18:00:35 +0200 Subject: more builtin variables --- build.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/consts.rs | 3 +++ src/lib.rs | 1 + src/run/mod.rs | 3 +++ 4 files changed, 48 insertions(+) create mode 100644 build.rs create mode 100644 src/consts.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..95a5eae --- /dev/null +++ b/build.rs @@ -0,0 +1,41 @@ +use std::process::Command; + +fn git(args: &[&str]) -> String { + let output = Command::new("git") + .args(args) + .output() + .expect("failed to execute git"); + + if !output.status.success() { + panic!("git command failed: git {}", args.join(" ")); + } + + String::from_utf8(output.stdout) + .expect("git output not utf8") + .trim() + .to_string() +} + +fn main() { + // Re-run if HEAD changes + println!("cargo:rerun-if-changed=.git/HEAD"); + println!("cargo:rerun-if-changed=.git/refs"); + + let git_hash = git(&["rev-parse", "HEAD"]); + let git_hash_short = git(&["rev-parse", "--short", "HEAD"]); + + let git_dirty = { + let status = Command::new("git") + .args(["diff", "--quiet"]) + .status() + .expect("failed to check git dirty state"); + + if status.success() { false } else { true } + }; + + println!("cargo:rustc-env=GIT_HASH={git_hash}"); + println!("cargo:rustc-env=GIT_HASH_SHORT={git_hash_short}"); + if git_dirty { + println!("cargo:rustc-env=GIT_DIRTY=1"); + } +} diff --git a/src/consts.rs b/src/consts.rs new file mode 100644 index 0000000..7e5d0fd --- /dev/null +++ b/src/consts.rs @@ -0,0 +1,3 @@ +pub const PISH_VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const PISH_COMMIT: &str = env!("GIT_HASH_SHORT"); +pub const PISH_DIRTY: bool = option_env!("GIT_DIRTY").is_some(); diff --git a/src/lib.rs b/src/lib.rs index 2af7e08..44ea346 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ use std::sync::{Arc, Mutex}; pub mod ansi; pub mod basedir; pub mod completion; +pub mod consts; pub mod ctrlc; pub mod cursor; pub mod date; diff --git a/src/run/mod.rs b/src/run/mod.rs index bae9567..ddac394 100644 --- a/src/run/mod.rs +++ b/src/run/mod.rs @@ -465,6 +465,9 @@ impl parse::Expander for Executor { ) -> Result { match &var[..] { b"CWD_PRETTY" => return Ok(crate::pretty_cwd().into_bytes()), + b"PISH_VERSION" => return Ok(crate::consts::PISH_VERSION.as_bytes().to_vec()), + b"PISH_COMMIT" => return Ok(crate::consts::PISH_COMMIT.as_bytes().to_vec()), + b"PISH_DIRTY" => return Ok(vec![crate::consts::PISH_DIRTY as u8 + b'0']), _ => {} } -- cgit v1.2.3