style.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/bash
  2. # Copyright 2017 Google
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. # Usage:
  13. # ./scripts/style.sh [branch-name | filenames]
  14. #
  15. # With no arguments, formats all eligible files in the repo
  16. # Pass a branch name to format all eligible files changed since that branch
  17. # Pass a specific file or directory name to format just files found there
  18. #
  19. # Commonly
  20. # ./scripts/style.sh master
  21. if [[ $(clang-format --version) != **"version 6"** ]]; then
  22. echo "Please upgrade to clang-format version 6."
  23. echo "If it's installed via homebrew you can run: brew upgrade clang-format"
  24. exit 1
  25. fi
  26. if [[ $# -gt 0 && "$1" = "test-only" ]]; then
  27. test_only=true
  28. options="-output-replacements-xml"
  29. else
  30. test_only=false
  31. options="-i"
  32. fi
  33. (
  34. if [[ "$test_only" = false && $# -gt 0 ]]; then
  35. if git rev-parse "$1" -- >& /dev/null; then
  36. # Argument was a branch name show files changed since that branch
  37. git diff --name-only --relative "$1"
  38. else
  39. # Otherwise assume the passed things are files or directories
  40. find "$@" -type f
  41. fi
  42. else
  43. # Do everything by default
  44. find . -type f
  45. fi
  46. ) | sed -E -n '
  47. # Build outputs
  48. \%/Pods/% d
  49. \%^./build/% d
  50. # Sources controlled outside this tree
  51. \%/third_party/% d
  52. \%/Firestore/Port/% d
  53. # Sources within the tree that are not subject to formatting
  54. \%^./(Example|Firebase)/(Auth|AuthSamples|Database|Messaging)/% d
  55. # Checked-in generated code
  56. \%\.pb(objc|rpc)\.% d
  57. # Format C-ish sources only
  58. \%\.(h|m|mm|cc)$% p
  59. ' | xargs clang-format -style=file $options | grep "<replacement " > /dev/null
  60. if [[ "$test_only" = true && $? -ne 1 ]]; then
  61. echo "Proposed commit is not style compliant. Run scripts/style.sh and git add the result."
  62. exit 1
  63. fi