~abentley/bzrtools/bzrtools.dev

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