/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: Marco Pantaleoni
  • Date: 2010-03-24 14:35:25 UTC
  • mto: This revision was merged to the branch mainline in revision 5174.
  • Revision ID: panta@elasticworld.org-20100324143525-00sej430c23d3gto
Added the new hooks 'post_branch', 'post_switch' and 'post_repo_init',
called respectively whenever a new branch is created, when a checkout is
switched to a different branch and when a repository is initialized
(created).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1496
1496
        """Return the short format description for this format."""
1497
1497
        raise NotImplementedError(self.get_format_description)
1498
1498
 
 
1499
    def _run_post_branch_hooks(self, a_bzrdir, name, branch):
 
1500
        hooks = Branch.hooks['post_branch']
 
1501
        if not hooks:
 
1502
            return
 
1503
        params = BranchHookParams(self, a_bzrdir, name, branch)
 
1504
        for hook in hooks:
 
1505
            hook(params)
 
1506
 
1499
1507
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
1500
1508
                           lock_type='metadir', set_format=True):
1501
1509
        """Initialize a branch in a bzrdir, with specified files
1537
1545
        finally:
1538
1546
            if lock_taken:
1539
1547
                control_files.unlock()
1540
 
        return self.open(a_bzrdir, name, _found=True)
 
1548
        branch = self.open(a_bzrdir, name, _found=True)
 
1549
        if Branch.hooks['post_branch']:
 
1550
            self._run_post_branch_hooks(a_bzrdir, name, branch)
 
1551
        return branch
1541
1552
 
1542
1553
    def initialize(self, a_bzrdir, name=None):
1543
1554
        """Create a branch of this format in a_bzrdir.
1703
1714
            "should return a tag name or None if no tag name could be "
1704
1715
            "determined. The first non-None tag name returned will be used.",
1705
1716
            (2, 2), None))
 
1717
        self.create_hook(HookPoint('post_branch',
 
1718
            "Called after new branch initialization completes. "
 
1719
            "post_branch is called with a bzrlib.branch.BranchHookParams. "
 
1720
            "Note that init, branch and checkout will trigger this hook.",
 
1721
            (2, 2), None))
 
1722
        self.create_hook(HookPoint('post_switch',
 
1723
            "Called after a checkout switches branch. "
 
1724
            "post_switch is called with a "
 
1725
            "bzrlib.branch.SwitchHookParams.", (2, 2), None))
1706
1726
 
1707
1727
 
1708
1728
 
1747
1767
            self.__class__.__name__, self.branch,
1748
1768
            self.old_revno, self.old_revid, self.new_revno, self.new_revid)
1749
1769
 
 
1770
class BranchHookParams(object):
 
1771
    """Object holding parameters passed to *_branch hooks.
 
1772
 
 
1773
    There are 4 fields that hooks may wish to access:
 
1774
 
 
1775
    :ivar format: the branch format
 
1776
    :ivar bzrdir: the bzrdir where the branch will be/has been initialized
 
1777
    :ivar name: name of colocated branch, if any (or None)
 
1778
    :ivar branch: the branch
 
1779
    """
 
1780
 
 
1781
    def __init__(self, format, a_bzrdir, name, branch):
 
1782
        """Create a group of BranchHook parameters.
 
1783
 
 
1784
        :param format: the branch format
 
1785
        :param a_bzrdir: the bzrdir where the branch will be/has been initialized
 
1786
        :param name: name of colocated branch, if any (or None)
 
1787
        :param branch: the branch
 
1788
        """
 
1789
        self.format = format
 
1790
        self.bzrdir = a_bzrdir
 
1791
        self.name   = name
 
1792
        self.branch = branch
 
1793
 
 
1794
    def __eq__(self, other):
 
1795
        return self.__dict__ == other.__dict__
 
1796
 
 
1797
    def __repr__(self):
 
1798
        if self.branch:
 
1799
            return "<%s of %s>" % (self.__class__.__name__, self.branch)
 
1800
        else:
 
1801
            return "<%s of format:%s bzrdir:%s>" % (
 
1802
                self.__class__.__name__, self.branch,
 
1803
                self.format, self.bzrdir)
 
1804
 
 
1805
class SwitchHookParams(object):
 
1806
    """Object holding parameters passed to *_switch hooks.
 
1807
 
 
1808
    There are 4 fields that hooks may wish to access:
 
1809
 
 
1810
    :ivar control_dir: BzrDir of the checkout to change
 
1811
    :ivar to_branch: branch that the checkout is to reference
 
1812
    :ivar force: skip the check for local commits in a heavy checkout
 
1813
    :ivar revision_id: revision ID to switch to (or None)
 
1814
    """
 
1815
 
 
1816
    def __init__(self, control_dir, to_branch, force, revision_id):
 
1817
        """Create a group of SwitchHook parameters.
 
1818
 
 
1819
        :param control_dir: BzrDir of the checkout to change
 
1820
        :param to_branch: branch that the checkout is to reference
 
1821
        :param force: skip the check for local commits in a heavy checkout
 
1822
        :param revision_id: revision ID to switch to (or None)
 
1823
        """
 
1824
        self.control_dir = control_dir
 
1825
        self.to_branch   = to_branch
 
1826
        self.force       = force
 
1827
        self.revision_id = revision_id
 
1828
 
 
1829
    def __eq__(self, other):
 
1830
        return self.__dict__ == other.__dict__
 
1831
 
 
1832
    def __repr__(self):
 
1833
        return "<%s for %s to (%s, %s)>" % (self.__class__.__name__,
 
1834
            self.control_dir, self.to_branch,
 
1835
            self.revision_id)
1750
1836
 
1751
1837
class BzrBranchFormat4(BranchFormat):
1752
1838
    """Bzr branch format 4.
2022
2108
        branch_transport.put_bytes('location',
2023
2109
            target_branch.bzrdir.root_transport.base)
2024
2110
        branch_transport.put_bytes('format', self.get_format_string())
2025
 
        return self.open(
 
2111
        branch = self.open(
2026
2112
            a_bzrdir, name, _found=True,
2027
2113
            possible_transports=[target_branch.bzrdir.root_transport])
 
2114
        if Branch.hooks['post_branch']:
 
2115
            self._run_post_branch_hooks(a_bzrdir, name, branch)
 
2116
        return branch
2028
2117
 
2029
2118
    def __init__(self):
2030
2119
        super(BranchReferenceFormat, self).__init__()