~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Vincent Ladeuil
  • Date: 2010-06-23 08:19:28 UTC
  • mfrom: (5317 +trunk)
  • mto: (5247.1.11 first-try)
  • mto: This revision was merged to the branch mainline in revision 5326.
  • Revision ID: v.ladeuil+lp@free.fr-20100623081928-z9q18q30oo5as831
Merge bzr.dev into cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1518
1518
        try:
1519
1519
            transport = a_bzrdir.get_branch_transport(None, name=name)
1520
1520
            format_string = transport.get_bytes("format")
1521
 
            return klass._formats[format_string]
 
1521
            format = klass._formats[format_string]
 
1522
            if isinstance(format, MetaDirBranchFormatFactory):
 
1523
                return format()
 
1524
            return format
1522
1525
        except errors.NoSuchFile:
1523
1526
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1524
1527
        except KeyError:
1529
1532
        """Return the current default format."""
1530
1533
        return klass._default_format
1531
1534
 
 
1535
    @classmethod
 
1536
    def get_formats(klass):
 
1537
        """Get all the known formats.
 
1538
 
 
1539
        Warning: This triggers a load of all lazy registered formats: do not
 
1540
        use except when that is desireed.
 
1541
        """
 
1542
        result = []
 
1543
        for fmt in klass._formats.values():
 
1544
            if isinstance(fmt, MetaDirBranchFormatFactory):
 
1545
                fmt = fmt()
 
1546
            result.append(fmt)
 
1547
        return result
 
1548
 
1532
1549
    def get_reference(self, a_bzrdir, name=None):
1533
1550
        """Get the target reference of the branch in a_bzrdir.
1534
1551
 
1671
1688
 
1672
1689
    @classmethod
1673
1690
    def register_format(klass, format):
1674
 
        """Register a metadir format."""
 
1691
        """Register a metadir format.
 
1692
        
 
1693
        See MetaDirBranchFormatFactory for the ability to register a format
 
1694
        without loading the code the format needs until it is actually used.
 
1695
        """
1675
1696
        klass._formats[format.get_format_string()] = format
1676
1697
        # Metadir formats have a network name of their format string, and get
1677
 
        # registered as class factories.
1678
 
        network_format_registry.register(format.get_format_string(), format.__class__)
 
1698
        # registered as factories.
 
1699
        if isinstance(format, MetaDirBranchFormatFactory):
 
1700
            network_format_registry.register(format.get_format_string(), format)
 
1701
        else:
 
1702
            network_format_registry.register(format.get_format_string(),
 
1703
                format.__class__)
1679
1704
 
1680
1705
    @classmethod
1681
1706
    def set_default_format(klass, format):
1701
1726
        return False  # by default
1702
1727
 
1703
1728
 
 
1729
class MetaDirBranchFormatFactory(registry._LazyObjectGetter):
 
1730
    """A factory for a BranchFormat object, permitting simple lazy registration.
 
1731
    
 
1732
    While none of the built in BranchFormats are lazy registered yet,
 
1733
    bzrlib.tests.test_branch.TestMetaDirBranchFormatFactory demonstrates how to
 
1734
    use it, and the bzr-loom plugin uses it as well (see
 
1735
    bzrlib.plugins.loom.formats).
 
1736
    """
 
1737
 
 
1738
    def __init__(self, format_string, module_name, member_name):
 
1739
        """Create a MetaDirBranchFormatFactory.
 
1740
 
 
1741
        :param format_string: The format string the format has.
 
1742
        :param module_name: Module to load the format class from.
 
1743
        :param member_name: Attribute name within the module for the format class.
 
1744
        """
 
1745
        registry._LazyObjectGetter.__init__(self, module_name, member_name)
 
1746
        self._format_string = format_string
 
1747
        
 
1748
    def get_format_string(self):
 
1749
        """See BranchFormat.get_format_string."""
 
1750
        return self._format_string
 
1751
 
 
1752
    def __call__(self):
 
1753
        """Used for network_format_registry support."""
 
1754
        return self.get_obj()()
 
1755
 
 
1756
 
1704
1757
class BranchHooks(Hooks):
1705
1758
    """A dictionary mapping hook name to a list of callables for branch hooks.
1706
1759