~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Aaron Bentley
  • Date: 2006-03-07 15:23:15 UTC
  • mfrom: (321.1.2 bzrtools)
  • mto: (147.4.31 trunk)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: abentley@panoramicfeedback.com-20060307152315-42337454a0ad956b
MergeĀ fromĀ mainline

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()