|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright ARDUINO SRL |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +# parse arguments |
| 7 | +while getopts "p:f:o:r:e:i:" opt; do |
| 8 | + case $opt in |
| 9 | + p) CHECK_PATH="$OPTARG" ;; |
| 10 | + f) CHECK_FILES_FROM="$OPTARG" ;; |
| 11 | + e) EXCLUDE_REGEX="$OPTARG" ;; |
| 12 | + i) INCLUDE_REGEX="$OPTARG" ;; |
| 13 | + *) echo "Invalid option: -$OPTARG" >&2 ; exit 1 ;; |
| 14 | + esac |
| 15 | +done |
| 16 | + |
| 17 | +if [[ -z "$CHECK_FILES_FROM" ]] && [[ -z "$CHECK_PATH" ]]; then |
| 18 | + echo "No source input provided. Fallback to all files." >&2 |
| 19 | + CHECK_PATH="." |
| 20 | +elif [[ -f "$CHECK_FILES_FROM" ]]; then |
| 21 | + echo "Checking files listed in 'check-files-from'." >&2 |
| 22 | +elif [[ ! -z "$CHECK_PATH" ]] && [[ -d "$CHECK_PATH" ]]; then |
| 23 | + echo "Checking directory specified by 'check-path'." >&2 |
| 24 | +else |
| 25 | + echo "::warning::Invalid inputs provided. Fallback to all files." >&2 |
| 26 | + CHECK_FILES_FROM="" |
| 27 | + CHECK_PATH="." |
| 28 | +fi |
| 29 | + |
| 30 | +# Set the regex to an empty string regex if nothing was provided |
| 31 | +if [[ -z $EXCLUDE_REGEX ]]; then |
| 32 | + EXCLUDE_REGEX="^$" |
| 33 | +fi |
| 34 | + |
| 35 | +# Set the filetype regex if nothing was provided. |
| 36 | +# Find all C/C++/Protobuf/CUDA files: |
| 37 | +# h, H, hpp, hh, h++, hxx |
| 38 | +# c, C, cpp, cc, c++, cxx |
| 39 | +# ino, pde |
| 40 | +# proto |
| 41 | +# cu |
| 42 | +if [[ -z $INCLUDE_REGEX ]]; then |
| 43 | + INCLUDE_REGEX='^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu))$' |
| 44 | +fi |
| 45 | + |
| 46 | +cd "$GITHUB_WORKSPACE" || exit 2 |
| 47 | + |
| 48 | +exit_code=0 |
| 49 | +if [[ -f "$CHECK_FILES_FROM" ]]; then |
| 50 | + # Use the provided list of files to check. |
| 51 | + src_files=$(grep -E "$INCLUDE_REGEX" "$CHECK_FILES_FROM") |
| 52 | +else |
| 53 | + # Find all source files in the provided path, excluding .git directories. |
| 54 | + src_files=$(find "$CHECK_PATH" -name .git -prune -o -regextype posix-egrep -regex "$INCLUDE_REGEX" -print) |
| 55 | +fi |
| 56 | + |
| 57 | +HEAD_SHA=$(git rev-parse $GITHUB_HEAD_REF) |
| 58 | + |
| 59 | +intro_out=false |
| 60 | +IFS=$'\n' # Loop below should separate on new lines, not spaces. |
| 61 | +for file in $src_files; do |
| 62 | + # Only check formatting if the path doesn't match the regex |
| 63 | + if ! [[ ${file} =~ $EXCLUDE_REGEX ]]; then |
| 64 | + clang-format -i -style=file "${file}" 2>&1 |
| 65 | + changes=$(git diff -U0 ${file}) |
| 66 | + [ -z "$changes" ] && continue |
| 67 | + |
| 68 | + exit_code=1 |
| 69 | + |
| 70 | + git diff --color=always ${file} |
| 71 | + |
| 72 | + echo "$changes" | while IFS= read -r line ; do |
| 73 | + # match file and line numbers in the diff output and provide Github Actions annotations |
| 74 | + if [[ $line =~ ^@@\ -([0-9]+),?([0-9]*)\ \+([0-9]+),?([0-9]*)\ @@ ]]; then |
| 75 | + # Extract the line numbers from the diff output |
| 76 | + git_start_line=${BASH_REMATCH[1]} |
| 77 | + git_line_count=${BASH_REMATCH[2]:-1} |
| 78 | + fix_start_line=${BASH_REMATCH[3]} |
| 79 | + fix_line_count=${BASH_REMATCH[4]:-1} |
| 80 | + |
| 81 | + git_end_line=$((git_start_line + git_line_count - 1)) |
| 82 | + |
| 83 | + echo "::warning file=${file},line=${git_start_line},endLine=${git_end_line}::Fix code formatting" |
| 84 | + fi |
| 85 | + done |
| 86 | + fi |
| 87 | +done |
| 88 | + |
| 89 | +exit $exit_code |
0 commit comments