~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:54:05 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506235405-wii4elupfhzl3jvy
Add __str__ to the new helper classes.

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
    def __str__(self):
 
2292
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
 
2293
            self.unlock)
 
2294
 
 
2295
 
2268
2296
class BzrBranch(Branch, _RelockDebugMixin):
2269
2297
    """A branch stored in the actual filesystem.
2270
2298
 
2324
2352
        return self.control_files.is_locked()
2325
2353
 
2326
2354
    def lock_write(self, token=None):
 
2355
        """Lock the branch for write operations.
 
2356
 
 
2357
        :param token: A token to permit reacquiring a previously held and
 
2358
            preserved lock.
 
2359
        :return: A BranchWriteLockResult.
 
2360
        """
2327
2361
        if not self.is_locked():
2328
2362
            self._note_lock('w')
2329
2363
        # All-in-one needs to always unlock/lock.
2335
2369
        else:
2336
2370
            took_lock = False
2337
2371
        try:
2338
 
            return self.control_files.lock_write(token=token)
 
2372
            return BranchWriteLockResult(self.unlock,
 
2373
                self.control_files.lock_write(token=token))
2339
2374
        except:
2340
2375
            if took_lock:
2341
2376
                self.repository.unlock()
2342
2377
            raise
2343
2378
 
2344
2379
    def lock_read(self):
 
2380
        """Lock the branch for read operations.
 
2381
 
 
2382
        :return: An object with an unlock method which will release the lock
 
2383
            obtained.
 
2384
        """
2345
2385
        if not self.is_locked():
2346
2386
            self._note_lock('r')
2347
2387
        # All-in-one needs to always unlock/lock.
2354
2394
            took_lock = False
2355
2395
        try:
2356
2396
            self.control_files.lock_read()
 
2397
            return self
2357
2398
        except:
2358
2399
            if took_lock:
2359
2400
                self.repository.unlock()