Add commit hash to version command (#2312)

* Add commit to version command

* Replace unwrap with expect.
This commit is contained in:
Shaurya Shubham 2020-08-08 11:09:34 +05:30 committed by GitHub
parent 724b177c97
commit 362bb1bea3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 1 deletions

18
Cargo.lock generated
View File

@ -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"

View File

@ -112,6 +112,8 @@ optional = true
version = "0.23.1"
[build-dependencies]
git2 = "0.13"
[dev-dependencies]
quickcheck = "0.9"

41
crates/nu-cli/build.rs Normal file
View File

@ -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<git2::Error> for Error {
fn from(git_error: git2::Error) -> Self {
Self::GitError(git_error)
}
}
impl From<io::Error> 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(())
}

View File

@ -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<OutputStream, ShellError> {
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))