diff options
| author | Ahmad Samir <a.samirh78@gmail.com> | 2022-04-10 13:56:35 +0200 | 
|---|---|---|
| committer | Ahmad Samir <a.samirh78@gmail.com> | 2022-04-10 16:32:10 +0200 | 
| commit | 6224e7b8c28d434b81a19ac47b88cb58fee9d7d5 (patch) | |
| tree | 0a6ffde7a8d87938a210d710c808482c0ab2ee9f | |
| parent | d0f58e8beafe4ffd4bd371152947b84ebe8a8ef2 (diff) | |
| download | extra-cmake-modules-6224e7b8c28d434b81a19ac47b88cb58fee9d7d5.tar.gz extra-cmake-modules-6224e7b8c28d434b81a19ac47b88cb58fee9d7d5.tar.bz2  | |
KDEGitCommitHooks: don't overwrite git pre-commit hook script
If the .git/hooks/pre-commit already exists, only add the clang-format line
if it doesn't already exist.
| -rw-r--r-- | kde-modules/KDEGitCommitHooks.cmake | 28 | 
1 files changed, 27 insertions, 1 deletions
diff --git a/kde-modules/KDEGitCommitHooks.cmake b/kde-modules/KDEGitCommitHooks.cmake index bb593080..a9ee76e9 100644 --- a/kde-modules/KDEGitCommitHooks.cmake +++ b/kde-modules/KDEGitCommitHooks.cmake @@ -1,4 +1,5 @@  # SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de> +# SPDX-FileCopyrightText: 2022 Ahmad Samir <a.samirh78@gmail.com>  #  # SPDX-License-Identifier: BSD-3-Clause @@ -73,7 +74,32 @@ function(KDE_CONFIGURE_GIT_PRE_COMMIT_HOOK)          message(WARNING "No clang-format executable was found, skipping the formatting pre-commit hook")      endif() -    if(_write_hook) +    if(NOT _write_hook) +        return() +    endif() + +    set(_hook_file "${GIT_DIR}/hooks/pre-commit") +    # Doesn't exist? write away +    if(NOT EXISTS ${_hook_file})          configure_file(${PRE_COMMIT_HOOK_UNIX} "${GIT_DIR}/hooks/pre-commit") +        return() +    endif() + +    file(READ ${_hook_file} _contents) + +    # For when CLANG_FORMAT_SCRIPT didn't have the 'git rev-parse --git-common-dir' part +    set(_old_cmd "./.git/hooks/scripts/clang-format.sh") +    string(FIND ${_contents} ${_old_cmd} _idx) +    if (${_idx} GREATER -1) +        string(REPLACE "${_old_cmd}" "${CLANG_FORMAT_SCRIPT}" _contents "${_contents}") +        file(WRITE ${_hook_file} ${_contents}) +        return() +    endif() + +    string(FIND ${_contents} ${CLANG_FORMAT_SCRIPT} _idx) +    # File exists and doesn't have the clang-format.sh line, append it +    # so as to not overwrite users' customisations +    if (_idx EQUAL -1) +        file(APPEND ${_hook_file} ${CLANG_FORMAT_SCRIPT})      endif()  endfunction()  | 
