diff --git a/scripts/cmake/vcpkg_from_git.cmake b/scripts/cmake/vcpkg_from_git.cmake index c77e63e1dc..0b477acb82 100644 --- a/scripts/cmake/vcpkg_from_git.cmake +++ b/scripts/cmake/vcpkg_from_git.cmake @@ -104,49 +104,63 @@ function(vcpkg_from_git) vcpkg_execute_required_process( ALLOW_IN_DOWNLOAD_MODE - COMMAND ${GIT} lfs install --local --force + COMMAND "${GIT}" lfs install --local --force WORKING_DIRECTORY "${git_working_directory}" LOGNAME "git-lfs-install-${TARGET_TRIPLET}" ) vcpkg_execute_required_process( ALLOW_IN_DOWNLOAD_MODE - COMMAND ${GIT} lfs fetch "${arg_LFS}" "${ref_to_fetch}" + COMMAND "${GIT}" lfs fetch "${arg_LFS}" "${ref_to_fetch}" WORKING_DIRECTORY "${git_working_directory}" LOGNAME "git-lfs-fetch-${TARGET_TRIPLET}" ) endif() if(VCPKG_USE_HEAD_VERSION) - vcpkg_execute_in_download_mode( - COMMAND "${GIT}" rev-parse FETCH_HEAD - OUTPUT_VARIABLE rev_parse_ref - ERROR_VARIABLE rev_parse_ref - RESULT_VARIABLE error_code - WORKING_DIRECTORY "${git_working_directory}" - ) - if(error_code) - message(FATAL_ERROR "unable to determine FETCH_HEAD after fetching git repository") - endif() - string(STRIP "${rev_parse_ref}" rev_parse_ref) - set(VCPKG_HEAD_VERSION "${rev_parse_ref}" PARENT_SCOPE) + set(expected_rev_parse FETCH_HEAD) else() - vcpkg_execute_in_download_mode( - COMMAND "${GIT}" rev-parse "${arg_REF}" - OUTPUT_VARIABLE rev_parse_ref - ERROR_VARIABLE rev_parse_ref - RESULT_VARIABLE error_code - WORKING_DIRECTORY "${git_working_directory}" + set(expected_rev_parse "${arg_REF}") + endif() + + vcpkg_execute_in_download_mode( + COMMAND "${GIT}" rev-parse "${expected_rev_parse}" + OUTPUT_VARIABLE rev_parse_ref + ERROR_VARIABLE rev_parse_ref + RESULT_VARIABLE error_code + WORKING_DIRECTORY "${git_working_directory}" + ) + + if(error_code) + if(VCPKG_USE_HEAD_VERSION) + message(FATAL_ERROR "Unable to determine the commit SHA of the HEAD version to use after \ +fetching ${ref_to_fetch} from the git repository. (git rev-parse ${expected_rev_parse} failed)") + elseif(DEFINED arg_FETCH_REF) + message(FATAL_ERROR "After fetching ${ref_to_fetch}, the target ref ${expected_rev_parse} appears \ +inaccessible. A common cause of this failure is setting REF to a named branch or tag rather than a commit SHA. REF \ +must be a commit SHA. (git rev-parse ${expected_rev_parse} failed)") + else() + message(FATAL_ERROR "After fetching ${ref_to_fetch}, the target ref ${expected_rev_parse} appears \ +inaccessible. A common cause of this failure is setting REF to a named branch or tag rather than a commit SHA. REF \ +must be a commit SHA. If the git server does not advertise commit SHAs \ +(uploadpack.allowReachableSHA1InWant is false), you can set FETCH_REF to a named branch in which the desired commit \ +SHA is in the history. For example, you may be able to fix this error by changing \"REF ${arg_REF}\" to \ +\"REF a-commit-sha FETCH_REF ${arg_REF}\". (git rev-parse ${expected_rev_parse} failed)") + endif() + endif() + + string(STRIP "${rev_parse_ref}" rev_parse_ref) + if(VCPKG_USE_HEAD_VERSION) + set(VCPKG_HEAD_VERSION "${rev_parse_ref}" PARENT_SCOPE) + elseif(NOT "${rev_parse_ref}" STREQUAL "${arg_REF}") + message(FATAL_ERROR "After fetching ${ref_to_fetch}, the requested REF (${arg_REF}) does not match \ +its commit SHA returned by git rev-parse (${rev_parse_ref}). This is usually caused by trying to set REF to a named \ +branch or tag rather than a commit SHA. REF must be a commit SHA. If the git server does not advertise commit SHAs \ +(uploadpack.allowReachableSHA1InWant is false), you can set FETCH_REF to a named branch in which the desired commit \ +SHA is in the history. For example, you may be able to fix this error by changing \"REF ${arg_REF}\" to \ +\"REF a-commit-sha FETCH_REF ${arg_REF}\". + [Expected : ( ${arg_REF} )]) + [ Actual : ( ${rev_parse_ref} )]" ) - if(error_code) - message(FATAL_ERROR "unable to rev-parse ${arg_REF} after fetching git repository") - endif() - string(STRIP "${rev_parse_ref}" rev_parse_ref) - if(NOT "${rev_parse_ref}" STREQUAL "${arg_REF}") - message(FATAL_ERROR "REF (${arg_REF}) does not match rev-parse'd reference (${rev_parse_ref}) - [Expected : ( ${arg_REF} )]) - [ Actual : ( ${rev_parse_ref} )]" - ) - endif() endif() file(MAKE_DIRECTORY "${DOWNLOADS}/temp") diff --git a/scripts/test_ports/vcpkg-from-git-test/portfile.cmake b/scripts/test_ports/vcpkg-from-git-test/portfile.cmake index e2c6bd8866..841a6e2208 100644 --- a/scripts/test_ports/vcpkg-from-git-test/portfile.cmake +++ b/scripts/test_ports/vcpkg-from-git-test/portfile.cmake @@ -6,6 +6,7 @@ file(REMOVE_RECURSE "${git_test_repo}") # LFS expects a URL for a local repository set(git_remote "file:///${git_test_repo}") +message(STATUS "Creating test git repository") vcpkg_find_acquire_program(GIT) vcpkg_list(SET git_config -c core.autocrlf=false @@ -75,7 +76,7 @@ if(NOT "${error_code}" EQUAL "0") endif() string(STRIP "${head_ref}" head_ref) -# test regular mode +message(STATUS "Testing regular mode") set(VCPKG_USE_HEAD_VERSION OFF) vcpkg_from_git( OUT_SOURCE_PATH source_path @@ -90,7 +91,22 @@ ${contents} ") endif() -# test regular mode with FETCH_REF +message(STATUS "Testing regular mode that happens to match HEAD") +set(VCPKG_USE_HEAD_VERSION OFF) +vcpkg_from_git( + OUT_SOURCE_PATH source_path + URL "${git_remote}" + REF "${head_ref}" + HEAD_REF main +) +file(READ "${source_path}/README.txt" contents) +if(NOT "${contents}" STREQUAL "second commit") + message(FATAL_ERROR "Failed to checkout the second commit. Contents were: +${contents} +") +endif() + +message(STATUS "Testing regular mode with FETCH_REF") vcpkg_execute_required_process( COMMAND ${git} config uploadpack.allowReachableSHA1InWant false WORKING_DIRECTORY "${git_test_repo}" @@ -111,13 +127,29 @@ ${contents} ") endif() +message(STATUS "Testing regular mode with FETCH_REF that happens to match HEAD") +set(VCPKG_USE_HEAD_VERSION OFF) +vcpkg_from_git( + OUT_SOURCE_PATH source_path + URL "${git_remote}" + REF "${head_ref}" + FETCH_REF main + HEAD_REF main +) +file(READ "${source_path}/README.txt" contents) +if(NOT "${contents}" STREQUAL "second commit") + message(FATAL_ERROR "Failed to checkout the second commit. Contents were: +${contents} +") +endif() + vcpkg_execute_required_process( COMMAND ${git} config uploadpack.allowReachableSHA1InWant true WORKING_DIRECTORY "${git_test_repo}" LOGNAME "git-config" ) -# test head mode +message(STATUS "Testing head mode") set(VCPKG_USE_HEAD_VERSION ON) vcpkg_from_git( OUT_SOURCE_PATH source_path @@ -138,7 +170,7 @@ if(NOT "${VCPKG_HEAD_VERSION}" STREQUAL "${head_ref}") ") endif() -# test head mode + no HEAD_REF -> just uses REF +message(STATUS "Testing head mode + no HEAD_REF -> just uses REF") set(VCPKG_USE_HEAD_VERSION ON) vcpkg_from_git( OUT_SOURCE_PATH source_path @@ -152,7 +184,7 @@ ${contents} ") endif() -# test new head ref +message(STATUS "Testing new head ref") file(WRITE "${git_test_repo}/README.txt" "third commit") vcpkg_execute_required_process( COMMAND ${git} add "README.txt" @@ -195,7 +227,7 @@ if(NOT "${VCPKG_HEAD_VERSION}" STREQUAL "${new_head_ref}") ") endif() -# test LFS support +message(STATUS "Testing LFS support") vcpkg_execute_in_download_mode( COMMAND "${GIT}" lfs --version OUTPUT_VARIABLE lfs_version_output @@ -211,7 +243,7 @@ if(NOT lfs_version_result) ) file(WRITE "${git_test_repo}/.gitattributes" "* text=auto\n*.bin filter=lfs diff=lfs merge=lfs -text\n") - # test fetching with the same Git and LFS urls + message(STATUS "Testing fetching with the same Git and LFS urls") file(WRITE "${git_test_repo}/lfs_file.bin" "fourth commit") vcpkg_execute_required_process( COMMAND ${git} add ".gitattributes" "lfs_file.bin" @@ -263,7 +295,7 @@ ${contents} ") endif() - # test fetching from different Git and LFS urls + message(STATUS "Testing fetching from different Git and LFS urls") # requires LFS 3.0.0 or later for "--force" on prune string(REGEX MATCH "git-lfs/([0-9\\.]+) " lfs_version "${lfs_version_output}") set(lfs_version "${CMAKE_MATCH_1}")