From a26fad09723e8745b4c6f29074cd7ebfef5b645b Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 12 May 2022 09:25:28 +0200 Subject: [PATCH 1/8] reformat-clang.sh - now also formats *.rml files using `xmllint` --- scripts/reformat-clang.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/reformat-clang.sh b/scripts/reformat-clang.sh index d6b73b737..dabb27698 100755 --- a/scripts/reformat-clang.sh +++ b/scripts/reformat-clang.sh @@ -47,7 +47,8 @@ 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!" + find "$DIRECTORY" -name "*.rml" -type f -exec xmllint --output '{}' --format '{}' \; + echo "Done formatting all files in '$DIRECTORY'" done elif [ -f "$pathToFormat" ]; then echo "Formatting file \"$pathToFormat\"" @@ -55,8 +56,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 + xmllint --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" From 4c8698393f6086acdb8146399fc0a98d0dd0ba92 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 12 May 2022 09:43:21 +0200 Subject: [PATCH 2/8] reformat-clang.sh - added checks for `clang-format` and `xmllint` --- scripts/reformat-clang.sh | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/scripts/reformat-clang.sh b/scripts/reformat-clang.sh index dabb27698..30216b3d4 100755 --- a/scripts/reformat-clang.sh +++ b/scripts/reformat-clang.sh @@ -24,6 +24,7 @@ # Change this if your clang-format executable is somewhere else CLANG_FORMAT="clang-format" +XML_LINT="xmllint" if [ $# -eq 0 ]; then echo ' ' @@ -41,13 +42,29 @@ if [ $# -eq 0 ]; then 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 - find "$DIRECTORY" -name "*.rml" -type f -exec xmllint --output '{}' --format '{}' \; + 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 @@ -57,7 +74,7 @@ elif [ -f "$pathToFormat" ]; then echo "$CLANG_FORMAT -i $pathToFormat" eval "$CLANG_FORMAT -i $pathToFormat" elif [[ "$ext" == "rml" ]]; then - xmllint --output "$pathToFormat" --format "$pathToFormat" + $XML_LINT --output "$pathToFormat" --format "$pathToFormat" else echo "Not valid extension $ext, valid extensions are *.h, *.cxx, *.cc, *.C or *.rml" fi From 8d98a571e55f552a8094df9ef1bf2dbeedcd7fb8 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 12 May 2022 09:45:13 +0200 Subject: [PATCH 3/8] Renamed `reformat-clang.sh` to `reformat-files.sh` because it is now not limited to clang-format --- scripts/{reformat-clang.sh => reformat-files.sh} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename scripts/{reformat-clang.sh => reformat-files.sh} (92%) mode change 100755 => 100644 diff --git a/scripts/reformat-clang.sh b/scripts/reformat-files.sh old mode 100755 new mode 100644 similarity index 92% rename from scripts/reformat-clang.sh rename to scripts/reformat-files.sh index 30216b3d4..0c5946a5c --- a/scripts/reformat-clang.sh +++ b/scripts/reformat-files.sh @@ -60,10 +60,10 @@ if [ -d "$pathToFormat" ]; then for DIRECTORY in $pathToFormat; do echo "Formatting code under $DIRECTORY/" 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 + 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 '{}' \; + find "$DIRECTORY" -name "*.rml" -type f -exec $XML_LINT --output '{}' --format '{}' \; fi echo "Done formatting all files in '$DIRECTORY'" done From c20eb3d9b9bdb8c87379c0fee772822a7756e08f Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Thu, 12 May 2022 09:51:00 +0200 Subject: [PATCH 4/8] reformat-files.sh - updated description --- scripts/reformat-files.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/scripts/reformat-files.sh b/scripts/reformat-files.sh index 0c5946a5c..e8a34ed68 100644 --- a/scripts/reformat-files.sh +++ b/scripts/reformat-files.sh @@ -29,16 +29,13 @@ 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 From 2ce46138928bc9ce08ca739b5181f401c80eb538 Mon Sep 17 00:00:00 2001 From: juanan Date: Thu, 12 May 2022 18:41:08 +0200 Subject: [PATCH 5/8] Updating git-hooks, now support submodules --- cmake/GitHooks.cmake | 16 ++++++++++++++++ cmake/utils/git_pre-commit.in | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) 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..8309dc649 100755 --- a/cmake/utils/git_pre-commit.in +++ b/cmake/utils/git_pre-commit.in @@ -1,6 +1,8 @@ #!/bin/bash -STYLE=$PWD/.clang-format +gitcmd=$(git rev-parse --show-superproject-working-tree) +gitpath=$(readlink -f $gitcmd) +STYLE=$gitpath/.clang-format echo "$STYLE" if [ -n "$STYLE" ]; then STYLEARG="-style=file" From 072430e06fbdc406e7278a9e6b88968a51d34f98 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 13 May 2022 09:34:32 +0200 Subject: [PATCH 6/8] Now git hook should also format rml files --- cmake/utils/git_pre-commit.in | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/cmake/utils/git_pre-commit.in b/cmake/utils/git_pre-commit.in index 8309dc649..979719f3e 100755 --- a/cmake/utils/git_pre-commit.in +++ b/cmake/utils/git_pre-commit.in @@ -1,19 +1,13 @@ #!/bin/bash -gitcmd=$(git rev-parse --show-superproject-working-tree) -gitpath=$(readlink -f $gitcmd) -STYLE=$gitpath/.clang-format -echo "$STYLE" -if [ -n "$STYLE" ]; then - STYLEARG="-style=file" -else - STYLEARG="" -fi +gitCommand=$(git rev-parse --show-superproject-working-tree) +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 } @@ -23,7 +17,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 ;; From de34f6130d84a2a200ee6c647dcab5afbdbaa00f Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 13 May 2022 09:37:29 +0200 Subject: [PATCH 7/8] git_pre-commit.in - fix typo --- cmake/utils/git_pre-commit.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/utils/git_pre-commit.in b/cmake/utils/git_pre-commit.in index 979719f3e..b362daf79 100755 --- a/cmake/utils/git_pre-commit.in +++ b/cmake/utils/git_pre-commit.in @@ -7,7 +7,7 @@ reformatCommand="bash $gitPath/scripts/reformat-files.sh" format_file() { file="$1" if [ -f $file ]; then - reformatCommand $1 + $reformatCommand $1 git add $1 fi } From 43df942528c893342237dda9d2d70799ab742e85 Mon Sep 17 00:00:00 2001 From: juanan Date: Fri, 13 May 2022 10:26:09 +0200 Subject: [PATCH 8/8] Fixing bug while running from superproject working tree --- cmake/utils/git_pre-commit.in | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmake/utils/git_pre-commit.in b/cmake/utils/git_pre-commit.in index b362daf79..8950bc289 100755 --- a/cmake/utils/git_pre-commit.in +++ b/cmake/utils/git_pre-commit.in @@ -1,6 +1,10 @@ #!/bin/bash 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"