inittags: some updates
[git-dm.git] / logparser.py
bloba25d08d6ad941a273f9f0713b4aaaec8e76337dd
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 # We need to avoid decoding errors during the parsing of
42 # patches and using "surrogateescape" allow for encoding
43 # methods to restore the byte if need.
44 self.fd.reconfigure(errors="surrogateescape")
46 def __iter__(self):
47 return self
49 def __next__(self):
50 patch = self.__grab_patch__()
51 if not patch:
52 raise StopIteration
53 return patch
55 def __grab_patch__(self):
56 """
57 Extract a patch from the file descriptor and the
58 patch is returned as a list of lines.
59 """
61 patch = []
62 line = self.buffer or self.fd.readline()
64 while line:
65 if line.startswith('commit '):
66 patch = [line]
67 break
68 line = self.fd.readline()
70 if not line:
71 return None
73 line = self.fd.readline()
74 while line:
75 # If this line starts a new commit, drop out.
76 if line.startswith ('commit '):
77 self.buffer = line
78 break
80 patch.append(line)
81 self.buffer = None
82 line = self.fd.readline()
84 return patch
87 if __name__ == '__main__':
88 patches = LogPatchSplitter(sys.stdin)
90 for patch in patches:
91 print('---------- NEW PATCH ----------')
92 for line in patch:
93 print(line, end = '')