Skip to content

Commit

Permalink
feat: add support to check out on a specific directory (#16)
Browse files Browse the repository at this point in the history
* feat: add `DIRECTORY` argument in `git_checkout` function

* test: add test for checking out Git repo to specific dir
  • Loading branch information
threeal committed Mar 11, 2024
1 parent 9af07fd commit e5e97c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
15 changes: 9 additions & 6 deletions cmake/GitCheckout.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ endmacro()
# - URL: The URL of the remote Git repository.
#
# Optional arguments:
# - DIRECTORY: The path of the directory to check out the Git repository.
# - REF: The reference (branch, tag, or commit) to check out the Git repository.
function(git_checkout URL)
cmake_parse_arguments(ARG "" "REF;ERROR_VARIABLE" "" ${ARGN})
cmake_parse_arguments(ARG "" "DIRECTORY;REF;ERROR_VARIABLE" "" ${ARGN})

# Clones the Git repository.
execute_process(
COMMAND git clone ${URL}
COMMAND git clone ${URL} ${ARG_DIRECTORY}
RESULT_VARIABLE RES
)
if(NOT RES EQUAL 0)
Expand All @@ -45,18 +46,20 @@ function(git_checkout URL)
)
endif()

# Determines the directory of the cloned Git repository.
string(REGEX REPLACE ".*/" "" GIT_DIR ${URL})
if(NOT DEFINED ARG_DIRECTORY)
# Determines the directory of the cloned Git repository if it is not specified.
string(REGEX REPLACE ".*/" "" ARG_DIRECTORY ${URL})
endif()

if(ARG_REF)
# Checks out the Git repository to a specific reference.
execute_process(
COMMAND git -C ${GIT_DIR} checkout ${ARG_REF}
COMMAND git -C ${ARG_DIRECTORY} checkout ${ARG_REF}
RESULT_VARIABLE RES
)
if(NOT RES EQUAL 0)
_set_error(
"Failed to check out '${GIT_DIR}' to '${ARG_REF}' (${RES})"
"Failed to check out '${ARG_DIRECTORY}' to '${ARG_REF}' (${RES})"
ERROR_VARIABLE ${ARG_ERROR_VARIABLE}
)
endif()
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_cmake_test(
GitCheckoutTest.cmake
"Check out a Git repository"
"Check out an invalid Git repository"
"Check out a Git repository to a specific directory"
"Check out a Git repository on a specific ref"
"Check out a Git repository on a specific invalid ref"
)
Expand Down
15 changes: 15 additions & 0 deletions test/GitCheckoutTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ if("Check out an invalid Git repository" MATCHES ${TEST_MATCHES})
endif()
endif()

if("Check out a Git repository to a specific directory" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

if(EXISTS some-directory)
file(REMOVE_RECURSE some-directory)
endif()

include(GitCheckout)
git_checkout(https://github.com/threeal/project-starter DIRECTORY some-directory)

if(NOT EXISTS some-directory)
message(FATAL_ERROR "The 'some-directory' directory should exist")
endif()
endif()

if("Check out a Git repository on a specific ref" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

Expand Down

0 comments on commit e5e97c8

Please sign in to comment.