1
#!/usr/bin/env python
2
#-*- coding:utf-8 -*-
3
#
4
# Copyright © 2009 Germán Póo-Caamaño <gpoo@gnome.org>
5
#
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.
10
#
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.
15
#
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
19
20
import sys
21
from patterns import patterns
22
23
class LogPatchSplitter:
24
    """
25
        LogPatchSplitters provides a iterator to extract every
26
        changeset from a git log output.
27
28
        Typical use case:
29
30
            patches = LogPatchSplitter(sys.stdin)
31
32
            for patch in patches:
33
                parse_patch(patch)
34
    """
35
36
    def __init__(self, fd):
37
        self.fd = fd
38
        self.buffer = None
39
        self.patch = []
40
41
    def __iter__(self):
42
        return self
43
44
    def next(self):
45
        patch = self.__grab_patch__()
46
        if not patch:
47
            raise StopIteration
48
        return patch
49
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
        """
55
56
        patch = []
57
        line = self.buffer or self.fd.readline()
58
59
        while line:
60
            m = patterns['commit'].match(line)
61
            if m:
62
                patch = [line]
63
                break
64
            line = self.fd.readline()
65
66
        if not line:
67
            return None
68
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
76
77
            patch.append(line)
78
            self.buffer = None
79
            line = self.fd.readline()
80
81
        return patch
82
83
84
if __name__ == '__main__':
85
    patches = LogPatchSplitter(sys.stdin)
86
87
    for patch in patches:
88
        print '---------- NEW PATCH ----------'
89
        for line in patch:
90
            print line,