~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-08-27 04:42:41 UTC
  • mfrom: (1092.1.43)
  • mto: (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1178.
  • Revision ID: aaron.bentley@utoronto.ca-20050827044241-23d676133b9fc981
Merge of robertc@robertcollins.net-20050826013321-52eee1f1da679ee9

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
 
26
27
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
27
28
import bzrlib.errors
28
29
from bzrlib.textui import show_status
30
31
from bzrlib.xml import unpack_xml
31
32
from bzrlib.delta import compare_trees
32
33
from bzrlib.tree import EmptyTree, RevisionTree
33
 
from bzrlib.progress import ProgressBar
34
 
 
35
 
       
 
34
import bzrlib.ui
 
35
 
 
36
 
 
37
 
36
38
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
37
39
## TODO: Maybe include checks for common corruption of newlines, etc?
38
40
 
106
108
    It is not necessary that f exists.
107
109
 
108
110
    Basically we keep looking up until we find the control directory or
109
 
    run into the root."""
 
111
    run into the root.  If there isn't one, raises NotBranchError.
 
112
    """
110
113
    if f == None:
111
114
        f = os.getcwd()
112
115
    elif hasattr(os.path, 'realpath'):
125
128
        head, tail = os.path.split(f)
126
129
        if head == f:
127
130
            # reached the root, whatever that may be
128
 
            raise BzrError('%r is not in a branch' % orig_f)
 
131
            raise bzrlib.errors.NotBranchError('%s is not in a branch' % orig_f)
129
132
        f = head
130
 
    
 
133
 
 
134
 
 
135
 
 
136
# XXX: move into bzrlib.errors; subclass BzrError    
131
137
class DivergedBranches(Exception):
132
138
    def __init__(self, branch1, branch2):
133
139
        self.branch1 = branch1
213
219
            self._lock.unlock()
214
220
 
215
221
 
216
 
 
217
222
    def lock_write(self):
218
223
        if self._lock_mode:
219
224
            if self._lock_mode != 'w':
229
234
            self._lock_count = 1
230
235
 
231
236
 
232
 
 
233
237
    def lock_read(self):
234
238
        if self._lock_mode:
235
239
            assert self._lock_mode in ('r', 'w'), \
242
246
            self._lock_mode = 'r'
243
247
            self._lock_count = 1
244
248
                        
245
 
 
246
 
            
247
249
    def unlock(self):
248
250
        if not self._lock_mode:
249
251
            from errors import LockError
256
258
            self._lock = None
257
259
            self._lock_mode = self._lock_count = None
258
260
 
259
 
 
260
261
    def abspath(self, name):
261
262
        """Return absolute filename for something in the branch"""
262
263
        return os.path.join(self.base, name)
263
264
 
264
 
 
265
265
    def relpath(self, path):
266
266
        """Return path relative to this branch of something inside it.
267
267
 
268
268
        Raises an error if path is not in this branch."""
269
269
        return _relpath(self.base, path)
270
270
 
271
 
 
272
271
    def controlfilename(self, file_or_path):
273
272
        """Return location relative to branch."""
274
273
        if isinstance(file_or_path, basestring):
301
300
        else:
302
301
            raise BzrError("invalid controlfile mode %r" % mode)
303
302
 
304
 
 
305
 
 
306
303
    def _make_control(self):
307
304
        from bzrlib.inventory import Inventory
308
305
        from bzrlib.xml import pack_xml
326
323
        # simplicity.
327
324
        pack_xml(Inventory(), self.controlfile('inventory','w'))
328
325
 
329
 
 
330
326
    def _check_format(self):
331
327
        """Check this branch format is supported.
332
328
 
807
803
        if stop_revision is None:
808
804
            stop_revision = other_len
809
805
        elif stop_revision > other_len:
810
 
            raise NoSuchRevision(self, stop_revision)
 
806
            raise bzrlib.errors.NoSuchRevision(self, stop_revision)
811
807
        
812
808
        return other_history[self_len:stop_revision]
813
809
 
814
810
 
815
811
    def update_revisions(self, other, stop_revision=None):
816
812
        """Pull in all new revisions from other branch.
817
 
        
818
 
        >>> from bzrlib.commit import commit
819
 
        >>> bzrlib.trace.silent = True
820
 
        >>> br1 = ScratchBranch(files=['foo', 'bar'])
821
 
        >>> br1.add('foo')
822
 
        >>> br1.add('bar')
823
 
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
824
 
        >>> br2 = ScratchBranch()
825
 
        >>> br2.update_revisions(br1)
826
 
        Added 2 texts.
827
 
        Added 1 inventories.
828
 
        Added 1 revisions.
829
 
        >>> br2.revision_history()
830
 
        [u'REVISION-ID-1']
831
 
        >>> br2.update_revisions(br1)
832
 
        Added 0 revisions.
833
 
        >>> br1.text_store.total_size() == br2.text_store.total_size()
834
 
        True
835
813
        """
836
814
        from bzrlib.fetch import greedy_fetch
837
 
        pb = ProgressBar()
 
815
 
 
816
        pb = bzrlib.ui.ui_factory.progress_bar()
838
817
        pb.update('comparing histories')
 
818
 
839
819
        revision_ids = self.missing_revisions(other, stop_revision)
 
820
 
840
821
        if len(revision_ids) > 0:
841
822
            count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
842
823
        else:
843
824
            count = 0
844
825
        self.append_revision(*revision_ids)
845
 
        print "Added %d revisions." % count
846
 
                    
847
 
    def install_revisions(self, other, revision_ids, pb=None):
848
 
        if pb is None:
849
 
            pb = ProgressBar()
 
826
        ## note("Added %d revisions." % count)
 
827
        pb.clear()
 
828
 
 
829
    def install_revisions(self, other, revision_ids, pb):
850
830
        if hasattr(other.revision_store, "prefetch"):
851
831
            other.revision_store.prefetch(revision_ids)
852
832
        if hasattr(other.inventory_store, "prefetch"):
853
833
            inventory_ids = [other.get_revision(r).inventory_id
854
834
                             for r in revision_ids]
855
835
            other.inventory_store.prefetch(inventory_ids)
 
836
 
 
837
        if pb is None:
 
838
            pb = bzrlib.ui.ui_factory.progress_bar()
856
839
                
857
840
        revisions = []
858
841
        needed_texts = set()
859
842
        i = 0
 
843
 
860
844
        failures = set()
861
845
        for i, rev_id in enumerate(revision_ids):
862
846
            pb.update('fetching revision', i+1, len(revision_ids))
865
849
            except bzrlib.errors.NoSuchRevision:
866
850
                failures.add(rev_id)
867
851
                continue
 
852
 
868
853
            revisions.append(rev)
869
854
            inv = other.get_inventory(str(rev.inventory_id))
870
855
            for key, entry in inv.iter_entries():
877
862
                    
878
863
        count, cp_fail = self.text_store.copy_multi(other.text_store, 
879
864
                                                    needed_texts)
880
 
        print "Added %d texts." % count 
 
865
        #print "Added %d texts." % count 
881
866
        inventory_ids = [ f.inventory_id for f in revisions ]
882
867
        count, cp_fail = self.inventory_store.copy_multi(other.inventory_store, 
883
868
                                                         inventory_ids)
884
 
        print "Added %d inventories." % count 
 
869
        #print "Added %d inventories." % count 
885
870
        revision_ids = [ f.revision_id for f in revisions]
 
871
 
886
872
        count, cp_fail = self.revision_store.copy_multi(other.revision_store, 
887
873
                                                          revision_ids,
888
874
                                                          permit_failure=True)
889
875
        assert len(cp_fail) == 0 
890
876
        return count, failures
891
877
       
 
878
 
892
879
    def commit(self, *args, **kw):
893
880
        from bzrlib.commit import commit
894
881
        commit(self, *args, **kw)
899
886
        revno, info = self._get_revision_info(revision)
900
887
        return info
901
888
 
 
889
 
 
890
    def revision_id_to_revno(self, revision_id):
 
891
        """Given a revision id, return its revno"""
 
892
        history = self.revision_history()
 
893
        try:
 
894
            return history.index(revision_id) + 1
 
895
        except ValueError:
 
896
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
 
897
 
 
898
 
902
899
    def get_revision_info(self, revision):
903
900
        """Return (revno, revision id) for revision identifier.
904
901
 
1444
1441
    """Return a new tree-root file id."""
1445
1442
    return gen_file_id('TREE_ROOT')
1446
1443
 
 
1444
 
 
1445
def pull_loc(branch):
 
1446
    # TODO: Should perhaps just make attribute be 'base' in
 
1447
    # RemoteBranch and Branch?
 
1448
    if hasattr(branch, "baseurl"):
 
1449
        return branch.baseurl
 
1450
    else:
 
1451
        return branch.base
 
1452
 
 
1453
 
 
1454
def copy_branch(branch_from, to_location, revision=None):
 
1455
    """Copy branch_from into the existing directory to_location.
 
1456
 
 
1457
    If revision is not None, the head of the new branch will be revision.
 
1458
    """
 
1459
    from bzrlib.merge import merge
 
1460
    from bzrlib.branch import Branch
 
1461
    br_to = Branch(to_location, init=True)
 
1462
    br_to.set_root_id(branch_from.get_root_id())
 
1463
    if revision is None:
 
1464
        revno = branch_from.revno()
 
1465
    else:
 
1466
        revno, rev_id = branch_from.get_revision_info(revision)
 
1467
    br_to.update_revisions(branch_from, stop_revision=revno)
 
1468
    merge((to_location, -1), (to_location, 0), this_dir=to_location,
 
1469
          check_clean=False, ignore_zero=True)
 
1470
    from_location = pull_loc(branch_from)
 
1471
    br_to.controlfile("x-pull", "wb").write(from_location + "\n")
 
1472