/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

[merge] bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
from bzrlib.tree import EmptyTree, RevisionTree
45
45
from bzrlib.inventory import Inventory
46
46
from bzrlib.store import copy_all
47
 
from bzrlib.store.compressed_text import CompressedTextStore
48
47
from bzrlib.store.text import TextStore
49
48
from bzrlib.store.weave import WeaveStore
50
49
from bzrlib.testament import Testament
52
51
from bzrlib.transport import Transport, get_transport
53
52
import bzrlib.xml5
54
53
import bzrlib.ui
 
54
from config import TreeConfig
55
55
 
56
56
 
57
57
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
111
111
        """Open a branch which may be of an old format.
112
112
        
113
113
        Only local branches are supported."""
114
 
        return _Branch(get_transport(base), relax_version_check=True)
 
114
        return BzrBranch(get_transport(base), relax_version_check=True)
115
115
        
116
116
    @staticmethod
117
117
    def open(base):
118
118
        """Open an existing branch, rooted at 'base' (url)"""
119
119
        t = get_transport(base)
120
120
        mutter("trying to open %r with transport %r", base, t)
121
 
        return _Branch(t)
 
121
        return BzrBranch(t)
122
122
 
123
123
    @staticmethod
124
124
    def open_containing(url):
133
133
        t = get_transport(url)
134
134
        while True:
135
135
            try:
136
 
                return _Branch(t), t.relpath(url)
 
136
                return BzrBranch(t), t.relpath(url)
137
137
            except NotBranchError:
138
138
                pass
139
139
            new_t = t.clone('..')
146
146
    def initialize(base):
147
147
        """Create a new branch, rooted at 'base' (url)"""
148
148
        t = get_transport(base)
149
 
        return _Branch(t, init=True)
 
149
        return BzrBranch(t, init=True)
150
150
 
151
151
    def setup_caching(self, cache_root):
152
152
        """Subclasses that care about caching should override this, and set
154
154
        """
155
155
        self.cache_root = cache_root
156
156
 
157
 
 
158
 
class _Branch(Branch):
 
157
    def _get_nick(self):
 
158
        cfg = self.tree_config()
 
159
        return cfg.get_option(u"nickname", default=self.base.split('/')[-1])
 
160
 
 
161
    def _set_nick(self, nick):
 
162
        cfg = self.tree_config()
 
163
        cfg.set_option(nick, "nickname")
 
164
        assert cfg.get_option("nickname") == nick
 
165
 
 
166
    nick = property(_get_nick, _set_nick)
 
167
        
 
168
    def push_stores(self, branch_to):
 
169
        """Copy the content of this branches store to branch_to."""
 
170
        raise NotImplementedError('push_stores is abstract')
 
171
 
 
172
    def get_transaction(self):
 
173
        """Return the current active transaction.
 
174
 
 
175
        If no transaction is active, this returns a passthrough object
 
176
        for which all data is immediately flushed and no caching happens.
 
177
        """
 
178
        raise NotImplementedError('get_transaction is abstract')
 
179
 
 
180
    def lock_write(self):
 
181
        raise NotImplementedError('lock_write is abstract')
 
182
        
 
183
    def lock_read(self):
 
184
        raise NotImplementedError('lock_read is abstract')
 
185
 
 
186
    def unlock(self):
 
187
        raise NotImplementedError('unlock is abstract')
 
188
 
 
189
    def abspath(self, name):
 
190
        """Return absolute filename for something in the branch
 
191
        
 
192
        XXX: Robert Collins 20051017 what is this used for? why is it a branch
 
193
        method and not a tree method.
 
194
        """
 
195
        raise NotImplementedError('abspath is abstract')
 
196
 
 
197
    def controlfilename(self, file_or_path):
 
198
        """Return location relative to branch."""
 
199
        raise NotImplementedError('controlfilename is abstract')
 
200
 
 
201
    def controlfile(self, file_or_path, mode='r'):
 
202
        """Open a control file for this branch.
 
203
 
 
204
        There are two classes of file in the control directory: text
 
205
        and binary.  binary files are untranslated byte streams.  Text
 
206
        control files are stored with Unix newlines and in UTF-8, even
 
207
        if the platform or locale defaults are different.
 
208
 
 
209
        Controlfiles should almost never be opened in write mode but
 
210
        rather should be atomically copied and replaced using atomicfile.
 
211
        """
 
212
        raise NotImplementedError('controlfile is abstract')
 
213
 
 
214
    def put_controlfile(self, path, f, encode=True):
 
215
        """Write an entry as a controlfile.
 
216
 
 
217
        :param path: The path to put the file, relative to the .bzr control
 
218
                     directory
 
219
        :param f: A file-like or string object whose contents should be copied.
 
220
        :param encode:  If true, encode the contents as utf-8
 
221
        """
 
222
        raise NotImplementedError('put_controlfile is abstract')
 
223
 
 
224
    def put_controlfiles(self, files, encode=True):
 
225
        """Write several entries as controlfiles.
 
226
 
 
227
        :param files: A list of [(path, file)] pairs, where the path is the directory
 
228
                      underneath the bzr control directory
 
229
        :param encode:  If true, encode the contents as utf-8
 
230
        """
 
231
        raise NotImplementedError('put_controlfiles is abstract')
 
232
 
 
233
    def get_root_id(self):
 
234
        """Return the id of this branches root"""
 
235
        raise NotImplementedError('get_root_id is abstract')
 
236
 
 
237
    def set_root_id(self, file_id):
 
238
        raise NotImplementedError('set_root_id is abstract')
 
239
 
 
240
    def add(self, files, ids=None):
 
241
        """Make files versioned.
 
242
 
 
243
        Note that the command line normally calls smart_add instead,
 
244
        which can automatically recurse.
 
245
 
 
246
        This puts the files in the Added state, so that they will be
 
247
        recorded by the next commit.
 
248
 
 
249
        files
 
250
            List of paths to add, relative to the base of the tree.
 
251
 
 
252
        ids
 
253
            If set, use these instead of automatically generated ids.
 
254
            Must be the same length as the list of files, but may
 
255
            contain None for ids that are to be autogenerated.
 
256
 
 
257
        TODO: Perhaps have an option to add the ids even if the files do
 
258
              not (yet) exist.
 
259
 
 
260
        TODO: Perhaps yield the ids and paths as they're added.
 
261
        """
 
262
        raise NotImplementedError('add is abstract')
 
263
 
 
264
    def print_file(self, file, revno):
 
265
        """Print `file` to stdout."""
 
266
        raise NotImplementedError('print_file is abstract')
 
267
 
 
268
    def unknowns(self):
 
269
        """Return all unknown files.
 
270
 
 
271
        These are files in the working directory that are not versioned or
 
272
        control files or ignored.
 
273
        
 
274
        >>> from bzrlib.workingtree import WorkingTree
 
275
        >>> b = ScratchBranch(files=['foo', 'foo~'])
 
276
        >>> map(str, b.unknowns())
 
277
        ['foo']
 
278
        >>> b.add('foo')
 
279
        >>> list(b.unknowns())
 
280
        []
 
281
        >>> WorkingTree(b.base, b).remove('foo')
 
282
        >>> list(b.unknowns())
 
283
        [u'foo']
 
284
        """
 
285
        raise NotImplementedError('unknowns is abstract')
 
286
 
 
287
    def append_revision(self, *revision_ids):
 
288
        raise NotImplementedError('append_revision is abstract')
 
289
 
 
290
    def set_revision_history(self, rev_history):
 
291
        raise NotImplementedError('set_revision_history is abstract')
 
292
 
 
293
    def has_revision(self, revision_id):
 
294
        """True if this branch has a copy of the revision.
 
295
 
 
296
        This does not necessarily imply the revision is merge
 
297
        or on the mainline."""
 
298
        raise NotImplementedError('has_revision is abstract')
 
299
 
 
300
    def get_revision_xml_file(self, revision_id):
 
301
        """Return XML file object for revision object."""
 
302
        raise NotImplementedError('get_revision_xml_file is abstract')
 
303
 
 
304
    def get_revision_xml(self, revision_id):
 
305
        raise NotImplementedError('get_revision_xml is abstract')
 
306
 
 
307
    def get_revision(self, revision_id):
 
308
        """Return the Revision object for a named revision"""
 
309
        raise NotImplementedError('get_revision is abstract')
 
310
 
 
311
    def get_revision_delta(self, revno):
 
312
        """Return the delta for one revision.
 
313
 
 
314
        The delta is relative to its mainline predecessor, or the
 
315
        empty tree for revision 1.
 
316
        """
 
317
        assert isinstance(revno, int)
 
318
        rh = self.revision_history()
 
319
        if not (1 <= revno <= len(rh)):
 
320
            raise InvalidRevisionNumber(revno)
 
321
 
 
322
        # revno is 1-based; list is 0-based
 
323
 
 
324
        new_tree = self.revision_tree(rh[revno-1])
 
325
        if revno == 1:
 
326
            old_tree = EmptyTree()
 
327
        else:
 
328
            old_tree = self.revision_tree(rh[revno-2])
 
329
 
 
330
        return compare_trees(old_tree, new_tree)
 
331
 
 
332
    def get_revision_sha1(self, revision_id):
 
333
        """Hash the stored value of a revision, and return it."""
 
334
        raise NotImplementedError('get_revision_sha1 is abstract')
 
335
 
 
336
    def get_ancestry(self, revision_id):
 
337
        """Return a list of revision-ids integrated by a revision.
 
338
        
 
339
        This currently returns a list, but the ordering is not guaranteed:
 
340
        treat it as a set.
 
341
        """
 
342
        raise NotImplementedError('get_ancestry is abstract')
 
343
 
 
344
    def get_inventory(self, revision_id):
 
345
        """Get Inventory object by hash."""
 
346
        raise NotImplementedError('get_inventory is abstract')
 
347
 
 
348
    def get_inventory_xml(self, revision_id):
 
349
        """Get inventory XML as a file object."""
 
350
        raise NotImplementedError('get_inventory_xml is abstract')
 
351
 
 
352
    def get_inventory_sha1(self, revision_id):
 
353
        """Return the sha1 hash of the inventory entry."""
 
354
        raise NotImplementedError('get_inventory_sha1 is abstract')
 
355
 
 
356
    def get_revision_inventory(self, revision_id):
 
357
        """Return inventory of a past revision."""
 
358
        raise NotImplementedError('get_revision_inventory is abstract')
 
359
 
 
360
    def revision_history(self):
 
361
        """Return sequence of revision hashes on to this branch."""
 
362
        raise NotImplementedError('revision_history is abstract')
 
363
 
 
364
    def revno(self):
 
365
        """Return current revision number for this branch.
 
366
 
 
367
        That is equivalent to the number of revisions committed to
 
368
        this branch.
 
369
        """
 
370
        return len(self.revision_history())
 
371
 
 
372
    def last_revision(self):
 
373
        """Return last patch hash, or None if no history."""
 
374
        ph = self.revision_history()
 
375
        if ph:
 
376
            return ph[-1]
 
377
        else:
 
378
            return None
 
379
 
 
380
    def missing_revisions(self, other, stop_revision=None, diverged_ok=False):
 
381
        """Return a list of new revisions that would perfectly fit.
 
382
        
 
383
        If self and other have not diverged, return a list of the revisions
 
384
        present in other, but missing from self.
 
385
 
 
386
        >>> from bzrlib.commit import commit
 
387
        >>> bzrlib.trace.silent = True
 
388
        >>> br1 = ScratchBranch()
 
389
        >>> br2 = ScratchBranch()
 
390
        >>> br1.missing_revisions(br2)
 
391
        []
 
392
        >>> commit(br2, "lala!", rev_id="REVISION-ID-1")
 
393
        >>> br1.missing_revisions(br2)
 
394
        [u'REVISION-ID-1']
 
395
        >>> br2.missing_revisions(br1)
 
396
        []
 
397
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1")
 
398
        >>> br1.missing_revisions(br2)
 
399
        []
 
400
        >>> commit(br2, "lala!", rev_id="REVISION-ID-2A")
 
401
        >>> br1.missing_revisions(br2)
 
402
        [u'REVISION-ID-2A']
 
403
        >>> commit(br1, "lala!", rev_id="REVISION-ID-2B")
 
404
        >>> br1.missing_revisions(br2)
 
405
        Traceback (most recent call last):
 
406
        DivergedBranches: These branches have diverged.
 
407
        """
 
408
        self_history = self.revision_history()
 
409
        self_len = len(self_history)
 
410
        other_history = other.revision_history()
 
411
        other_len = len(other_history)
 
412
        common_index = min(self_len, other_len) -1
 
413
        if common_index >= 0 and \
 
414
            self_history[common_index] != other_history[common_index]:
 
415
            raise DivergedBranches(self, other)
 
416
 
 
417
        if stop_revision is None:
 
418
            stop_revision = other_len
 
419
        else:
 
420
            assert isinstance(stop_revision, int)
 
421
            if stop_revision > other_len:
 
422
                raise bzrlib.errors.NoSuchRevision(self, stop_revision)
 
423
        return other_history[self_len:stop_revision]
 
424
    
 
425
    def update_revisions(self, other, stop_revision=None):
 
426
        """Pull in new perfect-fit revisions."""
 
427
        raise NotImplementedError('update_revisions is abstract')
 
428
 
 
429
    def pullable_revisions(self, other, stop_revision):
 
430
        raise NotImplementedError('pullable_revisions is abstract')
 
431
        
 
432
    def revision_id_to_revno(self, revision_id):
 
433
        """Given a revision id, return its revno"""
 
434
        if revision_id is None:
 
435
            return 0
 
436
        history = self.revision_history()
 
437
        try:
 
438
            return history.index(revision_id) + 1
 
439
        except ValueError:
 
440
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
 
441
 
 
442
    def get_rev_id(self, revno, history=None):
 
443
        """Find the revision id of the specified revno."""
 
444
        if revno == 0:
 
445
            return None
 
446
        if history is None:
 
447
            history = self.revision_history()
 
448
        elif revno <= 0 or revno > len(history):
 
449
            raise bzrlib.errors.NoSuchRevision(self, revno)
 
450
        return history[revno - 1]
 
451
 
 
452
    def revision_tree(self, revision_id):
 
453
        """Return Tree for a revision on this branch.
 
454
 
 
455
        `revision_id` may be None for the null revision, in which case
 
456
        an `EmptyTree` is returned."""
 
457
        raise NotImplementedError('revision_tree is abstract')
 
458
 
 
459
    def working_tree(self):
 
460
        """Return a `Tree` for the working copy."""
 
461
        raise NotImplementedError('working_tree is abstract')
 
462
 
 
463
    def pull(self, source, overwrite=False):
 
464
        raise NotImplementedError('pull is abstract')
 
465
 
 
466
    def basis_tree(self):
 
467
        """Return `Tree` object for last revision.
 
468
 
 
469
        If there are no revisions yet, return an `EmptyTree`.
 
470
        """
 
471
        return self.revision_tree(self.last_revision())
 
472
 
 
473
    def rename_one(self, from_rel, to_rel):
 
474
        """Rename one file.
 
475
 
 
476
        This can change the directory or the filename or both.
 
477
        """
 
478
        raise NotImplementedError('rename_one is abstract')
 
479
 
 
480
    def move(self, from_paths, to_name):
 
481
        """Rename files.
 
482
 
 
483
        to_name must exist as a versioned directory.
 
484
 
 
485
        If to_name exists and is a directory, the files are moved into
 
486
        it, keeping their old names.  If it is a directory, 
 
487
 
 
488
        Note that to_name is only the last component of the new name;
 
489
        this doesn't change the directory.
 
490
 
 
491
        This returns a list of (from_path, to_path) pairs for each
 
492
        entry that is moved.
 
493
        """
 
494
        raise NotImplementedError('move is abstract')
 
495
 
 
496
    def revert(self, filenames, old_tree=None, backups=True):
 
497
        """Restore selected files to the versions from a previous tree.
 
498
 
 
499
        backups
 
500
            If true (default) backups are made of files before
 
501
            they're renamed.
 
502
        """
 
503
        raise NotImplementedError('revert is abstract')
 
504
 
 
505
    def pending_merges(self):
 
506
        """Return a list of pending merges.
 
507
 
 
508
        These are revisions that have been merged into the working
 
509
        directory but not yet committed.
 
510
        """
 
511
        raise NotImplementedError('pending_merges is abstract')
 
512
 
 
513
    def add_pending_merge(self, *revision_ids):
 
514
        # TODO: Perhaps should check at this point that the
 
515
        # history of the revision is actually present?
 
516
        raise NotImplementedError('add_pending_merge is abstract')
 
517
 
 
518
    def set_pending_merges(self, rev_list):
 
519
        raise NotImplementedError('set_pending_merges is abstract')
 
520
 
 
521
    def get_parent(self):
 
522
        """Return the parent location of the branch.
 
523
 
 
524
        This is the default location for push/pull/missing.  The usual
 
525
        pattern is that the user can override it by specifying a
 
526
        location.
 
527
        """
 
528
        raise NotImplementedError('get_parent is abstract')
 
529
 
 
530
    def get_push_location(self):
 
531
        """Return the None or the location to push this branch to."""
 
532
        raise NotImplementedError('get_push_location is abstract')
 
533
 
 
534
    def set_push_location(self, location):
 
535
        """Set a new push location for this branch."""
 
536
        raise NotImplementedError('set_push_location is abstract')
 
537
 
 
538
    def set_parent(self, url):
 
539
        raise NotImplementedError('set_parent is abstract')
 
540
 
 
541
    def check_revno(self, revno):
 
542
        """\
 
543
        Check whether a revno corresponds to any revision.
 
544
        Zero (the NULL revision) is considered valid.
 
545
        """
 
546
        if revno != 0:
 
547
            self.check_real_revno(revno)
 
548
            
 
549
    def check_real_revno(self, revno):
 
550
        """\
 
551
        Check whether a revno corresponds to a real revision.
 
552
        Zero (the NULL revision) is considered invalid
 
553
        """
 
554
        if revno < 1 or revno > self.revno():
 
555
            raise InvalidRevisionNumber(revno)
 
556
        
 
557
    def sign_revision(self, revision_id, gpg_strategy):
 
558
        raise NotImplementedError('sign_revision is abstract')
 
559
 
 
560
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
 
561
        raise NotImplementedError('store_revision_signature is abstract')
 
562
 
 
563
class BzrBranch(Branch):
159
564
    """A branch stored in the actual filesystem.
160
565
 
161
566
    Note that it's "local" in the context of the filesystem; it doesn't
187
592
    REVISION_NAMESPACES = {}
188
593
 
189
594
    def push_stores(self, branch_to):
190
 
        """Copy the content of this branches store to branch_to."""
 
595
        """See Branch.push_stores."""
191
596
        if (self._branch_format != branch_to._branch_format
192
597
            or self._branch_format != 4):
193
598
            from bzrlib.fetch import greedy_fetch
237
642
            # some existing branches where there's a mixture; we probably 
238
643
            # still want the option to look for both.
239
644
            relpath = self._rel_controlfilename(name)
240
 
            if compressed:
241
 
                store = CompressedTextStore(self._transport.clone(relpath),
242
 
                                            prefixed=prefixed)
243
 
            else:
244
 
                store = TextStore(self._transport.clone(relpath),
245
 
                                  prefixed=prefixed)
 
645
            store = TextStore(self._transport.clone(relpath),
 
646
                              prefixed=prefixed,
 
647
                              compressed=compressed)
246
648
            #if self._transport.should_cache():
247
649
            #    cache_path = os.path.join(self.cache_root, name)
248
650
            #    os.mkdir(cache_path)
313
715
        transaction.finish()
314
716
 
315
717
    def get_transaction(self):
316
 
        """Return the current active transaction.
317
 
 
318
 
        If no transaction is active, this returns a passthrough object
319
 
        for which all data is immediately flushed and no caching happens.
320
 
        """
 
718
        """See Branch.get_transaction."""
321
719
        if self._transaction is None:
322
720
            return transactions.PassThroughTransaction()
323
721
        else:
375
773
            self._lock_mode = self._lock_count = None
376
774
 
377
775
    def abspath(self, name):
378
 
        """Return absolute filename for something in the branch
379
 
        
380
 
        XXX: Robert Collins 20051017 what is this used for? why is it a branch
381
 
        method and not a tree method.
382
 
        """
 
776
        """See Branch.abspath."""
383
777
        return self._transport.abspath(name)
384
778
 
385
779
    def _rel_controlfilename(self, file_or_path):
390
784
        return bzrlib.transport.urlescape(bzrlib.BZRDIR + '/' + file_or_path)
391
785
 
392
786
    def controlfilename(self, file_or_path):
393
 
        """Return location relative to branch."""
 
787
        """See Branch.controlfilename."""
394
788
        return self._transport.abspath(self._rel_controlfilename(file_or_path))
395
789
 
396
790
    def controlfile(self, file_or_path, mode='r'):
397
 
        """Open a control file for this branch.
398
 
 
399
 
        There are two classes of file in the control directory: text
400
 
        and binary.  binary files are untranslated byte streams.  Text
401
 
        control files are stored with Unix newlines and in UTF-8, even
402
 
        if the platform or locale defaults are different.
403
 
 
404
 
        Controlfiles should almost never be opened in write mode but
405
 
        rather should be atomically copied and replaced using atomicfile.
406
 
        """
 
791
        """See Branch.controlfile."""
407
792
        import codecs
408
793
 
409
794
        relpath = self._rel_controlfilename(file_or_path)
425
810
            raise BzrError("invalid controlfile mode %r" % mode)
426
811
 
427
812
    def put_controlfile(self, path, f, encode=True):
428
 
        """Write an entry as a controlfile.
429
 
 
430
 
        :param path: The path to put the file, relative to the .bzr control
431
 
                     directory
432
 
        :param f: A file-like or string object whose contents should be copied.
433
 
        :param encode:  If true, encode the contents as utf-8
434
 
        """
 
813
        """See Branch.put_controlfile."""
435
814
        self.put_controlfiles([(path, f)], encode=encode)
436
815
 
437
816
    def put_controlfiles(self, files, encode=True):
438
 
        """Write several entries as controlfiles.
439
 
 
440
 
        :param files: A list of [(path, file)] pairs, where the path is the directory
441
 
                      underneath the bzr control directory
442
 
        :param encode:  If true, encode the contents as utf-8
443
 
        """
 
817
        """See Branch.put_controlfiles."""
444
818
        import codecs
445
819
        ctrl_files = []
446
820
        for path, f in files:
517
891
                            ' and "bzr init" again'])
518
892
 
519
893
    def get_root_id(self):
520
 
        """Return the id of this branches root"""
 
894
        """See Branch.get_root_id."""
521
895
        inv = self.get_inventory(self.last_revision())
522
896
        return inv.root.file_id
523
897
 
524
898
    @needs_write_lock
 
899
    def set_root_id(self, file_id):
 
900
        """See Branch.set_root_id."""
 
901
        inv = self.working_tree().read_working_inventory()
 
902
        orig_root_id = inv.root.file_id
 
903
        del inv._byid[inv.root.file_id]
 
904
        inv.root.file_id = file_id
 
905
        inv._byid[inv.root.file_id] = inv.root
 
906
        for fid in inv:
 
907
            entry = inv[fid]
 
908
            if entry.parent_id in (None, orig_root_id):
 
909
                entry.parent_id = inv.root.file_id
 
910
        self._write_inventory(inv)
 
911
 
 
912
    @needs_write_lock
525
913
    def add(self, files, ids=None):
526
 
        """Make files versioned.
527
 
 
528
 
        Note that the command line normally calls smart_add instead,
529
 
        which can automatically recurse.
530
 
 
531
 
        This puts the files in the Added state, so that they will be
532
 
        recorded by the next commit.
533
 
 
534
 
        files
535
 
            List of paths to add, relative to the base of the tree.
536
 
 
537
 
        ids
538
 
            If set, use these instead of automatically generated ids.
539
 
            Must be the same length as the list of files, but may
540
 
            contain None for ids that are to be autogenerated.
541
 
 
542
 
        TODO: Perhaps have an option to add the ids even if the files do
543
 
              not (yet) exist.
544
 
 
545
 
        TODO: Perhaps yield the ids and paths as they're added.
546
 
        """
 
914
        """See Branch.add."""
547
915
        # TODO: Re-adding a file that is removed in the working copy
548
916
        # should probably put it back with the previous ID.
549
917
        if isinstance(files, basestring):
589
957
 
590
958
    @needs_read_lock
591
959
    def print_file(self, file, revno):
592
 
        """Print `file` to stdout."""
 
960
        """See Branch.print_file."""
593
961
        tree = self.revision_tree(self.get_rev_id(revno))
594
962
        # use inventory as it was in that revision
595
963
        file_id = tree.inventory.path2id(file)
598
966
        tree.print_file(file_id)
599
967
 
600
968
    def unknowns(self):
601
 
        """Return all unknown files.
602
 
 
603
 
        These are files in the working directory that are not versioned or
604
 
        control files or ignored.
605
 
        
606
 
        >>> from bzrlib.workingtree import WorkingTree
607
 
        >>> b = ScratchBranch(files=['foo', 'foo~'])
608
 
        >>> map(str, b.unknowns())
609
 
        ['foo']
610
 
        >>> b.add('foo')
611
 
        >>> list(b.unknowns())
612
 
        []
613
 
        >>> WorkingTree(b.base, b).remove('foo')
614
 
        >>> list(b.unknowns())
615
 
        [u'foo']
616
 
        """
 
969
        """See Branch.unknowns."""
617
970
        return self.working_tree().unknowns()
618
971
 
619
972
    @needs_write_lock
620
973
    def append_revision(self, *revision_ids):
 
974
        """See Branch.append_revision."""
621
975
        for revision_id in revision_ids:
622
976
            mutter("add {%s} to revision-history" % revision_id)
623
977
        rev_history = self.revision_history()
626
980
 
627
981
    @needs_write_lock
628
982
    def set_revision_history(self, rev_history):
 
983
        """See Branch.set_revision_history."""
629
984
        self.put_controlfile('revision-history', '\n'.join(rev_history))
630
985
 
631
986
    def has_revision(self, revision_id):
632
 
        """True if this branch has a copy of the revision.
633
 
 
634
 
        This does not necessarily imply the revision is merge
635
 
        or on the mainline."""
 
987
        """See Branch.has_revision."""
636
988
        return (revision_id is None
637
989
                or self.revision_store.has_id(revision_id))
638
990
 
639
991
    @needs_read_lock
640
992
    def get_revision_xml_file(self, revision_id):
641
 
        """Return XML file object for revision object."""
 
993
        """See Branch.get_revision_xml_file."""
642
994
        if not revision_id or not isinstance(revision_id, basestring):
643
995
            raise InvalidRevisionId(revision_id=revision_id, branch=self)
644
996
        try:
650
1002
    get_revision_xml = get_revision_xml_file
651
1003
 
652
1004
    def get_revision_xml(self, revision_id):
 
1005
        """See Branch.get_revision_xml."""
653
1006
        return self.get_revision_xml_file(revision_id).read()
654
1007
 
655
1008
 
656
1009
    def get_revision(self, revision_id):
657
 
        """Return the Revision object for a named revision"""
 
1010
        """See Branch.get_revision."""
658
1011
        xml_file = self.get_revision_xml_file(revision_id)
659
1012
 
660
1013
        try:
667
1020
        assert r.revision_id == revision_id
668
1021
        return r
669
1022
 
670
 
    def get_revision_delta(self, revno):
671
 
        """Return the delta for one revision.
672
 
 
673
 
        The delta is relative to its mainline predecessor, or the
674
 
        empty tree for revision 1.
675
 
        """
676
 
        assert isinstance(revno, int)
677
 
        rh = self.revision_history()
678
 
        if not (1 <= revno <= len(rh)):
679
 
            raise InvalidRevisionNumber(revno)
680
 
 
681
 
        # revno is 1-based; list is 0-based
682
 
 
683
 
        new_tree = self.revision_tree(rh[revno-1])
684
 
        if revno == 1:
685
 
            old_tree = EmptyTree()
686
 
        else:
687
 
            old_tree = self.revision_tree(rh[revno-2])
688
 
 
689
 
        return compare_trees(old_tree, new_tree)
690
 
 
691
1023
    def get_revision_sha1(self, revision_id):
692
 
        """Hash the stored value of a revision, and return it."""
 
1024
        """See Branch.get_revision_sha1."""
693
1025
        # In the future, revision entries will be signed. At that
694
1026
        # point, it is probably best *not* to include the signature
695
1027
        # in the revision hash. Because that lets you re-sign
699
1031
        return bzrlib.osutils.sha_file(self.get_revision_xml_file(revision_id))
700
1032
 
701
1033
    def get_ancestry(self, revision_id):
702
 
        """Return a list of revision-ids integrated by a revision.
703
 
        
704
 
        This currently returns a list, but the ordering is not guaranteed:
705
 
        treat it as a set.
706
 
        """
 
1034
        """See Branch.get_ancestry."""
707
1035
        if revision_id is None:
708
1036
            return [None]
709
 
        w = self.get_inventory_weave()
 
1037
        w = self._get_inventory_weave()
710
1038
        return [None] + map(w.idx_to_name,
711
1039
                            w.inclusions([w.lookup(revision_id)]))
712
1040
 
713
 
    def get_inventory_weave(self):
 
1041
    def _get_inventory_weave(self):
714
1042
        return self.control_weaves.get_weave('inventory',
715
1043
                                             self.get_transaction())
716
1044
 
717
1045
    def get_inventory(self, revision_id):
718
 
        """Get Inventory object by hash."""
 
1046
        """See Branch.get_inventory."""
719
1047
        xml = self.get_inventory_xml(revision_id)
720
1048
        return bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
721
1049
 
722
1050
    def get_inventory_xml(self, revision_id):
723
 
        """Get inventory XML as a file object."""
 
1051
        """See Branch.get_inventory_xml."""
724
1052
        try:
725
1053
            assert isinstance(revision_id, basestring), type(revision_id)
726
 
            iw = self.get_inventory_weave()
 
1054
            iw = self._get_inventory_weave()
727
1055
            return iw.get_text(iw.lookup(revision_id))
728
1056
        except IndexError:
729
1057
            raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
730
1058
 
731
1059
    def get_inventory_sha1(self, revision_id):
732
 
        """Return the sha1 hash of the inventory entry
733
 
        """
 
1060
        """See Branch.get_inventory_sha1."""
734
1061
        return self.get_revision(revision_id).inventory_sha1
735
1062
 
736
1063
    def get_revision_inventory(self, revision_id):
737
 
        """Return inventory of a past revision."""
 
1064
        """See Branch.get_revision_inventory."""
738
1065
        # TODO: Unify this with get_inventory()
739
1066
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
740
1067
        # must be the same as its revision, so this is trivial.
750
1077
 
751
1078
    @needs_read_lock
752
1079
    def revision_history(self):
753
 
        """Return sequence of revision hashes on to this branch."""
 
1080
        """See Branch.revision_history."""
754
1081
        transaction = self.get_transaction()
755
1082
        history = transaction.map.find_revision_history()
756
1083
        if history is not None:
764
1091
        # transaction.register_clean(history, precious=True)
765
1092
        return list(history)
766
1093
 
767
 
    def revno(self):
768
 
        """Return current revision number for this branch.
769
 
 
770
 
        That is equivalent to the number of revisions committed to
771
 
        this branch.
772
 
        """
773
 
        return len(self.revision_history())
774
 
 
775
 
    def last_revision(self):
776
 
        """Return last patch hash, or None if no history.
777
 
        """
778
 
        ph = self.revision_history()
779
 
        if ph:
780
 
            return ph[-1]
781
 
        else:
782
 
            return None
783
 
 
784
 
    def missing_revisions(self, other, stop_revision=None, diverged_ok=False):
785
 
        """Return a list of new revisions that would perfectly fit.
786
 
        
787
 
        If self and other have not diverged, return a list of the revisions
788
 
        present in other, but missing from self.
789
 
 
790
 
        >>> from bzrlib.commit import commit
791
 
        >>> bzrlib.trace.silent = True
792
 
        >>> br1 = ScratchBranch()
793
 
        >>> br2 = ScratchBranch()
794
 
        >>> br1.missing_revisions(br2)
795
 
        []
796
 
        >>> commit(br2, "lala!", rev_id="REVISION-ID-1")
797
 
        >>> br1.missing_revisions(br2)
798
 
        [u'REVISION-ID-1']
799
 
        >>> br2.missing_revisions(br1)
800
 
        []
801
 
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1")
802
 
        >>> br1.missing_revisions(br2)
803
 
        []
804
 
        >>> commit(br2, "lala!", rev_id="REVISION-ID-2A")
805
 
        >>> br1.missing_revisions(br2)
806
 
        [u'REVISION-ID-2A']
807
 
        >>> commit(br1, "lala!", rev_id="REVISION-ID-2B")
808
 
        >>> br1.missing_revisions(br2)
809
 
        Traceback (most recent call last):
810
 
        DivergedBranches: These branches have diverged.
811
 
        """
812
 
        self_history = self.revision_history()
813
 
        self_len = len(self_history)
814
 
        other_history = other.revision_history()
815
 
        other_len = len(other_history)
816
 
        common_index = min(self_len, other_len) -1
817
 
        if common_index >= 0 and \
818
 
            self_history[common_index] != other_history[common_index]:
819
 
            raise DivergedBranches(self, other)
820
 
 
821
 
        if stop_revision is None:
822
 
            stop_revision = other_len
823
 
        else:
824
 
            assert isinstance(stop_revision, int)
825
 
            if stop_revision > other_len:
826
 
                raise bzrlib.errors.NoSuchRevision(self, stop_revision)
827
 
        return other_history[self_len:stop_revision]
828
 
 
829
1094
    def update_revisions(self, other, stop_revision=None):
830
 
        """Pull in new perfect-fit revisions."""
 
1095
        """See Branch.update_revisions."""
831
1096
        from bzrlib.fetch import greedy_fetch
832
1097
        if stop_revision is None:
833
1098
            stop_revision = other.last_revision()
842
1107
            self.append_revision(*pullable_revs)
843
1108
 
844
1109
    def pullable_revisions(self, other, stop_revision):
 
1110
        """See Branch.pullable_revisions."""
845
1111
        other_revno = other.revision_id_to_revno(stop_revision)
846
1112
        try:
847
1113
            return self.missing_revisions(other, other_revno)
878
1144
        return history[revno - 1]
879
1145
 
880
1146
    def revision_tree(self, revision_id):
881
 
        """Return Tree for a revision on this branch.
882
 
 
883
 
        `revision_id` may be None for the null revision, in which case
884
 
        an `EmptyTree` is returned."""
 
1147
        """See Branch.revision_tree."""
885
1148
        # TODO: refactor this to use an existing revision object
886
1149
        # so we don't need to read it in twice.
887
1150
        if revision_id == None or revision_id == NULL_REVISION:
891
1154
            return RevisionTree(self.weave_store, inv, revision_id)
892
1155
 
893
1156
    def working_tree(self):
894
 
        """Return a `Tree` for the working copy."""
 
1157
        """See Branch.working_tree."""
895
1158
        from bzrlib.workingtree import WorkingTree
896
1159
        # TODO: In the future, perhaps WorkingTree should utilize Transport
897
1160
        # RobertCollins 20051003 - I don't think it should - working trees are
904
1167
 
905
1168
    @needs_write_lock
906
1169
    def pull(self, source, overwrite=False):
 
1170
        """See Branch.pull."""
907
1171
        source.lock_read()
908
1172
        try:
909
1173
            try:
915
1179
        finally:
916
1180
            source.unlock()
917
1181
 
918
 
    def basis_tree(self):
919
 
        """Return `Tree` object for last revision.
920
 
 
921
 
        If there are no revisions yet, return an `EmptyTree`.
922
 
        """
923
 
        return self.revision_tree(self.last_revision())
924
 
 
925
1182
    @needs_write_lock
926
1183
    def rename_one(self, from_rel, to_rel):
927
 
        """Rename one file.
928
 
 
929
 
        This can change the directory or the filename or both.
930
 
        """
 
1184
        """See Branch.rename_one."""
931
1185
        tree = self.working_tree()
932
1186
        inv = tree.inventory
933
1187
        if not tree.has_filename(from_rel):
969
1223
 
970
1224
    @needs_write_lock
971
1225
    def move(self, from_paths, to_name):
972
 
        """Rename files.
973
 
 
974
 
        to_name must exist as a versioned directory.
975
 
 
976
 
        If to_name exists and is a directory, the files are moved into
977
 
        it, keeping their old names.  If it is a directory, 
978
 
 
979
 
        Note that to_name is only the last component of the new name;
980
 
        this doesn't change the directory.
981
 
 
982
 
        This returns a list of (from_path, to_path) pairs for each
983
 
        entry that is moved.
984
 
        """
 
1226
        """See Branch.move."""
985
1227
        result = []
986
1228
        ## TODO: Option to move IDs only
987
1229
        assert not isinstance(from_paths, basestring)
1033
1275
        return result
1034
1276
 
1035
1277
    def get_parent(self):
1036
 
        """Return the parent location of the branch.
1037
 
 
1038
 
        This is the default location for push/pull/missing.  The usual
1039
 
        pattern is that the user can override it by specifying a
1040
 
        location.
1041
 
        """
 
1278
        """See Branch.get_parent."""
1042
1279
        import errno
1043
1280
        _locs = ['parent', 'pull', 'x-pull']
1044
1281
        for l in _locs:
1050
1287
        return None
1051
1288
 
1052
1289
    def get_push_location(self):
1053
 
        """Return the None or the location to push this branch to."""
 
1290
        """See Branch.get_push_location."""
1054
1291
        config = bzrlib.config.BranchConfig(self)
1055
1292
        push_loc = config.get_user_option('push_location')
1056
1293
        return push_loc
1057
1294
 
1058
1295
    def set_push_location(self, location):
1059
 
        """Set a new push location for this branch."""
 
1296
        """See Branch.set_push_location."""
1060
1297
        config = bzrlib.config.LocationConfig(self.base)
1061
1298
        config.set_user_option('push_location', location)
1062
1299
 
1063
1300
    @needs_write_lock
1064
1301
    def set_parent(self, url):
 
1302
        """See Branch.set_parent."""
1065
1303
        # TODO: Maybe delete old location files?
1066
1304
        from bzrlib.atomicfile import AtomicFile
1067
1305
        f = AtomicFile(self.controlfilename('parent'))
1071
1309
        finally:
1072
1310
            f.close()
1073
1311
 
 
1312
    def tree_config(self):
 
1313
        return TreeConfig(self)
 
1314
 
1074
1315
    def check_revno(self, revno):
1075
1316
        """\
1076
1317
        Check whether a revno corresponds to any revision.
1088
1329
            raise InvalidRevisionNumber(revno)
1089
1330
        
1090
1331
    def sign_revision(self, revision_id, gpg_strategy):
 
1332
        """See Branch.sign_revision."""
1091
1333
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
1092
1334
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
1093
1335
 
1094
1336
    @needs_write_lock
1095
1337
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
 
1338
        """See Branch.store_revision_signature."""
1096
1339
        self.revision_store.add(StringIO(gpg_strategy.sign(plaintext)), 
1097
1340
                                revision_id, "sig")
1098
1341
 
1099
1342
 
1100
 
class ScratchBranch(_Branch):
 
1343
class ScratchBranch(BzrBranch):
1101
1344
    """Special test class: a branch that cleans up after itself.
1102
1345
 
1103
1346
    >>> b = ScratchBranch()