clang-format.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/sh
  2. CLANG_FORMAT=clang-format-6.0
  3. if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
  4. # Travis only clones the PR branch and uses its HEAD commit as detached HEAD,
  5. # so it's problematic when we want an exact commit range for format checks.
  6. # We fetch upstream to ensure that we have the proper references to resolve.
  7. # Ideally we would use $TRAVIS_COMMIT_RANGE but it doesn't play well with PR
  8. # updates, as it only includes changes since the previous state of the PR.
  9. git remote add upstream https://github.com/godotengine/godot \
  10. --no-tags -f -t $TRAVIS_BRANCH
  11. RANGE="upstream/$TRAVIS_BRANCH HEAD"
  12. else
  13. # Test only the last commit, since $TRAVIS_COMMIT_RANGE wouldn't support
  14. # force pushes.
  15. RANGE=HEAD
  16. fi
  17. 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|java|glsl)$")
  18. echo "Checking files:\n$FILES"
  19. # create a random filename to store our generated patch
  20. prefix="static-check-clang-format"
  21. suffix="$(date +%s)"
  22. patch="/tmp/$prefix-$suffix.patch"
  23. for file in $FILES; do
  24. "$CLANG_FORMAT" -style=file "$file" | \
  25. diff -u "$file" - | \
  26. sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
  27. done
  28. # if no patch has been generated all is ok, clean up the file stub and exit
  29. if [ ! -s "$patch" ] ; then
  30. printf "Files in this commit comply with the clang-format rules.\n"
  31. rm -f "$patch"
  32. exit 0
  33. fi
  34. # a patch has been created, notify the user and exit
  35. printf "\n*** The following differences were found between the code to commit "
  36. printf "and the clang-format rules:\n\n"
  37. cat "$patch"
  38. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  39. exit 1