/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

  • Committer: Lalo Martins
  • Date: 2005-09-07 08:20:27 UTC
  • mto: (1185.1.5)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: lalo@exoweb.net-20050907082026-401ea0f66d26dcca
moving DivergedBranches from bzrlib.branch to bzrlib.errors, obeying:
# XXX: move into bzrlib.errors; subclass BzrError

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
24
24
     splitpath, \
25
25
     sha_file, appendpath, file_kind
26
 
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
 
26
 
 
27
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId, \
 
28
     DivergedBranches
27
29
import bzrlib.errors
28
30
from bzrlib.textui import show_status
29
31
from bzrlib.revision import Revision
30
 
from bzrlib.xml import unpack_xml
31
32
from bzrlib.delta import compare_trees
32
33
from bzrlib.tree import EmptyTree, RevisionTree
33
 
        
 
34
import bzrlib.xml
 
35
import bzrlib.ui
 
36
 
 
37
 
 
38
 
34
39
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
35
40
## TODO: Maybe include checks for common corruption of newlines, etc?
36
41
 
39
44
# repeatedly to calculate deltas.  We could perhaps have a weakref
40
45
# cache in memory to make this faster.
41
46
 
 
47
# TODO: please move the revision-string syntax stuff out of the branch
 
48
# object; it's clutter
 
49
 
42
50
 
43
51
def find_branch(f, **args):
44
52
    if f and (f.startswith('http://') or f.startswith('https://')):
101
109
    It is not necessary that f exists.
102
110
 
103
111
    Basically we keep looking up until we find the control directory or
104
 
    run into the root."""
 
112
    run into the root.  If there isn't one, raises NotBranchError.
 
113
    """
105
114
    if f == None:
106
115
        f = os.getcwd()
107
116
    elif hasattr(os.path, 'realpath'):
120
129
        head, tail = os.path.split(f)
121
130
        if head == f:
122
131
            # reached the root, whatever that may be
123
 
            raise BzrError('%r is not in a branch' % orig_f)
 
132
            raise bzrlib.errors.NotBranchError('%s is not in a branch' % orig_f)
124
133
        f = head
125
 
    
126
 
class DivergedBranches(Exception):
127
 
    def __init__(self, branch1, branch2):
128
 
        self.branch1 = branch1
129
 
        self.branch2 = branch2
130
 
        Exception.__init__(self, "These branches have diverged.")
 
134
 
 
135
 
131
136
 
132
137
 
133
138
######################################################################
208
213
            self._lock.unlock()
209
214
 
210
215
 
211
 
 
212
216
    def lock_write(self):
213
217
        if self._lock_mode:
214
218
            if self._lock_mode != 'w':
224
228
            self._lock_count = 1
225
229
 
226
230
 
227
 
 
228
231
    def lock_read(self):
229
232
        if self._lock_mode:
230
233
            assert self._lock_mode in ('r', 'w'), \
237
240
            self._lock_mode = 'r'
238
241
            self._lock_count = 1
239
242
                        
240
 
 
241
 
            
242
243
    def unlock(self):
243
244
        if not self._lock_mode:
244
245
            from errors import LockError
251
252
            self._lock = None
252
253
            self._lock_mode = self._lock_count = None
253
254
 
254
 
 
255
255
    def abspath(self, name):
256
256
        """Return absolute filename for something in the branch"""
257
257
        return os.path.join(self.base, name)
258
258
 
259
 
 
260
259
    def relpath(self, path):
261
260
        """Return path relative to this branch of something inside it.
262
261
 
263
262
        Raises an error if path is not in this branch."""
264
263
        return _relpath(self.base, path)
265
264
 
266
 
 
267
265
    def controlfilename(self, file_or_path):
268
266
        """Return location relative to branch."""
269
267
        if isinstance(file_or_path, basestring):
296
294
        else:
297
295
            raise BzrError("invalid controlfile mode %r" % mode)
298
296
 
299
 
 
300
 
 
301
297
    def _make_control(self):
302
298
        from bzrlib.inventory import Inventory
303
 
        from bzrlib.xml import pack_xml
304
299
        
305
300
        os.mkdir(self.controlfilename([]))
306
301
        self.controlfile('README', 'w').write(
316
311
            self.controlfile(f, 'w').write('')
317
312
        mutter('created control directory in ' + self.base)
318
313
 
319
 
        pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
 
314
        # if we want per-tree root ids then this is the place to set
 
315
        # them; they're not needed for now and so ommitted for
 
316
        # simplicity.
 
317
        f = self.controlfile('inventory','w')
 
318
        bzrlib.xml.serializer_v4.write_inventory(Inventory(), f)
320
319
 
321
320
 
322
321
    def _check_format(self):
331
330
        # on Windows from Linux and so on.  I think it might be better
332
331
        # to always make all internal files in unix format.
333
332
        fmt = self.controlfile('branch-format', 'r').read()
334
 
        fmt.replace('\r\n', '')
 
333
        fmt = fmt.replace('\r\n', '\n')
335
334
        if fmt != BZR_BRANCH_FORMAT:
336
335
            raise BzrError('sorry, branch format %r not supported' % fmt,
337
336
                           ['use a different bzr version',
357
356
    def read_working_inventory(self):
358
357
        """Read the working inventory."""
359
358
        from bzrlib.inventory import Inventory
360
 
        from bzrlib.xml import unpack_xml
361
 
        from time import time
362
 
        before = time()
363
359
        self.lock_read()
364
360
        try:
365
361
            # ElementTree does its own conversion from UTF-8, so open in
366
362
            # binary.
367
 
            inv = unpack_xml(Inventory,
368
 
                             self.controlfile('inventory', 'rb'))
369
 
            mutter("loaded inventory of %d items in %f"
370
 
                   % (len(inv), time() - before))
371
 
            return inv
 
363
            f = self.controlfile('inventory', 'rb')
 
364
            return bzrlib.xml.serializer_v4.read_inventory(f)
372
365
        finally:
373
366
            self.unlock()
374
367
            
380
373
        will be committed to the next revision.
381
374
        """
382
375
        from bzrlib.atomicfile import AtomicFile
383
 
        from bzrlib.xml import pack_xml
384
376
        
385
377
        self.lock_write()
386
378
        try:
387
379
            f = AtomicFile(self.controlfilename('inventory'), 'wb')
388
380
            try:
389
 
                pack_xml(inv, f)
 
381
                bzrlib.xml.serializer_v4.write_inventory(inv, f)
390
382
                f.commit()
391
383
            finally:
392
384
                f.close()
400
392
                         """Inventory for the working copy.""")
401
393
 
402
394
 
403
 
    def add(self, files, verbose=False, ids=None):
 
395
    def add(self, files, ids=None):
404
396
        """Make files versioned.
405
397
 
406
 
        Note that the command line normally calls smart_add instead.
 
398
        Note that the command line normally calls smart_add instead,
 
399
        which can automatically recurse.
407
400
 
408
401
        This puts the files in the Added state, so that they will be
409
402
        recorded by the next commit.
419
412
        TODO: Perhaps have an option to add the ids even if the files do
420
413
              not (yet) exist.
421
414
 
422
 
        TODO: Perhaps return the ids of the files?  But then again it
423
 
              is easy to retrieve them if they're needed.
424
 
 
425
 
        TODO: Adding a directory should optionally recurse down and
426
 
              add all non-ignored children.  Perhaps do that in a
427
 
              higher-level method.
 
415
        TODO: Perhaps yield the ids and paths as they're added.
428
416
        """
429
417
        # TODO: Re-adding a file that is removed in the working copy
430
418
        # should probably put it back with the previous ID.
466
454
                    file_id = gen_file_id(f)
467
455
                inv.add_path(f, kind=kind, file_id=file_id)
468
456
 
469
 
                if verbose:
470
 
                    print 'added', quotefn(f)
471
 
 
472
457
                mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
473
458
 
474
459
            self._write_inventory(inv)
584
569
            f.close()
585
570
 
586
571
 
587
 
    def get_revision_xml(self, revision_id):
 
572
    def get_revision_xml_file(self, revision_id):
588
573
        """Return XML file object for revision object."""
589
574
        if not revision_id or not isinstance(revision_id, basestring):
590
575
            raise InvalidRevisionId(revision_id)
594
579
            try:
595
580
                return self.revision_store[revision_id]
596
581
            except IndexError:
597
 
                raise bzrlib.errors.NoSuchRevision(revision_id)
 
582
                raise bzrlib.errors.NoSuchRevision(self, revision_id)
598
583
        finally:
599
584
            self.unlock()
600
585
 
601
586
 
 
587
    #deprecated
 
588
    get_revision_xml = get_revision_xml_file
 
589
 
 
590
 
602
591
    def get_revision(self, revision_id):
603
592
        """Return the Revision object for a named revision"""
604
 
        xml_file = self.get_revision_xml(revision_id)
 
593
        xml_file = self.get_revision_xml_file(revision_id)
605
594
 
606
595
        try:
607
 
            r = unpack_xml(Revision, xml_file)
 
596
            r = bzrlib.xml.serializer_v4.read_revision(xml_file)
608
597
        except SyntaxError, e:
609
598
            raise bzrlib.errors.BzrError('failed to unpack revision_xml',
610
599
                                         [revision_id,
655
644
               parameter which can be either an integer revno or a
656
645
               string hash."""
657
646
        from bzrlib.inventory import Inventory
658
 
        from bzrlib.xml import unpack_xml
659
 
 
660
 
        return unpack_xml(Inventory, self.inventory_store[inventory_id])
 
647
 
 
648
        f = self.get_inventory_xml_file(inventory_id)
 
649
        return bzrlib.xml.serializer_v4.read_inventory(f)
 
650
 
 
651
 
 
652
    def get_inventory_xml(self, inventory_id):
 
653
        """Get inventory XML as a file object."""
 
654
        return self.inventory_store[inventory_id]
 
655
 
 
656
    get_inventory_xml_file = get_inventory_xml
661
657
            
662
658
 
663
659
    def get_inventory_sha1(self, inventory_id):
664
660
        """Return the sha1 hash of the inventory entry
665
661
        """
666
 
        return sha_file(self.inventory_store[inventory_id])
 
662
        return sha_file(self.get_inventory_xml(inventory_id))
667
663
 
668
664
 
669
665
    def get_revision_inventory(self, revision_id):
755
751
            return None
756
752
 
757
753
 
758
 
    def missing_revisions(self, other, stop_revision=None):
 
754
    def missing_revisions(self, other, stop_revision=None, diverged_ok=False):
759
755
        """
760
756
        If self and other have not diverged, return a list of the revisions
761
757
        present in other, but missing from self.
794
790
        if stop_revision is None:
795
791
            stop_revision = other_len
796
792
        elif stop_revision > other_len:
797
 
            raise NoSuchRevision(self, stop_revision)
 
793
            raise bzrlib.errors.NoSuchRevision(self, stop_revision)
798
794
        
799
795
        return other_history[self_len:stop_revision]
800
796
 
801
797
 
802
798
    def update_revisions(self, other, stop_revision=None):
803
799
        """Pull in all new revisions from other branch.
804
 
        
805
 
        >>> from bzrlib.commit import commit
806
 
        >>> bzrlib.trace.silent = True
807
 
        >>> br1 = ScratchBranch(files=['foo', 'bar'])
808
 
        >>> br1.add('foo')
809
 
        >>> br1.add('bar')
810
 
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
811
 
        >>> br2 = ScratchBranch()
812
 
        >>> br2.update_revisions(br1)
813
 
        Added 2 texts.
814
 
        Added 1 inventories.
815
 
        Added 1 revisions.
816
 
        >>> br2.revision_history()
817
 
        [u'REVISION-ID-1']
818
 
        >>> br2.update_revisions(br1)
819
 
        Added 0 texts.
820
 
        Added 0 inventories.
821
 
        Added 0 revisions.
822
 
        >>> br1.text_store.total_size() == br2.text_store.total_size()
823
 
        True
824
800
        """
825
 
        from bzrlib.progress import ProgressBar
826
 
 
827
 
        pb = ProgressBar()
828
 
 
 
801
        from bzrlib.fetch import greedy_fetch
 
802
 
 
803
        pb = bzrlib.ui.ui_factory.progress_bar()
829
804
        pb.update('comparing histories')
 
805
 
830
806
        revision_ids = self.missing_revisions(other, stop_revision)
831
807
 
 
808
        if len(revision_ids) > 0:
 
809
            count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
 
810
        else:
 
811
            count = 0
 
812
        self.append_revision(*revision_ids)
 
813
        ## note("Added %d revisions." % count)
 
814
        pb.clear()
 
815
 
 
816
    def install_revisions(self, other, revision_ids, pb):
832
817
        if hasattr(other.revision_store, "prefetch"):
833
818
            other.revision_store.prefetch(revision_ids)
834
819
        if hasattr(other.inventory_store, "prefetch"):
835
820
            inventory_ids = [other.get_revision(r).inventory_id
836
821
                             for r in revision_ids]
837
822
            other.inventory_store.prefetch(inventory_ids)
 
823
 
 
824
        if pb is None:
 
825
            pb = bzrlib.ui.ui_factory.progress_bar()
838
826
                
839
827
        revisions = []
840
828
        needed_texts = set()
841
829
        i = 0
842
 
        for rev_id in revision_ids:
843
 
            i += 1
844
 
            pb.update('fetching revision', i, len(revision_ids))
845
 
            rev = other.get_revision(rev_id)
 
830
 
 
831
        failures = set()
 
832
        for i, rev_id in enumerate(revision_ids):
 
833
            pb.update('fetching revision', i+1, len(revision_ids))
 
834
            try:
 
835
                rev = other.get_revision(rev_id)
 
836
            except bzrlib.errors.NoSuchRevision:
 
837
                failures.add(rev_id)
 
838
                continue
 
839
 
846
840
            revisions.append(rev)
847
841
            inv = other.get_inventory(str(rev.inventory_id))
848
842
            for key, entry in inv.iter_entries():
853
847
 
854
848
        pb.clear()
855
849
                    
856
 
        count = self.text_store.copy_multi(other.text_store, needed_texts)
857
 
        print "Added %d texts." % count 
 
850
        count, cp_fail = self.text_store.copy_multi(other.text_store, 
 
851
                                                    needed_texts)
 
852
        #print "Added %d texts." % count 
858
853
        inventory_ids = [ f.inventory_id for f in revisions ]
859
 
        count = self.inventory_store.copy_multi(other.inventory_store, 
860
 
                                                inventory_ids)
861
 
        print "Added %d inventories." % count 
 
854
        count, cp_fail = self.inventory_store.copy_multi(other.inventory_store, 
 
855
                                                         inventory_ids)
 
856
        #print "Added %d inventories." % count 
862
857
        revision_ids = [ f.revision_id for f in revisions]
863
 
        count = self.revision_store.copy_multi(other.revision_store, 
864
 
                                               revision_ids)
865
 
        for revision_id in revision_ids:
866
 
            self.append_revision(revision_id)
867
 
        print "Added %d revisions." % count
868
 
                    
869
 
        
 
858
 
 
859
        count, cp_fail = self.revision_store.copy_multi(other.revision_store, 
 
860
                                                          revision_ids,
 
861
                                                          permit_failure=True)
 
862
        assert len(cp_fail) == 0 
 
863
        return count, failures
 
864
       
 
865
 
870
866
    def commit(self, *args, **kw):
871
867
        from bzrlib.commit import commit
872
868
        commit(self, *args, **kw)
874
870
 
875
871
    def lookup_revision(self, revision):
876
872
        """Return the revision identifier for a given revision information."""
877
 
        revno, info = self.get_revision_info(revision)
 
873
        revno, info = self._get_revision_info(revision)
878
874
        return info
879
875
 
 
876
 
 
877
    def revision_id_to_revno(self, revision_id):
 
878
        """Given a revision id, return its revno"""
 
879
        history = self.revision_history()
 
880
        try:
 
881
            return history.index(revision_id) + 1
 
882
        except ValueError:
 
883
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
 
884
 
 
885
 
880
886
    def get_revision_info(self, revision):
881
887
        """Return (revno, revision id) for revision identifier.
882
888
 
885
891
        revision can also be a string, in which case it is parsed for something like
886
892
            'date:' or 'revid:' etc.
887
893
        """
 
894
        revno, rev_id = self._get_revision_info(revision)
 
895
        if revno is None:
 
896
            raise bzrlib.errors.NoSuchRevision(self, revision)
 
897
        return revno, rev_id
 
898
 
 
899
    def get_rev_id(self, revno, history=None):
 
900
        """Find the revision id of the specified revno."""
 
901
        if revno == 0:
 
902
            return None
 
903
        if history is None:
 
904
            history = self.revision_history()
 
905
        elif revno <= 0 or revno > len(history):
 
906
            raise bzrlib.errors.NoSuchRevision(self, revno)
 
907
        return history[revno - 1]
 
908
 
 
909
    def _get_revision_info(self, revision):
 
910
        """Return (revno, revision id) for revision specifier.
 
911
 
 
912
        revision can be an integer, in which case it is assumed to be revno
 
913
        (though this will translate negative values into positive ones)
 
914
        revision can also be a string, in which case it is parsed for something
 
915
        like 'date:' or 'revid:' etc.
 
916
 
 
917
        A revid is always returned.  If it is None, the specifier referred to
 
918
        the null revision.  If the revid does not occur in the revision
 
919
        history, revno will be None.
 
920
        """
 
921
        
888
922
        if revision is None:
889
923
            return 0, None
890
924
        revno = None
894
928
            pass
895
929
        revs = self.revision_history()
896
930
        if isinstance(revision, int):
897
 
            if revision == 0:
898
 
                return 0, None
899
 
            # Mabye we should do this first, but we don't need it if revision == 0
900
931
            if revision < 0:
901
932
                revno = len(revs) + revision + 1
902
933
            else:
903
934
                revno = revision
 
935
            rev_id = self.get_rev_id(revno, revs)
904
936
        elif isinstance(revision, basestring):
905
937
            for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
906
938
                if revision.startswith(prefix):
907
 
                    revno = func(self, revs, revision)
 
939
                    result = func(self, revs, revision)
 
940
                    if len(result) > 1:
 
941
                        revno, rev_id = result
 
942
                    else:
 
943
                        revno = result[0]
 
944
                        rev_id = self.get_rev_id(revno, revs)
908
945
                    break
909
946
            else:
910
 
                raise BzrError('No namespace registered for string: %r' % revision)
 
947
                raise BzrError('No namespace registered for string: %r' %
 
948
                               revision)
 
949
        else:
 
950
            raise TypeError('Unhandled revision type %s' % revision)
911
951
 
912
 
        if revno is None or revno <= 0 or revno > len(revs):
913
 
            raise BzrError("no such revision %s" % revision)
914
 
        return revno, revs[revno-1]
 
952
        if revno is None:
 
953
            if rev_id is None:
 
954
                raise bzrlib.errors.NoSuchRevision(self, revision)
 
955
        return revno, rev_id
915
956
 
916
957
    def _namespace_revno(self, revs, revision):
917
958
        """Lookup a revision by revision number"""
918
959
        assert revision.startswith('revno:')
919
960
        try:
920
 
            return int(revision[6:])
 
961
            return (int(revision[6:]),)
921
962
        except ValueError:
922
963
            return None
923
964
    REVISION_NAMESPACES['revno:'] = _namespace_revno
924
965
 
925
966
    def _namespace_revid(self, revs, revision):
926
967
        assert revision.startswith('revid:')
 
968
        rev_id = revision[len('revid:'):]
927
969
        try:
928
 
            return revs.index(revision[6:]) + 1
 
970
            return revs.index(rev_id) + 1, rev_id
929
971
        except ValueError:
930
 
            return None
 
972
            return None, rev_id
931
973
    REVISION_NAMESPACES['revid:'] = _namespace_revid
932
974
 
933
975
    def _namespace_last(self, revs, revision):
935
977
        try:
936
978
            offset = int(revision[5:])
937
979
        except ValueError:
938
 
            return None
 
980
            return (None,)
939
981
        else:
940
982
            if offset <= 0:
941
983
                raise BzrError('You must supply a positive value for --revision last:XXX')
942
 
            return len(revs) - offset + 1
 
984
            return (len(revs) - offset + 1,)
943
985
    REVISION_NAMESPACES['last:'] = _namespace_last
944
986
 
945
987
    def _namespace_tag(self, revs, revision):
1020
1062
                # TODO: Handle timezone.
1021
1063
                dt = datetime.datetime.fromtimestamp(r.timestamp)
1022
1064
                if first >= dt and (last is None or dt >= last):
1023
 
                    return i+1
 
1065
                    return (i+1,)
1024
1066
        else:
1025
1067
            for i in range(len(revs)):
1026
1068
                r = self.get_revision(revs[i])
1027
1069
                # TODO: Handle timezone.
1028
1070
                dt = datetime.datetime.fromtimestamp(r.timestamp)
1029
1071
                if first <= dt and (last is None or dt <= last):
1030
 
                    return i+1
 
1072
                    return (i+1,)
1031
1073
    REVISION_NAMESPACES['date:'] = _namespace_date
1032
1074
 
1033
1075
    def revision_tree(self, revision_id):
1098
1140
 
1099
1141
            inv.rename(file_id, to_dir_id, to_tail)
1100
1142
 
1101
 
            print "%s => %s" % (from_rel, to_rel)
1102
 
 
1103
1143
            from_abs = self.abspath(from_rel)
1104
1144
            to_abs = self.abspath(to_rel)
1105
1145
            try:
1124
1164
 
1125
1165
        Note that to_name is only the last component of the new name;
1126
1166
        this doesn't change the directory.
 
1167
 
 
1168
        This returns a list of (from_path, to_path) pairs for each
 
1169
        entry that is moved.
1127
1170
        """
 
1171
        result = []
1128
1172
        self.lock_write()
1129
1173
        try:
1130
1174
            ## TODO: Option to move IDs only
1165
1209
            for f in from_paths:
1166
1210
                name_tail = splitpath(f)[-1]
1167
1211
                dest_path = appendpath(to_name, name_tail)
1168
 
                print "%s => %s" % (f, dest_path)
 
1212
                result.append((f, dest_path))
1169
1213
                inv.rename(inv.path2id(f), to_dir_id, name_tail)
1170
1214
                try:
1171
1215
                    os.rename(self.abspath(f), self.abspath(dest_path))
1177
1221
        finally:
1178
1222
            self.unlock()
1179
1223
 
 
1224
        return result
 
1225
 
1180
1226
 
1181
1227
    def revert(self, filenames, old_tree=None, backups=True):
1182
1228
        """Restore selected files to the versions from a previous tree.
1264
1310
            self.unlock()
1265
1311
 
1266
1312
 
 
1313
    def get_parent(self):
 
1314
        """Return the parent location of the branch.
 
1315
 
 
1316
        This is the default location for push/pull/missing.  The usual
 
1317
        pattern is that the user can override it by specifying a
 
1318
        location.
 
1319
        """
 
1320
        import errno
 
1321
        _locs = ['parent', 'pull', 'x-pull']
 
1322
        for l in _locs:
 
1323
            try:
 
1324
                return self.controlfile(l, 'r').read().strip('\n')
 
1325
            except IOError, e:
 
1326
                if e.errno != errno.ENOENT:
 
1327
                    raise
 
1328
        return None
 
1329
 
 
1330
 
 
1331
    def set_parent(self, url):
 
1332
        # TODO: Maybe delete old location files?
 
1333
        from bzrlib.atomicfile import AtomicFile
 
1334
        self.lock_write()
 
1335
        try:
 
1336
            f = AtomicFile(self.controlfilename('parent'))
 
1337
            try:
 
1338
                f.write(url + '\n')
 
1339
                f.commit()
 
1340
            finally:
 
1341
                f.close()
 
1342
        finally:
 
1343
            self.unlock()
 
1344
 
 
1345
    def check_revno(self, revno):
 
1346
        """\
 
1347
        Check whether a revno corresponds to any revision.
 
1348
        Zero (the NULL revision) is considered valid.
 
1349
        """
 
1350
        if revno != 0:
 
1351
            self.check_real_revno(revno)
 
1352
            
 
1353
    def check_real_revno(self, revno):
 
1354
        """\
 
1355
        Check whether a revno corresponds to a real revision.
 
1356
        Zero (the NULL revision) is considered invalid
 
1357
        """
 
1358
        if revno < 1 or revno > self.revno():
 
1359
            raise InvalidRevisionNumber(revno)
 
1360
        
 
1361
        
 
1362
 
1267
1363
 
1268
1364
class ScratchBranch(Branch):
1269
1365
    """Special test class: a branch that cleans up after itself.
1311
1407
        os.rmdir(base)
1312
1408
        copytree(self.base, base, symlinks=True)
1313
1409
        return ScratchBranch(base=base)
 
1410
 
 
1411
 
1314
1412
        
1315
1413
    def __del__(self):
1316
1414
        self.destroy()
1386
1484
    """Return a new tree-root file id."""
1387
1485
    return gen_file_id('TREE_ROOT')
1388
1486
 
 
1487
 
 
1488
def pull_loc(branch):
 
1489
    # TODO: Should perhaps just make attribute be 'base' in
 
1490
    # RemoteBranch and Branch?
 
1491
    if hasattr(branch, "baseurl"):
 
1492
        return branch.baseurl
 
1493
    else:
 
1494
        return branch.base
 
1495
 
 
1496
 
 
1497
def copy_branch(branch_from, to_location, revision=None):
 
1498
    """Copy branch_from into the existing directory to_location.
 
1499
 
 
1500
    revision
 
1501
        If not None, only revisions up to this point will be copied.
 
1502
        The head of the new branch will be that revision.
 
1503
 
 
1504
    to_location
 
1505
        The name of a local directory that exists but is empty.
 
1506
    """
 
1507
    from bzrlib.merge import merge
 
1508
    from bzrlib.branch import Branch
 
1509
 
 
1510
    assert isinstance(branch_from, Branch)
 
1511
    assert isinstance(to_location, basestring)
 
1512
    
 
1513
    br_to = Branch(to_location, init=True)
 
1514
    br_to.set_root_id(branch_from.get_root_id())
 
1515
    if revision is None:
 
1516
        revno = branch_from.revno()
 
1517
    else:
 
1518
        revno, rev_id = branch_from.get_revision_info(revision)
 
1519
    br_to.update_revisions(branch_from, stop_revision=revno)
 
1520
    merge((to_location, -1), (to_location, 0), this_dir=to_location,
 
1521
          check_clean=False, ignore_zero=True)
 
1522
    
 
1523
    from_location = pull_loc(branch_from)
 
1524
    br_to.set_parent(pull_loc(branch_from))
 
1525