| 1 |
# |
| 2 |
# cmake -P script to do and update of the base git repo. |
| 3 |
# |
| 4 |
# Usage: |
| 5 |
# |
| 6 |
# cmake [arguments] -P tribits_ctest_update_commands.cmake |
| 7 |
# |
| 8 |
# where the -D<var>=<value> arguments are shown below. |
| 9 |
# |
| 10 |
# The list of commands in this script completely clean out a git repo and |
| 11 |
# create a local branch with name ${BRANCH} tracking a remote branch ${BRANCH} |
| 12 |
# in the remote repo ${REMOTE_NAME}. This is robust no matter what the |
| 13 |
# current state of the local git repo. The built-in git commands used by |
| 14 |
# ctest_update() are not robust to some use cases where these commands are. |
| 15 |
# For example, the commands are robust to the situation where the local repo |
| 16 |
# may be tracking a remote branch that may have been deleted in the remote |
| 17 |
# repo. The default commands used in ctest_update() (at least as of CMake |
| 18 |
# 3.12) crash in that case. |
| 19 |
# |
| 20 |
|
| 21 |
cmake_minimum_required(VERSION 3.23.0 FATAL_ERROR) |
| 22 |
|
| 23 |
message("\ncmake -P tribits_ctest_update_commands.cmake:") |
| 24 |
message("-- GIT_EXE=${GIT_EXE}") |
| 25 |
message("-- REMOTE_NAME=${REMOTE_NAME}") |
| 26 |
message("-- BRANCH=${BRANCH}") |
| 27 |
message("-- UNIT_TEST_MODE=${UNIT_TEST_MODE}") |
| 28 |
|
| 29 |
set(OVERALL_SUCCESS TRUE) |
| 30 |
set(ERROR_CODE 0) |
| 31 |
|
| 32 |
macro(execute_process_wrapper) |
| 33 |
if (UNIT_TEST_MODE) |
| 34 |
message("execute_process(${ARGN})") |
| 35 |
else() |
| 36 |
execute_process(${ARGN} RESULT_VARIABLE RTN_CODE) |
| 37 |
message("RTN_CODE: ${RTN_CODE}") |
| 38 |
if (NOT "${RTN_CODE}" STREQUAL "0") |
| 39 |
set(OVERALL_SUCCESS FALSE) |
| 40 |
set(ERROR_CODE ${RTN_CODE}) |
| 41 |
endif() |
| 42 |
endif() |
| 43 |
endmacro() |
| 44 |
|
| 45 |
macro(run_command) |
| 46 |
string(REPLACE ";" " " CMND_STR "${ARGN}") |
| 47 |
message("\nRunning: ${CMND_STR}") |
| 48 |
execute_process_wrapper(COMMAND ${ARGN}) |
| 49 |
endmacro() |
| 50 |
|
| 51 |
run_command( |
| 52 |
"${GIT_EXE}" fetch ${REMOTE_NAME} ) |
| 53 |
|
| 54 |
run_command( |
| 55 |
"${GIT_EXE}" clean -fdx ) |
| 56 |
|
| 57 |
run_command( |
| 58 |
"${GIT_EXE}" reset --hard HEAD ) |
| 59 |
|
| 60 |
if (BRANCH) |
| 61 |
run_command( |
| 62 |
"${GIT_EXE}" checkout -B ${BRANCH} --track ${REMOTE_NAME}/${BRANCH} ) |
| 63 |
else() |
| 64 |
run_command( |
| 65 |
"${GIT_EXE}" reset --hard @{u} ) |
| 66 |
endif() |
| 67 |
|
| 68 |
if (OVERALL_SUCCESS) |
| 69 |
message("\nGit Update PASSED!") |
| 70 |
else() |
| 71 |
message(FATAL_ERROR "Git Update FAILED!") |
| 72 |
endif() |
| 73 |
|
| 74 |
# NOTE: Above, you have to use separate execute_process() commands for each |
| 75 |
# git command or you get git errors complaining about git commands running on |
| 76 |
# top of each other. The execute_process() implementation must not ensure |
| 77 |
# that one command is completely finished before the next one starts. |