foozy@708: #!/usr/bin/python foozy@708: foozy@708: import re foozy@708: foozy@708: def trailing_whitespace(difflines): foozy@708: added, linenum, header = [], 0, False foozy@708: foozy@708: for line in difflines: foozy@708: if header: foozy@708: # remember the name of the file that this diff affects foozy@708: m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line) foozy@708: if m and m.group(1) != '/dev/null': foozy@708: filename = m.group(1).split('/', 1)[-1] foozy@708: if line.startswith('+++ '): foozy@708: header = False foozy@708: continue foozy@708: if line.startswith('diff '): foozy@708: header = True foozy@708: continue foozy@708: # hunk header - save the line number foozy@708: m = re.match(r'@@ -\d+,\d+ \+(\d+),', line) foozy@708: if m: foozy@708: linenum = int(m.group(1)) foozy@708: continue foozy@708: # hunk body - check for an added line with trailing whitespace foozy@708: m = re.match(r'\+.*\s$', line) foozy@708: if m: foozy@708: added.append((filename, linenum)) foozy@708: if line and line[0] in ' +': foozy@708: linenum += 1 foozy@708: return added foozy@708: foozy@708: if __name__ == '__main__': foozy@708: import os, sys foozy@708: foozy@708: added = trailing_whitespace(os.popen('hg export tip')) foozy@708: if added: foozy@708: for filename, linenum in added: foozy@708: print >> sys.stderr, ('%s, line %d: trailing whitespace added' % foozy@708: (filename, linenum)) foozy@708: # save the commit message so we don't need to retype it foozy@708: os.system('hg tip --template "{desc}" > .hg/commit.save') foozy@708: print >> sys.stderr, 'commit message saved to .hg/commit.save' foozy@708: sys.exit(1)