1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00
Files
CMake/Tests/RunCMake/ExternalProject/FetchGitRefs.cmake
Craig Scott 5e941a545b ExternalProject: Ensure git fetch if updating to hash we don't have yet
In ac6a4d4884 (ExternalProject: Improve robustness of update step,
2020-10-17), the method used to check whether we already have a
commit or not was changed from using git rev-list to git rev-parse.
The new logic assumed rev-parse would output nothing if given a commit
hash it didn't know about, but it simply prints the hash again without
raising an error in this scenario. Amend that logic by adding ^{commit} to
the ref to ensure we do get an error if that ref is not currently known.

Fixes: #22166
2021-05-09 12:33:16 +10:00

85 lines
2.6 KiB
CMake

find_package(Git QUIET REQUIRED)
include(ExternalProject)
set(srcRepo ${CMAKE_CURRENT_BINARY_DIR}/srcRepo)
set(srcDir ${CMAKE_CURRENT_BINARY_DIR}/src)
set(binDir ${CMAKE_CURRENT_BINARY_DIR}/build)
file(MAKE_DIRECTORY ${srcRepo})
file(MAKE_DIRECTORY ${srcDir})
file(GLOB entries ${srcRepo}/*)
file(REMOVE_RECURSE ${entries} ${binDir})
file(TOUCH ${srcRepo}/firstFile.txt)
configure_file(${CMAKE_CURRENT_LIST_DIR}/FetchGitRefs/CMakeLists.txt
${srcDir}/CMakeLists.txt COPYONLY)
function(execGitCommand)
execute_process(
WORKING_DIRECTORY ${srcRepo}
COMMAND ${GIT_EXECUTABLE} ${ARGN}
COMMAND_ECHO STDOUT
COMMAND_ERROR_IS_FATAL ANY
)
endfunction()
function(configureAndBuild tag)
execute_process(COMMAND ${CMAKE_COMMAND}
-G ${CMAKE_GENERATOR} -T "${CMAKE_GENERATOR_TOOLSET}"
-A "${CMAKE_GENERATOR_PLATFORM}"
-D repoDir:PATH=${srcRepo}
-D gitTag:STRING=${tag}
-B ${binDir}
-S ${srcDir}
COMMAND_ECHO STDOUT
COMMAND_ERROR_IS_FATAL ANY
)
execute_process(COMMAND ${CMAKE_COMMAND} --build ${binDir} --target fetcher
WORKING_DIRECTORY ${binDir}
COMMAND_ECHO STDOUT
COMMAND_ERROR_IS_FATAL ANY
)
endfunction()
# Setup a fresh source repo with a predictable default branch across all
# git versions
execGitCommand(-c init.defaultBranch=master init)
execGitCommand(config --add user.email "testauthor@cmake.org")
execGitCommand(config --add user.name testauthor)
# Create the initial repo structure
execGitCommand(add firstFile.txt)
execGitCommand(commit -m "First file")
message(STATUS "First configure-and-build")
configureAndBuild(master)
# Create a tagged commit that is not on any branch. With git 2.20 or later,
# this commit won't be fetched without the --tags option.
file(TOUCH ${srcRepo}/secondFile.txt)
execGitCommand(add secondFile.txt)
execGitCommand(commit -m "Second file")
execGitCommand(tag -a -m "Adding tag" tag_of_interest)
execGitCommand(reset --hard HEAD~1)
message(STATUS "Configure-and-build, update to tag")
configureAndBuild(tag_of_interest)
# Do the same, but this time for a commit hash
file(TOUCH ${srcRepo}/thirdFile.txt)
execGitCommand(add thirdFile.txt)
execGitCommand(commit -m "Third file")
execGitCommand(tag -a -m "Adding another tag" check_for_hash)
execGitCommand(reset --hard HEAD~1)
execute_process(
WORKING_DIRECTORY ${srcRepo}
COMMAND ${GIT_EXECUTABLE} rev-parse check_for_hash
COMMAND_ERROR_IS_FATAL ANY
OUTPUT_VARIABLE commit_hash
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS "Configure-and-build, update to commit hash ${commit_hash}")
configureAndBuild(${commit_hash})