style.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. system=$(uname -s)
  22. version=$(clang-format --version)
  23. version="${version/*version /}"
  24. version="${version/.*/}"
  25. if [[ "$version" != 6 && "$version" != 7 ]]; then
  26. # Allow an older clang-format to accommodate Travis version skew.
  27. echo "Please upgrade to clang-format version 7."
  28. echo "If it's installed via homebrew you can run: brew upgrade clang-format"
  29. exit 1
  30. fi
  31. if [[ "$system" == "Darwin" ]]; then
  32. version=$(swiftformat --version)
  33. version="${version/*version /}"
  34. # Allow an older swiftformat because travis isn't running High Sierra yet
  35. # and the formula hasn't been updated in a while on Sierra :-/.
  36. if [[ "$version" != "0.32.0" && "$version" != "0.33"* ]]; then
  37. echo "Please upgrade to swiftformat 0.33.3"
  38. echo "If it's installed via homebrew you can run: brew upgrade swiftformat"
  39. exit 1
  40. fi
  41. fi
  42. # Joins the given arguments with the separator given as the first argument.
  43. function join() {
  44. local IFS="$1"
  45. shift
  46. echo "$*"
  47. }
  48. clang_options=(-style=file)
  49. # Rules to disable in swiftformat:
  50. swift_disable=(
  51. # sortedImports is broken, sorting into the middle of the copyright notice.
  52. sortedImports
  53. # Too many of our swift files have simplistic examples. While technically
  54. # it's correct to remove the unused argument labels, it makes our examples
  55. # look wrong.
  56. unusedArguments
  57. )
  58. swift_options=(
  59. # Mimic Objective-C style.
  60. --indent 2
  61. --disable $(join , "${swift_disable[@]}")
  62. )
  63. if [[ $# -gt 0 && "$1" == "test-only" ]]; then
  64. test_only=true
  65. clang_options+=(-output-replacements-xml)
  66. swift_options+=(--dryrun)
  67. shift
  68. else
  69. test_only=false
  70. clang_options+=(-i)
  71. fi
  72. files=$(
  73. (
  74. if [[ $# -gt 0 ]]; then
  75. if git rev-parse "$1" -- >& /dev/null; then
  76. # Argument was a branch name show files changed since that branch
  77. git diff --name-only --relative --diff-filter=ACMR "$1"
  78. else
  79. # Otherwise assume the passed things are files or directories
  80. find "$@" -type f
  81. fi
  82. else
  83. # Do everything by default
  84. find . -type f
  85. fi
  86. ) | sed -E -n '
  87. # find . includes a leading "./" that git does not include
  88. s%^./%%
  89. # Build outputs
  90. \%/Pods/% d
  91. \%^build/% d
  92. \%^Debug/% d
  93. \%^Release/% d
  94. # Sources controlled outside this tree
  95. \%/third_party/% d
  96. \%/Firestore/Port/% d
  97. # Generated source
  98. \%/Firestore/core/src/firebase/firestore/util/config.h% d
  99. # Sources pulled in by travis bundler
  100. \%/vendor/bundle/% d
  101. # Sources within the tree that are not subject to formatting
  102. \%^(Example|Firebase)/(Auth|AuthSamples|Database|Messaging)/% d
  103. # Checked-in generated code
  104. \%\.pb(objc|rpc)\.% d
  105. \%\.pb\.% d
  106. \%\.nanopb\.% d
  107. # Format C-ish sources only
  108. \%\.(h|m|mm|cc|swift)$% p
  109. '
  110. )
  111. needs_formatting=false
  112. for f in $files; do
  113. if [[ "${f: -6}" == '.swift' ]]; then
  114. if [[ "$system" == 'Darwin' ]]; then
  115. swiftformat "${swift_options[@]}" "$f" 2> /dev/null | grep 'would have updated' > /dev/null
  116. else
  117. false
  118. fi
  119. else
  120. clang-format "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
  121. fi
  122. if [[ "$test_only" == true && $? -ne 1 ]]; then
  123. echo "$f needs formatting."
  124. needs_formatting=true
  125. fi
  126. done
  127. if [[ "$needs_formatting" == true ]]; then
  128. echo "Proposed commit is not style compliant."
  129. echo "Run scripts/style.sh and git add the result."
  130. exit 1
  131. fi