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
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))
404
wt = WorkingTree.open(output_dir)
405
except NotBranchError:
408
old_basis = EmptyTree()
410
old_basis = wt.basis_tree()
401
413
print "not fancy"
417
429
print 'Dry run, not modifying output_dir'
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')
426
bzr_dir = os.path.join(output_dir, '.bzr')
427
new_bzr_dir = os.path.join(tempdir, "rd", '.bzr')
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)
433
WorkingTree.open_containing(output_dir)[0].revert([])
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)
440
# no imports - perhaps just append_revisions
442
WorkingTree.open_containing(output_dir)[0].revert([])
444
revdir = os.path.join(tempdir, "rd")
445
os.rename(revdir, output_dir)
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,
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)
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)
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)
506
514
def iter_import_version(output_dir, ancestors, tempdir, pb, fast=False,
507
515
verbose=False, dry_run=False, max_count=None):
524
532
previous_version=None
525
533
missing_ancestor = None
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
540
if os.path.exists(output_dir):
541
target_branch = Branch.open(output_dir)
543
target_branch = BzrDir.create_standalone_workingtree(output_dir).branch
527
545
for i in range(len(ancestors)):
528
546
revision = ancestors[i]
537
555
previous_version = version
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
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)
559
if target_branch.repository.has_revision(rev_id):
560
target_branch.append_revision(rev_id)
548
562
if revdir is None:
549
563
revdir = os.path.join(tempdir, "rd")
557
571
print ("unable to access ancestor %s, making into a merge."
558
572
% missing_ancestor)
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
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
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)})
617
617
target_tree.unlock()
619
619
yield Progress("revisions", len(ancestors), len(ancestors))
620
unlink_unversioned(target_tree)
622
621
def get_direct_merges(revdir, revision, log):
623
622
continuation = log.continuation_of