style.sh 4.7 KB

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