Skip to content

Commit

Permalink
feat: add support for downloading package using commit hash as the re…
Browse files Browse the repository at this point in the history
…ference (#170)

* test: modify `test_download_version.cmake` to test download with tag and branch

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* feat: modify commands to download external package

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* test: modify `test_download_version.cmake` to test download with commit hash

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

---------

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
  • Loading branch information
threeal authored Oct 12, 2024
1 parent 0396dbd commit 1fd2b34
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
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

0 comments on commit 1fd2b34

Please sign in to comment.