From b7cfc56b59103d6d0f8b7f9087d44bfc5cb3673a Mon Sep 17 00:00:00 2001 From: Webster Sheets Date: Mon, 23 Mar 2020 23:28:39 -0400 Subject: [PATCH] Move clang-format to Github Actions Refactor clang-format, add Github Action Remove clang-format step from travis --- .github/workflows/clang-format.yml | 40 +++++++++++++++++++++++++++ .travis.yml | 30 --------------------- autoformat | 12 +++++---- scripts/clang-format.sh | 43 +++++++++++++++++++----------- 4 files changed, 74 insertions(+), 51 deletions(-) create mode 100644 .github/workflows/clang-format.yml diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 000000000..49aed4516 --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -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 diff --git a/.travis.yml b/.travis.yml index 7fb1a7a93..2afe4cef1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/autoformat b/autoformat index 0f0564d43..a310ff03e 100755 --- a/autoformat +++ b/autoformat @@ -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 diff --git a/scripts/clang-format.sh b/scripts/clang-format.sh index 9c5dfdd23..8cb8c40a9 100755 --- a/scripts/clang-format.sh +++ b/scripts/clang-format.sh @@ -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