~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: John Arbash Meinel
  • Date: 2009-06-03 18:22:21 UTC
  • mto: This revision was merged to the branch mainline in revision 4406.
  • Revision ID: john@arbash-meinel.com-20090603182221-0oauceo6yt59a08y
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
This gives us a single location where formats are hard-coded
it also means that 'bzr branch --stacked' can preserve the repository
format, even when the Branch format is 'too old'. (Such as when you
convert a repo, and don't convert all of the contained branches.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2358
2358
    def set_branch_format(self, format):
2359
2359
        self._branch_format = format
2360
2360
 
2361
 
    def require_stacking(self):
 
2361
    def require_stacking(self, stack_on=None, possible_transports=[]):
 
2362
        """We have a request to stack, try to ensure the formats support it.
 
2363
 
 
2364
        :param stack_on: If supplied, it is the URL to a branch that we want to
 
2365
            stack on. Check to see if that format supports stacking before
 
2366
            forcing an upgrade.
 
2367
        """
 
2368
        # Stacking is desired. requested by the target, but does the place it
 
2369
        # points at support stacking? If it doesn't then we should
 
2370
        # not implicitly upgrade. We check this here.
 
2371
        new_repo_format = None
 
2372
        new_branch_format = None
 
2373
 
 
2374
        # a bit of state for get_target_branch so that we don't try to open it
 
2375
        # 2 times, for both repo *and* branch
 
2376
        target = [None, False, None] # target_branch, checked, upgrade anyway
 
2377
        def get_target_branch():
 
2378
            if target[1]:
 
2379
                # We've checked, don't check again
 
2380
                return target
 
2381
            if stack_on is None:
 
2382
                # No target format, that means we want to force upgrading
 
2383
                target[:] = [None, True, True]
 
2384
                return target
 
2385
            try:
 
2386
                target_dir = BzrDir.open(stack_on,
 
2387
                    possible_transports=possible_transports)
 
2388
            except errors.NotBranchError:
 
2389
                # Nothing there, don't change formats
 
2390
                target[:] = [None, True, False]
 
2391
                return target
 
2392
            except errors.JailBreak:
 
2393
                # JailBreak, JFDI and upgrade anyway
 
2394
                target[:] = [None, True, True]
 
2395
                return target
 
2396
            try:
 
2397
                target_branch = target_dir.open_branch()
 
2398
            except errors.NotBranchError:
 
2399
                # No branch, don't upgrade formats
 
2400
                target[:] = [None, True, False]
 
2401
                return target
 
2402
            target[:] = [target_branch, True, False]
 
2403
            return target
 
2404
 
 
2405
        if not (self.repository_format.supports_external_lookups):
 
2406
            # We need to upgrade the Repository.
 
2407
            target_branch, _, do_upgrade = get_target_branch()
 
2408
            if target_branch is None:
 
2409
                # We don't have a target branch, should we upgrade anyway?
 
2410
                if do_upgrade:
 
2411
                    # stack_on is inaccessible, JFDI.
 
2412
                    # TODO: bad monkey, hard-coded formats...
 
2413
                    if self.repository_format.rich_root_data:
 
2414
                        new_repo_format = pack_repo.RepositoryFormatKnitPack6RichRoot()
 
2415
                    else:
 
2416
                        new_repo_format = pack_repo.RepositoryFormatKnitPack6()
 
2417
            else:
 
2418
                # If the target already supports stacking, then we know the
 
2419
                # project is already able to use stacking, so auto-upgrade
 
2420
                # for them
 
2421
                new_repo_format = target_branch.repository._format
 
2422
                if not new_repo_format.supports_external_lookups:
 
2423
                    # target doesn't, source doesn't, so don't auto upgrade
 
2424
                    # repo
 
2425
                    new_repo_format = None
 
2426
            if new_repo_format is not None:
 
2427
                self.repository_format = new_repo_format
 
2428
                note('Source repository format does not support stacking,'
 
2429
                     ' using format:\n  \'%s\'',
 
2430
                     new_repo_format.get_format_description())
 
2431
 
2362
2432
        if not self.get_branch_format().supports_stacking():
2363
 
            # We need to make a stacked branch, but the default format for the
2364
 
            # target doesn't support stacking.  So force a branch that *can*
2365
 
            # support stacking.
2366
 
            from bzrlib.branch import BzrBranchFormat7
2367
 
            branch_format = BzrBranchFormat7()
2368
 
            self.set_branch_format(branch_format)
2369
 
            mutter("using %r for stacking" % (branch_format,))
2370
 
            from bzrlib.repofmt import pack_repo
2371
 
            if self.repository_format.rich_root_data:
2372
 
                bzrdir_format_name = '1.6.1-rich-root'
2373
 
                repo_format = pack_repo.RepositoryFormatKnitPack5RichRoot()
 
2433
            # We just checked the repo, now lets check if we need to
 
2434
            # upgrade the branch format
 
2435
            target_branch, _, do_upgrade = get_target_branch()
 
2436
            if target_branch is None:
 
2437
                if do_upgrade:
 
2438
                    # TODO: bad monkey, hard-coded formats...
 
2439
                    new_branch_format = branch.BzrBranchFormat7()
2374
2440
            else:
2375
 
                bzrdir_format_name = '1.6'
2376
 
                repo_format = pack_repo.RepositoryFormatKnitPack5()
2377
 
            note('Source format does not support stacking, using format:'
2378
 
                 ' \'%s\'\n  %s\n',
2379
 
                 bzrdir_format_name, repo_format.get_format_description())
2380
 
            self.repository_format = repo_format
 
2441
                new_branch_format = target_branch._format
 
2442
                if not new_branch_format.supports_stacking():
 
2443
                    new_branch_format = None
 
2444
            if new_branch_format is not None:
 
2445
                # Does support stacking, use its format.
 
2446
                self.set_branch_format(new_branch_format)
 
2447
                note('Source branch format does not support stacking,'
 
2448
                     ' using format:\n  \'%s\'',
 
2449
                     new_branch_format.get_format_description())
2381
2450
 
2382
2451
    def get_converter(self, format=None):
2383
2452
        """See BzrDirFormat.get_converter()."""
3525
3594
                                             require_stacking)
3526
3595
        self._bzrdir = bzrdir
3527
3596
 
3528
 
    def _ensure_stackable_formats(self, stack_on):
3529
 
        """Both repository and branch formats should support stacking."""
3530
 
        # Stacking is desired. requested by the target, but does the place it
3531
 
        # points at support stacking? If it doesn't then we should
3532
 
        # not implicitly upgrade. We check this here.
3533
 
        format = self._bzrdir._format
3534
 
        new_repo_format = None
3535
 
        new_branch_format = None
3536
 
 
3537
 
        # a bit of state for get_target_branch so that we don't try to open it
3538
 
        # 2 times, for both repo *and* branch
3539
 
        target = [None, False, None] # target_branch, checked, upgrade anyway
3540
 
        def get_target_branch():
3541
 
            if target[1]:
3542
 
                # We've checked, don't check again
3543
 
                return target
3544
 
            try:
3545
 
                target_dir = BzrDir.open(stack_on,
3546
 
                    possible_transports=[self._bzrdir.root_transport])
3547
 
            except errors.NotBranchError:
3548
 
                # Nothing there, don't change formats
3549
 
                target[:] = [None, True, False]
3550
 
                return target
3551
 
            except errors.JailBreak:
3552
 
                # JailBreak, JFDI and upgrade anyway
3553
 
                target[:] = [None, True, True]
3554
 
                return target
3555
 
            try:
3556
 
                target_branch = target_dir.open_branch()
3557
 
            except errors.NotBranchError:
3558
 
                # No branch, don't upgrade formats
3559
 
                target[:] = [None, True, False]
3560
 
                return target
3561
 
            target[:] = [target_branch, True, False]
3562
 
            return target
3563
 
 
3564
 
        if not (format.repository_format.supports_external_lookups):
3565
 
            # We need to upgrade the Repository.
3566
 
            target_branch, _, do_upgrade = get_target_branch()
3567
 
            if target_branch is None:
3568
 
                # We don't have a target branch, should we upgrade anyway?
3569
 
                if do_upgrade:
3570
 
                    # stack_on is inaccessible, JFDI.
3571
 
                    # TODO: bad monkey, hard-coded formats...
3572
 
                    if format.repository_format.rich_root_data:
3573
 
                        new_repo_format = pack_repo.RepositoryFormatKnitPack6RichRoot()
3574
 
                    else:
3575
 
                        new_repo_format = pack_repo.RepositoryFormatKnitPack6()
3576
 
            else:
3577
 
                # If the target already supports stacking, then we know the
3578
 
                # project is already able to use stacking, so auto-upgrade
3579
 
                # for them
3580
 
                new_repo_format = target_branch.repository._format
3581
 
                if not new_repo_format.supports_external_lookups:
3582
 
                    # target doesn't, source doesn't, so don't auto upgrade
3583
 
                    # repo
3584
 
                    new_repo_format = None
3585
 
            if new_repo_format is not None:
3586
 
                format.repository_format = new_repo_format
3587
 
                note('Source repository format does not support stacking,'
3588
 
                     ' using format: \'%s\'',
3589
 
                     new_repo_format.get_format_description())
3590
 
 
3591
 
        if not format.get_branch_format().supports_stacking():
3592
 
            # We just checked the repo, now lets check if we need to
3593
 
            # upgrade the branch format
3594
 
            target_branch, _, do_upgrade = get_target_branch()
3595
 
            if target_branch is None:
3596
 
                if do_upgrade:
3597
 
                    # TODO: bad monkey, hard-coded formats...
3598
 
                    new_branch_format = branch.BzrBranchFormat7()
3599
 
            else:
3600
 
                new_branch_format = target_branch._format
3601
 
                if not new_branch_format.supports_stacking():
3602
 
                    new_branch_format = None
3603
 
            if new_branch_format is not None:
3604
 
                # Does support stacking, use its format.
3605
 
                format.set_branch_format(new_branch_format)
3606
 
                note('Source branch format does not support stacking,'
3607
 
                     ' using format: \'%s\'',
3608
 
                     new_branch_format.get_format_description())
3609
 
 
3610
 
        if not self._require_stacking:
3611
 
            # We have picked up automatic stacking somewhere.
3612
 
            note('Using default stacking branch %s at %s', self._stack_on,
3613
 
                self._stack_on_pwd)
3614
 
 
3615
3597
    def acquire_repository(self, make_working_trees=None, shared=False):
3616
3598
        """Implementation of RepositoryAcquisitionPolicy.acquire_repository
3617
3599
 
3619
3601
        """
3620
3602
        stack_on = self._get_full_stack_on()
3621
3603
        if stack_on:
3622
 
            self._ensure_stackable_formats(stack_on)
 
3604
            format = self._bzrdir._format
 
3605
            format.require_stacking(stack_on=stack_on,
 
3606
                                    possible_transports=[self._bzrdir.root_transport])
 
3607
            if not self._require_stacking:
 
3608
                # We have picked up automatic stacking somewhere.
 
3609
                note('Using default stacking branch %s at %s', self._stack_on,
 
3610
                    self._stack_on_pwd)
3623
3611
        repository = self._bzrdir.create_repository(shared=shared)
3624
3612
        self._add_fallback(repository,
3625
3613
                           possible_transports=[self._bzrdir.transport])