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
26
class ConflictDiffer(object):
32
def run(self, output, files=None, direction='other'):
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)
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 = {
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)
51
def get_old_tree(self, tree, base_path):
52
if self._old_tree is not None:
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)
61
def get_old_lines(self, filename, base_path):
62
""""Return the lines from before the conflicting changes were made."""
64
old_lines = open(base_path).readlines()
66
if e.errno != errno.ENOENT:
68
return self.get_base_tree_lines(filename, base_path)
71
def get_base_tree_lines(self, filename, base_path):
72
if self._tree is None:
73
tree, path = WorkingTree.open_containing(filename)
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 = {
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)
37
def get_old_lines(filename, base_path):
38
""""Return the lines from before the conflicting changes were made."""
40
old_lines = open(base_path).readlines()
42
if e.errno != errno.ENOENT:
44
tree, path = WorkingTree.open_containing(filename)
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)