189
190
self.create(file_id, stem+".OTHER", self.other_tree)
190
191
self.create(file_id, stem+".BASE", self.base_tree)
192
def threeway_contents_conflict(filename, this_contents, base_contents,
194
self.conflict("Three-way conflict merging %s" % filename)
196
193
def finalize(self):
197
194
if not self.ignore_zero:
198
195
note("%d conflicts encountered.\n" % self.conflicts)
200
def get_tree(treespec, local_branch=None):
197
def get_tree(treespec, temp_root, label, local_branch=None):
201
198
location, revno = treespec
202
branch = Branch.open_containing(location)[0]
199
branch = Branch.open_containing(location)
203
200
if revno is None:
205
202
elif revno == -1:
206
203
revision = branch.last_revision()
208
205
revision = branch.get_rev_id(revno)
209
return branch, get_revid_tree(branch, revision, local_branch)
206
return branch, get_revid_tree(branch, revision, temp_root, label,
211
def get_revid_tree(branch, revision, local_branch):
209
def get_revid_tree(branch, revision, temp_root, label, local_branch):
212
210
if revision is None:
213
211
base_tree = branch.working_tree()
217
215
base_tree = local_branch.revision_tree(revision)
219
217
base_tree = branch.revision_tree(revision)
218
temp_path = os.path.join(temp_root, label)
220
return MergeAdapterTree(base_tree, temp_path)
223
223
def file_exists(tree, file_id):
224
224
return tree.has_filename(tree.id2path(file_id))
227
class MergeAdapterTree(object):
228
"""MergeAdapterTree adapts a normal tree for merge_inner to use.
230
The interface the merge_inner needs is nearly but not quite
231
the same as that of bzrlib.tree with the exception of readonly_path.
234
def __init__(self, tree, tempdir):
235
object.__init__(self)
236
if hasattr(tree, "basedir"):
237
self.root = tree.basedir
241
self.tempdir = tempdir
242
os.mkdir(os.path.join(self.tempdir, "texts"))
243
os.mkdir(os.path.join(self.tempdir, "symlinks"))
247
return self.tree.__iter__()
249
def __contains__(self, file_id):
250
return file_id in self.tree
252
def get_file(self, file_id):
253
return self.tree.get_file(file_id)
255
def get_file_sha1(self, id):
256
return self.tree.get_file_sha1(id)
258
def is_executable(self, id):
259
return self.tree.is_executable(id)
261
def id2path(self, file_id):
262
return self.tree.id2path(file_id)
264
def has_id(self, file_id):
265
return self.tree.has_id(file_id)
267
def has_or_had_id(self, file_id):
268
if file_id == self.tree.inventory.root.file_id:
270
return self.tree.inventory.has_id(file_id)
272
def has_or_had_id(self, file_id):
273
if file_id == self.tree.inventory.root.file_id:
275
return self.tree.inventory.has_id(file_id)
277
def readonly_path(self, id):
278
if id not in self.tree:
280
if self.root is not None:
281
return self.tree.abspath(self.tree.id2path(id))
283
kind = self.tree.inventory[id].kind
284
if kind in ("directory", "root_directory"):
286
if not self.cached.has_key(id):
288
path = os.path.join(self.tempdir, "texts", id)
289
outfile = file(path, "wb")
290
outfile.write(self.tree.get_file(id).read())
291
assert(bzrlib.osutils.lexists(path))
292
if self.tree.is_executable(id):
295
assert kind == "symlink"
296
path = os.path.join(self.tempdir, "symlinks", id)
297
target = self.tree.get_symlink_target(id)
298
os.symlink(target, path)
299
self.cached[id] = path
300
return self.cached[id]
227
303
def build_working_dir(to_dir):
228
304
"""Build a working directory in an empty directory.
261
336
All available ancestors of other_revision and base_revision are
262
337
automatically pulled into the branch.
266
this_branch = Branch.open_containing(this_dir)[0]
267
this_rev_id = this_branch.last_revision()
268
if this_rev_id is None:
269
raise BzrCommandError("This branch has no commits")
271
changes = compare_trees(this_branch.working_tree(),
272
this_branch.basis_tree(), False)
273
if changes.has_changed():
274
raise BzrCommandError("Working tree has uncommitted changes.")
275
other_branch, other_tree = get_tree(other_revision, this_branch)
276
if other_revision[1] == -1:
277
other_rev_id = other_branch.last_revision()
278
if other_rev_id is None:
279
raise NoCommits(other_branch)
280
other_basis = other_rev_id
281
elif other_revision[1] is not None:
282
other_rev_id = other_branch.get_rev_id(other_revision[1])
283
other_basis = other_rev_id
286
other_basis = other_branch.last_revision()
287
if other_basis is None:
288
raise NoCommits(other_branch)
289
if base_revision == [None, None]:
291
base_rev_id = common_ancestor(this_rev_id, other_basis,
293
except NoCommonAncestor:
294
raise UnrelatedBranches()
295
base_tree = get_revid_tree(this_branch, base_rev_id, None)
296
base_is_ancestor = True
298
base_branch, base_tree = get_tree(base_revision)
299
if base_revision[1] == -1:
300
base_rev_id = base_branch.last_revision()
301
elif base_revision[1] is None:
304
base_rev_id = base_branch.get_rev_id(base_revision[1])
305
fetch(from_branch=base_branch, to_branch=this_branch)
306
base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
308
if file_list is None:
309
interesting_ids = None
311
interesting_ids = set()
312
this_tree = this_branch.working_tree()
313
for fname in file_list:
314
path = this_tree.relpath(fname)
316
for tree in (this_tree, base_tree, other_tree):
317
file_id = tree.inventory.path2id(path)
318
if file_id is not None:
319
interesting_ids.add(file_id)
322
raise BzrCommandError("%s is not a source file in any"
324
merge_inner(this_branch, other_tree, base_tree, tempdir=None,
325
ignore_zero=ignore_zero, backup_files=backup_files,
326
merge_type=merge_type, interesting_ids=interesting_ids)
327
if base_is_ancestor and other_rev_id is not None\
328
and other_rev_id not in this_branch.revision_history():
329
this_branch.add_pending_merge(other_rev_id)
339
tempdir = tempfile.mkdtemp(prefix="bzr-")
343
this_branch = Branch.open_containing(this_dir)
344
this_rev_id = this_branch.last_revision()
345
if this_rev_id is None:
346
raise BzrCommandError("This branch has no commits")
348
changes = compare_trees(this_branch.working_tree(),
349
this_branch.basis_tree(), False)
350
if changes.has_changed():
351
raise BzrCommandError("Working tree has uncommitted changes.")
352
other_branch, other_tree = get_tree(other_revision, tempdir, "other",
354
if other_revision[1] == -1:
355
other_rev_id = other_branch.last_revision()
356
if other_rev_id is None:
357
raise NoCommits(other_branch)
358
other_basis = other_rev_id
359
elif other_revision[1] is not None:
360
other_rev_id = other_branch.get_rev_id(other_revision[1])
361
other_basis = other_rev_id
364
other_basis = other_branch.last_revision()
365
if other_basis is None:
366
raise NoCommits(other_branch)
367
if base_revision == [None, None]:
369
base_rev_id = common_ancestor(this_rev_id, other_basis,
371
except NoCommonAncestor:
372
raise UnrelatedBranches()
373
base_tree = get_revid_tree(this_branch, base_rev_id, tempdir,
375
base_is_ancestor = True
377
base_branch, base_tree = get_tree(base_revision, tempdir, "base")
378
if base_revision[1] == -1:
379
base_rev_id = base_branch.last_revision()
380
elif base_revision[1] is None:
383
base_rev_id = base_branch.get_rev_id(base_revision[1])
384
fetch(from_branch=base_branch, to_branch=this_branch)
385
base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
387
if file_list is None:
388
interesting_ids = None
390
interesting_ids = set()
391
this_tree = this_branch.working_tree()
392
for fname in file_list:
393
path = this_branch.relpath(fname)
395
for tree in (this_tree, base_tree.tree, other_tree.tree):
396
file_id = tree.inventory.path2id(path)
397
if file_id is not None:
398
interesting_ids.add(file_id)
401
raise BzrCommandError("%s is not a source file in any"
403
merge_inner(this_branch, other_tree, base_tree, tempdir,
404
ignore_zero=ignore_zero, backup_files=backup_files,
405
merge_type=merge_type, interesting_ids=interesting_ids)
406
if base_is_ancestor and other_rev_id is not None\
407
and other_rev_id not in this_branch.revision_history():
408
this_branch.add_pending_merge(other_rev_id)
410
shutil.rmtree(tempdir)
332
413
def set_interesting(inventory_a, inventory_b, interesting_ids):
337
418
source_file.interesting = source_file.id in interesting_ids
340
def merge_inner(this_branch, other_tree, base_tree, tempdir=None,
341
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
342
interesting_ids=None):
343
"""Primary interface for merging.
345
typical use is probably
346
'merge_inner(branch, branch.get_revision_tree(other_revision),
347
branch.get_revision_tree(base_revision))'
350
_tempdir = tempfile.mkdtemp(prefix="bzr-")
354
_merge_inner(this_branch, other_tree, base_tree, _tempdir,
355
ignore_zero, merge_type, backup_files, interesting_ids)
358
shutil.rmtree(_tempdir)
361
def _merge_inner(this_branch, other_tree, base_tree, user_tempdir,
362
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
363
interesting_ids=None):
421
def merge_inner(this_branch, other_tree, base_tree, tempdir,
422
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
423
interesting_ids=None):
364
425
def merge_factory(file_id, base, other):
365
426
contents_change = merge_type(file_id, base, other)
367
428
contents_change = BackupBeforeChange(contents_change)
368
429
return contents_change
370
this_tree = get_tree((this_branch.base, None))[1]
431
this_tree = get_tree((this_branch.base, None), tempdir, "this")[1]
372
433
def get_inventory(tree):
373
return tree.inventory
434
return tree.tree.inventory
375
436
inv_changes = merge_flex(this_tree, base_tree, other_tree,
376
437
generate_changeset, get_inventory,