~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

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