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

Better CI Format Check #466

Merged
merged 2 commits into from
Aug 7, 2023
Merged
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
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,5 @@ jobs:

- name: Check formatting
run: |
./format_code.sh --check
./format_code.sh
git diff --exit-code
22 changes: 12 additions & 10 deletions format_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ set -o pipefail
IFS=$'\n\t'

# Bash script to automatically format LCM source code (currently C and C++).
# Requires `clang-format` utilily, which is part of the LLVM project. More
# Requires `clang-format-12` utility, which is part of the LLVM project. More
# information can be found here: https://clang.llvm.org/docs/ClangFormat.html
#
# To install `clang-format` on Ubuntu do:
# To install `clang-format-12` on Ubuntu do:
#
# $ sudo apt install clang-format
# $ sudo apt install clang-format-12
#
# This script does not format Java, Python, or Lua source code. At the moment,
# it only formats C and C++ sources, i.e., *.h, *.c, *.hpp, and *.cpp. It only
Expand All @@ -33,6 +33,8 @@ LCM_FORMAT_DIRS=(
"${LCM_ROOT}/test:/gtest/"
)

CLANG_FORMAT="clang-format-12"

# Function: show_usage
#
# Shows usage of this script.
Expand Down Expand Up @@ -69,7 +71,7 @@ function format_c_cpp_dir_r {
done
find "$1" -regex '.*\.\(c\|h\)\(pp\)?' \
| eval "grep -E -v" "${exclude_pattern_opts}" \
| xargs clang-format -i
| xargs $CLANG_FORMAT -i
}

# Function: check_format_c_cpp_dir_r DIR [EXCUDE_PATTERN...]
Expand All @@ -94,7 +96,7 @@ function check_format_c_cpp_dir_r {
done
find "$1" -regex '.*\.\(c\|h\)\(pp\)?' \
| eval "grep -E -v" "${exclude_pattern_opts}" \
| xargs clang-format --dry-run --Werror --ferror-limit=1 &>/dev/null
| xargs $CLANG_FORMAT --dry-run --Werror --ferror-limit=1 &>/dev/null
}

# Default option values
Expand All @@ -119,11 +121,11 @@ while getopts "ch" arg &>/dev/null; do
esac
done

# Check for clang-format
if ! command -v clang-format &>/dev/null; then
echo "ERROR: Can not find clang-format!" 1>&2
echo "Please add clang-format to your PATH" 1>&2
echo "On Ubuntu, install it with: sudo apt install clang-format" 1>&2
# Check for $CLANG_FORMAT
if ! command -v $CLANG_FORMAT &>/dev/null; then
echo "ERROR: Can not find $CLANG_FORMAT!" 1>&2
echo "Please add $CLANG_FORMAT to your PATH" 1>&2
echo "On Ubuntu, install it with: sudo apt install $CLANG_FORMAT" 1>&2
exit 1
fi

Expand Down