19
19
# TODO: 'bzr resolve' should accept a directory name and work from that
22
# TODO: bzr revert should resolve; even when reverting the whole tree
23
# or particular directories
29
from bzrlib.branch import Branch
30
from bzrlib.errors import BzrCommandError, NotConflicted
31
26
from bzrlib.commands import register_command
32
from bzrlib.workingtree import CONFLICT_SUFFIXES
27
from bzrlib.errors import BzrCommandError, NotConflicted, UnsupportedOperation
28
from bzrlib.option import Option
29
from bzrlib.osutils import rename, delete_any
30
from bzrlib.rio import Stanza
33
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
34
36
class cmd_conflicts(bzrlib.commands.Command):
35
37
"""List files with conflicts.
39
Merge will do its best to combine the changes in two branches, but there
40
are some kinds of problems only a human can fix. When it encounters those,
41
it will mark a conflict. A conflict means that you need to fix something,
42
before you should commit.
44
Use bzr resolve when you have fixed a problem.
36
46
(conflicts are determined by the presence of .BASE .TREE, and .OTHER
40
for path in Branch.open_containing(u'.')[0].working_tree().iter_conflicts():
52
from bzrlib.workingtree import WorkingTree
53
wt = WorkingTree.open_containing(u'.')[0]
54
for conflict in wt.conflicts():
43
58
class cmd_resolve(bzrlib.commands.Command):
44
59
"""Mark a conflict as resolved.
61
Merge will do its best to combine the changes in two branches, but there
62
are some kinds of problems only a human can fix. When it encounters those,
63
it will mark a conflict. A conflict means that you need to fix something,
64
before you should commit.
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
70
See also bzr conflicts.
46
72
aliases = ['resolved']
47
73
takes_args = ['file*']
48
takes_options = ['all']
74
takes_options = [Option('all', help='Resolve all conflicts in this tree')]
49
75
def run(self, file_list=None, all=False):
52
raise BzrCommandError(
53
"command 'resolve' needs one or more FILE, or --all")
54
tree = Branch.open_containing(u'.')[0].working_tree()
55
file_list = list(tree.abspath(f) for f in tree.iter_conflicts())
58
raise BzrCommandError(
59
"If --all is specified, no FILE may be provided")
60
for filename in file_list:
62
for suffix in CONFLICT_SUFFIXES:
64
os.unlink(filename+suffix)
66
if e.errno != errno.ENOENT:
70
if failures == len(CONFLICT_SUFFIXES):
71
if not os.path.exists(filename):
72
print "%s does not exist" % filename
74
print "%s is not conflicted" % filename
76
from bzrlib.workingtree import WorkingTree
79
raise BzrCommandError("If --all is specified, no FILE may be provided")
80
tree = WorkingTree.open_containing('.')[0]
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)
90
def resolve(tree, paths=None, ignore_misses=False):
93
tree_conflicts = tree.conflicts()
95
new_conflicts = ConflictList()
96
selected_conflicts = tree_conflicts
98
new_conflicts, selected_conflicts = \
99
tree_conflicts.select_conflicts(tree, paths, ignore_misses)
101
tree.set_conflicts(new_conflicts)
102
except UnsupportedOperation:
104
selected_conflicts.remove_files(tree)
76
109
def restore(filename):
100
133
if not conflicted:
101
134
raise NotConflicted(filename)
137
class ConflictList(object):
138
"""List of conflicts.
140
Typically obtained from WorkingTree.conflicts()
142
Can be instantiated from stanzas or from Conflict subclasses.
145
def __init__(self, conflicts=None):
146
object.__init__(self)
147
if conflicts is None:
150
self.__list = conflicts
153
return len(self.__list) == 0
156
return len(self.__list)
159
return iter(self.__list)
161
def __getitem__(self, key):
162
return self.__list[key]
164
def append(self, conflict):
165
return self.__list.append(conflict)
167
def __eq__(self, other_list):
168
return list(self) == list(other_list)
170
def __ne__(self, other_list):
171
return not (self == other_list)
174
return "ConflictList(%r)" % self.__list
177
def from_stanzas(stanzas):
178
"""Produce a new ConflictList from an iterable of stanzas"""
179
conflicts = ConflictList()
180
for stanza in stanzas:
181
conflicts.append(Conflict.factory(**stanza.as_dict()))
184
def to_stanzas(self):
185
"""Generator of stanzas"""
186
for conflict in self:
187
yield conflict.as_stanza()
189
def to_strings(self):
190
"""Generate strings for the provided conflicts"""
191
for conflict in self:
194
def remove_files(self, tree):
195
"""Remove the THIS, BASE and OTHER files for listed conflicts"""
196
for conflict in self:
197
if not conflict.has_files:
199
for suffix in CONFLICT_SUFFIXES:
201
delete_any(tree.abspath(conflict.path+suffix))
203
if e.errno != errno.ENOENT:
206
def select_conflicts(self, tree, paths, ignore_misses=False):
207
"""Select the conflicts associated with paths in a tree.
209
File-ids are also used for this.
210
:return: a pair of ConflictLists: (not_selected, selected)
212
path_set = set(paths)
214
selected_paths = set()
215
new_conflicts = ConflictList()
216
selected_conflicts = ConflictList()
218
file_id = tree.path2id(path)
219
if file_id is not None:
222
for conflict in self:
224
for key in ('path', 'conflict_path'):
225
cpath = getattr(conflict, key, None)
228
if cpath in path_set:
230
selected_paths.add(cpath)
231
for key in ('file_id', 'conflict_file_id'):
232
cfile_id = getattr(conflict, key, None)
236
cpath = ids[cfile_id]
240
selected_paths.add(cpath)
242
selected_conflicts.append(conflict)
244
new_conflicts.append(conflict)
245
if ignore_misses is not True:
246
for path in [p for p in paths if p not in selected_paths]:
247
if not os.path.exists(tree.abspath(path)):
248
print "%s does not exist" % path
250
print "%s is not conflicted" % path
251
return new_conflicts, selected_conflicts
254
class Conflict(object):
255
"""Base class for all types of conflict"""
259
def __init__(self, path, file_id=None):
261
self.file_id = file_id
264
s = Stanza(type=self.typestring, path=self.path)
265
if self.file_id is not None:
266
s.add('file_id', self.file_id)
270
return [type(self), self.path, self.file_id]
272
def __cmp__(self, other):
273
if getattr(other, "_cmp_list", None) is None:
275
return cmp(self._cmp_list(), other._cmp_list())
278
return hash((type(self), self.path, self.file_id))
280
def __eq__(self, other):
281
return self.__cmp__(other) == 0
283
def __ne__(self, other):
284
return not self.__eq__(other)
287
return self.format % self.__dict__
290
rdict = dict(self.__dict__)
291
rdict['class'] = self.__class__.__name__
292
return self.rformat % rdict
295
def factory(type, **kwargs):
297
return ctype[type](**kwargs)
300
def sort_key(conflict):
301
if conflict.path is not None:
302
return conflict.path, conflict.typestring
303
elif getattr(conflict, "conflict_path", None) is not None:
304
return conflict.conflict_path, conflict.typestring
306
return None, conflict.typestring
309
class PathConflict(Conflict):
310
"""A conflict was encountered merging file paths"""
312
typestring = 'path conflict'
314
format = 'Path conflict: %(path)s / %(conflict_path)s'
316
rformat = '%(class)s(%(path)r, %(conflict_path)r, %(file_id)r)'
317
def __init__(self, path, conflict_path=None, file_id=None):
318
Conflict.__init__(self, path, file_id)
319
self.conflict_path = conflict_path
322
s = Conflict.as_stanza(self)
323
if self.conflict_path is not None:
324
s.add('conflict_path', self.conflict_path)
328
class ContentsConflict(PathConflict):
329
"""The files are of different types, or not present"""
333
typestring = 'contents conflict'
335
format = 'Contents conflict in %(path)s'
338
class TextConflict(PathConflict):
339
"""The merge algorithm could not resolve all differences encountered."""
343
typestring = 'text conflict'
345
format = 'Text conflict in %(path)s'
348
class HandledConflict(Conflict):
349
"""A path problem that has been provisionally resolved.
350
This is intended to be a base class.
353
rformat = "%(class)s(%(action)r, %(path)r, %(file_id)r)"
355
def __init__(self, action, path, file_id=None):
356
Conflict.__init__(self, path, file_id)
360
return Conflict._cmp_list(self) + [self.action]
363
s = Conflict.as_stanza(self)
364
s.add('action', self.action)
368
class HandledPathConflict(HandledConflict):
369
"""A provisionally-resolved path problem involving two paths.
370
This is intended to be a base class.
373
rformat = "%(class)s(%(action)r, %(path)r, %(conflict_path)r,"\
374
" %(file_id)r, %(conflict_file_id)r)"
376
def __init__(self, action, path, conflict_path, file_id=None,
377
conflict_file_id=None):
378
HandledConflict.__init__(self, action, path, file_id)
379
self.conflict_path = conflict_path
380
self.conflict_file_id = conflict_file_id
383
return HandledConflict._cmp_list(self) + [self.conflict_path,
384
self.conflict_file_id]
387
s = HandledConflict.as_stanza(self)
388
s.add('conflict_path', self.conflict_path)
389
if self.conflict_file_id is not None:
390
s.add('conflict_file_id', self.conflict_file_id)
395
class DuplicateID(HandledPathConflict):
396
"""Two files want the same file_id."""
398
typestring = 'duplicate id'
400
format = 'Conflict adding id to %(conflict_path)s. %(action)s %(path)s.'
403
class DuplicateEntry(HandledPathConflict):
404
"""Two directory entries want to have the same name."""
406
typestring = 'duplicate'
408
format = 'Conflict adding file %(conflict_path)s. %(action)s %(path)s.'
411
class ParentLoop(HandledPathConflict):
412
"""An attempt to create an infinitely-looping directory structure.
413
This is rare, but can be produced like so:
422
typestring = 'parent loop'
424
format = 'Conflict moving %(conflict_path)s into %(path)s. %(action)s.'
427
class UnversionedParent(HandledConflict):
428
"""An attempt to version an file whose parent directory is not versioned.
429
Typically, the result of a merge where one tree unversioned the directory
430
and the other added a versioned file to it.
433
typestring = 'unversioned parent'
435
format = 'Conflict adding versioned files to %(path)s. %(action)s.'
438
class MissingParent(HandledConflict):
439
"""An attempt to add files to a directory that is not present.
440
Typically, the result of a merge where one tree deleted the directory and
441
the other added a file to it.
444
typestring = 'missing parent'
446
format = 'Conflict adding files to %(path)s. %(action)s.'
453
def register_types(*conflict_types):
454
"""Register a Conflict subclass for serialization purposes"""
456
for conflict_type in conflict_types:
457
ctype[conflict_type.typestring] = conflict_type
460
register_types(ContentsConflict, TextConflict, PathConflict, DuplicateID,
461
DuplicateEntry, ParentLoop, UnversionedParent, MissingParent,)