From 52439cee0ef7f68e8e924e8af9fb343d95b8b6f1 Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Sat, 7 Oct 2017 18:17:52 -0700 Subject: [PATCH] fix error and version info in shallow git clones Fix `cmake/GitTagVersion.cmake` to not throw errors when in a shallow git clone, or any git clone that has no tags. And when either git or git tag version info is not available, set the version variables to the defaults, which are: ``` VERSION = '2.0.0' REVISION = 'unknown' VERSION_RELEASE = 0 ``` --- CMakeLists.txt | 3 +++ cmake/GitTagVersion.cmake | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7d07583..bd09527d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,7 +114,10 @@ if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git") get_git_head_revision(REFSPEC COMMITHASH) else() message(WARNING "Git not found, cannot set version info") +endif() +# no git or no tags +if(NOT VERSION) set(VERSION 2.0.0) set(REVISION "unknown") set(VERSION_RELEASE 0) diff --git a/cmake/GitTagVersion.cmake b/cmake/GitTagVersion.cmake index ca484a96..b46e98e3 100644 --- a/cmake/GitTagVersion.cmake +++ b/cmake/GitTagVersion.cmake @@ -8,6 +8,11 @@ function(git_version version revision version_release) # get latest version from tag history execute_process(COMMAND ${GIT_EXECUTABLE} tag --sort=-creatordate OUTPUT_VARIABLE sorted_tags OUTPUT_STRIP_TRAILING_WHITESPACE) + # if no tags (e.g. shallow clone) do nothing + if(NOT sorted_tags) + return() + endif() + # convert to list (see: https://public.kitware.com/pipermail/cmake/2007-May/014222.html) string(REGEX REPLACE ";" "\\\\;" sorted_tags "${sorted_tags}") string(REGEX REPLACE "\n" ";" sorted_tags "${sorted_tags}") @@ -29,6 +34,11 @@ function(git_version version revision version_release) # get the current revision execute_process(COMMAND ${GIT_EXECUTABLE} tag "--format=%(align:width=20)%(refname:short)%(end)%(if)%(*objectname)%(then)%(*objectname)%(else)%(objectname)%(end)" --sort=-creatordate OUTPUT_VARIABLE sorted_tags_refs OUTPUT_STRIP_TRAILING_WHITESPACE) + # if no tags (e.g. shallow clone) do nothing + if(NOT sorted_tags_refs) + return() + endif() + # convert to list (see: https://public.kitware.com/pipermail/cmake/2007-May/014222.html) string(REGEX REPLACE ";" "\\\\;" sorted_tags_refs "${sorted_tags_refs}") string(REGEX REPLACE "\n" ";" sorted_tags_refs "${sorted_tags_refs}")