trailing-newline.sh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env bash
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # trailing-newline.sh — checks file for missing trailing newline.
  12. set -euo pipefail
  13. # shellcheck source=misc/shlib/shlib.bash
  14. . "$(dirname "$0")/../shlib/shlib.bash"
  15. if [[ $# -lt 1 ]]; then
  16. echo "usage: $0 <file>" >&2
  17. exit 1
  18. fi
  19. errors=0
  20. for file in "$@"; do
  21. if [[ ! -f "$file" ]]; then
  22. echo "lint: $(red error:) trailing-newline: internal error: $file is not a file" >&2
  23. exit 1
  24. fi
  25. last_byte=$(tail -c1 "$file")
  26. if [[ "$last_byte" != $'\n' && "$last_byte" != "" ]] &> /dev/null; then
  27. echo "lint: $(red error:) trailing-newline: $file is missing a trailing newline" >&2
  28. ((errors++))
  29. fi
  30. done
  31. if [[ $errors -gt 0 ]]; then
  32. exit 1
  33. fi