clang-format.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/sh
  2. CLANG_FORMAT=clang-format-3.9
  3. if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
  4. # Check the whole commit range against $TRAVIS_BRANCH, the base merge branch
  5. # We could use $TRAVIS_COMMIT_RANGE but it doesn't play well with force pushes
  6. RANGE="$(git rev-parse $TRAVIS_BRANCH) HEAD"
  7. else
  8. # Test only the last commit
  9. RANGE=HEAD
  10. fi
  11. FILES=$(git diff-tree --no-commit-id --name-only -r $RANGE | grep -v thirdparty/ | grep -E "\.(c|h|cpp|hpp|cc|hh|cxx|m|mm|inc)$")
  12. echo "Checking files:\n$FILES"
  13. # create a random filename to store our generated patch
  14. prefix="static-check-clang-format"
  15. suffix="$(date +%s)"
  16. patch="/tmp/$prefix-$suffix.patch"
  17. for file in $FILES; do
  18. "$CLANG_FORMAT" -style=file "$file" | \
  19. diff -u "$file" - | \
  20. sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
  21. done
  22. # if no patch has been generated all is ok, clean up the file stub and exit
  23. if [ ! -s "$patch" ] ; then
  24. printf "Files in this commit comply with the clang-format rules.\n"
  25. rm -f "$patch"
  26. exit 0
  27. fi
  28. # a patch has been created, notify the user and exit
  29. printf "\n*** The following differences were found between the code to commit "
  30. printf "and the clang-format rules:\n\n"
  31. cat "$patch"
  32. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  33. exit 1