23
import bzrlib.revision
24
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
25
from bzrlib.merge_core import WeaveMerge
26
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
27
from bzrlib.changeset import Inventory, Diff3Merge, ReplaceContents
22
from bzrlib._changeset import generate_changeset, ExceptionConflictHandler
23
from bzrlib._changeset import Inventory, Diff3Merge, ReplaceContents
24
from bzrlib._merge_core import WeaveMerge
25
from bzrlib._merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
28
26
from bzrlib.branch import Branch
27
from bzrlib.delta import compare_trees
29
28
from bzrlib.errors import (BzrCommandError,
33
WorkingTreeNotRevision,
37
from bzrlib.delta import compare_trees
38
from bzrlib.trace import mutter, warning, note
36
WorkingTreeNotRevision,
39
38
from bzrlib.fetch import greedy_fetch, fetch
40
from bzrlib.revision import is_ancestor, NULL_REVISION
41
40
from bzrlib.osutils import rename, pathjoin
42
from bzrlib.revision import common_ancestor, MultipleRevisionSources
43
from bzrlib.errors import NoSuchRevision
41
from bzrlib.revision import common_ancestor, is_ancestor, NULL_REVISION
42
from bzrlib.trace import mutter, warning, note
45
44
# TODO: Report back as changes are merged in
47
# TODO: build_working_dir can be built on something simpler than merge()
49
# FIXME: merge() parameters seem oriented towards the command line
50
# NOTABUG: merge is a helper for commandline functions. merge_inner is the
51
# the core functionality.
53
46
# comments from abentley on irc: merge happens in two stages, each
54
47
# of which generates a changeset object
56
49
# stage 1: generate OLD->OTHER,
57
50
# stage 2: use MINE and OLD->OTHER to generate MINE -> RESULT
59
class MergeConflictHandler(ExceptionConflictHandler):
52
class _MergeConflictHandler(ExceptionConflictHandler):
60
53
"""Handle conflicts encountered while merging.
62
55
This subclasses ExceptionConflictHandler, so that any types of
237
230
revision = branch.get_rev_id(revno)
238
231
if revision is None:
239
232
revision = NULL_REVISION
240
return branch, get_revid_tree(branch, revision, local_branch)
242
def get_revid_tree(branch, revision, local_branch):
233
return branch, _get_revid_tree(branch, revision, local_branch)
236
def _get_revid_tree(branch, revision, local_branch):
243
237
if revision is None:
244
base_tree = branch.working_tree()
238
base_tree = branch.bzrdir.open_workingtree()
246
240
if local_branch is not None:
247
greedy_fetch(local_branch, branch, revision)
248
base_tree = local_branch.revision_tree(revision)
241
if local_branch.base != branch.base:
242
greedy_fetch(local_branch, branch, revision)
243
base_tree = local_branch.repository.revision_tree(revision)
250
base_tree = branch.revision_tree(revision)
245
base_tree = branch.repository.revision_tree(revision)
254
def file_exists(tree, file_id):
255
return tree.has_filename(tree.id2path(file_id))
258
def build_working_dir(to_dir):
259
"""Build a working directory in an empty directory.
261
to_dir is a directory containing branch metadata but no working files,
262
typically constructed by cloning an existing branch.
264
This is split out as a special idiomatic case of merge. It could
265
eventually be done by just building the tree directly calling into
266
lower-level code (e.g. constructing a changeset).
268
# RBC 20051019 is this not just 'export' ?
269
# AB Well, export doesn't take care of inventory...
270
this_branch = Branch.open_containing(to_dir)[0]
271
transform_tree(this_branch.working_tree(), this_branch.basis_tree())
274
249
def transform_tree(from_tree, to_tree, interesting_ids=None):
275
250
merge_inner(from_tree.branch, to_tree, from_tree, ignore_zero=True,
276
251
interesting_ids=interesting_ids)
279
def merge(other_revision, base_revision,
280
check_clean=True, ignore_zero=False,
281
this_dir=None, backup_files=False, merge_type=ApplyMerge3,
282
file_list=None, show_base=False, reprocess=False):
283
"""Merge changes into a tree.
286
list(path, revno) Base for three-way merge.
287
If [None, None] then a base will be automatically determined.
289
list(path, revno) Other revision for three-way merge.
291
Directory to merge changes into; '.' by default.
293
If true, this_dir must have no uncommitted changes before the
295
ignore_zero - If true, suppress the "zero conflicts" message when
296
there are no conflicts; should be set when doing something we expect
297
to complete perfectly.
298
file_list - If supplied, merge only changes to selected files.
300
All available ancestors of other_revision and base_revision are
301
automatically pulled into the branch.
303
The revno may be -1 to indicate the last revision on the branch, which is
306
This function is intended for use from the command line; programmatic
307
clients might prefer to call merge_inner(), which has less magic behavior.
311
this_branch = Branch.open_containing(this_dir)[0]
312
if show_base and not merge_type is ApplyMerge3:
313
raise BzrCommandError("Show-base is not supported for this merge"
314
" type. %s" % merge_type)
315
if reprocess and not merge_type is ApplyMerge3:
316
raise BzrCommandError("Reprocess is not supported for this merge"
317
" type. %s" % merge_type)
318
if reprocess and show_base:
319
raise BzrCommandError("Cannot reprocess and show base.")
320
merger = Merger(this_branch)
321
merger.check_basis(check_clean)
322
merger.set_other(other_revision)
323
merger.set_base(base_revision)
324
if merger.base_rev_id == merger.other_rev_id:
325
note('Nothing to do.')
327
merger.backup_files = backup_files
328
merger.merge_type = merge_type
329
merger.set_interesting_files(file_list)
330
merger.show_base = show_base
331
merger.reprocess = reprocess
332
merger.conflict_handler = MergeConflictHandler(merger.this_tree,
335
ignore_zero=ignore_zero)
336
conflicts = merger.do_merge()
340
254
def merge_inner(this_branch, other_tree, base_tree, ignore_zero=False,
341
255
backup_files=False,
342
256
merge_type=ApplyMerge3,
361
278
merger._set_interesting_files(interesting_files)
362
279
merger.show_base = show_base
363
280
merger.reprocess = reprocess
364
merger.conflict_handler = MergeConflictHandler(merger.this_tree, base_tree,
366
ignore_zero=ignore_zero)
281
merger.conflict_handler = _MergeConflictHandler(merger.this_tree,
282
base_tree, other_tree,
283
ignore_zero=ignore_zero)
367
284
merger.other_rev_id = other_rev_id
368
285
merger.other_basis = other_rev_id
369
286
return merger.do_merge()
372
289
class Merger(object):
373
def __init__(self, this_branch, other_tree=None, base_tree=None):
290
def __init__(self, this_branch, other_tree=None, base_tree=None, this_tree=None):
374
291
object.__init__(self)
292
assert this_tree is not None, "this_tree is required"
375
293
self.this_branch = this_branch
376
294
self.this_basis = this_branch.last_revision()
377
295
self.this_rev_id = None
378
self.this_tree = this_branch.working_tree()
296
self.this_tree = this_tree
379
297
self.this_revision_tree = None
380
298
self.this_basis_tree = None
381
299
self.other_tree = other_tree
385
303
self.interesting_ids = None
386
304
self.show_base = False
387
305
self.reprocess = False
388
self.conflict_handler = MergeConflictHandler(self.this_tree, base_tree,
306
self.conflict_handler = _MergeConflictHandler(self.this_tree,
307
base_tree, other_tree)
391
309
def revision_tree(self, revision_id):
392
return self.this_branch.revision_tree(revision_id)
310
return self.this_branch.repository.revision_tree(revision_id)
394
312
def ensure_revision_trees(self):
395
313
if self.this_revision_tree is None:
396
self.this_basis_tree = self.this_branch.revision_tree(
314
self.this_basis_tree = self.this_branch.repository.revision_tree(
398
316
if self.this_basis == self.this_rev_id:
399
317
self.this_revision_tree = self.this_basis_tree
402
319
if self.other_rev_id is None:
403
320
other_basis_tree = self.revision_tree(self.other_basis)
404
321
changes = compare_trees(self.other_tree, other_basis_tree)