From d0f58e8beafe4ffd4bd371152947b84ebe8a8ef2 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 10 Apr 2022 06:22:49 +0200 Subject: KDEGitCommitHooks: only configure pre-commit hook if needed If clang-format isn't found, there is nothing to write to the file. --- kde-modules/KDEGitCommitHooks.cmake | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'kde-modules') diff --git a/kde-modules/KDEGitCommitHooks.cmake b/kde-modules/KDEGitCommitHooks.cmake index b4819396..bb593080 100644 --- a/kde-modules/KDEGitCommitHooks.cmake +++ b/kde-modules/KDEGitCommitHooks.cmake @@ -55,22 +55,25 @@ function(KDE_CONFIGURE_GIT_PRE_COMMIT_HOOK) endif() set(GIT_DIR "${CMAKE_SOURCE_DIR}/.git") - # In case of tarballs there is no .git directory - if (IS_DIRECTORY ${GIT_DIR}) - # The pre-commit hook is a bash script, consequently it won't work on non-unix platforms - # git on Windows provides its own bash - if (UNIX OR WIN32) - if(KDE_CLANG_FORMAT_EXECUTABLE) - list(FIND ARG_CHECKS "CLANG_FORMAT" _index) - if (${_index} GREATER -1) - set(CLANG_FORMAT_SCRIPT "\"$(git rev-parse --git-common-dir)\"/hooks/scripts/clang-format.sh") - configure_file(${CLANG_FORMAT_UNIX} "${GIT_DIR}/hooks/scripts/clang-format.sh" @ONLY) - endif() - else() - message(WARNING "No clang-format executable was found, skipping the formatting pre-commit hook") - endif() - - configure_file(${PRE_COMMIT_HOOK_UNIX} "${GIT_DIR}/hooks/pre-commit") + if (NOT IS_DIRECTORY ${GIT_DIR} # In case of tarballs there is no .git directory + OR NOT (UNIX OR WIN32) + ) + return() + endif() + + set(_write_hook FALSE) + if(KDE_CLANG_FORMAT_EXECUTABLE) + list(FIND ARG_CHECKS "CLANG_FORMAT" _index) + if (${_index} GREATER -1) + set(CLANG_FORMAT_SCRIPT "\"$(git rev-parse --git-common-dir)\"/hooks/scripts/clang-format.sh") + configure_file(${CLANG_FORMAT_UNIX} "${GIT_DIR}/hooks/scripts/clang-format.sh" @ONLY) + set(_write_hook TRUE) endif() + else() + message(WARNING "No clang-format executable was found, skipping the formatting pre-commit hook") + endif() + + if(_write_hook) + configure_file(${PRE_COMMIT_HOOK_UNIX} "${GIT_DIR}/hooks/pre-commit") endif() endfunction() -- cgit v1.2.1