|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +. ./scripts/log.sh |
| 4 | +export LOG_LEVEL=4 # debug |
| 5 | +. ./.githooks/util |
| 6 | + |
| 7 | +log_trace "Running $0" |
| 8 | + |
| 9 | +if [ -z "$SUBJECT_LINE" ]; then |
| 10 | + log_error "Commit message cannot be empty" |
| 11 | + print_help_msg |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +if [[ "$SUBJECT_LINE" != *": "* ]]; then |
| 16 | + log_error "Commit message has invalid format: '$SUBJECT_LINE'" |
| 17 | + print_help_msg |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +type="${SUBJECT_LINE%%:*}" |
| 22 | +if ! grep -qx "$type" <(printf "%s\n" $VALID_TYPES); then |
| 23 | + log_error "Commit message has invalid type: '$type'" |
| 24 | + log_error "Allowed types are: $VALID_TYPES" |
| 25 | + print_help_msg |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +summary="${SUBJECT_LINE#*: }" |
| 30 | +if [[ -z "$summary" ]]; then |
| 31 | + log_error "Commit message missing summary" |
| 32 | + log_error "The part after ":" must describe your change." |
| 33 | + print_help_msg |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +len_SUBJECT_LINE=${#SUBJECT_LINE} |
| 38 | +if (( len_SUBJECT_LINE > 50 )); then |
| 39 | + log_error "First line of commit message is too long: $len_SUBJECT_LINE characters (max 50)." |
| 40 | + |
| 41 | + # print the part which is too long in red |
| 42 | + prefix="${SUBJECT_LINE:0:50}" # everything before 50th place |
| 43 | + suffix="${SUBJECT_LINE:50}" # everything after 50th place |
| 44 | + log_error "Message subject line, with excessive characters shown in red: " |
| 45 | + log_error |
| 46 | + log_error -n && printf "%s%b\n" "$prefix" "${ANSI_RED}${suffix}${ANSI_NC}" |
| 47 | + log_error |
| 48 | + |
| 49 | + print_help_msg |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +if [[ "$SUBJECT_LINE" == *."" ]]; then |
| 54 | + log_error "Subject line should not end with a period: '$SUBJECT_LINE'" |
| 55 | + print_help_msg |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +if (( LINE_COUNT > 1 )); then |
| 60 | +# If there's a body, ensure blank line separator |
| 61 | + if [ -n "$SECOND_LINE" ]; then |
| 62 | + log_error "Missing blank line between subject and body: '$SECOND_LINE'" |
| 63 | + print_help_msg |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | +fi |
| 67 | + |
| 68 | +line_no=0 |
| 69 | +found_too_long_body_line=false |
| 70 | +while IFS= read -r line; do |
| 71 | + # skip blank lines |
| 72 | + [[ -z "$line" ]] && { (( line_no++ )); continue; } |
| 73 | + |
| 74 | + len=${#line} |
| 75 | + if (( len > 72 )); then |
| 76 | + prefix="${line:0:72}" # everything before 72nd place |
| 77 | + suffix="${line:72}" # everything after 72nd place |
| 78 | + |
| 79 | + log_error "Body line $line_no is too long: $len characters (max 72):" |
| 80 | + log_error |
| 81 | + log_error -n && printf "%s%b\n" "$prefix" "${ANSI_RED}${suffix}${ANSI_NC}" |
| 82 | + log_error |
| 83 | + |
| 84 | + found_too_long_body_line=true |
| 85 | + fi |
| 86 | + (( line_no++ )) |
| 87 | +done <<< "$BODY" |
| 88 | + |
| 89 | +if [ "$found_too_long_body_line" = "true" ]; then |
| 90 | + exit 1 |
| 91 | +fi |
0 commit comments