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