~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Aaron Bentley
  • Date: 2006-03-16 14:53:00 UTC
  • mfrom: (0.1.96 shelf)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: abentley@panoramicfeedback.com-20060316145300-836889d55437eb15
Merge shelf v2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
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 readhunks(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
        self.file_list = file_list
 
28
        if file_list is not None and len(file_list) > 0:
 
29
            location = file_list[0]
 
30
        else:
 
31
            location = '.'
 
32
 
 
33
        # Hack to cope with 0.7 and 0.8 bzr
 
34
        try:
 
35
            from bzrlib.bzrdir import BzrDir
 
36
            self.bzrdir = BzrDir.open_containing(location)[0]
 
37
            self.base = self.bzrdir.open_branch().base
 
38
            self.__readlines = self._v08_readlines
 
39
        except ImportError:
 
40
            from bzrlib.branch import Branch
 
41
            self.branch = Branch.open_containing(location)[0]
 
42
            self.base = self.branch.base
 
43
            self.__readlines = self._v07_readlines
 
44
 
 
45
        self.revision = revision
 
46
 
 
47
        PatchSource.__init__(self)
 
48
 
 
49
    def readlines(self):
 
50
        from StringIO import StringIO
 
51
        f = StringIO()
 
52
        self.__readlines(f)
 
53
        f.seek(0)
 
54
        return f.readlines()
 
55
 
 
56
    def _v07_readlines(self, output):
 
57
        from bzrlib.diff import show_diff
 
58
        show_diff(self.branch, self.revision,
 
59
            specific_files=self.file_list, output=output)
 
60
 
 
61
    def _v08_readlines(self, output):
 
62
        import sys
 
63
        from bzrlib.diff import diff_cmd_helper
 
64
        tmp = sys.stdout
 
65
        sys.stdout = output
 
66
        # FIXME diff_cmd_helper() should take an output parameter
 
67
        diff_cmd_helper(self.bzrdir.open_workingtree(), self.file_list,
 
68
                    external_diff_options=None, old_revision_spec=self.revision)
 
69
        sys.stdout = tmp