1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2005-2010 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
55
54
def transform_tree(from_tree, to_tree, interesting_ids=None):
56
55
from_tree.lock_tree_write()
57
operation = cleanup.OperationWithCleanups(merge_inner)
56
operation = OperationWithCleanups(merge_inner)
58
57
operation.add_cleanup(from_tree.unlock)
59
58
operation.run_simple(from_tree.branch, to_tree, from_tree,
60
59
ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
63
62
class MergeHooks(hooks.Hooks):
65
64
def __init__(self):
66
hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
67
self.add_hook('merge_file_content',
65
hooks.Hooks.__init__(self)
66
self.create_hook(hooks.HookPoint('merge_file_content',
68
67
"Called with a bzrlib.merge.Merger object to create a per file "
69
68
"merge object when starting a merge. "
70
69
"Should return either None or a subclass of "
93
92
"""Attempt to merge the contents of a single file.
95
94
:param merge_params: A bzrlib.merge.MergeHookParams
96
:return: A tuple of (status, chunks), where status is one of
95
:return : A tuple of (status, chunks), where status is one of
97
96
'not_applicable', 'success', 'conflicted', or 'delete'. If status
98
97
is 'success' or 'conflicted', then chunks should be an iterable of
99
98
strings for the new file contents.
459
458
@deprecated_method(deprecated_in((2, 1, 0)))
460
459
def file_revisions(self, file_id):
461
460
self.ensure_revision_trees()
461
def get_id(tree, file_id):
462
revision_id = tree.inventory[file_id].revision
462
464
if self.this_rev_id is None:
463
465
if self.this_basis_tree.get_file_sha1(file_id) != \
464
466
self.this_tree.get_file_sha1(file_id):
465
467
raise errors.WorkingTreeNotRevision(self.this_tree)
467
469
trees = (self.this_basis_tree, self.other_tree)
468
return [tree.get_file_revision(file_id) for tree in trees]
470
return [get_id(tree, file_id) for tree in trees]
470
472
@deprecated_method(deprecated_in((2, 1, 0)))
471
473
def check_basis(self, check_clean, require_commits=True):
499
501
def _add_parent(self):
500
502
new_parents = self.this_tree.get_parent_ids() + [self.other_rev_id]
501
503
new_parent_trees = []
502
operation = cleanup.OperationWithCleanups(
503
self.this_tree.set_parent_trees)
504
operation = OperationWithCleanups(self.this_tree.set_parent_trees)
504
505
for revision_id in new_parents:
506
507
tree = self.revision_tree(revision_id)
565
566
def _maybe_fetch(self, source, target, revision_id):
566
567
if not source.repository.has_same_location(target.repository):
567
target.fetch(source, revision_id)
569
tags_to_fetch = set(source.tags.get_reverse_tag_dict())
570
except errors.TagsNotSupported:
572
fetch_spec = _mod_graph.NotInOtherForRevs(target.repository,
573
source.repository, [revision_id],
574
if_present_ids=tags_to_fetch).execute()
575
target.fetch(source, fetch_spec=fetch_spec)
569
577
def find_base(self):
570
578
revisions = [_mod_revision.ensure_null(self.this_basis),
702
710
def do_merge(self):
703
operation = cleanup.OperationWithCleanups(self._do_merge_to)
711
operation = OperationWithCleanups(self._do_merge_to)
704
712
self.this_tree.lock_tree_write()
705
713
operation.add_cleanup(self.this_tree.unlock)
706
714
if self.base_tree is not None:
712
720
merge = operation.run_simple()
713
721
if len(merge.cooked_conflicts) == 0:
714
722
if not self.ignore_zero and not trace.is_quiet():
715
trace.note(gettext("All changes applied successfully."))
723
trace.note("All changes applied successfully.")
717
trace.note(gettext("%d conflicts encountered.")
725
trace.note("%d conflicts encountered."
718
726
% len(merge.cooked_conflicts))
720
728
return len(merge.cooked_conflicts)
812
820
warnings.warn("pb argument to Merge3Merger is deprecated")
814
822
def do_merge(self):
815
operation = cleanup.OperationWithCleanups(self._do_merge)
823
operation = OperationWithCleanups(self._do_merge)
816
824
self.this_tree.lock_tree_write()
817
825
operation.add_cleanup(self.this_tree.unlock)
818
826
self.base_tree.lock_read()
835
843
def make_preview_transform(self):
836
operation = cleanup.OperationWithCleanups(self._make_preview_transform)
844
operation = OperationWithCleanups(self._make_preview_transform)
837
845
self.base_tree.lock_read()
838
846
operation.add_cleanup(self.base_tree.unlock)
839
847
self.other_tree.lock_read()
859
867
self.active_hooks = [hook for hook in hooks if hook is not None]
860
868
for num, (file_id, changed, parents3, names3,
861
869
executable3) in enumerate(entries):
862
child_pb.update(gettext('Preparing file merge'), num, len(entries))
870
child_pb.update('Preparing file merge', num, len(entries))
863
871
self._merge_names(file_id, parents3, names3, resolver=resolver)
865
873
file_status = self._do_merge_contents(file_id)
869
877
executable3, file_status, resolver=resolver)
871
879
child_pb.finished()
872
self.tt.fixup_new_roots()
873
881
self._finish_computing_transform()
875
883
def _finish_computing_transform(self):
889
897
self.tt.iter_changes(), self.change_reporter)
890
898
self.cook_conflicts(fs_conflicts)
891
899
for conflict in self.cooked_conflicts:
892
trace.warning(unicode(conflict))
900
trace.warning(conflict)
894
902
def _entries3(self):
895
903
"""Gather data about files modified between three trees.
904
912
iterator = self.other_tree.iter_changes(self.base_tree,
905
specific_files=self.interesting_files,
913
include_unchanged=True, specific_files=self.interesting_files,
906
914
extra_trees=[self.this_tree])
907
915
this_entries = dict((e.file_id, e) for p, e in
908
916
self.this_tree.iter_entries_by_dir(
934
942
it then compares with THIS and BASE.
936
944
For the multi-valued entries, the format will be (BASE, [lca1, lca2])
938
:return: [(file_id, changed, parents, names, executable)], where:
940
* file_id: Simple file_id of the entry
941
* changed: Boolean, True if the kind or contents changed else False
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,
945
:return: [(file_id, changed, parents, names, executable)]
946
file_id Simple file_id of the entry
947
changed Boolean, True if the kind or contents changed
949
parents ((base, [parent_id, in, lcas]), parent_id_other,
951
names ((base, [name, in, lcas]), name_in_other, name_in_this)
952
executable ((base, [exec, in, lcas]), exec_in_other, exec_in_this)
948
954
if self.interesting_files is not None:
949
955
lookup_trees = [self.this_tree, self.base_tree]
992
998
lca_entries.append(lca_ie)
994
if base_inventory.has_id(file_id):
1000
if file_id in base_inventory:
995
1001
base_ie = base_inventory[file_id]
997
1003
base_ie = _none_entry
999
if this_inventory.has_id(file_id):
1005
if file_id in this_inventory:
1000
1006
this_ie = this_inventory[file_id]
1002
1008
this_ie = _none_entry
1107
1112
other_root = self.tt.trans_id_file_id(other_root_file_id)
1108
1113
if other_root == self.tt.root:
1110
if self.this_tree.inventory.has_id(
1111
self.other_tree.inventory.root.file_id):
1112
# the other tree's root is a non-root in the current tree (as
1113
# when a previously unrelated branch is merged into another)
1115
if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
1116
# the other tree's root is a non-root in the current tree (as when
1117
# a previously unrelated branch is merged into another)
1115
1119
if self.tt.final_kind(other_root) is not None:
1116
1120
other_root_is_present = True
1121
1125
# 'other_tree.inventory.root' is not present in this tree. We are
1122
1126
# calling adjust_path for children which *want* to be present with a
1123
1127
# correct place to go.
1124
for _, child in self.other_tree.inventory.root.children.iteritems():
1128
for thing, child in self.other_tree.inventory.root.children.iteritems():
1125
1129
trans_id = self.tt.trans_id_file_id(child.file_id)
1126
1130
if not other_root_is_present:
1127
1131
if self.tt.final_kind(trans_id) is not None:
1129
1133
# to go already.
1131
1135
# Move the item into the root
1133
final_name = self.tt.final_name(trans_id)
1134
except errors.NoFinalPath:
1135
# This file is not present anymore, ignore it.
1137
self.tt.adjust_path(final_name, self.tt.root, trans_id)
1136
self.tt.adjust_path(self.tt.final_name(trans_id),
1137
self.tt.root, trans_id)
1138
1138
if other_root_is_present:
1139
1139
self.tt.cancel_creation(other_root)
1140
1140
self.tt.cancel_versioning(other_root)
1169
1169
def contents_sha1(tree, file_id):
1170
1170
"""Determine the sha1 of the file contents (used as a key method)."""
1171
if not tree.has_id(file_id):
1171
if file_id not in tree:
1173
1173
return tree.get_file_sha1(file_id)
1319
1319
self._raw_conflicts.append(('path conflict', trans_id, file_id,
1320
1320
this_parent, this_name,
1321
1321
other_parent, other_name))
1322
if not self.other_tree.has_id(file_id):
1322
if other_name is None:
1323
1323
# it doesn't matter whether the result was 'other' or
1324
# 'conflict'-- if it has no file id, we leave it alone.
1324
# 'conflict'-- if there's no 'other', we leave it alone.
1326
1326
parent_id = parents[self.winner_idx[parent_id_winner]]
1327
name = names[self.winner_idx[name_winner]]
1328
if parent_id is not None or name is not None:
1327
if parent_id is not None:
1329
1328
# if we get here, name_winner and parent_winner are set to safe
1331
if parent_id is None and name is not None:
1332
# if parent_id is None and name is non-None, current file is
1334
if names[self.winner_idx[parent_id_winner]] != '':
1335
raise AssertionError(
1336
'File looks like a root, but named %s' %
1337
names[self.winner_idx[parent_id_winner]])
1338
parent_trans_id = transform.ROOT_PARENT
1340
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1341
self.tt.adjust_path(name, parent_trans_id,
1330
self.tt.adjust_path(names[self.winner_idx[name_winner]],
1331
self.tt.trans_id_file_id(parent_id),
1342
1332
self.tt.trans_id_file_id(file_id))
1344
1334
def _do_merge_contents(self, file_id):
1345
1335
"""Performs a merge on file_id contents."""
1346
1336
def contents_pair(tree):
1347
if not tree.has_id(file_id):
1337
if file_id not in tree:
1348
1338
return (None, None)
1349
1339
kind = tree.kind(file_id)
1350
1340
if kind == "file":
1619
1609
def cook_conflicts(self, fs_conflicts):
1620
1610
"""Convert all conflicts into a form that doesn't depend on trans_id"""
1621
content_conflict_file_ids = set()
1622
cooked_conflicts = transform.cook_conflicts(fs_conflicts, self.tt)
1611
self.cooked_conflicts.extend(transform.cook_conflicts(
1612
fs_conflicts, self.tt))
1623
1613
fp = transform.FinalPaths(self.tt)
1624
1614
for conflict in self._raw_conflicts:
1625
1615
conflict_type = conflict[0]
1636
1626
if other_parent is None or other_name is None:
1637
1627
other_path = '<deleted>'
1639
if other_parent == self.other_tree.get_root_id():
1640
# The tree transform doesn't know about the other root,
1641
# so we special case here to avoid a NoFinalPath
1645
parent_path = fp.get_path(
1646
self.tt.trans_id_file_id(other_parent))
1629
parent_path = fp.get_path(
1630
self.tt.trans_id_file_id(other_parent))
1647
1631
other_path = osutils.pathjoin(parent_path, other_name)
1648
1632
c = _mod_conflicts.Conflict.factory(
1649
1633
'path conflict', path=this_path,
1662
1646
c = _mod_conflicts.Conflict.factory(conflict_type,
1663
1647
path=path, file_id=file_id)
1664
content_conflict_file_ids.add(file_id)
1665
1648
elif conflict_type == 'text conflict':
1666
1649
trans_id = conflict[1]
1667
1650
path = fp.get_path(trans_id)
1670
1653
path=path, file_id=file_id)
1672
1655
raise AssertionError('bad conflict type: %r' % (conflict,))
1673
cooked_conflicts.append(c)
1675
self.cooked_conflicts = []
1676
# We want to get rid of path conflicts when a corresponding contents
1677
# conflict exists. This can occur when one branch deletes a file while
1678
# the other renames *and* modifies it. In this case, the content
1679
# conflict is enough.
1680
for c in cooked_conflicts:
1681
if (c.typestring == 'path conflict'
1682
and c.file_id in content_conflict_file_ids):
1684
1656
self.cooked_conflicts.append(c)
1685
1657
self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1907
1879
entries = self._entries_to_incorporate()
1908
1880
entries = list(entries)
1909
1881
for num, (entry, parent_id) in enumerate(entries):
1910
child_pb.update(gettext('Preparing file merge'), num, len(entries))
1882
child_pb.update('Preparing file merge', num, len(entries))
1911
1883
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1912
1884
trans_id = transform.new_by_entry(self.tt, entry,
1913
1885
parent_trans_id, self.other_tree)
1931
1903
name_in_target = osutils.basename(self._target_subdir)
1932
1904
merge_into_root = subdir.copy()
1933
1905
merge_into_root.name = name_in_target
1934
if self.this_tree.inventory.has_id(merge_into_root.file_id):
1906
if merge_into_root.file_id in self.this_tree.inventory:
1935
1907
# Give the root a new file-id.
1936
1908
# This can happen fairly easily if the directory we are
1937
1909
# incorporating is the root, and both trees have 'TREE_ROOT' as
1975
1947
if this_tree is None:
1976
1948
raise errors.BzrError("bzrlib.merge.merge_inner requires a this_tree "
1949
"parameter as of bzrlib version 0.8.")
1978
1950
merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1979
1951
pb=pb, change_reporter=change_reporter)
1980
1952
merger.backup_files = backup_files
2434
2406
class _PlanLCAMerge(_PlanMergeBase):
2436
2408
This merge algorithm differs from _PlanMerge in that:
2438
2409
1. comparisons are done against LCAs only
2439
2410
2. cases where a contested line is new versus one LCA but old versus
2440
2411
another are marked as conflicts, by emitting the line as conflicted-a
2482
2453
If a line is killed and new, this indicates that the two merge
2483
2454
revisions contain differing conflict resolutions.
2485
2455
:param revision_id: The id of the revision in which the lines are
2487
2457
:param unique_line_numbers: The line numbers of unique lines.
2488
:return: a tuple of (new_this, killed_other)
2458
:return a tuple of (new_this, killed_other):