~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Aaron Bentley
  • Date: 2006-05-03 20:05:46 UTC
  • mto: This revision was merged to the branch mainline in revision 366.
  • Revision ID: abentley@panoramicfeedback.com-20060503200546-83ae584b88d70a6b
Changed rpush to rspush

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import re
2
 
 
3
 
from bzrlib import patches
4
 
 
5
 
from bzrlib.plugins.bzrtools import errors
 
1
import patches
6
2
 
7
3
class PatchSource(object):
8
4
    def __iter__(self):
14
10
    def readlines(self):
15
11
        raise NotImplementedError()
16
12
 
17
 
    def readpatches(self):
 
13
    def readhunks(self):
18
14
        return patches.parse_patches(self.readlines())
19
15
 
20
16
class FilePatchSource(PatchSource):
28
24
 
29
25
class BzrPatchSource(PatchSource):
30
26
    def __init__(self, revision=None, file_list=None):
31
 
        from bzrlib.builtins import tree_files
32
 
        self.tree, self.file_list = tree_files(file_list)
33
 
        self.base = self.tree.basedir
 
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.workingtree import WorkingTree
 
36
            self.wt = WorkingTree.open_containing(location)[0]
 
37
            self.base = self.wt.basedir
 
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
 
34
45
        self.revision = revision
35
46
 
36
 
        # Hacks to cope with v0.7 and v0.8 of bzr
37
 
        if self.revision is None:
38
 
            if hasattr(self.tree, 'basis_tree'):
39
 
                self.old_tree = self.tree.basis_tree()
40
 
            else:
41
 
                self.old_tree = self.tree.branch.basis_tree()
42
 
        else:
43
 
            revision_id = self.revision.in_store(self.tree.branch).rev_id
44
 
            if hasattr(self.tree.branch, 'repository'):
45
 
                self.old_tree = self.tree.branch.repository.revision_tree(revision_id)
46
 
            else:
47
 
                self.old_tree = self.tree.branch.revision_tree(revision_id)
48
 
 
49
47
        PatchSource.__init__(self)
50
48
 
51
49
    def readlines(self):
52
 
        from bzrlib.diff import show_diff_trees
53
50
        from StringIO import StringIO
54
51
        f = StringIO()
55
 
 
56
 
        show_diff_trees(self.old_tree, self.tree, f, self.file_list,
57
 
                        old_label='', new_label='')
58
 
        if re.search('Binary files .* differ', f.getvalue()):
59
 
            raise errors.ChangedBinaryFiles()
 
52
        self.__readlines(f)
60
53
        f.seek(0)
61
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.wt, self.file_list, external_diff_options=None,
 
68
                        old_revision_spec=self.revision)
 
69
        sys.stdout = tmp