Move clang-format to Github Actions

Refactor clang-format, add Github Action
Remove clang-format step from travis
master
Webster Sheets 2020-03-23 23:28:39 -04:00
parent c2f3305730
commit b7cfc56b59
4 changed files with 74 additions and 51 deletions

40
.github/workflows/clang-format.yml vendored Normal file
View File

@ -0,0 +1,40 @@
# Check all pull requests against the clang-format rules.
name: Clang Format
# Controls when the action will run.
# Run clang-format checks on all pull-requests
on:
pull_request:
paths:
- 'src/**.cpp'
- 'src/**.h'
paths-ignore:
- 'contrib/'
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
clang-format-check:
timeout-minutes: 5
# Ubuntu 18.04 at the time of writing includes clang-format-9 as default
runs-on: [ubuntu-18.04]
# We want to check all changed files between the pull-request base and the head of the pull request
env:
FORMAT_BASE: ${{ github.base_ref }}
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checkout the repository as $GITHUB_WORKSPACE
- uses: actions/checkout@v2
# Make sure to include the base we want to merge into
- run: |
git fetch --no-tags --depth=1 origin ${{ github.base_ref }}
# Run the clang-format command (in $GITHUB_WORKSPACE, which is where the repo is checked out to)
- name: Run clang-format
run: ./scripts/clang-format

View File

@ -31,22 +31,6 @@ addons:
matrix:
include:
# OSX is faster to boot and execute the checks, but has more contention for VM instances.
# Not waiting fifteen minutes for an instance to come available is faster.
- name: Static Checks
os: linux
env: STATIC_CHECKS=yes
deploy:
provider: null
before_install: skip
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- clang-format
script: bash ./scripts/clang-format.sh
- name: Build GCC
os: linux
@ -74,17 +58,3 @@ deploy:
on:
repo: pioneerspacesim/pioneer
tags: true
condition: $STATIC_CHECKS != yes
notifications:
irc:
channels:
- "ircs://chat.freenode.net:6669/#pioneer"
on_success: always
on_failure: always
template:
- "%{repository_slug}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}"
- "Change view : %{compare_url}"
- "Build details : %{build_url}"
nick: travis-jameson
password: flyordie

View File

@ -1,20 +1,22 @@
#!/bin/sh
#!/bin/bash
./scripts/clang-format.sh "$@"
./scripts/clang-format.sh
# Run clang-format, storing the output in MSG
# Git runs hooks in the base directory, so we don't need to do any fancy dirname stuff.
# $? will be non-zero if clang-format detected formatting issues.
if [ "$?" != 0 ]; then
format_ok=$?
if ((format_ok == 1)); then
# We've had a major malfunction!
read -p "Do you want to automatically apply these changes (y/N)?" apply
case $apply in
y|Y)
# Get the patch info from the message and apply it to the staged changes.
PATCH_MODE=1 ./scripts/clang-format.sh | git apply --index -
PATCH_MODE=1 ./scripts/clang-format.sh "$@" | git apply --index -
;;
*) exit 1;;
esac
else
# All good, carry on.
exit 0
exit $format_ok
fi

View File

@ -1,32 +1,43 @@
#!/bin/bash
CLANG_FORMAT=`which "clang-format"`
if [ ! $PATCH_MODE ]; then
CLANG_FORMAT=$(which clang-format)
# Github Actions compatibility
if [[ $GITHUB_WORKSPACE ]]; then cd $GIT_WORKSPACE; fi
# if PATCH_MODE is set to 0, then set it to the empty string so $PATCH_MODE continues to work
if ((PATCH_MODE == 0)); then PATCH_MODE=""; fi
if [[ $PATCH_MODE ]]; then
echo "clang-format = $CLANG_FORMAT"
fi
if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
if [[ $TRAVIS_PULL_REQUEST && $TRAVIS_PULL_REQUEST != "false" ]]; then
# Check the whole commit range against $TRAVIS_BRANCH, the base merge branch
# We could use $TRAVIS_COMMIT_RANGE but it doesn't play well with force pushes
RANGE="$(git rev-parse $TRAVIS_BRANCH) HEAD"
else
# Test only the last commit
RANGE=HEAD
RANGE_BASE=$(git rev-parse $TRAVIS_BRANCH)
elif [[ $FORMAT_BASE ]]; then
# Test all commits between the $FORMAT_BASE commit and HEAD
RANGE_BASE=$(git rev-parse $FORMAT_BASE)
fi
if [ -n "$TRAVIS" ]; then
if [[ $RANGE_BASE ]]; then
# if we're comparing against a certain commit, diff the tree
GIT_DIFF_TOOL="git diff-tree"
else
# otherwise, only diff the HEAD against the index
GIT_DIFF_TOOL="git diff-index --cached"
fi
FILES="$@"
# Allow manually specifiying the files.
if [ -z "$FILES" ]; then
FILES=$($GIT_DIFF_TOOL --no-commit-id --name-only --diff-filter=d -r $RANGE | grep -v contrib/ | grep -E "\.(c|h|cpp|hpp|cc|hh|cxx|m|mm|inc)$")
FILES="$@"
# Otherwise, get a list of all files changed between our RANGE_BASE and the current HEAD
if [[ -z $FILES ]]; then
FILES=$($GIT_DIFF_TOOL --no-commit-id --name-only --diff-filter=d $RANGE_BASE HEAD | grep -v contrib/ | grep -E "\.(c|h|cpp|hpp|cc|hh|cxx|m|mm|inc)$")
fi
if [ ! $PATCH_MODE ]; then
if [[ -z $PATCH_MODE ]]; then
echo -e "Checking files:"
for file in $FILES; do
echo -e "\t$file"
@ -37,7 +48,7 @@ prefix="static-check-clang-format"
suffix="$(date +%s)"
patch="/tmp/$prefix-$suffix.patch"
if [ -z "$TRAVIS" ] && [ ! $PATCH_MODE ]; then
if [[ -z $PATCH_MODE ]]; then
DIFF_COLOR="--color=always"
else
DIFF_COLOR=""
@ -53,14 +64,14 @@ for file in $FILES; do
done
# if no patch has been generated all is ok, clean up the file stub and exit
if [ ! -s "$patch" ] ; then
if [[ ! -s $patch ]] ; then
printf "Files in this commit comply with the clang-format rules.\n"
rm -f "$patch"
exit 0
fi
if [ $PATCH_MODE ]; then
# Print the filename of the generated patch.
if [[ $PATCH_MODE ]]; then
# Print the generated patch to stdout.
cat "$patch"
rm -f "$patch"
exit 1