diff --git a/cli/build.rs b/cli/build.rs index fc4702ae..0a26b95d 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,9 +1,10 @@ use std::path::PathBuf; -use std::{env, fs, io}; +use std::{env, fs}; fn main() { - let git_sha = read_git_sha().unwrap(); - println!("cargo:rustc-env={}={}", "BUILD_SHA", git_sha); + if let Some(git_sha) = read_git_sha() { + println!("cargo:rustc-env={}={}", "BUILD_SHA", git_sha); + } println!( "cargo:rustc-env=BUILD_TARGET={}", @@ -11,32 +12,43 @@ fn main() { ); } -fn read_git_sha() -> io::Result { +fn read_git_sha() -> Option { let mut repo_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let mut git_path; loop { git_path = repo_path.join(".git"); if git_path.exists() { break; - } else { - assert!(repo_path.pop()); + } else if !repo_path.pop() { + return None; } } - let git_head_path = git_path.join("HEAD"); - println!("cargo:rerun-if-changed={}", git_head_path.to_str().unwrap()); - let mut head_content = fs::read_to_string(&git_head_path)?; - assert!(head_content.ends_with("\n")); - head_content.pop(); - if head_content.starts_with("ref: ") { - // We're on a branch. Read the SHA from the ref file. - head_content.replace_range(0.."ref: ".len(), ""); - let ref_filename = git_path.join(&head_content); - println!("cargo:rerun-if-changed={}", ref_filename.to_str().unwrap()); - fs::read_to_string(&ref_filename) - } else { - // We're not on a branch. The `HEAD` file itself contains the sha. - assert_eq!(head_content.len(), 40); - Ok(head_content) + let git_head_path = git_path.join("HEAD"); + if let Some(path) = git_head_path.to_str() { + println!("cargo:rerun-if-changed={}", path); } + if let Ok(mut head_content) = fs::read_to_string(&git_head_path) { + if head_content.ends_with("\n") { + head_content.pop(); + } + + // If we're on a branch, read the SHA from the ref file. + if head_content.starts_with("ref: ") { + head_content.replace_range(0.."ref: ".len(), ""); + let ref_filename = git_path.join(&head_content); + if let Some(path) = ref_filename.to_str() { + println!("cargo:rerun-if-changed={}", path); + } + return fs::read_to_string(&ref_filename).ok(); + } + + // If we're on a detached commit, then the `HEAD` file itself contains the sha. + else if head_content.len() == 40 { + return Some(head_content); + } + } + + None } diff --git a/cli/src/main.rs b/cli/src/main.rs index 12dbe44e..7a6eba35 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -9,6 +9,9 @@ use tree_sitter_cli::{ config, error, generate, highlight, loader, logger, parse, properties, test, wasm, web_ui, }; +const BUILD_VERSION: &'static str = env!("CARGO_PKG_VERSION"); +const BUILD_SHA: Option<&'static str> = option_env!("BUILD_SHA"); + fn main() { if let Err(e) = run() { eprintln!("{}", e.message()); @@ -17,13 +20,14 @@ fn main() { } fn run() -> error::Result<()> { + let version = if let Some(build_sha) = BUILD_SHA { + format!("{} ({})", BUILD_VERSION, build_sha) + } else { + BUILD_VERSION.to_string() + }; + let matches = App::new("tree-sitter") - .version(concat!( - env!("CARGO_PKG_VERSION"), - " (", - env!("BUILD_SHA"), - ")" - )) + .version(version.as_str()) .setting(AppSettings::SubcommandRequiredElseHelp) .author("Max Brunsfeld ") .about("Generates and tests parsers")