util: Add a git commit-msg hook
[gem5.git] / util / git-commit-msg.py
1 #!/usr/bin/env python2.7
2 #
3 # Copyright (c) 2019 Inria
4 # All rights reserved
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met: redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer;
10 # redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution;
13 # neither the name of the copyright holders nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #
29 # Authors: Daniel Carvalho
30
31 # If your commit has been canceled because of this file, do not panic: a
32 # copy of it has been stored in ./.git/COMMIT_EDITMSG
33
34 import os
35 import re
36 import sys
37
38 from style.repo import GitRepo
39
40 def _printErrorQuit(error_message):
41 """
42 Print an error message, followed my a help message and inform failure.
43
44 @param error_message A message describing the error that caused the
45 failure.
46 """
47 print(error_message)
48 print("""
49 The commit has been canceled, but a copy of it can be found in
50 .git/COMMIT_EDITMSG
51
52 The first line of a commit must contain one or more gem5 tags separated by
53 commas (see MAINTAINERS for the possible tags), followed by a colon and a
54 commit title. There must be no leading nor trailing whitespaces.
55
56 This header line must then be followed by an empty line. A detailed message,
57 although highly recommended, is not mandatory and can follow that empty line.
58
59 e.g.:
60 cpu: Refactor branch predictors
61
62 Refactor branch predictor code to improve its readability, moving functions
63 X and Y to the base class...
64
65 e.g.:
66 mem,mem-cache: Improve packet class readability
67
68 The packet class...
69 """)
70 print("A copy of your commit message can be found in " + sys.argv[1])
71 sys.exit(1)
72
73 def _validateTags(commit_header):
74 """
75 Check if all tags in the commit header belong to the list of valid
76 gem5 tags.
77
78 @param commit_header The first line of the commit message.
79 """
80
81 # List of valid tags
82 # @todo this is error prone, and should be extracted automatically from
83 # a file
84 valid_tags = ["arch", "arch-alpha", "arch-arm", "arch-hsail", "arch-mips",
85 "arch-power", "arch-riscv", "arch-sparc", "arch-x86", "base",
86 "configs", "cpu", "cpu-kvm", "cpu-minor", "cpu-o3", "cpu-simple",
87 "dev", "dev-arm", "dev-virtio", "ext", "gpu-compute", "learning-gem5",
88 "mem", "mem-cache", "mem-garnet", "mem-ruby", "misc", "python",
89 "scons", "sim", "sim-se", "sim-power", "stats", "system",
90 "system-alpha", "system-arm", "systemc", "tests", "util", "RFC", "WIP"]
91
92 tags = ''.join(commit_header.split(':')[0].split()).split(',')
93 if (any(tag not in valid_tags for tag in tags)):
94 invalid_tag = next((tag for tag in tags if tag not in valid_tags))
95 _printErrorQuit("Invalid Gem5 tag: " + invalid_tag)
96
97 # Go to git directory
98 os.chdir(GitRepo().repo_base())
99
100 # Get the commit message
101 commit_message = open(sys.argv[1]).read()
102
103 # The first line of a commit must contain at least one valid gem5 tag, and
104 # a commit title
105 commit_message_lines = commit_message.splitlines()
106 commit_header = commit_message_lines[0]
107 commit_header_match = \
108 re.search("^(\S[\w\-][,\s*[\w\-]+]*:.+\S$)", commit_header)
109 if ((commit_header_match is None)):
110 _printErrorQuit("Invalid commit header")
111 _validateTags(commit_header)
112
113 # Make sure commit title does not exceed threshold. This line is limited to
114 # a smaller number because version control systems may add a prefix, causing
115 # line-wrapping for longer lines
116 commit_title = commit_header.split(':')[1]
117 max_header_size = 65
118 if (len(commit_header) > max_header_size):
119 _printErrorQuit("The commit header (tags + title) is too long (" + \
120 str(len(commit_header)) + " > " + str(max_header_size) + ")")
121
122 # Then there must be at least one empty line between the commit header and
123 # the commit description
124 if (commit_message_lines[1] != ""):
125 _printErrorQuit("Please add an empty line between the commit title and " \
126 "its description")
127
128 # Encourage providing descriptions
129 if (re.search("^(Signed-off-by|Change-Id|Reviewed-by):",
130 commit_message_lines[2])):
131 print("Warning: Commit does not have a description")
132
133 sys.exit(0)