diff --git a/cmake/GitHooks.cmake b/cmake/GitHooks.cmake index 1d5a26f5a..2b94b8790 100644 --- a/cmake/GitHooks.cmake +++ b/cmake/GitHooks.cmake @@ -21,3 +21,19 @@ endif () # We cannot write the file from here because we need exec permissions configure_file(${CMAKE_SOURCE_DIR}/cmake/utils/git_pre-commit.in ${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit) +find_package(Git) + +if(Git_FOUND) + message(STATUS "${GIT_EXECUTABLE} submodule foreach git rev-parse --git-path hooks") + execute_process(COMMAND ${GIT_EXECUTABLE} submodule --quiet foreach "git rev-parse --git-path hooks" OUTPUT_VARIABLE SUBMODULE_OUTPUT) + string(REPLACE "\n" ";" SUBMODULE_OUTPUT ${SUBMODULE_OUTPUT}) + set(SUBMODULE_HOOK ${SUBMODULE_OUTPUT}) + foreach(HOOK_DIR ${SUBMODULE_HOOK}) + if (NOT EXISTS ${HOOK_DIR}/pre-commit) + configure_file(${CMAKE_SOURCE_DIR}/cmake/utils/git_pre-commit.in ${HOOK_DIR}/pre-commit) + message(STATUS "Installing ${CMAKE_SOURCE_DIR}/cmake/utils/git_pre-commit.in in ${HOOK_DIR}/pre-commit") + endif() + endforeach() +else () + message (WARNING "Git not found") +endif () diff --git a/cmake/utils/git_pre-commit.in b/cmake/utils/git_pre-commit.in index 9f8dd585f..8950bc289 100755 --- a/cmake/utils/git_pre-commit.in +++ b/cmake/utils/git_pre-commit.in @@ -1,17 +1,17 @@ #!/bin/bash -STYLE=$PWD/.clang-format -echo "$STYLE" -if [ -n "$STYLE" ]; then - STYLEARG="-style=file" -else - STYLEARG="" +gitCommand=$(git rev-parse --show-superproject-working-tree) +if [ -z $gitCommand ]; then + gitCommand=$(git rev-parse --show-toplevel) fi +gitPath=$(readlink -f $gitCommand) +reformatCommand="bash $gitPath/scripts/reformat-files.sh" + format_file() { file="$1" if [ -f $file ]; then - clang-format -i $STYLEARG $1 + $reformatCommand $1 git add $1 fi } @@ -21,7 +21,7 @@ case "${1}" in echo "Runs clang-format on source files" ;; *) - for file in $(git diff-index --cached --name-only HEAD | grep -iE '\.(cxx|cc|h|C)$'); do + for file in $(git diff-index --cached --name-only HEAD | grep -iE '\.(cxx|cc|h|C|rml)$'); do format_file "$file" done ;; diff --git a/scripts/reformat-clang.sh b/scripts/reformat-files.sh old mode 100755 new mode 100644 similarity index 64% rename from scripts/reformat-clang.sh rename to scripts/reformat-files.sh index d6b73b737..e8a34ed68 --- a/scripts/reformat-clang.sh +++ b/scripts/reformat-files.sh @@ -24,30 +24,45 @@ # Change this if your clang-format executable is somewhere else CLANG_FORMAT="clang-format" +XML_LINT="xmllint" if [ $# -eq 0 ]; then echo ' ' echo 'This script formats the source directory /path/to/source/' - echo 'given as the only argument. Only *.h, *.cxx, *.cc and *.C files will be ' - echo 'reformatted.' + echo 'given as the only argument. Only *.h, *.cxx, *.cc, *.C and *.rml files will be reformatted.' echo ' ' - echo 'The formatting will consider the .clang-format file closer to' - echo 'the source directory given.' + echo 'The formatting will consider the .clang-format file closer to the source directory given.' echo ' ' - echo 'Usage: ./reformat-clang.sh /path/to/source/' + echo 'Usage: ./reformat-files.sh /path/to/source/' echo ' ' echo 'Use carefully! :)' - exit 1 fi +echo "Begin formatting!" + +if ! type -P $CLANG_FORMAT; then + echo "WARNING: '$CLANG_FORMAT' was not found in your system! we cannot format code files" + CLANG_FORMAT_DISABLED=True +fi + +if ! type -P $XML_LINT; then + echo "WARNING: '$XML_LINT' was not found in your system! we cannot format *.rml files" + XML_LINT_DISABLED=True +fi + pathToFormat=$(readlink -f $1) if [ -d "$pathToFormat" ]; then for DIRECTORY in $pathToFormat; do echo "Formatting code under $DIRECTORY/" - find "$DIRECTORY" \( -name '*.h' -or -name '*.cxx' -or -name '*.cc' -or -name '*.C' \) -print0 | xargs -0 "$CLANG_FORMAT" -i - echo "DONE!" + if [[ -z "$CLANG_FORMAT_DISABLED" ]]; then + find "$DIRECTORY" \( -name '*.h' -or -name '*.cxx' -or -name '*.cc' -or -name '*.C' \) -print0 | xargs -0 "$CLANG_FORMAT" -i + fi + if [[ -z "$XML_LINT_DISABLED" ]]; then + find "$DIRECTORY" -name "*.rml" -type f -exec $XML_LINT --output '{}' --format '{}' \; + fi + echo "Done formatting all files in '$DIRECTORY'" done elif [ -f "$pathToFormat" ]; then echo "Formatting file \"$pathToFormat\"" @@ -55,8 +70,10 @@ elif [ -f "$pathToFormat" ]; then if [[ "$ext" == "h" || "$ext" == "cxx" || "$ext" == "cc" || "$ext" == "C" ]]; then echo "$CLANG_FORMAT -i $pathToFormat" eval "$CLANG_FORMAT -i $pathToFormat" + elif [[ "$ext" == "rml" ]]; then + $XML_LINT --output "$pathToFormat" --format "$pathToFormat" else - echo "Not valid extension $ext, valid extensions are *.h, *.cxx, *.cc and *.C" + echo "Not valid extension $ext, valid extensions are *.h, *.cxx, *.cc, *.C or *.rml" fi else echo "$pathToFormat is not valid file or directory"