~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to conflict_diff.py

  • Committer: Aaron Bentley
  • Date: 2009-10-20 22:21:59 UTC
  • Revision ID: aaron@aaronbentley.com-20091020222159-x8yo2eog4gy0v8qn
Conflict differ works with no file specified or many files specified.

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
20
21
from bzrlib.diff import internal_diff
21
22
from bzrlib.workingtree import WorkingTree
22
23
from bzrlib.plugins.bzrtools import errors
23
24
 
24
25
 
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)
 
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)
45
77
        tree.lock_read()
46
78
        try:
47
79
            file_id = tree.path2id(path)
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)
 
80
            old_tree = self.get_old_tree(tree, base_path)
 
81
            return old_tree.get_file_lines(file_id)
55
82
        finally:
56
83
            tree.unlock()
57
 
    return old_lines