diff --git a/README.md b/README.md index db40fc0..76d55ea 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,20 @@ jobs: The linter check will fail on a pull request if style changes are required. +Only files whose `hooks.style` Git attribute includes `clangformat` are checked, so the repository +needs a *.gitattributes* file that marks its C and C++ sources, as ITK's does: + +``` +[attr]our-c-style whitespace=tab-in-indent,no-lf-at-eof hooks.style=KWStyle,clangformat + +*.c our-c-style +*.h our-c-style +*.cxx our-c-style +*.hxx our-c-style +``` + +The check fails if no tracked file has this attribute, since there would be nothing to check. + ## See Also When used with diff --git a/action.yml b/action.yml index 0be625f..2678c93 100644 --- a/action.yml +++ b/action.yml @@ -100,6 +100,27 @@ runs: shell: bash run: | set -euo pipefail + + # clang-format.bash only formats tracked files whose hooks.style Git + # attribute includes "clangformat", as set in .gitattributes. When no + # file matches, it runs "clang-format -i" with no file arguments, which + # prints "cannot use -i when reading from stdin" but exits 0, so the + # check would pass without having checked anything. Fail instead. + file_count=$(git ls-files | git check-attr --stdin hooks.style \ + | grep -e 'hooks.style: .*clangformat' | grep -v 'ThirdParty' | wc -l || true) + if [[ "${file_count}" -eq 0 ]]; then + echo "::error::No tracked files have the hooks.style=clangformat Git attribute, so there is nothing to check." + echo "" + echo "Mark the sources to check in a .gitattributes file at the repository root, for example:" + echo "" + echo " [attr]our-c-style whitespace=tab-in-indent,no-lf-at-eof hooks.style=KWStyle,clangformat" + echo " *.h our-c-style" + echo " *.hxx our-c-style" + echo " *.cxx our-c-style" + exit 1 + fi + echo "Checking ${file_count} files with the hooks.style=clangformat Git attribute." + ./clang-format.bash --tracked if ! git diff-index --diff-filter=M --quiet HEAD -- ':!.clang-format'; then echo "::error::${{ inputs.error-message }}"