~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Robert Collins
  • Date: 2010-05-11 08:36:16 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100511083616-b8fjb19zomwupid0
Make all lock methods return Result objects, rather than lock_read returning self, as per John's review.

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):
2265
2275
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2266
2276
 
2267
2277
 
 
2278
class BranchWriteLockResult(LogicalLockResult):
 
2279
    """The result of write locking a branch.
 
2280
 
 
2281
    :ivar branch_token: The token obtained from the underlying branch lock, or
 
2282
        None.
 
2283
    :ivar unlock: A callable which will unlock the lock.
 
2284
    """
 
2285
 
 
2286
    def __init__(self, unlock, branch_token):
 
2287
        LogicalLockResult.__init__(self, unlock)
 
2288
        self.branch_token = branch_token
 
2289
 
 
2290
    def __repr__(self):
 
2291
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
 
2292
            self.unlock)
 
2293
 
 
2294
 
2268
2295
class BzrBranch(Branch, _RelockDebugMixin):
2269
2296
    """A branch stored in the actual filesystem.
2270
2297
 
2324
2351
        return self.control_files.is_locked()
2325
2352
 
2326
2353
    def lock_write(self, token=None):
 
2354
        """Lock the branch for write operations.
 
2355
 
 
2356
        :param token: A token to permit reacquiring a previously held and
 
2357
            preserved lock.
 
2358
        :return: A BranchWriteLockResult.
 
2359
        """
2327
2360
        if not self.is_locked():
2328
2361
            self._note_lock('w')
2329
2362
        # All-in-one needs to always unlock/lock.
2335
2368
        else:
2336
2369
            took_lock = False
2337
2370
        try:
2338
 
            return self.control_files.lock_write(token=token)
 
2371
            return BranchWriteLockResult(self.unlock,
 
2372
                self.control_files.lock_write(token=token))
2339
2373
        except:
2340
2374
            if took_lock:
2341
2375
                self.repository.unlock()
2342
2376
            raise
2343
2377
 
2344
2378
    def lock_read(self):
 
2379
        """Lock the branch for read operations.
 
2380
 
 
2381
        :return: A bzrlib.lock.LogicalLockResult.
 
2382
        """
2345
2383
        if not self.is_locked():
2346
2384
            self._note_lock('r')
2347
2385
        # All-in-one needs to always unlock/lock.
2354
2392
            took_lock = False
2355
2393
        try:
2356
2394
            self.control_files.lock_read()
 
2395
            return LogicalLockResult(self.unlock)
2357
2396
        except:
2358
2397
            if took_lock:
2359
2398
                self.repository.unlock()