112
112
aliases = ['resolved']
113
113
takes_args = ['file*']
114
114
takes_options = [
115
116
option.Option('all', help='Resolve all conflicts in this tree.'),
116
117
ResolveActionOption(),
118
119
_see_also = ['conflicts']
119
def run(self, file_list=None, all=False, action=None):
120
def run(self, file_list=None, all=False, action=None, directory=u'.'):
122
123
raise errors.BzrCommandError("If --all is specified,"
123
124
" no FILE may be provided")
124
tree = workingtree.WorkingTree.open_containing('.')[0]
125
tree = workingtree.WorkingTree.open_containing(directory)[0]
125
126
if action is None:
128
tree, file_list = builtins.tree_files(file_list)
129
tree, file_list = workingtree.WorkingTree.open_containing_paths(
129
131
if file_list is None:
130
132
if action is None:
131
133
# FIXME: There is a special case here related to the option
435
437
def action_take_other(self, tree):
436
438
raise NotImplementedError(self.action_take_other)
440
def _resolve_with_cleanups(self, tree, *args, **kwargs):
441
tt = transform.TreeTransform(tree)
442
op = cleanup.OperationWithCleanups(self._resolve)
443
op.add_cleanup(tt.finalize)
444
op.run_simple(tt, *args, **kwargs)
439
447
class PathConflict(Conflict):
440
448
"""A conflict was encountered merging file paths"""
459
467
# No additional files have been generated here
470
def _resolve(self, tt, file_id, path, winner):
471
"""Resolve the conflict.
473
:param tt: The TreeTransform where the conflict is resolved.
474
:param file_id: The retained file id.
475
:param path: The retained path.
476
:param winner: 'this' or 'other' indicates which side is the winner.
478
path_to_create = None
480
if self.path == '<deleted>':
481
return # Nothing to do
482
if self.conflict_path == '<deleted>':
483
path_to_create = self.path
484
revid = tt._tree.get_parent_ids()[0]
485
elif winner == 'other':
486
if self.conflict_path == '<deleted>':
487
return # Nothing to do
488
if self.path == '<deleted>':
489
path_to_create = self.conflict_path
490
# FIXME: If there are more than two parents we may need to
491
# iterate. Taking the last parent is the safer bet in the mean
492
# time. -- vila 20100309
493
revid = tt._tree.get_parent_ids()[-1]
496
raise AssertionError('bad winner: %r' % (winner,))
497
if path_to_create is not None:
498
tid = tt.trans_id_tree_path(path_to_create)
499
transform.create_from_tree(
500
tt, tt.trans_id_tree_path(path_to_create),
501
self._revision_tree(tt._tree, revid), file_id)
502
tt.version_file(file_id, tid)
504
# Adjust the path for the retained file id
505
tid = tt.trans_id_file_id(file_id)
506
parent_tid = tt.get_tree_parent(tid)
507
tt.adjust_path(path, parent_tid, tid)
510
def _revision_tree(self, tree, revid):
511
return tree.branch.repository.revision_tree(revid)
513
def _infer_file_id(self, tree):
514
# Prior to bug #531967, file_id wasn't always set, there may still be
515
# conflict files in the wild so we need to cope with them
516
# Establish which path we should use to find back the file-id
518
for p in (self.path, self.conflict_path):
520
# special hard-coded path
523
possible_paths.append(p)
524
# Search the file-id in the parents with any path available
526
for revid in tree.get_parent_ids():
527
revtree = self._revision_tree(tree, revid)
528
for p in possible_paths:
529
file_id = revtree.path2id(p)
530
if file_id is not None:
531
return revtree, file_id
462
534
def action_take_this(self, tree):
463
tree.rename_one(self.conflict_path, self.path)
535
if self.file_id is not None:
536
self._resolve_with_cleanups(tree, self.file_id, self.path,
539
# Prior to bug #531967 we need to find back the file_id and restore
540
# the content from there
541
revtree, file_id = self._infer_file_id(tree)
542
tree.revert([revtree.id2path(file_id)],
543
old_tree=revtree, backups=False)
465
545
def action_take_other(self, tree):
466
# just acccept bzr proposal
546
if self.file_id is not None:
547
self._resolve_with_cleanups(tree, self.file_id,
551
# Prior to bug #531967 we need to find back the file_id and restore
552
# the content from there
553
revtree, file_id = self._infer_file_id(tree)
554
tree.revert([revtree.id2path(file_id)],
555
old_tree=revtree, backups=False)
470
558
class ContentsConflict(PathConflict):
471
"""The files are of different types, or not present"""
559
"""The files are of different types (or both binary), or not present"""
479
567
def associated_filenames(self):
480
568
return [self.path + suffix for suffix in ('.BASE', '.OTHER')]
482
# FIXME: I smell something weird here and it seems we should be able to be
483
# more coherent with some other conflict ? bzr *did* a choice there but
484
# neither action_take_this nor action_take_other reflect that...
570
def _resolve(self, tt, suffix_to_remove):
571
"""Resolve the conflict.
573
:param tt: The TreeTransform where the conflict is resolved.
574
:param suffix_to_remove: Either 'THIS' or 'OTHER'
576
The resolution is symmetric, when taking THIS, OTHER is deleted and
577
item.THIS is renamed into item and vice-versa.
580
# Delete 'item.THIS' or 'item.OTHER' depending on
583
tt.trans_id_tree_path(self.path + '.' + suffix_to_remove))
584
except errors.NoSuchFile:
585
# There are valid cases where 'item.suffix_to_remove' either
586
# never existed or was already deleted (including the case
587
# where the user deleted it)
589
# Rename 'item.suffix_to_remove' (note that if
590
# 'item.suffix_to_remove' has been deleted, this is a no-op)
591
this_tid = tt.trans_id_file_id(self.file_id)
592
parent_tid = tt.get_tree_parent(this_tid)
593
tt.adjust_path(self.path, parent_tid, this_tid)
486
596
def action_take_this(self, tree):
487
tree.remove([self.path + '.OTHER'], force=True, keep_files=False)
597
self._resolve_with_cleanups(tree, 'OTHER')
489
599
def action_take_other(self, tree):
490
tree.remove([self.path], force=True, keep_files=False)
600
self._resolve_with_cleanups(tree, 'THIS')
494
603
# FIXME: TextConflict is about a single file-id, there never is a conflict_path