~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Michael Ellerman
  • Date: 2006-03-11 13:19:21 UTC
  • mto: (325.1.2 bzrtools) (0.3.1 shelf-dev)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20060311131921-8565bcc3ac4a10b6
I'm sure someone will complain about this, but remove the diffstat after
shelve/unshelve. If you want diffstat after shelve/unshelve then run diffstat!
Make the shelve and unshelve messages symmetrical, and make sure if there's
no message in the patch we print something when unshelving.

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 can_live_update(self):
 
11
        return False
 
12
 
 
13
    def readlines(self):
 
14
        raise NotImplementedError()
 
15
 
 
16
    def readhunks(self):
 
17
        return patches.parse_patches(self.readlines())
 
18
 
 
19
class FilePatchSource(PatchSource):
 
20
    def __init__(self, filename):
 
21
        self.filename = filename
 
22
        PatchSource.__init__(self)
 
23
 
 
24
    def readlines(self):
 
25
        f = open(self.filename, 'r')
 
26
        return f.readlines()
 
27
 
 
28
class BzrPatchSource(PatchSource):
 
29
    def __init__(self, revision=None, file_list=None):
 
30
        from bzrlib.bzrdir import BzrDir
 
31
 
 
32
        self.file_list = file_list
 
33
        if file_list is not None and len(file_list) > 0:
 
34
            location = file_list[0]
 
35
        else:
 
36
            location = '.'
 
37
 
 
38
        self.bzrdir = BzrDir.open_containing(location)[0]
 
39
        self.base = self.bzrdir.open_branch().base
 
40
 
 
41
        self.revision = revision
 
42
 
 
43
        PatchSource.__init__(self)
 
44
 
 
45
    def can_live_update(self):
 
46
        return True
 
47
 
 
48
    def readlines(self):
 
49
        import sys
 
50
        from StringIO import StringIO
 
51
        from bzrlib.diff import diff_cmd_helper
 
52
 
 
53
        # FIXME diff_cmd_helper() should take an output parameter
 
54
        f = StringIO()
 
55
        tmp = sys.stdout
 
56
        sys.stdout = f
 
57
        diff_cmd_helper(self.bzrdir.open_workingtree(), self.file_list,
 
58
                external_diff_options=None, old_revision_spec=self.revision)
 
59
        sys.stdout = tmp
 
60
        f.seek(0)
 
61
        return f.readlines()