~abentley/bzrtools/bzrtools.dev

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