~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2008-10-27 08:02:47 UTC
  • mfrom: (3795 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3798.
  • Revision ID: mbp@sourcefrog.net-20081027080247-0al6nrx2v8u1dcci
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1126
1126
        # (params) where params is a ChangeBranchTipParams with the members
1127
1127
        # (branch, old_revno, new_revno, old_revid, new_revid)
1128
1128
        self['post_change_branch_tip'] = []
 
1129
        # Introduced in 1.9
 
1130
        # Invoked when a stacked branch activates its fallback locations and
 
1131
        # allows the transformation of the url of said location.
 
1132
        # the api signature is
 
1133
        # (branch, url) where branch is the branch having its fallback
 
1134
        # location activated and url is the url for the fallback location.
 
1135
        # The hook should return a url.
 
1136
        self['transform_fallback_location'] = []
1129
1137
 
1130
1138
 
1131
1139
# install the default hooks into the Branch class.
1743
1751
        """
1744
1752
        # TODO: Public option to disable running hooks - should be trivial but
1745
1753
        # needs tests.
1746
 
        target.lock_write()
1747
 
        try:
1748
 
            result = self._push_with_bound_branches(target, overwrite,
1749
 
                    stop_revision,
1750
 
                    _override_hook_source_branch=_override_hook_source_branch)
1751
 
            return result
1752
 
        finally:
1753
 
            target.unlock()
 
1754
        return _run_with_write_locked_target(
 
1755
            target, self._push_with_bound_branches, target, overwrite,
 
1756
            stop_revision,
 
1757
            _override_hook_source_branch=_override_hook_source_branch)
1754
1758
 
1755
1759
    def _push_with_bound_branches(self, target, overwrite,
1756
1760
            stop_revision,
2025
2029
            errors.UnstackableBranchFormat):
2026
2030
            pass
2027
2031
        else:
 
2032
            for hook in Branch.hooks['transform_fallback_location']:
 
2033
                url = hook(self, url)
 
2034
                if url is None:
 
2035
                    hook_name = Branch.hooks.get_hook_name(hook)
 
2036
                    raise AssertionError(
 
2037
                        "'transform_fallback_location' hook %s returned "
 
2038
                        "None, not a URL." % hook_name)
2028
2039
            self._activate_fallback_location(url)
2029
2040
 
2030
2041
    def _check_stackable_repo(self):
2445
2456
        branch._set_config_location('stacked_on_location', '')
2446
2457
        # update target format
2447
2458
        branch._transport.put_bytes('format', format.get_format_string())
 
2459
 
 
2460
 
 
2461
 
 
2462
def _run_with_write_locked_target(target, callable, *args, **kwargs):
 
2463
    """Run ``callable(*args, **kwargs)``, write-locking target for the
 
2464
    duration.
 
2465
 
 
2466
    _run_with_write_locked_target will attempt to release the lock it acquires.
 
2467
 
 
2468
    If an exception is raised by callable, then that exception *will* be
 
2469
    propagated, even if the unlock attempt raises its own error.  Thus
 
2470
    _run_with_write_locked_target should be preferred to simply doing::
 
2471
 
 
2472
        target.lock_write()
 
2473
        try:
 
2474
            return callable(*args, **kwargs)
 
2475
        finally:
 
2476
            target.unlock()
 
2477
    
 
2478
    """
 
2479
    # This is very similar to bzrlib.decorators.needs_write_lock.  Perhaps they
 
2480
    # should share code?
 
2481
    target.lock_write()
 
2482
    try:
 
2483
        result = callable(*args, **kwargs)
 
2484
    except:
 
2485
        exc_info = sys.exc_info()
 
2486
        try:
 
2487
            target.unlock()
 
2488
        finally:
 
2489
            raise exc_info[0], exc_info[1], exc_info[2]
 
2490
    else:
 
2491
        target.unlock()
 
2492
        return result