<div dir="ltr">Here it goes:<div><br></div><div><div>#!/usr/bin/env python</div><div><br></div><div>import re</div><div>import fileinput</div><div>import unidiff</div><div><br></div><div># Add new checks as (<regex>, <message>) tuples.</div>
<div>checks = {</div><div> (re.compile(r'.* \).*'),</div><div> "Excessive space before ')'"),</div><div> (re.compile(r'.*[A-Za-z_][A-Za-z0-9_]*\s*\( .*'),</div><div> "Excessive space after '('"),</div>
<div> (re.compile(r'\s+{\s*'),</div><div> "Opening '{' on separate line"),</div><div> (re.compile(r'.*,(?!\s|$).*', re.MULTILINE),</div><div> "Missing space after ','"),</div>
<div> (re.compile(r'.*for \(.*;[^\s].*'),</div><div> "Missing space after ';'"),</div><div> (re.compile(r'.*(?:\+=|-=|==|!=)[^\s].*'),</div><div> "Missing space after operator"),</div>
<div> (re.compile(r'.*[^\s](?:\+=|-=|==|!=).*'),</div><div> "Missing space before operator"),</div><div> (re.compile(r'.*if\(.*'),</div><div> "Missing space after \"if\""),</div>
<div> (re.compile(r'.*for\(.*'),</div><div> "Missing space after \"for\""),</div><div> (re.compile(r'.*while\(.*'),</div><div> "Missing space after \"while\"")</div>
<div>}</div><div><br></div><div>for patch in unidiff.parser.parse_unidiff(fileinput.input()):</div><div> suffix = patch.path.split('.')[-1]</div><div> if suffix != 'cpp' and suffix != 'cc' and suffix != 'h':</div>
<div> continue</div><div> for hunk in patch:</div><div> for i, line in enumerate(hunk.target_lines):</div><div> if hunk.target_types[i] != unidiff.parser.LINE_TYPE_ADD:</div><div> continue</div>
<div> line_nr = hunk.target_start + i</div><div> for (regex, message) in checks:</div><div> if regex.match(line):</div><div> print("%s:%i: %s" % (patch.path, line_nr, message))</div>
</div><div><br></div><div>It needs python-unidiff [1] for diff parsing.</div><div><br></div><div>Example usage:</div><div><br></div><div><div>$ curl -s <a href="https://git.reviewboard.kde.org/r/109907/diff/raw/">https://git.reviewboard.kde.org/r/109907/diff/raw/</a> | difflint</div>
<div>libs/main/KoFileDialogHelper.cpp:35: Excessive space before ')'</div><div>libs/main/KoFileDialogHelper.cpp:35: Excessive space after '('</div><div>libs/main/KoFileDialogHelper.cpp:36: Missing space after "if"</div>
<div>libs/main/KoFileDialogHelper.cpp:61: Missing space after "if"</div><div>libs/main/KoMainWindow.cpp:848: Excessive space before ')'</div><div>libs/main/KoMainWindow.cpp:860: Excessive space before ')'</div>
<div>libs/main/KoMainWindow.cpp:980: Missing space before operator</div><div>libs/main/KoMainWindow.cpp:980: Excessive space after '('</div><div>libs/main/KoMainWindow.cpp:1082: Excessive space before ')'</div>
<div>libs/main/KoMainWindow.cpp:1667: Excessive space before ')'</div><div>libs/main/KoMainWindow.cpp:1667: Excessive space after '('</div><div>libs/main/KoMainWindow.cpp:1731: Excessive space before ')'</div>
<div>libs/main/KoMainWindow.cpp:1731: Excessive space after '('</div></div><div><br></div><div>It makes a few false positives, but mostly points to real style breaks. Perhaps it can be of some small use to reviewers/reviewees.</div>
<div><br></div><div>Cheers,</div><div>Elvis</div><div><br></div><div>[1]</div></div>