~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Aaron Bentley
  • Date: 2008-01-11 03:01:54 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20080111030154-apm50v0b0tu93prh
Support branch6 formats in rspush

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import re
 
2
 
 
3
from bzrlib import patches
 
4
 
 
5
from bzrlib.plugins.bzrtools import errors
 
6
 
 
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
 
 
17
    def readpatches(self):
 
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):
 
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()
 
42
        else:
 
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)
 
48
 
 
49
        PatchSource.__init__(self)
 
50
 
 
51
    def readlines(self):
 
52
        from bzrlib.diff import show_diff_trees
 
53
        from StringIO import StringIO
 
54
        f = StringIO()
 
55
 
 
56
        show_diff_trees(self.old_tree, self.tree, f, self.file_list,
 
57
                        old_label='', new_label='')
 
58
        if re.search('Binary files .* differ', f.getvalue()):
 
59
            raise errors.ChangedBinaryFiles()
 
60
        f.seek(0)
 
61
        return f.readlines()