style.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. 6 | 7)
  37. # Allow an older clang-format to accommodate Travis version skew.
  38. ;;
  39. google3-trunk)
  40. echo "Please use a publicly released clang-format; a recent LLVM release"
  41. echo "from http://releases.llvm.org/download.html will work."
  42. echo "Prepend to PATH when running style.sh."
  43. exit 1
  44. ;;
  45. *)
  46. echo "Please upgrade to clang-format version 7."
  47. echo "If it's installed via homebrew you can run: brew upgrade clang-format"
  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"* ]]; 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. files=$(
  94. (
  95. if [[ $# -gt 0 ]]; then
  96. if git rev-parse "$1" -- >& /dev/null; then
  97. # Argument was a branch name show files changed since that branch
  98. git diff --name-only --relative --diff-filter=ACMR "$1"
  99. else
  100. # Otherwise assume the passed things are files or directories
  101. find "$@" -type f
  102. fi
  103. else
  104. # Do everything by default
  105. find . -type f
  106. fi
  107. ) | sed -E -n '
  108. # find . includes a leading "./" that git does not include
  109. s%^./%%
  110. # Build outputs
  111. \%/Pods/% d
  112. \%^build/% d
  113. \%^Debug/% d
  114. \%^Release/% d
  115. # Sources controlled outside this tree
  116. \%/third_party/% d
  117. \%/Firestore/Port/% d
  118. # Generated source
  119. \%/Firestore/core/src/firebase/firestore/util/config.h% d
  120. # Sources pulled in by travis bundler
  121. \%/vendor/bundle/% d
  122. # Sources within the tree that are not subject to formatting
  123. \%^(Example|Firebase)/(Auth|AuthSamples|Database|Messaging)/% d
  124. # Checked-in generated code
  125. \%\.pb(objc|rpc)\.% d
  126. \%\.pb\.% d
  127. \%\.nanopb\.% d
  128. # Format C-ish sources only
  129. \%\.(h|m|mm|cc|swift)$% p
  130. '
  131. )
  132. needs_formatting=false
  133. for f in $files; do
  134. if [[ "${f: -6}" == '.swift' ]]; then
  135. if [[ "$system" == 'Darwin' ]]; then
  136. swiftformat "${swift_options[@]}" "$f" 2> /dev/null | grep 'would have updated' > /dev/null
  137. else
  138. false
  139. fi
  140. else
  141. clang-format "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
  142. fi
  143. if [[ "$test_only" == true && $? -ne 1 ]]; then
  144. echo "$f needs formatting."
  145. needs_formatting=true
  146. fi
  147. done
  148. if [[ "$needs_formatting" == true ]]; then
  149. echo "Proposed commit is not style compliant."
  150. echo "Run scripts/style.sh and git add the result."
  151. exit 1
  152. fi