| #!/bin/bash |
| # Copyright 2021 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| # Display an error message. |
| err() { |
| printf "ERROR: %b\n" "$*" >&2 |
| } |
| |
| # Display an error message & exit. |
| die() { |
| err "$@" |
| exit 1 |
| } |
| |
| # Check the commit message to make sure it follows this repo's formatting |
| # guidelines. |
| check_message() { |
| local commit="${1:-HEAD}" |
| |
| # If there's only one file in the commit, make sure the commit message is |
| # scoped to it. |
| local message files file prefix |
| mapfile -d $'\0' files < <( |
| git diff-tree --no-commit-id --name-only -z -r "${commit}" | |
| grep -zZ '\.md$') |
| if [[ ${#files[@]} -eq 1 ]]; then |
| file="${files[0]%.md}" |
| message="$(git log -1 --format=%s "${commit}")" |
| case "${message}" in |
| "${file}: "*|"${file##*/}: "*) |
| ;; |
| *) |
| die "Commit messages should be scoped to the doc you're changing:\n" \ |
| " ${file##*/}: ${message}" |
| ;; |
| esac |
| fi |
| } |
| |
| main() { |
| if [[ $# -lt 1 ]]; then |
| echo "Usage: $0 <action> [args]" |
| exit 1 |
| fi |
| |
| local act="$1" |
| shift |
| case "${act}" in |
| message) |
| "check_${act}" "$@" |
| ;; |
| *) |
| die "Unknown action: ${act}" |
| ;; |
| esac |
| } |
| main "$@" |