~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Michael Ellerman
  • Date: 2005-11-29 07:12:26 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051129071226-a04b3f827880025d
Unshelve --pick was broken, because we deleted the whole patch, even when only
part of it was unshelved. So now if we unshelve part of a patch, the patch is
replaced with a new patch that has just the unshelved parts. That's a long way
of saying it does what you'd expect.

Implementing this required changing HunkSelector to return both the selected,
and unselected hunks (ie. patches to shelve, and patches to keep).

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