]> code.delx.au - gnu-emacs/blob - build-aux/git-hooks/commit-msg
Improve commit-msg messages and autosquash
[gnu-emacs] / build-aux / git-hooks / commit-msg
1 #!/bin/sh
2 # Check the format of GNU Emacs change log entries.
3
4 # Copyright 2014 Free Software Foundation, Inc.
5
6 # This file is part of GNU Emacs.
7
8 # GNU Emacs is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # GNU Emacs is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20
21 # Written by Paul Eggert.
22
23 # Prefer gawk if available, as it handles NUL bytes properly.
24 if type gawk >/dev/null 2>&1; then
25 awk=gawk
26 else
27 awk=awk
28 fi
29
30 # Use a UTF-8 locale if available, so that the UTF-8 check works.
31 # Use U+00A2 CENT SIGN to test whether the locale works.
32 cent_sign_utf8_octal='\302\242'
33 at_sign=`
34 printf "${cent_sign_utf8_octal}@" |
35 $awk '{print substr($0, 2)}' 2>/dev/null
36 `
37 if test "$at_sign" != @; then
38 at_sign=`
39 printf "${cent_sign_utf8_octal}@" |
40 LC_ALL=en_US.UTF-8 $awk '{print substr($0, 2)}' 2>/dev/null
41 `
42 if test "$at_sign" = @; then
43 LC_ALL=en_US.UTF-8; export LC_ALL
44 fi
45 fi
46
47 # Check the log entry.
48 exec $awk '
49 /^#/ { next }
50
51 !/^.*$/ {
52 print "Invalid character (not UTF-8) in commit message"
53 status = 1
54 }
55
56 nlines == 0 && !/[^[:space:]]/ { next }
57
58 { nlines++ }
59
60 nlines == 1 {
61 # Ignore special markers used by "git rebase --autosquash".
62 if (! sub(/^fixup! /, ""))
63 sub(/^squash! /, "")
64
65 if (/^[[:space:]]/) {
66 print "White space at start of commit message'\''s first line"
67 status = 1
68 }
69 }
70
71 nlines == 2 && /[^[:space:]]/ {
72 print "Nonempty second line in commit message"
73 status = 1
74 }
75
76 72 < length && /[[:space:]]/ {
77 print "Line longer than 72 characters in commit message"
78 status = 1
79 }
80
81 140 < length {
82 print "Word longer than 140 characters in commit message"
83 status = 1
84 }
85
86 /^Signed-off-by: / {
87 print "'\''Signed-off-by:'\'' in commit message"
88 status = 1
89 }
90
91 /[^[:print:]]/ {
92 if (gsub(/\t/, "")) {
93 print "Tab in commit message; please use spaces instead"
94 }
95 if (/[^[:print:]]/) {
96 print "Unprintable character in commit message"
97 }
98 status = 1
99 }
100
101 END {
102 if (nlines == 0) {
103 print "Empty commit message"
104 status = 1
105 }
106 exit status
107 }
108 ' <"$1"