From 8bf08ddaa214b3ed38f2d1b0a58732a727bbed1a Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Mon, 17 Jun 2024 22:45:38 -0400 Subject: [PATCH 1/4] initial format-filepath addition --- README.md | 2 ++ action.yml | 6 +++++- check.sh | 12 ++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f56edf1..eabbcae 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ You can sponsor me [here](https://github.com/sponsors/jidicula)! * `include-regex` [optional]: A regex to include files or directories that should be checked. * Default: see [`INCLUDE_REGEX`](./check.sh#77) * 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 [regex101.com](https://regex101.com) ([example](https://regex101.com/r/llFcLy/7)). +* `format-filepath` [optional]: A filepath to the clang_format file to override the default, which is `.clang_format` in the directory closest to the parent.' + default: '' 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. diff --git a/action.yml b/action.yml index 26fe4d0..c2b0d98 100644 --- a/action.yml +++ b/action.yml @@ -26,12 +26,16 @@ inputs: 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: '' + format-filepath: + description: 'A filepath to the clang_format file to override the default, which is `.clang_format` in the directory closest to the parent.' + 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 }}" "${{ inputs.include-regex }}" + "${{ github.action_path }}/check.sh" "${{ inputs.clang-format-version }}" "${{ inputs.check-path }}" "${{ inputs.fallback-style }}" "${{ inputs.exclude-regex }}" "${{ inputs.include-regex }}" "${{ inputs.format-filepath }}" shell: bash - name: Save PR head commit SHA if: failure() && github.event_name == 'pull_request' diff --git a/check.sh b/check.sh index 46dd269..24d37a5 100755 --- a/check.sh +++ b/check.sh @@ -27,7 +27,7 @@ format_diff() { ghcr.io/jidicula/clang-format:"$CLANG_FORMAT_MAJOR_VERSION" \ --dry-run \ --Werror \ - --style=file \ + --style=file"$FORMAT_FILEPATH" \ --fallback-style="$FALLBACK_STYLE" \ "${filepath}")" else # Versions below 9 don't have dry run @@ -35,7 +35,7 @@ format_diff() { --volume "$(pwd)":"$(pwd)" \ --workdir "$(pwd)" \ ghcr.io/jidicula/clang-format:"$CLANG_FORMAT_MAJOR_VERSION" \ - --style=file \ + --style=file"$FORMAT_FILEPATH" \ --fallback-style="$FALLBACK_STYLE" \ "${filepath}")" local_format="$(diff -q <(cat "${filepath}") <(echo "${formatted}"))" @@ -60,6 +60,7 @@ CHECK_PATH="$2" FALLBACK_STYLE="$3" EXCLUDE_REGEX="$4" INCLUDE_REGEX="$5" +FORMAT_FILEPATH="$6" # Set the regex to an empty string regex if nothing was provided if [[ -z $EXCLUDE_REGEX ]]; then @@ -84,6 +85,13 @@ if [[ ! -d $CHECK_PATH ]]; then CHECK_PATH="." fi +if [[ ! -f $FORMAT_FILEPATH ]]; then + echo "Not a file in the workspace, fallback to .clang_format." >&2 + FORMAT_FILEPATH="" +else + FORMAT_FILEPATH=":$FORMAT_FILEPATH" +fi + # initialize exit code exit_code=0 From 90955bb91ec4f79ce234536d153c377a8fa38087 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Tue, 18 Jun 2024 00:23:07 -0400 Subject: [PATCH 2/4] add docs --- README.md | 2 +- check.sh | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eabbcae..a4a2215 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ You can sponsor me [here](https://github.com/sponsors/jidicula)! * `include-regex` [optional]: A regex to include files or directories that should be checked. * Default: see [`INCLUDE_REGEX`](./check.sh#77) * 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 [regex101.com](https://regex101.com) ([example](https://regex101.com/r/llFcLy/7)). -* `format-filepath` [optional]: A filepath to the clang_format file to override the default, which is `.clang_format` in the directory closest to the parent.' +* `format-filepath` [optional]: A filepath to the clang_format file to override the default, which is `.clang_format` file located in the closest parent directory of the input file.' default: '' 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. diff --git a/check.sh b/check.sh index 24d37a5..bb72f15 100755 --- a/check.sh +++ b/check.sh @@ -86,9 +86,10 @@ if [[ ! -d $CHECK_PATH ]]; then fi if [[ ! -f $FORMAT_FILEPATH ]]; then - echo "Not a file in the workspace, fallback to .clang_format." >&2 + echo "Not a file in the workspace, fallback to search for .clang_format." >&2 FORMAT_FILEPATH="" else + # if being used, add the colon for seperating the syntax file: FORMAT_FILEPATH=":$FORMAT_FILEPATH" fi From cbb9a04f45887235a879ecac5bacdbb592a0d3e2 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Mon, 5 Aug 2024 20:47:27 -0400 Subject: [PATCH 3/4] add tests, fix extra verbosity --- check.sh | 4 ++-- .../clang_format_example} | 0 test/test.sh | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) rename test/{.clang-format => format_files/clang_format_example} (100%) diff --git a/check.sh b/check.sh index bb72f15..67f703e 100755 --- a/check.sh +++ b/check.sh @@ -85,10 +85,10 @@ if [[ ! -d $CHECK_PATH ]]; then CHECK_PATH="." fi -if [[ ! -f $FORMAT_FILEPATH ]]; then +if [[ -n $FORMAT_FILEPATH ]] && [[ ! -f $FORMAT_FILEPATH ]]; then echo "Not a file in the workspace, fallback to search for .clang_format." >&2 FORMAT_FILEPATH="" -else +elif [[ -n $FORMAT_FILEPATH ]]; then # if being used, add the colon for seperating the syntax file: FORMAT_FILEPATH=":$FORMAT_FILEPATH" fi diff --git a/test/.clang-format b/test/format_files/clang_format_example similarity index 100% rename from test/.clang-format rename to test/format_files/clang_format_example diff --git a/test/test.sh b/test/test.sh index ac8b7f8..057f66c 100755 --- a/test/test.sh +++ b/test/test.sh @@ -3,13 +3,14 @@ FALLBACK_STYLE="llvm" EXCLUDE_REGEX="capital" CLANG_FORMAT_VERSION="$1" +CLANG_FORMAT_FILEPATH="$GITHUB_WORKSPACE/test/format_files/clang_format_example" ############################################################################### # Default C/C++/Protobuf/CUDA regex # ############################################################################### # should succeed -"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_pass" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" +"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_pass" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "" "$CLANG_FORMAT_FILEPATH" docker_status="$?" if [[ $docker_status != "0" ]]; then echo "files that should succeed have failed!" @@ -17,7 +18,7 @@ if [[ $docker_status != "0" ]]; then fi # should fail -"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_fail" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" +"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_fail" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "" "$CLANG_FORMAT_FILEPATH" docker_status="$?" if [[ $docker_status == "0" ]]; then echo "files that should fail have succeeded!" @@ -26,7 +27,7 @@ fi # load test on known_pass/addition.c copies -"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/load_test" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" +"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/load_test" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "" "$CLANG_FORMAT_FILEPATH" docker_status="$?" if [[ $docker_status != "0" ]]; then echo "files that should succeed have failed in the loadtest!" @@ -40,7 +41,7 @@ fi INCLUDE_REGEX='^.*\.(c|C)' # should succeed -"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_pass" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "$INCLUDE_REGEX" +"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_pass" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "$INCLUDE_REGEX" "$CLANG_FORMAT_FILEPATH" docker_status="$?" if [[ $docker_status != "0" ]]; then echo "files that should succeed have failed!" @@ -48,7 +49,7 @@ if [[ $docker_status != "0" ]]; then fi # should fail -"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_fail" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "$INCLUDE_REGEX" +"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_fail" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "$INCLUDE_REGEX" "$CLANG_FORMAT_FILEPATH" docker_status="$?" if [[ $docker_status == "0" ]]; then echo "files that should fail have succeeded!" From 2544576b40b955eddc1c6adb0090d4c462bd95f9 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Mon, 5 Aug 2024 22:03:28 -0400 Subject: [PATCH 4/4] fix ci partially --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cd4f791..a5c6e70 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -85,6 +85,7 @@ jobs: check-path: ${{ matrix.path['check'] }} clang-format-version: ${{ matrix.clang-format-version }} exclude-regex: ${{ matrix.path['exclude'] }} + format-filepath: 'test/format_files/clang_format_example' - name: Incorrect success on known-fail test if: ${{ steps.check.outcome == 'success' && matrix.path['known_fail'] }} run: exit 1