1
from merge_core import merge_flex
2
from changeset import generate_changeset, ExceptionConflictHandler
3
from changeset import Inventory
4
from bzrlib import Branch
6
from trace import mutter
12
class MergeConflictHandler(ExceptionConflictHandler):
13
"""Handle conflicts encountered while merging"""
14
def copy(self, source, dest):
15
"""Copy the text and mode of a file
16
:param source: The path of the file to copy
17
:param dest: The distination file to create
19
s_file = file(source, "rb")
20
d_file = file(dest, "wb")
23
os.chmod(dest, 0777 & os.stat(source).st_mode)
25
def add_suffix(self, name, suffix, last_new_name=None):
26
"""Rename a file to append a suffix. If the new name exists, the
27
suffix is added repeatedly until a non-existant name is found
29
:param name: The path of the file
30
:param suffix: The suffix to append
31
:param last_new_name: (used for recursive calls) the last name tried
33
if last_new_name is None:
35
new_name = last_new_name+suffix
37
os.rename(name, new_name)
39
if e.errno != errno.EEXIST and e.errno != errno.ENOTEMPTY:
41
self.add_suffix(name, suffix, last_new_name=new_name)
43
def merge_conflict(self, new_file, this_path, base_path, other_path):
45
Handle diff3 conflicts by producing a .THIS, .BASE and .OTHER. The
46
main file will be a version with diff3 conflicts.
47
:param new_file: Path to the output file with diff3 markers
48
:param this_path: Path to the file text for the THIS tree
49
:param base_path: Path to the file text for the BASE tree
50
:param other_path: Path to the file text for the OTHER tree
52
self.add_suffix(this_path, ".THIS")
53
self.copy(base_path, this_path+".BASE")
54
self.copy(other_path, this_path+".OTHER")
55
os.rename(new_file, this_path)
57
def target_exists(self, entry, target, old_path):
58
"""Handle the case when the target file or dir exists"""
59
self.add_suffix(target, ".moved")
62
def __init__(self, path, id, present=None, isdir=None):
65
self.present = present
67
self.interesting = True
70
return "SourceFile(%s, %s)" % (self.path, self.id)
72
def get_tree(treespec, temp_root, label):
76
base_tree = branch.working_tree()
78
base_tree = branch.basis_tree()
80
base_tree = branch.revision_tree(branch.lookup_revision(revno))
81
temp_path = os.path.join(temp_root, label)
83
return MergeTree(base_tree, temp_path)
86
def abspath(tree, file_id):
87
path = tree.inventory.id2path(file_id)
92
def file_exists(tree, file_id):
93
return tree.has_filename(tree.id2path(file_id))
95
def inventory_map(tree):
97
for file_id in tree.inventory:
98
if not file_exists(tree, file_id):
100
path = abspath(tree, file_id)
101
inventory[path] = SourceFile(path, file_id)
105
class MergeTree(object):
106
def __init__(self, tree, tempdir):
107
object.__init__(self)
108
if hasattr(tree, "basedir"):
109
self.root = tree.basedir
112
self.inventory = inventory_map(tree)
114
self.tempdir = tempdir
115
os.mkdir(os.path.join(self.tempdir, "texts"))
118
def readonly_path(self, id):
119
if self.root is not None:
120
return self.tree.abspath(self.tree.id2path(id))
122
if self.tree.inventory[id].kind in ("directory", "root_directory"):
124
if not self.cached.has_key(id):
125
path = os.path.join(self.tempdir, "texts", id)
126
outfile = file(path, "wb")
127
outfile.write(self.tree.get_file(id).read())
128
assert(os.path.exists(path))
129
self.cached[id] = path
130
return self.cached[id]
132
def merge(other_revision, base_revision):
133
tempdir = tempfile.mkdtemp(prefix="bzr-")
135
this_branch = Branch('.')
136
other_tree = get_tree(other_revision, tempdir, "other")
137
base_tree = get_tree(base_revision, tempdir, "base")
138
merge_inner(this_branch, other_tree, base_tree, tempdir)
140
shutil.rmtree(tempdir)
143
def generate_cset_optimized(tree_a, tree_b, inventory_a, inventory_b):
144
"""Generate a changeset, using the text_id to mark really-changed files.
145
This permits blazing comparisons when text_ids are present. It also
146
disables metadata comparison for files with identical texts.
148
for file_id in tree_a.tree.inventory:
149
if file_id not in tree_b.tree.inventory:
151
entry_a = tree_a.tree.inventory[file_id]
152
entry_b = tree_b.tree.inventory[file_id]
153
if (entry_a.kind, entry_b.kind) != ("file", "file"):
155
if None in (entry_a.text_id, entry_b.text_id):
157
if entry_a.text_id != entry_b.text_id:
159
inventory_a[abspath(tree_a.tree, file_id)].interesting = False
160
inventory_b[abspath(tree_b.tree, file_id)].interesting = False
161
cset = generate_changeset(tree_a, tree_b, inventory_a, inventory_b)
162
for entry in cset.entries.itervalues():
163
entry.metadata_change = None
167
def merge_inner(this_branch, other_tree, base_tree, tempdir):
168
this_tree = get_tree(('.', None), tempdir, "this")
170
def get_inventory(tree):
171
return tree.inventory
173
inv_changes = merge_flex(this_tree, base_tree, other_tree,
174
generate_cset_optimized, get_inventory,
175
MergeConflictHandler(base_tree.root))
178
for id, path in inv_changes.iteritems():
183
assert path.startswith('./')
185
adjust_ids.append((path, id))
186
this_branch.set_inventory(regen_inventory(this_branch, this_tree.root, adjust_ids))
189
def regen_inventory(this_branch, root, new_entries):
190
old_entries = this_branch.read_working_inventory()
193
for file_id in old_entries:
194
entry = old_entries[file_id]
195
path = old_entries.id2path(file_id)
196
new_inventory[file_id] = (path, file_id, entry.parent_id, entry.kind)
197
by_path[path] = file_id
202
for path, file_id in new_entries:
204
del new_inventory[file_id]
207
new_path_list.append((path, file_id))
208
if file_id not in old_entries:
210
# Ensure no file is added before its parent
212
for path, file_id in new_path_list:
216
parent = by_path[os.path.dirname(path)]
217
kind = bzrlib.osutils.file_kind(os.path.join(root, path))
218
new_inventory[file_id] = (path, file_id, parent, kind)
219
by_path[path] = file_id
221
# Get a list in insertion order
222
new_inventory_list = new_inventory.values()
223
mutter ("""Inventory regeneration:
224
old length: %i insertions: %i deletions: %i new_length: %i"""\
225
% (len(old_entries), insertions, deletions, len(new_inventory_list)))
226
assert len(new_inventory_list) == len(old_entries) + insertions - deletions
227
new_inventory_list.sort()
228
return new_inventory_list