Add support for different LFS url to vcpkg_from_git (#28693)

This commit is contained in:
Aleksi Sapon 2023-01-23 19:26:29 -05:00 committed by GitHub
parent 4b2725f6f4
commit 50805d6ece
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 6 deletions

View file

@ -12,7 +12,7 @@ vcpkg_from_git(
REF <59f7335e4d...>
[HEAD_REF <ref>]
[PATCHES <patch1.patch> <patch2.patch>...]
[LFS]
[LFS [url]]
)
```
@ -47,6 +47,8 @@ Relative paths are based on the port directory.
Enable fetching files stored using Git LFS.
Only files pointed to by `REF` are fetched.
The LFS url is optional. By default the Git url is used.
This makes Git LFS mandatory for the port.
It's a fatal error if the extension is not installed.

View file

@ -1,7 +1,7 @@
function(vcpkg_from_git)
cmake_parse_arguments(PARSE_ARGV 0 "arg"
"LFS"
"OUT_SOURCE_PATH;URL;REF;FETCH_REF;HEAD_REF;TAG"
""
"OUT_SOURCE_PATH;URL;REF;FETCH_REF;HEAD_REF;TAG;LFS"
"PATCHES"
)
@ -12,7 +12,6 @@ function(vcpkg_from_git)
message(WARNING "The TAG argument to vcpkg_from_git has been deprecated and has no effect.")
endif()
if(NOT DEFINED arg_OUT_SOURCE_PATH)
message(FATAL_ERROR "OUT_SOURCE_PATH must be specified")
endif()
@ -25,6 +24,9 @@ function(vcpkg_from_git)
if(DEFINED arg_FETCH_REF AND NOT DEFINED arg_REF)
message(FATAL_ERROR "REF must be specified if FETCH_REF is specified")
endif()
if(NOT DEFINED arg_LFS AND "LFS" IN_LIST arg_KEYWORDS_MISSING_VALUES)
set(arg_LFS "${arg_URL}")
endif()
vcpkg_list(SET git_fetch_shallow_param --depth 1)
vcpkg_list(SET extract_working_directory_param)
@ -108,7 +110,7 @@ function(vcpkg_from_git)
)
vcpkg_execute_required_process(
ALLOW_IN_DOWNLOAD_MODE
COMMAND ${GIT} lfs fetch "${arg_URL}" "${ref_to_fetch}"
COMMAND ${GIT} lfs fetch "${arg_LFS}" "${ref_to_fetch}"
WORKING_DIRECTORY "${git_working_directory}"
LOGNAME "git-lfs-fetch-${TARGET_TRIPLET}"
)

View file

@ -209,8 +209,9 @@ if(NOT lfs_version_result)
WORKING_DIRECTORY "${git_test_repo}"
LOGNAME "git-lfs-install"
)
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
file(WRITE "${git_test_repo}/lfs_file.bin" "fourth commit")
vcpkg_execute_required_process(
COMMAND ${git} add ".gitattributes" "lfs_file.bin"
@ -261,6 +262,85 @@ if(NOT lfs_version_result)
${contents}
")
endif()
# test 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}")
if(lfs_version VERSION_GREATER_EQUAL "3.0.0")
file(WRITE "${git_test_repo}/lfs_file2.bin" "fifth commit")
vcpkg_execute_required_process(
COMMAND ${git} add "lfs_file2.bin"
WORKING_DIRECTORY "${git_test_repo}"
LOGNAME "git-lfs-add.2"
)
vcpkg_execute_required_process(
COMMAND ${git} commit -m "fifth commit"
WORKING_DIRECTORY "${git_test_repo}"
LOGNAME "git-lfs-commit.2"
)
vcpkg_execute_in_download_mode(
COMMAND ${git} rev-parse HEAD
OUTPUT_VARIABLE ref
RESULT_VARIABLE error_code
WORKING_DIRECTORY "${git_test_repo}"
)
if(NOT "${error_code}" EQUAL "0")
message(FATAL_ERROR "Failed to rev-parse HEAD: ${error_code}")
endif()
string(STRIP "${ref}" ref)
set(git_test_repo_2 "${CURRENT_BUILDTREES_DIR}/test-git-repo-2")
file(REMOVE_RECURSE "${git_test_repo_2}")
set(git_remote_2 "file:///${git_test_repo_2}")
vcpkg_execute_required_process(
COMMAND ${git} init --bare "test-git-repo-2"
WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}"
LOGNAME "git-init.2"
)
# note: LFS won't prune "unpushed" files, which is checked using the "origin" remote by default.
# If there is no remote then files are never considered to be "pushed", and so are never pruned.
vcpkg_execute_required_process(
COMMAND ${git} remote add origin "${git_remote_2}"
WORKING_DIRECTORY "${git_test_repo}"
LOGNAME "git-remote-add"
)
vcpkg_execute_required_process(
COMMAND ${git} push --all origin
WORKING_DIRECTORY "${git_test_repo}"
LOGNAME "git-push"
)
vcpkg_execute_in_download_mode(
COMMAND ${git} lfs prune --force --verbose
OUTPUT_VARIABLE lfs_prune
RESULT_VARIABLE error_code
WORKING_DIRECTORY "${git_test_repo}"
)
if(NOT "${error_code}" EQUAL "0")
message(FATAL_ERROR "Failed to prune LFS files: ${error_code}")
endif()
if(NOT "${lfs_prune}" MATCHES "0 retained")
message(FATAL_ERROR "LFS prune did not delete all files:\n${lfs_prune}")
endif()
set(VCPKG_USE_HEAD_VERSION OFF)
vcpkg_from_git(
OUT_SOURCE_PATH source_path
URL "${git_remote}"
REF "${ref}"
HEAD_REF main
LFS "${git_remote_2}"
)
file(READ "${source_path}/lfs_file2.bin" contents)
if(NOT "${contents}" STREQUAL "fifth commit")
message(FATAL_ERROR "Failed to checkout the fifth commit. Contents were:
${contents}
")
endif()
else()
message(NOTICE "Git LFS version is older than 3.0.0: some tests were skipped")
endif()
else()
message(NOTICE "Git LFS is not available: some tests were skipped")
endif()