Skip to content

Commit

Permalink
fix: handle race condition when repo is cloned
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Aug 13, 2023
1 parent 9070045 commit 9fe821c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Git.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function(git_clone)
message(FATAL_ERROR "REPOSITORY_PATH and _fun_REMOTE_URL are required")
endif()

# the folder is created as soon as the clone starts
if(NOT EXISTS "${_fun_REPOSITORY_PATH}")
message(STATUS "Cloning at ${_fun_REPOSITORY_PATH}")

Expand All @@ -33,6 +34,8 @@ function(git_clone)
)
else()
message(STATUS "Repository already exists at ${_fun_REPOSITORY_PATH}.")
git_wait(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")

git_add_remote(
REMOTE_URL
"${_fun_REMOTE_URL}"
Expand Down Expand Up @@ -337,3 +340,20 @@ function(git_switch_back)
)
endif()
endfunction()

function(git_wait REPOSITORY_PATH)
set(oneValueArgs REPOSITORY_PATH)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})

if("${_fun_REPOSITORY_PATH}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH is required")
endif()

# 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"
)
message(STATUS "Waiting for git lock file...")
execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 0.5)
endwhile()
endfunction()

0 comments on commit 9fe821c

Please sign in to comment.