aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/git/commit-msg-hook.py
blob: 9c397edba727831e090f48aa6c6bd6b353ba64a2 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys


#   format: \033[type;fg;bgm
#
#     fg                bg              color
#   -------------------------------------------
#     30                40              black
#     31                41              red
#     32                42              green
#     33                43              yellow
#     34                44              blue
#     35                45              purple
#     36                46              cyan
#     37                47              white
#
#     type
#   -------------------------
#      0               normal
#      1               bold
#      4               underline
#      5               blink
#      7               invert
#      8               hide
#
#   examples:
#   \033[1;31;40m    <!--1-bold 31-red fg 40-black bg-->
#   \033[0m          <!--back to normal-->


STYLE = {
        'fore':
        {
            'black'    : 30,
            'red'      : 31,
            'green'    : 32,
            'yellow'   : 33,
            'blue'     : 34,
            'purple'   : 35,
            'cyan'     : 36,
            'white'    : 37,
        },

        'back':
        {
            'black'     : 40,
            'red'       : 41,
            'green'     : 42,
            'yellow'    : 43,
            'blue'      : 44,
            'purple'    : 45,
            'cyan'      : 46,
            'white'     : 47,
        },

        'mode':
        {
            'normal'    : 0,
            'bold'      : 1,
            'underline' : 4,
            'blink'     : 5,
            'invert'    : 7,
            'hide'      : 8,
        },

        'default':
        {
            'end': 0,
        },
}


def style(string, mode='', fore='', back=''):

    mode = '%s' % STYLE['mode'][mode] if STYLE['mode'].has_key(mode) else ''

    fore = '%s' % STYLE['fore'][fore] if STYLE['fore'].has_key(fore) else ''

    back = '%s' % STYLE['back'][back] if STYLE['back'].has_key(back) else ''

    style = ';'.join([s for s in [mode, fore, back] if s])

    style = '\033[%sm' % style if style else ''

    end = '\033[%sm' % STYLE['default']['end'] if style else ''

    return '%s%s%s' % (style, string, end)


def check_subject(subject_line):
    types = ['Feat', 'Fix', 'Refactor', 'Style', 'Docs', 'Test', 'Chore']

    if subject_line.startswith(' '):
        print style('Error: Subject line starts with whitespace\n', fore='red')
        return 1

    if len(subject_line) > 50:
        print style('Error: Subject line should be limited to 50 chars\n', fore='red')
        return 1

    ll = subject_line.split(':')
    if len(ll) < 2:
        print style('Error: Subject line should have a type\n', fore='red')
        return 1

    type = ll[0]
    if type not in types:
        print style('Error: Subject line starts with unknown type\n', fore='red')
        return 1

    return 0


contents = []
ret = 0
subject = True

with open(sys.argv[1], 'r') as commit_msg:
    contents = commit_msg.readlines()

for line in contents:
    dup = line.lstrip()
    if dup.startswith('#') or dup.startswith("Change-Id") or dup.startswith("Signed-of-by"):
        continue
    if subject is True:
        ret = check_subject(line)
        subject = False
    else:
        if len(line) > 72:
            print style('Error: Body line should be limited to 72 chars\n', fore='red')
            ret = 1

exit(ret)