~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Aaron Bentley
  • Date: 2011-06-10 00:54:13 UTC
  • mfrom: (769.1.1 unicode-feature)
  • Revision ID: aaron@aaronbentley.com-20110610005413-re8o7dxxqlh11u7s
Skip unicode test on non-unicode platforms.

Show diffs side-by-side

added added

removed removed

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