mirror of
https://github.com/nushell/nushell.git
synced 2025-08-16 09:58:27 +02:00
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:
@ -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
28
crates/nu-cli/build.rs
Normal 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())
|
||||
}
|
@ -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);
|
||||
|
Reference in New Issue
Block a user