def get_branch(repo, relpath, format=None): """Return existing branch in destination repo. Create new if don't exist. @param format: force create new branch in specified format. """ repo_trans = repo.bzrdir.root_transport try: br_dir = BzrDir.open(repo_trans.abspath(relpath)) branch = br_dir.open_branch() except errors.NotBranchError: # create destination branch directory, creating parents as needed. needed = [relpath] while needed: try: repo_trans.mkdir(needed[-1]) needed.pop() except errors.NoSuchFile: parent = urlutils.dirname(needed[-1]) if parent == '': raise errors.BzrCommandError('Could not create branch dir') needed.append(parent) br_dir = BzrDir.create(repo_trans.abspath(relpath)) if format is None: format = BranchFormat.get_default_format() branch = format.initialize(br_dir) note('Created destination branch %s' % relpath) if branch.repository.bzrdir.root_transport.base != repo_trans.base: raise errors.BzrCommandError('Branch %s does not use repository %s' % (relpath, repo_trans.base)) # XXX: hack to make sure the branch is using the same repository # instance, for locking purposes branch.repository = repo return branch
def test_register_unregister_format(self): format = SampleBranchFormat() # make a control dir dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url()) # make a branch format.initialize(dir) # register a format for it. BranchFormat.register_format(format) # which branch.Open will refuse (not supported) self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url()) self.make_branch_and_tree('foo') # but open_downlevel will work self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True)) # unregister the format BranchFormat.unregister_format(format) self.make_branch_and_tree('bar')
def test_register_unregister_format(self): format = SampleBranchFormat() # make a control dir dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url()) # make a branch format.initialize(dir) # register a format for it. BranchFormat.register_format(format) # which branch.Open will refuse (not supported) self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url()) self.make_branch_and_tree('foo') # but open_downlevel will work self.assertEqual( format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True)) # unregister the format BranchFormat.unregister_format(format) self.make_branch_and_tree('bar')
def repo_push(src_repo, dst_repo, pb, overwrite=False): src_repo.lock_read() try: dst_repo.lock_write() try: src_repo_trans = src_repo.bzrdir.root_transport dst_repo_trans = dst_repo.bzrdir.root_transport pb.update('Getting list of branches', 0, 1) branches = list_branches(src_repo) note('Pushing %d branches from %s to %s' % (len(branches), src_repo_trans.base, dst_repo_trans.base)) # XXX: ideally this would only fetch the tips of the # branches we found previously. pb.update('Fetching entire repo', 0, 1) dst_repo.fetch(src_repo, pb=pb) # Now synchronise the revision histories of the local and # remote branches. The previous fetch() call has made # sure that the corresponding revisions exist in dst_repo. for index, src_branch in enumerate(branches): pb.update('Updating branches', index, len(branches)) relpath = src_repo_trans.relpath( src_branch.bzrdir.root_transport.base) format = BranchFormat.find_format(src_branch.bzrdir) dst_branch = get_branch(dst_repo, relpath, format) src_history = src_branch.revision_history() dst_history = dst_branch.revision_history() # If we aren't overwriting and the destination history # is not a subset of the source history, error out. # XXX this implementation is buggy in some cases if not overwrite and (src_history[:len(dst_history)] != dst_history): raise errors.BzrCommandError('Branch %s has diverged' % relpath) # push tags src_branch.tags.merge_to(dst_branch.tags) if src_history != dst_history: dst_branch.set_revision_history(src_history) note('%d revision(s) pushed to %s' % (len(src_history) - len(dst_history), relpath)) finally: dst_repo.unlock() finally: src_repo.unlock()
def test_get_set_default_format(self): # set the format and then set it back again old_format = BranchFormat.get_default_format() BranchFormat.set_default_format(SampleBranchFormat()) try: # the default branch format is used by the meta dir format # which is not the default bzrdir format at this point dir = BzrDirMetaFormat1().initialize('memory:///') result = dir.create_branch() self.assertEqual(result, 'A branch') finally: BranchFormat.set_default_format(old_format) self.assertEqual(old_format, BranchFormat.get_default_format())
def test_default_format_is_same_as_bzrdir_default(self): # XXX: it might be nice if there was only one place the default was # set, but at the moment that's not true -- mbp 20070814 -- # https://bugs.launchpad.net/bzr/+bug/132376 self.assertEqual(BranchFormat.get_default_format(), BzrDirFormat.get_default_format().get_branch_format())
def test_default_format(self): # update this if you change the default branch format self.assertIsInstance(BranchFormat.get_default_format(), BzrBranchFormat6)
def check_format(format, url): dir = format._matchingbzrdir.initialize(url) dir.create_repository() format.initialize(dir) found_format = BranchFormat.find_format(dir) self.failUnless(isinstance(found_format, format.__class__))