~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: 2010-05-11 11:47:36 UTC
  • mfrom: (5200.3.8 lock_return)
  • Revision ID: pqm@pqm.ubuntu.com-20100511114736-mc1sq9zyo3vufec7
(lifeless) Provide a consistent interface to Tree, Branch,
 Repository where lock methods return an object with an unlock method to
 unlock the lock. This breaks the API for Branch,
 Repository on their lock_write methods. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
50
50
from bzrlib.hooks import HookPoint, Hooks
51
51
from bzrlib.inter import InterObject
52
 
from bzrlib.lock import _RelockDebugMixin
 
52
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
53
53
from bzrlib import registry
54
54
from bzrlib.symbol_versioning import (
55
55
    deprecated_in,
283
283
        new_history.reverse()
284
284
        return new_history
285
285
 
286
 
    def lock_write(self):
 
286
    def lock_write(self, token=None):
 
287
        """Lock the branch for write operations.
 
288
 
 
289
        :param token: A token to permit reacquiring a previously held and
 
290
            preserved lock.
 
291
        :return: A BranchWriteLockResult.
 
292
        """
287
293
        raise NotImplementedError(self.lock_write)
288
294
 
289
295
    def lock_read(self):
 
296
        """Lock the branch for read operations.
 
297
 
 
298
        :return: A bzrlib.lock.LogicalLockResult.
 
299
        """
290
300
        raise NotImplementedError(self.lock_read)
291
301
 
292
302
    def unlock(self):
2269
2279
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2270
2280
 
2271
2281
 
 
2282
class BranchWriteLockResult(LogicalLockResult):
 
2283
    """The result of write locking a branch.
 
2284
 
 
2285
    :ivar branch_token: The token obtained from the underlying branch lock, or
 
2286
        None.
 
2287
    :ivar unlock: A callable which will unlock the lock.
 
2288
    """
 
2289
 
 
2290
    def __init__(self, unlock, branch_token):
 
2291
        LogicalLockResult.__init__(self, unlock)
 
2292
        self.branch_token = branch_token
 
2293
 
 
2294
    def __repr__(self):
 
2295
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
 
2296
            self.unlock)
 
2297
 
 
2298
 
2272
2299
class BzrBranch(Branch, _RelockDebugMixin):
2273
2300
    """A branch stored in the actual filesystem.
2274
2301
 
2328
2355
        return self.control_files.is_locked()
2329
2356
 
2330
2357
    def lock_write(self, token=None):
 
2358
        """Lock the branch for write operations.
 
2359
 
 
2360
        :param token: A token to permit reacquiring a previously held and
 
2361
            preserved lock.
 
2362
        :return: A BranchWriteLockResult.
 
2363
        """
2331
2364
        if not self.is_locked():
2332
2365
            self._note_lock('w')
2333
2366
        # All-in-one needs to always unlock/lock.
2339
2372
        else:
2340
2373
            took_lock = False
2341
2374
        try:
2342
 
            return self.control_files.lock_write(token=token)
 
2375
            return BranchWriteLockResult(self.unlock,
 
2376
                self.control_files.lock_write(token=token))
2343
2377
        except:
2344
2378
            if took_lock:
2345
2379
                self.repository.unlock()
2346
2380
            raise
2347
2381
 
2348
2382
    def lock_read(self):
 
2383
        """Lock the branch for read operations.
 
2384
 
 
2385
        :return: A bzrlib.lock.LogicalLockResult.
 
2386
        """
2349
2387
        if not self.is_locked():
2350
2388
            self._note_lock('r')
2351
2389
        # All-in-one needs to always unlock/lock.
2358
2396
            took_lock = False
2359
2397
        try:
2360
2398
            self.control_files.lock_read()
 
2399
            return LogicalLockResult(self.unlock)
2361
2400
        except:
2362
2401
            if took_lock:
2363
2402
                self.repository.unlock()