~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-07-25 15:19:45 UTC
  • mfrom: (3577.1.3 tip-change-rejected-error)
  • Revision ID: pqm@pqm.ubuntu.com-20080725151945-kdaru30ix1m8k0h6
Add TipChangeRejected error so that pre_change_branch_tip hook
        functions can cleanly reject a change. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
 
18
import sys
 
19
 
18
20
from bzrlib.lazy_import import lazy_import
19
21
lazy_import(globals(), """
20
22
from itertools import chain
533
535
        finally:
534
536
            other.unlock()
535
537
 
536
 
 
537
 
 
538
538
    def revision_id_to_revno(self, revision_id):
539
539
        """Given a revision id, return its revno"""
540
540
        if _mod_revision.is_null(revision_id):
899
899
        elif relation == 'a_descends_from_b':
900
900
            return False
901
901
        else:
902
 
            raise AssertionError("invalid heads: %r" % heads)
 
902
            raise AssertionError("invalid relation: %r" % (relation,))
903
903
 
904
904
    def _revision_relations(self, revision_a, revision_b, graph):
905
905
        """Determine the relationship between two revisions.
915
915
        elif heads == set([revision_a]):
916
916
            return 'a_descends_from_b'
917
917
        else:
918
 
            raise AssertionError("invalid heads: %r" % heads)
 
918
            raise AssertionError("invalid heads: %r" % (heads,))
919
919
 
920
920
 
921
921
class BranchFormat(object):
1586
1586
        check_not_reserved_id = _mod_revision.check_not_reserved_id
1587
1587
        for rev_id in rev_history:
1588
1588
            check_not_reserved_id(rev_id)
 
1589
        if Branch.hooks['post_change_branch_tip']:
 
1590
            # Don't calculate the last_revision_info() if there are no hooks
 
1591
            # that will use it.
 
1592
            old_revno, old_revid = self.last_revision_info()
 
1593
        if len(rev_history) == 0:
 
1594
            revid = _mod_revision.NULL_REVISION
 
1595
        else:
 
1596
            revid = rev_history[-1]
 
1597
        self._run_pre_change_branch_tip_hooks(len(rev_history), revid)
1589
1598
        self._write_revision_history(rev_history)
1590
1599
        self._clear_cached_state()
1591
1600
        self._cache_revision_history(rev_history)
1592
1601
        for hook in Branch.hooks['set_rh']:
1593
1602
            hook(self, rev_history)
 
1603
        if Branch.hooks['post_change_branch_tip']:
 
1604
            self._run_post_change_branch_tip_hooks(old_revno, old_revid)
1594
1605
 
1595
1606
    def _run_pre_change_branch_tip_hooks(self, new_revno, new_revid):
1596
1607
        """Run the pre_change_branch_tip hooks."""
1601
1612
        params = ChangeBranchTipParams(
1602
1613
            self, old_revno, new_revno, old_revid, new_revid)
1603
1614
        for hook in hooks:
1604
 
            hook(params)
 
1615
            try:
 
1616
                hook(params)
 
1617
            except errors.TipChangeRejected:
 
1618
                raise
 
1619
            except Exception:
 
1620
                exc_info = sys.exc_info()
 
1621
                hook_name = Branch.hooks.get_hook_name(hook)
 
1622
                raise errors.HookFailed(
 
1623
                    'pre_change_branch_tip', hook_name, exc_info)
1605
1624
 
1606
1625
    def _run_post_change_branch_tip_hooks(self, old_revno, old_revid):
1607
1626
        """Run the post_change_branch_tip hooks."""
1627
1646
        be permitted.
1628
1647
        """
1629
1648
        revision_id = _mod_revision.ensure_null(revision_id)
1630
 
        old_revno, old_revid = self.last_revision_info()
1631
1649
        # this old format stores the full history, but this api doesn't
1632
1650
        # provide it, so we must generate, and might as well check it's
1633
1651
        # correct
1634
1652
        history = self._lefthand_history(revision_id)
1635
1653
        if len(history) != revno:
1636
1654
            raise AssertionError('%d != %d' % (len(history), revno))
1637
 
        self._run_pre_change_branch_tip_hooks(revno, revision_id)
1638
1655
        self.set_revision_history(history)
1639
 
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
1640
1656
 
1641
1657
    def _gen_revision_history(self):
1642
1658
        history = self._transport.get_bytes('revision-history').split('\n')