mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-17 22:58:14 -05:00
* Use shallow clone for git repositories by default (#129) * use shallow clone for git repositories by default * remove trailing spaces * Enable shallow clone for actual tags * Support short commit hash * Enable shallow only when downloading dependencies into cache * Always honor user specified GIT_SHALLOW opiton
This commit is contained in:
@@ -191,6 +191,7 @@ function(CPMAddPackage)
|
||||
DOWNLOAD_COMMAND
|
||||
FIND_PACKAGE_ARGUMENTS
|
||||
NO_CACHE
|
||||
GIT_SHALLOW
|
||||
)
|
||||
|
||||
set(multiValueArgs
|
||||
@@ -230,6 +231,10 @@ function(CPMAddPackage)
|
||||
|
||||
if (DEFINED CPM_ARGS_GIT_TAG)
|
||||
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG})
|
||||
# If GIT_SHALLOW is explicitly specified, honor the value.
|
||||
if (DEFINED CPM_ARGS_GIT_SHALLOW)
|
||||
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW ${CPM_ARGS_GIT_SHALLOW})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Check if package has been added before
|
||||
@@ -309,6 +314,15 @@ function(CPMAddPackage)
|
||||
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND "${CMAKE_COMMAND}")
|
||||
set(PACKAGE_INFO "${download_directory}")
|
||||
else()
|
||||
# Enable shallow clone when GIT_TAG is not a commit hash.
|
||||
# Our guess may not be accurate, but it should guarantee no commit hash get mis-detected.
|
||||
if (NOT DEFINED CPM_ARGS_GIT_SHALLOW)
|
||||
cpm_is_git_tag_commit_hash(${CPM_ARGS_GIT_TAG} IS_HASH)
|
||||
if (NOT ${IS_HASH})
|
||||
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW TRUE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# remove timestamps so CMake will re-download the dependency
|
||||
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild)
|
||||
set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}")
|
||||
@@ -474,3 +488,18 @@ function(cpm_get_version_from_git_tag GIT_TAG RESULT)
|
||||
SET(${RESULT} ${CMAKE_MATCH_1} PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# guesses if the git tag is a commit hash or an actual tag or a branch nane.
|
||||
function(cpm_is_git_tag_commit_hash GIT_TAG RESULT)
|
||||
string(LENGTH ${GIT_TAG} length)
|
||||
# full hash has 40 characters, and short hash has at least 7 characters.
|
||||
if (length LESS 7 OR length GREATER 40)
|
||||
SET(${RESULT} 0 PARENT_SCOPE)
|
||||
else()
|
||||
if (${GIT_TAG} MATCHES "^[a-fA-F0-9]+$")
|
||||
SET(${RESULT} 1 PARENT_SCOPE)
|
||||
else()
|
||||
SET(${RESULT} 0 PARENT_SCOPE)
|
||||
endif()
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
22
test/unit/is_git_tag_commit_hash.cmake
Normal file
22
test/unit/is_git_tag_commit_hash.cmake
Normal file
@@ -0,0 +1,22 @@
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
include(${CPM_PATH}/CPM.cmake)
|
||||
include(${CPM_PATH}/testing.cmake)
|
||||
|
||||
CPM_IS_GIT_TAG_COMMIT_HASH("v1.2.3" RESULT)
|
||||
ASSERT_EQUAL("0" ${RESULT})
|
||||
|
||||
CPM_IS_GIT_TAG_COMMIT_HASH("asio-1-12-1" RESULT)
|
||||
ASSERT_EQUAL("0" ${RESULT})
|
||||
|
||||
CPM_IS_GIT_TAG_COMMIT_HASH("513039e3cba83284cec71287fd829865b9f423bc" RESULT)
|
||||
ASSERT_EQUAL("1" ${RESULT})
|
||||
|
||||
CPM_IS_GIT_TAG_COMMIT_HASH("513039E3CBA83284CEC71287FD829865B9F423BC" RESULT)
|
||||
ASSERT_EQUAL("1" ${RESULT})
|
||||
|
||||
CPM_IS_GIT_TAG_COMMIT_HASH("513039E" RESULT)
|
||||
ASSERT_EQUAL("1" ${RESULT})
|
||||
|
||||
CPM_IS_GIT_TAG_COMMIT_HASH("513039E3CBA8" RESULT)
|
||||
ASSERT_EQUAL("1" ${RESULT})
|
||||
Reference in New Issue
Block a user