aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Lohnau <alexander.lohnau@gmx.de>2021-01-11 20:39:06 +0100
committerDavid Faure <faure@kde.org>2021-01-16 08:50:13 +0000
commit45edd1b17051def13c157cba3b01fac53364149c (patch)
treeec6644d3b24cf272ae1a6e87e9720fe409de2985
parentb7affc18e5dad21fe29451cd9ecff54e40e42d79 (diff)
downloadextra-cmake-modules-45edd1b17051def13c157cba3b01fac53364149c.tar.gz
extra-cmake-modules-45edd1b17051def13c157cba3b01fac53364149c.tar.bz2
Add cmake function to configure git pre-commit hooks
This will allow us to force QS checks before the developer commits a change. Currently only clang-format is supported, but as on #plasma and https://phabricator.kde.org/T11214 discussed we might want to add different formatters. By making the checks configurable we are flexible and can easily extend it.
-rw-r--r--CMakeLists.txt6
-rw-r--r--kde-modules/KDEGitCommitHooks.cmake66
-rwxr-xr-xkde-modules/kde-git-commit-hooks/clang-format.sh13
-rw-r--r--kde-modules/kde-git-commit-hooks/pre-commit.in3
4 files changed, 87 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e46b5c09..7e346d90 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -96,8 +96,12 @@ install(FILES ${installModuleFiles} DESTINATION ${MODULES_INSTALL_DIR})
file(GLOB installTestModuleFiles ${CMAKE_SOURCE_DIR}/test-modules/*[^~])
install(FILES ${installTestModuleFiles} DESTINATION ${SHARE_INSTALL_DIR}/ECM/test-modules)
-file(GLOB installKdeModuleFiles ${CMAKE_SOURCE_DIR}/kde-modules/*[^~])
+file(GLOB installKdeModuleFiles LIST_DIRECTORIES FALSE ${CMAKE_SOURCE_DIR}/kde-modules/*[^~])
install(FILES ${installKdeModuleFiles} DESTINATION ${KDE_MODULES_INSTALL_DIR})
+# The scripts need to be executable
+file(GLOB installKDECommitHooks ${CMAKE_SOURCE_DIR}/kde-modules/kde-git-commit-hooks/*[^~])
+install(FILES ${installKDECommitHooks} DESTINATION ${KDE_MODULES_INSTALL_DIR}/kde-git-commit-hooks
+ PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
file(GLOB installFindModuleFiles ${CMAKE_SOURCE_DIR}/find-modules/*.cmake ${CMAKE_SOURCE_DIR}/find-modules/*.py)
install(FILES ${installFindModuleFiles} DESTINATION ${FIND_MODULES_INSTALL_DIR})
diff --git a/kde-modules/KDEGitCommitHooks.cmake b/kde-modules/KDEGitCommitHooks.cmake
new file mode 100644
index 00000000..90f53844
--- /dev/null
+++ b/kde-modules/KDEGitCommitHooks.cmake
@@ -0,0 +1,66 @@
+#.rst:
+# KDEGitCommitHooks
+# --------------------
+#
+# This module provides a functionality to enforce formatting
+# or in the future other QS checks.
+#
+# This module provides the following function:
+#
+# ::
+#
+# kde_configure_pre_commit_hook(GIR_DIR <dir> CHECKS <checks...>)
+#
+# This function will create a pre-commit hook which contains all the given checks.
+# In case the source dir does not contain the .git folder, the GIT_DIR
+# parameter can be passed in.
+#
+# Example usage:
+#
+# .. code-block:: cmake
+#
+# include(KDEGitCommitHooks)
+# kde_configure_git_pre_commit_hook(CHECKS CLANG_FORMAT)
+#
+# Since 5.79
+
+#=============================================================================
+# SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+# try to find clang-format in path
+find_program(KDE_CLANG_FORMAT_EXECUTABLE clang-format)
+include(CMakeParseArguments)
+set(PRE_COMMIT_HOOK_UNIX "${CMAKE_CURRENT_LIST_DIR}/kde-git-commit-hooks/pre-commit.in")
+set(CLANG_FORMAT_UNIX "${CMAKE_CURRENT_LIST_DIR}/kde-git-commit-hooks/clang-format.sh")
+
+function(KDE_CONFIGURE_GIT_PRE_COMMIT_HOOK)
+ set(_oneValueArgs GIT_DIR)
+ set(_multiValueArgs CHECKS)
+ cmake_parse_arguments(ARG "" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN} )
+
+ if(NOT ARG_CHECKS)
+ message(FATAL_ERROR "No checks were specified")
+ endif()
+ if(NOT ARG_GIT_DIR)
+ set(ARG_GIT_DIR "${CMAKE_SOURCE_DIR}/.git")
+ endif()
+
+ # In case of tarballs there is no .git directory
+ if (EXISTS ${ARG_GIT_DIR})
+ # The pre-commit hook is a bash script, consequently it won't work on non-unix platforms
+ if (UNIX)
+ if(KDE_CLANG_FORMAT_EXECUTABLE)
+ list(FIND ARG_CHECKS "CLANG_FORMAT" _index)
+ if (${_index} GREATER -1)
+ set(CLANG_FORMAT_SCRIPT ${CLANG_FORMAT_UNIX})
+ endif()
+ else()
+ message(WARNING "No clang-format executable was found, skipping the formatting pre-commit hook")
+ endif()
+
+ configure_file(${PRE_COMMIT_HOOK_UNIX} "${ARG_GIT_DIR}/hooks/pre-commit")
+ endif()
+ endif()
+endfunction()
diff --git a/kde-modules/kde-git-commit-hooks/clang-format.sh b/kde-modules/kde-git-commit-hooks/clang-format.sh
new file mode 100755
index 00000000..d351e3cd
--- /dev/null
+++ b/kde-modules/kde-git-commit-hooks/clang-format.sh
@@ -0,0 +1,13 @@
+#!/usr/bin/env bash
+
+# Based on okular/hooks/pre-commit, credits go to Albert Astals Cid
+
+readonly output=$(git clang-format -v --diff)
+
+if [[ "$output" == *"no modified files to format"* ]]; then exit 0; fi
+if [[ "$output" == *"clang-format did not modify any files"* ]]; then exit 0; fi
+
+echo "ERROR: You have unformatted changes, please format your files. You can do this using the following commands:"
+echo " git clang-format --force # format the changed parts"
+echo " git clang-format --diff # preview the changes done by the formatter"
+exit 1
diff --git a/kde-modules/kde-git-commit-hooks/pre-commit.in b/kde-modules/kde-git-commit-hooks/pre-commit.in
new file mode 100644
index 00000000..36cdfd5d
--- /dev/null
+++ b/kde-modules/kde-git-commit-hooks/pre-commit.in
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+${CLANG_FORMAT_SCRIPT}