-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Git.cmake
425 lines (324 loc) · 11.3 KB
/
Git.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
include_guard()
#[[.rst:
``git_clone``
===============
Clone the given repository to the given path
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
SHALLOW_SINCE
BRANCH
FORCE_CLONE
)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})
if("${_fun_REPOSITORY_PATH}" STREQUAL "" OR "${_fun_REMOTE_URL}" STREQUAL "")
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}" OR "${_fun_FORCE_CLONE}" STREQUAL "TRUE")
message(STATUS "Cloning at ${_fun_REPOSITORY_PATH}")
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}" ${GIT_ARGS} WORKING_DIRECTORY "${_fun_REPOSITORY_PARENT_PATH}"
COMMAND_ERROR_IS_FATAL LAST
)
else()
message(STATUS "Repository already exists at ${_fun_REPOSITORY_PATH}.")
git_wait(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
if(NOT EXISTS "${_fun_REPOSITORY_PATH}/.git")
message(
STATUS
"Folder ${_fun_REPOSITORY_PATH} exists but is not a git repository. Trying to force clone"
)
# recall the function with the force flag
git_clone(
REPOSITORY_PATH
"${_fun_REPOSITORY_PATH}"
REMOTE_URL
"${_fun_REMOTE_URL}"
REMOTE_NAME
"${_fun_REMOTE_NAME}"
FORCE_CLONE
TRUE
)
endif()
git_add_remote(
REMOTE_URL
"${_fun_REMOTE_URL}"
REPOSITORY_PATH
"${_fun_REPOSITORY_PATH}"
REMOTE_NAME
"${_fun_REMOTE_NAME}"
)
endif()
endfunction()
#[[.rst:
``git_pull``
============
Pull the given repository
It will temporarily switch back to the previous branch if the head is detached for updating
Input variables:
- ``REPOSITORY_PATH``: The path to the repository
]]
function(git_pull)
set(oneValueArgs REPOSITORY_PATH)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})
if("${_fun_REPOSITORY_PATH}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH is required")
endif()
# store the current revision
git_revision(REVISION REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
git_switch_back(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
message(STATUS "Updating ${_fun_REPOSITORY_PATH}")
find_program(GIT_EXECUTABLE "git" REQUIRED)
execute_process(COMMAND "${GIT_EXECUTABLE}" "pull" WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}")
# restore the revision
git_checkout(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}" REVISION "${REVISION}")
endfunction()
#[[.rst:
``git_checkout``
==================
Checkout the given revision
Input variables:
- ``REPOSITORY_PATH``: The path to the repository
- ``REVISION``: The revision to checkout
]]
function(git_checkout)
set(oneValueArgs REPOSITORY_PATH REVISION)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})
if("${_fun_REPOSITORY_PATH}" STREQUAL "" OR "${_fun_REVISION}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH and REVISION are required")
endif()
find_program(GIT_EXECUTABLE "git" REQUIRED)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "-c" "advice.detachedHead=false" "checkout" "${_fun_REVISION}"
WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}" COMMAND_ERROR_IS_FATAL LAST
)
endfunction()
#[[.rst:
``git_parse_url``
====================
Parse the given Git url into its components
It expects the url to be in the form of:
`[protocol:https://][host]/user/repo[.git]`
Input variables:
- ``INPUT_URL``: The url to parse
Output variables:
- ``PROTOCOL``: The protocol of the url (http, https, ssh, etc)
- ``HOST``: The host of the url (github, gitlab, etc)
- ``USER``: The user of the url (username, organization, etc)
- ``REPOSITORY_NAME``: The repository of the url (project name)
- ``FULL_URL``: The url of the repository (protocol + host + user + repo)
#]]
function(
git_parse_url
INPUT_URL
# output variables
PROTOCOL
HOST
USER
REPOSITORY_NAME
FULL_URL
)
# https://regex101.com/r/gVep0l/1
string(REGEX MATCH "([a-z]+:\/\/)?(.*\/)?([^/]*)\/(.*)" _matched "${INPUT_URL}")
if(NOT "${_matched}" STREQUAL "${INPUT_URL}")
message(SEND_ERROR "Could not parse git url: ${URL}")
return()
endif()
set(_PROTOCOL ${CMAKE_MATCH_1})
set(_HOST ${CMAKE_MATCH_2})
set(_USER ${CMAKE_MATCH_3})
set(_REPOSITORY_NAME ${CMAKE_MATCH_4})
if(NOT _USER OR NOT _REPOSITORY_NAME)
message(SEND_ERROR "Could not parse git url: ${URL}")
return()
endif()
if(NOT _PROTOCOL)
set(_PROTOCOL "https://")
endif()
if(NOT _HOST)
set(_HOST "github.com")
endif()
# strip .git from the end of the repository name
string(REGEX REPLACE "\.git$" "" _REPOSITORY_NAME "${_REPOSITORY_NAME}")
# construct the full url
set(_FULL_URL "${_PROTOCOL}${_HOST}/${_USER}/${_REPOSITORY_NAME}.git")
set(${PROTOCOL} ${_PROTOCOL} PARENT_SCOPE)
set(${HOST} ${_HOST} PARENT_SCOPE)
set(${USER} ${_USER} PARENT_SCOPE)
set(${REPOSITORY_NAME} ${_REPOSITORY_NAME} PARENT_SCOPE)
set(${FULL_URL} ${_FULL_URL} PARENT_SCOPE)
endfunction()
#[[.rst:
``git_add_remote``
=====================
Add a remote to the given repository on the given path
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)
]]
function(git_add_remote)
set(oneValueArgs REPOSITORY_PATH REMOTE_URL REMOTE_NAME)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})
if("${_fun_REPOSITORY_PATH}" STREQUAL "" OR "${_fun_REMOTE_URL}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH and REMOTE_URL are required")
endif()
find_program(GIT_EXECUTABLE "git" REQUIRED)
# ensure that the given repository's remote is the current remote
execute_process(
COMMAND "${GIT_EXECUTABLE}" "remote" "-v" WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
COMMAND_ERROR_IS_FATAL LAST
OUTPUT_VARIABLE _remote_output
)
string(FIND "${_remote_output}" "${_fun_REMOTE_URL}" _find_index)
# Add the given remote if it doesn't exist
if(${_find_index} EQUAL -1)
if("${_fun_REMOTE_NAME}" STREQUAL "")
# use the remote user as the remote name if it's not given
git_parse_url(
"${_fun_REMOTE_URL}"
_PROTOCOL
_HOST
_USER
_REPOSITORY_NAME
_FULL_URL
)
set(_fun_REMOTE_NAME "${_USER}")
endif()
execute_process(
COMMAND "${GIT_EXECUTABLE}" "remote" "add" "${_fun_REMOTE_NAME}" "${_fun_REMOTE_URL}"
WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}" COMMAND_ERROR_IS_FATAL LAST
)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "fetch" "${_fun_REMOTE_NAME}"
WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}" COMMAND_ERROR_IS_FATAL LAST
)
endif()
endfunction()
#[[.rst:
``git_revision``
================
Find the current git revision of the given repository
Input variables:
- ``REPOSITORY_PATH``: The path to the repository
Output variables:
- ``REVISION``: The variable to store the revision in
]]
function(git_revision REVISION)
set(oneValueArgs REPOSITORY_PATH)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})
if("${_fun_REPOSITORY_PATH}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH is required")
endif()
find_program(GIT_EXECUTABLE "git" REQUIRED)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "rev-parse" "HEAD"
OUTPUT_VARIABLE _git_revision
WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(${REVISION} ${_git_revision} PARENT_SCOPE)
endfunction()
#[[.rst:
``git_is_detached``
===================
Check if the given repository is in a detached state
Input variables:
- ``REPOSITORY_PATH``: The path to the repository
Output variables:
- ``IS_DETACHED``: The variable to store the result in
]]
function(git_is_detached IS_DETACHED)
set(oneValueArgs REPOSITORY_PATH)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})
if("${_fun_REPOSITORY_PATH}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH is required")
endif()
set(_git_status "")
find_program(GIT_EXECUTABLE "git" REQUIRED)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "rev-parse" "--abbrev-ref" "--symbolic-full-name" "HEAD"
OUTPUT_VARIABLE _git_status
WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if("${_git_status}" STREQUAL "HEAD")
set(${IS_DETACHED} TRUE PARENT_SCOPE)
else()
set(${IS_DETACHED} FALSE PARENT_SCOPE)
endif()
endfunction()
#[[.rst:
``git_switch_back``
===================
Detect if the head is detached, if so, switch back before calling git pull on a detached head
Input variables:
- ``REPOSITORY_PATH``: The path to the repository
]]
function(git_switch_back)
set(oneValueArgs REPOSITORY_PATH)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})
if("${_fun_REPOSITORY_PATH}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH is required")
endif()
git_is_detached(IS_DETACHED REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
if(${IS_DETACHED})
message(STATUS "Switch back ${_fun_REPOSITORY_PATH}")
execute_process(
COMMAND "${GIT_EXECUTABLE}" "switch" "-" WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
)
endif()
endfunction()
#[[.rst:
``git_wait``
============
Wait for the git lock file to be released
Input variables:
- ``REPOSITORY_PATH``: The path to the repository
- ``TIMEOUT_COUNTER``: The number of times to wait before timing out
]]
function(git_wait)
set(oneValueArgs REPOSITORY_PATH TIMEOUT_COUNTER)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})
if("${_fun_REPOSITORY_PATH}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH is required")
endif()
if("${_fun_TIMEOUT_COUNTER}" STREQUAL "")
set(_fun_TIMEOUT_COUNTER 20)
endif()
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"
)
execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 0.5)
math(EXPR counter "${counter} + 1")
if(${counter} GREATER ${_fun_TIMEOUT_COUNTER})
message(STATUS "Timeout waiting for git lock file. Continuing...")
return()
else()
message(STATUS "Waiting for git lock file...[${counter}/${_fun_TIMEOUT_COUNTER}]")
endif()
endwhile()
endfunction()