| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Copyright 2025 The go-yaml Project Contributors |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | # https://www.shellcheck.net/wiki/SC1091 |
| 7 | # shellcheck disable=1091 |
| 8 | source "$(dirname "${BASH_SOURCE[0]}")"/common.bash || exit |
| 9 | require-bash-4.4+ |
| 10 | |
| 11 | usage() ( |
| 12 | cat <<-... |
| 13 | Usage: $0 <commit-range>|<file> |
| 14 | <commit-range>: A range of commits in the form hash..hash |
| 15 | <file>: A file containing a list of commit hashes, one per line" |
| 16 | ... |
| 17 | ) |
| 18 | |
| 19 | main() ( |
| 20 | require-commands git head sed |
| 21 | |
| 22 | case $# in |
| 23 | 0) usage; exit ;; |
| 24 | 1) range_or_file=$1 ;; |
| 25 | *) die \ |
| 26 | 'Error: Too many arguments.' \ |
| 27 | '' \ |
| 28 | "$(usage)" ;; |
| 29 | esac |
| 30 | |
| 31 | # Determine input type |
| 32 | range_or_file=${1:-HEAD} |
| 33 | if [[ -f $range_or_file ]]; then |
| 34 | message=$(< "$range_or_file") |
| 35 | validate-commit-message "$range_or_file" "$message" || |
| 36 | die "Commit message in $range_or_file is invalid." |
| 37 | |
| 38 | else |
| 39 | ok=true |
| 40 | commits=$(git rev-list "$range_or_file") |
| 41 | for commit in $commits; do |
| 42 | message=$(git log --format=%B -n 1 "$commit") |
| 43 | validate-commit-message "$commit" "$message" || |
| 44 | ok=false |
| 45 | done |
| 46 | $ok || die 'At least one commit message is invalid.' |
| 47 | fi |
| 48 | ) |
| 49 | |
| 50 | validate-commit-message() ( |
| 51 | commit_or_file=$1 |
| 52 | message=$2 |
| 53 | subject=$(head -n1 <<<"$message") |
| 54 | length=${#subject} |
| 55 | errors=() |
| 56 | error_lines=() |
| 57 | |
| 58 | [[ $subject =~ ^(feat|fix|docs|style|refactor|perf|test|chore)(\(.*\))?: ]] && |
| 59 | errors+=('Line 1: Do not use conventional commit format for subject') |
| 60 | |
| 61 | # subject should not start with square brackets |
| 62 | [[ $subject =~ ^\[.*\] ]] && |
| 63 | errors+=('Line 1: Subject should not start with square brackets') |
| 64 | |
| 65 | [[ $subject =~ ^[A-Z] ]] || |
| 66 | errors+=('Line 1: Subject should start with a capital letter') |
| 67 | |
| 68 | [[ $subject == *. ]] && |
| 69 | errors+=('Line 1: Subject should not end with a period') |
| 70 | |
| 71 | [[ $subject == *' '* ]] && |
| 72 | errors+=('Line 1: Subject should not contain consecutive spaces') |
| 73 | |
| 74 | [[ $subject == *' ' ]] && |
| 75 | errors+=('Line 1: Subject should not have trailing space(s)') |
| 76 | |
| 77 | [[ $length -ge 20 ]] || |
| 78 | errors+=("Line 1: Subject should be longer than 20 characters (current: $length)") |
| 79 | |
| 80 | [[ $length -le 50 ]] || |
| 81 | errors+=("Line 1: Subject should be shorter than 50 characters (current: $length)") |
| 82 | |
| 83 | [[ ${#errors[*]} -eq 0 ]] || |
| 84 | error_lines+=(1) |
| 85 | |
| 86 | if [[ $(sed -n '2p' <<<"$message") ]]; then |
| 87 | errors+=('Line 2: Subject and body should be separated by a single blank line') |
| 88 | error_lines+=(2) |
| 89 | fi |
| 90 | |
| 91 | body=$(sed -n '3,$p' <<<"$message") |
| 92 | i=3 |
| 93 | while IFS= read -r line; do |
| 94 | if [[ $line == *' ' ]]; then |
| 95 | errors+=("Line $i: Body should not have trailing space(s)") |
| 96 | (( ${#error_lines[@]} )) && [[ ${error_lines[-1]} == "$i" ]] || |
| 97 | error_lines+=("$i") |
| 98 | fi |
| 99 | |
| 100 | lower=${line,,} |
| 101 | if [[ $lower =~ co-authored-by:.*(claude|anthropic|copilot|codex|openai|aider|gemini) ]]; then |
| 102 | errors+=("Line $i: Co-authored-by AI attribution is not currently allowed in commit messages") |
| 103 | (( ${#error_lines[@]} )) && [[ ${error_lines[-1]} == "$i" ]] || |
| 104 | error_lines+=("$i") |
| 105 | fi |
| 106 | |
| 107 | ((i++)) |
| 108 | done <<<"$body" |
| 109 | |
| 110 | # Return if no errors: |
| 111 | [[ ${#errors[@]} -eq 0 ]] && return |
| 112 | |
| 113 | # Report errors to stderr: |
| 114 | ( |
| 115 | echo -e "${RED}Error: '$commit_or_file' has an invalid commit message:$RESET\n" |
| 116 | |
| 117 | # read the message and add the line number in front of each line, and use |
| 118 | # warn_color to display a line with an error based on line_with_errors |
| 119 | i=1 |
| 120 | while IFS= read -r line && [[ ${#error_lines[*]} -gt 0 ]]; do |
| 121 | COLOR=$RESET |
| 122 | if [[ $i == "${error_lines[0]}" ]]; then |
| 123 | COLOR="\e[1;33m" # Bold yellow |
| 124 | error_lines=("${error_lines[@]:1}") |
| 125 | fi |
| 126 | echo -e "${COLOR}Line $(printf '%2d' $i): $line$RESET" |
| 127 | ((i++)) |
| 128 | done <<<"$message" |
| 129 | |
| 130 | echo |
| 131 | printf -- '* %s\n' "${errors[@]}" |
| 132 | echo |
| 133 | ) >&2 |
| 134 | |
| 135 | return 1 |
| 136 | ) |
| 137 | |
| 138 | main "$@" |