From ff0a9a8d96ff4d4e533718aab8b18a631c1303dd Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 12 Sep 2017 15:42:37 -0700 Subject: [PATCH] Enforce clang-format for c-like sources on Travis (#4089) * Enforce clang-format for c-like sources on Travis * Edit CONTRIBUTING,md * review feedback: * ++ * ++ * ++ --- .travis.yml | 1 + CONTRIBUTING.md | 12 +++++++----- travis/format.sh | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100755 travis/format.sh diff --git a/.travis.yml b/.travis.yml index 1ed00e9bf13..c72c15b6a69 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,4 @@ before_script: script: - ./travis/build.sh - ./travis/test.sh + - ./travis/format.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5466bff1c3a..d0c7030316d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -201,11 +201,13 @@ To start working on a patch: * Make sure you are in the `engine/src/flutter` directory. * `git fetch upstream` * `git checkout upstream/master -b name_of_your_branch` - * Hack away. Please peruse our - [style guides](https://flutter.io/style-guide/) and - [design principles](https://flutter.io/design-principles/) before - working on anything non-trivial. These guidelines are intended to - keep the code consistent and avoid common pitfalls. + * Hack away. + * Please peruse our [style guides](https://flutter.io/style-guide/) and + [design principles](https://flutter.io/design-principles/) before + working on anything non-trivial. These guidelines are intended to + keep the code consistent and avoid common pitfalls. + * C, C++, and Objective-C code should be formatted with `clang-format` before + submission (use `buildtools//clang/bin/clang-format --style=file -i`). * `git commit -a -m ""` * `git push origin name_of_your_branch` diff --git a/travis/format.sh b/travis/format.sh new file mode 100755 index 00000000000..0375cd21320 --- /dev/null +++ b/travis/format.sh @@ -0,0 +1,39 @@ +#!/bin/bash +set -e +echo "Checking formatting..." +cd .. + +case "$(uname -s)" in + Darwin) + OS="mac-x64" + ;; + Linux) + OS="linux-x64" + ;; + *) + echo "Unknown operating system." + exit -1 + ;; +esac + +CLANG_FORMAT="buildtools/$OS/clang/bin/clang-format" +$CLANG_FORMAT --version + +FILES="$(find flutter/ -name '*.cpp' -or -name '*.h' -or -name '*.c' -or -name '*.cc' -or -name '*.m' -or -name '*.mm')" +FAILED_CHECKS=0 + +for FILE in $FILES; do + set +e + RESULT="$(diff -u "$FILE" <($CLANG_FORMAT --style=file "$FILE"))" + set -e + if ! [ -z "$RESULT" ]; then + echo "$RESULT" + FAILED_CHECKS=$(($counter+1)) + fi +done + +if [ $FAILED_CHECKS -ne 0 ]; then + echo "Some files are formatted incorrectly. To fix, apply diffs from above." +fi + +exit $FAILED_CHECKS