diff --git a/Cargo.lock b/Cargo.lock index 2f429bdce1..f076743662 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1600,6 +1600,8 @@ dependencies = [ "libc", "libgit2-sys", "log", + "openssl-probe", + "openssl-sys", "url", ] @@ -2117,7 +2119,9 @@ checksum = "9b33bf3d9d4c45b48ae1ea7c334be69994624dc0a69f833d5d9f7605f24b552b" dependencies = [ "cc", "libc", + "libssh2-sys", "libz-sys", + "openssl-sys", "pkg-config", ] @@ -2142,6 +2146,20 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "libssh2-sys" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eafa907407504b0e683786d4aba47acf250f114d37357d56608333fd167dd0fc" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + [[package]] name = "libz-sys" version = "1.0.25" diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index a853ffcbe9..9740c677ce 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -112,6 +112,8 @@ optional = true version = "0.23.1" [build-dependencies] +git2 = "0.13" + [dev-dependencies] quickcheck = "0.9" diff --git a/crates/nu-cli/build.rs b/crates/nu-cli/build.rs new file mode 100644 index 0000000000..7b255bec32 --- /dev/null +++ b/crates/nu-cli/build.rs @@ -0,0 +1,41 @@ +use std::path::Path; +use std::{env, fs, io}; + +use git2::Repository; + +#[derive(Debug)] +enum Error { + IoError(io::Error), + GitError(git2::Error), +} + +impl From for Error { + fn from(git_error: git2::Error) -> Self { + Self::GitError(git_error) + } +} + +impl From for Error { + fn from(io_error: io::Error) -> Self { + Self::IoError(io_error) + } +} + +fn main() -> Result<(), Error> { + let out_dir = env::var_os("OUT_DIR").expect( + "\ + OUT_DIR environment variable not found. \ + OUT_DIR is guaranteed to to exist in a build script by cargo - see \ + https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts\ + "); + let latest_commit_hash = Repository::discover(env::current_dir()?)? + .head()? + .peel_to_commit()? + .id() + .to_string(); + + let commit_hash_path = Path::new(&out_dir).join("git_commit_hash"); + fs::write(commit_hash_path, latest_commit_hash)?; + + Ok(()) +} diff --git a/crates/nu-cli/src/commands/version.rs b/crates/nu-cli/src/commands/version.rs index 1b1f17956a..7d013d2134 100644 --- a/crates/nu-cli/src/commands/version.rs +++ b/crates/nu-cli/src/commands/version.rs @@ -4,6 +4,8 @@ use indexmap::IndexMap; use nu_errors::ShellError; use nu_protocol::{Dictionary, Signature, UntaggedValue}; +const GIT_COMMIT_HASH: &str = include_str!(concat!(env!("OUT_DIR"), "/git_commit_hash")); + pub struct Version; #[async_trait] @@ -40,11 +42,16 @@ impl WholeStreamCommand for Version { pub fn version(args: CommandArgs, _registry: &CommandRegistry) -> Result { let tag = args.call_info.args.span; - let mut indexmap = IndexMap::new(); + let mut indexmap = IndexMap::with_capacity(2); + indexmap.insert( "version".to_string(), UntaggedValue::string(clap::crate_version!()).into_value(&tag), ); + indexmap.insert( + "commit_hash".to_string(), + UntaggedValue::string(GIT_COMMIT_HASH).into_value(&tag), + ); let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag); Ok(OutputStream::one(value))