Commit fcdff64a authored by Alvin Wong's avatar Alvin Wong

CMake: Support reading git ref with git-worktree

Support reading the git ref and commit hash when git-worktree is being
used.

Differential Revision: https://phabricator.kde.org/D9172
parent 2fd00cc0
......@@ -53,12 +53,16 @@ function(get_git_head_revision _refspecvar _hashvar)
endif()
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
endwhile()
# check if this is a submodule
# check if this is a linked working tree (e.g. submodule or git-worktree)
if(NOT IS_DIRECTORY ${GIT_DIR})
file(READ ${GIT_DIR} submodule)
string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
file(READ ${GIT_DIR} gitdirfile)
string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_PATH ${gitdirfile})
if(IS_ABSOLUTE ${GIT_DIR_PATH})
get_filename_component(GIT_DIR ${GIT_DIR_PATH} ABSOLUTE)
else()
get_filename_component(LINKED_DIR ${GIT_DIR} PATH)
get_filename_component(GIT_DIR ${LINKED_DIR}/${GIT_DIR_PATH} ABSOLUTE)
endif()
endif()
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
if(NOT EXISTS "${GIT_DATA}")
......
......@@ -18,18 +18,30 @@ set(HEAD_HASH)
file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)
string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
set(GIT_DIR "@GIT_DIR@")
# handle git-worktree
if(EXISTS "${GIT_DIR}/commondir")
file(READ "${GIT_DIR}/commondir" GIT_DIR_NEW LIMIT 1024)
string(STRIP "${GIT_DIR_NEW}" GIT_DIR_NEW)
if(NOT IS_ABSOLUTE "${GIT_DIR_NEW}")
get_filename_component(GIT_DIR_NEW ${GIT_DIR}/${GIT_DIR_NEW} ABSOLUTE)
endif()
if(EXISTS "${GIT_DIR_NEW}")
set(GIT_DIR "${GIT_DIR_NEW}")
endif()
endif()
if(HEAD_CONTENTS MATCHES "ref")
# named branch
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
elseif(EXISTS "@GIT_DIR@/logs/${HEAD_REF}")
configure_file("@GIT_DIR@/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
if(EXISTS "${GIT_DIR}/${HEAD_REF}")
configure_file("${GIT_DIR}/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
elseif(EXISTS "${GIT_DIR}/logs/${HEAD_REF}")
configure_file("${GIT_DIR}/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
set(HEAD_HASH "${HEAD_REF}")
endif()
else()
# detached HEAD
configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
configure_file("${GIT_DIR}/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
endif()
if(NOT HEAD_HASH)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment