~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-07-23 13:52:38 UTC
  • Revision ID: mbp@sourcefrog.net-20050723135238-96b1580de8dff136
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
import sys, os, os.path, random, time, sha, sets, types, re, shutil, tempfile
19
 
import traceback, socket, fnmatch, difflib, time
20
 
from binascii import hexlify
 
18
import sys, os
21
19
 
22
20
import bzrlib
23
 
from inventory import Inventory
24
 
from trace import mutter, note
25
 
from tree import Tree, EmptyTree, RevisionTree
26
 
from inventory import InventoryEntry, Inventory
27
 
from osutils import isdir, quotefn, isfile, uuid, sha_file, username, \
28
 
     format_date, compact_date, pumpfile, user_email, rand_bytes, splitpath, \
29
 
     joinpath, sha_file, sha_string, file_kind, local_time_offset, appendpath
30
 
from store import ImmutableStore
31
 
from revision import Revision
32
 
from errors import BzrError
33
 
from textui import show_status
 
21
from bzrlib.trace import mutter, note
 
22
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, splitpath, \
 
23
     sha_file, appendpath, file_kind
 
24
from bzrlib.errors import BzrError
34
25
 
35
26
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
36
27
## TODO: Maybe include checks for common corruption of newlines, etc?
159
150
    _lock_count = None
160
151
    _lock = None
161
152
    
 
153
    # Map some sort of prefix into a namespace
 
154
    # stuff like "revno:10", "revid:", etc.
 
155
    # This should match a prefix with a function which accepts
 
156
    REVISION_NAMESPACES = {}
 
157
 
162
158
    def __init__(self, base, init=False, find_root=True):
163
159
        """Create new branch object at a particular location.
164
160
 
174
170
        In the test suite, creation of new trees is tested using the
175
171
        `ScratchBranch` class.
176
172
        """
 
173
        from bzrlib.store import ImmutableStore
177
174
        if init:
178
175
            self.base = os.path.realpath(base)
179
176
            self._make_control()
265
262
 
266
263
    def controlfilename(self, file_or_path):
267
264
        """Return location relative to branch."""
268
 
        if isinstance(file_or_path, types.StringTypes):
 
265
        if isinstance(file_or_path, basestring):
269
266
            file_or_path = [file_or_path]
270
267
        return os.path.join(self.base, bzrlib.BZRDIR, *file_or_path)
271
268
 
298
295
 
299
296
 
300
297
    def _make_control(self):
 
298
        from bzrlib.inventory import Inventory
 
299
        from bzrlib.xml import pack_xml
 
300
        
301
301
        os.mkdir(self.controlfilename([]))
302
302
        self.controlfile('README', 'w').write(
303
303
            "This is a Bazaar-NG control directory.\n"
307
307
            os.mkdir(self.controlfilename(d))
308
308
        for f in ('revision-history', 'merged-patches',
309
309
                  'pending-merged-patches', 'branch-name',
310
 
                  'branch-lock'):
 
310
                  'branch-lock',
 
311
                  'pending-merges'):
311
312
            self.controlfile(f, 'w').write('')
312
313
        mutter('created control directory in ' + self.base)
313
 
        Inventory().write_xml(self.controlfile('inventory','w'))
 
314
 
 
315
        pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
314
316
 
315
317
 
316
318
    def _check_format(self):
331
333
                           ['use a different bzr version',
332
334
                            'or remove the .bzr directory and "bzr init" again'])
333
335
 
 
336
    def get_root_id(self):
 
337
        """Return the id of this branches root"""
 
338
        inv = self.read_working_inventory()
 
339
        return inv.root.file_id
334
340
 
 
341
    def set_root_id(self, file_id):
 
342
        inv = self.read_working_inventory()
 
343
        orig_root_id = inv.root.file_id
 
344
        del inv._byid[inv.root.file_id]
 
345
        inv.root.file_id = file_id
 
346
        inv._byid[inv.root.file_id] = inv.root
 
347
        for fid in inv:
 
348
            entry = inv[fid]
 
349
            if entry.parent_id in (None, orig_root_id):
 
350
                entry.parent_id = inv.root.file_id
 
351
        self._write_inventory(inv)
335
352
 
336
353
    def read_working_inventory(self):
337
354
        """Read the working inventory."""
338
 
        before = time.time()
339
 
        # ElementTree does its own conversion from UTF-8, so open in
340
 
        # binary.
 
355
        from bzrlib.inventory import Inventory
 
356
        from bzrlib.xml import unpack_xml
 
357
        from time import time
 
358
        before = time()
341
359
        self.lock_read()
342
360
        try:
343
 
            inv = Inventory.read_xml(self.controlfile('inventory', 'rb'))
 
361
            # ElementTree does its own conversion from UTF-8, so open in
 
362
            # binary.
 
363
            inv = unpack_xml(Inventory,
 
364
                             self.controlfile('inventory', 'rb'))
344
365
            mutter("loaded inventory of %d items in %f"
345
 
                   % (len(inv), time.time() - before))
 
366
                   % (len(inv), time() - before))
346
367
            return inv
347
368
        finally:
348
369
            self.unlock()
354
375
        That is to say, the inventory describing changes underway, that
355
376
        will be committed to the next revision.
356
377
        """
 
378
        from bzrlib.atomicfile import AtomicFile
 
379
        from bzrlib.xml import pack_xml
 
380
        
357
381
        self.lock_write()
358
382
        try:
359
 
            from bzrlib.atomicfile import AtomicFile
360
 
 
361
383
            f = AtomicFile(self.controlfilename('inventory'), 'wb')
362
384
            try:
363
 
                inv.write_xml(f)
 
385
                pack_xml(inv, f)
364
386
                f.commit()
365
387
            finally:
366
388
                f.close()
400
422
              add all non-ignored children.  Perhaps do that in a
401
423
              higher-level method.
402
424
        """
 
425
        from bzrlib.textui import show_status
403
426
        # TODO: Re-adding a file that is removed in the working copy
404
427
        # should probably put it back with the previous ID.
405
 
        if isinstance(files, types.StringTypes):
406
 
            assert(ids is None or isinstance(ids, types.StringTypes))
 
428
        if isinstance(files, basestring):
 
429
            assert(ids is None or isinstance(ids, basestring))
407
430
            files = [files]
408
431
            if ids is not None:
409
432
                ids = [ids]
458
481
            # use inventory as it was in that revision
459
482
            file_id = tree.inventory.path2id(file)
460
483
            if not file_id:
461
 
                raise BzrError("%r is not present in revision %d" % (file, revno))
 
484
                raise BzrError("%r is not present in revision %s" % (file, revno))
462
485
            tree.print_file(file_id)
463
486
        finally:
464
487
            self.unlock()
478
501
        is the opposite of add.  Removing it is consistent with most
479
502
        other tools.  Maybe an option.
480
503
        """
 
504
        from bzrlib.textui import show_status
481
505
        ## TODO: Normalize names
482
506
        ## TODO: Remove nested loops; better scalability
483
 
        if isinstance(files, types.StringTypes):
 
507
        if isinstance(files, basestring):
484
508
            files = [files]
485
509
 
486
510
        self.lock_write()
511
535
 
512
536
    # FIXME: this doesn't need to be a branch method
513
537
    def set_inventory(self, new_inventory_list):
514
 
        inv = Inventory()
 
538
        from bzrlib.inventory import Inventory, InventoryEntry
 
539
        inv = Inventory(self.get_root_id())
515
540
        for path, file_id, parent, kind in new_inventory_list:
516
541
            name = os.path.basename(path)
517
542
            if name == "":
539
564
        return self.working_tree().unknowns()
540
565
 
541
566
 
542
 
    def append_revision(self, revision_id):
 
567
    def append_revision(self, *revision_ids):
543
568
        from bzrlib.atomicfile import AtomicFile
544
569
 
545
 
        mutter("add {%s} to revision-history" % revision_id)
546
 
        rev_history = self.revision_history() + [revision_id]
 
570
        for revision_id in revision_ids:
 
571
            mutter("add {%s} to revision-history" % revision_id)
 
572
 
 
573
        rev_history = self.revision_history()
 
574
        rev_history.extend(revision_ids)
547
575
 
548
576
        f = AtomicFile(self.controlfilename('revision-history'))
549
577
        try:
556
584
 
557
585
    def get_revision(self, revision_id):
558
586
        """Return the Revision object for a named revision"""
559
 
        if not revision_id or not isinstance(revision_id, basestring):
560
 
            raise ValueError('invalid revision-id: %r' % revision_id)
561
 
        r = Revision.read_xml(self.revision_store[revision_id])
 
587
        from bzrlib.revision import Revision
 
588
        from bzrlib.xml import unpack_xml
 
589
 
 
590
        self.lock_read()
 
591
        try:
 
592
            if not revision_id or not isinstance(revision_id, basestring):
 
593
                raise ValueError('invalid revision-id: %r' % revision_id)
 
594
            r = unpack_xml(Revision, self.revision_store[revision_id])
 
595
        finally:
 
596
            self.unlock()
 
597
            
562
598
        assert r.revision_id == revision_id
563
599
        return r
 
600
        
564
601
 
565
602
    def get_revision_sha1(self, revision_id):
566
603
        """Hash the stored value of a revision, and return it."""
579
616
        TODO: Perhaps for this and similar methods, take a revision
580
617
               parameter which can be either an integer revno or a
581
618
               string hash."""
582
 
        i = Inventory.read_xml(self.inventory_store[inventory_id])
583
 
        return i
 
619
        from bzrlib.inventory import Inventory
 
620
        from bzrlib.xml import unpack_xml
 
621
 
 
622
        return unpack_xml(Inventory, self.inventory_store[inventory_id])
 
623
            
584
624
 
585
625
    def get_inventory_sha1(self, inventory_id):
586
626
        """Return the sha1 hash of the inventory entry
590
630
 
591
631
    def get_revision_inventory(self, revision_id):
592
632
        """Return inventory of a past revision."""
 
633
        # bzr 0.0.6 imposes the constraint that the inventory_id
 
634
        # must be the same as its revision, so this is trivial.
593
635
        if revision_id == None:
594
 
            return Inventory()
 
636
            from bzrlib.inventory import Inventory
 
637
            return Inventory(self.get_root_id())
595
638
        else:
596
 
            return self.get_inventory(self.get_revision(revision_id).inventory_id)
 
639
            return self.get_inventory(revision_id)
597
640
 
598
641
 
599
642
    def revision_history(self):
777
820
            other.inventory_store.prefetch(inventory_ids)
778
821
                
779
822
        revisions = []
780
 
        needed_texts = sets.Set()
 
823
        needed_texts = set()
781
824
        i = 0
782
825
        for rev_id in revision_ids:
783
826
            i += 1
812
855
        commit(self, *args, **kw)
813
856
        
814
857
 
815
 
    def lookup_revision(self, revno):
816
 
        """Return revision hash for revision number."""
817
 
        if revno == 0:
818
 
            return None
819
 
 
820
 
        try:
821
 
            # list is 0-based; revisions are 1-based
822
 
            return self.revision_history()[revno-1]
823
 
        except IndexError:
824
 
            raise BzrError("no such revision %s" % revno)
825
 
 
 
858
    def lookup_revision(self, revision):
 
859
        """Return the revision identifier for a given revision information."""
 
860
        revno, info = self.get_revision_info(revision)
 
861
        return info
 
862
 
 
863
    def get_revision_info(self, revision):
 
864
        """Return (revno, revision id) for revision identifier.
 
865
 
 
866
        revision can be an integer, in which case it is assumed to be revno (though
 
867
            this will translate negative values into positive ones)
 
868
        revision can also be a string, in which case it is parsed for something like
 
869
            'date:' or 'revid:' etc.
 
870
        """
 
871
        if revision is None:
 
872
            return 0, None
 
873
        revno = None
 
874
        try:# Convert to int if possible
 
875
            revision = int(revision)
 
876
        except ValueError:
 
877
            pass
 
878
        revs = self.revision_history()
 
879
        if isinstance(revision, int):
 
880
            if revision == 0:
 
881
                return 0, None
 
882
            # Mabye we should do this first, but we don't need it if revision == 0
 
883
            if revision < 0:
 
884
                revno = len(revs) + revision + 1
 
885
            else:
 
886
                revno = revision
 
887
        elif isinstance(revision, basestring):
 
888
            for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
 
889
                if revision.startswith(prefix):
 
890
                    revno = func(self, revs, revision)
 
891
                    break
 
892
            else:
 
893
                raise BzrError('No namespace registered for string: %r' % revision)
 
894
 
 
895
        if revno is None or revno <= 0 or revno > len(revs):
 
896
            raise BzrError("no such revision %s" % revision)
 
897
        return revno, revs[revno-1]
 
898
 
 
899
    def _namespace_revno(self, revs, revision):
 
900
        """Lookup a revision by revision number"""
 
901
        assert revision.startswith('revno:')
 
902
        try:
 
903
            return int(revision[6:])
 
904
        except ValueError:
 
905
            return None
 
906
    REVISION_NAMESPACES['revno:'] = _namespace_revno
 
907
 
 
908
    def _namespace_revid(self, revs, revision):
 
909
        assert revision.startswith('revid:')
 
910
        try:
 
911
            return revs.index(revision[6:]) + 1
 
912
        except ValueError:
 
913
            return None
 
914
    REVISION_NAMESPACES['revid:'] = _namespace_revid
 
915
 
 
916
    def _namespace_last(self, revs, revision):
 
917
        assert revision.startswith('last:')
 
918
        try:
 
919
            offset = int(revision[5:])
 
920
        except ValueError:
 
921
            return None
 
922
        else:
 
923
            if offset <= 0:
 
924
                raise BzrError('You must supply a positive value for --revision last:XXX')
 
925
            return len(revs) - offset + 1
 
926
    REVISION_NAMESPACES['last:'] = _namespace_last
 
927
 
 
928
    def _namespace_tag(self, revs, revision):
 
929
        assert revision.startswith('tag:')
 
930
        raise BzrError('tag: namespace registered, but not implemented.')
 
931
    REVISION_NAMESPACES['tag:'] = _namespace_tag
 
932
 
 
933
    def _namespace_date(self, revs, revision):
 
934
        assert revision.startswith('date:')
 
935
        import datetime
 
936
        # Spec for date revisions:
 
937
        #   date:value
 
938
        #   value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
 
939
        #   it can also start with a '+/-/='. '+' says match the first
 
940
        #   entry after the given date. '-' is match the first entry before the date
 
941
        #   '=' is match the first entry after, but still on the given date.
 
942
        #
 
943
        #   +2005-05-12 says find the first matching entry after May 12th, 2005 at 0:00
 
944
        #   -2005-05-12 says find the first matching entry before May 12th, 2005 at 0:00
 
945
        #   =2005-05-12 says find the first match after May 12th, 2005 at 0:00 but before
 
946
        #       May 13th, 2005 at 0:00
 
947
        #
 
948
        #   So the proper way of saying 'give me all entries for today' is:
 
949
        #       -r {date:+today}:{date:-tomorrow}
 
950
        #   The default is '=' when not supplied
 
951
        val = revision[5:]
 
952
        match_style = '='
 
953
        if val[:1] in ('+', '-', '='):
 
954
            match_style = val[:1]
 
955
            val = val[1:]
 
956
 
 
957
        today = datetime.datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
 
958
        if val.lower() == 'yesterday':
 
959
            dt = today - datetime.timedelta(days=1)
 
960
        elif val.lower() == 'today':
 
961
            dt = today
 
962
        elif val.lower() == 'tomorrow':
 
963
            dt = today + datetime.timedelta(days=1)
 
964
        else:
 
965
            import re
 
966
            # This should be done outside the function to avoid recompiling it.
 
967
            _date_re = re.compile(
 
968
                    r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
 
969
                    r'(,|T)?\s*'
 
970
                    r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
 
971
                )
 
972
            m = _date_re.match(val)
 
973
            if not m or (not m.group('date') and not m.group('time')):
 
974
                raise BzrError('Invalid revision date %r' % revision)
 
975
 
 
976
            if m.group('date'):
 
977
                year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
 
978
            else:
 
979
                year, month, day = today.year, today.month, today.day
 
980
            if m.group('time'):
 
981
                hour = int(m.group('hour'))
 
982
                minute = int(m.group('minute'))
 
983
                if m.group('second'):
 
984
                    second = int(m.group('second'))
 
985
                else:
 
986
                    second = 0
 
987
            else:
 
988
                hour, minute, second = 0,0,0
 
989
 
 
990
            dt = datetime.datetime(year=year, month=month, day=day,
 
991
                    hour=hour, minute=minute, second=second)
 
992
        first = dt
 
993
        last = None
 
994
        reversed = False
 
995
        if match_style == '-':
 
996
            reversed = True
 
997
        elif match_style == '=':
 
998
            last = dt + datetime.timedelta(days=1)
 
999
 
 
1000
        if reversed:
 
1001
            for i in range(len(revs)-1, -1, -1):
 
1002
                r = self.get_revision(revs[i])
 
1003
                # TODO: Handle timezone.
 
1004
                dt = datetime.datetime.fromtimestamp(r.timestamp)
 
1005
                if first >= dt and (last is None or dt >= last):
 
1006
                    return i+1
 
1007
        else:
 
1008
            for i in range(len(revs)):
 
1009
                r = self.get_revision(revs[i])
 
1010
                # TODO: Handle timezone.
 
1011
                dt = datetime.datetime.fromtimestamp(r.timestamp)
 
1012
                if first <= dt and (last is None or dt <= last):
 
1013
                    return i+1
 
1014
    REVISION_NAMESPACES['date:'] = _namespace_date
826
1015
 
827
1016
    def revision_tree(self, revision_id):
828
1017
        """Return Tree for a revision on this branch.
829
1018
 
830
1019
        `revision_id` may be None for the null revision, in which case
831
1020
        an `EmptyTree` is returned."""
 
1021
        from bzrlib.tree import EmptyTree, RevisionTree
832
1022
        # TODO: refactor this to use an existing revision object
833
1023
        # so we don't need to read it in twice.
834
1024
        if revision_id == None:
835
 
            return EmptyTree()
 
1025
            return EmptyTree(self.get_root_id())
836
1026
        else:
837
1027
            inv = self.get_revision_inventory(revision_id)
838
1028
            return RevisionTree(self.text_store, inv)
849
1039
 
850
1040
        If there are no revisions yet, return an `EmptyTree`.
851
1041
        """
 
1042
        from bzrlib.tree import EmptyTree, RevisionTree
852
1043
        r = self.last_patch()
853
1044
        if r == None:
854
 
            return EmptyTree()
 
1045
            return EmptyTree(self.get_root_id())
855
1046
        else:
856
1047
            return RevisionTree(self.text_store, self.get_revision_inventory(r))
857
1048
 
1016
1207
                f.close()
1017
1208
 
1018
1209
 
 
1210
    def pending_merges(self):
 
1211
        """Return a list of pending merges.
 
1212
 
 
1213
        These are revisions that have been merged into the working
 
1214
        directory but not yet committed.
 
1215
        """
 
1216
        cfn = self.controlfilename('pending-merges')
 
1217
        if not os.path.exists(cfn):
 
1218
            return []
 
1219
        p = []
 
1220
        for l in self.controlfile('pending-merges', 'r').readlines():
 
1221
            p.append(l.rstrip('\n'))
 
1222
        return p
 
1223
 
 
1224
 
 
1225
    def add_pending_merge(self, revision_id):
 
1226
        from bzrlib.revision import validate_revision_id
 
1227
 
 
1228
        validate_revision_id(revision_id)
 
1229
 
 
1230
        p = self.pending_merges()
 
1231
        if revision_id in p:
 
1232
            return
 
1233
        p.append(revision_id)
 
1234
        self.set_pending_merges(p)
 
1235
 
 
1236
 
 
1237
    def set_pending_merges(self, rev_list):
 
1238
        from bzrlib.atomicfile import AtomicFile
 
1239
        self.lock_write()
 
1240
        try:
 
1241
            f = AtomicFile(self.controlfilename('pending-merges'))
 
1242
            try:
 
1243
                for l in rev_list:
 
1244
                    print >>f, l
 
1245
                f.commit()
 
1246
            finally:
 
1247
                f.close()
 
1248
        finally:
 
1249
            self.unlock()
 
1250
 
 
1251
 
1019
1252
 
1020
1253
class ScratchBranch(Branch):
1021
1254
    """Special test class: a branch that cleans up after itself.
1035
1268
 
1036
1269
        If any files are listed, they are created in the working copy.
1037
1270
        """
 
1271
        from tempfile import mkdtemp
1038
1272
        init = False
1039
1273
        if base is None:
1040
 
            base = tempfile.mkdtemp()
 
1274
            base = mkdtemp()
1041
1275
            init = True
1042
1276
        Branch.__init__(self, base, init=init)
1043
1277
        for d in dirs:
1056
1290
        >>> os.path.isfile(os.path.join(clone.base, "file1"))
1057
1291
        True
1058
1292
        """
1059
 
        base = tempfile.mkdtemp()
 
1293
        from shutil import copytree
 
1294
        from tempfile import mkdtemp
 
1295
        base = mkdtemp()
1060
1296
        os.rmdir(base)
1061
 
        shutil.copytree(self.base, base, symlinks=True)
 
1297
        copytree(self.base, base, symlinks=True)
1062
1298
        return ScratchBranch(base=base)
1063
1299
        
1064
1300
    def __del__(self):
1066
1302
 
1067
1303
    def destroy(self):
1068
1304
        """Destroy the test branch, removing the scratch directory."""
 
1305
        from shutil import rmtree
1069
1306
        try:
1070
1307
            if self.base:
1071
1308
                mutter("delete ScratchBranch %s" % self.base)
1072
 
                shutil.rmtree(self.base)
 
1309
                rmtree(self.base)
1073
1310
        except OSError, e:
1074
1311
            # Work around for shutil.rmtree failing on Windows when
1075
1312
            # readonly files are encountered
1077
1314
            for root, dirs, files in os.walk(self.base, topdown=False):
1078
1315
                for name in files:
1079
1316
                    os.chmod(os.path.join(root, name), 0700)
1080
 
            shutil.rmtree(self.base)
 
1317
            rmtree(self.base)
1081
1318
        self.base = None
1082
1319
 
1083
1320
    
1108
1345
    cope with just randomness because running uuidgen every time is
1109
1346
    slow."""
1110
1347
    import re
 
1348
    from binascii import hexlify
 
1349
    from time import time
1111
1350
 
1112
1351
    # get last component
1113
1352
    idx = name.rfind('/')
1125
1364
    name = re.sub(r'[^\w.]', '', name)
1126
1365
 
1127
1366
    s = hexlify(rand_bytes(8))
1128
 
    return '-'.join((name, compact_date(time.time()), s))
 
1367
    return '-'.join((name, compact_date(time()), s))
 
1368
 
 
1369
 
 
1370
def gen_root_id():
 
1371
    """Return a new tree-root file id."""
 
1372
    return gen_file_id('TREE_ROOT')
 
1373