~abentley/bzrtools/bzrtools.dev

0.4.1 by Aaron Bentley
Use bzrlib.patches instead of a separate copy
1
from bzrlib import patches
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
2
3
class PatchSource(object):
4
    def __iter__(self):
5
        def iterator(obj):
6
            for p in obj.read():
7
                yield p
8
        return iterator(self)
9
10
    def readlines(self):
11
        raise NotImplementedError()
12
0.1.101 by Michael Ellerman
Cleanup naming. PatchSource gives us back Patches not Hunks.
13
    def readpatches(self):
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
14
        return patches.parse_patches(self.readlines())
15
16
class FilePatchSource(PatchSource):
17
    def __init__(self, filename):
18
        self.filename = filename
19
        PatchSource.__init__(self)
20
21
    def readlines(self):
22
        f = open(self.filename, 'r')
23
        return f.readlines()
24
25
class BzrPatchSource(PatchSource):
26
    def __init__(self, revision=None, file_list=None):
0.1.104 by Michael Ellerman
Use show_diff_trees() in BzrPatchSource, reduce the size of the v0.7
27
        from bzrlib.builtins import tree_files
28
        self.tree, self.file_list = tree_files(file_list)
29
        self.base = self.tree.basedir
30
        self.revision = revision
31
32
        # Hacks to cope with v0.7 and v0.8 of bzr
33
        if self.revision is None:
34
            if hasattr(self.tree, 'basis_tree'):
35
                self.old_tree = self.tree.basis_tree()
36
            else:
37
                self.old_tree = self.tree.branch.basis_tree()
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
38
        else:
0.1.104 by Michael Ellerman
Use show_diff_trees() in BzrPatchSource, reduce the size of the v0.7
39
            revision_id = self.revision.in_store(self.tree.branch).rev_id
40
            if hasattr(self.tree.branch, 'repository'):
41
                self.old_tree = self.tree.branch.repository.revision_tree(revision_id)
42
            else:
43
                self.old_tree = self.tree.branch.revision_tree(revision_id)
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
44
45
        PatchSource.__init__(self)
46
47
    def readlines(self):
0.1.104 by Michael Ellerman
Use show_diff_trees() in BzrPatchSource, reduce the size of the v0.7
48
        from bzrlib.diff import show_diff_trees
0.1.96 by Michael Ellerman
Support for bzr 0.7 and 0.8 in BzrPatchSource. Big thanks to marienz
49
        from StringIO import StringIO
50
        f = StringIO()
0.1.104 by Michael Ellerman
Use show_diff_trees() in BzrPatchSource, reduce the size of the v0.7
51
0.1.106 by Michael Ellerman
Save shelved patches in -p0 format by default.
52
        show_diff_trees(self.old_tree, self.tree, f, self.file_list,
53
                        old_label='', new_label='')
0.1.104 by Michael Ellerman
Use show_diff_trees() in BzrPatchSource, reduce the size of the v0.7
54
0.1.96 by Michael Ellerman
Support for bzr 0.7 and 0.8 in BzrPatchSource. Big thanks to marienz
55
        f.seek(0)
56
        return f.readlines()