Mike Frysinger | 7ca2af9 | 2021-06-01 10:57:11 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2021 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | # Display an error message. |
| 7 | err() { |
| 8 | printf "ERROR: %b\n" "$*" >&2 |
| 9 | } |
| 10 | |
| 11 | # Display an error message & exit. |
| 12 | die() { |
| 13 | err "$@" |
| 14 | exit 1 |
| 15 | } |
| 16 | |
| 17 | # Check the commit message to make sure it follows this repo's formatting |
| 18 | # guidelines. |
| 19 | check_message() { |
| 20 | local commit="${1:-HEAD}" |
| 21 | |
| 22 | # If there's only one file in the commit, make sure the commit message is |
| 23 | # scoped to it. |
| 24 | local message files file prefix |
| 25 | mapfile -d $'\0' files < <( |
| 26 | git diff-tree --no-commit-id --name-only -z -r "${commit}" | |
| 27 | grep -zZ '\.md$') |
| 28 | if [[ ${#files[@]} -eq 1 ]]; then |
| 29 | file="${files[0]%.md}" |
| 30 | message="$(git log -1 --format=%s "${commit}")" |
| 31 | case "${message}" in |
Sami Kyostila | 37067de | 2021-10-28 12:05:15 +0100 | [diff] [blame] | 32 | "${file}: "*|"${file##*/}: "*) |
Mike Frysinger | 7ca2af9 | 2021-06-01 10:57:11 -0400 | [diff] [blame] | 33 | ;; |
| 34 | *) |
| 35 | die "Commit messages should be scoped to the doc you're changing:\n" \ |
| 36 | " ${file##*/}: ${message}" |
| 37 | ;; |
| 38 | esac |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | main() { |
| 43 | if [[ $# -lt 1 ]]; then |
| 44 | echo "Usage: $0 <action> [args]" |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | local act="$1" |
| 49 | shift |
| 50 | case "${act}" in |
| 51 | message) |
| 52 | "check_${act}" "$@" |
| 53 | ;; |
| 54 | *) |
| 55 | die "Unknown action: ${act}" |
| 56 | ;; |
| 57 | esac |
| 58 | } |
| 59 | main "$@" |