Switch to unified view

a/scripts/git-hooks/for-your-local-repo/commit-msg b/scripts/git-hooks/for-your-local-repo/commit-msg
1
#!/bin/bash
1
#!/usr/bin/python
2
2
3
grep '^Signed-off-by: ' $1 || {
3
import re, sys
4
    echo >&2 No Sign-off.
4
5
    exit 1
5
signoff = re.compile('^Signed-off-by: (.*)$', flags=re.MULTILINE)
6
}
6
bug = re.compile('\[(?:.*:)?#\d+\]')
7
8
def deny_commit(message):
9
    print message
10
    sys.exit(1)
11
12
def main():
13
    # argv[1] is the name of the file holding the commit message.
14
    # It is _not_ a commit, it has no headers.  The first line is
15
    # the subject.
16
    with open(sys.argv[1]) as commit_msg:
17
        subject = commit_msg.readline()
18
19
        if not bug.search(subject):
20
            deny_commit('Commit subject must reference a ticket.')
21
22
        number_of_signoffs = 0
23
        signoffs = set()
24
        for line in commit_msg.readlines():
25
            match = signoff.match(line)
26
            # comment lines won't match signoff, so we effectively ignore them
27
            if match:
28
                number_of_signoffs += 1
29
                signoffs.add(match.group(1))
30
31
    # must be at least one sign-off
32
    if not len(signoffs):
33
        deny_commit('Commit must be signed-off.')
34
35
    # and every sign-off must be different
36
    if len(signoffs) < number_of_signoffs:
37
        deny_commit('Duplicate sign-offs found.')
38
39
if __name__ == '__main__':
40
    main()