435
436
def action_take_other(self, tree):
436
437
raise NotImplementedError(self.action_take_other)
439
def _resolve_with_cleanups(self, tree, *args, **kwargs):
440
tt = transform.TreeTransform(tree)
441
op = cleanup.OperationWithCleanups(self._resolve)
442
op.add_cleanup(tt.finalize)
443
op.run_simple(tt, *args, **kwargs)
439
446
class PathConflict(Conflict):
440
447
"""A conflict was encountered merging file paths"""
459
466
# No additional files have been generated here
469
def _resolve(self, tt, file_id, path, winner):
470
"""Resolve the conflict.
472
:param tt: The TreeTransform where the conflict is resolved.
473
:param file_id: The retained file id.
474
:param path: The retained path.
475
:param winner: 'this' or 'other' indicates which side is the winner.
477
path_to_create = None
479
if self.path == '<deleted>':
480
return # Nothing to do
481
if self.conflict_path == '<deleted>':
482
path_to_create = self.path
483
revid = tt._tree.get_parent_ids()[0]
484
elif winner == 'other':
485
if self.conflict_path == '<deleted>':
486
return # Nothing to do
487
if self.path == '<deleted>':
488
path_to_create = self.conflict_path
489
# FIXME: If there are more than two parents we may need to
490
# iterate. Taking the last parent is the safer bet in the mean
491
# time. -- vila 20100309
492
revid = tt._tree.get_parent_ids()[-1]
495
raise AssertionError('bad winner: %r' % (winner,))
496
if path_to_create is not None:
497
tid = tt.trans_id_tree_path(path_to_create)
498
transform.create_from_tree(
499
tt, tt.trans_id_tree_path(path_to_create),
500
self._revision_tree(tt._tree, revid), file_id)
501
tt.version_file(file_id, tid)
503
# Adjust the path for the retained file id
504
tid = tt.trans_id_file_id(file_id)
505
parent_tid = tt.get_tree_parent(tid)
506
tt.adjust_path(path, parent_tid, tid)
509
def _revision_tree(self, tree, revid):
510
return tree.branch.repository.revision_tree(revid)
512
def _infer_file_id(self, tree):
513
# Prior to bug #531967, file_id wasn't always set, there may still be
514
# conflict files in the wild so we need to cope with them
515
# Establish which path we should use to find back the file-id
517
for p in (self.path, self.conflict_path):
519
# special hard-coded path
522
possible_paths.append(p)
523
# Search the file-id in the parents with any path available
525
for revid in tree.get_parent_ids():
526
revtree = self._revision_tree(tree, revid)
527
for p in possible_paths:
528
file_id = revtree.path2id(p)
529
if file_id is not None:
530
return revtree, file_id
462
533
def action_take_this(self, tree):
463
tree.rename_one(self.conflict_path, self.path)
534
if self.file_id is not None:
535
self._resolve_with_cleanups(tree, self.file_id, self.path,
538
# Prior to bug #531967 we need to find back the file_id and restore
539
# the content from there
540
revtree, file_id = self._infer_file_id(tree)
541
tree.revert([revtree.id2path(file_id)],
542
old_tree=revtree, backups=False)
465
544
def action_take_other(self, tree):
466
# just acccept bzr proposal
545
if self.file_id is not None:
546
self._resolve_with_cleanups(tree, self.file_id,
550
# Prior to bug #531967 we need to find back the file_id and restore
551
# the content from there
552
revtree, file_id = self._infer_file_id(tree)
553
tree.revert([revtree.id2path(file_id)],
554
old_tree=revtree, backups=False)
470
557
class ContentsConflict(PathConflict):
471
"""The files are of different types, or not present"""
558
"""The files are of different types (or both binary), or not present"""
479
566
def associated_filenames(self):
480
567
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...
569
def _resolve(self, tt, suffix_to_remove):
570
"""Resolve the conflict.
572
:param tt: The TreeTransform where the conflict is resolved.
573
:param suffix_to_remove: Either 'THIS' or 'OTHER'
575
The resolution is symmetric, when taking THIS, OTHER is deleted and
576
item.THIS is renamed into item and vice-versa.
579
# Delete 'item.THIS' or 'item.OTHER' depending on
582
tt.trans_id_tree_path(self.path + '.' + suffix_to_remove))
583
except errors.NoSuchFile:
584
# There are valid cases where 'item.suffix_to_remove' either
585
# never existed or was already deleted (including the case
586
# where the user deleted it)
588
# Rename 'item.suffix_to_remove' (note that if
589
# 'item.suffix_to_remove' has been deleted, this is a no-op)
590
this_tid = tt.trans_id_file_id(self.file_id)
591
parent_tid = tt.get_tree_parent(this_tid)
592
tt.adjust_path(self.path, parent_tid, this_tid)
486
595
def action_take_this(self, tree):
487
tree.remove([self.path + '.OTHER'], force=True, keep_files=False)
596
self._resolve_with_cleanups(tree, 'OTHER')
489
598
def action_take_other(self, tree):
490
tree.remove([self.path], force=True, keep_files=False)
599
self._resolve_with_cleanups(tree, 'THIS')
494
602
# FIXME: TextConflict is about a single file-id, there never is a conflict_path