256
260
ignore_zero - If true, suppress the "zero conflicts" message when
257
261
there are no conflicts; should be set when doing something we expect
258
262
to complete perfectly.
263
file_list - If true, merge only changes to selected files.
260
265
All available ancestors of other_revision and base_revision are
261
266
automatically pulled into the branch.
268
The revno may be -1 to indicate the last revision on the branch, which is the
271
This function is intended for use from the command line; programmatic clients
272
might prefer to call merge_inner(), which has less magic behavior.
263
tempdir = tempfile.mkdtemp(prefix="bzr-")
267
this_branch = Branch.open_containing(this_dir)[0]
268
this_rev_id = this_branch.last_revision()
269
if this_rev_id is None:
270
raise BzrCommandError("This branch has no commits")
272
changes = compare_trees(this_branch.working_tree(),
273
this_branch.basis_tree(), False)
274
if changes.has_changed():
275
raise BzrCommandError("Working tree has uncommitted changes.")
276
other_branch, other_tree = get_tree(other_revision, this_branch)
277
if other_revision[1] == -1:
278
other_rev_id = other_branch.last_revision()
279
if other_rev_id is None:
280
raise NoCommits(other_branch)
281
other_basis = other_rev_id
282
elif other_revision[1] is not None:
283
other_rev_id = other_branch.get_rev_id(other_revision[1])
284
other_basis = other_rev_id
287
other_basis = other_branch.last_revision()
288
if other_basis is None:
289
raise NoCommits(other_branch)
290
if base_revision == [None, None]:
292
base_rev_id = common_ancestor(this_rev_id, other_basis,
294
except NoCommonAncestor:
295
raise UnrelatedBranches()
296
base_tree = get_revid_tree(this_branch, base_rev_id, None)
297
base_is_ancestor = True
299
base_branch, base_tree = get_tree(base_revision)
300
if base_revision[1] == -1:
301
base_rev_id = base_branch.last_revision()
302
elif base_revision[1] is None:
305
base_rev_id = base_branch.get_rev_id(base_revision[1])
306
fetch(from_branch=base_branch, to_branch=this_branch)
307
base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
309
if file_list is None:
310
interesting_ids = None
312
interesting_ids = set()
313
this_tree = this_branch.working_tree()
314
for fname in file_list:
315
path = this_tree.relpath(fname)
317
for tree in (this_tree, base_tree, other_tree):
318
file_id = tree.inventory.path2id(path)
319
if file_id is not None:
320
interesting_ids.add(file_id)
323
raise BzrCommandError("%s is not a source file in any"
325
merge_inner(this_branch, other_tree, base_tree, tempdir,
326
ignore_zero=ignore_zero, backup_files=backup_files,
327
merge_type=merge_type, interesting_ids=interesting_ids)
328
if base_is_ancestor and other_rev_id is not None\
329
and other_rev_id not in this_branch.revision_history():
330
this_branch.add_pending_merge(other_rev_id)
332
shutil.rmtree(tempdir)
274
# TODO: please check this docstring is true and accurate - mbp 20051024
277
this_branch = Branch.open_containing(this_dir)[0]
278
this_rev_id = this_branch.last_revision()
279
if this_rev_id is None:
280
raise BzrCommandError("This branch has no commits")
282
changes = compare_trees(this_branch.working_tree(),
283
this_branch.basis_tree(), False)
284
if changes.has_changed():
285
raise BzrCommandError("Working tree has uncommitted changes.")
286
other_branch, other_tree = get_tree(other_revision, this_branch)
287
if other_revision[1] == -1:
288
other_rev_id = other_branch.last_revision()
289
if other_rev_id is None:
290
raise NoCommits(other_branch)
291
other_basis = other_rev_id
292
elif other_revision[1] is not None:
293
other_rev_id = other_branch.get_rev_id(other_revision[1])
294
other_basis = other_rev_id
297
other_basis = other_branch.last_revision()
298
if other_basis is None:
299
raise NoCommits(other_branch)
300
fetch(from_branch=other_branch, to_branch=this_branch)
301
if base_revision == [None, None]:
302
mutter("doing merge() with no base_revision specified")
304
base_rev_id = common_ancestor(this_rev_id, other_basis,
306
except NoCommonAncestor:
307
raise UnrelatedBranches()
308
# fetch() is probably unnecessary in this case, because
309
# get_revid_tree() does it anyway if base_rev_id is not None and the
310
# local_branch is None -- but we do it above just to be sure -- mbp 20051024
311
base_tree = get_revid_tree(this_branch, base_rev_id, None)
312
base_is_ancestor = True
314
mutter('doing merge() with base %r' % (base_revision,))
315
base_branch, base_tree = get_tree(base_revision)
316
if base_revision[1] == -1:
317
base_rev_id = base_branch.last_revision()
318
elif base_revision[1] is None:
321
base_rev_id = base_branch.get_rev_id(base_revision[1])
322
fetch(from_branch=base_branch, to_branch=this_branch)
323
base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
325
if file_list is None:
326
interesting_ids = None
328
interesting_ids = set()
329
this_tree = this_branch.working_tree()
330
for fname in file_list:
331
path = this_tree.relpath(fname)
333
for tree in (this_tree, base_tree, other_tree):
334
file_id = tree.inventory.path2id(path)
335
if file_id is not None:
336
interesting_ids.add(file_id)
339
raise BzrCommandError("%s is not a source file in any"
341
conflicts = merge_inner(this_branch, other_tree, base_tree, tempdir=None,
342
ignore_zero=ignore_zero,
343
backup_files=backup_files,
344
merge_type=merge_type,
345
interesting_ids=interesting_ids,
347
if base_is_ancestor and other_rev_id is not None\
348
and other_rev_id not in this_branch.revision_history():
349
this_branch.add_pending_merge(other_rev_id)
335
353
def set_interesting(inventory_a, inventory_b, interesting_ids):
340
358
source_file.interesting = source_file.id in interesting_ids
343
def merge_inner(this_branch, other_tree, base_tree, tempdir,
344
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
345
interesting_ids=None):
361
def merge_inner(this_branch, other_tree, base_tree, tempdir=None,
362
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
363
interesting_ids=None, show_base=False):
364
"""Primary interface for merging.
366
typical use is probably
367
'merge_inner(branch, branch.get_revision_tree(other_revision),
368
branch.get_revision_tree(base_revision))'
371
_tempdir = tempfile.mkdtemp(prefix="bzr-")
375
return _merge_inner(this_branch, other_tree, base_tree, _tempdir,
376
ignore_zero, merge_type, backup_files,
381
shutil.rmtree(_tempdir)
384
def _merge_inner(this_branch, other_tree, base_tree, user_tempdir,
385
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
386
interesting_ids=None, show_base=False):
347
387
def merge_factory(file_id, base, other):
348
contents_change = merge_type(file_id, base, other)
388
if show_base is True:
389
contents_change = merge_type(file_id, base, other, show_base=True)
391
contents_change = merge_type(file_id, base, other)
350
393
contents_change = BackupBeforeChange(contents_change)
351
394
return contents_change