~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

[merge] robert

Show diffs side-by-side

added added

removed removed

Lines of Context:
234
234
    eventually be done by just building the tree directly calling into 
235
235
    lower-level code (e.g. constructing a changeset).
236
236
    """
 
237
    # RBC 20051019 is this not just 'export' ?
237
238
    merge((to_dir, -1), (to_dir, 0), this_dir=to_dir,
238
239
          check_clean=False, ignore_zero=True)
239
240
 
260
261
    All available ancestors of other_revision and base_revision are
261
262
    automatically pulled into the branch.
262
263
    """
263
 
    tempdir = tempfile.mkdtemp(prefix="bzr-")
264
 
    try:
265
 
        if this_dir is None:
266
 
            this_dir = '.'
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")
271
 
        if check_clean:
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
285
 
        else:
286
 
            other_rev_id = None
287
 
            other_basis = other_branch.last_revision()
288
 
            if other_basis is None:
289
 
                raise NoCommits(other_branch)
290
 
        if base_revision == [None, None]:
291
 
            try:
292
 
                base_rev_id = common_ancestor(this_rev_id, other_basis, 
293
 
                                              this_branch)
294
 
            except NoCommonAncestor:
295
 
                raise UnrelatedBranches()
296
 
            base_tree = get_revid_tree(this_branch, base_rev_id, None)
297
 
            base_is_ancestor = True
298
 
        else:
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:
303
 
                base_rev_id = None
304
 
            else:
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,
308
 
                                           this_branch)
309
 
        if file_list is None:
310
 
            interesting_ids = None
311
 
        else:
312
 
            interesting_ids = set()
313
 
            this_tree = this_branch.working_tree()
314
 
            for fname in file_list:
315
 
                path = this_tree.relpath(fname)
316
 
                found_id = False
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)
321
 
                        found_id = True
322
 
                if not found_id:
323
 
                    raise BzrCommandError("%s is not a source file in any"
324
 
                                          " tree." % fname)
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)
331
 
    finally:
332
 
        shutil.rmtree(tempdir)
 
264
    if this_dir is None:
 
265
        this_dir = '.'
 
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")
 
270
    if check_clean:
 
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
 
284
    else:
 
285
        other_rev_id = None
 
286
        other_basis = other_branch.last_revision()
 
287
        if other_basis is None:
 
288
            raise NoCommits(other_branch)
 
289
    if base_revision == [None, None]:
 
290
        try:
 
291
            base_rev_id = common_ancestor(this_rev_id, other_basis, 
 
292
                                          this_branch)
 
293
        except NoCommonAncestor:
 
294
            raise UnrelatedBranches()
 
295
        base_tree = get_revid_tree(this_branch, base_rev_id, None)
 
296
        base_is_ancestor = True
 
297
    else:
 
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:
 
302
            base_rev_id = None
 
303
        else:
 
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,
 
307
                                       this_branch)
 
308
    if file_list is None:
 
309
        interesting_ids = None
 
310
    else:
 
311
        interesting_ids = set()
 
312
        this_tree = this_branch.working_tree()
 
313
        for fname in file_list:
 
314
            path = this_tree.relpath(fname)
 
315
            found_id = False
 
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)
 
320
                    found_id = True
 
321
            if not found_id:
 
322
                raise BzrCommandError("%s is not a source file in any"
 
323
                                      " tree." % fname)
 
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)
333
330
 
334
331
 
335
332
def set_interesting(inventory_a, inventory_b, interesting_ids):
340
337
             source_file.interesting = source_file.id in interesting_ids
341
338
 
342
339
 
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):
346
 
 
 
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. 
 
344
 
 
345
    typical use is probably 
 
346
    'merge_inner(branch, branch.get_revision_tree(other_revision),
 
347
                 branch.get_revision_tree(base_revision))'
 
348
    """
 
349
    if tempdir is None:
 
350
        _tempdir = tempfile.mkdtemp(prefix="bzr-")
 
351
    else:
 
352
        _tempdir = tempdir
 
353
    try:
 
354
        _merge_inner(this_branch, other_tree, base_tree, _tempdir,
 
355
                     ignore_zero, merge_type, backup_files, interesting_ids)
 
356
    finally:
 
357
        if tempdir is None:
 
358
            shutil.rmtree(_tempdir)
 
359
 
 
360
 
 
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):
347
364
    def merge_factory(file_id, base, other):
348
365
        contents_change = merge_type(file_id, base, other)
349
366
        if backup_files: