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.
This commit is contained in:
ridiculousfish 2022-08-06 12:52:11 -07:00
parent 39fbc27d8d
commit deec78cdd3

View File

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