Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Downloading Package Using Commit Hash as the Reference #170

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions cmake/CDeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,36 @@ function(cdeps_download_package NAME URL REF)
endif()
endif()

set(CLONE_COMMAND "${GIT_EXECUTABLE}" clone -b "${REF}" --depth 1)
set(CLONE_COMMAND "${GIT_EXECUTABLE}" clone --no-checkout --depth 1
https://${URL}.git ${CDEPS_DIR}/${NAME}/src)

set(FETCH_COMMAND "${GIT_EXECUTABLE}" -C ${CDEPS_DIR}/${NAME}/src
fetch --depth 1 --tags origin "${REF}")

set(CHECKOUT_COMMAND "${GIT_EXECUTABLE}" -C ${CDEPS_DIR}/${NAME}/src
checkout "${REF}")

set(COMMANDS CLONE_COMMAND FETCH_COMMAND CHECKOUT_COMMAND)

if(ARG_RECURSE_SUBMODULES)
list(APPEND CLONE_COMMAND --recurse-submodules)
set(SUBMODULE_COMMAND "${GIT_EXECUTABLE}" -C ${CDEPS_DIR}/${NAME}/src
submodule update --init --recursive)
list(APPEND COMMANDS SUBMODULE_COMMAND)
endif()
list(APPEND CLONE_COMMAND https://${URL}.git ${CDEPS_DIR}/${NAME}/src)

execute_process(
COMMAND ${CLONE_COMMAND}
ERROR_VARIABLE ERR
RESULT_VARIABLE RES
OUTPUT_QUIET)
if(NOT "${RES}" EQUAL 0)
string(JOIN " " COMMAND ${CLONE_COMMAND})
message(FATAL_ERROR
"CDeps: Failed to execute process:\n ${COMMAND}\n${ERR}")
return()
endif()
foreach(COMMAND IN LISTS COMMANDS)
execute_process(
COMMAND ${${COMMAND}}
ERROR_VARIABLE ERR
RESULT_VARIABLE RES
OUTPUT_QUIET)
if(NOT "${RES}" EQUAL 0)
string(JOIN " " COMMAND ${${COMMAND}})
message(FATAL_ERROR
"CDeps: Failed to execute process:\n ${COMMAND}\n${ERR}")
return()
endif()
endforeach()

file(WRITE ${CDEPS_DIR}/${NAME}/src.lock "${SOURCE_LOCK}")
set(${NAME}_SOURCE_DIR ${CDEPS_DIR}/${NAME}/src PARENT_SCOPE)
Expand Down
35 changes: 25 additions & 10 deletions test/test_download_version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,50 @@ find_package(Git REQUIRED QUIET)
set(CDEPS_DIR .cdeps)
file(REMOVE_RECURSE .cdeps)

section("it should download a package with a specific version")
cdeps_download_package(pkg github.com/threeal/project-starter v1.0.0)
section("it should download a package with a specific branch name")
cdeps_download_package(pkg github.com/threeal/project-starter main)

section("it should generate the lock file")
file(READ .cdeps/pkg/src.lock CONTENT)
assert(CONTENT MATCHES main$)
endsection()

section("it should download with the correct branch name")
assert_execute_process(
COMMAND ${GIT_EXECUTABLE} -C .cdeps/pkg/src rev-parse --abbrev-ref HEAD
OUTPUT main)
endsection()
endsection()

section("it should redownload the package with a specific tag name")
cdeps_download_package(pkg github.com/threeal/project-starter v1.0.0)

section("it should regenerate the lock file")
file(READ .cdeps/pkg/src.lock CONTENT)
assert(CONTENT MATCHES v1.0.0$)
endsection()

section("it should download with the correct version")
section("it should redownload with the correct tag name")
assert_execute_process(
COMMAND ${GIT_EXECUTABLE} -C .cdeps/pkg/src
describe --exact-match --tags v1.0.0
OUTPUT v1.0.0)
endsection()
endsection()

section("it should redownload the package with a different version")
cdeps_download_package(pkg github.com/threeal/project-starter v1.1.0)
section("it should redownload the package with a specific commit hash")
cdeps_download_package(pkg github.com/threeal/project-starter
ad2c03959cb67fffc7cce7266bc244c73a2af8ec)

section("it should regenerate the lock file")
file(READ .cdeps/pkg/src.lock CONTENT)
assert(CONTENT MATCHES v1.1.0$)
assert(CONTENT MATCHES ad2c03959cb67fffc7cce7266bc244c73a2af8ec$)
endsection()

section("it should redownload with the correct version")
section("it should redownload with the correct commit hash")
assert_execute_process(
COMMAND ${GIT_EXECUTABLE} -C .cdeps/pkg/src
describe --exact-match --tags v1.1.0
OUTPUT v1.1.0)
COMMAND ${GIT_EXECUTABLE} -C .cdeps/pkg/src rev-parse HEAD
OUTPUT ad2c03959cb67fffc7cce7266bc244c73a2af8ec)
endsection()
endsection()

Expand Down