From 276d3cb5adb9c43cb02cf8edb7deb18c9ba6f3f8 Mon Sep 17 00:00:00 2001 From: Matthew Krupcale Date: Mon, 12 Aug 2019 17:11:59 -0400 Subject: [PATCH] tree-sitter-cli: build: add support for git submodules (#422) The .git file can contain a reference to the actual git directory as is commonly the case for a submodule[1]. When this is the case, read the .git file to discover the actual git directory. * cli/build.rs: Read the .git file to discover the git directory [1] https://git-scm.com/docs/gitrepository-layout --- cli/build.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index 0a26b95d..0ed9ef06 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -25,7 +25,15 @@ fn read_git_sha() -> Option { } } - let git_head_path = git_path.join("HEAD"); + let git_dir_path; + if git_path.is_dir() { + git_dir_path = git_path; + } else if let Ok(git_path_content) = fs::read_to_string(&git_path) { + git_dir_path = repo_path.join(git_path_content.get("gitdir: ".len()..).unwrap().trim_end()); + } else { + return None; + } + let git_head_path = git_dir_path.join("HEAD"); if let Some(path) = git_head_path.to_str() { println!("cargo:rerun-if-changed={}", path); } @@ -37,7 +45,7 @@ fn read_git_sha() -> Option { // 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); + let ref_filename = git_dir_path.join(&head_content); if let Some(path) = ref_filename.to_str() { println!("cargo:rerun-if-changed={}", path); }