From deec78cdd37cb0e2ef7a5748128d6b9870bcdbbb Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 6 Aug 2022 12:52:11 -0700 Subject: [PATCH] cmake: Unset GIT_WORK_TREE and GIT_DIR before FetchContent_Populate GIT_WORK_TREE is an environment variable which tells git where the worktree is. It may be set by the user or by git itself, e.g. when running `git rebase -i --exec ...`. If it is set, it overrides the working directory, causing the `git checkout` from FetchContent_Populate to fail. Clear this variable. Do the same for GIT_DIR for the same reason. A way to reproduce the failure that this commit fixes is: git rebase -i HEAD^^^ --exec 'ninja -C /path/to/build/dir fish' prior to this commit, using the fetched PCRE2, this would fail in CMake. --- cmake/PCRE2.cmake | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmake/PCRE2.cmake b/cmake/PCRE2.cmake index b9138fe2e..8b3710131 100644 --- a/cmake/PCRE2.cmake +++ b/cmake/PCRE2.cmake @@ -54,6 +54,12 @@ else() # so we end up installing all of PCRE2 including its headers, man pages, etc. FetchContent_GetProperties(pcre2) if (NOT pcre2_POPULATED) + # If GIT_WORK_TREE is set (by user or by git itself with e.g. git rebase), it + # will override the git directory which CMake tries to apply in FetchContent_Populate, + # resulting in a failed checkout. + # Ensure it is not set. + unset(ENV{GIT_WORK_TREE}) + unset(ENV{GIT_DIR}) FetchContent_Populate(pcre2) add_subdirectory(${pcre2_SOURCE_DIR} ${pcre2_BINARY_DIR} EXCLUDE_FROM_ALL) endif()