/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Jelmer Vernooij
  • Date: 2010-03-21 21:39:33 UTC
  • mfrom: (5102 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5143.
  • Revision ID: jelmer@samba.org-20100321213933-fexeh9zcoz8oaju2
merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
167
167
        """
168
168
        control = bzrdir.BzrDir.open(base, _unsupported,
169
169
                                     possible_transports=possible_transports)
170
 
        return control.open_branch(_unsupported)
 
170
        return control.open_branch(unsupported=_unsupported)
171
171
 
172
172
    @staticmethod
173
 
    def open_from_transport(transport, _unsupported=False):
 
173
    def open_from_transport(transport, name=None, _unsupported=False):
174
174
        """Open the branch rooted at transport"""
175
175
        control = bzrdir.BzrDir.open_from_transport(transport, _unsupported)
176
 
        return control.open_branch(_unsupported)
 
176
        return control.open_branch(name=name, unsupported=_unsupported)
177
177
 
178
178
    @staticmethod
179
179
    def open_containing(url, possible_transports=None):
217
217
    def _get_fallback_repository(self, url):
218
218
        """Get the repository we fallback to at url."""
219
219
        url = urlutils.join(self.base, url)
220
 
        a_bzrdir = bzrdir.BzrDir.open(url,
 
220
        a_branch = Branch.open(url,
221
221
            possible_transports=[self.bzrdir.root_transport])
222
 
        return a_bzrdir.open_branch().repository
 
222
        return a_branch.repository
223
223
 
224
224
    def _get_tags_bytes(self):
225
225
        """Get the bytes of a serialised tags dict.
1102
1102
        params = ChangeBranchTipParams(
1103
1103
            self, old_revno, new_revno, old_revid, new_revid)
1104
1104
        for hook in hooks:
1105
 
            try:
1106
 
                hook(params)
1107
 
            except errors.TipChangeRejected:
1108
 
                raise
1109
 
            except Exception:
1110
 
                exc_info = sys.exc_info()
1111
 
                hook_name = Branch.hooks.get_hook_name(hook)
1112
 
                raise errors.HookFailed(
1113
 
                    'pre_change_branch_tip', hook_name, exc_info)
 
1105
            hook(params)
1114
1106
 
1115
1107
    @needs_write_lock
1116
1108
    def update(self):
1324
1316
        if lightweight:
1325
1317
            format = self._get_checkout_format()
1326
1318
            checkout = format.initialize_on_transport(t)
1327
 
            from_branch = BranchReferenceFormat().initialize(checkout, self)
 
1319
            from_branch = BranchReferenceFormat().initialize(checkout, 
 
1320
                target_branch=self)
1328
1321
        else:
1329
1322
            format = self._get_checkout_format()
1330
1323
            checkout_branch = bzrdir.BzrDir.create_branch_convenience(
1441
1434
        return not (self == other)
1442
1435
 
1443
1436
    @classmethod
1444
 
    def find_format(klass, a_bzrdir):
 
1437
    def find_format(klass, a_bzrdir, name=None):
1445
1438
        """Return the format for the branch object in a_bzrdir."""
1446
1439
        try:
1447
 
            transport = a_bzrdir.get_branch_transport(None)
 
1440
            transport = a_bzrdir.get_branch_transport(None, name=name)
1448
1441
            format_string = transport.get_bytes("format")
1449
1442
            return klass._formats[format_string]
1450
1443
        except errors.NoSuchFile:
1451
 
            raise errors.NotBranchError(path=transport.base)
 
1444
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1452
1445
        except KeyError:
1453
1446
            raise errors.UnknownFormatError(format=format_string, kind='branch')
1454
1447
 
1490
1483
        """Return the short format description for this format."""
1491
1484
        raise NotImplementedError(self.get_format_description)
1492
1485
 
1493
 
    def _initialize_helper(self, a_bzrdir, utf8_files, lock_type='metadir',
1494
 
                           set_format=True):
 
1486
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
 
1487
                           lock_type='metadir', set_format=True):
1495
1488
        """Initialize a branch in a bzrdir, with specified files
1496
1489
 
1497
1490
        :param a_bzrdir: The bzrdir to initialize the branch in
1498
1491
        :param utf8_files: The files to create as a list of
1499
1492
            (filename, content) tuples
 
1493
        :param name: Name of colocated branch to create, if any
1500
1494
        :param set_format: If True, set the format with
1501
1495
            self.get_format_string.  (BzrBranch4 has its format set
1502
1496
            elsewhere)
1503
1497
        :return: a branch in this format
1504
1498
        """
1505
1499
        mutter('creating branch %r in %s', self, a_bzrdir.transport.base)
1506
 
        branch_transport = a_bzrdir.get_branch_transport(self)
 
1500
        branch_transport = a_bzrdir.get_branch_transport(self, name=name)
1507
1501
        lock_map = {
1508
1502
            'metadir': ('lock', lockdir.LockDir),
1509
1503
            'branch4': ('branch-lock', lockable_files.TransportLock),
1530
1524
        finally:
1531
1525
            if lock_taken:
1532
1526
                control_files.unlock()
1533
 
        return self.open(a_bzrdir, _found=True)
 
1527
        return self.open(a_bzrdir, name, _found=True)
1534
1528
 
1535
 
    def initialize(self, a_bzrdir):
1536
 
        """Create a branch of this format in a_bzrdir."""
 
1529
    def initialize(self, a_bzrdir, name=None):
 
1530
        """Create a branch of this format in a_bzrdir.
 
1531
        
 
1532
        :param name: Name of the colocated branch to create.
 
1533
        """
1537
1534
        raise NotImplementedError(self.initialize)
1538
1535
 
1539
1536
    def is_supported(self):
1569
1566
        """
1570
1567
        raise NotImplementedError(self.network_name)
1571
1568
 
1572
 
    def open(self, a_bzrdir, _found=False, ignore_fallbacks=False):
 
1569
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
1573
1570
        """Return the branch object for a_bzrdir
1574
1571
 
1575
1572
        :param a_bzrdir: A BzrDir that contains a branch.
 
1573
        :param name: Name of colocated branch to open
1576
1574
        :param _found: a private parameter, do not use it. It is used to
1577
1575
            indicate if format probing has already be done.
1578
1576
        :param ignore_fallbacks: when set, no fallback branches will be opened
1742
1740
        """See BranchFormat.get_format_description()."""
1743
1741
        return "Branch format 4"
1744
1742
 
1745
 
    def initialize(self, a_bzrdir):
 
1743
    def initialize(self, a_bzrdir, name=None):
1746
1744
        """Create a branch of this format in a_bzrdir."""
1747
1745
        utf8_files = [('revision-history', ''),
1748
1746
                      ('branch-name', ''),
1749
1747
                      ]
1750
 
        return self._initialize_helper(a_bzrdir, utf8_files,
 
1748
        return self._initialize_helper(a_bzrdir, utf8_files, name=name,
1751
1749
                                       lock_type='branch4', set_format=False)
1752
1750
 
1753
1751
    def __init__(self):
1758
1756
        """The network name for this format is the control dirs disk label."""
1759
1757
        return self._matchingbzrdir.get_format_string()
1760
1758
 
1761
 
    def open(self, a_bzrdir, _found=False, ignore_fallbacks=False):
 
1759
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
1762
1760
        """See BranchFormat.open()."""
1763
1761
        if not _found:
1764
1762
            # we are being called directly and must probe.
1766
1764
        return BzrBranch(_format=self,
1767
1765
                         _control_files=a_bzrdir._control_files,
1768
1766
                         a_bzrdir=a_bzrdir,
 
1767
                         name=name,
1769
1768
                         _repository=a_bzrdir.open_repository())
1770
1769
 
1771
1770
    def __str__(self):
1786
1785
        """
1787
1786
        return self.get_format_string()
1788
1787
 
1789
 
    def open(self, a_bzrdir, _found=False, ignore_fallbacks=False):
 
1788
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
1790
1789
        """See BranchFormat.open()."""
1791
1790
        if not _found:
1792
 
            format = BranchFormat.find_format(a_bzrdir)
 
1791
            format = BranchFormat.find_format(a_bzrdir, name=name)
1793
1792
            if format.__class__ != self.__class__:
1794
1793
                raise AssertionError("wrong format %r found for %r" %
1795
1794
                    (format, self))
1796
1795
        try:
1797
 
            transport = a_bzrdir.get_branch_transport(None)
 
1796
            transport = a_bzrdir.get_branch_transport(None, name=name)
1798
1797
            control_files = lockable_files.LockableFiles(transport, 'lock',
1799
1798
                                                         lockdir.LockDir)
1800
1799
            return self._branch_class()(_format=self,
1801
1800
                              _control_files=control_files,
 
1801
                              name=name,
1802
1802
                              a_bzrdir=a_bzrdir,
1803
1803
                              _repository=a_bzrdir.find_repository(),
1804
1804
                              ignore_fallbacks=ignore_fallbacks)
1805
1805
        except errors.NoSuchFile:
1806
 
            raise errors.NotBranchError(path=transport.base)
 
1806
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1807
1807
 
1808
1808
    def __init__(self):
1809
1809
        super(BranchFormatMetadir, self).__init__()
1838
1838
        """See BranchFormat.get_format_description()."""
1839
1839
        return "Branch format 5"
1840
1840
 
1841
 
    def initialize(self, a_bzrdir):
 
1841
    def initialize(self, a_bzrdir, name=None):
1842
1842
        """Create a branch of this format in a_bzrdir."""
1843
1843
        utf8_files = [('revision-history', ''),
1844
1844
                      ('branch-name', ''),
1845
1845
                      ]
1846
 
        return self._initialize_helper(a_bzrdir, utf8_files)
 
1846
        return self._initialize_helper(a_bzrdir, utf8_files, name)
1847
1847
 
1848
1848
    def supports_tags(self):
1849
1849
        return False
1871
1871
        """See BranchFormat.get_format_description()."""
1872
1872
        return "Branch format 6"
1873
1873
 
1874
 
    def initialize(self, a_bzrdir):
 
1874
    def initialize(self, a_bzrdir, name=None):
1875
1875
        """Create a branch of this format in a_bzrdir."""
1876
1876
        utf8_files = [('last-revision', '0 null:\n'),
1877
1877
                      ('branch.conf', ''),
1878
1878
                      ('tags', ''),
1879
1879
                      ]
1880
 
        return self._initialize_helper(a_bzrdir, utf8_files)
 
1880
        return self._initialize_helper(a_bzrdir, utf8_files, name)
1881
1881
 
1882
1882
    def make_tags(self, branch):
1883
1883
        """See bzrlib.branch.BranchFormat.make_tags()."""
1901
1901
        """See BranchFormat.get_format_description()."""
1902
1902
        return "Branch format 8"
1903
1903
 
1904
 
    def initialize(self, a_bzrdir):
 
1904
    def initialize(self, a_bzrdir, name=None):
1905
1905
        """Create a branch of this format in a_bzrdir."""
1906
1906
        utf8_files = [('last-revision', '0 null:\n'),
1907
1907
                      ('branch.conf', ''),
1908
1908
                      ('tags', ''),
1909
1909
                      ('references', '')
1910
1910
                      ]
1911
 
        return self._initialize_helper(a_bzrdir, utf8_files)
 
1911
        return self._initialize_helper(a_bzrdir, utf8_files, name)
1912
1912
 
1913
1913
    def __init__(self):
1914
1914
        super(BzrBranchFormat8, self).__init__()
1937
1937
    This format was introduced in bzr 1.6.
1938
1938
    """
1939
1939
 
1940
 
    def initialize(self, a_bzrdir):
 
1940
    def initialize(self, a_bzrdir, name=None):
1941
1941
        """Create a branch of this format in a_bzrdir."""
1942
1942
        utf8_files = [('last-revision', '0 null:\n'),
1943
1943
                      ('branch.conf', ''),
1944
1944
                      ('tags', ''),
1945
1945
                      ]
1946
 
        return self._initialize_helper(a_bzrdir, utf8_files)
 
1946
        return self._initialize_helper(a_bzrdir, utf8_files, name)
1947
1947
 
1948
1948
    def _branch_class(self):
1949
1949
        return BzrBranch7
1991
1991
        transport = a_bzrdir.get_branch_transport(None)
1992
1992
        location = transport.put_bytes('location', to_branch.base)
1993
1993
 
1994
 
    def initialize(self, a_bzrdir, target_branch=None):
 
1994
    def initialize(self, a_bzrdir, name=None, target_branch=None):
1995
1995
        """Create a branch of this format in a_bzrdir."""
1996
1996
        if target_branch is None:
1997
1997
            # this format does not implement branch itself, thus the implicit
1998
1998
            # creation contract must see it as uninitializable
1999
1999
            raise errors.UninitializableFormat(self)
2000
2000
        mutter('creating branch reference in %s', a_bzrdir.transport.base)
2001
 
        branch_transport = a_bzrdir.get_branch_transport(self)
 
2001
        branch_transport = a_bzrdir.get_branch_transport(self, name=name)
2002
2002
        branch_transport.put_bytes('location',
2003
2003
            target_branch.bzrdir.root_transport.base)
2004
2004
        branch_transport.put_bytes('format', self.get_format_string())
2005
2005
        return self.open(
2006
 
            a_bzrdir, _found=True,
 
2006
            a_bzrdir, name, _found=True,
2007
2007
            possible_transports=[target_branch.bzrdir.root_transport])
2008
2008
 
2009
2009
    def __init__(self):
2016
2016
        def clone(to_bzrdir, revision_id=None,
2017
2017
            repository_policy=None):
2018
2018
            """See Branch.clone()."""
2019
 
            return format.initialize(to_bzrdir, a_branch)
 
2019
            return format.initialize(to_bzrdir, target_branch=a_branch)
2020
2020
            # cannot obey revision_id limits when cloning a reference ...
2021
2021
            # FIXME RBC 20060210 either nuke revision_id for clone, or
2022
2022
            # emit some sort of warning/error to the caller ?!
2023
2023
        return clone
2024
2024
 
2025
 
    def open(self, a_bzrdir, _found=False, location=None,
 
2025
    def open(self, a_bzrdir, name=None, _found=False, location=None,
2026
2026
             possible_transports=None, ignore_fallbacks=False):
2027
2027
        """Return the branch that the branch reference in a_bzrdir points at.
2028
2028
 
2029
2029
        :param a_bzrdir: A BzrDir that contains a branch.
 
2030
        :param name: Name of colocated branch to open, if any
2030
2031
        :param _found: a private parameter, do not use it. It is used to
2031
2032
            indicate if format probing has already be done.
2032
2033
        :param ignore_fallbacks: when set, no fallback branches will be opened
2037
2038
        :param possible_transports: An optional reusable transports list.
2038
2039
        """
2039
2040
        if not _found:
2040
 
            format = BranchFormat.find_format(a_bzrdir)
 
2041
            format = BranchFormat.find_format(a_bzrdir, name=name)
2041
2042
            if format.__class__ != self.__class__:
2042
2043
                raise AssertionError("wrong format %r found for %r" %
2043
2044
                    (format, self))
2045
2046
            location = self.get_reference(a_bzrdir)
2046
2047
        real_bzrdir = bzrdir.BzrDir.open(
2047
2048
            location, possible_transports=possible_transports)
2048
 
        result = real_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
 
2049
        result = real_bzrdir.open_branch(name=name, 
 
2050
            ignore_fallbacks=ignore_fallbacks)
2049
2051
        # this changes the behaviour of result.clone to create a new reference
2050
2052
        # rather than a copy of the content of the branch.
2051
2053
        # I did not use a proxy object because that needs much more extensive
2097
2099
    :ivar repository: Repository for this branch.
2098
2100
    :ivar base: The url of the base directory for this branch; the one
2099
2101
        containing the .bzr directory.
 
2102
    :ivar name: Optional colocated branch name as it exists in the control
 
2103
        directory.
2100
2104
    """
2101
2105
 
2102
2106
    def __init__(self, _format=None,
2103
 
                 _control_files=None, a_bzrdir=None, _repository=None,
2104
 
                 ignore_fallbacks=False):
 
2107
                 _control_files=None, a_bzrdir=None, name=None,
 
2108
                 _repository=None, ignore_fallbacks=False):
2105
2109
        """Create new branch object at a particular location."""
2106
2110
        if a_bzrdir is None:
2107
2111
            raise ValueError('a_bzrdir must be supplied')
2108
2112
        else:
2109
2113
            self.bzrdir = a_bzrdir
2110
2114
        self._base = self.bzrdir.transport.clone('..').base
 
2115
        self.name = name
2111
2116
        # XXX: We should be able to just do
2112
2117
        #   self.base = self.bzrdir.root_transport.base
2113
2118
        # but this does not quite work yet -- mbp 20080522
2120
2125
        Branch.__init__(self)
2121
2126
 
2122
2127
    def __str__(self):
2123
 
        return '%s(%r)' % (self.__class__.__name__, self.base)
 
2128
        if self.name is None:
 
2129
            return '%s(%r)' % (self.__class__.__name__, self.base)
 
2130
        else:
 
2131
            return '%s(%r,%r)' % (self.__class__.__name__, self.base, self.name)
2124
2132
 
2125
2133
    __repr__ = __str__
2126
2134