[PATCH 2 of 5 v2] check-commit: modularize

timeless timeless at mozdev.org
Tue Jan 12 02:56:34 CST 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1452128145 0
#      Thu Jan 07 00:55:45 2016 +0000
# Node ID a5652300012404f634f56b38df51dc5218f571c0
# Parent  c686621f7ac9981d33d2a7f4e6fd20a7678790f3
check-commit: modularize

diff --git a/contrib/check-commit b/contrib/check-commit
--- a/contrib/check-commit
+++ b/contrib/check-commit
@@ -35,25 +35,32 @@
     (r"^\+[ \t]+def [a-z]+_[a-z]", "adds a function with foo_bar naming"),
 ]
 
-node = os.environ.get("HG_NODE")
+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
 
-if node:
-    commit = os.popen("hg export %s" % node).read()
-else:
-    commit = sys.stdin.read()
+def readcommit(node):
+    return os.popen("hg export %s" % node).read()
 
-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
+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)


More information about the Mercurial-devel mailing list