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

feat: support shallow and single branch clones in Git #243

Merged
merged 1 commit into from
Nov 14, 2023
Merged
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
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
Loading