nushell/crates/nu-cli/build.rs
Shaurya Shubham 362bb1bea3
Add commit hash to version command (#2312)
* Add commit to version command

* Replace unwrap with expect.
2020-08-08 17:39:34 +12:00

42 lines
1.0 KiB
Rust

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(())
}