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
24 from collections import Counter
28 sha1_hash = hashlib.sha1
33 _hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
34 _filename_re = re.compile('^(---|\+\+\+) (\S+)')
36 def parse_patch(text):
41 # state specified the line we just saw, and what to expect next
44 # 1: suspected patch header (diff, ====, Index:)
45 # 2: patch header line 1 (---)
46 # 3: patch header line 2 (+++)
47 # 4: patch hunk header line (@@ line)
48 # 5: patch hunk content
49 # 6: patch meta header (rename from/rename to)
52 # 0 -> 1 (diff, ===, Index:)
57 # 4 -> 5 (patch content)
58 # 5 -> 1 (run out of lines from @@-specifed count)
59 # 1 -> 6 (rename from / rename to)
63 # Suspected patch header is stored into buf, and appended to
64 # patchbuf if we find a following hunk. Otherwise, append to
65 # comment after parsing.
67 # line counts while parsing a patch hunk
72 for line in text.split('\n'):
76 if line.startswith('diff ') or line.startswith('===') \
77 or line.startswith('Index: '):
81 elif line.startswith('--- '):
90 if line.startswith('--- '):
93 if line.startswith('rename from ') or line.startswith('rename to '):
97 if line.startswith('+++ '):
107 commentbuf += buf + line
111 match = _hunk_re.match(line)
119 lc = map(fn, match.groups())
122 patchbuf += buf + line
125 elif line.startswith('--- '):
126 patchbuf += buf + line
130 elif hunk and line.startswith('\ No newline at end of file'):
131 # If we had a hunk and now we see this, it's part of the patch,
132 # and we're still expecting another @@ line.
141 commentbuf += buf + line
144 elif state == 4 or state == 5:
145 if line.startswith('-'):
147 elif line.startswith('+'):
149 elif line.startswith('\ No newline at end of file'):
150 # Special case: Not included as part of the hunk's line count
158 if lc[0] <= 0 and lc[1] <= 0:
165 if line.startswith('rename to ') or line.startswith('rename from '):
166 patchbuf += buf + line
169 elif line.startswith('--- '):
170 patchbuf += buf + line
179 raise Exception("Unknown state %d! (line '%s')" % (state, line))
189 return (patchbuf, commentbuf)
193 str = str.replace('\r', '')
194 str = str.strip() + '\n'
196 prefixes = ['-', '+', ' ']
199 for line in str.split('\n'):
204 hunk_match = _hunk_re.match(line)
205 filename_match = _filename_re.match(line)
208 # normalise -p1 top-directories
209 if filename_match.group(1) == '---':
213 filename += '/'.join(filename_match.group(2).split('/')[1:])
215 line = filename_match.group(1) + ' ' + filename
218 # remove line numbers, but leave line counts
223 line_nos = map(fn, hunk_match.groups())
224 line = '@@ -%d +%d @@' % tuple(line_nos)
226 elif line[0] in prefixes:
227 # if we have a +, - or context line, leave as-is
231 # other lines are ignored
234 hash.update(line.encode('utf-8') + '\n')
238 def extract_tags(content, tags):
242 regex = re.compile(tag.pattern, re.MULTILINE | re.IGNORECASE)
243 counts[tag] = len(regex.findall(content))
248 from optparse import OptionParser
250 parser = OptionParser()
251 parser.add_option('-p', '--patch', action = 'store_true',
252 dest = 'print_patch', help = 'print parsed patch')
253 parser.add_option('-c', '--comment', action = 'store_true',
254 dest = 'print_comment', help = 'print parsed comment')
255 parser.add_option('-#', '--hash', action = 'store_true',
256 dest = 'print_hash', help = 'print patch hash')
258 (options, args) = parser.parse_args()
260 # decode from (assumed) UTF-8
261 content = sys.stdin.read().decode('utf-8')
263 (patch, comment) = parse_patch(content)
265 if options.print_hash and patch:
266 print hash_patch(patch).hexdigest()
268 if options.print_patch and patch:
269 print "Patch: ------\n" + patch
271 if options.print_comment and comment:
272 print "Comment: ----\n" + comment
274 if __name__ == '__main__':
276 sys.exit(main(sys.argv))