diff --git a/README.md b/README.md index 69afaf4..9a9d15c 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,14 @@ You can sponsor me [here](https://github.com/sponsors/jidicula)! * Available values: `LLVM`, `Google`, `Chromium`, `Mozilla`, `WebKit` and others listed in the `clang-format` [docs for BasedOnStyle](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configurable-format-style-options). * `exclude-regex` [optional]: A regex to exclude files or directories that should not be checked. * Default: `^$` - * Pattern matching is done with a real regex, not a glob expression. You can exclude multiple patterns like this: `(hello|world)`. + * Pattern matching is done with a POSIX `grep -E` extended regex, **not** a glob expression. You can exclude multiple patterns like this: `(hello|world)`. Build and verify your regex at https://regex101.com . +* `include-regex` [optional]: A regex to include files or directories that should be checked. + * Default: `^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu))$` + * Pattern matching is done with a POSIX `grep -E` extended regex, **not** a glob expression. You can exclude multiple patterns like this: `(hello|world)`. Build and verify your regex at https://regex101.com . This action checks all C/C++/Protobuf (including Arduino `.ino` and `.pde`) files in the provided directory in the GitHub workspace are formatted correctly using `clang-format`. If no directory is provided or the provided path is not a directory in the GitHub workspace, all C/C++/Protobuf files are checked. -The following file extensions are checked: +The following file extensions are checked by default: * Header files: * `.h` * `.H` @@ -71,7 +74,7 @@ The following file extensions are checked: # Usage -⚠️This action does not run on `windows` GitHub Actions runners! +⚠️This action does not run on `windows` GitHub Actions runners! ## Single Path diff --git a/action.yml b/action.yml index 99fd048..91817bf 100644 --- a/action.yml +++ b/action.yml @@ -22,12 +22,16 @@ inputs: description: 'The fallback style for clang-format if no .clang-format file exists in your repository.' required: false default: 'llvm' + include-regex: + description: 'A regex to override the C/C++/Protobuf/CUDA filetype regex. that should be checked. Default results in the regex defined in `check.sh`.' + required: false + default: '' runs: using: "composite" steps: - run: | - "${{ github.action_path }}/check.sh" "${{ inputs.clang-format-version }}" "${{ inputs.check-path }}" "${{ inputs.fallback-style }}" "${{ inputs.exclude-regex }}" + "${{ github.action_path }}/check.sh" "${{ inputs.clang-format-version }}" "${{ inputs.check-path }}" "${{ inputs.fallback-style }}" "${{ inputs.exclude-regex }} "${{ inputs.include-regex }}"" shell: bash - name: Save PR head commit SHA if: failure() && github.event_name == 'pull_request' diff --git a/check.sh b/check.sh index 2bf4cdb..95cb388 100755 --- a/check.sh +++ b/check.sh @@ -45,12 +45,24 @@ CLANG_FORMAT_VERSION="$1" CHECK_PATH="$2" FALLBACK_STYLE="$3" EXCLUDE_REGEX="$4" +INCLUDE_REGEX="$5" # Set the regex to an empty string regex if nothing was provided -if [ -z "$EXCLUDE_REGEX" ]; then +if [[ -z $EXCLUDE_REGEX ]]; then EXCLUDE_REGEX="^$" fi +# Set the filetype regex if nothing was provided. +# Find all C/C++/Protobuf/CUDA files: +# h, H, hpp, hh, h++, hxx +# c, C, cpp, cc, c++, cxx +# ino, pde +# proto +# cu +if [[ -z $INCLUDE_REGEX ]]; then + INCLUDE_REGEX='^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu))$' +fi + cd "$GITHUB_WORKSPACE" || exit 2 if [[ ! -d $CHECK_PATH ]]; then @@ -62,13 +74,7 @@ fi exit_code=0 # All files improperly formatted will be printed to the output. -# find all C/C++/Protobuf/CUDA files: -# h, H, hpp, hh, h++, hxx -# c, C, cpp, cc, c++, cxx -# ino, pde -# proto -# cu -src_files=$(find "$CHECK_PATH" -name .git -prune -o -regextype posix-egrep -regex '^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu))$' -print) +src_files=$(find "$CHECK_PATH" -name .git -prune -o -regextype posix-egrep -regex "$INCLUDE_REGEX" -print) # check formatting in each source file for file in $src_files; do diff --git a/test/test.sh b/test/test.sh index fbe1633..34dc065 100755 --- a/test/test.sh +++ b/test/test.sh @@ -4,10 +4,14 @@ FALLBACK_STYLE="llvm" EXCLUDE_REGEX="capital" CLANG_FORMAT_VERSION="$1" +############################################################################### +# Default C/C++/Protobuf/CUDA regex # +############################################################################### + # should succeed "$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_pass" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" docker_status="$?" -if [[ "$docker_status" != "0" ]]; then +if [[ $docker_status != "0" ]]; then echo "files that should succeed have failed!" exit 1 fi @@ -15,7 +19,29 @@ fi # should fail "$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_fail" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" docker_status="$?" -if [[ "$docker_status" == "0" ]]; then +if [[ $docker_status == "0" ]]; then + echo "files that should fail have succeeded!" + exit 2 +fi + +############################################################################### +# Custom filetype regex # +############################################################################### + +INCLUDE_REGEX='^.*\.(c|C)' + +# should succeed +"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_pass" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "$INCLUDE_REGEX" +docker_status="$?" +if [[ $docker_status != "0" ]]; then + echo "files that should succeed have failed!" + exit 1 +fi + +# should fail +"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_fail" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "$INCLUDE_REGEX" +docker_status="$?" +if [[ $docker_status == "0" ]]; then echo "files that should fail have succeeded!" exit 2 fi