style.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. set -euo pipefail
  22. (
  23. if [[ $# -gt 0 ]]; then
  24. if git rev-parse "$1" -- >& /dev/null; then
  25. # Argument was a branch name show files changed since that branch
  26. git diff --name-only --relative
  27. else
  28. # Otherwise assume the passed things are files or directories
  29. find "$@" -type f
  30. fi
  31. else
  32. # Do everything by default
  33. find . -type f
  34. fi
  35. ) | sed -E -n '
  36. # Build outputs
  37. \%/Pods/% d
  38. \%^./build/% d
  39. # Sources controlled outside this tree
  40. \%/third_party/% d
  41. \%/Firestore/Port/% d
  42. # Sources within the tree that are not subject to formatting
  43. \%^./(Example|Firebase)/(Auth|AuthSamples|Database|Messaging)/% d
  44. # Checked-in generated code
  45. \%\.pb(objc|rpc)\.% d
  46. # Format C-ish sources only
  47. \%\.(h|m|mm|cc)$% p
  48. ' | xargs clang-format -style=file -i