Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds format-filepath to customize the filepath of .clang-format #211

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 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.

Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
13 changes: 11 additions & 2 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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
formatted="$(docker run \
--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}"))"
Expand All @@ -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
Expand All @@ -84,6 +85,14 @@ if [[ ! -d $CHECK_PATH ]]; then
CHECK_PATH="."
fi

if [[ -n $FORMAT_FILEPATH ]] && [[ ! -f $FORMAT_FILEPATH ]]; then
echo "Not a file in the workspace, fallback to search for .clang_format." >&2
FORMAT_FILEPATH=""
elif [[ -n $FORMAT_FILEPATH ]]; then
# if being used, add the colon for seperating the syntax file:<file_name>
FORMAT_FILEPATH=":$FORMAT_FILEPATH"
fi

# initialize exit code
exit_code=0

Expand Down
File renamed without changes.
11 changes: 6 additions & 5 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
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!"
exit 1
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!"
Expand All @@ -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!"
Expand All @@ -40,15 +41,15 @@ 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!"
exit 1
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!"
Expand Down
Loading