~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import patches

class PatchSource(object):
    def __iter__(self):
        def iterator(obj):
            for p in obj.read():
                yield p
        return iterator(self)

    def readlines(self):
        raise NotImplementedError()

    def readhunks(self):
        return patches.parse_patches(self.readlines())

class FilePatchSource(PatchSource):
    def __init__(self, filename):
        self.filename = filename
        PatchSource.__init__(self)

    def readlines(self):
        f = open(self.filename, 'r')
        return f.readlines()

class BzrPatchSource(PatchSource):
    def __init__(self, revision=None, file_list=None):
        self.file_list = file_list
        if file_list is not None and len(file_list) > 0:
            location = file_list[0]
        else:
            location = '.'

        # Hack to cope with 0.7 and 0.8 bzr
        try:
            from bzrlib.workingtree import WorkingTree
            self.wt = WorkingTree.open_containing(location)[0]
            self.base = self.wt.basedir
            self.__readlines = self._v08_readlines
        except ImportError:
            from bzrlib.branch import Branch
            self.branch = Branch.open_containing(location)[0]
            self.base = self.branch.base
            self.__readlines = self._v07_readlines

        self.revision = revision

        PatchSource.__init__(self)

    def readlines(self):
        from StringIO import StringIO
        f = StringIO()
        self.__readlines(f)
        f.seek(0)
        return f.readlines()

    def _v07_readlines(self, output):
        from bzrlib.diff import show_diff
        show_diff(self.branch, self.revision,
            specific_files=self.file_list, output=output)

    def _v08_readlines(self, output):
        import sys
        from bzrlib.diff import diff_cmd_helper
        tmp = sys.stdout
        sys.stdout = output
        # FIXME diff_cmd_helper() should take an output parameter
        diff_cmd_helper(self.wt, self.file_list, external_diff_options=None,
                        old_revision_spec=self.revision)
        sys.stdout = tmp