From 9ae12195b74256e14f73199b05c70c9dd7eb0723 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Wed, 14 Aug 2019 15:08:31 +0200
Subject: [PATCH] [hooks] Added cmake-format hook.

Requires cmake-format pip package.
---
 .pre-commit-config.yaml                  |  6 +++
 CMakeLists.txt                           |  5 +-
 Tests/CMakeLists.txt                     | 69 +++++++++++++-----------
 scripts/hooks/pre-commit-cmake-format.sh | 30 +++++++++++
 scripts/hooks/pre-commit-git-lfs.sh      |  3 --
 5 files changed, 76 insertions(+), 37 deletions(-)
 create mode 100755 scripts/hooks/pre-commit-cmake-format.sh

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 1437ab2a2f4..8e06df58a29 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -32,5 +32,11 @@ repos:
         entry: scripts/hooks/pre-commit-git-lfs.sh
         language: system
         stages: [commit, manual]
+    -   id: cmake-format
+        name: Format CMake files
+        entry: scripts/hooks/pre-commit-cmake-format.sh
+        language: system
+        files: 'CMakeLists.txt'
+        stages: [commit, manual]
 
 exclude: 'ThirdParty/*|Tests/Data/*|web/static/images/xsd'
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 61912a1c66e..efbc30c5324 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -181,10 +181,7 @@ set(OGS_EIGEN_DYNAMIC_SHAPE_MATRICES
     "Default"
     CACHE STRING "Use dynamically allocated shape matrices")
 set_property(CACHE OGS_EIGEN_DYNAMIC_SHAPE_MATRICES
-             PROPERTY STRINGS
-                      "Default"
-                      "ON"
-                      "OFF")
+             PROPERTY STRINGS "Default" "ON" "OFF")
 
 if(OGS_EIGEN_DYNAMIC_SHAPE_MATRICES STREQUAL "Default")
     if(CMAKE_BUILD_TYPE STREQUAL "Release"
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 64a699d341d..502ea4d5571 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -1,34 +1,34 @@
 include(${PROJECT_SOURCE_DIR}/scripts/cmake/OGSEnabledElements.cmake)
 
-# VS2012 doesn't support correctly the tuples yet
-# See http://code.google.com/p/googletest/issues/detail?id=412
+# VS2012 doesn't support correctly the tuples yet See
+# http://code.google.com/p/googletest/issues/detail?id=412
 if(MSVC)
     add_definitions(/D_VARIADIC_MAX=10)
 endif()
 
-APPEND_SOURCE_FILES(TEST_SOURCES)
-APPEND_SOURCE_FILES(TEST_SOURCES BaseLib)
-APPEND_SOURCE_FILES(TEST_SOURCES FileIO)
-APPEND_SOURCE_FILES(TEST_SOURCES GeoLib)
-APPEND_SOURCE_FILES(TEST_SOURCES GeoLib/IO)
-APPEND_SOURCE_FILES(TEST_SOURCES MaterialLib)
-APPEND_SOURCE_FILES(TEST_SOURCES MathLib)
-APPEND_SOURCE_FILES(TEST_SOURCES MeshLib)
-APPEND_SOURCE_FILES(TEST_SOURCES MeshGeoToolsLib)
-APPEND_SOURCE_FILES(TEST_SOURCES NumLib)
-APPEND_SOURCE_FILES(TEST_SOURCES ParameterLib)
-APPEND_SOURCE_FILES(TEST_SOURCES ProcessLib)
+append_source_files(TEST_SOURCES)
+append_source_files(TEST_SOURCES BaseLib)
+append_source_files(TEST_SOURCES FileIO)
+append_source_files(TEST_SOURCES GeoLib)
+append_source_files(TEST_SOURCES GeoLib/IO)
+append_source_files(TEST_SOURCES MaterialLib)
+append_source_files(TEST_SOURCES MathLib)
+append_source_files(TEST_SOURCES MeshLib)
+append_source_files(TEST_SOURCES MeshGeoToolsLib)
+append_source_files(TEST_SOURCES NumLib)
+append_source_files(TEST_SOURCES ParameterLib)
+append_source_files(TEST_SOURCES ProcessLib)
 
 if(OGS_BUILD_GUI)
-    APPEND_SOURCE_FILES(TEST_SOURCES FileIO_Qt)
+    append_source_files(TEST_SOURCES FileIO_Qt)
 endif()
 
 if(OGS_BUILD_SWMM)
-    APPEND_SOURCE_FILES(TEST_SOURCES FileIO_SWMM)
+    append_source_files(TEST_SOURCES FileIO_SWMM)
 endif()
 
 if(OGS_BUILD_PROCESS_LIE)
-    APPEND_SOURCE_FILES(TEST_SOURCES ProcessLib/LIE)
+    append_source_files(TEST_SOURCES ProcessLib/LIE)
 endif()
 
 if(OGS_USE_PETSC)
@@ -39,17 +39,16 @@ add_executable(testrunner ${TEST_SOURCES})
 set_target_properties(testrunner PROPERTIES FOLDER Testing)
 
 target_link_libraries(testrunner
-    ApplicationsFileIO
-    gtest
-    MeshGeoToolsLib
-    MaterialLib
-    MeshLib
-    NumLib
-    ParameterLib
-    ProcessLib
-    Threads::Threads
-    ${VTK_LIBRARIES}
-)
+                      ApplicationsFileIO
+                      gtest
+                      MeshGeoToolsLib
+                      MaterialLib
+                      MeshLib
+                      NumLib
+                      ParameterLib
+                      ProcessLib
+                      Threads::Threads
+                      ${VTK_LIBRARIES})
 
 if(OGS_BUILD_PROCESS_LIE)
     target_link_libraries(testrunner LIE)
@@ -72,7 +71,13 @@ if(OGS_INSITU)
 endif()
 
 if(OGS_BUILD_GUI)
-    target_link_libraries(testrunner Qt5::Core Qt5::Gui Qt5::Xml Qt5::Network VtkVis QtDataView)
+    target_link_libraries(testrunner
+                          Qt5::Core
+                          Qt5::Gui
+                          Qt5::Xml
+                          Qt5::Network
+                          VtkVis
+                          QtDataView)
     if(GEOTIFF_FOUND)
         target_link_libraries(testrunner ${GEOTIFF_LIBRARIES})
     endif()
@@ -82,6 +87,7 @@ if(OGS_USE_PCH)
     cotire(testrunner)
 endif()
 
+# cmake-format: off
 # Add make-target tests which runs the testrunner
 if(IS_CI AND NOT OGS_COVERAGE)
     set(TESTRUNNER_ADDITIONAL_ARGUMENTS ${TESTRUNNER_ADDITIONAL_ARGUMENTS}
@@ -114,8 +120,11 @@ else()
         DEPENDS testrunner tests-cleanup
     )
 endif()
+# cmake-format: on
 
 set_target_properties(tests PROPERTIES FOLDER Testing)
 
 # Creates one ctest entry for every googletest
-#ADD_GOOGLE_TESTS ( ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/testrunner ${TEST_SOURCES})
+# ~~~
+# ADD_GOOGLE_TESTS (${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/testrunner ${TEST_SOURCES})
+# ~~~
diff --git a/scripts/hooks/pre-commit-cmake-format.sh b/scripts/hooks/pre-commit-cmake-format.sh
new file mode 100755
index 00000000000..785a6cb5b6a
--- /dev/null
+++ b/scripts/hooks/pre-commit-cmake-format.sh
@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+
+EXE=$(command -v cmake-format)
+if [ -z "$EXE" ]; then
+    echo "cmake-format not found; skipping check."
+    exit 0
+fi
+
+
+MOD_FILES=""
+
+MOD_FILES=()
+for FILE in "$@"
+do
+    cmake-format -i "$FILE"
+    MODIFIED=$(git status --porcelain "$FILE" | head -c 2 | tail -c 1)
+    # echo $FILE: $MODIFIED
+    if [ "$MODIFIED" = "M" ]; then
+        MOD_FILES+=("$FILE")
+        echo "Fixed $FILE"
+    fi
+done
+
+if [ -z "$MOD_FILES" ]; then
+    exit 0
+fi
+
+echo "CMake files have been modified."
+echo "Add them to the commit!"
+exit 1
diff --git a/scripts/hooks/pre-commit-git-lfs.sh b/scripts/hooks/pre-commit-git-lfs.sh
index 820f73e2250..cd6397eaad9 100755
--- a/scripts/hooks/pre-commit-git-lfs.sh
+++ b/scripts/hooks/pre-commit-git-lfs.sh
@@ -1,14 +1,11 @@
 #!/usr/bin/env bash
 
-set -e
-
 BINARY_FILES=""
 LFS_FILES=""
 
 LFS_FILES=()
 for FILE in "$@"
 do
-    echo FILE: $FILE
     LFS_FILE=$(git check-attr filter "$FILE" | grep 'filter: lfs$' | sed -e 's/: filter: lfs//')
     if [ ! -z "$LFS_FILE" ]; then
         LFS_FILES+=("$LFS_FILE")
-- 
GitLab