~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-03-02 18:38:07 UTC
  • mto: (147.4.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: abentley@panoramicfeedback.com-20060302183807-ce34e84d08b9e9a7
Switched to using checkouts instead of branch copies

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import errno
18
18
 
19
19
from bzrlib.bzrdir import BzrDir
 
20
import bzrlib.bzrdir as bzrdir
20
21
from bzrlib.errors import BzrError
21
22
from bzrlib.errors import NotBranchError, BzrCommandError, NoSuchRevision
22
23
from bzrlib.branch import Branch
23
24
from bzrlib.commit import Commit, NullCommitReporter
24
25
from bzrlib.commands import Command
25
26
from bzrlib.option import _global_option
 
27
from bzrlib.merge import merge_inner
 
28
from bzrlib.revision import NULL_REVISION
 
29
from bzrlib.tree import EmptyTree
26
30
import bzrlib.ui
27
31
from bzrlib.workingtree import WorkingTree
28
32
from errors import NoPyBaz
397
401
    tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
398
402
                               dir=os.path.dirname(output_dir))
399
403
    try:
 
404
        wt = WorkingTree.open(output_dir)
 
405
    except NotBranchError:
 
406
        wt = None
 
407
    if wt is None:
 
408
        old_basis = EmptyTree()
 
409
    else:
 
410
        old_basis = wt.basis_tree()
 
411
    try:
400
412
        if not fancy:
401
413
            print "not fancy"
402
414
        try:
416
428
        if dry_run:
417
429
            print 'Dry run, not modifying output_dir'
418
430
            return
419
 
        if os.path.exists(output_dir):
420
 
            # Move the bzr control directory back, and update the working tree
421
 
            revdir = os.path.join(tempdir, "rd")
422
 
            if os.path.exists(revdir):
423
 
                # actual imports were done
424
 
                tmp_bzr_dir = os.path.join(tempdir, '.bzr')
425
 
                
426
 
                bzr_dir = os.path.join(output_dir, '.bzr')
427
 
                new_bzr_dir = os.path.join(tempdir, "rd", '.bzr')
428
 
                
429
 
                # Move the original bzr out of the way    
430
 
                os.rename(bzr_dir, tmp_bzr_dir) 
431
 
                os.rename(new_bzr_dir, bzr_dir)
432
 
                try:
433
 
                    WorkingTree.open_containing(output_dir)[0].revert([])
434
 
                except:
435
 
                    # If something failed, move back the original bzr directory
436
 
                    os.rename(bzr_dir, new_bzr_dir)
437
 
                    os.rename(tmp_bzr_dir, bzr_dir)
438
 
                    raise
439
 
            else:
440
 
                # no imports - perhaps just append_revisions
441
 
                # should not fail:
442
 
                WorkingTree.open_containing(output_dir)[0].revert([])
443
 
        else:
444
 
            revdir = os.path.join(tempdir, "rd")
445
 
            os.rename(revdir, output_dir)
 
431
 
 
432
        # Update the working tree of the branch
 
433
        wt = WorkingTree.open(output_dir)
 
434
        wt.set_last_revision(wt.branch.last_revision())
 
435
        merge_inner(wt.branch, wt.basis_tree(), old_basis, ignore_zero=True,
 
436
                    this_tree=wt)
 
437
        wt.revert([])
446
438
 
447
439
    finally:
448
440
        printer('Cleaning up')
502
494
            return pybaz.Revision(revision_id[7:].replace('%', '/'))
503
495
        except pybaz.errors.NamespaceError, e:
504
496
            raise NotArchRevision(revision_id)
505
 
            
 
497
 
 
498
 
 
499
def create_checkout(source, to_location, revision_id=None):
 
500
    checkout = bzrdir.BzrDirMetaFormat1().initialize(to_location)
 
501
    bzrlib.branch.BranchReferenceFormat().initialize(checkout, source)
 
502
    return checkout.create_workingtree(revision_id)
 
503
 
 
504
 
 
505
def create_checkout_metadata(source, to_location, revision_id=None):
 
506
    if revision_id is None:
 
507
        revision_id = source.last_revision()
 
508
    wt = create_checkout(source, to_location, NULL_REVISION)
 
509
    wt.set_last_revision(revision_id)
 
510
    wt._write_inventory(wt.basis_tree().inventory)
 
511
    return wt
 
512
 
 
513
 
506
514
def iter_import_version(output_dir, ancestors, tempdir, pb, fast=False,
507
515
                        verbose=False, dry_run=False, max_count=None):
508
516
    revdir = None
523
531
 
524
532
    previous_version=None
525
533
    missing_ancestor = None
 
534
    if dry_run:
 
535
        dry_output_dir = os.path.join(tempdir, 'od')
 
536
        if os.path.exists(output_dir):
 
537
            shutil.copytree(output_dir, dry_output_dir)
 
538
        output_dir = dry_output_dir
 
539
 
 
540
    if os.path.exists(output_dir):
 
541
        target_branch = Branch.open(output_dir)
 
542
    else:
 
543
        target_branch = BzrDir.create_standalone_workingtree(output_dir).branch
526
544
 
527
545
    for i in range(len(ancestors)):
528
546
        revision = ancestors[i]
537
555
            previous_version = version
538
556
        else:
539
557
            yield Progress("revisions", i, len(ancestors))
540
 
        if revdir is None and os.path.exists(output_dir):
541
 
            # check for imported revisions and if present just append
542
 
            # immediately
543
 
            target_tree = WorkingTree.open(output_dir)
544
 
            branch = target_tree.branch
545
 
            if branch.repository.has_revision(rev_id):
546
 
                branch.append_revision(rev_id)
547
 
                continue
 
558
 
 
559
        if target_branch.repository.has_revision(rev_id):
 
560
            target_branch.append_revision(rev_id)
 
561
            continue
548
562
        if revdir is None:
549
563
            revdir = os.path.join(tempdir, "rd")
550
564
            try:
557
571
                print ("unable to access ancestor %s, making into a merge."
558
572
                       % missing_ancestor)
559
573
                continue
560
 
            if os.path.exists(output_dir):
561
 
                bzr_dir = os.path.join(output_dir, '.bzr')
562
 
                new_bzr_dir = os.path.join(tempdir, "rd", '.bzr')
563
 
                # This would be much faster with a simple os.rename(), but if
564
 
                # we fail, we have corrupted the original .bzr directory.  Is
565
 
                # that a big problem, as we can just back out the last
566
 
                # revisions in .bzr/revision_history I don't really know
567
 
                # RBC20051024 - yes, it would be a problem as we could not then
568
 
                # apply the corrupted revision.
569
 
                shutil.copytree(bzr_dir, new_bzr_dir)
570
 
                # Now revdir should have a tree with the latest .bzr, and the
571
 
                # next revision of the baz tree
572
 
                target_tree = WorkingTree.open(revdir)
573
 
                branch = target_tree.branch
574
 
            else:
575
 
                target_tree = BzrDir.create_standalone_workingtree(revdir)
576
 
                branch = target_tree.branch
 
574
            target_tree = create_checkout_metadata(target_branch, revdir)
 
575
            branch = target_tree.branch
577
576
        else:
578
577
            old = os.path.join(revdir, ".bzr")
579
578
            new = os.path.join(tempdir, ".bzr")
612
611
            commitobj.commit(working_tree=target_tree,
613
612
                             message=log_message.decode('ascii', 'replace'), 
614
613
                             verbose=False, committer=log_creator,
615
 
                             timestamp=timestamp, timezone=0, rev_id=rev_id)
 
614
                             timestamp=timestamp, timezone=0, rev_id=rev_id,
 
615
                             revprops={'branch-nick': str(revision.version)})
616
616
        finally:
617
617
            target_tree.unlock()
618
618
            branch.unlock()
619
619
    yield Progress("revisions", len(ancestors), len(ancestors))
620
 
    unlink_unversioned(target_tree)
621
620
 
622
621
def get_direct_merges(revdir, revision, log):
623
622
    continuation = log.continuation_of