Patchwork [2,of,5,v2] check-commit: modularize

login
register
mail settings
Submitter timeless@mozdev.org
Date Jan. 12, 2016, 8:56 a.m.
Message ID <a5652300012404f634f5.1452588994@waste.org>
Download mbox | patch
Permalink /patch/12689/
State Accepted
Commit f47185f09533e156f4121279c19acb140bcceabd
Headers show

Comments

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

Patch

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)