[RFC] checkpatch.py

Stefano Mioli jstevie at gmail.com
Fri Jul 3 17:59:57 CDT 2009


Hi devs,
	here is a script I wrote to check a patch created by hg export against
some of the coding style rules that can be found at

	http://mercurial.selenic.com/wiki/BasicCodingStyle

It's still very simple, but Matt said he would like to have such a
tool in contrib, so here it is.

It only does extra-simple checks for now, namely:

- leading tabs
- trailing whitespaces
- DOS-style line endings
- lines longer than 80 characters

Feedback, patches and suggestions are very welcome.

Please keep in mind that I'm no Python Ninja (actually these are my
very first lines of Python code), so the code will probably be not
that pythonic. Suggestions in this regard would help too.

This script is in a very early stage of development: it can only check
one patch at a time and knows nothing about sys.stdin, so you still
can't do something like

	hg export tip | checkpatch.py

but I am willing to improve it based on the feedback I will receive, and
I obviously welcome patches.

Patch attached below, hope it doesn't get screwed up.

If you feel more like pulling, I've put up a clone of mercurial's main
repo at

	http://bitbucket.org/sm/hg-checkpatch


As a side note, this is my first post to this ML (except for a patch I
sent a few days ago) so I would like to introduce myself with a few
words: my name is Stefano Mioli and I write from Italy. I can be found
(very rarely) as 'walkindude' on #mercurial.


Stefano


# HG changeset patch
# User Stefano Mioli <jstevie at gmail.com>
# Date 1246658568 -7200
# Node ID 03d1c0a9167aece6a588c358a55f3bcfbea13d4c
# Parent  b81baf9e4dd6fe5e475fb431977e838761632ef2
added checkpatch.py

diff -r b81baf9e4dd6 -r 03d1c0a9167a contrib/checkpatch.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/checkpatch.py	Sat Jul 04 00:02:48 2009 +0200
@@ -0,0 +1,86 @@
+#!/usr/bin/python
+#
+# checkpatch.py - coding style checker for Mercurial developers
+#
+# Copyright 2009 Stefano Mioli <jstevie at gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2, incorporated herein by reference.
+
+import sys, re
+
+def checkpatch(lines):
+
+    errs = list()
+    warns = list()
+
+    lineindex = 0
+
+    addedline_re = re.compile(r'^\+{1,2}[^\+][\w\W]*')
+    leadingtab_re = re.compile(r'^ *\t{1,}[\s\S]*')
+    trailingwspace_re = re.compile(r'[ \t]+$')
+    crlf_re = re.compile(r'\r\n$')
+
+    for line in lines:
+        lineindex += 1
+
+        r = addedline_re.match(line)
+        if (not r):
+            continue
+
+        line = line[1:]
+
+        r = leadingtab_re.match(line)
+        if (r):
+            errs.append('line ' + str(lineindex) + ': leading tab')
+
+        r = trailingwspace_re.search(line)
+        if (r):
+            errs.append('line ' + str(lineindex) + ': trailing whitespace')
+
+        r = crlf_re.search(line)
+        if (r):
+            errs.append('line ' + str(lineindex) + ': DOS style line ending')
+
+        if (len(line) > 80):
+            warns.append('line ' + str(lineindex) +
+                            ': line longer than 80 chr')
+
+
+    if (len(errs) == 0 and len(warns) == 0):
+        print 'patch looks good'
+    else:
+        errors = (len(errs) > 0)
+        warnings = (len(warns) > 0)
+
+        msg = ''
+        if (errors):
+            msg = str(len(errs)) + ' errors'
+        if (warnings):
+            if (len(msg)):
+                msg += ', '
+            msg += str(len(warns)) + ' warnings'
+        print msg + '\n'
+
+        if (errors):
+            print 'errors:'
+            for e in errs:
+                print e
+        if (warnings):
+            print 'warnings:'
+            for w in warns:
+                print w
+
+if (__name__ == '__main__'):
+    if (len(sys.argv) < 2):
+        print 'usage: ./checkpatch.py patch-to-check\n'
+        sys.exit(1)
+
+    filepath = str(sys.argv[1])
+
+    patchfile = open(filepath, 'r')
+    lines = patchfile.readlines()
+    patchfile.close()
+
+    checkpatch(lines)
+



More information about the Mercurial-devel mailing list