summaryrefslogtreecommitdiff
path: root/scripts/git-hooks/commit-msg
blob: 96d594799dafb3c1b91164cbae5770bfc82733a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh
# A hook script to check the commit log message.

log_file=$1
export log_file

re_edit()
{
  read -p "Hit return to edit. Ctrl-C to abort..." v 1>&2
  ${EDITOR:-vi} "$log_file"
}

get_msg()
{
  sed '/^#/d' "$log_file" #filter comments
}

check_msg()
{
  # First line must contain a colon, e.g., "keyword: ...".
  line_1=$(get_msg | sed 1q)
  case $line_1 in
    *:*) ;;
    [Vv]ersion' '[0-9]*) return 0;;
    *) echo "missing colon on first line of log message"; return 1;;
  esac

  # The token(s) before the colon on the first line must be one of
  # the following.  Tokens may be space- or comma-separated.
  fail=0
  for w in $(echo "$line_1"|sed 's/:.*//'|tr -s ' ,' '  '); do
    case $w in
      # program names
      \[|arch|base64|basename|cat|chcon|chgrp|chmod|chown|chroot) ;;
      cksum|comm|cp|csplit|cut|date|dd|df|dir|dircolors|dirname|du) ;;
      echo|env|expand|expr|factor|false|fmt|fold|groups|head|hostid) ;;
      hostname|id|install|join|kill|link|ln|logname|ls|md5sum|mkdir) ;;
      mkfifo|mknod|mktemp|mv|nice|nl|nohup|nproc|od|paste|pathchk) ;;
      pinky|pr|printenv|printf|ptx|pwd|readlink|rm|rmdir|runcon) ;;
      seq|sha1sum|sha224sum|sha256sum|sha384sum|sha512sum|shred|shuf) ;;
      sleep|sort|split|stat|stdbuf|stty|su|sum|sync|tac|tail|tee) ;;
      test|timeout|touch|tr|true|truncate|tsort|tty|uname|unexpand) ;;
      uniq|unlink|uptime|users|vdir|wc|who|whoami|yes) ;;
      # other tags
      gnulib|tests|maint|doc|build|scripts) ;;
      *) echo "invalid first word of summary line: $w"; fail=1;;
    esac
  done
  test $fail = 1 && return 1

  # Limit line length to allow for tab in changelog
  test $(get_msg | wc -L | cut -f1 -d' ') -gt 72 \
    && { echo "line > 72 chars"; return 1; }

  # Second line should be blank or not present
  test "$(get_msg | sed -n 2p)" \
    && { echo "second line should be blank"; return 1; }

  get_msg | grep -E 'https?://bugzilla\.redhat\.com/show_bug\.cgi' >&2 \
    && { echo 'use shorter http://bugzilla.redhat.com/NNNNNN'; return 1; }

  get_msg | grep -E 'https?://debbugs\.gnu\.org/cgi/bugreport\.cgi?bug=' >&2 \
    && { echo 'use shorter http://bugs.gnu.org/NNNNN'; return 1; }

  # Flag redundant use of "issue"
  get_msg | grep -Fi "issue reported by" >&2 \
    && { echo "just say: Reported by ..."; return 1; }

  return 0;
}

while :; do
  err=$(check_msg) && break
  ME=${0##*/}
  err="$ME: $err"
  # Insert the diagnostic as a comment on the first line of $log_file.
  perl -ni -e '$. == 1 and print "# '"$err"'\n"; print' $log_file
  printf '%s\n' "$err" 1>&2
  re_edit
done