style.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. shift
  30. else
  31. test_only=false
  32. options="-i"
  33. fi
  34. files=$(
  35. (
  36. if [[ $# -gt 0 ]]; then
  37. if git rev-parse "$1" -- >& /dev/null; then
  38. # Argument was a branch name show files changed since that branch
  39. git diff --name-only --relative --diff-filter=ACMR "$1"
  40. else
  41. # Otherwise assume the passed things are files or directories
  42. find "$@" -type f
  43. fi
  44. else
  45. # Do everything by default
  46. find . -type f
  47. fi
  48. ) | sed -E -n '
  49. # Build outputs
  50. \%/Pods/% d
  51. \%^./build/% d
  52. # Sources controlled outside this tree
  53. \%/third_party/% d
  54. \%/Firestore/Port/% d
  55. # Generated source
  56. \%/Firestore/core/src/firebase/firestore/util/config.h% d
  57. # Sources pulled in by travis bundler
  58. \%/vendor/bundle/% d
  59. # Sources within the tree that are not subject to formatting
  60. \%^./(Example|Firebase)/(Auth|AuthSamples|Database|Messaging)/% d
  61. # Checked-in generated code
  62. \%\.pb(objc|rpc)\.% d
  63. \%\.pb\.% d
  64. # Format C-ish sources only
  65. \%\.(h|m|mm|cc)$% p
  66. '
  67. )
  68. needs_formatting=false
  69. for f in $files; do
  70. clang-format -style=file $options $f | grep "<replacement " > /dev/null
  71. if [[ "$test_only" = true && $? -ne 1 ]]; then
  72. echo "$f needs formatting."
  73. needs_formatting=true
  74. fi
  75. done
  76. if [[ "$needs_formatting" = true ]]; then
  77. echo "Proposed commit is not style compliant."
  78. echo "Run scripts/style.sh and git add the result."
  79. exit 1
  80. fi