~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/conflicts.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Aaron Bentley
 
1
# Copyright (C) 2005 Aaron Bentley, Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
# point down
21
21
 
22
22
import os
 
23
 
 
24
from bzrlib.lazy_import import lazy_import
 
25
lazy_import(globals(), """
23
26
import errno
24
27
 
25
 
import bzrlib
26
 
from bzrlib.commands import register_command
27
 
from bzrlib.errors import BzrCommandError, NotConflicted, UnsupportedOperation
 
28
from bzrlib import (
 
29
    builtins,
 
30
    commands,
 
31
    errors,
 
32
    osutils,
 
33
    rio,
 
34
    trace,
 
35
    )
 
36
""")
28
37
from bzrlib.option import Option
29
 
from bzrlib.osutils import rename, delete_any
30
 
from bzrlib.rio import Stanza
31
38
 
32
39
 
33
40
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
34
41
 
35
42
 
36
 
class cmd_conflicts(bzrlib.commands.Command):
 
43
class cmd_conflicts(commands.Command):
37
44
    """List files with conflicts.
38
45
 
39
46
    Merge will do its best to combine the changes in two branches, but there
41
48
    it will mark a conflict.  A conflict means that you need to fix something,
42
49
    before you should commit.
43
50
 
 
51
    Conflicts normally are listed as short, human-readable messages.  If --text
 
52
    is supplied, the pathnames of files with text conflicts are listed,
 
53
    instead.  (This is useful for editing all files with text conflicts.)
 
54
 
44
55
    Use bzr resolve when you have fixed a problem.
45
56
 
46
 
    (conflicts are determined by the presence of .BASE .TREE, and .OTHER 
47
 
    files.)
48
 
 
49
57
    See also bzr resolve.
50
58
    """
51
 
    def run(self):
 
59
    takes_options = [Option('text', help='list text conflicts by pathname')]
 
60
 
 
61
    def run(self, text=False):
52
62
        from bzrlib.workingtree import WorkingTree
53
63
        wt = WorkingTree.open_containing(u'.')[0]
54
64
        for conflict in wt.conflicts():
55
 
            print conflict
56
 
 
57
 
 
58
 
class cmd_resolve(bzrlib.commands.Command):
 
65
            if text:
 
66
                if conflict.typestring != 'text conflict':
 
67
                    continue
 
68
                self.outf.write(conflict.path + '\n')
 
69
            else:
 
70
                self.outf.write(str(conflict) + '\n')
 
71
 
 
72
 
 
73
class cmd_resolve(commands.Command):
59
74
    """Mark a conflict as resolved.
60
75
 
61
76
    Merge will do its best to combine the changes in two branches, but there
63
78
    it will mark a conflict.  A conflict means that you need to fix something,
64
79
    before you should commit.
65
80
 
66
 
    Once you have fixed a problem, use "bzr resolve FILE.." to mark
67
 
    individual files as fixed, or "bzr resolve --all" to mark all conflicts as
68
 
    resolved.
 
81
    Once you have fixed a problem, use "bzr resolve" to automatically mark
 
82
    text conflicts as fixed, resolve FILE to mark a specific conflict as
 
83
    resolved, or "bzr resolve --all" to mark all conflicts as resolved.
69
84
 
70
85
    See also bzr conflicts.
71
86
    """
76
91
        from bzrlib.workingtree import WorkingTree
77
92
        if all:
78
93
            if file_list:
79
 
                raise BzrCommandError("If --all is specified, no FILE may be provided")
 
94
                raise errors.BzrCommandError("If --all is specified,"
 
95
                                             " no FILE may be provided")
80
96
            tree = WorkingTree.open_containing('.')[0]
81
97
            resolve(tree)
82
98
        else:
 
99
            tree, file_list = builtins.tree_files(file_list)
83
100
            if file_list is None:
84
 
                raise BzrCommandError("command 'resolve' needs one or more FILE, or --all")
85
 
            tree = WorkingTree.open_containing(file_list[0])[0]
86
 
            to_resolve = [tree.relpath(p) for p in file_list]
87
 
            resolve(tree, to_resolve)
 
101
                un_resolved, resolved = tree.auto_resolve()
 
102
                if len(un_resolved) > 0:
 
103
                    trace.note('%d conflict(s) auto-resolved.', len(resolved))
 
104
                    trace.note('Remaining conflicts:')
 
105
                    for conflict in un_resolved:
 
106
                        trace.note(conflict)
 
107
                    return 1
 
108
                else:
 
109
                    trace.note('All conflicts resolved.')
 
110
                    return 0
 
111
            else:
 
112
                resolve(tree, file_list)
88
113
 
89
114
 
90
115
def resolve(tree, paths=None, ignore_misses=False):
99
124
                tree_conflicts.select_conflicts(tree, paths, ignore_misses)
100
125
        try:
101
126
            tree.set_conflicts(new_conflicts)
102
 
        except UnsupportedOperation:
 
127
        except errors.UnsupportedOperation:
103
128
            pass
104
129
        selected_conflicts.remove_files(tree)
105
130
    finally:
113
138
    """
114
139
    conflicted = False
115
140
    try:
116
 
        rename(filename + ".THIS", filename)
 
141
        osutils.rename(filename + ".THIS", filename)
117
142
        conflicted = True
118
143
    except OSError, e:
119
144
        if e.errno != errno.ENOENT:
131
156
        if e.errno != errno.ENOENT:
132
157
            raise
133
158
    if not conflicted:
134
 
        raise NotConflicted(filename)
 
159
        raise errors.NotConflicted(filename)
135
160
 
136
161
 
137
162
class ConflictList(object):
198
223
                continue
199
224
            for suffix in CONFLICT_SUFFIXES:
200
225
                try:
201
 
                    delete_any(tree.abspath(conflict.path+suffix))
 
226
                    osutils.delete_any(tree.abspath(conflict.path+suffix))
202
227
                except OSError, e:
203
228
                    if e.errno != errno.ENOENT:
204
229
                        raise
258
283
 
259
284
    def __init__(self, path, file_id=None):
260
285
        self.path = path
261
 
        self.file_id = file_id
 
286
        # warn turned off, because the factory blindly transfers the Stanza
 
287
        # values to __init__ and Stanza is purely a Unicode api.
 
288
        self.file_id = osutils.safe_file_id(file_id, warn=False)
262
289
 
263
290
    def as_stanza(self):
264
 
        s = Stanza(type=self.typestring, path=self.path)
 
291
        s = rio.Stanza(type=self.typestring, path=self.path)
265
292
        if self.file_id is not None:
266
 
            s.add('file_id', self.file_id)
 
293
            # Stanza requires Unicode apis
 
294
            s.add('file_id', self.file_id.decode('utf8'))
267
295
        return s
268
296
 
269
297
    def _cmp_list(self):
377
405
                 conflict_file_id=None):
378
406
        HandledConflict.__init__(self, action, path, file_id)
379
407
        self.conflict_path = conflict_path 
380
 
        self.conflict_file_id = conflict_file_id
 
408
        # warn turned off, because the factory blindly transfers the Stanza
 
409
        # values to __init__.
 
410
        self.conflict_file_id = osutils.safe_file_id(conflict_file_id,
 
411
                                                     warn=False)
381
412
        
382
413
    def _cmp_list(self):
383
414
        return HandledConflict._cmp_list(self) + [self.conflict_path, 
387
418
        s = HandledConflict.as_stanza(self)
388
419
        s.add('conflict_path', self.conflict_path)
389
420
        if self.conflict_file_id is not None:
390
 
            s.add('conflict_file_id', self.conflict_file_id)
 
421
            s.add('conflict_file_id', self.conflict_file_id.decode('utf8'))
391
422
            
392
423
        return s
393
424