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
25 def parse_patch(text):
30 # state specified the line we just saw, and what to expect next
33 # 1: suspected patch header (diff, ====, Index:)
34 # 2: patch header line 1 (---)
35 # 3: patch header line 2 (+++)
36 # 4: patch hunk header line (@@ line)
37 # 5: patch hunk content
40 # 0 -> 1 (diff, ===, Index:)
45 # 4 -> 5 (patch content)
46 # 5 -> 1 (run out of lines from @@-specifed count)
48 # Suspected patch header is stored into buf, and appended to
49 # patchbuf if we find a following hunk. Otherwise, append to
50 # comment after parsing.
52 # line counts while parsing a patch hunk
56 hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
58 for line in text.split('\n'):
62 if line.startswith('diff') or line.startswith('===') \
63 or line.startswith('Index: '):
67 elif line.startswith('--- '):
76 if line.startswith('--- '):
80 if line.startswith('+++ '):
90 commentbuf += buf + line
94 match = hunk_re.match(line)
102 lc = map(fn, match.groups())
105 patchbuf += buf + line
108 elif line.startswith('--- '):
109 patchbuf += buf + line
119 commentbuf += buf + line
122 elif state == 4 or state == 5:
123 if line.startswith('-'):
125 elif line.startswith('+'):
133 if lc[0] <= 0 and lc[1] <= 0:
140 raise Exception("Unknown state %d! (line '%s')" % (state, line))
150 return (patchbuf, commentbuf)
152 if __name__ == '__main__':
154 (patch, comment) = parse_patch(sys.stdin.read())
156 print "Patch: ------\n" + patch
158 print "Comment: ----\n" + comment