Skip to content

Commit

Permalink
Merge pull request #243 from aminya/git-clone-shallow
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Nov 14, 2023
2 parents 804311c + d333b7c commit 538baf8
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/Git.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ Input variables:
- ``REPOSITORY_PATH``: The path to the repository
- ``REMOTE_URL``: The url of the remote to add
- ``REMOTE_NAME``: The name of the remote to add (defaults to the remote user)
- ``SHALLOW_SINCE``: Create a shallow clone with a history after the specified time. date should be in format of `git log --date=raw`.
- ``BRANCH``: Only clone the given branch
- ``FORCE_CLONE``: Force the clone even if the directory exists

]]
function(git_clone)
set(oneValueArgs REPOSITORY_PATH REMOTE_URL REMOTE_NAME FORCE_CLONE)
set(oneValueArgs
REPOSITORY_PATH
REMOTE_URL
REMOTE_NAME
SHALLOW_SINCE
BRANCH
FORCE_CLONE
)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})

if("${_fun_REPOSITORY_PATH}" STREQUAL "" OR "${_fun_REMOTE_URL}" STREQUAL "")
Expand All @@ -29,14 +37,23 @@ function(git_clone)

find_program(GIT_EXECUTABLE "git" REQUIRED)
get_filename_component(_fun_REPOSITORY_PARENT_PATH "${_fun_REPOSITORY_PATH}" DIRECTORY)

set(GIT_ARGS "clone" "${_fun_REMOTE_URL}" "${_fun_REPOSITORY_PATH}")

if(NOT "${_fun_SHALLOW_SINCE}" STREQUAL "")
list(APPEND GIT_ARGS "--shallow-since=${_fun_SHALLOW_SINCE}")
endif()

if(NOT "${_fun_BRANCH}" STREQUAL "")
list(APPEND GIT_ARGS "--single-branch" "--branch=${_fun_BRANCH}")
endif()

execute_process(
COMMAND "${GIT_EXECUTABLE}" "clone" "${_fun_REMOTE_URL}"
WORKING_DIRECTORY "${_fun_REPOSITORY_PARENT_PATH}" COMMAND_ERROR_IS_FATAL LAST
COMMAND "${GIT_EXECUTABLE}" ${GIT_ARGS} WORKING_DIRECTORY "${_fun_REPOSITORY_PARENT_PATH}"
COMMAND_ERROR_IS_FATAL LAST
)
else()
message(
STATUS "Repository already exists at ${_fun_REPOSITORY_PATH}. Waiting for git lock file.."
)
message(STATUS "Repository already exists at ${_fun_REPOSITORY_PATH}.")
git_wait(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")

if(NOT EXISTS "${_fun_REPOSITORY_PATH}/.git")
Expand Down Expand Up @@ -389,6 +406,8 @@ function(git_wait)

set(counter 0)

message(STATUS "Waiting for git lock file...[${counter}/${_fun_TIMEOUT_COUNTER}]")

# wait until .git/index is present (in case a parallel clone is running)
while(NOT EXISTS "${_fun_REPOSITORY_PATH}/.git/index"
OR EXISTS "${_fun_REPOSITORY_PATH}/.git/index.lock"
Expand Down

0 comments on commit 538baf8

Please sign in to comment.