Skip to content

Commit f1e47e6

Browse files
herrgahrphkahler
authored andcommitted
CMake: use git rev-parse to get GIT_COMMIT_HASH
The old approach of reading .git/HEAD does not work when using git worktrees, where the folder layout looks roughly like: solvespace.git/ - bare clone (.git dir) solvespace.git/work - example worktree containing master solvespage.git/worktrees/work/ - .git dir of worktree solvespage.git/worktrees/work/HEAD - actual HEAD ref for master First attempt was to just get GIT_ROOT from `git rev-parse --git-dir` but that wasn't enough, since: 1. GIT_ROOT points to solvespage.git/worktrees/work/ 2. GIT_ROOT/HEAD points to refs/heads/master 3. GIT_ROOT/refs/heads/master does not exist but the old implementation would want to use this to get the sha so we need two invocations of git rev-parse 1. `git rev-parse --git-dir` to get GIT_DIR needed for setting GIT_DEPENDS 2. `git rev-parse HEAD` to get the sha of the worktree's HEAD
1 parent 5efc148 commit f1e47e6

1 file changed

Lines changed: 2 additions & 22 deletions

File tree

cmake/GetGitCommitHash.cmake

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
function(get_git_commit_hash)
2-
get_filename_component(GIT_DESCRIBE_CMAKE_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
3-
get_filename_component(GIT_ROOT ${GIT_DESCRIBE_CMAKE_DIR} PATH)
4-
set(GIT_DIR "${GIT_ROOT}/.git")
2+
execute_process(COMMAND git rev-parse --git-dir OUTPUT_VARIABLE GIT_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
3+
execute_process(COMMAND git rev-parse HEAD OUTPUT_VARIABLE HEAD_REF OUTPUT_STRIP_TRAILING_WHITESPACE)
54

65
# Add a CMake configure dependency to the currently checked out revision.
76
set(GIT_DEPENDS ${GIT_DIR}/HEAD)
8-
file(READ ${GIT_DIR}/HEAD HEAD_REF)
9-
if(HEAD_REF MATCHES "ref: (.+)\n")
10-
set(HEAD_REF ${CMAKE_MATCH_1})
11-
if(EXISTS "${GIT_DIR}/${HEAD_REF}")
12-
list(APPEND GIT_DEPENDS ${GIT_DIR}/${HEAD_REF})
13-
file(READ ${GIT_DIR}/${HEAD_REF} HEAD_REF)
14-
elseif(EXISTS "${GIT_DIR}/packed-refs")
15-
list(APPEND GIT_DEPENDS ${GIT_DIR}/packed-refs)
16-
file(READ "${GIT_DIR}/packed-refs" PACKED_REFS)
17-
if(${PACKED_REFS} MATCHES "([0-9a-z]*) ${HEAD_REF}")
18-
set(HEAD_REF ${CMAKE_MATCH_1})
19-
else()
20-
set(HEAD_REF "")
21-
endif()
22-
else()
23-
set(HEAD_REF "")
24-
endif()
25-
endif()
267
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${GIT_DEPENDS})
278

28-
string(STRIP ${HEAD_REF} HEAD_REF)
299
if(HEAD_REF STREQUAL "")
3010
message(WARNING "Cannot determine git HEAD")
3111
else()

0 commit comments

Comments
 (0)