lint-commit.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env bash
  2. # the file containing the commit message is passed as the first argument
  3. commit_file="$1"
  4. commit_message=$(cat "$commit_file")
  5. error() {
  6. echo -e "\033[0;31m$1:\033[0m"
  7. echo "$commit_message"
  8. exit 1
  9. }
  10. # fail if the commit message contains windows style line breaks (carriage returns)
  11. if grep -q -U $'\x0D' "$commit_file"; then
  12. error "Commit message contains CRLF line breaks (only unix-style LF linebreaks are allowed)"
  13. fi
  14. line_number=0
  15. while read -r line; do
  16. # break on git cut line, used by git commit --verbose
  17. if [[ "$line" == "# ------------------------ >8 ------------------------" ]]; then
  18. break
  19. fi
  20. # ignore comment lines
  21. [[ "$line" =~ ^#.* ]] && continue
  22. # ignore overlong 'fixup!' commit descriptions
  23. [[ "$line" =~ ^fixup!\ .* ]] && continue
  24. ((line_number += 1))
  25. line_length=${#line}
  26. category_pattern="^\S.*?\S: .+"
  27. if [[ $line_number -eq 1 ]] && (echo "$line" | grep -E -v -q "$category_pattern"); then
  28. error "Missing category in commit title (if this is a fix up of a previous commit, it should be squashed)"
  29. fi
  30. title_case_pattern="^\S.*?: [A-Z0-9]"
  31. if [[ $line_number -eq 1 ]] && (echo "$line" | grep -E -v -q "$title_case_pattern"); then
  32. error "First word of commit after the subsystem is not capitalized"
  33. fi
  34. if [[ $line_number -eq 1 ]] && [[ "$line" =~ \.$ ]]; then
  35. error "Commit title ends in a period"
  36. fi
  37. url_pattern="([a-z]+:\/\/)?(([a-zA-Z0-9_]|-)+\.)+[a-z]{2,}(:\d+)?([a-zA-Z_0-9@:%\+.~\?&\/=]|-)+"
  38. if [[ $line_length -gt 72 ]] && (echo "$line" | grep -E -v -q "$url_pattern"); then
  39. error "Commit message lines are too long (maximum allowed is 72 characters)"
  40. fi
  41. if [[ "$line" == "Signed-off-by: "* ]]; then
  42. error "Commit body contains a Signed-off-by tag"
  43. fi
  44. done <"$commit_file"
  45. exit 0