util: Break up some unit tests in the m5 utility.
[gem5.git] / util / git-commit-msg.py
index 1ce8aa1716e8c376232cde4aca4913e73bb24a25..b2ff164522b17d9b16e114990f1bd88dd2e2d698 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2.7
+#!/usr/bin/env python3
 #
 # Copyright (c) 2019 Inria
 # All rights reserved
@@ -25,8 +25,6 @@
 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-# Authors: Daniel Carvalho
 
 # If your commit has been canceled because of this file, do not panic: a
 # copy of it has been stored in ./.git/COMMIT_EDITMSG
@@ -34,6 +32,7 @@
 import os
 import re
 import sys
+from maint.lib import maintainers
 
 from style.repo import GitRepo
 
@@ -45,13 +44,22 @@ def _printErrorQuit(error_message):
             failure.
     """
     print(error_message)
+
+    print("The commit has been cancelled, but a copy of it can be found in "
+          + sys.argv[1] + " : ")
+
     print("""
-The commit has been canceled, but a copy of it can be found in
-.git/COMMIT_EDITMSG
+--------------------------------------------------------------------------
+    """)
+    print(open(sys.argv[1], "r").read())
+    print("""
+--------------------------------------------------------------------------
+    """)
 
+    print("""
 The first line of a commit must contain one or more gem5 tags separated by
-commas (see MAINTAINERS for the possible tags), followed by a colon and a
-commit title. There must be no leading nor trailing whitespaces.
+commas (see MAINTAINERS.yaml for the possible tags), followed by a colon and
+commit title. There must be no leading nor trailing whitespaces.
 
 This header line must then be followed by an empty line. A detailed message,
 although highly recommended, is not mandatory and can follow that empty line.
@@ -67,7 +75,6 @@ e.g.:
 
     The packet class...
 """)
-    print("A copy of your commit message can be found in " + sys.argv[1])
     sys.exit(1)
 
 def _validateTags(commit_header):
@@ -79,16 +86,12 @@ def _validateTags(commit_header):
     """
 
     # List of valid tags
-    # @todo this is error prone, and should be extracted automatically from
-    #       a file
+    maintainer_dict = maintainers.Maintainers.from_file()
+    valid_tags = [tag for tag, _ in maintainer_dict]
 
-    valid_tags = ["arch", "arch-alpha", "arch-arm", "arch-hsail", "arch-mips",
-        "arch-power", "arch-riscv", "arch-sparc", "arch-x86", "base",
-        "configs", "cpu", "cpu-kvm", "cpu-minor", "cpu-o3", "cpu-simple",
-        "dev", "dev-arm", "dev-virtio", "ext", "fastmodel", "gpu-compute",
-        "learning-gem5", "mem", "mem-cache", "mem-garnet", "mem-ruby", "misc",
-        "python", "scons", "sim", "sim-se", "sim-power", "stats", "system",
-        "system-alpha", "system-arm", "systemc", "tests", "util", "RFC", "WIP"]
+    # Remove non-tag 'pmc' and add special tags not in MAINTAINERS.yaml
+    valid_tags.remove('pmc')
+    valid_tags.extend(['RFC', 'WIP'])
 
     tags = ''.join(commit_header.split(':')[0].split()).split(',')
     if (any(tag not in valid_tags for tag in tags)):
@@ -106,10 +109,12 @@ commit_message = open(sys.argv[1]).read()
 commit_message_lines = commit_message.splitlines()
 commit_header = commit_message_lines[0]
 commit_header_match = \
-    re.search("^(\S[\w\-][,\s*[\w\-]+]*:.+\S$)", commit_header)
+    re.search("^(fixup! )?(\S[\w\-][,\s*[\w\-]+]*:.+\S$)", commit_header)
 if ((commit_header_match is None)):
     _printErrorQuit("Invalid commit header")
-_validateTags(commit_header)
+if commit_header_match.group(1) == "fixup! ":
+    sys.exit(0)
+_validateTags(commit_header_match.group(2))
 
 # Make sure commit title does not exceed threshold. This line is limited to
 # a smaller number because version control systems may add a prefix, causing