~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-06-27 05:14:15 UTC
  • Revision ID: mbp@sourcefrog.net-20050627051415-0f1f3bded0761fe8
- stub shell plugin to check permissions

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
 
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
19
21
 
20
22
import bzrlib
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
 
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
25
34
 
26
35
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
27
36
## TODO: Maybe include checks for common corruption of newlines, etc?
150
159
    _lock_count = None
151
160
    _lock = None
152
161
    
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
 
 
158
162
    def __init__(self, base, init=False, find_root=True):
159
163
        """Create new branch object at a particular location.
160
164
 
170
174
        In the test suite, creation of new trees is tested using the
171
175
        `ScratchBranch` class.
172
176
        """
173
 
        from bzrlib.store import ImmutableStore
174
177
        if init:
175
178
            self.base = os.path.realpath(base)
176
179
            self._make_control()
262
265
 
263
266
    def controlfilename(self, file_or_path):
264
267
        """Return location relative to branch."""
265
 
        if isinstance(file_or_path, basestring):
 
268
        if isinstance(file_or_path, types.StringTypes):
266
269
            file_or_path = [file_or_path]
267
270
        return os.path.join(self.base, bzrlib.BZRDIR, *file_or_path)
268
271
 
295
298
 
296
299
 
297
300
    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',
311
 
                  'pending-merges'):
 
310
                  'branch-lock'):
312
311
            self.controlfile(f, 'w').write('')
313
312
        mutter('created control directory in ' + self.base)
314
 
 
315
 
        pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
 
313
        Inventory().write_xml(self.controlfile('inventory','w'))
316
314
 
317
315
 
318
316
    def _check_format(self):
333
331
                           ['use a different bzr version',
334
332
                            'or remove the .bzr directory and "bzr init" again'])
335
333
 
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
340
334
 
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)
352
335
 
353
336
    def read_working_inventory(self):
354
337
        """Read the working inventory."""
355
 
        from bzrlib.inventory import Inventory
356
 
        from bzrlib.xml import unpack_xml
357
 
        from time import time
358
 
        before = time()
 
338
        before = time.time()
 
339
        # ElementTree does its own conversion from UTF-8, so open in
 
340
        # binary.
359
341
        self.lock_read()
360
342
        try:
361
 
            # ElementTree does its own conversion from UTF-8, so open in
362
 
            # binary.
363
 
            inv = unpack_xml(Inventory,
364
 
                             self.controlfile('inventory', 'rb'))
 
343
            inv = Inventory.read_xml(self.controlfile('inventory', 'rb'))
365
344
            mutter("loaded inventory of %d items in %f"
366
 
                   % (len(inv), time() - before))
 
345
                   % (len(inv), time.time() - before))
367
346
            return inv
368
347
        finally:
369
348
            self.unlock()
375
354
        That is to say, the inventory describing changes underway, that
376
355
        will be committed to the next revision.
377
356
        """
378
 
        from bzrlib.atomicfile import AtomicFile
379
 
        from bzrlib.xml import pack_xml
380
 
        
381
357
        self.lock_write()
382
358
        try:
 
359
            from bzrlib.atomicfile import AtomicFile
 
360
 
383
361
            f = AtomicFile(self.controlfilename('inventory'), 'wb')
384
362
            try:
385
 
                pack_xml(inv, f)
 
363
                inv.write_xml(f)
386
364
                f.commit()
387
365
            finally:
388
366
                f.close()
422
400
              add all non-ignored children.  Perhaps do that in a
423
401
              higher-level method.
424
402
        """
425
 
        from bzrlib.textui import show_status
426
403
        # TODO: Re-adding a file that is removed in the working copy
427
404
        # should probably put it back with the previous ID.
428
 
        if isinstance(files, basestring):
429
 
            assert(ids is None or isinstance(ids, basestring))
 
405
        if isinstance(files, types.StringTypes):
 
406
            assert(ids is None or isinstance(ids, types.StringTypes))
430
407
            files = [files]
431
408
            if ids is not None:
432
409
                ids = [ids]
481
458
            # use inventory as it was in that revision
482
459
            file_id = tree.inventory.path2id(file)
483
460
            if not file_id:
484
 
                raise BzrError("%r is not present in revision %s" % (file, revno))
 
461
                raise BzrError("%r is not present in revision %d" % (file, revno))
485
462
            tree.print_file(file_id)
486
463
        finally:
487
464
            self.unlock()
501
478
        is the opposite of add.  Removing it is consistent with most
502
479
        other tools.  Maybe an option.
503
480
        """
504
 
        from bzrlib.textui import show_status
505
481
        ## TODO: Normalize names
506
482
        ## TODO: Remove nested loops; better scalability
507
 
        if isinstance(files, basestring):
 
483
        if isinstance(files, types.StringTypes):
508
484
            files = [files]
509
485
 
510
486
        self.lock_write()
535
511
 
536
512
    # FIXME: this doesn't need to be a branch method
537
513
    def set_inventory(self, new_inventory_list):
538
 
        from bzrlib.inventory import Inventory, InventoryEntry
539
 
        inv = Inventory(self.get_root_id())
 
514
        inv = Inventory()
540
515
        for path, file_id, parent, kind in new_inventory_list:
541
516
            name = os.path.basename(path)
542
517
            if name == "":
564
539
        return self.working_tree().unknowns()
565
540
 
566
541
 
567
 
    def append_revision(self, *revision_ids):
 
542
    def append_revision(self, revision_id):
568
543
        from bzrlib.atomicfile import AtomicFile
569
544
 
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)
 
545
        mutter("add {%s} to revision-history" % revision_id)
 
546
        rev_history = self.revision_history() + [revision_id]
575
547
 
576
548
        f = AtomicFile(self.controlfilename('revision-history'))
577
549
        try:
584
556
 
585
557
    def get_revision(self, revision_id):
586
558
        """Return the Revision object for a named revision"""
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
 
            
 
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])
598
562
        assert r.revision_id == revision_id
599
563
        return r
600
 
        
601
564
 
602
565
    def get_revision_sha1(self, revision_id):
603
566
        """Hash the stored value of a revision, and return it."""
616
579
        TODO: Perhaps for this and similar methods, take a revision
617
580
               parameter which can be either an integer revno or a
618
581
               string hash."""
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
 
            
 
582
        i = Inventory.read_xml(self.inventory_store[inventory_id])
 
583
        return i
624
584
 
625
585
    def get_inventory_sha1(self, inventory_id):
626
586
        """Return the sha1 hash of the inventory entry
630
590
 
631
591
    def get_revision_inventory(self, revision_id):
632
592
        """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.
635
593
        if revision_id == None:
636
 
            from bzrlib.inventory import Inventory
637
 
            return Inventory(self.get_root_id())
 
594
            return Inventory()
638
595
        else:
639
 
            return self.get_inventory(revision_id)
 
596
            return self.get_inventory(self.get_revision(revision_id).inventory_id)
640
597
 
641
598
 
642
599
    def revision_history(self):
820
777
            other.inventory_store.prefetch(inventory_ids)
821
778
                
822
779
        revisions = []
823
 
        needed_texts = set()
 
780
        needed_texts = sets.Set()
824
781
        i = 0
825
782
        for rev_id in revision_ids:
826
783
            i += 1
855
812
        commit(self, *args, **kw)
856
813
        
857
814
 
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
 
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
 
1015
826
 
1016
827
    def revision_tree(self, revision_id):
1017
828
        """Return Tree for a revision on this branch.
1018
829
 
1019
830
        `revision_id` may be None for the null revision, in which case
1020
831
        an `EmptyTree` is returned."""
1021
 
        from bzrlib.tree import EmptyTree, RevisionTree
1022
832
        # TODO: refactor this to use an existing revision object
1023
833
        # so we don't need to read it in twice.
1024
834
        if revision_id == None:
1025
 
            return EmptyTree(self.get_root_id())
 
835
            return EmptyTree()
1026
836
        else:
1027
837
            inv = self.get_revision_inventory(revision_id)
1028
838
            return RevisionTree(self.text_store, inv)
1039
849
 
1040
850
        If there are no revisions yet, return an `EmptyTree`.
1041
851
        """
1042
 
        from bzrlib.tree import EmptyTree, RevisionTree
1043
852
        r = self.last_patch()
1044
853
        if r == None:
1045
 
            return EmptyTree(self.get_root_id())
 
854
            return EmptyTree()
1046
855
        else:
1047
856
            return RevisionTree(self.text_store, self.get_revision_inventory(r))
1048
857
 
1207
1016
                f.close()
1208
1017
 
1209
1018
 
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
 
 
1252
1019
 
1253
1020
class ScratchBranch(Branch):
1254
1021
    """Special test class: a branch that cleans up after itself.
1268
1035
 
1269
1036
        If any files are listed, they are created in the working copy.
1270
1037
        """
1271
 
        from tempfile import mkdtemp
1272
1038
        init = False
1273
1039
        if base is None:
1274
 
            base = mkdtemp()
 
1040
            base = tempfile.mkdtemp()
1275
1041
            init = True
1276
1042
        Branch.__init__(self, base, init=init)
1277
1043
        for d in dirs:
1290
1056
        >>> os.path.isfile(os.path.join(clone.base, "file1"))
1291
1057
        True
1292
1058
        """
1293
 
        from shutil import copytree
1294
 
        from tempfile import mkdtemp
1295
 
        base = mkdtemp()
 
1059
        base = tempfile.mkdtemp()
1296
1060
        os.rmdir(base)
1297
 
        copytree(self.base, base, symlinks=True)
 
1061
        shutil.copytree(self.base, base, symlinks=True)
1298
1062
        return ScratchBranch(base=base)
1299
1063
        
1300
1064
    def __del__(self):
1302
1066
 
1303
1067
    def destroy(self):
1304
1068
        """Destroy the test branch, removing the scratch directory."""
1305
 
        from shutil import rmtree
1306
1069
        try:
1307
1070
            if self.base:
1308
1071
                mutter("delete ScratchBranch %s" % self.base)
1309
 
                rmtree(self.base)
 
1072
                shutil.rmtree(self.base)
1310
1073
        except OSError, e:
1311
1074
            # Work around for shutil.rmtree failing on Windows when
1312
1075
            # readonly files are encountered
1314
1077
            for root, dirs, files in os.walk(self.base, topdown=False):
1315
1078
                for name in files:
1316
1079
                    os.chmod(os.path.join(root, name), 0700)
1317
 
            rmtree(self.base)
 
1080
            shutil.rmtree(self.base)
1318
1081
        self.base = None
1319
1082
 
1320
1083
    
1345
1108
    cope with just randomness because running uuidgen every time is
1346
1109
    slow."""
1347
1110
    import re
1348
 
    from binascii import hexlify
1349
 
    from time import time
1350
1111
 
1351
1112
    # get last component
1352
1113
    idx = name.rfind('/')
1364
1125
    name = re.sub(r'[^\w.]', '', name)
1365
1126
 
1366
1127
    s = hexlify(rand_bytes(8))
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
 
 
 
1128
    return '-'.join((name, compact_date(time.time()), s))