356
def import_version(output_dir, version, printer, fancy=True, fast=False,
360
def import_version(output_dir, version, encoding, fast=False,
357
361
verbose=False, dry_run=False, max_count=None,
358
362
reuse_history_from=[], standalone=True):
360
364
>>> q = test_environ()
366
Progress bars output to stderr, but doctest does not capture that.
368
>>> old_stderr = sys.stderr
369
>>> sys.stderr = sys.stdout
361
371
>>> result_path = os.path.join(q, "result")
362
372
>>> commit_test_revisions()
363
373
>>> version = pybaz.Version("test@example.com/test--test--0.1")
364
>>> def printer(message): print message
365
>>> import_version('/', version, printer, fancy=False, dry_run=True)
374
>>> old_ui = bzrlib.ui.ui_factory
375
>>> bzrlib.ui.ui_factory = bzrlib.ui.text.TextUIFactory(
376
... bar_type=bzrlib.progress.DotsProgressBar)
378
>>> import_version('/', version, None, dry_run=True)
366
379
Traceback (most recent call last):
367
380
NotPreviousImport: / is not the location of a previous import.
368
>>> import_version(result_path, version, printer, fancy=False, dry_run=True)
381
>>> import_version(result_path, version, None, dry_run=True)
369
382
Traceback (most recent call last):
370
383
UserError: The version test@example.com/test--test--0.1 does not exist.
371
384
>>> version = pybaz.Version("test@example.com/test--test--0")
372
>>> import_version(result_path, version, printer, fancy=False, dry_run=True)
385
>>> import_version(result_path, version, None, dry_run=True) #doctest: +ELLIPSIS
386
importing test@example.com/test--test--0 into ...
388
revisions: ..........................................
375
389
Dry run, not modifying output_dir
377
>>> import_version(result_path, version, printer, fancy=False)
391
>>> import_version(result_path, version, None) #doctest: +ELLIPSIS
392
importing test@example.com/test--test--0 into ...
394
revisions: .....................................................................
382
>>> import_version(result_path, version, printer, fancy=False)
397
>>> import_version(result_path, version, None) #doctest: +ELLIPSIS
383
398
Tree is up-to-date with test@example.com/test--test--0--patch-2
384
399
>>> commit_more_test_revisions()
385
>>> import_version(result_path, version, printer, fancy=False)
400
>>> import_version(result_path, version, None) #doctest: +ELLIPSIS
401
importing test@example.com/test--test--0 into ...
402
revisions: ....................................................
405
>>> bzrlib.ui.ui_factory = old_ui
406
>>> sys.stderr = old_stderr
390
407
>>> teardown_environ(q)
393
ancestors, old_revno = get_remaining_revisions(output_dir, version,
395
except NotBranchError, e:
396
raise NotPreviousImport(e.path)
397
if old_revno is None and len(ancestors) == 0:
398
print 'Version %s has no revisions.' % version
400
if len(ancestors) == 0:
401
last_revision = get_last_revision(Branch.open(output_dir))
402
print 'Tree is up-to-date with %s' % last_revision
405
409
progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
406
tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
407
dir=os.path.dirname(output_dir))
409
wt = WorkingTree.open(output_dir)
410
except (NotBranchError, NoWorkingTree):
413
old_basis = EmptyTree()
415
old_basis = wt.basis_tree()
420
for result in iter_import_version(output_dir, ancestors, tempdir,
421
progress_bar, fast=fast, verbose=verbose, dry_run=dry_run,
422
max_count=max_count, standalone=standalone):
424
show_progress(progress_bar, result)
426
sys.stdout.write('.')
429
progress_bar.finished()
431
sys.stdout.write('\n')
434
print 'Dry run, not modifying output_dir'
437
# Update the working tree of the branch
412
ancestors, old_revno = get_remaining_revisions(output_dir, version,
415
except NotBranchError, e:
416
raise NotPreviousImport(e.path)
417
if old_revno is None and len(ancestors) == 0:
418
progress_bar.note('Version %s has no revisions.' % version)
420
if len(ancestors) == 0:
421
last_revision, last_encoding = \
422
get_last_revision(Branch.open(output_dir))
423
progress_bar.note('Tree is up-to-date with %s' % last_revision)
426
progress_bar.note("importing %s into %s" % (version, output_dir))
428
tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
429
dir=os.path.dirname(output_dir))
439
431
wt = WorkingTree.open(output_dir)
440
except NoWorkingTree:
432
except (NotBranchError, NoWorkingTree):
443
wt.set_last_revision(wt.branch.last_revision())
444
merge_inner(wt.branch, wt.basis_tree(), old_basis,
445
ignore_zero=True, this_tree=wt)
435
for result in iter_import_version(output_dir, ancestors, tempdir,
436
pb=progress_bar, encoding=encoding, fast=fast,
437
verbose=verbose, dry_run=dry_run, max_count=max_count,
438
standalone=standalone):
439
show_progress(progress_bar, result)
441
progress_bar.note('Dry run, not modifying output_dir')
444
# Update the working tree of the branch
446
wt = WorkingTree.open(output_dir)
447
except NoWorkingTree:
450
wt.set_last_revision(wt.branch.last_revision())
451
wt.set_root_id(BAZ_IMPORT_ROOT)
456
progress_bar.note('Cleaning up')
457
shutil.rmtree(tempdir)
458
progress_bar.note("Import complete.")
449
printer('Cleaning up')
450
shutil.rmtree(tempdir)
451
printer("Import complete.")
460
progress_bar.finished()
453
462
class UserError(BzrCommandError):
454
463
def __init__(self, message):
491
506
>>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--base-5"))
492
507
Traceback (most recent call last):
493
508
NotArchRevision: The revision id Arch-1:jrandom@example.com%test--test--0--base-5 does not look like it came from Arch.
494
>>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--patch-5"))
495
'jrandom@example.com/test--test--0--patch-5'
509
>>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--patch-5")[0])
510
'jrandom@example.com/test--test--0--patch-5'
511
>>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--patch-5")[0])
512
'jrandom@example.com/test--test--0--patch-5'
513
>>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--patch-5")[1])
515
>>> str(arch_revision("Arch-1-utf-8:jrandom@example.com%test--test--0--patch-5")[1])
497
518
if revision_id is None:
499
if revision_id[:7] != 'Arch-1:':
520
if revision_id[:7] not in ('Arch-1:', 'Arch-1-'):
500
521
raise NotArchRevision(revision_id)
503
return pybaz.Revision(revision_id[7:].replace('%', '/'))
524
encoding, arch_name = revision_id[6:].split(':', 1)
525
arch_name = arch_name.replace('%', '/')
529
encoding = encoding[1:]
530
return pybaz.Revision(arch_name), encoding
504
531
except pybaz.errors.NamespaceError, e:
505
532
raise NotArchRevision(revision_id)
627
657
if missing_ancestor:
628
658
# if we want it to be in revision-history, do that here.
629
target_tree.add_pending_merge(revision_id(missing_ancestor))
659
target_tree.set_parent_ids(
660
[revision_id(missing_ancestor, encoding)],
661
allow_leftmost_as_ghost=True)
630
662
missing_ancestor = None
631
663
for merged_rev in direct_merges:
632
target_tree.add_pending_merge(revision_id(merged_rev))
664
target_tree.add_pending_merge(revision_id(merged_rev,
666
target_tree.set_root_id(BAZ_IMPORT_ROOT)
633
667
target_tree.set_inventory(baz_inv)
634
commitobj = Commit(reporter=ImportCommitReporter(pb))
668
commitobj = Commit(reporter=ImportCommitReporter())
635
669
commitobj.commit(working_tree=target_tree,
636
message=log_message.decode('ascii', 'replace'),
637
verbose=False, committer=log_creator,
638
timestamp=timestamp, timezone=0, rev_id=rev_id,
670
message=log_message.decode(log_encoding, 'replace'),
671
verbose=False, committer=log_creator, timestamp=timestamp,
672
timezone=0, rev_id=rev_id, revprops={})
641
674
target_tree.unlock()
725
_global_option('max-count', type = int)
726
class cmd_baz_import_branch(Command):
727
"""Import an Arch or Baz branch into a bzr branch. <BZRTOOLS>"""
728
takes_args = ['to_location', 'from_branch?', 'reuse_history*']
729
takes_options = ['verbose', 'max-count']
731
def printer(self, name):
734
def run(self, to_location, from_branch=None, fast=False, max_count=None,
735
verbose=False, dry_run=False, reuse_history_list=[]):
736
to_location = os.path.realpath(str(to_location))
737
if from_branch is not None:
739
from_branch = pybaz.Version(from_branch)
740
except pybaz.errors.NamespaceError:
741
print "%s is not a valid Arch branch." % from_branch
743
if reuse_history_list is None:
744
reuse_history_list = []
745
import_version(to_location, from_branch, self.printer,
747
reuse_history_from=reuse_history_list)
759
def baz_import_branch(to_location, from_branch, fast, max_count, verbose,
760
encoding, dry_run, reuse_history_list):
761
to_location = os.path.realpath(str(to_location))
762
if from_branch is not None:
764
from_branch = pybaz.Version(from_branch)
765
except pybaz.errors.NamespaceError:
766
print "%s is not a valid Arch branch." % from_branch
768
if reuse_history_list is None:
769
reuse_history_list = []
770
import_version(to_location, from_branch, encoding, max_count=max_count,
771
reuse_history_from=reuse_history_list)
750
774
class NotInABranch(Exception):
756
class cmd_baz_import(Command):
757
"""Import an Arch or Baz archive into bzr branches. <BZRTOOLS>
759
This command should be used on local archives (or mirrors) only. It is
760
quite slow on remote archives.
762
reuse_history allows you to specify any previous imports you
763
have done of different archives, which this archive has branches
764
tagged from. This will dramatically reduce the time to convert
765
the archive as it will not have to convert the history already
766
converted in that other branch.
768
If you specify prefixes, only branches whose names start with that prefix
769
will be imported. Skipped branches will be listed, so you can import any
770
branches you missed by accident. Here's an example of doing a partial
771
import from thelove@canonical.com:
772
bzr baz-import thelove thelove@canonical.com --prefixes dists:talloc-except
774
takes_args = ['to_root_dir', 'from_archive', 'reuse_history*']
775
takes_options = ['verbose', Option('prefixes', type=str,
776
help="Prefixes of branches to import, colon-separated")]
778
def printer(self, name):
781
def run(self, to_root_dir, from_archive, verbose=False,
782
reuse_history_list=[], prefixes=None):
783
if reuse_history_list is None:
784
reuse_history_list = []
785
to_root = str(os.path.realpath(to_root_dir))
786
if not os.path.exists(to_root):
788
if prefixes is not None:
789
prefixes = prefixes.split(':')
790
import_archive(to_root, from_archive, verbose, self.printer,
791
reuse_history_list, prefixes=prefixes)
794
def import_archive(to_root, from_archive, verbose, printer,
795
reuse_history_from=[], standalone=False,
781
def baz_import(to_root_dir, from_archive, encoding, verbose=False,
782
reuse_history_list=[], prefixes=None):
783
if reuse_history_list is None:
784
reuse_history_list = []
785
to_root = str(os.path.realpath(to_root_dir))
786
if not os.path.exists(to_root):
788
if prefixes is not None:
789
prefixes = prefixes.split(':')
790
import_archive(to_root, from_archive, verbose, encoding,
791
reuse_history_list, prefixes=prefixes)
794
def import_archive(to_root, from_archive, verbose,
795
encoding, reuse_history_from=[], standalone=False,
797
797
def selected(version):
798
798
if prefixes is None:
813
813
except NoRepositoryPresent:
814
814
raise BzrCommandError("Can't create repository at existing branch.")
815
815
versions = list(pybaz.Archive(str(from_archive)).iter_versions())
816
# progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
816
progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
818
818
for num, version in enumerate(versions):
819
# progress_bar.update("Branch", num, len(versions))
819
progress_bar.update("Branch", num, len(versions))
820
820
if not selected(version):
821
821
print "Skipping %s" % version
823
823
target = os.path.join(to_root, map_namespace(version))
824
printer("importing %s into %s" % (version, target))
825
824
if not os.path.exists(os.path.dirname(target)):
826
825
os.makedirs(os.path.dirname(target))
828
import_version(target, version, printer,
827
import_version(target, version, encoding,
829
828
reuse_history_from=reuse_history_from,
830
829
standalone=standalone)
831
830
except pybaz.errors.ExecProblem,e:
832
831
if str(e).find('The requested revision cannot be built.') != -1:
833
printer("Skipping version %s as it cannot be built due"
834
" to a missing parent archive." % version)
833
"Skipping version %s as it cannot be built due"
834
" to a missing parent archive." % version)
837
837
except UserError, e:
838
838
if str(e).find('already exists, and the last revision ') != -1:
839
printer("Skipping version %s as it has had commits made"
840
" since it was converted to bzr." % version)
840
"Skipping version %s as it has had commits made"
841
" since it was converted to bzr." % version)
845
# progress_bar.finished()
845
progress_bar.finished()
847
848
def map_namespace(a_version):
848
849
a_version = pybaz.Version("%s" % a_version)