forked from plfiorini/clang-format-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·71 lines (59 loc) · 2.37 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
###############################################################################
# entrypoint.sh #
###############################################################################
# USAGE: ./entrypoint.sh [<path>] [<fallback style>]
# Checks all C/C++ files (.h, .H, .hpp, .hh, .h++, .hxx and .c, .C, .cpp, .cc,
# .c++, .cxx) in the provided GitHub repository path (arg1) for conforming to
# clang-format. If no path is provided or provided path is not a directory, all
# C/C++ files are checked. If any C files are incorrectly formatted, the script
# lists them and exits with 1.
#
# Define your own formatting rules in a .clang-format file at your repository
# root. Otherwise, the provided style guide (arg2) is used as a fallback.
# format_diff function
# Accepts a filepath argument. The filepath passed to this function must point
# to a C/C++ file.
format_diff() {
local filepath="$1"
# Invoke clang-format with dry run and formatting error output
local_format="$(/usr/bin/clang-format-"$CLANG_FORMAT_VERSION" -n --Werror --style=file --fallback-style="$FALLBACK_STYLE" "${filepath}")"
local format_status="$?"
if [[ "${format_status}" -ne 0 ]]; then
echo "Failed on file: $filepath"
echo "$local_format" >&2
exit_code=1
return "${format_status}"
fi
return 0
}
CHECK_PATH="$1"
FALLBACK_STYLE="$2"
EXCLUDE_REGEX="$3"
# Set the regex to an empty string regex if nothing was provided
if [ -z "$EXCLUDE_REGEX" ]; then
EXCLUDE_REGEX="^$"
fi
# Install clang-format
echo "Installing clang-format-$CLANG_FORMAT_VERSION"
apt-get update >/dev/null && apt-get install -y --no-install-recommends clang-format-"$CLANG_FORMAT_VERSION" >/dev/null
cd "$GITHUB_WORKSPACE" || exit 2
if [[ ! -d "$CHECK_PATH" ]]; then
echo "Not a directory in the workspace, fallback to all files."
CHECK_PATH="."
fi
# initialize exit code
exit_code=0
# All files improperly formatted will be printed to the output.
# find all C/C++ files:
# h, H, hpp, hh, h++, hxx
# c, C, cpp, cc, c++, cxx
c_files=$(find "$CHECK_PATH" -regextype posix-egrep -regex '^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde))$')
# check formatting in each C file
for file in $c_files; do
# Only check formatting if the path doesn't match the regex
if ! [[ "${file}" =~ $EXCLUDE_REGEX ]]; then
format_diff "${file}"
fi
done
exit "$exit_code"