~abentley/bzrtools/bzrtools.dev

702 by Aaron Bentley
Get conflict-diff under test.
1
# Copyright (C) 2009 Aaron Bentley <aaron@aaronbentley.com>
2
#
3
#    This program is free software; you can redistribute it and/or modify
4
#    it under the terms of the GNU General Public License as published by
5
#    the Free Software Foundation; either version 2 of the License, or
6
#    (at your option) any later version.
7
#
8
#    This program is distributed in the hope that it will be useful,
9
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
10
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
#    GNU General Public License for more details.
12
#
13
#    You should have received a copy of the GNU General Public License
14
#    along with this program; if not, write to the Free Software
15
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
import errno
19
20
from bzrlib.diff import internal_diff
21
from bzrlib.workingtree import WorkingTree
22
from bzrlib.plugins.bzrtools import errors
23
24
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)
45
        tree.lock_read()
46
        try:
47
            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)
55
        finally:
56
            tree.unlock()
57
    return old_lines