]> code.delx.au - gnu-emacs/blob - build-aux/git-hooks/commit-msg
Port commit-msg to broken MS-Windows shell
[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-2015 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='ยข'
33 print_at_sign='{print substr("'$cent_sign'@", 2)}'
34 at_sign=`$awk "$print_at_sign" 2>/dev/null`
35 if test "$at_sign" != @; then
36 at_sign=`LC_ALL=en_US.UTF-8 $awk "$print_at_sign" 2>/dev/null`
37 if test "$at_sign" = @; then
38 LC_ALL=en_US.UTF-8; export LC_ALL
39 fi
40 fi
41
42 # Check the log entry.
43 exec $awk -v at_sign="$at_sign" -v cent_sign="$cent_sign" '
44 BEGIN {
45 # These regular expressions assume traditional Unix unibyte behavior.
46 # They are needed for old or broken versions of awk, e.g.,
47 # mawk 1.3.3 (1996), or gawk on MSYS (2015).
48 space = "[ \f\n\r\t\v]"
49 non_space = "[^ \f\n\r\t\v]"
50 non_print = "[\1-\37\177]"
51
52 # Prefer POSIX regular expressions if available, as they do a
53 # better job of checking. Similarly, prefer POSIX negated
54 # expressions if UTF-8 also works.
55 if (" " ~ /[[:space:]]/) {
56 space = "[[:space:]]"
57 if (at_sign == "@" && cent_sign ~ /^[[:print:]]$/) {
58 non_space = "[^[:space:]]"
59 non_print = "[^[:print:]]"
60 }
61 }
62 }
63
64 /^#/ {
65 # Ignore every line after a scissors line.
66 if (/^# *---* *(>[8%]|[8%]<) *---* *$/) { exit }
67
68 # Ignore comment lines.
69 next
70 }
71
72 !/^.*$/ {
73 print "Invalid character (not UTF-8) in commit message"
74 status = 1
75 }
76
77 nlines == 0 && $0 !~ non_space { next }
78
79 { nlines++ }
80
81 nlines == 1 {
82 # Ignore special markers used by "git rebase --autosquash".
83 if (! sub(/^fixup! /, ""))
84 sub(/^squash! /, "")
85
86 if ($0 ~ "^" space) {
87 print "White space at start of commit message'\''s first line"
88 status = 1
89 }
90 }
91
92 nlines == 2 && $0 ~ non_space {
93 print "Nonempty second line in commit message"
94 status = 1
95 }
96
97 {
98 # Expand tabs to spaces for length calculations etc.
99 while (match($0, /\t/)) {
100 before_tab = substr($0, 1, RSTART - 1)
101 after_tab = substr($0, RSTART + 1)
102 $0 = sprintf("%s%*s%s", before_tab, 8 - (RSTART - 1) % 8, "", after_tab)
103 }
104 }
105
106 78 < length && $0 ~ space {
107 print "Line longer than 78 characters in commit message"
108 status = 1
109 }
110
111 140 < length {
112 print "Word longer than 140 characters in commit message"
113 status = 1
114 }
115
116 /^Signed-off-by: / {
117 print "'\''Signed-off-by:'\'' in commit message"
118 status = 1
119 }
120
121 $0 ~ non_print {
122 print "Unprintable character in commit message"
123 status = 1
124 }
125
126 END {
127 if (nlines == 0) {
128 print "Empty commit message"
129 status = 1
130 }
131 if (status != 0) {
132 print "Commit aborted; please see the file 'CONTRIBUTE'"
133 }
134 exit status
135 }
136 ' <"$1"