comparison check_whitespace.py @ 6:95e8189b5fb4

Check for newline at end of file.
author Sjoerd Mullender <sjoerd@acm.org>
date Fri, 31 Oct 2014 16:19:24 +0100 (2014-10-31)
parents 32c51a111990
children 2ac85cc1e11b
comparison
equal deleted inserted replaced
5:32c51a111990 6:95e8189b5fb4
25 fnre = re.compile(r'(?:---|\+\+\+) (?P<filename>[^\t\r\n]+)') 25 fnre = re.compile(r'(?:---|\+\+\+) (?P<filename>[^\t\r\n]+)')
26 lnre = re.compile(r'@@ -\d+,\d+ \+(?P<lineno>\d+),') 26 lnre = re.compile(r'@@ -\d+,\d+ \+(?P<lineno>\d+),')
27 wsre = re.compile(r'\+.*[ \t]$') 27 wsre = re.compile(r'\+.*[ \t]$')
28 tbre = re.compile(r'\+.*\t') 28 tbre = re.compile(r'\+.*\t')
29 resre = re.compile(r'\+(<<<<<<<|>>>>>>>)') 29 resre = re.compile(r'\+(<<<<<<<|>>>>>>>)')
30 adding = False
30 31
31 for chunk in difflines: 32 for chunk in difflines:
32 for line in chunk.split('\n'): 33 for line in chunk.split('\n'):
33 if header: 34 if header:
34 # remember the name of the file that this diff affects 35 # remember the name of the file that this diff affects
38 if line.startswith('+++ '): 39 if line.startswith('+++ '):
39 header = False 40 header = False
40 continue 41 continue
41 if line.startswith('diff '): 42 if line.startswith('diff '):
42 header = True 43 header = True
44 adding = False
43 continue 45 continue
44 # hunk header - save the line number 46 # hunk header - save the line number
45 m = lnre.match(line) 47 m = lnre.match(line)
46 if m is not None: 48 if m is not None:
47 linenum = int(m.group('lineno')) 49 linenum = int(m.group('lineno'))
48 continue 50 continue
49 if header or not filename: 51 if header or not filename:
50 continue 52 continue
53 if line[:1] == '+':
54 adding = True
55 elif line[:1] in (' ', '-'):
56 adding = False
57 elif adding and line.startswith(r'\ No newline at end of file'):
58 adding = False
59 yield filename, linenum, 'no newline at end of file'
51 # hunk body - check for an added line with bad whitespace 60 # hunk body - check for an added line with bad whitespace
52 if filename[-3:] == '.py' or filename[-5:] == '.py.in': 61 if filename[-3:] == '.py' or filename[-5:] == '.py.in':
53 m = tbre.match(line) 62 m = tbre.match(line)
54 if m is not None: 63 if m is not None:
55 yield filename, linenum, 'TABs' 64 yield filename, linenum, 'TABs'