style.sh 5.0 KB

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