~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Robert Collins
  • Date: 2005-10-07 04:10:46 UTC
  • mto: This revision was merged to the branch mainline in revision 1420.
  • Revision ID: robertc@robertcollins.net-20051007041046-10b07ae31ecde799
introduce transactions for grouping actions done to and with branches

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from bzrlib.osutils import (isdir, quotefn, compact_date, rand_bytes, 
30
30
                            rename, splitpath, sha_file, appendpath, 
31
31
                            file_kind)
 
32
import bzrlib.errors as errors
32
33
from bzrlib.errors import (BzrError, InvalidRevisionNumber, InvalidRevisionId,
33
34
                           NoSuchRevision, HistoryMissing, NotBranchError,
34
35
                           DivergedBranches, LockError, UnlistableStore,
42
43
from bzrlib.store.compressed_text import CompressedTextStore
43
44
from bzrlib.store.text import TextStore
44
45
from bzrlib.store.weave import WeaveStore
 
46
import bzrlib.transactions as transactions
45
47
from bzrlib.transport import Transport, get_transport
46
48
import bzrlib.xml5
47
49
import bzrlib.ui
268
270
            self.control_weaves = get_weave([])
269
271
            self.weave_store = get_weave('weaves')
270
272
            self.revision_store = get_store('revision-store', compressed=False)
 
273
        self._transaction = None
271
274
 
272
275
    def __str__(self):
273
276
        return '%s(%r)' % (self.__class__.__name__, self._transport.base)
304
307
 
305
308
    base = property(_get_base)
306
309
 
 
310
    def _finish_transaction(self):
 
311
        """Exit the current transaction."""
 
312
        if self._transaction is None:
 
313
            raise errors.LockError('Branch %s is not in a transaction' %
 
314
                                   self)
 
315
        transaction = self._transaction
 
316
        self._transaction = None
 
317
        transaction.finish()
 
318
 
 
319
    def get_transaction(self):
 
320
        """Return the current active transaction.
 
321
 
 
322
        If no transaction is active, this returns a passthrough object
 
323
        for which all data is immedaitely flushed and no caching happens.
 
324
        """
 
325
        if self._transaction is None:
 
326
            return transactions.PassThroughTransaction()
 
327
        else:
 
328
            return self._transaction
 
329
 
 
330
    def _set_transaction(self, new_transaction):
 
331
        """Set a new active transaction."""
 
332
        if self._transaction is not None:
 
333
            raise errors.LockError('Branch %s is in a transaction already.' %
 
334
                                   self)
 
335
        self._transaction = new_transaction
307
336
 
308
337
    def lock_write(self):
309
338
        # TODO: Upgrade locking to support using a Transport,
318
347
                    self._rel_controlfilename('branch-lock'))
319
348
            self._lock_mode = 'w'
320
349
            self._lock_count = 1
 
350
            self._set_transaction(transactions.PassThroughTransaction())
321
351
 
322
352
 
323
353
    def lock_read(self):
330
360
                    self._rel_controlfilename('branch-lock'))
331
361
            self._lock_mode = 'r'
332
362
            self._lock_count = 1
 
363
            self._set_transaction(transactions.ReadOnlyTransaction())
333
364
                        
334
365
    def unlock(self):
335
366
        if not self._lock_mode:
338
369
        if self._lock_count > 1:
339
370
            self._lock_count -= 1
340
371
        else:
 
372
            self._finish_transaction()
341
373
            self._lock.unlock()
342
374
            self._lock = None
343
375
            self._lock_mode = self._lock_count = None