Skip to content

Commit a9ae25d

Browse files
fredroyhugtalbotbakpaul
authored
[Config] CMake: Shallow clone when using the git-fetch mechanism (#5889)
* fetch shallow clone on repo * do not use shallow feature if the GIT_TAG is hash to a commit --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org> Co-authored-by: Paul Baksic <30337881+bakpaul@users.noreply.github.com>
1 parent 328a525 commit a9ae25d

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Sofa/framework/Config/cmake/SofaMacrosConfigure.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ macro(sofa_fetch_dependency name)
204204
file(MAKE_DIRECTORY "${fetched_dir}-temp/")
205205
endif()
206206

207+
set(GIT_SHALLOW_VALUE TRUE)
208+
# GIT_SHALLOW does work if GIT_TAG is a commit SHA hash
209+
__is_git_tag_commit_hash(${upper_name}_GIT_TAG IS_GIT_TAG_COMMIT_HASH)
210+
if(IS_GIT_TAG_COMMIT_HASH)
211+
set(GIT_SHALLOW_VALUE FALSE)
212+
endif()
207213

208214
file(WRITE ${fetched_dir}-temp/CMakeLists.txt "
209215
cmake_minimum_required(VERSION 3.22)
@@ -219,6 +225,7 @@ macro(sofa_fetch_dependency name)
219225
INSTALL_COMMAND \"\"
220226
TEST_COMMAND \"\"
221227
GIT_CONFIG \"remote.origin.fetch=+refs/pull/*:refs/remotes/origin/pr/*\"
228+
GIT_SHALLOW ${GIT_SHALLOW_VALUE}
222229
)"
223230
)
224231

Sofa/framework/Config/cmake/SofaMacrosUtils.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,18 @@ function(sofa_get_all_targets var)
5252
set(${var} ${targets} PARENT_SCOPE)
5353
endfunction()
5454

55+
# guess if the git tag is a commit hash or an actual tag or a branch nane.
56+
# heavily inspired by https://github.com/cpm-cmake/CPM.cmake/pull/130/changes#diff-6fcfee7f313f15253f88285a499e62cb58746d47ff2cfec173f1f4cd29feb44d
57+
function(__is_git_tag_commit_hash GIT_TAG RESULT)
58+
string(LENGTH ${GIT_TAG} length)
59+
# full hash has 40 characters, and short hash has at least 7 characters.
60+
if (length LESS 7 OR length GREATER 40)
61+
SET(${RESULT} 0 PARENT_SCOPE)
62+
else()
63+
if (${GIT_TAG} MATCHES "^[a-fA-F0-9]+$")
64+
SET(${RESULT} 1 PARENT_SCOPE)
65+
else()
66+
SET(${RESULT} 0 PARENT_SCOPE)
67+
endif()
68+
endif()
69+
endfunction()

0 commit comments

Comments
 (0)