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 _hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
27 _filename_re = re.compile('^(---|\+\+\+) (\S+)')
29 def parse_patch(text):
34 # state specified the line we just saw, and what to expect next
37 # 1: suspected patch header (diff, ====, Index:)
38 # 2: patch header line 1 (---)
39 # 3: patch header line 2 (+++)
40 # 4: patch hunk header line (@@ line)
41 # 5: patch hunk content
44 # 0 -> 1 (diff, ===, Index:)
49 # 4 -> 5 (patch content)
50 # 5 -> 1 (run out of lines from @@-specifed count)
52 # Suspected patch header is stored into buf, and appended to
53 # patchbuf if we find a following hunk. Otherwise, append to
54 # comment after parsing.
56 # line counts while parsing a patch hunk
61 for line in text.split('\n'):
65 if line.startswith('diff') or line.startswith('===') \
66 or line.startswith('Index: '):
70 elif line.startswith('--- '):
79 if line.startswith('--- '):
83 if line.startswith('+++ '):
93 commentbuf += buf + line
97 match = _hunk_re.match(line)
105 lc = map(fn, match.groups())
108 patchbuf += buf + line
111 elif line.startswith('--- '):
112 patchbuf += buf + line
122 commentbuf += buf + line
125 elif state == 4 or state == 5:
126 if line.startswith('-'):
128 elif line.startswith('+'):
136 if lc[0] <= 0 and lc[1] <= 0:
143 raise Exception("Unknown state %d! (line '%s')" % (state, line))
153 return (patchbuf, commentbuf)
156 str = str.replace('\r', '')
157 str = str.strip() + '\n'
158 lines = str.split('\n')
160 prefixes = ['-', '+', ' ']
161 hash = hashlib.sha1()
163 for line in str.split('\n'):
168 hunk_match = _hunk_re.match(line)
169 filename_match = _filename_re.match(line)
172 # normalise -p1 top-directories
173 if filename_match.group(1) == '---':
177 filename += '/'.join(filename_match.group(2).split('/')[1:])
179 line = filename_match.group(1) + ' ' + filename
183 # remove line numbers
188 line_nos = map(fn, hunk_match.groups())
189 line = '@@ -%d +%d @@' % tuple(line_nos)
191 elif line[0] in prefixes:
197 hash.update(line + '\n')
199 if __name__ == '__main__':
201 # (patch, comment) = parse_patch(sys.stdin.read())
203 # print "Patch: ------\n" + patch
205 # print "Comment: ----\n" + comment
206 normalise_patch_content(sys.stdin.read())