~abentley/bzrtools/bzrtools.dev

119 by aaron.bentley at utoronto
Added conflict-awareness tools
1
# Copyright (C) 2005 by Aaron Bentley
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
import bzrlib.status
18
from bzrlib.branch import Branch
122 by aaron.bentley at utoronto
Added resolve command
19
import os
20
import errno
119 by aaron.bentley at utoronto
Added conflict-awareness tools
21
121 by aaron.bentley at utoronto
Fixed suffixes for conflict files
22
SUFFIXES = ('.THIS', '.BASE', '.OTHER')
119 by aaron.bentley at utoronto
Added conflict-awareness tools
23
def get_conflicted_stem(path):
24
    for suffix in SUFFIXES:
25
        if path.endswith(suffix):
26
            return path[:-len(suffix)]
27
28
def iter_conflicts(tree):
29
    conflicted = set()
30
    for path in (s[0] for s in tree.list_files()):
31
        stem = get_conflicted_stem(path)
32
        if stem is None:
33
            continue
34
        if stem not in conflicted:
35
            conflicted.add(stem)
36
            yield stem
37
38
class cmd_conflicts(bzrlib.commands.Command):
39
    """List files with conflicts.
40
    (conflicts are determined by the presence of .BASE .TREE, and .OTHER 
41
    files.)
42
    """
43
    def run(self):
158 by Aaron Bentley
Updated to match API changes
44
        for path in iter_conflicts(Branch.open_containing('.').working_tree()):
119 by aaron.bentley at utoronto
Added conflict-awareness tools
45
            print path
46
122 by aaron.bentley at utoronto
Added resolve command
47
class cmd_resolve(bzrlib.commands.Command):
48
    """Mark a conflict as resolved.
49
    """
50
    takes_args = ['file+']
51
    def run(self, file_list):
52
        for file in file_list:
53
            failures = 0
54
            for suffix in SUFFIXES:
55
                try:
56
                    os.unlink(file+suffix)
57
                except OSError, e:
58
                    if e.errno != errno.ENOENT:
59
                        raise
60
                    else:
61
                        failures += 1
62
            if failures == len(SUFFIXES):
63
                print "%s is not conflicted" % file
64
                        
65
            
66
119 by aaron.bentley at utoronto
Added conflict-awareness tools
67
# monkey-patch the standard 'status' to give us conflicts, too.
68
def _show_status(branch, **kwargs):
69
    old_show_status(branch, **kwargs)
70
    conflicted = list(iter_conflicts(branch.working_tree()))
71
    if len(conflicted) > 0:
72
        print "conflicts:"
73
        for f in conflicted:
74
            print " ", f
75
76
old_show_status = bzrlib.status.show_status
77
bzrlib.status.show_status = _show_status