lint-commit-message.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bash
  2. #
  3. # DO NOT EDIT THIS FILE
  4. #
  5. # It is automatically copied from https://github.com/pion/.goassets repository.
  6. #
  7. # If you want to update the shared CI config, send a PR to
  8. # https://github.com/pion/.goassets instead of this repository.
  9. #
  10. set -e
  11. display_commit_message_error() {
  12. cat << EndOfMessage
  13. $1
  14. -------------------------------------------------
  15. The preceding commit message is invalid
  16. it failed '$2' of the following checks
  17. * Separate subject from body with a blank line
  18. * Limit the subject line to 50 characters
  19. * Capitalize the subject line
  20. * Do not end the subject line with a period
  21. * Wrap the body at 72 characters
  22. EndOfMessage
  23. exit 1
  24. }
  25. lint_commit_message() {
  26. if [[ "$(echo "$1" | awk 'NR == 2 {print $1;}' | wc -c)" -ne 1 ]]; then
  27. display_commit_message_error "$1" 'Separate subject from body with a blank line'
  28. fi
  29. if [[ "$(echo "$1" | head -n1 | awk '{print length}')" -gt 50 ]]; then
  30. display_commit_message_error "$1" 'Limit the subject line to 50 characters'
  31. fi
  32. if [[ ! $1 =~ ^[A-Z] ]]; then
  33. display_commit_message_error "$1" 'Capitalize the subject line'
  34. fi
  35. if [[ "$(echo "$1" | awk 'NR == 1 {print substr($0,length($0),1)}')" == "." ]]; then
  36. display_commit_message_error "$1" 'Do not end the subject line with a period'
  37. fi
  38. if [[ "$(echo "$1" | awk '{print length}' | sort -nr | head -1)" -gt 72 ]]; then
  39. display_commit_message_error "$1" 'Wrap the body at 72 characters'
  40. fi
  41. }
  42. if [ "$#" -eq 1 ]; then
  43. if [ ! -f "$1" ]; then
  44. echo "$0 was passed one argument, but was not a valid file"
  45. exit 1
  46. fi
  47. lint_commit_message "$(sed -n '/# Please enter the commit message for your changes. Lines starting/q;p' "$1")"
  48. else
  49. for commit in $(git rev-list --no-merges origin/master..); do
  50. lint_commit_message "$(git log --format="%B" -n 1 $commit)"
  51. done
  52. fi