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
27 sha1_hash = hashlib.sha1
32 _hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
33 _filename_re = re.compile('^(---|\+\+\+) (\S+)')
35 def parse_patch(text):
40 # state specified the line we just saw, and what to expect next
43 # 1: suspected patch header (diff, ====, Index:)
44 # 2: patch header line 1 (---)
45 # 3: patch header line 2 (+++)
46 # 4: patch hunk header line (@@ line)
47 # 5: patch hunk content
48 # 6: patch meta header (rename from/rename to)
51 # 0 -> 1 (diff, ===, Index:)
56 # 4 -> 5 (patch content)
57 # 5 -> 1 (run out of lines from @@-specifed count)
58 # 1 -> 6 (rename from / rename to)
62 # Suspected patch header is stored into buf, and appended to
63 # patchbuf if we find a following hunk. Otherwise, append to
64 # comment after parsing.
66 # line counts while parsing a patch hunk
71 for line in text.split('\n'):
75 if line.startswith('diff ') or line.startswith('===') \
76 or line.startswith('Index: '):
80 elif line.startswith('--- '):
89 if line.startswith('--- '):
92 if line.startswith('rename from ') or line.startswith('rename to '):
96 if line.startswith('+++ '):
106 commentbuf += buf + line
110 match = _hunk_re.match(line)
118 lc = map(fn, match.groups())
121 patchbuf += buf + line
124 elif line.startswith('--- '):
125 patchbuf += buf + line
135 commentbuf += buf + line
138 elif state == 4 or state == 5:
139 if line.startswith('-'):
141 elif line.startswith('+'):
143 elif line.startswith('\ No newline at end of file'):
144 # Special case: Not included as part of the hunk's line count
152 if lc[0] <= 0 and lc[1] <= 0:
159 if line.startswith('rename to ') or line.startswith('rename from '):
160 patchbuf += buf + line
163 elif line.startswith('--- '):
164 patchbuf += buf + line
173 raise Exception("Unknown state %d! (line '%s')" % (state, line))
183 return (patchbuf, commentbuf)
187 str = str.replace('\r', '')
188 str = str.strip() + '\n'
190 prefixes = ['-', '+', ' ']
193 for line in str.split('\n'):
198 hunk_match = _hunk_re.match(line)
199 filename_match = _filename_re.match(line)
202 # normalise -p1 top-directories
203 if filename_match.group(1) == '---':
207 filename += '/'.join(filename_match.group(2).split('/')[1:])
209 line = filename_match.group(1) + ' ' + filename
212 # remove line numbers, but leave line counts
217 line_nos = map(fn, hunk_match.groups())
218 line = '@@ -%d +%d @@' % tuple(line_nos)
220 elif line[0] in prefixes:
221 # if we have a +, - or context line, leave as-is
225 # other lines are ignored
228 hash.update(line.encode('utf-8') + '\n')
234 from optparse import OptionParser
236 parser = OptionParser()
237 parser.add_option('-p', '--patch', action = 'store_true',
238 dest = 'print_patch', help = 'print parsed patch')
239 parser.add_option('-c', '--comment', action = 'store_true',
240 dest = 'print_comment', help = 'print parsed comment')
241 parser.add_option('-#', '--hash', action = 'store_true',
242 dest = 'print_hash', help = 'print patch hash')
244 (options, args) = parser.parse_args()
246 # decode from (assumed) UTF-8
247 content = sys.stdin.read().decode('utf-8')
249 (patch, comment) = parse_patch(content)
251 if options.print_hash and patch:
252 print hash_patch(patch).hexdigest()
254 if options.print_patch and patch:
255 print "Patch: ------\n" + patch
257 if options.print_comment and comment:
258 print "Comment: ----\n" + comment
260 if __name__ == '__main__':
262 sys.exit(main(sys.argv))