From 0189bc3d358c5da77403c792502872629a1ebc71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Wed, 23 Sep 2026 17:21:27 +0000 Subject: [PATCH] BUG: Fail the check when no files are marked for clang-format clang-format.bash formats only tracked files whose hooks.style Git attribute includes clangformat. In a repository without such a .gitattributes entry no file is selected, and the script then runs "clang-format -i" with no file arguments. clang-format prints "error: cannot use -i when reading from stdin." but exits 0, so the check reported success without checking anything. Count the matching files first, fail with instructions for adding a .gitattributes when there are none, and log how many files are checked. Document the .gitattributes requirement in the README. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015k1PExcmd3DPt9VFSGGmcL --- README.md | 14 ++++++++++++++ action.yml | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) 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 }}"