Only have commit hash in version if git doesn't error (#2336)

* Add commit to version command

* Replace unwrap with expect.

* Only have commit hash if git doesn't error

Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
Shaurya Shubham 2020-08-13 00:21:12 +05:30 committed by GitHub
parent 1601aa2f49
commit 22519c9083
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 0 deletions

18
Cargo.lock generated
View file

@ -1787,6 +1787,8 @@ dependencies = [
"bitflags",
"libc",
"libgit2-sys",
"openssl-probe",
"openssl-sys",
"log 0.4.11",
"url 2.1.1",
]
@ -2458,7 +2460,9 @@ checksum = "9b33bf3d9d4c45b48ae1ea7c334be69994624dc0a69f833d5d9f7605f24b552b"
dependencies = [
"cc",
"libc",
"libssh2-sys",
"libz-sys",
"openssl-sys",
"pkg-config",
]
@ -2483,6 +2487,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

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

28
crates/nu-cli/build.rs Normal file
View file

@ -0,0 +1,28 @@
use git2::Repository;
use std::path::Path;
use std::{env, fs, io};
fn main() -> Result<(), io::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 = latest_commit_hash(env::current_dir()?).unwrap_or_default();
let commit_hash_path = Path::new(&out_dir).join("git_commit_hash");
fs::write(commit_hash_path, latest_commit_hash)?;
Ok(())
}
fn latest_commit_hash<P: AsRef<Path>>(dir: P) -> Result<String, git2::Error> {
let dir = dir.as_ref();
Ok(Repository::discover(dir)?
.head()?
.peel_to_commit()?
.id()
.to_string())
}

View file

@ -5,6 +5,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]
@ -48,6 +50,14 @@ pub fn version(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputS
UntaggedValue::string(clap::crate_version!()).into_value(&tag),
);
let commit_hash = Some(GIT_COMMIT_HASH.trim()).filter(|x| x.is_empty());
if let Some(commit_hash) = commit_hash {
indexmap.insert(
"commit_hash".to_string(),
UntaggedValue::string(commit_hash).into_value(&tag),
);
}
indexmap.insert("features".to_string(), features_enabled(&tag).into_value());
let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag);