@@ -53,20 +53,38 @@ macro(ASPECT_QUERY_GIT_INFORMATION)
5353
5454 find_package (Git)
5555
56+
57+ set (_head_location "" )
58+
5659 #
5760 # Only run the following if we have git and the source directory seems to
5861 # be under version control.
5962 #
60- if (GIT_FOUND AND EXISTS ${CMAKE_SOURCE_DIR} /.git/HEAD)
61- #
63+ if (GIT_FOUND AND EXISTS ${CMAKE_SOURCE_DIR} /.git)
64+
65+ if (EXISTS ${CMAKE_SOURCE_DIR} /.git/HEAD)
66+ # A normal git repository
67+ set (_head_location ${CMAKE_SOURCE_DIR} /.git/HEAD)
68+ else ()
69+ # Likely a git worktree: Read .git file, which has the format "gitdir: <absolute path>"
70+ file (STRINGS ${CMAKE_SOURCE_DIR} /.git _gitdir LIMIT_COUNT 1)
71+ string (REGEX REPLACE "gitdir: " "" _gitdir ${_gitdir} )
72+ if (EXISTS ${_gitdir} /HEAD)
73+ set (_head_location ${_gitdir} /HEAD)
74+ endif ()
75+ endif ()
76+ endif ()
77+
78+ if (EXISTS ${_head_location} )
79+ # We have a git repository, now grab the details
80+
6281 # Bogus configure_file calls to trigger a reconfigure, and thus an
6382 # update of branch and commit information every time HEAD has changed.
64- #
6583 configure_file (
66- ${CMAKE_SOURCE_DIR} /.git/HEAD
84+ ${_head_location}
6785 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY} /HEAD
6886 )
69- file (STRINGS ${CMAKE_SOURCE_DIR} /.git/HEAD _head_ref LIMIT_COUNT 1)
87+ file (STRINGS ${_head_location} _head_ref LIMIT_COUNT 1)
7088 string (REPLACE "ref: " "" _head_ref ${_head_ref} )
7189 if (EXISTS ${CMAKE_SOURCE_DIR} /.git/${_head_ref} )
7290 configure_file (
@@ -78,7 +96,6 @@ macro(ASPECT_QUERY_GIT_INFORMATION)
7896 #
7997 # Query for revision:
8098 #
81-
8299 execute_process (
83100 COMMAND ${GIT_EXECUTABLE} log -n 1 --pretty=format:"%H %h"
84101 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
0 commit comments