internal/control/repos/acme/site.git/hooks/commit-msg.sample
74 lines · 1972 bytes · executable
1#!/bin/sh
2#
3# An example hook script to check the commit log message.
4# Called by "git commit" with one argument, the name of the file
5# that has the commit message. The hook should exit with non-zero
6# status after issuing an appropriate message if it wants to stop the
7# commit. The hook is allowed to edit the commit message file.
8#
9# To enable this hook, rename this file to "commit-msg".
10
11# Uncomment the below to add a Signed-off-by line to the message.
12# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13# hook is more suited to it.
14#
15# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17
18# This example catches duplicate Signed-off-by lines and messages that
19# would confuse 'git am'.
20
21ret=0
22
23test "" = "$(grep '^Signed-off-by: ' "$1" |
24 sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
25 echo >&2 Duplicate Signed-off-by lines.
26 ret=1
27}
28
29comment_re="$(
30 {
31 git config --get-regexp "^core\.comment(char|string)\$" ||
32 echo '#'
33 } | sed -n -e '
34 ${
35 s/^[^ ]* //
36 s|[][*./\]|\\&|g
37 s/^auto$/[#;@!$%^&|:]/
38 p
39 }'
40)"
41scissors_line="^${comment_re} -\{8,\} >8 -\{8,\}\$"
42comment_line="^${comment_re}.*"
43blank_line='^[ ]*$'
44# Disallow lines starting with "diff -" or "Index: " in the body of the
45# message. Stop looking if we see a scissors line.
46line="$(sed -n -e "
47 # Skip comments and blank lines at the start of the file.
48 /${scissors_line}/q
49 /${comment_line}/d
50 /${blank_line}/d
51 # The first paragraph will become the subject header so
52 # does not need to be checked.
53 : subject
54 n
55 /${scissors_line}/q
56 /${blank_line}/!b subject
57 # Check the body of the message for problematic
58 # prefixes.
59 : body
60 n
61 /${scissors_line}/q
62 /${comment_line}/b body
63 /^diff -/{p;q;}
64 /^Index: /{p;q;}
65 b body
66 " "$1")"
67if test -n "$line"
68then
69 echo >&2 "Message contains a diff that will confuse 'git am'."
70 echo >&2 "To fix this indent the diff."
71 ret=1
72fi
73
74exit $ret