3 # Patchwork - automated patch tracking system
4 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
6 # This file is part of the Patchwork package.
8 # Patchwork is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # Patchwork is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Patchwork; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 sha1_hash = hashlib.sha1
31 _hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
32 _filename_re = re.compile('^(---|\+\+\+) (\S+)')
34 def parse_patch(text):
39 # state specified the line we just saw, and what to expect next
42 # 1: suspected patch header (diff, ====, Index:)
43 # 2: patch header line 1 (---)
44 # 3: patch header line 2 (+++)
45 # 4: patch hunk header line (@@ line)
46 # 5: patch hunk content
49 # 0 -> 1 (diff, ===, Index:)
54 # 4 -> 5 (patch content)
55 # 5 -> 1 (run out of lines from @@-specifed count)
57 # Suspected patch header is stored into buf, and appended to
58 # patchbuf if we find a following hunk. Otherwise, append to
59 # comment after parsing.
61 # line counts while parsing a patch hunk
66 for line in text.split('\n'):
70 if line.startswith('diff') or line.startswith('===') \
71 or line.startswith('Index: '):
75 elif line.startswith('--- '):
84 if line.startswith('--- '):
88 if line.startswith('+++ '):
98 commentbuf += buf + line
102 match = _hunk_re.match(line)
110 lc = map(fn, match.groups())
113 patchbuf += buf + line
116 elif line.startswith('--- '):
117 patchbuf += buf + line
127 commentbuf += buf + line
130 elif state == 4 or state == 5:
131 if line.startswith('-'):
133 elif line.startswith('+'):
141 if lc[0] <= 0 and lc[1] <= 0:
148 raise Exception("Unknown state %d! (line '%s')" % (state, line))
158 return (patchbuf, commentbuf)
161 str = str.replace('\r', '')
162 str = str.strip() + '\n'
163 lines = str.split('\n')
165 prefixes = ['-', '+', ' ']
168 for line in str.split('\n'):
173 hunk_match = _hunk_re.match(line)
174 filename_match = _filename_re.match(line)
177 # normalise -p1 top-directories
178 if filename_match.group(1) == '---':
182 filename += '/'.join(filename_match.group(2).split('/')[1:])
184 line = filename_match.group(1) + ' ' + filename
188 # remove line numbers
193 line_nos = map(fn, hunk_match.groups())
194 line = '@@ -%d +%d @@' % tuple(line_nos)
196 elif line[0] in prefixes:
202 hash.update(line + '\n')
204 if __name__ == '__main__':
206 # (patch, comment) = parse_patch(sys.stdin.read())
208 # print "Patch: ------\n" + patch
210 # print "Comment: ----\n" + comment
211 normalise_patch_content(sys.stdin.read())