|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Git commit hook: |
| 4 | + .git/hooks/commit-msg |
| 5 | +
|
| 6 | + Check commit message according to angularjs guidelines: |
| 7 | + * https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit# |
| 8 | +
|
| 9 | + Check commit scope according to geomatys internal guidelines: |
| 10 | + * https://docs.google.com/a/geomatys.com/spreadsheets/d/1hGwogTSwlmMmIgDsDbAFyBDC9G5xixcMAIRbn9A9TtM/edit# |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | +import sys |
| 15 | +import re |
| 16 | + |
| 17 | +valid_commit_types = ['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'chore'] |
| 18 | +valid_scope_types = ['Admin', 'Client', 'SDI', 'Services', 'Database', 'Token', 'Engine', 'Gui Swing', 'JSON Binding', 'Library', 'Storage', 'Testing', 'WS', 'XML Binding', 'Docker', '*'] |
| 19 | +commit_file = sys.argv[1] |
| 20 | +help_address = 'https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#' |
| 21 | +help_scopes = 'https://docs.google.com/a/geomatys.com/spreadsheets/d/1hGwogTSwlmMmIgDsDbAFyBDC9G5xixcMAIRbn9A9TtM/edit#' |
| 22 | + |
| 23 | +with open(commit_file) as commit: |
| 24 | + lines = commit.readlines() |
| 25 | + if len(lines) == 0: |
| 26 | + sys.stderr.write("\nEmpty commit message\n") |
| 27 | + sys.stderr.write("\n - Refer commit guide: %s\n\n" % help_address) |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + # first line |
| 31 | + line = lines[0] |
| 32 | + m = re.search('^(.*)\((.*)\): (.*)$', line) |
| 33 | + |
| 34 | + if not m or len(m.groups()) != 3: |
| 35 | + sys.stderr.write("\nFirst commit message line (header) does not follow format: type(scope): message\n") |
| 36 | + sys.stderr.write("\n - Refer commit guide: %s\n\n" % help_address) |
| 37 | + sys.exit(1) |
| 38 | + commit_type, commit_scope, commit_message = m.groups() |
| 39 | + if commit_type not in valid_commit_types: |
| 40 | + sys.stderr.write("\nCommit type not in valid ones: %s\n" % ", ".join(valid_commit_types)) |
| 41 | + sys.stderr.write("\n - Refer commit guide: %s\n\n" % help_address) |
| 42 | + sys.exit(1) |
| 43 | + if commit_scope not in valid_scope_types: |
| 44 | + sys.stderr.write("\nCommit scope not in valid ones: %s\n" % ", ".join(valid_scope_types)) |
| 45 | + sys.stderr.write("\n - Refer scope guide: %s\n\n" % help_scopes) |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + |
| 49 | + if len(lines) > 1 and lines[1].strip(): |
| 50 | + sys.stderr.write("\nSecond commit message line must be empty\n") |
| 51 | + sys.stderr.write("\n - Refer commit guide: %s\n\n" % help_address) |
| 52 | + sys.exit(1) |
| 53 | + |
| 54 | + if len(lines) > 2 and not lines[2].strip(): |
| 55 | + sys.stderr.write("\nThird commit message line (body) must not be empty\n") |
| 56 | + sys.stderr.write("\n - Refer commit guide: %s\n\n" % help_address) |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | +sys.exit(0) |
0 commit comments