Skip to content

Commit 1b372e1

Browse files
authored
Merge pull request #6751 from tjhei/git-worktree-support
CMake: support worktrees for git information
2 parents 3f408f2 + 32abfba commit 1b372e1

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

cmake/macro_aspect_query_git_information.cmake

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)