4
from changeset import Inventory, apply_changeset, invert_dict
5
from bzrlib.osutils import backup_file, rename
6
from bzrlib.merge3 import Merge3
8
from bzrlib.atomicfile import AtomicFile
9
from changeset import get_contents
13
"""Contents-change wrapper around merge3.Merge3"""
14
def __init__(self, file_id, base, other, show_base=False, reprocess=False):
15
self.file_id = file_id
18
self.show_base = show_base
19
self.reprocess = reprocess
21
def is_creation(self):
24
def is_deletion(self):
27
def __eq__(self, other):
28
if not isinstance(other, ApplyMerge3):
30
return (self.base == other.base and
31
self.other == other.other and self.file_id == other.file_id)
33
def __ne__(self, other):
34
return not (self == other)
36
def apply(self, filename, conflict_handler, reverse=False):
37
new_file = filename+".new"
45
if self.file_id not in tree:
46
raise Exception("%s not in tree" % self.file_id)
48
return tree.get_file(self.file_id).readlines()
49
base_lines = get_lines(base)
50
other_lines = get_lines(other)
51
m3 = Merge3(base_lines, file(filename, "rb").readlines(), other_lines)
54
output_file = file(new_file, "wb")
55
start_marker = "!START OF MERGE CONFLICT!" + "I HOPE THIS IS UNIQUE"
56
if self.show_base is True:
60
for line in m3.merge_lines(name_a = "TREE", name_b = "MERGE-SOURCE",
61
name_base = "BASE-REVISION",
62
start_marker=start_marker, base_marker=base_marker,
63
reprocess = self.reprocess):
64
if line.startswith(start_marker):
66
output_file.write(line.replace(start_marker, '<' * 7))
68
output_file.write(line)
71
os.chmod(new_file, os.stat(filename).st_mode)
72
rename(new_file, filename)
75
conflict_handler.merge_conflict(new_file, filename, base_lines,
79
"""Contents-change wrapper around weave merge"""
81
def __init__(self, weave, this_revision_id, other_revision_id):
83
self.this_revision_id = this_revision_id
84
self.other_revision_id = other_revision_id
86
def is_creation(self):
89
def is_deletion(self):
92
def __eq__(self, other):
93
if not isinstance(other, WeaveMerge):
95
return self.weave == other.weave and\
96
self.this_revision_id == other.this_revision_id and\
97
self.other_revision_id == other.other_revision_id
99
def __ne__(self, other):
100
return not (self == other)
102
def apply(self, filename, conflict_handler, reverse=False):
103
this_i = self.weave.lookup(self.this_revision_id)
104
other_i = self.weave.lookup(self.other_revision_id)
105
plan = self.weave.plan_merge(this_i, other_i)
106
lines = self.weave.weave_merge(plan)
108
out_file = AtomicFile(filename, mode='wb')
110
if line == '<<<<<<<\n':
114
conflict_handler.weave_merge_conflict(filename, self.weave,
119
class BackupBeforeChange:
120
"""Contents-change wrapper to back up file first"""
121
def __init__(self, contents_change):
122
self.contents_change = contents_change
124
def is_creation(self):
125
return self.contents_change.is_creation()
127
def is_deletion(self):
128
return self.contents_change.is_deletion()
130
def __eq__(self, other):
131
if not isinstance(other, BackupBeforeChange):
133
return (self.contents_change == other.contents_change)
135
def __ne__(self, other):
136
return not (self == other)
138
def apply(self, filename, conflict_handler, reverse=False):
139
backup_file(filename)
140
self.contents_change.apply(filename, conflict_handler, reverse)
143
def invert_invent(inventory):
145
for file_id in inventory:
146
path = inventory.id2path(file_id)
151
invert_invent[file_id] = path
155
def merge_flex(this, base, other, changeset_function, inventory_function,
156
conflict_handler, merge_factory, interesting_ids):
157
cset = changeset_function(base, other, interesting_ids)
158
new_cset = make_merge_changeset(cset, this, base, other,
159
conflict_handler, merge_factory)
160
result = apply_changeset(new_cset, invert_invent(this.inventory),
161
this.basedir, conflict_handler, False)
165
def make_merge_changeset(cset, this, base, other,
166
conflict_handler, merge_factory):
167
new_cset = changeset.Changeset()
169
for entry in cset.entries.itervalues():
170
if entry.is_boring():
171
new_cset.add_entry(entry)
173
new_entry = make_merged_entry(entry, this, base, other,
175
new_contents = make_merged_contents(entry, this, base, other,
178
new_entry.contents_change = new_contents
179
new_entry.metadata_change = make_merged_metadata(entry, base, other)
180
new_cset.add_entry(new_entry)
184
class ThreeWayConflict(Exception):
185
def __init__(self, this, base, other):
189
msg = "Conflict merging %s %s and %s" % (this, base, other)
190
Exception.__init__(self, msg)
192
def threeway_select(this, base, other):
193
"""Returns a value selected by the three-way algorithm.
194
Raises ThreewayConflict if the algorithm yields a conflict"""
202
raise ThreeWayConflict(this, base, other)
205
def make_merged_entry(entry, this, base, other, conflict_handler):
206
from bzrlib.trace import mutter
207
def entry_data(file_id, tree):
208
assert hasattr(tree, "__contains__"), "%s" % tree
209
if not tree.has_or_had_id(file_id):
210
return (None, None, "")
211
entry = tree.inventory[file_id]
212
my_dir = tree.id2path(entry.parent_id)
215
return entry.name, entry.parent_id, my_dir
216
this_name, this_parent, this_dir = entry_data(entry.id, this)
217
base_name, base_parent, base_dir = entry_data(entry.id, base)
218
other_name, other_parent, other_dir = entry_data(entry.id, other)
219
mutter("Dirs: this, base, other %r %r %r", this_dir, base_dir, other_dir)
220
mutter("Names: this, base, other %r %r %r", this_name, base_name, other_name)
223
new_name = threeway_select(this_name, base_name, other_name)
224
except ThreeWayConflict:
225
new_name = conflict_handler.rename_conflict(entry.id, this_name,
226
base_name, other_name)
228
old_parent = this_parent
230
new_parent = threeway_select(this_parent, base_parent, other_parent)
231
except ThreeWayConflict:
232
new_parent = conflict_handler.move_conflict(entry.id, this_dir,
234
def get_path(name, parent):
237
assert parent is None
239
parent_dir = {this_parent: this_dir, other_parent: other_dir,
240
base_parent: base_dir}
241
directory = parent_dir[parent]
242
return os.path.join(directory, name)
244
assert parent is None
247
old_path = get_path(old_name, old_parent)
249
new_entry = changeset.ChangesetEntry(entry.id, old_parent, old_path)
250
new_entry.new_path = get_path(new_name, new_parent)
251
new_entry.new_parent = new_parent
252
mutter(repr(new_entry))
256
def make_merged_contents(entry, this, base, other, conflict_handler,
258
contents = entry.contents_change
262
this_path = this.id2abspath(entry.id)
266
if this_path is None:
267
return conflict_handler.missing_for_merge(entry.id,
268
other.id2path(entry.id))
269
return merge_factory(entry.id, base, other)
271
if isinstance(contents, changeset.ReplaceContents):
272
base_contents = contents.old_contents
273
other_contents = contents.new_contents
274
if base_contents is None and other_contents is None:
276
if other_contents is None:
277
this_contents = get_contents(this, entry.id)
278
if this_path is not None and bzrlib.osutils.lexists(this_path):
279
if this_contents != base_contents:
280
return conflict_handler.rem_contents_conflict(this_path,
281
this_contents, base_contents)
285
elif base_contents is None:
286
if this_path is None or not bzrlib.osutils.lexists(this_path):
289
this_contents = get_contents(this, entry.id)
290
if this_contents == other_contents:
293
conflict_handler.new_contents_conflict(this_path,
295
elif isinstance(base_contents, changeset.TreeFileCreate) and \
296
isinstance(other_contents, changeset.TreeFileCreate):
299
this_contents = get_contents(this, entry.id)
300
if this_contents == base_contents:
302
elif this_contents == other_contents:
304
elif base_contents == other_contents:
307
conflict_handler.threeway_contents_conflict(this_path,
313
def make_merged_metadata(entry, base, other):
314
metadata = entry.metadata_change
317
assert isinstance(metadata, changeset.ChangeExecFlag)
318
if metadata.new_exec_flag is None:
320
elif metadata.old_exec_flag is None:
323
return ExecFlagMerge(base, other, entry.id)
326
class ExecFlagMerge(object):
327
def __init__(self, base_tree, other_tree, file_id):
328
self.base_tree = base_tree
329
self.other_tree = other_tree
330
self.file_id = file_id
332
def apply(self, filename, conflict_handler, reverse=False):
334
base = self.base_tree
335
other = self.other_tree
337
base = self.other_tree
338
other = self.base_tree
339
base_exec_flag = base.is_executable(self.file_id)
340
other_exec_flag = other.is_executable(self.file_id)
341
this_mode = os.stat(filename).st_mode
342
this_exec_flag = bool(this_mode & 0111)
343
if (base_exec_flag != other_exec_flag and
344
this_exec_flag != other_exec_flag):
345
assert this_exec_flag == base_exec_flag
346
current_mode = os.stat(filename).st_mode
350
to_mode = current_mode | (0100 & ~umask)
351
# Enable x-bit for others only if they can read it.
352
if current_mode & 0004:
353
to_mode |= 0001 & ~umask
354
if current_mode & 0040:
355
to_mode |= 0010 & ~umask
357
to_mode = current_mode & ~0111
358
os.chmod(filename, to_mode)