blob: 253bddc33fbfc4b53a07966e0646afa39b85dfaf [file] [log] [blame]
Mike Frysinger7ca2af92021-06-01 10:57:11 -04001#!/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.
7err() {
8 printf "ERROR: %b\n" "$*" >&2
9}
10
11# Display an error message & exit.
12die() {
13 err "$@"
14 exit 1
15}
16
17# Check the commit message to make sure it follows this repo's formatting
18# guidelines.
19check_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 Kyostila37067de2021-10-28 12:05:15 +010032 "${file}: "*|"${file##*/}: "*)
Mike Frysinger7ca2af92021-06-01 10:57:11 -040033 ;;
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
42main() {
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}
59main "$@"