~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 00:18:14 UTC
  • Revision ID: mbp@sourcefrog.net-20050723001814-03baa015c59253f6
- Add less-sucky is_within_any

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?
45
36
        return Branch(f, **args)
46
37
 
47
38
 
 
39
def find_cached_branch(f, cache_root, **args):
 
40
    from remotebranch import RemoteBranch
 
41
    br = find_branch(f, **args)
 
42
    def cacheify(br, store_name):
 
43
        from meta_store import CachedStore
 
44
        cache_path = os.path.join(cache_root, store_name)
 
45
        os.mkdir(cache_path)
 
46
        new_store = CachedStore(getattr(br, store_name), cache_path)
 
47
        setattr(br, store_name, new_store)
 
48
 
 
49
    if isinstance(br, RemoteBranch):
 
50
        cacheify(br, 'inventory_store')
 
51
        cacheify(br, 'text_store')
 
52
        cacheify(br, 'revision_store')
 
53
    return br
 
54
 
48
55
 
49
56
def _relpath(base, path):
50
57
    """Return path relative to base, or raise exception.
143
150
    _lock_count = None
144
151
    _lock = None
145
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
 
146
158
    def __init__(self, base, init=False, find_root=True):
147
159
        """Create new branch object at a particular location.
148
160
 
158
170
        In the test suite, creation of new trees is tested using the
159
171
        `ScratchBranch` class.
160
172
        """
 
173
        from bzrlib.store import ImmutableStore
161
174
        if init:
162
175
            self.base = os.path.realpath(base)
163
176
            self._make_control()
249
262
 
250
263
    def controlfilename(self, file_or_path):
251
264
        """Return location relative to branch."""
252
 
        if isinstance(file_or_path, types.StringTypes):
 
265
        if isinstance(file_or_path, basestring):
253
266
            file_or_path = [file_or_path]
254
267
        return os.path.join(self.base, bzrlib.BZRDIR, *file_or_path)
255
268
 
282
295
 
283
296
 
284
297
    def _make_control(self):
 
298
        from bzrlib.inventory import Inventory
 
299
        from bzrlib.xml import pack_xml
 
300
        
285
301
        os.mkdir(self.controlfilename([]))
286
302
        self.controlfile('README', 'w').write(
287
303
            "This is a Bazaar-NG control directory.\n"
291
307
            os.mkdir(self.controlfilename(d))
292
308
        for f in ('revision-history', 'merged-patches',
293
309
                  'pending-merged-patches', 'branch-name',
294
 
                  'branch-lock'):
 
310
                  'branch-lock',
 
311
                  'pending-merges'):
295
312
            self.controlfile(f, 'w').write('')
296
313
        mutter('created control directory in ' + self.base)
297
 
        Inventory().write_xml(self.controlfile('inventory','w'))
 
314
 
 
315
        pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
298
316
 
299
317
 
300
318
    def _check_format(self):
315
333
                           ['use a different bzr version',
316
334
                            'or remove the .bzr directory and "bzr init" again'])
317
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
318
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)
319
352
 
320
353
    def read_working_inventory(self):
321
354
        """Read the working inventory."""
322
 
        before = time.time()
323
 
        # ElementTree does its own conversion from UTF-8, so open in
324
 
        # binary.
 
355
        from bzrlib.inventory import Inventory
 
356
        from bzrlib.xml import unpack_xml
 
357
        from time import time
 
358
        before = time()
325
359
        self.lock_read()
326
360
        try:
327
 
            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'))
328
365
            mutter("loaded inventory of %d items in %f"
329
 
                   % (len(inv), time.time() - before))
 
366
                   % (len(inv), time() - before))
330
367
            return inv
331
368
        finally:
332
369
            self.unlock()
338
375
        That is to say, the inventory describing changes underway, that
339
376
        will be committed to the next revision.
340
377
        """
341
 
        ## TODO: factor out to atomicfile?  is rename safe on windows?
342
 
        ## TODO: Maybe some kind of clean/dirty marker on inventory?
343
 
        tmpfname = self.controlfilename('inventory.tmp')
344
 
        tmpf = file(tmpfname, 'wb')
345
 
        inv.write_xml(tmpf)
346
 
        tmpf.close()
347
 
        inv_fname = self.controlfilename('inventory')
348
 
        if sys.platform == 'win32':
349
 
            os.remove(inv_fname)
350
 
        os.rename(tmpfname, inv_fname)
 
378
        from bzrlib.atomicfile import AtomicFile
 
379
        from bzrlib.xml import pack_xml
 
380
        
 
381
        self.lock_write()
 
382
        try:
 
383
            f = AtomicFile(self.controlfilename('inventory'), 'wb')
 
384
            try:
 
385
                pack_xml(inv, f)
 
386
                f.commit()
 
387
            finally:
 
388
                f.close()
 
389
        finally:
 
390
            self.unlock()
 
391
        
351
392
        mutter('wrote working inventory')
352
393
            
353
394
 
381
422
              add all non-ignored children.  Perhaps do that in a
382
423
              higher-level method.
383
424
        """
 
425
        from bzrlib.textui import show_status
384
426
        # TODO: Re-adding a file that is removed in the working copy
385
427
        # should probably put it back with the previous ID.
386
 
        if isinstance(files, types.StringTypes):
387
 
            assert(ids is None or isinstance(ids, types.StringTypes))
 
428
        if isinstance(files, basestring):
 
429
            assert(ids is None or isinstance(ids, basestring))
388
430
            files = [files]
389
431
            if ids is not None:
390
432
                ids = [ids]
422
464
                inv.add_path(f, kind=kind, file_id=file_id)
423
465
 
424
466
                if verbose:
425
 
                    show_status('A', kind, quotefn(f))
 
467
                    print 'added', quotefn(f)
426
468
 
427
469
                mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
428
470
 
439
481
            # use inventory as it was in that revision
440
482
            file_id = tree.inventory.path2id(file)
441
483
            if not file_id:
442
 
                raise BzrError("%r is not present in revision %d" % (file, revno))
 
484
                raise BzrError("%r is not present in revision %s" % (file, revno))
443
485
            tree.print_file(file_id)
444
486
        finally:
445
487
            self.unlock()
459
501
        is the opposite of add.  Removing it is consistent with most
460
502
        other tools.  Maybe an option.
461
503
        """
 
504
        from bzrlib.textui import show_status
462
505
        ## TODO: Normalize names
463
506
        ## TODO: Remove nested loops; better scalability
464
 
        if isinstance(files, types.StringTypes):
 
507
        if isinstance(files, basestring):
465
508
            files = [files]
466
509
 
467
510
        self.lock_write()
492
535
 
493
536
    # FIXME: this doesn't need to be a branch method
494
537
    def set_inventory(self, new_inventory_list):
495
 
        inv = Inventory()
 
538
        from bzrlib.inventory import Inventory, InventoryEntry
 
539
        inv = Inventory(self.get_root_id())
496
540
        for path, file_id, parent, kind in new_inventory_list:
497
541
            name = os.path.basename(path)
498
542
            if name == "":
520
564
        return self.working_tree().unknowns()
521
565
 
522
566
 
523
 
    def append_revision(self, revision_id):
524
 
        mutter("add {%s} to revision-history" % revision_id)
 
567
    def append_revision(self, *revision_ids):
 
568
        from bzrlib.atomicfile import AtomicFile
 
569
 
 
570
        for revision_id in revision_ids:
 
571
            mutter("add {%s} to revision-history" % revision_id)
 
572
 
525
573
        rev_history = self.revision_history()
526
 
 
527
 
        tmprhname = self.controlfilename('revision-history.tmp')
528
 
        rhname = self.controlfilename('revision-history')
529
 
        
530
 
        f = file(tmprhname, 'wt')
531
 
        rev_history.append(revision_id)
532
 
        f.write('\n'.join(rev_history))
533
 
        f.write('\n')
534
 
        f.close()
535
 
 
536
 
        if sys.platform == 'win32':
537
 
            os.remove(rhname)
538
 
        os.rename(tmprhname, rhname)
539
 
        
 
574
        rev_history.extend(revision_ids)
 
575
 
 
576
        f = AtomicFile(self.controlfilename('revision-history'))
 
577
        try:
 
578
            for rev_id in rev_history:
 
579
                print >>f, rev_id
 
580
            f.commit()
 
581
        finally:
 
582
            f.close()
540
583
 
541
584
 
542
585
    def get_revision(self, revision_id):
543
586
        """Return the Revision object for a named revision"""
544
 
        if not revision_id or not isinstance(revision_id, basestring):
545
 
            raise ValueError('invalid revision-id: %r' % revision_id)
546
 
        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
            
547
598
        assert r.revision_id == revision_id
548
599
        return r
 
600
        
549
601
 
550
602
    def get_revision_sha1(self, revision_id):
551
603
        """Hash the stored value of a revision, and return it."""
564
616
        TODO: Perhaps for this and similar methods, take a revision
565
617
               parameter which can be either an integer revno or a
566
618
               string hash."""
567
 
        i = Inventory.read_xml(self.inventory_store[inventory_id])
568
 
        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
            
569
624
 
570
625
    def get_inventory_sha1(self, inventory_id):
571
626
        """Return the sha1 hash of the inventory entry
575
630
 
576
631
    def get_revision_inventory(self, revision_id):
577
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.
578
635
        if revision_id == None:
579
 
            return Inventory()
 
636
            from bzrlib.inventory import Inventory
 
637
            return Inventory(self.get_root_id())
580
638
        else:
581
 
            return self.get_inventory(self.get_revision(revision_id).inventory_id)
 
639
            return self.get_inventory(revision_id)
582
640
 
583
641
 
584
642
    def revision_history(self):
753
811
 
754
812
        pb.update('comparing histories')
755
813
        revision_ids = self.missing_revisions(other, stop_revision)
 
814
 
 
815
        if hasattr(other.revision_store, "prefetch"):
 
816
            other.revision_store.prefetch(revision_ids)
 
817
        if hasattr(other.inventory_store, "prefetch"):
 
818
            inventory_ids = [other.get_revision(r).inventory_id
 
819
                             for r in revision_ids]
 
820
            other.inventory_store.prefetch(inventory_ids)
 
821
                
756
822
        revisions = []
757
 
        needed_texts = sets.Set()
 
823
        needed_texts = set()
758
824
        i = 0
759
825
        for rev_id in revision_ids:
760
826
            i += 1
785
851
                    
786
852
        
787
853
    def commit(self, *args, **kw):
788
 
        """Deprecated"""
789
854
        from bzrlib.commit import commit
790
855
        commit(self, *args, **kw)
791
856
        
792
857
 
793
 
    def lookup_revision(self, revno):
794
 
        """Return revision hash for revision number."""
795
 
        if revno == 0:
796
 
            return None
797
 
 
798
 
        try:
799
 
            # list is 0-based; revisions are 1-based
800
 
            return self.revision_history()[revno-1]
801
 
        except IndexError:
802
 
            raise BzrError("no such revision %s" % revno)
803
 
 
 
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
804
1015
 
805
1016
    def revision_tree(self, revision_id):
806
1017
        """Return Tree for a revision on this branch.
807
1018
 
808
1019
        `revision_id` may be None for the null revision, in which case
809
1020
        an `EmptyTree` is returned."""
 
1021
        from bzrlib.tree import EmptyTree, RevisionTree
810
1022
        # TODO: refactor this to use an existing revision object
811
1023
        # so we don't need to read it in twice.
812
1024
        if revision_id == None:
813
 
            return EmptyTree()
 
1025
            return EmptyTree(self.get_root_id())
814
1026
        else:
815
1027
            inv = self.get_revision_inventory(revision_id)
816
1028
            return RevisionTree(self.text_store, inv)
827
1039
 
828
1040
        If there are no revisions yet, return an `EmptyTree`.
829
1041
        """
 
1042
        from bzrlib.tree import EmptyTree, RevisionTree
830
1043
        r = self.last_patch()
831
1044
        if r == None:
832
 
            return EmptyTree()
 
1045
            return EmptyTree(self.get_root_id())
833
1046
        else:
834
1047
            return RevisionTree(self.text_store, self.get_revision_inventory(r))
835
1048
 
950
1163
            self.unlock()
951
1164
 
952
1165
 
 
1166
    def revert(self, filenames, old_tree=None, backups=True):
 
1167
        """Restore selected files to the versions from a previous tree.
 
1168
 
 
1169
        backups
 
1170
            If true (default) backups are made of files before
 
1171
            they're renamed.
 
1172
        """
 
1173
        from bzrlib.errors import NotVersionedError, BzrError
 
1174
        from bzrlib.atomicfile import AtomicFile
 
1175
        from bzrlib.osutils import backup_file
 
1176
        
 
1177
        inv = self.read_working_inventory()
 
1178
        if old_tree is None:
 
1179
            old_tree = self.basis_tree()
 
1180
        old_inv = old_tree.inventory
 
1181
 
 
1182
        nids = []
 
1183
        for fn in filenames:
 
1184
            file_id = inv.path2id(fn)
 
1185
            if not file_id:
 
1186
                raise NotVersionedError("not a versioned file", fn)
 
1187
            if not old_inv.has_id(file_id):
 
1188
                raise BzrError("file not present in old tree", fn, file_id)
 
1189
            nids.append((fn, file_id))
 
1190
            
 
1191
        # TODO: Rename back if it was previously at a different location
 
1192
 
 
1193
        # TODO: If given a directory, restore the entire contents from
 
1194
        # the previous version.
 
1195
 
 
1196
        # TODO: Make a backup to a temporary file.
 
1197
 
 
1198
        # TODO: If the file previously didn't exist, delete it?
 
1199
        for fn, file_id in nids:
 
1200
            backup_file(fn)
 
1201
            
 
1202
            f = AtomicFile(fn, 'wb')
 
1203
            try:
 
1204
                f.write(old_tree.get_file(file_id).read())
 
1205
                f.commit()
 
1206
            finally:
 
1207
                f.close()
 
1208
 
 
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
 
953
1252
 
954
1253
class ScratchBranch(Branch):
955
1254
    """Special test class: a branch that cleans up after itself.
969
1268
 
970
1269
        If any files are listed, they are created in the working copy.
971
1270
        """
 
1271
        from tempfile import mkdtemp
972
1272
        init = False
973
1273
        if base is None:
974
 
            base = tempfile.mkdtemp()
 
1274
            base = mkdtemp()
975
1275
            init = True
976
1276
        Branch.__init__(self, base, init=init)
977
1277
        for d in dirs:
990
1290
        >>> os.path.isfile(os.path.join(clone.base, "file1"))
991
1291
        True
992
1292
        """
993
 
        base = tempfile.mkdtemp()
 
1293
        from shutil import copytree
 
1294
        from tempfile import mkdtemp
 
1295
        base = mkdtemp()
994
1296
        os.rmdir(base)
995
 
        shutil.copytree(self.base, base, symlinks=True)
 
1297
        copytree(self.base, base, symlinks=True)
996
1298
        return ScratchBranch(base=base)
997
1299
        
998
1300
    def __del__(self):
1000
1302
 
1001
1303
    def destroy(self):
1002
1304
        """Destroy the test branch, removing the scratch directory."""
 
1305
        from shutil import rmtree
1003
1306
        try:
1004
1307
            if self.base:
1005
1308
                mutter("delete ScratchBranch %s" % self.base)
1006
 
                shutil.rmtree(self.base)
 
1309
                rmtree(self.base)
1007
1310
        except OSError, e:
1008
1311
            # Work around for shutil.rmtree failing on Windows when
1009
1312
            # readonly files are encountered
1011
1314
            for root, dirs, files in os.walk(self.base, topdown=False):
1012
1315
                for name in files:
1013
1316
                    os.chmod(os.path.join(root, name), 0700)
1014
 
            shutil.rmtree(self.base)
 
1317
            rmtree(self.base)
1015
1318
        self.base = None
1016
1319
 
1017
1320
    
1042
1345
    cope with just randomness because running uuidgen every time is
1043
1346
    slow."""
1044
1347
    import re
 
1348
    from binascii import hexlify
 
1349
    from time import time
1045
1350
 
1046
1351
    # get last component
1047
1352
    idx = name.rfind('/')
1059
1364
    name = re.sub(r'[^\w.]', '', name)
1060
1365
 
1061
1366
    s = hexlify(rand_bytes(8))
1062
 
    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