-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify check_format.sh such that it can be used also locally to test …
…code style before submitting a PR.
- Loading branch information
Showing
2 changed files
with
47 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,56 @@ | ||
#!/usr/bin/env bash | ||
# This script checks code style by using clang-format | ||
# on a git diff made between a base branch and a test branch, | ||
# which is the one we want to check. | ||
# | ||
# This script was originally inspired by: | ||
# https://github.com/root-project/root/blob/master/.travis.yml | ||
|
||
if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then | ||
# Unofficial strict bash mode | ||
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/ | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
|
||
BASE_COMMIT=$(git rev-parse $TRAVIS_BRANCH) | ||
function check_shogun_style { | ||
|
||
echo "-----" | ||
echo "Shogun Style Checker" | ||
echo "-----" | ||
echo "Running clang-format-3.8 against branch $TRAVIS_BRANCH, with hash $BASE_COMMIT" | ||
BASE_COMMIT=$(git rev-parse ${2:-}) | ||
|
||
COMMIT_FILES=$(git diff --name-only $BASE_COMMIT) | ||
RESULT_OUTPUT="$(git clang-format-3.8 --commit $BASE_COMMIT --diff --binary `which clang-format-3.8` $COMMIT_FILES)" | ||
|
||
if [ "$RESULT_OUTPUT" == "no modified files to format" ] \ | ||
|| [ "$RESULT_OUTPUT" == "clang-format-3.8 did not modify any files" ] \ | ||
|| [ "$RESULT_OUTPUT" == "clang-format did not modify any files" ]; then | ||
echo "clang-format-3.8 passed. \o/" | ||
echo "-----" | ||
exit 0 | ||
else | ||
echo "-----" | ||
echo "clang-format failed." | ||
echo "To reproduce it locally please run: " | ||
echo -e "\t1) git checkout $TRAVIS_PULL_REQUEST_BRANCH" | ||
echo -e "\t2) git clang-format-3.8 --commit $BASE_COMMIT --diff --binary $(which clang-format-3.8)" | ||
echo "To fix the errors automatically please run: " | ||
echo -e "\t1) git checkout $TRAVIS_PULL_REQUEST_BRANCH" | ||
echo -e "\t2) git clang-format-3.8 --commit $BASE_COMMIT --binary $(which clang-format-3.8)" | ||
echo "Shogun Style Checker" | ||
echo "-----" | ||
echo "Style errors found:" | ||
echo "$RESULT_OUTPUT" | ||
exit 1 | ||
fi | ||
echo "Running clang-format-3.8 against branch ${2:-}, with hash $BASE_COMMIT" | ||
|
||
COMMIT_FILES=$(git diff --name-only $BASE_COMMIT) | ||
RESULT_OUTPUT="$(git clang-format-3.8 --commit $BASE_COMMIT --diff --binary `which clang-format-3.8` $COMMIT_FILES)" | ||
|
||
if [ "$RESULT_OUTPUT" == "no modified files to format" ] \ | ||
|| [ "$RESULT_OUTPUT" == "clang-format-3.8 did not modify any files" ] \ | ||
|| [ "$RESULT_OUTPUT" == "clang-format did not modify any files" ]; then | ||
echo "clang-format-3.8 passed. \o/" | ||
echo "-----" | ||
exit 0 | ||
else | ||
echo "-----" | ||
echo "clang-format failed." | ||
echo "To reproduce it locally please run: " | ||
echo -e "\t1) git checkout ${1:-}" | ||
echo -e "\t2) git clang-format-3.8 --commit $BASE_COMMIT --diff --binary $(which clang-format-3.8)" | ||
echo "To fix the errors automatically please run: " | ||
echo -e "\t1) git checkout ${1:-}" | ||
echo -e "\t2) git clang-format-3.8 --commit $BASE_COMMIT --binary $(which clang-format-3.8)" | ||
echo "-----" | ||
echo "Style errors found:" | ||
echo "$RESULT_OUTPUT" | ||
exit 1 | ||
fi | ||
} | ||
|
||
# Check only if we have enough arguments | ||
if [[ $# -ne 2 ]]; then | ||
echo "Wrong number of parameters supplied!" | ||
echo "Usage: ./check_format.sh <test_branch> <base_branch>" | ||
exit 1 | ||
fi | ||
|
||
# Run the check | ||
check_shogun_style ${1:-} ${2:-} |