aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--build.rs41
-rw-r--r--src/consts.rs3
-rw-r--r--src/lib.rs1
-rw-r--r--src/run/mod.rs3
4 files changed, 48 insertions, 0 deletions
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<BString, Self::Error> {
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']),
_ => {}
}