~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2008-10-16 03:58:42 UTC
  • mfrom: (3763.3.3 1.8)
  • mto: This revision was merged to the branch mainline in revision 3779.
  • Revision ID: mbp@sourcefrog.net-20081016035842-77pczqghpnd5rxkt
merge 1.8final back to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1743
1743
        """
1744
1744
        # TODO: Public option to disable running hooks - should be trivial but
1745
1745
        # 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()
 
1746
        return _run_with_write_locked_target(
 
1747
            target, self._push_with_bound_branches, target, overwrite,
 
1748
            stop_revision,
 
1749
            _override_hook_source_branch=_override_hook_source_branch)
1754
1750
 
1755
1751
    def _push_with_bound_branches(self, target, overwrite,
1756
1752
            stop_revision,
2445
2441
        branch._set_config_location('stacked_on_location', '')
2446
2442
        # update target format
2447
2443
        branch._transport.put_bytes('format', format.get_format_string())
 
2444
 
 
2445
 
 
2446
 
 
2447
def _run_with_write_locked_target(target, callable, *args, **kwargs):
 
2448
    """Run ``callable(*args, **kwargs)``, write-locking target for the
 
2449
    duration.
 
2450
 
 
2451
    _run_with_write_locked_target will attempt to release the lock it acquires.
 
2452
 
 
2453
    If an exception is raised by callable, then that exception *will* be
 
2454
    propagated, even if the unlock attempt raises its own error.  Thus
 
2455
    _run_with_write_locked_target should be preferred to simply doing::
 
2456
 
 
2457
        target.lock_write()
 
2458
        try:
 
2459
            return callable(*args, **kwargs)
 
2460
        finally:
 
2461
            target.unlock()
 
2462
    
 
2463
    """
 
2464
    # This is very similar to bzrlib.decorators.needs_write_lock.  Perhaps they
 
2465
    # should share code?
 
2466
    target.lock_write()
 
2467
    try:
 
2468
        result = callable(*args, **kwargs)
 
2469
    except:
 
2470
        exc_info = sys.exc_info()
 
2471
        try:
 
2472
            target.unlock()
 
2473
        finally:
 
2474
            raise exc_info[0], exc_info[1], exc_info[2]
 
2475
    else:
 
2476
        target.unlock()
 
2477
        return result