From b6dd6ed9e11ac60a60b7b648080bf7cec28a02f3 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Wed, 12 Dec 2018 15:40:11 +0100
Subject: [PATCH] [CMake] Add --force to git submodule update when on CI.

This cleans dirty states in submodules which may happen when a
non-existing submodule hash was referenced.

Makes sure to always have the submodule at the desired state.
---
 scripts/cmake/SubmoduleSetup.cmake | 34 ++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/scripts/cmake/SubmoduleSetup.cmake b/scripts/cmake/SubmoduleSetup.cmake
index a07dbb57ab1..280ef5aab8f 100644
--- a/scripts/cmake/SubmoduleSetup.cmake
+++ b/scripts/cmake/SubmoduleSetup.cmake
@@ -40,22 +40,34 @@ foreach(SUBMODULE ${REQUIRED_SUBMODULES})
     string(REGEX MATCH "^\\-" UNINITIALIZED ${SUBMODULE_STATE})
     string(REGEX MATCH "^\\+" MISMATCH ${SUBMODULE_STATE})
 
-    set(RESULT "")
-    if(UNINITIALIZED)
-        message(STATUS "Initializing submodule ${SUBMODULE}")
+    if(IS_CI)
+        # Always set submodule to the given state
         execute_process(
-            COMMAND ${GIT_TOOL_PATH} submodule update --init --recursive ${DEPTH} ${SUBMODULE}
+            COMMAND ${GIT_TOOL_PATH} submodule update --init --force
+                --recursive ${DEPTH} ${SUBMODULE}
             WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
             RESULT_VARIABLE RESULT
         )
+    else()
+        set(RESULT "")
+        if(UNINITIALIZED)
+            message(STATUS "Initializing submodule ${SUBMODULE}")
+            execute_process(
+                COMMAND ${GIT_TOOL_PATH} submodule update --init
+                    --recursive ${DEPTH} ${SUBMODULE}
+                WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+                RESULT_VARIABLE RESULT
+            )
 
-    elseif(MISMATCH)
-        message(STATUS "Updating submodule ${SUBMODULE}")
-        execute_process(
-            COMMAND ${GIT_TOOL_PATH} submodule update --recursive ${SUBMODULE}
-            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
-            RESULT_VARIABLE RESULT
-        )
+        elseif(MISMATCH)
+            message(STATUS "Updating submodule ${SUBMODULE}")
+            execute_process(
+                COMMAND ${GIT_TOOL_PATH} submodule update
+                    --recursive ${SUBMODULE}
+                WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+                RESULT_VARIABLE RESULT
+            )
+        endif()
     endif()
 
     if((NOT ${RESULT} STREQUAL "") AND (NOT ${RESULT} STREQUAL "0"))
-- 
GitLab