~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Michael Ellerman
  • Date: 2006-06-16 03:48:48 UTC
  • mfrom: (0.4.1 shelf)
  • mto: (0.3.4 shelf-dev)
  • mto: This revision was merged to the branch mainline in revision 396.
  • Revision ID: michael@ellerman.id.au-20060616034848-25d1c5bb8a043237
Use bzrlib.patches, thanks to Aaron.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from bzrlib import patches
 
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
 
 
13
    def readpatches(self):
 
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):
 
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()
 
38
        else:
 
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)
 
44
 
 
45
        PatchSource.__init__(self)
 
46
 
 
47
    def readlines(self):
 
48
        from bzrlib.diff import show_diff_trees
 
49
        from StringIO import StringIO
 
50
        f = StringIO()
 
51
 
 
52
        show_diff_trees(self.old_tree, self.tree, f, self.file_list,
 
53
                        old_label='', new_label='')
 
54
 
 
55
        f.seek(0)
 
56
        return f.readlines()