~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: John Arbash Meinel
  • Date: 2010-05-11 14:13:31 UTC
  • mto: (5218.2.2 bytes_to_entry_c)
  • mto: This revision was merged to the branch mainline in revision 5225.
  • Revision ID: john@arbash-meinel.com-20100511141331-rizo2ez6bze3ao66
Some small tweaks to the chk_map code.

Find out that we actually weren't using the global definition because we
were assigning inside the if block. So factor that out into a helper.

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):
1346
1356
        """
1347
1357
        # XXX: Fix the bzrdir API to allow getting the branch back from the
1348
1358
        # clone call. Or something. 20090224 RBC/spiv.
 
1359
        # XXX: Should this perhaps clone colocated branches as well, 
 
1360
        # rather than just the default branch? 20100319 JRV
1349
1361
        if revision_id is None:
1350
1362
            revision_id = self.last_revision()
1351
1363
        dir_to = self.bzrdir.clone_on_transport(to_transport,
1521
1533
        """Return the current default format."""
1522
1534
        return klass._default_format
1523
1535
 
1524
 
    def get_reference(self, a_bzrdir):
 
1536
    def get_reference(self, a_bzrdir, name=None):
1525
1537
        """Get the target reference of the branch in a_bzrdir.
1526
1538
 
1527
1539
        format probing must have been completed before calling
1529
1541
        in a_bzrdir is correct.
1530
1542
 
1531
1543
        :param a_bzrdir: The bzrdir to get the branch data from.
 
1544
        :param name: Name of the colocated branch to fetch
1532
1545
        :return: None if the branch is not a reference branch.
1533
1546
        """
1534
1547
        return None
1535
1548
 
1536
1549
    @classmethod
1537
 
    def set_reference(self, a_bzrdir, to_branch):
 
1550
    def set_reference(self, a_bzrdir, name, to_branch):
1538
1551
        """Set the target reference of the branch in a_bzrdir.
1539
1552
 
1540
1553
        format probing must have been completed before calling
1542
1555
        in a_bzrdir is correct.
1543
1556
 
1544
1557
        :param a_bzrdir: The bzrdir to set the branch reference for.
 
1558
        :param name: Name of colocated branch to set, None for default
1545
1559
        :param to_branch: branch that the checkout is to reference
1546
1560
        """
1547
1561
        raise NotImplementedError(self.set_reference)
2157
2171
        """See BranchFormat.get_format_description()."""
2158
2172
        return "Checkout reference format 1"
2159
2173
 
2160
 
    def get_reference(self, a_bzrdir):
 
2174
    def get_reference(self, a_bzrdir, name=None):
2161
2175
        """See BranchFormat.get_reference()."""
2162
 
        transport = a_bzrdir.get_branch_transport(None)
 
2176
        transport = a_bzrdir.get_branch_transport(None, name=name)
2163
2177
        return transport.get_bytes('location')
2164
2178
 
2165
 
    def set_reference(self, a_bzrdir, to_branch):
 
2179
    def set_reference(self, a_bzrdir, name, to_branch):
2166
2180
        """See BranchFormat.set_reference()."""
2167
 
        transport = a_bzrdir.get_branch_transport(None)
 
2181
        transport = a_bzrdir.get_branch_transport(None, name=name)
2168
2182
        location = transport.put_bytes('location', to_branch.base)
2169
2183
 
2170
2184
    def initialize(self, a_bzrdir, name=None, target_branch=None):
2221
2235
                raise AssertionError("wrong format %r found for %r" %
2222
2236
                    (format, self))
2223
2237
        if location is None:
2224
 
            location = self.get_reference(a_bzrdir)
 
2238
            location = self.get_reference(a_bzrdir, name)
2225
2239
        real_bzrdir = bzrdir.BzrDir.open(
2226
2240
            location, possible_transports=possible_transports)
2227
2241
        result = real_bzrdir.open_branch(name=name, 
2265
2279
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2266
2280
 
2267
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
 
2268
2299
class BzrBranch(Branch, _RelockDebugMixin):
2269
2300
    """A branch stored in the actual filesystem.
2270
2301
 
2324
2355
        return self.control_files.is_locked()
2325
2356
 
2326
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
        """
2327
2364
        if not self.is_locked():
2328
2365
            self._note_lock('w')
2329
2366
        # All-in-one needs to always unlock/lock.
2335
2372
        else:
2336
2373
            took_lock = False
2337
2374
        try:
2338
 
            return self.control_files.lock_write(token=token)
 
2375
            return BranchWriteLockResult(self.unlock,
 
2376
                self.control_files.lock_write(token=token))
2339
2377
        except:
2340
2378
            if took_lock:
2341
2379
                self.repository.unlock()
2342
2380
            raise
2343
2381
 
2344
2382
    def lock_read(self):
 
2383
        """Lock the branch for read operations.
 
2384
 
 
2385
        :return: A bzrlib.lock.LogicalLockResult.
 
2386
        """
2345
2387
        if not self.is_locked():
2346
2388
            self._note_lock('r')
2347
2389
        # All-in-one needs to always unlock/lock.
2354
2396
            took_lock = False
2355
2397
        try:
2356
2398
            self.control_files.lock_read()
 
2399
            return LogicalLockResult(self.unlock)
2357
2400
        except:
2358
2401
            if took_lock:
2359
2402
                self.repository.unlock()