Add an introductory comment.
[git-dm.git] / logparser.py
blobb375034a48259378f7a35938cf698becd7e962f5
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
4 # Copyright © 2009 Germán Póo-Caamaño <gpoo@gnome.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Library General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 import sys
21 from patterns import patterns
23 class LogPatchSplitter:
24 """
25 LogPatchSplitters provides a iterator to extract every
26 changeset from a git log output.
28 Typical use case:
30 patches = LogPatchSplitter(sys.stdin)
32 for patch in patches:
33 parse_patch(patch)
34 """
36 def __init__(self, fd):
37 self.fd = fd
38 self.buffer = None
39 self.patch = []
41 def __iter__(self):
42 return self
44 def next(self):
45 patch = self.__grab_patch__()
46 if not patch:
47 raise StopIteration
48 return patch
50 def __grab_patch__(self):
51 """
52 Extract a patch from the file descriptor and the
53 patch is returned as a list of lines.
54 """
56 patch = []
57 line = self.buffer or self.fd.readline()
59 while line:
60 m = patterns['commit'].match(line)
61 if m:
62 patch = [line]
63 break
64 line = self.fd.readline()
66 if not line:
67 return None
69 line = self.fd.readline()
70 while line:
71 # If this line starts a new commit, drop out.
72 m = patterns['commit'].match(line)
73 if m:
74 self.buffer = line
75 break
77 patch.append(line)
78 self.buffer = None
79 line = self.fd.readline()
81 return patch
84 if __name__ == '__main__':
85 patches = LogPatchSplitter(sys.stdin)
87 for patch in patches:
88 print '---------- NEW PATCH ----------'
89 for line in patch:
90 print line,