1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005-2011 Canonical Ltd
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
54
54
def transform_tree(from_tree, to_tree, interesting_ids=None):
55
55
from_tree.lock_tree_write()
56
operation = OperationWithCleanups(merge_inner)
56
operation = cleanup.OperationWithCleanups(merge_inner)
57
57
operation.add_cleanup(from_tree.unlock)
58
58
operation.run_simple(from_tree.branch, to_tree, from_tree,
59
59
ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
62
62
class MergeHooks(hooks.Hooks):
64
64
def __init__(self):
65
hooks.Hooks.__init__(self)
66
self.create_hook(hooks.HookPoint('merge_file_content',
65
hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
66
self.add_hook('merge_file_content',
67
67
"Called with a bzrlib.merge.Merger object to create a per file "
68
68
"merge object when starting a merge. "
69
69
"Should return either None or a subclass of "
92
92
"""Attempt to merge the contents of a single file.
94
94
:param merge_params: A bzrlib.merge.MergeHookParams
95
:return : A tuple of (status, chunks), where status is one of
95
:return: A tuple of (status, chunks), where status is one of
96
96
'not_applicable', 'success', 'conflicted', or 'delete'. If status
97
97
is 'success' or 'conflicted', then chunks should be an iterable of
98
98
strings for the new file contents.
458
458
@deprecated_method(deprecated_in((2, 1, 0)))
459
459
def file_revisions(self, file_id):
460
460
self.ensure_revision_trees()
461
def get_id(tree, file_id):
462
revision_id = tree.inventory[file_id].revision
464
461
if self.this_rev_id is None:
465
462
if self.this_basis_tree.get_file_sha1(file_id) != \
466
463
self.this_tree.get_file_sha1(file_id):
467
464
raise errors.WorkingTreeNotRevision(self.this_tree)
469
466
trees = (self.this_basis_tree, self.other_tree)
470
return [get_id(tree, file_id) for tree in trees]
467
return [tree.get_file_revision(file_id) for tree in trees]
472
469
@deprecated_method(deprecated_in((2, 1, 0)))
473
470
def check_basis(self, check_clean, require_commits=True):
501
498
def _add_parent(self):
502
499
new_parents = self.this_tree.get_parent_ids() + [self.other_rev_id]
503
500
new_parent_trees = []
504
operation = OperationWithCleanups(self.this_tree.set_parent_trees)
501
operation = cleanup.OperationWithCleanups(
502
self.this_tree.set_parent_trees)
505
503
for revision_id in new_parents:
507
505
tree = self.revision_tree(revision_id)
703
701
def do_merge(self):
704
operation = OperationWithCleanups(self._do_merge_to)
702
operation = cleanup.OperationWithCleanups(self._do_merge_to)
705
703
self.this_tree.lock_tree_write()
706
704
operation.add_cleanup(self.this_tree.unlock)
707
705
if self.base_tree is not None:
813
811
warnings.warn("pb argument to Merge3Merger is deprecated")
815
813
def do_merge(self):
816
operation = OperationWithCleanups(self._do_merge)
814
operation = cleanup.OperationWithCleanups(self._do_merge)
817
815
self.this_tree.lock_tree_write()
818
816
operation.add_cleanup(self.this_tree.unlock)
819
817
self.base_tree.lock_read()
836
834
def make_preview_transform(self):
837
operation = OperationWithCleanups(self._make_preview_transform)
835
operation = cleanup.OperationWithCleanups(self._make_preview_transform)
838
836
self.base_tree.lock_read()
839
837
operation.add_cleanup(self.base_tree.unlock)
840
838
self.other_tree.lock_read()
870
868
executable3, file_status, resolver=resolver)
872
870
child_pb.finished()
871
self.tt.fixup_new_roots()
874
872
self._finish_computing_transform()
876
874
def _finish_computing_transform(self):
890
888
self.tt.iter_changes(), self.change_reporter)
891
889
self.cook_conflicts(fs_conflicts)
892
890
for conflict in self.cooked_conflicts:
893
trace.warning(conflict)
891
trace.warning(unicode(conflict))
895
893
def _entries3(self):
896
894
"""Gather data about files modified between three trees.
905
903
iterator = self.other_tree.iter_changes(self.base_tree,
906
include_unchanged=True, specific_files=self.interesting_files,
904
specific_files=self.interesting_files,
907
905
extra_trees=[self.this_tree])
908
906
this_entries = dict((e.file_id, e) for p, e in
909
907
self.this_tree.iter_entries_by_dir(
935
933
it then compares with THIS and BASE.
937
935
For the multi-valued entries, the format will be (BASE, [lca1, lca2])
938
:return: [(file_id, changed, parents, names, executable)]
939
file_id Simple file_id of the entry
940
changed Boolean, True if the kind or contents changed
942
parents ((base, [parent_id, in, lcas]), parent_id_other,
944
names ((base, [name, in, lcas]), name_in_other, name_in_this)
945
executable ((base, [exec, in, lcas]), exec_in_other, exec_in_this)
937
:return: [(file_id, changed, parents, names, executable)], where:
939
* file_id: Simple file_id of the entry
940
* changed: Boolean, True if the kind or contents changed else False
941
* parents: ((base, [parent_id, in, lcas]), parent_id_other,
943
* names: ((base, [name, in, lcas]), name_in_other, name_in_this)
944
* executable: ((base, [exec, in, lcas]), exec_in_other,
947
947
if self.interesting_files is not None:
948
948
lookup_trees = [self.this_tree, self.base_tree]
991
991
lca_entries.append(lca_ie)
993
if file_id in base_inventory:
993
if base_inventory.has_id(file_id):
994
994
base_ie = base_inventory[file_id]
996
996
base_ie = _none_entry
998
if file_id in this_inventory:
998
if this_inventory.has_id(file_id):
999
999
this_ie = this_inventory[file_id]
1001
1001
this_ie = _none_entry
1105
1106
other_root = self.tt.trans_id_file_id(other_root_file_id)
1106
1107
if other_root == self.tt.root:
1108
if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
1109
# the other tree's root is a non-root in the current tree (as when
1110
# a previously unrelated branch is merged into another)
1109
if self.this_tree.inventory.has_id(
1110
self.other_tree.inventory.root.file_id):
1111
# the other tree's root is a non-root in the current tree (as
1112
# when a previously unrelated branch is merged into another)
1112
1114
if self.tt.final_kind(other_root) is not None:
1113
1115
other_root_is_present = True
1118
1120
# 'other_tree.inventory.root' is not present in this tree. We are
1119
1121
# calling adjust_path for children which *want* to be present with a
1120
1122
# correct place to go.
1121
for thing, child in self.other_tree.inventory.root.children.iteritems():
1123
for _, child in self.other_tree.inventory.root.children.iteritems():
1122
1124
trans_id = self.tt.trans_id_file_id(child.file_id)
1123
1125
if not other_root_is_present:
1124
1126
if self.tt.final_kind(trans_id) is not None:
1126
1128
# to go already.
1128
1130
# Move the item into the root
1129
self.tt.adjust_path(self.tt.final_name(trans_id),
1130
self.tt.root, trans_id)
1132
final_name = self.tt.final_name(trans_id)
1133
except errors.NoFinalPath:
1134
# This file is not present anymore, ignore it.
1136
self.tt.adjust_path(final_name, self.tt.root, trans_id)
1131
1137
if other_root_is_present:
1132
1138
self.tt.cancel_creation(other_root)
1133
1139
self.tt.cancel_versioning(other_root)
1162
1168
def contents_sha1(tree, file_id):
1163
1169
"""Determine the sha1 of the file contents (used as a key method)."""
1164
if file_id not in tree:
1170
if not tree.has_id(file_id):
1166
1172
return tree.get_file_sha1(file_id)
1312
1318
self._raw_conflicts.append(('path conflict', trans_id, file_id,
1313
1319
this_parent, this_name,
1314
1320
other_parent, other_name))
1315
if other_name is None:
1321
if not self.other_tree.has_id(file_id):
1316
1322
# it doesn't matter whether the result was 'other' or
1317
# 'conflict'-- if there's no 'other', we leave it alone.
1323
# 'conflict'-- if it has no file id, we leave it alone.
1319
1325
parent_id = parents[self.winner_idx[parent_id_winner]]
1320
if parent_id is not None:
1326
name = names[self.winner_idx[name_winner]]
1327
if parent_id is not None or name is not None:
1321
1328
# if we get here, name_winner and parent_winner are set to safe
1323
self.tt.adjust_path(names[self.winner_idx[name_winner]],
1324
self.tt.trans_id_file_id(parent_id),
1330
if parent_id is None and name is not None:
1331
# if parent_id is None and name is non-None, current file is
1333
if names[self.winner_idx[parent_id_winner]] != '':
1334
raise AssertionError(
1335
'File looks like a root, but named %s' %
1336
names[self.winner_idx[parent_id_winner]])
1337
parent_trans_id = transform.ROOT_PARENT
1339
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1340
self.tt.adjust_path(name, parent_trans_id,
1325
1341
self.tt.trans_id_file_id(file_id))
1327
1343
def _do_merge_contents(self, file_id):
1328
1344
"""Performs a merge on file_id contents."""
1329
1345
def contents_pair(tree):
1330
if file_id not in tree:
1346
if not tree.has_id(file_id):
1331
1347
return (None, None)
1332
1348
kind = tree.kind(file_id)
1333
1349
if kind == "file":
1602
1618
def cook_conflicts(self, fs_conflicts):
1603
1619
"""Convert all conflicts into a form that doesn't depend on trans_id"""
1604
self.cooked_conflicts.extend(transform.cook_conflicts(
1605
fs_conflicts, self.tt))
1620
content_conflict_file_ids = set()
1621
cooked_conflicts = transform.cook_conflicts(fs_conflicts, self.tt)
1606
1622
fp = transform.FinalPaths(self.tt)
1607
1623
for conflict in self._raw_conflicts:
1608
1624
conflict_type = conflict[0]
1645
1661
c = _mod_conflicts.Conflict.factory(conflict_type,
1646
1662
path=path, file_id=file_id)
1663
content_conflict_file_ids.add(file_id)
1647
1664
elif conflict_type == 'text conflict':
1648
1665
trans_id = conflict[1]
1649
1666
path = fp.get_path(trans_id)
1652
1669
path=path, file_id=file_id)
1654
1671
raise AssertionError('bad conflict type: %r' % (conflict,))
1672
cooked_conflicts.append(c)
1674
self.cooked_conflicts = []
1675
# We want to get rid of path conflicts when a corresponding contents
1676
# conflict exists. This can occur when one branch deletes a file while
1677
# the other renames *and* modifies it. In this case, the content
1678
# conflict is enough.
1679
for c in cooked_conflicts:
1680
if (c.typestring == 'path conflict'
1681
and c.file_id in content_conflict_file_ids):
1655
1683
self.cooked_conflicts.append(c)
1656
1684
self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1902
1930
name_in_target = osutils.basename(self._target_subdir)
1903
1931
merge_into_root = subdir.copy()
1904
1932
merge_into_root.name = name_in_target
1905
if merge_into_root.file_id in self.this_tree.inventory:
1933
if self.this_tree.inventory.has_id(merge_into_root.file_id):
1906
1934
# Give the root a new file-id.
1907
1935
# This can happen fairly easily if the directory we are
1908
1936
# incorporating is the root, and both trees have 'TREE_ROOT' as
1946
1974
if this_tree is None:
1947
1975
raise errors.BzrError("bzrlib.merge.merge_inner requires a this_tree "
1948
"parameter as of bzrlib version 0.8.")
1949
1977
merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1950
1978
pb=pb, change_reporter=change_reporter)
1951
1979
merger.backup_files = backup_files
2405
2433
class _PlanLCAMerge(_PlanMergeBase):
2407
2435
This merge algorithm differs from _PlanMerge in that:
2408
2437
1. comparisons are done against LCAs only
2409
2438
2. cases where a contested line is new versus one LCA but old versus
2410
2439
another are marked as conflicts, by emitting the line as conflicted-a
2452
2481
If a line is killed and new, this indicates that the two merge
2453
2482
revisions contain differing conflict resolutions.
2454
2484
:param revision_id: The id of the revision in which the lines are
2456
2486
:param unique_line_numbers: The line numbers of unique lines.
2457
:return a tuple of (new_this, killed_other):
2487
:return: a tuple of (new_this, killed_other)