check-commit: modularize
authortimeless <timeless@mozdev.org>
Thu, 07 Jan 2016 00:55:45 +0000
changeset 27780 f47185f09533
parent 27779 b2479d305c10
child 27781 2af351bd289c
check-commit: modularize
contrib/check-commit
--- a/contrib/check-commit	Thu Jan 07 03:58:40 2016 +0000
+++ b/contrib/check-commit	Thu Jan 07 00:55:45 2016 +0000
@@ -35,25 +35,32 @@
     (r"^\+[ \t]+def [a-z]+_[a-z]", "adds a function with foo_bar naming"),
 ]
 
-node = os.environ.get("HG_NODE")
-
-if node:
-    commit = os.popen("hg export %s" % node).read()
-else:
-    commit = sys.stdin.read()
+def checkcommit(commit):
+    exitcode = 0
+    for exp, msg in errors:
+        m = re.search(exp, commit, re.MULTILINE)
+        if m:
+            pos = 0
+            for n, l in enumerate(commit.splitlines(True)):
+                pos += len(l)
+                if pos >= m.end():
+                    print "%d: %s" % (n, msg)
+                    print " %s" % l[:-1]
+                    if "BYPASS" not in os.environ:
+                        exitcode = 1
+                    break
+    return exitcode
 
-exitcode = 0
-for exp, msg in errors:
-    m = re.search(exp, commit, re.MULTILINE)
-    if m:
-        pos = 0
-        for n, l in enumerate(commit.splitlines(True)):
-            pos += len(l)
-            if pos >= m.end():
-                print "%d: %s" % (n, msg)
-                print " %s" % l[:-1]
-                if "BYPASS" not in os.environ:
-                    exitcode = 1
-                break
+def readcommit(node):
+    return os.popen("hg export %s" % node).read()
+
+if __name__ == "__main__":
+    node = os.environ.get("HG_NODE")
 
-sys.exit(exitcode)
+    if node:
+        commit = readcommit(node)
+    else:
+        commit = sys.stdin.read()
+
+    exitcode = checkcommit(commit)
+    sys.exit(exitcode)