/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
70 by mbp at sourcefrog
Prepare for smart recursive add.
1
# Copyright (C) 2005 Canonical Ltd
2
1 by mbp at sourcefrog
import from baz patch-364
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
18
from copy import deepcopy
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
19
from cStringIO import StringIO
20
import errno
21
import os
1497 by Robert Collins
Move Branch.read_working_inventory to WorkingTree.
22
import shutil
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
23
import sys
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
24
from unittest import TestSuite
1372 by Martin Pool
- avoid converting inventories to/from StringIO
25
from warnings import warn
26
1 by mbp at sourcefrog
import from baz patch-364
27
28
import bzrlib
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
29
from bzrlib.config import TreeConfig
30
from bzrlib.delta import compare_trees
31
import bzrlib.errors as errors
32
from bzrlib.errors import (BzrError, InvalidRevisionNumber, InvalidRevisionId,
33
                           NoSuchRevision, HistoryMissing, NotBranchError,
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
34
                           DivergedBranches, LockError, 
35
                           UninitializableFormat,
36
                           UnlistableStore,
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
37
                           UnlistableBranch, NoSuchFile, NotVersionedError,
38
                           NoWorkingTree)
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
39
import bzrlib.inventory as inventory
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
40
from bzrlib.inventory import Inventory
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
41
from bzrlib.osutils import (isdir, quotefn,
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
42
                            rename, splitpath, sha_file,
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
43
                            file_kind, abspath, normpath, pathjoin,
44
                            safe_unicode,
45
                            )
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
46
from bzrlib.textui import show_status
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
47
from bzrlib.trace import mutter, note
48
from bzrlib.tree import EmptyTree, RevisionTree
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
49
from bzrlib.revision import (Revision, is_ancestor, get_intervening_revisions,
50
                             NULL_REVISION)
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
51
from bzrlib.store import copy_all
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
52
from bzrlib.store.text import TextStore
1393.2.3 by John Arbash Meinel
Fixing typos, updating stores, getting tests to pass.
53
from bzrlib.store.weave import WeaveStore
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
54
from bzrlib.symbol_versioning import deprecated_nonce, deprecated_passed
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
55
from bzrlib.testament import Testament
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
56
import bzrlib.transactions as transactions
1393.2.4 by John Arbash Meinel
All tests pass.
57
from bzrlib.transport import Transport, get_transport
1189 by Martin Pool
- BROKEN: partial support for commit into weave
58
import bzrlib.xml5
1104 by Martin Pool
- Add a simple UIFactory
59
import bzrlib.ui
60
1094 by Martin Pool
- merge aaron's merge improvements 999..1008
61
1186 by Martin Pool
- start implementing v5 format; Branch refuses to operate on old branches
62
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
63
BZR_BRANCH_FORMAT_5 = "Bazaar-NG branch, format 5\n"
1429 by Robert Collins
merge in niemeyers prefixed-store patch
64
BZR_BRANCH_FORMAT_6 = "Bazaar-NG branch, format 6\n"
1 by mbp at sourcefrog
import from baz patch-364
65
## TODO: Maybe include checks for common corruption of newlines, etc?
66
67
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
68
# TODO: Some operations like log might retrieve the same revisions
69
# repeatedly to calculate deltas.  We could perhaps have a weakref
1223 by Martin Pool
- store inventories in weave
70
# cache in memory to make this faster.  In general anything can be
71
# cached in memory between lock and unlock operations.
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
72
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
73
def find_branch(*ignored, **ignored_too):
74
    # XXX: leave this here for about one release, then remove it
75
    raise NotImplementedError('find_branch() is not supported anymore, '
76
                              'please use one of the new branch constructors')
416 by Martin Pool
- bzr log and bzr root now accept an http URL
77
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
78
79
def needs_read_lock(unbound):
80
    """Decorate unbound to take out and release a read lock."""
81
    def decorated(self, *args, **kwargs):
82
        self.lock_read()
83
        try:
84
            return unbound(self, *args, **kwargs)
85
        finally:
86
            self.unlock()
87
    return decorated
88
89
90
def needs_write_lock(unbound):
91
    """Decorate unbound to take out and release a write lock."""
92
    def decorated(self, *args, **kwargs):
93
        self.lock_write()
94
        try:
95
            return unbound(self, *args, **kwargs)
96
        finally:
97
            self.unlock()
98
    return decorated
99
1 by mbp at sourcefrog
import from baz patch-364
100
######################################################################
101
# branch objects
102
558 by Martin Pool
- All top-level classes inherit from object
103
class Branch(object):
1 by mbp at sourcefrog
import from baz patch-364
104
    """Branch holding a history of revisions.
105
343 by Martin Pool
doc
106
    base
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
107
        Base directory/url of the branch.
108
    """
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
109
    # this is really an instance variable - FIXME move it there
110
    # - RBC 20060112
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
111
    base = None
112
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
113
    _default_initializer = None
114
    """The default initializer for making new branches."""
115
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
116
    def __init__(self, *ignored, **ignored_too):
117
        raise NotImplementedError('The Branch class is abstract')
118
119
    @staticmethod
1393.1.2 by Martin Pool
- better representation in Branch factories of opening old formats
120
    def open_downlevel(base):
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
121
        """Open a branch which may be of an old format."""
122
        return Branch.open(base, _unsupported=True)
1393.1.2 by Martin Pool
- better representation in Branch factories of opening old formats
123
        
124
    @staticmethod
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
125
    def open(base, _unsupported=False):
126
        """Open an existing branch, rooted at 'base' (url)
127
        
128
        _unsupported is a private parameter to the Branch class.
129
        """
1393.2.4 by John Arbash Meinel
All tests pass.
130
        t = get_transport(base)
1393.1.63 by Martin Pool
- add some trace statements
131
        mutter("trying to open %r with transport %r", base, t)
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
132
        format = BzrBranchFormat.find_format(t)
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
133
        if not _unsupported and not format.is_supported():
134
            # see open_downlevel to open legacy branches.
135
            raise errors.UnsupportedFormatError(
136
                    'sorry, branch format %s not supported' % format,
137
                    ['use a different bzr version',
138
                     'or remove the .bzr directory'
139
                     ' and "bzr init" again'])
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
140
        return format.open(t)
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
141
142
    @staticmethod
1185.2.8 by Lalo Martins
creating the new branch constructors
143
    def open_containing(url):
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
144
        """Open an existing branch which contains url.
145
        
146
        This probes for a branch at url, and searches upwards from there.
1185.17.2 by Martin Pool
[pick] avoid problems in fetching when .bzr is not listable
147
148
        Basically we keep looking up until we find the control directory or
149
        run into the root.  If there isn't one, raises NotBranchError.
1442.1.64 by Robert Collins
Branch.open_containing now returns a tuple (Branch, relative-path).
150
        If there is one, it is returned, along with the unused portion of url.
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
151
        """
1393.2.4 by John Arbash Meinel
All tests pass.
152
        t = get_transport(url)
1185.17.2 by Martin Pool
[pick] avoid problems in fetching when .bzr is not listable
153
        while True:
154
            try:
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
155
                format = BzrBranchFormat.find_format(t)
156
                return format.open(t), t.relpath(url)
157
            # TODO FIXME, distinguish between formats that cannot be
158
            # identified, and a lack of format.
1185.31.38 by John Arbash Meinel
Changing os.path.normpath to osutils.normpath
159
            except NotBranchError, e:
160
                mutter('not a branch in: %r %s', t.base, e)
1185.17.2 by Martin Pool
[pick] avoid problems in fetching when .bzr is not listable
161
            new_t = t.clone('..')
162
            if new_t.base == t.base:
163
                # reached the root, whatever that may be
1185.16.61 by mbp at sourcefrog
- start introducing hct error classes
164
                raise NotBranchError(path=url)
1185.17.2 by Martin Pool
[pick] avoid problems in fetching when .bzr is not listable
165
            t = new_t
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
166
167
    @staticmethod
168
    def initialize(base):
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
169
        """Create a new branch, rooted at 'base' (url)
170
        
171
        This will call the current default initializer with base
172
        as the only parameter.
173
        """
174
        return Branch._default_initializer(safe_unicode(base))
175
176
    @staticmethod
177
    def get_default_initializer():
178
        """Return the initializer being used for new branches."""
179
        return Branch._default_initializer
180
181
    @staticmethod
182
    def set_default_initializer(initializer):
183
        """Set the initializer to be used for new branches."""
184
        Branch._default_initializer = staticmethod(initializer)
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
185
186
    def setup_caching(self, cache_root):
187
        """Subclasses that care about caching should override this, and set
188
        up cached stores located under cache_root.
189
        """
1400.1.1 by Robert Collins
implement a basic test for the ui branch command from http servers
190
        self.cache_root = cache_root
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
191
1185.35.11 by Aaron Bentley
Added support for branch nicks
192
    def _get_nick(self):
193
        cfg = self.tree_config()
1530.1.3 by Robert Collins
transport implementations now tested consistently.
194
        return cfg.get_option(u"nickname", default=self.base.split('/')[-2])
1185.35.11 by Aaron Bentley
Added support for branch nicks
195
196
    def _set_nick(self, nick):
197
        cfg = self.tree_config()
198
        cfg.set_option(nick, "nickname")
199
        assert cfg.get_option("nickname") == nick
200
201
    nick = property(_get_nick, _set_nick)
202
        
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
203
    def push_stores(self, branch_to):
204
        """Copy the content of this branches store to branch_to."""
205
        raise NotImplementedError('push_stores is abstract')
206
207
    def get_transaction(self):
208
        """Return the current active transaction.
209
210
        If no transaction is active, this returns a passthrough object
211
        for which all data is immediately flushed and no caching happens.
212
        """
213
        raise NotImplementedError('get_transaction is abstract')
214
215
    def lock_write(self):
216
        raise NotImplementedError('lock_write is abstract')
217
        
218
    def lock_read(self):
219
        raise NotImplementedError('lock_read is abstract')
220
221
    def unlock(self):
222
        raise NotImplementedError('unlock is abstract')
223
224
    def abspath(self, name):
225
        """Return absolute filename for something in the branch
226
        
227
        XXX: Robert Collins 20051017 what is this used for? why is it a branch
228
        method and not a tree method.
229
        """
230
        raise NotImplementedError('abspath is abstract')
231
232
    def controlfilename(self, file_or_path):
233
        """Return location relative to branch."""
234
        raise NotImplementedError('controlfilename is abstract')
235
236
    def controlfile(self, file_or_path, mode='r'):
237
        """Open a control file for this branch.
238
239
        There are two classes of file in the control directory: text
240
        and binary.  binary files are untranslated byte streams.  Text
241
        control files are stored with Unix newlines and in UTF-8, even
242
        if the platform or locale defaults are different.
243
244
        Controlfiles should almost never be opened in write mode but
245
        rather should be atomically copied and replaced using atomicfile.
246
        """
247
        raise NotImplementedError('controlfile is abstract')
248
249
    def put_controlfile(self, path, f, encode=True):
250
        """Write an entry as a controlfile.
251
252
        :param path: The path to put the file, relative to the .bzr control
253
                     directory
254
        :param f: A file-like or string object whose contents should be copied.
255
        :param encode:  If true, encode the contents as utf-8
256
        """
257
        raise NotImplementedError('put_controlfile is abstract')
258
259
    def put_controlfiles(self, files, encode=True):
260
        """Write several entries as controlfiles.
261
262
        :param files: A list of [(path, file)] pairs, where the path is the directory
263
                      underneath the bzr control directory
264
        :param encode:  If true, encode the contents as utf-8
265
        """
266
        raise NotImplementedError('put_controlfiles is abstract')
267
268
    def get_root_id(self):
269
        """Return the id of this branches root"""
270
        raise NotImplementedError('get_root_id is abstract')
271
272
    def set_root_id(self, file_id):
273
        raise NotImplementedError('set_root_id is abstract')
274
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
275
    def print_file(self, file, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
276
        """Print `file` to stdout."""
277
        raise NotImplementedError('print_file is abstract')
278
279
    def append_revision(self, *revision_ids):
280
        raise NotImplementedError('append_revision is abstract')
281
282
    def set_revision_history(self, rev_history):
283
        raise NotImplementedError('set_revision_history is abstract')
284
285
    def has_revision(self, revision_id):
286
        """True if this branch has a copy of the revision.
287
288
        This does not necessarily imply the revision is merge
289
        or on the mainline."""
290
        raise NotImplementedError('has_revision is abstract')
291
292
    def get_revision_xml(self, revision_id):
293
        raise NotImplementedError('get_revision_xml is abstract')
294
295
    def get_revision(self, revision_id):
296
        """Return the Revision object for a named revision"""
297
        raise NotImplementedError('get_revision is abstract')
298
299
    def get_revision_delta(self, revno):
300
        """Return the delta for one revision.
301
302
        The delta is relative to its mainline predecessor, or the
303
        empty tree for revision 1.
304
        """
1495.1.3 by Jelmer Vernooij
Move some more generic methods from NativeBranch to Branch.
305
        assert isinstance(revno, int)
306
        rh = self.revision_history()
307
        if not (1 <= revno <= len(rh)):
308
            raise InvalidRevisionNumber(revno)
309
310
        # revno is 1-based; list is 0-based
311
312
        new_tree = self.revision_tree(rh[revno-1])
313
        if revno == 1:
314
            old_tree = EmptyTree()
315
        else:
316
            old_tree = self.revision_tree(rh[revno-2])
317
318
        return compare_trees(old_tree, new_tree)
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
319
320
    def get_revision_sha1(self, revision_id):
321
        """Hash the stored value of a revision, and return it."""
322
        raise NotImplementedError('get_revision_sha1 is abstract')
323
324
    def get_ancestry(self, revision_id):
325
        """Return a list of revision-ids integrated by a revision.
326
        
327
        This currently returns a list, but the ordering is not guaranteed:
328
        treat it as a set.
329
        """
330
        raise NotImplementedError('get_ancestry is abstract')
331
332
    def get_inventory(self, revision_id):
333
        """Get Inventory object by hash."""
334
        raise NotImplementedError('get_inventory is abstract')
335
336
    def get_inventory_xml(self, revision_id):
337
        """Get inventory XML as a file object."""
338
        raise NotImplementedError('get_inventory_xml is abstract')
339
340
    def get_inventory_sha1(self, revision_id):
341
        """Return the sha1 hash of the inventory entry."""
342
        raise NotImplementedError('get_inventory_sha1 is abstract')
343
344
    def get_revision_inventory(self, revision_id):
345
        """Return inventory of a past revision."""
346
        raise NotImplementedError('get_revision_inventory is abstract')
347
348
    def revision_history(self):
349
        """Return sequence of revision hashes on to this branch."""
350
        raise NotImplementedError('revision_history is abstract')
351
352
    def revno(self):
353
        """Return current revision number for this branch.
354
355
        That is equivalent to the number of revisions committed to
356
        this branch.
357
        """
358
        return len(self.revision_history())
359
360
    def last_revision(self):
361
        """Return last patch hash, or None if no history."""
362
        ph = self.revision_history()
363
        if ph:
364
            return ph[-1]
365
        else:
366
            return None
367
368
    def missing_revisions(self, other, stop_revision=None, diverged_ok=False):
369
        """Return a list of new revisions that would perfectly fit.
370
        
371
        If self and other have not diverged, return a list of the revisions
372
        present in other, but missing from self.
373
374
        >>> from bzrlib.commit import commit
375
        >>> bzrlib.trace.silent = True
376
        >>> br1 = ScratchBranch()
377
        >>> br2 = ScratchBranch()
378
        >>> br1.missing_revisions(br2)
379
        []
380
        >>> commit(br2, "lala!", rev_id="REVISION-ID-1")
381
        >>> br1.missing_revisions(br2)
382
        [u'REVISION-ID-1']
383
        >>> br2.missing_revisions(br1)
384
        []
385
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1")
386
        >>> br1.missing_revisions(br2)
387
        []
388
        >>> commit(br2, "lala!", rev_id="REVISION-ID-2A")
389
        >>> br1.missing_revisions(br2)
390
        [u'REVISION-ID-2A']
391
        >>> commit(br1, "lala!", rev_id="REVISION-ID-2B")
392
        >>> br1.missing_revisions(br2)
393
        Traceback (most recent call last):
1185.56.1 by Michael Ellerman
Simplify handling of DivergedBranches in cmd_pull()
394
        DivergedBranches: These branches have diverged.  Try merge.
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
395
        """
396
        self_history = self.revision_history()
397
        self_len = len(self_history)
398
        other_history = other.revision_history()
399
        other_len = len(other_history)
400
        common_index = min(self_len, other_len) -1
401
        if common_index >= 0 and \
402
            self_history[common_index] != other_history[common_index]:
403
            raise DivergedBranches(self, other)
404
405
        if stop_revision is None:
406
            stop_revision = other_len
407
        else:
408
            assert isinstance(stop_revision, int)
409
            if stop_revision > other_len:
410
                raise bzrlib.errors.NoSuchRevision(self, stop_revision)
411
        return other_history[self_len:stop_revision]
412
    
413
    def update_revisions(self, other, stop_revision=None):
414
        """Pull in new perfect-fit revisions."""
415
        raise NotImplementedError('update_revisions is abstract')
416
417
    def pullable_revisions(self, other, stop_revision):
418
        raise NotImplementedError('pullable_revisions is abstract')
419
        
420
    def revision_id_to_revno(self, revision_id):
421
        """Given a revision id, return its revno"""
422
        if revision_id is None:
423
            return 0
424
        history = self.revision_history()
425
        try:
426
            return history.index(revision_id) + 1
427
        except ValueError:
428
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
429
430
    def get_rev_id(self, revno, history=None):
431
        """Find the revision id of the specified revno."""
432
        if revno == 0:
433
            return None
434
        if history is None:
435
            history = self.revision_history()
436
        elif revno <= 0 or revno > len(history):
437
            raise bzrlib.errors.NoSuchRevision(self, revno)
438
        return history[revno - 1]
439
440
    def revision_tree(self, revision_id):
441
        """Return Tree for a revision on this branch.
442
443
        `revision_id` may be None for the null revision, in which case
444
        an `EmptyTree` is returned."""
445
        raise NotImplementedError('revision_tree is abstract')
446
447
    def working_tree(self):
1508.1.15 by Robert Collins
Merge from mpool.
448
        """Return a `Tree` for the working copy if this is a local branch."""
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
449
        raise NotImplementedError('working_tree is abstract')
450
451
    def pull(self, source, overwrite=False):
452
        raise NotImplementedError('pull is abstract')
453
454
    def basis_tree(self):
455
        """Return `Tree` object for last revision.
456
457
        If there are no revisions yet, return an `EmptyTree`.
458
        """
459
        return self.revision_tree(self.last_revision())
460
461
    def rename_one(self, from_rel, to_rel):
462
        """Rename one file.
463
464
        This can change the directory or the filename or both.
465
        """
466
        raise NotImplementedError('rename_one is abstract')
467
468
    def move(self, from_paths, to_name):
469
        """Rename files.
470
471
        to_name must exist as a versioned directory.
472
473
        If to_name exists and is a directory, the files are moved into
474
        it, keeping their old names.  If it is a directory, 
475
476
        Note that to_name is only the last component of the new name;
477
        this doesn't change the directory.
478
479
        This returns a list of (from_path, to_path) pairs for each
480
        entry that is moved.
481
        """
482
        raise NotImplementedError('move is abstract')
483
484
    def get_parent(self):
485
        """Return the parent location of the branch.
486
487
        This is the default location for push/pull/missing.  The usual
488
        pattern is that the user can override it by specifying a
489
        location.
490
        """
491
        raise NotImplementedError('get_parent is abstract')
492
493
    def get_push_location(self):
494
        """Return the None or the location to push this branch to."""
495
        raise NotImplementedError('get_push_location is abstract')
496
497
    def set_push_location(self, location):
498
        """Set a new push location for this branch."""
499
        raise NotImplementedError('set_push_location is abstract')
500
501
    def set_parent(self, url):
502
        raise NotImplementedError('set_parent is abstract')
503
504
    def check_revno(self, revno):
505
        """\
506
        Check whether a revno corresponds to any revision.
507
        Zero (the NULL revision) is considered valid.
508
        """
509
        if revno != 0:
510
            self.check_real_revno(revno)
511
            
512
    def check_real_revno(self, revno):
513
        """\
514
        Check whether a revno corresponds to a real revision.
515
        Zero (the NULL revision) is considered invalid
516
        """
517
        if revno < 1 or revno > self.revno():
518
            raise InvalidRevisionNumber(revno)
519
        
520
    def sign_revision(self, revision_id, gpg_strategy):
521
        raise NotImplementedError('sign_revision is abstract')
522
523
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
524
        raise NotImplementedError('store_revision_signature is abstract')
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
525
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
526
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
527
class BzrBranchFormat(object):
528
    """An encapsulation of the initialization and open routines for a format.
529
530
    Formats provide three things:
531
     * An initialization routine,
532
     * a format string,
533
     * an open routine.
534
535
    Formats are placed in an dict by their format string for reference 
536
    during branch opening. Its not required that these be instances, they
537
    can be classes themselves with class methods - it simply depends on 
538
    whether state is needed for a given format or not.
539
540
    Once a format is deprecated, just deprecate the initialize and open
541
    methods on the format class. Do not deprecate the object, as the 
542
    object will be created every time regardless.
543
    """
544
545
    _formats = {}
546
    """The known formats."""
547
548
    @classmethod
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
549
    def find_format(klass, transport):
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
550
        """Return the format registered for URL."""
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
551
        try:
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
552
            format_string = transport.get(".bzr/branch-format").read()
553
            return klass._formats[format_string]
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
554
        except NoSuchFile:
555
            raise NotBranchError(path=transport.base)
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
556
        except KeyError:
557
            raise errors.UnknownFormatError(format_string)
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
558
559
    def get_format_string(self):
560
        """Return the ASCII format string that identifies this format."""
561
        raise NotImplementedError(self.get_format_string)
562
563
    def _find_modes(self, t):
564
        """Determine the appropriate modes for files and directories.
565
        
566
        FIXME: When this merges into, or from storage,
567
        this code becomes delgatable to a LockableFiles instance.
568
569
        For now its cribbed and returns (dir_mode, file_mode)
570
        """
571
        try:
572
            st = t.stat('.')
573
        except errors.TransportNotPossible:
574
            dir_mode = 0755
575
            file_mode = 0644
576
        else:
577
            dir_mode = st.st_mode & 07777
578
            # Remove the sticky and execute bits for files
579
            file_mode = dir_mode & ~07111
580
        if not BzrBranch._set_dir_mode:
581
            dir_mode = None
582
        if not BzrBranch._set_file_mode:
583
            file_mode = None
584
        return dir_mode, file_mode
585
586
    def initialize(self, url):
587
        """Create a branch of this format at url and return an open branch."""
588
        t = get_transport(url)
589
        from bzrlib.inventory import Inventory
590
        from bzrlib.weavefile import write_weave_v5
591
        from bzrlib.weave import Weave
592
        
593
        # Create an empty inventory
594
        sio = StringIO()
595
        # if we want per-tree root ids then this is the place to set
596
        # them; they're not needed for now and so ommitted for
597
        # simplicity.
598
        bzrlib.xml5.serializer_v5.write_inventory(Inventory(), sio)
599
        empty_inv = sio.getvalue()
600
        sio = StringIO()
601
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
602
        empty_weave = sio.getvalue()
603
604
        # Since we don't have a .bzr directory, inherit the
605
        # mode from the root directory
606
        dir_mode, file_mode = self._find_modes(t)
607
608
        t.mkdir('.bzr', mode=dir_mode)
609
        control = t.clone('.bzr')
610
        dirs = ['revision-store', 'weaves']
611
        files = [('README', 
612
            StringIO("This is a Bazaar-NG control directory.\n"
613
            "Do not change any files in this directory.\n")),
614
            ('branch-format', StringIO(self.get_format_string())),
615
            ('revision-history', StringIO('')),
616
            ('branch-name', StringIO('')),
617
            ('branch-lock', StringIO('')),
618
            ('pending-merges', StringIO('')),
619
            ('inventory', StringIO(empty_inv)),
620
            ('inventory.weave', StringIO(empty_weave)),
621
            ('ancestry.weave', StringIO(empty_weave))
622
        ]
623
        control.mkdir_multi(dirs, mode=dir_mode)
624
        control.put_multi(files, mode=file_mode)
625
        mutter('created control directory in ' + t.base)
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
626
        return BzrBranch(t, format=self)
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
627
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
628
    def is_supported(self):
629
        """Is this format supported?
630
631
        Supported formats can be initialized and opened.
632
        Unsupported formats may not support initialization or committing or 
633
        some other features depending on the reason for not being supported.
634
        """
635
        return True
636
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
637
    def open(self, transport):
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
638
        """Fill out the data in branch for the branch at url."""
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
639
        return BzrBranch(transport, format=self)
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
640
641
    @classmethod
642
    def register_format(klass, format):
643
        klass._formats[format.get_format_string()] = format
644
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
645
    @classmethod
646
    def unregister_format(klass, format):
647
        assert klass._formats[format.get_format_string()] is format
648
        del klass._formats[format.get_format_string()]
649
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
650
651
class BzrBranchFormat4(BzrBranchFormat):
652
    """Bzr branch format 4.
653
654
    This format has:
655
     - flat stores
656
     - TextStores for texts, inventories,revisions.
657
658
    This format is deprecated: it indexes texts using a text it which is
659
    removed in format 5; write support for this format has been removed.
660
    """
661
662
    def get_format_string(self):
663
        """See BzrBranchFormat.get_format_string()."""
664
        return BZR_BRANCH_FORMAT_4
665
666
    def initialize(self, url):
667
        """Format 4 branches cannot be created."""
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
668
        raise UninitializableFormat(self)
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
669
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
670
    def is_supported(self):
671
        """Format 4 is not supported.
672
673
        It is not supported because the model changed from 4 to 5 and the
674
        conversion logic is expensive - so doing it on the fly was not 
675
        feasible.
676
        """
677
        return False
678
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
679
680
class BzrBranchFormat5(BzrBranchFormat):
681
    """Bzr branch format 5.
682
683
    This format has:
684
     - weaves for file texts and inventory
685
     - flat stores
686
     - TextStores for revisions and signatures.
687
    """
688
689
    def get_format_string(self):
690
        """See BzrBranchFormat.get_format_string()."""
691
        return BZR_BRANCH_FORMAT_5
692
693
694
class BzrBranchFormat6(BzrBranchFormat):
695
    """Bzr branch format 6.
696
697
    This format has:
698
     - weaves for file texts and inventory
699
     - hash subdirectory based stores.
700
     - TextStores for revisions and signatures.
701
    """
702
703
    def get_format_string(self):
704
        """See BzrBranchFormat.get_format_string()."""
705
        return BZR_BRANCH_FORMAT_6
706
707
708
BzrBranchFormat.register_format(BzrBranchFormat4())
709
BzrBranchFormat.register_format(BzrBranchFormat5())
710
BzrBranchFormat.register_format(BzrBranchFormat6())
711
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
712
1495.1.5 by Jelmer Vernooij
Rename NativeBranch -> BzrBranch
713
class BzrBranch(Branch):
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
714
    """A branch stored in the actual filesystem.
715
716
    Note that it's "local" in the context of the filesystem; it doesn't
717
    really matter if it's on an nfs/smb/afs/coda/... share, as long as
718
    it's writable, and can be accessed via the normal filesystem API.
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
719
720
    _lock_mode
580 by Martin Pool
- Use explicit lock methods on a branch, rather than doing it
721
        None, or 'r' or 'w'
722
723
    _lock_count
724
        If _lock_mode is true, a positive count of the number of times the
725
        lock has been taken.
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
726
614 by Martin Pool
- unify two defintions of LockError
727
    _lock
728
        Lock object from bzrlib.lock.
1 by mbp at sourcefrog
import from baz patch-364
729
    """
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
730
    # We actually expect this class to be somewhat short-lived; part of its
731
    # purpose is to try to isolate what bits of the branch logic are tied to
732
    # filesystem access, so that in a later step, we can extricate them to
733
    # a separarte ("storage") class.
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
734
    _lock_mode = None
580 by Martin Pool
- Use explicit lock methods on a branch, rather than doing it
735
    _lock_count = None
615 by Martin Pool
Major rework of locking code:
736
    _lock = None
1223 by Martin Pool
- store inventories in weave
737
    _inventory_weave = None
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
738
    # If set to False (by a plugin, etc) BzrBranch will not set the
739
    # mode on created files or directories
740
    _set_file_mode = True
741
    _set_dir_mode = True
353 by Martin Pool
- Per-branch locks in read and write modes.
742
    
897 by Martin Pool
- merge john's revision-naming code
743
    # Map some sort of prefix into a namespace
744
    # stuff like "revno:10", "revid:", etc.
745
    # This should match a prefix with a function which accepts
746
    REVISION_NAMESPACES = {}
747
1391 by Robert Collins
merge from integration
748
    def push_stores(self, branch_to):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
749
        """See Branch.push_stores."""
1534.4.8 by Robert Collins
Unfuck upgrade.
750
        if (not isinstance(self._branch_format, BzrBranchFormat4) or
751
            self._branch_format != branch_to._branch_format):
1391 by Robert Collins
merge from integration
752
            from bzrlib.fetch import greedy_fetch
1534.4.8 by Robert Collins
Unfuck upgrade.
753
            mutter("Using fetch logic to push between %s(%s) and %s(%s)",
1393 by Robert Collins
reenable remotebranch tests
754
                   self, self._branch_format, branch_to, branch_to._branch_format)
1391 by Robert Collins
merge from integration
755
            greedy_fetch(to_branch=branch_to, from_branch=self,
756
                         revision=self.last_revision())
757
            return
758
1534.4.8 by Robert Collins
Unfuck upgrade.
759
        # format 4 to format 4 logic only.
1391 by Robert Collins
merge from integration
760
        store_pairs = ((self.text_store,      branch_to.text_store),
761
                       (self.inventory_store, branch_to.inventory_store),
762
                       (self.revision_store,  branch_to.revision_store))
763
        try:
764
            for from_store, to_store in store_pairs: 
765
                copy_all(from_store, to_store)
766
        except UnlistableStore:
767
            raise UnlistableBranch(from_store)
768
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
769
    def __init__(self, transport, init=deprecated_nonce,
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
770
                 relax_version_check=deprecated_nonce, format=None):
1 by mbp at sourcefrog
import from baz patch-364
771
        """Create new branch object at a particular location.
772
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
773
        transport -- A Transport object, defining how to access files.
62 by mbp at sourcefrog
- new find_branch_root function; based on suggestion from aaron
774
        
254 by Martin Pool
- Doc cleanups from Magnus Therning
775
        init -- If True, create new control files in a previously
1 by mbp at sourcefrog
import from baz patch-364
776
             unversioned directory.  If False, the branch must already
777
             be versioned.
778
1293 by Martin Pool
- add Branch constructor option to relax version check
779
        relax_version_check -- If true, the usual check for the branch
780
            version is not applied.  This is intended only for
781
            upgrade/recovery type use; it's not guaranteed that
782
            all operations will work on old format branches.
783
1 by mbp at sourcefrog
import from baz patch-364
784
        In the test suite, creation of new trees is tested using the
785
        `ScratchBranch` class.
786
        """
1393.1.15 by Martin Pool
- better assertion message
787
        assert isinstance(transport, Transport), \
788
            "%r is not a Transport" % transport
907.1.8 by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport
789
        self._transport = transport
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
790
        if deprecated_passed(init):
791
            warn("BzrBranch.__init__(..., init=XXX): The init parameter is "
792
                 "deprecated as of bzr 0.8. Please use Branch.initialize().",
793
                 DeprecationWarning)
794
            if init:
795
                # this is slower than before deprecation, oh well never mind.
796
                # -> its deprecated.
797
                self._initialize(transport.base)
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
798
        self._find_modes()
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
799
        self._check_format(format)
800
        if deprecated_passed(relax_version_check):
801
            warn("BzrBranch.__init__(..., relax_version_check=XXX_: The "
802
                 "relax_version_check parameter is deprecated as of bzr 0.8. "
803
                 "Please use Branch.open_downlevel, or a BzrBranchFormat's "
804
                 "open() method.", DeprecationWarning)
805
            if (not relax_version_check
1534.4.8 by Robert Collins
Unfuck upgrade.
806
                and not self._branch_format.is_supported()):
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
807
                raise errors.UnsupportedFormatError(
808
                        'sorry, branch format %r not supported' % fmt,
809
                        ['use a different bzr version',
810
                         'or remove the .bzr directory'
811
                         ' and "bzr init" again'])
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
812
1429 by Robert Collins
merge in niemeyers prefixed-store patch
813
        def get_store(name, compressed=True, prefixed=False):
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
814
            relpath = self._rel_controlfilename(safe_unicode(name))
1185.16.159 by John Arbash Meinel
Updated the stores, all tests pass, and a store doesn't have to be 100% compressed
815
            store = TextStore(self._transport.clone(relpath),
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
816
                              dir_mode=self._dir_mode,
817
                              file_mode=self._file_mode,
1185.16.159 by John Arbash Meinel
Updated the stores, all tests pass, and a store doesn't have to be 100% compressed
818
                              prefixed=prefixed,
819
                              compressed=compressed)
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
820
            return store
1185.33.87 by Martin Pool
[patch] refactor code that make sure stores are opened with unicode filenames (robertc)
821
1429 by Robert Collins
merge in niemeyers prefixed-store patch
822
        def get_weave(name, prefixed=False):
1185.33.87 by Martin Pool
[patch] refactor code that make sure stores are opened with unicode filenames (robertc)
823
            relpath = self._rel_controlfilename(unicode(name))
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
824
            ws = WeaveStore(self._transport.clone(relpath),
825
                            prefixed=prefixed,
826
                            dir_mode=self._dir_mode,
827
                            file_mode=self._file_mode)
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
828
            if self._transport.should_cache():
829
                ws.enable_cache = True
830
            return ws
831
1534.4.8 by Robert Collins
Unfuck upgrade.
832
        if isinstance(self._branch_format, BzrBranchFormat4):
1185.33.87 by Martin Pool
[patch] refactor code that make sure stores are opened with unicode filenames (robertc)
833
            self.inventory_store = get_store('inventory-store')
834
            self.text_store = get_store('text-store')
835
            self.revision_store = get_store('revision-store')
1534.4.8 by Robert Collins
Unfuck upgrade.
836
        elif isinstance(self._branch_format, BzrBranchFormat5):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
837
            self.control_weaves = get_weave(u'')
838
            self.weave_store = get_weave(u'weaves')
839
            self.revision_store = get_store(u'revision-store', compressed=False)
1534.4.8 by Robert Collins
Unfuck upgrade.
840
        elif isinstance(self._branch_format, BzrBranchFormat6):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
841
            self.control_weaves = get_weave(u'')
842
            self.weave_store = get_weave(u'weaves', prefixed=True)
843
            self.revision_store = get_store(u'revision-store', compressed=False,
1429 by Robert Collins
merge in niemeyers prefixed-store patch
844
                                            prefixed=True)
1442.1.59 by Robert Collins
Add re-sign command to generate a digital signature on a single revision.
845
        self.revision_store.register_suffix('sig')
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
846
        self._transaction = None
1 by mbp at sourcefrog
import from baz patch-364
847
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
848
    @staticmethod
849
    def _initialize(base):
850
        """Create a bzr branch in the latest format."""
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
851
        return BzrBranchFormat6().initialize(base)
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
852
1 by mbp at sourcefrog
import from baz patch-364
853
    def __str__(self):
907.1.5 by John Arbash Meinel
Some more work, including ScratchBranch changes.
854
        return '%s(%r)' % (self.__class__.__name__, self._transport.base)
1 by mbp at sourcefrog
import from baz patch-364
855
856
    __repr__ = __str__
857
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
858
    def __del__(self):
615 by Martin Pool
Major rework of locking code:
859
        if self._lock_mode or self._lock:
1390 by Robert Collins
pair programming worx... merge integration and weave
860
            # XXX: This should show something every time, and be suitable for
861
            # headless operation and embedding
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
862
            warn("branch %r was not explicitly unlocked" % self)
615 by Martin Pool
Major rework of locking code:
863
            self._lock.unlock()
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
864
907.1.23 by John Arbash Meinel
Branch objects now automatically create Cached stores if the protocol is_remote.
865
        # TODO: It might be best to do this somewhere else,
866
        # but it is nice for a Branch object to automatically
867
        # cache it's information.
868
        # Alternatively, we could have the Transport objects cache requests
869
        # See the earlier discussion about how major objects (like Branch)
870
        # should never expect their __del__ function to run.
1185.11.9 by John Arbash Meinel
Most tests pass, some problems with unavailable socket recv
871
        if hasattr(self, 'cache_root') and self.cache_root is not None:
907.1.23 by John Arbash Meinel
Branch objects now automatically create Cached stores if the protocol is_remote.
872
            try:
873
                shutil.rmtree(self.cache_root)
874
            except:
875
                pass
876
            self.cache_root = None
877
907.1.17 by John Arbash Meinel
Adding a Branch.base property, removing pull_loc()
878
    def _get_base(self):
907.1.19 by John Arbash Meinel
Updated ScratchBranch and Branch.base, All Tests PASS !!!
879
        if self._transport:
880
            return self._transport.base
881
        return None
907.1.17 by John Arbash Meinel
Adding a Branch.base property, removing pull_loc()
882
1442.1.5 by Robert Collins
Give branch.base a docstring.
883
    base = property(_get_base, doc="The URL for the root of this branch.")
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
884
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
885
    def _finish_transaction(self):
886
        """Exit the current transaction."""
887
        if self._transaction is None:
888
            raise errors.LockError('Branch %s is not in a transaction' %
889
                                   self)
890
        transaction = self._transaction
891
        self._transaction = None
892
        transaction.finish()
893
894
    def get_transaction(self):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
895
        """See Branch.get_transaction."""
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
896
        if self._transaction is None:
897
            return transactions.PassThroughTransaction()
898
        else:
899
            return self._transaction
900
901
    def _set_transaction(self, new_transaction):
902
        """Set a new active transaction."""
903
        if self._transaction is not None:
904
            raise errors.LockError('Branch %s is in a transaction already.' %
905
                                   self)
906
        self._transaction = new_transaction
610 by Martin Pool
- replace Branch.lock(mode) with separate lock_read and lock_write
907
908
    def lock_write(self):
1185.50.6 by John Arbash Meinel
Fixed a broken test from my 'push updates local working tree' fix
909
        #mutter("lock write: %s (%s)", self, self._lock_count)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
910
        # TODO: Upgrade locking to support using a Transport,
911
        # and potentially a remote locking protocol
610 by Martin Pool
- replace Branch.lock(mode) with separate lock_read and lock_write
912
        if self._lock_mode:
913
            if self._lock_mode != 'w':
914
                raise LockError("can't upgrade to a write lock from %r" %
915
                                self._lock_mode)
916
            self._lock_count += 1
917
        else:
907.1.24 by John Arbash Meinel
Remote functionality work.
918
            self._lock = self._transport.lock_write(
919
                    self._rel_controlfilename('branch-lock'))
610 by Martin Pool
- replace Branch.lock(mode) with separate lock_read and lock_write
920
            self._lock_mode = 'w'
921
            self._lock_count = 1
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
922
            self._set_transaction(transactions.PassThroughTransaction())
610 by Martin Pool
- replace Branch.lock(mode) with separate lock_read and lock_write
923
924
    def lock_read(self):
1185.50.6 by John Arbash Meinel
Fixed a broken test from my 'push updates local working tree' fix
925
        #mutter("lock read: %s (%s)", self, self._lock_count)
610 by Martin Pool
- replace Branch.lock(mode) with separate lock_read and lock_write
926
        if self._lock_mode:
927
            assert self._lock_mode in ('r', 'w'), \
928
                   "invalid lock mode %r" % self._lock_mode
929
            self._lock_count += 1
930
        else:
907.1.24 by John Arbash Meinel
Remote functionality work.
931
            self._lock = self._transport.lock_read(
932
                    self._rel_controlfilename('branch-lock'))
610 by Martin Pool
- replace Branch.lock(mode) with separate lock_read and lock_write
933
            self._lock_mode = 'r'
934
            self._lock_count = 1
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
935
            self._set_transaction(transactions.ReadOnlyTransaction())
1417.1.10 by Robert Collins
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.
936
            # 5K may be excessive, but hey, its a knob.
937
            self.get_transaction().set_cache_size(5000)
610 by Martin Pool
- replace Branch.lock(mode) with separate lock_read and lock_write
938
                        
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
939
    def unlock(self):
1185.50.6 by John Arbash Meinel
Fixed a broken test from my 'push updates local working tree' fix
940
        #mutter("unlock: %s (%s)", self, self._lock_count)
578 by Martin Pool
- start to move toward Branch.lock and unlock methods,
941
        if not self._lock_mode:
610 by Martin Pool
- replace Branch.lock(mode) with separate lock_read and lock_write
942
            raise LockError('branch %r is not locked' % (self))
580 by Martin Pool
- Use explicit lock methods on a branch, rather than doing it
943
944
        if self._lock_count > 1:
945
            self._lock_count -= 1
946
        else:
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
947
            self._finish_transaction()
615 by Martin Pool
Major rework of locking code:
948
            self._lock.unlock()
949
            self._lock = None
580 by Martin Pool
- Use explicit lock methods on a branch, rather than doing it
950
            self._lock_mode = self._lock_count = None
353 by Martin Pool
- Per-branch locks in read and write modes.
951
67 by mbp at sourcefrog
use abspath() for the function that makes an absolute
952
    def abspath(self, name):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
953
        """See Branch.abspath."""
907.1.5 by John Arbash Meinel
Some more work, including ScratchBranch changes.
954
        return self._transport.abspath(name)
67 by mbp at sourcefrog
use abspath() for the function that makes an absolute
955
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
956
    def _rel_controlfilename(self, file_or_path):
1469 by Robert Collins
Change Transport.* to work with URL's.
957
        if not isinstance(file_or_path, basestring):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
958
            file_or_path = u'/'.join(file_or_path)
1469 by Robert Collins
Change Transport.* to work with URL's.
959
        if file_or_path == '':
960
            return bzrlib.BZRDIR
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
961
        return bzrlib.transport.urlescape(bzrlib.BZRDIR + u'/' + file_or_path)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
962
1 by mbp at sourcefrog
import from baz patch-364
963
    def controlfilename(self, file_or_path):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
964
        """See Branch.controlfilename."""
907.1.8 by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport
965
        return self._transport.abspath(self._rel_controlfilename(file_or_path))
1 by mbp at sourcefrog
import from baz patch-364
966
967
    def controlfile(self, file_or_path, mode='r'):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
968
        """See Branch.controlfile."""
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
969
        import codecs
970
971
        relpath = self._rel_controlfilename(file_or_path)
972
        #TODO: codecs.open() buffers linewise, so it was overloaded with
973
        # a much larger buffer, do we need to do the same for getreader/getwriter?
974
        if mode == 'rb': 
907.1.8 by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport
975
            return self._transport.get(relpath)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
976
        elif mode == 'wb':
907.1.50 by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
977
            raise BzrError("Branch.controlfile(mode='wb') is not supported, use put_controlfiles")
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
978
        elif mode == 'r':
1185.16.149 by Martin Pool
doc
979
            # XXX: Do we really want errors='replace'?   Perhaps it should be
980
            # an error, or at least reported, if there's incorrectly-encoded
981
            # data inside a file.
982
            # <https://launchpad.net/products/bzr/+bug/3823>
907.1.50 by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
983
            return codecs.getreader('utf-8')(self._transport.get(relpath), errors='replace')
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
984
        elif mode == 'w':
907.1.50 by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
985
            raise BzrError("Branch.controlfile(mode='w') is not supported, use put_controlfiles")
245 by mbp at sourcefrog
- control files always in utf-8-unix format
986
        else:
987
            raise BzrError("invalid controlfile mode %r" % mode)
988
907.1.50 by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
989
    def put_controlfile(self, path, f, encode=True):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
990
        """See Branch.put_controlfile."""
907.1.50 by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
991
        self.put_controlfiles([(path, f)], encode=encode)
992
993
    def put_controlfiles(self, files, encode=True):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
994
        """See Branch.put_controlfiles."""
907.1.50 by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
995
        import codecs
996
        ctrl_files = []
997
        for path, f in files:
998
            if encode:
999
                if isinstance(f, basestring):
1000
                    f = f.encode('utf-8', 'replace')
1001
                else:
1002
                    f = codecs.getwriter('utf-8')(f, errors='replace')
1003
            path = self._rel_controlfilename(path)
1004
            ctrl_files.append((path, f))
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
1005
        self._transport.put_multi(ctrl_files, mode=self._file_mode)
1006
1007
    def _find_modes(self, path=None):
1008
        """Determine the appropriate modes for files and directories."""
1009
        try:
1010
            if path is None:
1011
                path = self._rel_controlfilename('')
1012
            st = self._transport.stat(path)
1013
        except errors.TransportNotPossible:
1014
            self._dir_mode = 0755
1015
            self._file_mode = 0644
1016
        else:
1017
            self._dir_mode = st.st_mode & 07777
1018
            # Remove the sticky and execute bits for files
1019
            self._file_mode = self._dir_mode & ~07111
1185.58.7 by John Arbash Meinel
Added the ability to disable setting permissions
1020
        if not self._set_dir_mode:
1021
            self._dir_mode = None
1022
        if not self._set_file_mode:
1023
            self._file_mode = None
1 by mbp at sourcefrog
import from baz patch-364
1024
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
1025
    def _check_format(self, format):
1534.4.8 by Robert Collins
Unfuck upgrade.
1026
        """Identify the branch format if needed.
1 by mbp at sourcefrog
import from baz patch-364
1027
1534.4.8 by Robert Collins
Unfuck upgrade.
1028
        The format is stored as a reference to the format object in
1187 by Martin Pool
- improved check for branch version
1029
        self._branch_format for code that needs to check it later.
1 by mbp at sourcefrog
import from baz patch-364
1030
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
1031
        The format parameter is either None or the branch format class
1032
        used to open this branch.
163 by mbp at sourcefrog
merge win32 portability fixes
1033
        """
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
1034
        if format is None:
1035
            format = BzrBranchFormat.find_format(self._transport)
1534.4.8 by Robert Collins
Unfuck upgrade.
1036
        self._branch_format = format
1037
        mutter("got branch format %s", self._branch_format)
1294 by Martin Pool
- refactor branch version detection
1038
1508.1.15 by Robert Collins
Merge from mpool.
1039
    @needs_read_lock
909 by Martin Pool
- merge John's code to give the tree root an explicit file id
1040
    def get_root_id(self):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1041
        """See Branch.get_root_id."""
1497 by Robert Collins
Move Branch.read_working_inventory to WorkingTree.
1042
        inv = self.get_inventory(self.last_revision())
909 by Martin Pool
- merge John's code to give the tree root an explicit file id
1043
        return inv.root.file_id
1 by mbp at sourcefrog
import from baz patch-364
1044
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1045
    @needs_read_lock
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
1046
    def print_file(self, file, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1047
        """See Branch.print_file."""
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
1048
        tree = self.revision_tree(revision_id)
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1049
        # use inventory as it was in that revision
1050
        file_id = tree.inventory.path2id(file)
1051
        if not file_id:
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
1052
            try:
1053
                revno = self.revision_id_to_revno(revision_id)
1054
            except errors.NoSuchRevision:
1055
                # TODO: This should not be BzrError,
1056
                # but NoSuchFile doesn't fit either
1057
                raise BzrError('%r is not present in revision %s' 
1058
                                % (file, revision_id))
1059
            else:
1060
                raise BzrError('%r is not present in revision %s'
1061
                                % (file, revno))
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1062
        tree.print_file(file_id)
1063
1064
    @needs_write_lock
905 by Martin Pool
- merge aaron's append_multiple.patch
1065
    def append_revision(self, *revision_ids):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1066
        """See Branch.append_revision."""
905 by Martin Pool
- merge aaron's append_multiple.patch
1067
        for revision_id in revision_ids:
1068
            mutter("add {%s} to revision-history" % revision_id)
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1069
        rev_history = self.revision_history()
1070
        rev_history.extend(revision_ids)
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
1071
        self.set_revision_history(rev_history)
1072
1073
    @needs_write_lock
1074
    def set_revision_history(self, rev_history):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1075
        """See Branch.set_revision_history."""
1185.33.59 by Martin Pool
[patch] keep a cached basis inventory (Johan Rydberg)
1076
        old_revision = self.last_revision()
1077
        new_revision = rev_history[-1]
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1078
        self.put_controlfile('revision-history', '\n'.join(rev_history))
1185.49.26 by John Arbash Meinel
Adding tests for remote sftp branches without working trees, plus a bugfix to allow push to still work with a warning.
1079
        try:
1080
            self.working_tree().set_last_revision(new_revision, old_revision)
1081
        except NoWorkingTree:
1082
            mutter('Unable to set_last_revision without a working tree.')
233 by mbp at sourcefrog
- more output from test.sh
1083
1261 by Martin Pool
- new method Branch.has_revision
1084
    def has_revision(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1085
        """See Branch.has_revision."""
1390 by Robert Collins
pair programming worx... merge integration and weave
1086
        return (revision_id is None
1442.1.45 by Robert Collins
replace __contains__ calls in stores with has_id
1087
                or self.revision_store.has_id(revision_id))
1261 by Martin Pool
- new method Branch.has_revision
1088
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1089
    @needs_read_lock
1185.42.5 by Jelmer Vernooij
Make get_revision_xml_file() private
1090
    def _get_revision_xml_file(self, revision_id):
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1091
        if not revision_id or not isinstance(revision_id, basestring):
1185.16.103 by mbp at sourcefrog
Fix up all calls to InvalidRevisionId() to specify parameters.
1092
            raise InvalidRevisionId(revision_id=revision_id, branch=self)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1093
        try:
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1094
            return self.revision_store.get(revision_id)
1095
        except (IndexError, KeyError):
1096
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1097
1231 by Martin Pool
- more progress on fetch on top of weaves
1098
    def get_revision_xml(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1099
        """See Branch.get_revision_xml."""
1185.42.5 by Jelmer Vernooij
Make get_revision_xml_file() private
1100
        return self._get_revision_xml_file(revision_id).read()
1231 by Martin Pool
- more progress on fetch on top of weaves
1101
1 by mbp at sourcefrog
import from baz patch-364
1102
    def get_revision(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1103
        """See Branch.get_revision."""
1185.42.5 by Jelmer Vernooij
Make get_revision_xml_file() private
1104
        xml_file = self._get_revision_xml_file(revision_id)
1027 by Martin Pool
- better error message when failing to get revision from store
1105
1106
        try:
1189 by Martin Pool
- BROKEN: partial support for commit into weave
1107
            r = bzrlib.xml5.serializer_v5.read_revision(xml_file)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1108
        except SyntaxError, e:
1109
            raise bzrlib.errors.BzrError('failed to unpack revision_xml',
1110
                                         [revision_id,
1111
                                          str(e)])
802 by Martin Pool
- Remove XMLMixin class in favour of simple pack_xml, unpack_xml functions
1112
            
1 by mbp at sourcefrog
import from baz patch-364
1113
        assert r.revision_id == revision_id
1114
        return r
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1115
672 by Martin Pool
- revision records include the hash of their inventory and
1116
    def get_revision_sha1(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1117
        """See Branch.get_revision_sha1."""
672 by Martin Pool
- revision records include the hash of their inventory and
1118
        # In the future, revision entries will be signed. At that
1119
        # point, it is probably best *not* to include the signature
1120
        # in the revision hash. Because that lets you re-sign
1121
        # the revision, (add signatures/remove signatures) and still
1122
        # have all hash pointers stay consistent.
1123
        # But for now, just hash the contents.
1230 by Martin Pool
- remove Branch.get_revision_xml; use get_revision_xml_file instead
1124
        return bzrlib.osutils.sha_file(self.get_revision_xml_file(revision_id))
672 by Martin Pool
- revision records include the hash of their inventory and
1125
1225 by Martin Pool
- branch now tracks ancestry - all merged revisions
1126
    def get_ancestry(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1127
        """See Branch.get_ancestry."""
1390 by Robert Collins
pair programming worx... merge integration and weave
1128
        if revision_id is None:
1129
            return [None]
1495.1.3 by Jelmer Vernooij
Move some more generic methods from NativeBranch to Branch.
1130
        w = self._get_inventory_weave()
1415 by Robert Collins
remove the ancestry weave file
1131
        return [None] + map(w.idx_to_name,
1132
                            w.inclusions([w.lookup(revision_id)]))
1225 by Martin Pool
- branch now tracks ancestry - all merged revisions
1133
1495.1.3 by Jelmer Vernooij
Move some more generic methods from NativeBranch to Branch.
1134
    def _get_inventory_weave(self):
1417.1.8 by Robert Collins
use transactions in the weave store interface, which enables caching for log
1135
        return self.control_weaves.get_weave('inventory',
1136
                                             self.get_transaction())
1223 by Martin Pool
- store inventories in weave
1137
1192 by Martin Pool
- clean up code for retrieving stored inventories
1138
    def get_inventory(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1139
        """See Branch.get_inventory."""
1372 by Martin Pool
- avoid converting inventories to/from StringIO
1140
        xml = self.get_inventory_xml(revision_id)
1141
        return bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1142
1192 by Martin Pool
- clean up code for retrieving stored inventories
1143
    def get_inventory_xml(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1144
        """See Branch.get_inventory_xml."""
1192 by Martin Pool
- clean up code for retrieving stored inventories
1145
        try:
1146
            assert isinstance(revision_id, basestring), type(revision_id)
1495.1.3 by Jelmer Vernooij
Move some more generic methods from NativeBranch to Branch.
1147
            iw = self._get_inventory_weave()
1223 by Martin Pool
- store inventories in weave
1148
            return iw.get_text(iw.lookup(revision_id))
1192 by Martin Pool
- clean up code for retrieving stored inventories
1149
        except IndexError:
1150
            raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
1180 by Martin Pool
- start splitting code for xml (de)serialization away from objects
1151
1192 by Martin Pool
- clean up code for retrieving stored inventories
1152
    def get_inventory_sha1(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1153
        """See Branch.get_inventory_sha1."""
1223 by Martin Pool
- store inventories in weave
1154
        return self.get_revision(revision_id).inventory_sha1
672 by Martin Pool
- revision records include the hash of their inventory and
1155
1 by mbp at sourcefrog
import from baz patch-364
1156
    def get_revision_inventory(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1157
        """See Branch.get_revision_inventory."""
1372 by Martin Pool
- avoid converting inventories to/from StringIO
1158
        # TODO: Unify this with get_inventory()
1218 by Martin Pool
- fix up import
1159
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
820 by Martin Pool
- faster Branch.get_revision_inventory now we know the ids are the same
1160
        # must be the same as its revision, so this is trivial.
1 by mbp at sourcefrog
import from baz patch-364
1161
        if revision_id == None:
1497 by Robert Collins
Move Branch.read_working_inventory to WorkingTree.
1162
            # This does not make sense: if there is no revision,
1163
            # then it is the current tree inventory surely ?!
1164
            # and thus get_root_id() is something that looks at the last
1165
            # commit on the branch, and the get_root_id is an inventory check.
1166
            raise NotImplementedError
1167
            # return Inventory(self.get_root_id())
1 by mbp at sourcefrog
import from baz patch-364
1168
        else:
820 by Martin Pool
- faster Branch.get_revision_inventory now we know the ids are the same
1169
            return self.get_inventory(revision_id)
1 by mbp at sourcefrog
import from baz patch-364
1170
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1171
    @needs_read_lock
1 by mbp at sourcefrog
import from baz patch-364
1172
    def revision_history(self):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1173
        """See Branch.revision_history."""
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1174
        transaction = self.get_transaction()
1175
        history = transaction.map.find_revision_history()
1176
        if history is not None:
1177
            mutter("cache hit for revision-history in %s", self)
1417.1.12 by Robert Collins
cache revision history during read transactions
1178
            return list(history)
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1179
        history = [l.rstrip('\r\n') for l in
1180
                self.controlfile('revision-history', 'r').readlines()]
1181
        transaction.map.add_revision_history(history)
1182
        # this call is disabled because revision_history is 
1183
        # not really an object yet, and the transaction is for objects.
1184
        # transaction.register_clean(history, precious=True)
1185
        return list(history)
1 by mbp at sourcefrog
import from baz patch-364
1186
974.1.28 by aaron.bentley at utoronto
factored install_revisions out of update_revisions, updated test cases for greedy_fetch
1187
    def update_revisions(self, other, stop_revision=None):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1188
        """See Branch.update_revisions."""
974.1.33 by aaron.bentley at utoronto
Added greedy_fetch to update_revisions
1189
        from bzrlib.fetch import greedy_fetch
974.1.75 by Aaron Bentley
Sped up pull by copying locally first
1190
        if stop_revision is None:
1390 by Robert Collins
pair programming worx... merge integration and weave
1191
            stop_revision = other.last_revision()
1185.12.44 by abentley
Restored branch convergence to bzr pull
1192
        ### Should this be checking is_ancestor instead of revision_history?
1441 by Robert Collins
tests passing is a good idea - move the branch open in cmd_branch to ensure this, and remove noise from the test suite
1193
        if (stop_revision is not None and 
1194
            stop_revision in self.revision_history()):
1440 by Robert Collins
further tuning of pull, do not do a local merge or fetch at all, if the remote branch is no newer than we are
1195
            return
1260 by Martin Pool
- some updates for fetch/update function
1196
        greedy_fetch(to_branch=self, from_branch=other,
1261 by Martin Pool
- new method Branch.has_revision
1197
                     revision=stop_revision)
1185.12.44 by abentley
Restored branch convergence to bzr pull
1198
        pullable_revs = self.pullable_revisions(other, stop_revision)
1185.12.45 by abentley
Cleanups for pull
1199
        if len(pullable_revs) > 0:
1261 by Martin Pool
- new method Branch.has_revision
1200
            self.append_revision(*pullable_revs)
1185.12.44 by abentley
Restored branch convergence to bzr pull
1201
1202
    def pullable_revisions(self, other, stop_revision):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1203
        """See Branch.pullable_revisions."""
1185.12.44 by abentley
Restored branch convergence to bzr pull
1204
        other_revno = other.revision_id_to_revno(stop_revision)
1205
        try:
1206
            return self.missing_revisions(other, other_revno)
1207
        except DivergedBranches, e:
1208
            try:
1209
                pullable_revs = get_intervening_revisions(self.last_revision(),
1210
                                                          stop_revision, self)
1211
                assert self.last_revision() not in pullable_revs
1212
                return pullable_revs
1213
            except bzrlib.errors.NotAncestor:
1214
                if is_ancestor(self.last_revision(), stop_revision, self):
1215
                    return []
1216
                else:
1217
                    raise e
1218
        
1 by mbp at sourcefrog
import from baz patch-364
1219
    def revision_tree(self, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1220
        """See Branch.revision_tree."""
529 by Martin Pool
todo
1221
        # TODO: refactor this to use an existing revision object
1222
        # so we don't need to read it in twice.
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
1223
        if revision_id == None or revision_id == NULL_REVISION:
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1224
            return EmptyTree()
1 by mbp at sourcefrog
import from baz patch-364
1225
        else:
1226
            inv = self.get_revision_inventory(revision_id)
1185.50.28 by John Arbash Meinel
Lots of updates for 'bzr check'
1227
            return RevisionTree(self, inv, revision_id)
1 by mbp at sourcefrog
import from baz patch-364
1228
1185.33.59 by Martin Pool
[patch] keep a cached basis inventory (Johan Rydberg)
1229
    def basis_tree(self):
1230
        """See Branch.basis_tree."""
1231
        try:
1232
            revision_id = self.revision_history()[-1]
1233
            xml = self.working_tree().read_basis_inventory(revision_id)
1234
            inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
1185.50.28 by John Arbash Meinel
Lots of updates for 'bzr check'
1235
            return RevisionTree(self, inv, revision_id)
1185.49.26 by John Arbash Meinel
Adding tests for remote sftp branches without working trees, plus a bugfix to allow push to still work with a warning.
1236
        except (IndexError, NoSuchFile, NoWorkingTree), e:
1185.33.59 by Martin Pool
[patch] keep a cached basis inventory (Johan Rydberg)
1237
            return self.revision_tree(self.last_revision())
1238
1 by mbp at sourcefrog
import from baz patch-364
1239
    def working_tree(self):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1240
        """See Branch.working_tree."""
1185.2.2 by Lalo Martins
cleaning up and refactoring the branch module.
1241
        from bzrlib.workingtree import WorkingTree
1497 by Robert Collins
Move Branch.read_working_inventory to WorkingTree.
1242
        if self._transport.base.find('://') != -1:
1243
            raise NoWorkingTree(self.base)
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
1244
        return WorkingTree(self.base, branch=self)
1 by mbp at sourcefrog
import from baz patch-364
1245
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
1246
    @needs_write_lock
1247
    def pull(self, source, overwrite=False):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1248
        """See Branch.pull."""
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
1249
        source.lock_read()
1250
        try:
1185.33.44 by Martin Pool
[patch] show number of revisions pushed/pulled/merged (Robey Pointer)
1251
            old_count = len(self.revision_history())
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
1252
            try:
1253
                self.update_revisions(source)
1254
            except DivergedBranches:
1255
                if not overwrite:
1256
                    raise
1185.50.5 by John Arbash Meinel
pull --overwrite should always overwrite, not just if diverged. (Test case from Robey Pointer)
1257
            if overwrite:
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
1258
                self.set_revision_history(source.revision_history())
1185.33.44 by Martin Pool
[patch] show number of revisions pushed/pulled/merged (Robey Pointer)
1259
            new_count = len(self.revision_history())
1260
            return new_count - old_count
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
1261
        finally:
1262
            source.unlock()
1 by mbp at sourcefrog
import from baz patch-364
1263
1149 by Martin Pool
- make get_parent() be a method of Branch; add simple tests for it
1264
    def get_parent(self):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1265
        """See Branch.get_parent."""
1149 by Martin Pool
- make get_parent() be a method of Branch; add simple tests for it
1266
        import errno
1267
        _locs = ['parent', 'pull', 'x-pull']
1268
        for l in _locs:
1269
            try:
1270
                return self.controlfile(l, 'r').read().strip('\n')
1185.31.45 by John Arbash Meinel
Refactoring Exceptions found some places where the wrong exception was caught.
1271
            except NoSuchFile:
1272
                pass
1149 by Martin Pool
- make get_parent() be a method of Branch; add simple tests for it
1273
        return None
1274
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
1275
    def get_push_location(self):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1276
        """See Branch.get_push_location."""
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
1277
        config = bzrlib.config.BranchConfig(self)
1278
        push_loc = config.get_user_option('push_location')
1279
        return push_loc
1280
1281
    def set_push_location(self, location):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1282
        """See Branch.set_push_location."""
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
1283
        config = bzrlib.config.LocationConfig(self.base)
1284
        config.set_user_option('push_location', location)
1285
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1286
    @needs_write_lock
1150 by Martin Pool
- add new Branch.set_parent and tests
1287
    def set_parent(self, url):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1288
        """See Branch.set_parent."""
1150 by Martin Pool
- add new Branch.set_parent and tests
1289
        # TODO: Maybe delete old location files?
1290
        from bzrlib.atomicfile import AtomicFile
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1291
        f = AtomicFile(self.controlfilename('parent'))
1150 by Martin Pool
- add new Branch.set_parent and tests
1292
        try:
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1293
            f.write(url + '\n')
1294
            f.commit()
1150 by Martin Pool
- add new Branch.set_parent and tests
1295
        finally:
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1296
            f.close()
1150 by Martin Pool
- add new Branch.set_parent and tests
1297
1185.35.11 by Aaron Bentley
Added support for branch nicks
1298
    def tree_config(self):
1299
        return TreeConfig(self)
1300
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
1301
    def sign_revision(self, revision_id, gpg_strategy):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1302
        """See Branch.sign_revision."""
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
1303
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
1304
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
1305
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1306
    @needs_write_lock
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
1307
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1495.1.2 by Jelmer Vernooij
Move some generic methods of NativeBranch to Branch.
1308
        """See Branch.store_revision_signature."""
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
1309
        self.revision_store.add(StringIO(gpg_strategy.sign(plaintext)), 
1310
                                revision_id, "sig")
1185.11.5 by John Arbash Meinel
Merged up-to-date against mainline, still broken.
1311
1312
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
1313
Branch.set_default_initializer(BzrBranch._initialize)
1314
1315
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
1316
class BranchTestProviderAdapter(object):
1317
    """A tool to generate a suite testing multiple branch formats at once.
1318
1319
    This is done by copying the test once for each transport and injecting
1320
    the transport_server, transport_readonly_server, and branch_format
1321
    classes into each copy. Each copy is also given a new id() to make it
1322
    easy to identify.
1323
    """
1324
1325
    def __init__(self, transport_server, transport_readonly_server, formats):
1326
        self._transport_server = transport_server
1327
        self._transport_readonly_server = transport_readonly_server
1328
        self._formats = formats
1329
    
1330
    def adapt(self, test):
1331
        result = TestSuite()
1332
        for format in self._formats:
1333
            new_test = deepcopy(test)
1334
            new_test.transport_server = self._transport_server
1335
            new_test.transport_readonly_server = self._transport_readonly_server
1336
            new_test.branch_format = format
1337
            def make_new_test_id():
1338
                new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1339
                return lambda: new_id
1340
            new_test.id = make_new_test_id()
1341
            result.addTest(new_test)
1342
        return result
1343
1344
1495.1.5 by Jelmer Vernooij
Rename NativeBranch -> BzrBranch
1345
class ScratchBranch(BzrBranch):
1 by mbp at sourcefrog
import from baz patch-364
1346
    """Special test class: a branch that cleans up after itself.
1347
1348
    >>> b = ScratchBranch()
1349
    >>> isdir(b.base)
1350
    True
1351
    >>> bd = b.base
1442.1.42 by Robert Collins
rebuild ScratchBranch on top of ScratchTransport
1352
    >>> b._transport.__del__()
1 by mbp at sourcefrog
import from baz patch-364
1353
    >>> isdir(bd)
1354
    False
1355
    """
1442.1.42 by Robert Collins
rebuild ScratchBranch on top of ScratchTransport
1356
1357
    def __init__(self, files=[], dirs=[], transport=None):
1 by mbp at sourcefrog
import from baz patch-364
1358
        """Make a test branch.
1359
1360
        This creates a temporary directory and runs init-tree in it.
1361
1362
        If any files are listed, they are created in the working copy.
1363
        """
1442.1.42 by Robert Collins
rebuild ScratchBranch on top of ScratchTransport
1364
        if transport is None:
1365
            transport = bzrlib.transport.local.ScratchTransport()
1534.4.6 by Robert Collins
Remove last uses of the init= parameter to BzrBranch.
1366
            Branch.initialize(transport.base)
1367
            super(ScratchBranch, self).__init__(transport)
1442.1.42 by Robert Collins
rebuild ScratchBranch on top of ScratchTransport
1368
        else:
1369
            super(ScratchBranch, self).__init__(transport)
1370
100 by mbp at sourcefrog
- add test case for ignore files
1371
        for d in dirs:
907.1.8 by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport
1372
            self._transport.mkdir(d)
100 by mbp at sourcefrog
- add test case for ignore files
1373
            
1 by mbp at sourcefrog
import from baz patch-364
1374
        for f in files:
907.1.8 by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport
1375
            self._transport.put(f, 'content of %s' % f)
1 by mbp at sourcefrog
import from baz patch-364
1376
1377
622 by Martin Pool
Updated merge patch from Aaron
1378
    def clone(self):
1379
        """
1380
        >>> orig = ScratchBranch(files=["file1", "file2"])
1381
        >>> clone = orig.clone()
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
1382
        >>> if os.name != 'nt':
1383
        ...   os.path.samefile(orig.base, clone.base)
1384
        ... else:
1385
        ...   orig.base == clone.base
1386
        ...
622 by Martin Pool
Updated merge patch from Aaron
1387
        False
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
1388
        >>> os.path.isfile(pathjoin(clone.base, "file1"))
622 by Martin Pool
Updated merge patch from Aaron
1389
        True
1390
        """
800 by Martin Pool
Merge John's import-speedup branch:
1391
        from shutil import copytree
1185.31.40 by John Arbash Meinel
Added osutils.mkdtemp()
1392
        from bzrlib.osutils import mkdtemp
800 by Martin Pool
Merge John's import-speedup branch:
1393
        base = mkdtemp()
622 by Martin Pool
Updated merge patch from Aaron
1394
        os.rmdir(base)
800 by Martin Pool
Merge John's import-speedup branch:
1395
        copytree(self.base, base, symlinks=True)
1442.1.42 by Robert Collins
rebuild ScratchBranch on top of ScratchTransport
1396
        return ScratchBranch(
1397
            transport=bzrlib.transport.local.ScratchTransport(base))
1 by mbp at sourcefrog
import from baz patch-364
1398
    
1399
1400
######################################################################
1401
# predicates
1402
1403
1404
def is_control_file(filename):
1405
    ## FIXME: better check
1185.31.38 by John Arbash Meinel
Changing os.path.normpath to osutils.normpath
1406
    filename = normpath(filename)
1 by mbp at sourcefrog
import from baz patch-364
1407
    while filename != '':
1408
        head, tail = os.path.split(filename)
1409
        ## mutter('check %r for control file' % ((head, tail), ))
1410
        if tail == bzrlib.BZRDIR:
1411
            return True
70 by mbp at sourcefrog
Prepare for smart recursive add.
1412
        if filename == head:
1413
            break
1 by mbp at sourcefrog
import from baz patch-364
1414
        filename = head
1415
    return False