~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to conflict_diff.py

  • Committer: Aaron Bentley
  • Date: 2009-07-10 04:05:24 UTC
  • Revision ID: aaron@aaronbentley.com-20090710040524-slufgm4he3fx1r4f
Mirror the child_submit_to setting.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import errno
19
19
 
20
 
from bzrlib.conflicts import TextConflict
21
20
from bzrlib.diff import internal_diff
22
21
from bzrlib.workingtree import WorkingTree
23
22
from bzrlib.plugins.bzrtools import errors
24
23
 
25
24
 
26
 
class ConflictDiffer(object):
27
 
 
28
 
    def __init__(self):
29
 
        self._old_tree = None
30
 
        self._tree = None
31
 
 
32
 
    def run(self, output, files=None, direction='other'):
33
 
        if files is None:
34
 
            self._tree = WorkingTree.open_containing('.')[0]
35
 
            files = [self._tree.abspath(c.path) for c in self._tree.conflicts()
36
 
                     if isinstance(c, TextConflict)]
37
 
        for filename in files:
38
 
            self.conflict_diff(output, filename, direction)
39
 
 
40
 
    def conflict_diff(self, output, filename, direction):
41
 
        """Perform a diff for a file with conflicts."""
42
 
        old_path = filename + '.BASE'
43
 
        old_lines = self.get_old_lines(filename, old_path)
44
 
        new_path_extension = {
45
 
            'other': '.OTHER',
46
 
            'this': '.THIS'}[direction]
47
 
        new_path = filename + new_path_extension
48
 
        newlines = open(new_path).readlines()
49
 
        internal_diff(old_path, old_lines, new_path, newlines, output)
50
 
 
51
 
    def get_old_tree(self, tree, base_path):
52
 
        if self._old_tree is not None:
53
 
            return self._old_tree
54
 
        graph = tree.branch.repository.get_graph()
55
 
        parent_ids = tree.get_parent_ids()
56
 
        if len(parent_ids) < 2:
57
 
            raise errors.NoConflictFiles(base_path)
58
 
        lca = graph.find_unique_lca(*parent_ids)
59
 
        return tree.branch.repository.revision_tree(lca)
60
 
 
61
 
    def get_old_lines(self, filename, base_path):
62
 
        """"Return the lines from before the conflicting changes were made."""
63
 
        try:
64
 
            old_lines = open(base_path).readlines()
65
 
        except IOError, e:
66
 
            if e.errno != errno.ENOENT:
67
 
                raise
68
 
            return self.get_base_tree_lines(filename, base_path)
69
 
        return old_lines
70
 
 
71
 
    def get_base_tree_lines(self, filename, base_path):
72
 
        if self._tree is None:
73
 
            tree, path = WorkingTree.open_containing(filename)
74
 
        else:
75
 
            tree = self._tree
76
 
            path = tree.relpath(filename)
 
25
def conflict_diff(output, filename, direction):
 
26
    """Perform a diff for a file with conflicts."""
 
27
    old_path = filename + '.BASE'
 
28
    old_lines = get_old_lines(filename, old_path)
 
29
    new_path_extension = {
 
30
        'other': '.OTHER',
 
31
        'this': '.THIS'}[direction]
 
32
    new_path = filename + new_path_extension
 
33
    newlines = open(new_path).readlines()
 
34
    internal_diff(old_path, old_lines, new_path, newlines, output)
 
35
 
 
36
 
 
37
def get_old_lines(filename, base_path):
 
38
    """"Return the lines from before the conflicting changes were made."""
 
39
    try:
 
40
        old_lines = open(base_path).readlines()
 
41
    except IOError, e:
 
42
        if e.errno != errno.ENOENT:
 
43
            raise
 
44
        tree, path = WorkingTree.open_containing(filename)
77
45
        tree.lock_read()
78
46
        try:
79
47
            file_id = tree.path2id(path)
80
 
            old_tree = self.get_old_tree(tree, base_path)
81
 
            return old_tree.get_file_lines(file_id)
 
48
            graph = tree.branch.repository.get_graph()
 
49
            parent_ids = tree.get_parent_ids()
 
50
            if len(parent_ids) < 2:
 
51
                raise errors.NoConflictFiles(base_path)
 
52
            lca = graph.find_unique_lca(*parent_ids)
 
53
            oldtree = tree.branch.repository.revision_tree(lca)
 
54
            old_lines = oldtree.get_file_lines(file_id)
82
55
        finally:
83
56
            tree.unlock()
 
57
    return old_lines