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