Skip to content

Commit

Permalink
Modify check_format.sh such that it can be used also locally to test …
Browse files Browse the repository at this point in the history
…code style before submitting a PR.
  • Loading branch information
geektoni committed May 15, 2017
1 parent f101fd9 commit 15eaf7e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ before_script:
script:
- |
if [ $CC == "gcc" ] && [ -z $OCTAVE_MODULAR ]; then
docker exec -t devenv /bin/sh -c "cd /opt/shogun/; ./scripts/check_format.sh"
docker exec -t devenv /bin/sh -c "cd /opt/shogun/; if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then ./scripts/check_format.sh "$TRAVIS_PULL_REQUEST_BRANCH" "$TRAVIS_BRANCH"; fi"
fi
- docker exec -t devenv /bin/sh -c "cd /opt/shogun/build; make -j2"
- docker exec -t devenv /bin/sh -c "cd /opt/shogun/build; make install"
Expand Down
73 changes: 46 additions & 27 deletions scripts/check_format.sh
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:-}

0 comments on commit 15eaf7e

Please sign in to comment.