~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-04-29 00:43:34 UTC
  • Revision ID: mbp@sourcefrog.net-20050429004334-bbb9dc81ce0d9de3
- update install instructions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
# -*- coding: UTF-8 -*-
 
1
# Copyright (C) 2005 Canonical Ltd
3
2
 
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
29
28
from inventory import InventoryEntry, Inventory
30
29
from osutils import isdir, quotefn, isfile, uuid, sha_file, username, chomp, \
31
30
     format_date, compact_date, pumpfile, user_email, rand_bytes, splitpath, \
32
 
     joinpath, sha_string, file_kind, local_time_offset
 
31
     joinpath, sha_string, file_kind, local_time_offset, appendpath
33
32
from store import ImmutableStore
34
33
from revision import Revision
35
 
from errors import bailout
 
34
from errors import bailout, BzrError
36
35
from textui import show_status
37
36
from diff import diff_trees
38
37
 
41
40
 
42
41
 
43
42
 
 
43
def find_branch_root(f=None):
 
44
    """Find the branch root enclosing f, or pwd.
 
45
 
 
46
    It is not necessary that f exists.
 
47
 
 
48
    Basically we keep looking up until we find the control directory or
 
49
    run into the root."""
 
50
    if f == None:
 
51
        f = os.getcwd()
 
52
    elif hasattr(os.path, 'realpath'):
 
53
        f = os.path.realpath(f)
 
54
    else:
 
55
        f = os.path.abspath(f)
 
56
 
 
57
    orig_f = f
 
58
 
 
59
    while True:
 
60
        if os.path.exists(os.path.join(f, bzrlib.BZRDIR)):
 
61
            return f
 
62
        head, tail = os.path.split(f)
 
63
        if head == f:
 
64
            # reached the root, whatever that may be
 
65
            raise BzrError('%r is not in a branch' % orig_f)
 
66
        f = head
 
67
    
44
68
 
45
69
 
46
70
######################################################################
49
73
class Branch:
50
74
    """Branch holding a history of revisions.
51
75
 
52
 
    :todo: Perhaps use different stores for different classes of object,
 
76
    TODO: Perhaps use different stores for different classes of object,
53
77
           so that we can keep track of how much space each one uses,
54
78
           or garbage-collect them.
55
79
 
56
 
    :todo: Add a RemoteBranch subclass.  For the basic case of read-only
 
80
    TODO: Add a RemoteBranch subclass.  For the basic case of read-only
57
81
           HTTP access this should be very easy by, 
58
82
           just redirecting controlfile access into HTTP requests.
59
83
           We would need a RemoteStore working similarly.
60
84
 
61
 
    :todo: Keep the on-disk branch locked while the object exists.
 
85
    TODO: Keep the on-disk branch locked while the object exists.
62
86
 
63
 
    :todo: mkdir() method.
 
87
    TODO: mkdir() method.
64
88
    """
65
 
    def __init__(self, base, init=False):
 
89
    def __init__(self, base, init=False, find_root=True):
66
90
        """Create new branch object at a particular location.
67
91
 
68
 
        :param base: Base directory for the branch.
69
 
 
70
 
        :param init: If True, create new control files in a previously
 
92
        base -- Base directory for the branch.
 
93
        
 
94
        init -- If True, create new control files in a previously
71
95
             unversioned directory.  If False, the branch must already
72
96
             be versioned.
73
97
 
 
98
        find_root -- If true and init is false, find the root of the
 
99
             existing branch containing base.
 
100
 
74
101
        In the test suite, creation of new trees is tested using the
75
102
        `ScratchBranch` class.
76
103
        """
77
 
        self.base = os.path.realpath(base)
78
104
        if init:
 
105
            self.base = os.path.realpath(base)
79
106
            self._make_control()
 
107
        elif find_root:
 
108
            self.base = find_branch_root(base)
80
109
        else:
 
110
            self.base = os.path.realpath(base)
81
111
            if not isdir(self.controlfilename('.')):
82
112
                bailout("not a bzr branch: %s" % quotefn(base),
83
113
                        ['use "bzr init" to initialize a new working tree',
84
114
                         'current bzr can only operate from top-of-tree'])
85
 
            self._check_format()
 
115
        self._check_format()
86
116
 
87
117
        self.text_store = ImmutableStore(self.controlfilename('text-store'))
88
118
        self.revision_store = ImmutableStore(self.controlfilename('revision-store'))
96
126
    __repr__ = __str__
97
127
 
98
128
 
99
 
    def _rel(self, name):
100
 
        """Return filename relative to branch top"""
 
129
    def abspath(self, name):
 
130
        """Return absolute filename for something in the branch"""
101
131
        return os.path.join(self.base, name)
102
 
        
 
132
 
 
133
 
 
134
    def relpath(self, path):
 
135
        """Return path relative to this branch of something inside it.
 
136
 
 
137
        Raises an error if path is not in this branch."""
 
138
        rp = os.path.realpath(path)
 
139
        # FIXME: windows
 
140
        if not rp.startswith(self.base):
 
141
            bailout("path %r is not within branch %r" % (rp, self.base))
 
142
        rp = rp[len(self.base):]
 
143
        rp = rp.lstrip(os.sep)
 
144
        return rp
 
145
 
103
146
 
104
147
    def controlfilename(self, file_or_path):
105
148
        """Return location relative to branch."""
109
152
 
110
153
 
111
154
    def controlfile(self, file_or_path, mode='r'):
112
 
        """Open a control file for this branch"""
113
 
        return file(self.controlfilename(file_or_path), mode)
 
155
        """Open a control file for this branch.
 
156
 
 
157
        There are two classes of file in the control directory: text
 
158
        and binary.  binary files are untranslated byte streams.  Text
 
159
        control files are stored with Unix newlines and in UTF-8, even
 
160
        if the platform or locale defaults are different.
 
161
        """
 
162
 
 
163
        fn = self.controlfilename(file_or_path)
 
164
 
 
165
        if mode == 'rb' or mode == 'wb':
 
166
            return file(fn, mode)
 
167
        elif mode == 'r' or mode == 'w':
 
168
            # open in binary mode anyhow so there's no newline translation;
 
169
            # codecs uses line buffering by default; don't want that.
 
170
            import codecs
 
171
            return codecs.open(fn, mode + 'b', 'utf-8',
 
172
                               buffering=60000)
 
173
        else:
 
174
            raise BzrError("invalid controlfile mode %r" % mode)
 
175
 
114
176
 
115
177
 
116
178
    def _make_control(self):
135
197
 
136
198
        In the future, we might need different in-memory Branch
137
199
        classes to support downlevel branches.  But not yet.
138
 
        """        
139
 
        # read in binary mode to detect newline wierdness.
140
 
        fmt = self.controlfile('branch-format', 'rb').read()
 
200
        """
 
201
        # This ignores newlines so that we can open branches created
 
202
        # on Windows from Linux and so on.  I think it might be better
 
203
        # to always make all internal files in unix format.
 
204
        fmt = self.controlfile('branch-format', 'r').read()
 
205
        fmt.replace('\r\n', '')
141
206
        if fmt != BZR_BRANCH_FORMAT:
142
207
            bailout('sorry, branch format %r not supported' % fmt,
143
208
                    ['use a different bzr version',
147
212
    def read_working_inventory(self):
148
213
        """Read the working inventory."""
149
214
        before = time.time()
150
 
        inv = Inventory.read_xml(self.controlfile('inventory', 'r'))
 
215
        # ElementTree does its own conversion from UTF-8, so open in
 
216
        # binary.
 
217
        inv = Inventory.read_xml(self.controlfile('inventory', 'rb'))
151
218
        mutter("loaded inventory of %d items in %f"
152
219
               % (len(inv), time.time() - before))
153
220
        return inv
160
227
        will be committed to the next revision.
161
228
        """
162
229
        ## TODO: factor out to atomicfile?  is rename safe on windows?
 
230
        ## TODO: Maybe some kind of clean/dirty marker on inventory?
163
231
        tmpfname = self.controlfilename('inventory.tmp')
164
 
        tmpf = file(tmpfname, 'w')
 
232
        tmpf = file(tmpfname, 'wb')
165
233
        inv.write_xml(tmpf)
166
234
        tmpf.close()
167
 
        os.rename(tmpfname, self.controlfilename('inventory'))
 
235
        inv_fname = self.controlfilename('inventory')
 
236
        if sys.platform == 'win32':
 
237
            os.remove(inv_fname)
 
238
        os.rename(tmpfname, inv_fname)
168
239
        mutter('wrote working inventory')
169
240
 
170
241
 
175
246
    def add(self, files, verbose=False):
176
247
        """Make files versioned.
177
248
 
 
249
        Note that the command line normally calls smart_add instead.
 
250
 
178
251
        This puts the files in the Added state, so that they will be
179
252
        recorded by the next commit.
180
253
 
181
 
        :todo: Perhaps have an option to add the ids even if the files do
 
254
        TODO: Perhaps have an option to add the ids even if the files do
182
255
               not (yet) exist.
183
256
 
184
 
        :todo: Perhaps return the ids of the files?  But then again it
 
257
        TODO: Perhaps return the ids of the files?  But then again it
185
258
               is easy to retrieve them if they're needed.
186
259
 
187
 
        :todo: Option to specify file id.
 
260
        TODO: Option to specify file id.
188
261
 
189
 
        :todo: Adding a directory should optionally recurse down and
 
262
        TODO: Adding a directory should optionally recurse down and
190
263
               add all non-ignored children.  Perhaps do that in a
191
264
               higher-level method.
192
265
 
228
301
            if len(fp) == 0:
229
302
                bailout("cannot add top-level %r" % f)
230
303
                
231
 
            fullpath = os.path.normpath(self._rel(f))
232
 
 
233
 
            if isfile(fullpath):
234
 
                kind = 'file'
235
 
            elif isdir(fullpath):
236
 
                kind = 'directory'
237
 
            else:
238
 
                bailout('cannot add: not a regular file or directory: %s' % quotefn(f))
239
 
 
240
 
            if len(fp) > 1:
241
 
                parent_name = joinpath(fp[:-1])
242
 
                mutter("lookup parent %r" % parent_name)
243
 
                parent_id = inv.path2id(parent_name)
244
 
                if parent_id == None:
245
 
                    bailout("cannot add: parent %r is not versioned"
246
 
                            % joinpath(fp[:-1]))
247
 
            else:
248
 
                parent_id = None
249
 
 
250
 
            file_id = _gen_file_id(fp[-1])
251
 
            inv.add(InventoryEntry(file_id, fp[-1], kind=kind, parent_id=parent_id))
 
304
            fullpath = os.path.normpath(self.abspath(f))
 
305
 
 
306
            try:
 
307
                kind = file_kind(fullpath)
 
308
            except OSError:
 
309
                # maybe something better?
 
310
                bailout('cannot add: not a regular file or directory: %s' % quotefn(f))
 
311
            
 
312
            if kind != 'file' and kind != 'directory':
 
313
                bailout('cannot add: not a regular file or directory: %s' % quotefn(f))
 
314
 
 
315
            file_id = gen_file_id(f)
 
316
            inv.add_path(f, kind=kind, file_id=file_id)
 
317
 
252
318
            if verbose:
253
319
                show_status('A', kind, quotefn(f))
254
320
                
255
 
            mutter("add file %s file_id:{%s} kind=%r parent_id={%s}"
256
 
                   % (f, file_id, kind, parent_id))
 
321
            mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
 
322
            
257
323
        self._write_inventory(inv)
258
324
 
259
325
 
 
326
    def print_file(self, file, revno):
 
327
        """Print `file` to stdout."""
 
328
        tree = self.revision_tree(self.lookup_revision(revno))
 
329
        # use inventory as it was in that revision
 
330
        file_id = tree.inventory.path2id(file)
 
331
        if not file_id:
 
332
            bailout("%r is not present in revision %d" % (file, revno))
 
333
        tree.print_file(file_id)
 
334
        
260
335
 
261
336
    def remove(self, files, verbose=False):
262
337
        """Mark nominated files for removal from the inventory.
263
338
 
264
339
        This does not remove their text.  This does not run on 
265
340
 
266
 
        :todo: Refuse to remove modified files unless --force is given?
 
341
        TODO: Refuse to remove modified files unless --force is given?
267
342
 
268
343
        >>> b = ScratchBranch(files=['foo'])
269
344
        >>> b.add('foo')
287
362
        >>> b.working_tree().has_filename('foo') 
288
363
        True
289
364
 
290
 
        :todo: Do something useful with directories.
 
365
        TODO: Do something useful with directories.
291
366
 
292
 
        :todo: Should this remove the text or not?  Tough call; not
 
367
        TODO: Should this remove the text or not?  Tough call; not
293
368
        removing may be useful and the user can just use use rm, and
294
369
        is the opposite of add.  Removing it is consistent with most
295
370
        other tools.  Maybe an option.
359
434
        be robust against files disappearing, moving, etc.  So the
360
435
        whole thing is a bit hard.
361
436
 
362
 
        :param timestamp: if not None, seconds-since-epoch for a
 
437
        timestamp -- if not None, seconds-since-epoch for a
363
438
             postdated/predated commit.
364
439
        """
365
440
 
388
463
 
389
464
            entry = entry.copy()
390
465
 
391
 
            p = self._rel(path)
 
466
            p = self.abspath(path)
392
467
            file_id = entry.file_id
393
468
            mutter('commit prep file %s, id %r ' % (p, file_id))
394
469
 
434
509
                           entry.text_id)
435
510
                    
436
511
                else:
437
 
                    entry.text_id = _gen_file_id(entry.name)
 
512
                    entry.text_id = gen_file_id(entry.name)
438
513
                    self.text_store.add(content, entry.text_id)
439
514
                    mutter('    stored with text_id {%s}' % entry.text_id)
440
515
                    if verbose:
442
517
                            state = 'A'
443
518
                        elif (old_ie.name == entry.name
444
519
                              and old_ie.parent_id == entry.parent_id):
 
520
                            state = 'M'
 
521
                        else:
445
522
                            state = 'R'
446
 
                        else:
447
 
                            state = 'M'
448
523
 
449
524
                        show_status(state, entry.kind, quotefn(path))
450
525
 
503
578
        ## TODO: Also calculate and store the inventory SHA1
504
579
        mutter("committing patch r%d" % (self.revno() + 1))
505
580
 
506
 
        mutter("append to revision-history")
507
 
        self.controlfile('revision-history', 'at').write(rev_id + '\n')
508
 
 
509
 
        mutter("done!")
 
581
 
 
582
        self.append_revision(rev_id)
 
583
        
 
584
        if verbose:
 
585
            note("commited r%d" % self.revno())
 
586
 
 
587
 
 
588
    def append_revision(self, revision_id):
 
589
        mutter("add {%s} to revision-history" % revision_id)
 
590
        rev_history = self.revision_history()
 
591
 
 
592
        tmprhname = self.controlfilename('revision-history.tmp')
 
593
        rhname = self.controlfilename('revision-history')
 
594
        
 
595
        f = file(tmprhname, 'wt')
 
596
        rev_history.append(revision_id)
 
597
        f.write('\n'.join(rev_history))
 
598
        f.write('\n')
 
599
        f.close()
 
600
 
 
601
        if sys.platform == 'win32':
 
602
            os.remove(rhname)
 
603
        os.rename(tmprhname, rhname)
 
604
        
510
605
 
511
606
 
512
607
    def get_revision(self, revision_id):
519
614
    def get_inventory(self, inventory_id):
520
615
        """Get Inventory object by hash.
521
616
 
522
 
        :todo: Perhaps for this and similar methods, take a revision
 
617
        TODO: Perhaps for this and similar methods, take a revision
523
618
               parameter which can be either an integer revno or a
524
619
               string hash."""
525
620
        i = Inventory.read_xml(self.inventory_store[inventory_id])
540
635
        >>> ScratchBranch().revision_history()
541
636
        []
542
637
        """
543
 
        return [chomp(l) for l in self.controlfile('revision-history').readlines()]
 
638
        return [chomp(l) for l in self.controlfile('revision-history', 'r').readlines()]
544
639
 
545
640
 
546
641
    def revno(self):
568
663
        ph = self.revision_history()
569
664
        if ph:
570
665
            return ph[-1]
571
 
 
 
666
        else:
 
667
            return None
 
668
        
572
669
 
573
670
    def lookup_revision(self, revno):
574
671
        """Return revision hash for revision number."""
579
676
            # list is 0-based; revisions are 1-based
580
677
            return self.revision_history()[revno-1]
581
678
        except IndexError:
582
 
            bailout("no such revision %s" % revno)
 
679
            raise BzrError("no such revision %s" % revno)
583
680
 
584
681
 
585
682
    def revision_tree(self, revision_id):
623
720
 
624
721
 
625
722
 
626
 
    def write_log(self, show_timezone='original'):
 
723
    def write_log(self, show_timezone='original', verbose=False):
627
724
        """Write out human-readable log of commits to this branch
628
725
 
629
 
        :param utc: If true, show dates in universal time, not local time."""
 
726
        utc -- If true, show dates in universal time, not local time."""
630
727
        ## TODO: Option to choose either original, utc or local timezone
631
728
        revno = 1
632
729
        precursor = None
651
748
                for l in rev.message.split('\n'):
652
749
                    print '  ' + l
653
750
 
 
751
            if verbose == True and precursor != None:
 
752
                print 'changed files:'
 
753
                tree = self.revision_tree(p)
 
754
                prevtree = self.revision_tree(precursor)
 
755
                
 
756
                for file_state, fid, old_name, new_name, kind in \
 
757
                                        diff_trees(prevtree, tree, ):
 
758
                    if file_state == 'A' or file_state == 'M':
 
759
                        show_status(file_state, kind, new_name)
 
760
                    elif file_state == 'D':
 
761
                        show_status(file_state, kind, old_name)
 
762
                    elif file_state == 'R':
 
763
                        show_status(file_state, kind,
 
764
                            old_name + ' => ' + new_name)
 
765
                
654
766
            revno += 1
655
767
            precursor = p
656
768
 
657
769
 
658
 
 
659
 
    def show_status(branch, show_all=False):
 
770
    def rename_one(self, from_rel, to_rel):
 
771
        """Rename one file.
 
772
 
 
773
        This can change the directory or the filename or both.
 
774
         """
 
775
        tree = self.working_tree()
 
776
        inv = tree.inventory
 
777
        if not tree.has_filename(from_rel):
 
778
            bailout("can't rename: old working file %r does not exist" % from_rel)
 
779
        if tree.has_filename(to_rel):
 
780
            bailout("can't rename: new working file %r already exists" % to_rel)
 
781
            
 
782
        file_id = inv.path2id(from_rel)
 
783
        if file_id == None:
 
784
            bailout("can't rename: old name %r is not versioned" % from_rel)
 
785
 
 
786
        if inv.path2id(to_rel):
 
787
            bailout("can't rename: new name %r is already versioned" % to_rel)
 
788
 
 
789
        to_dir, to_tail = os.path.split(to_rel)
 
790
        to_dir_id = inv.path2id(to_dir)
 
791
        if to_dir_id == None and to_dir != '':
 
792
            bailout("can't determine destination directory id for %r" % to_dir)
 
793
 
 
794
        mutter("rename_one:")
 
795
        mutter("  file_id    {%s}" % file_id)
 
796
        mutter("  from_rel   %r" % from_rel)
 
797
        mutter("  to_rel     %r" % to_rel)
 
798
        mutter("  to_dir     %r" % to_dir)
 
799
        mutter("  to_dir_id  {%s}" % to_dir_id)
 
800
            
 
801
        inv.rename(file_id, to_dir_id, to_tail)
 
802
 
 
803
        print "%s => %s" % (from_rel, to_rel)
 
804
        
 
805
        from_abs = self.abspath(from_rel)
 
806
        to_abs = self.abspath(to_rel)
 
807
        try:
 
808
            os.rename(from_abs, to_abs)
 
809
        except OSError, e:
 
810
            bailout("failed to rename %r to %r: %s"
 
811
                    % (from_abs, to_abs, e[1]),
 
812
                    ["rename rolled back"])
 
813
 
 
814
        self._write_inventory(inv)
 
815
            
 
816
 
 
817
 
 
818
    def move(self, from_paths, to_name):
 
819
        """Rename files.
 
820
 
 
821
        to_name must exist as a versioned directory.
 
822
 
 
823
        If to_name exists and is a directory, the files are moved into
 
824
        it, keeping their old names.  If it is a directory, 
 
825
 
 
826
        Note that to_name is only the last component of the new name;
 
827
        this doesn't change the directory.
 
828
        """
 
829
        ## TODO: Option to move IDs only
 
830
        assert not isinstance(from_paths, basestring)
 
831
        tree = self.working_tree()
 
832
        inv = tree.inventory
 
833
        to_abs = self.abspath(to_name)
 
834
        if not isdir(to_abs):
 
835
            bailout("destination %r is not a directory" % to_abs)
 
836
        if not tree.has_filename(to_name):
 
837
            bailout("destination %r not in working directory" % to_abs)
 
838
        to_dir_id = inv.path2id(to_name)
 
839
        if to_dir_id == None and to_name != '':
 
840
            bailout("destination %r is not a versioned directory" % to_name)
 
841
        to_dir_ie = inv[to_dir_id]
 
842
        if to_dir_ie.kind not in ('directory', 'root_directory'):
 
843
            bailout("destination %r is not a directory" % to_abs)
 
844
 
 
845
        to_idpath = Set(inv.get_idpath(to_dir_id))
 
846
 
 
847
        for f in from_paths:
 
848
            if not tree.has_filename(f):
 
849
                bailout("%r does not exist in working tree" % f)
 
850
            f_id = inv.path2id(f)
 
851
            if f_id == None:
 
852
                bailout("%r is not versioned" % f)
 
853
            name_tail = splitpath(f)[-1]
 
854
            dest_path = appendpath(to_name, name_tail)
 
855
            if tree.has_filename(dest_path):
 
856
                bailout("destination %r already exists" % dest_path)
 
857
            if f_id in to_idpath:
 
858
                bailout("can't move %r to a subdirectory of itself" % f)
 
859
 
 
860
        # OK, so there's a race here, it's possible that someone will
 
861
        # create a file in this interval and then the rename might be
 
862
        # left half-done.  But we should have caught most problems.
 
863
 
 
864
        for f in from_paths:
 
865
            name_tail = splitpath(f)[-1]
 
866
            dest_path = appendpath(to_name, name_tail)
 
867
            print "%s => %s" % (f, dest_path)
 
868
            inv.rename(inv.path2id(f), to_dir_id, name_tail)
 
869
            try:
 
870
                os.rename(self.abspath(f), self.abspath(dest_path))
 
871
            except OSError, e:
 
872
                bailout("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
 
873
                        ["rename rolled back"])
 
874
 
 
875
        self._write_inventory(inv)
 
876
 
 
877
 
 
878
 
 
879
    def show_status(self, show_all=False):
660
880
        """Display single-line status for non-ignored working files.
661
881
 
662
882
        The list is show sorted in order by file name.
669
889
        A       foo
670
890
        >>> b.commit("add foo")
671
891
        >>> b.show_status()
672
 
        >>> os.unlink(b._rel('foo'))
 
892
        >>> os.unlink(b.abspath('foo'))
673
893
        >>> b.show_status()
674
894
        D       foo
675
895
        
676
 
 
677
 
        :todo: Get state for single files.
678
 
 
679
 
        :todo: Perhaps show a slash at the end of directory names.        
680
 
 
 
896
        TODO: Get state for single files.
681
897
        """
682
898
 
683
899
        # We have to build everything into a list first so that it can
689
905
        # Interesting case: the old ID for a file has been removed,
690
906
        # but a new file has been created under that name.
691
907
 
692
 
        old = branch.basis_tree()
693
 
        old_inv = old.inventory
694
 
        new = branch.working_tree()
695
 
        new_inv = new.inventory
 
908
        old = self.basis_tree()
 
909
        new = self.working_tree()
696
910
 
697
911
        for fs, fid, oldname, newname, kind in diff_trees(old, new):
698
912
            if fs == 'R':
711
925
            elif fs == '?':
712
926
                show_status(fs, kind, newname)
713
927
            else:
714
 
                bailout("wierd file state %r" % ((fs, fid),))
 
928
                bailout("weird file state %r" % ((fs, fid),))
715
929
                
716
930
 
717
931
 
726
940
    >>> isdir(bd)
727
941
    False
728
942
    """
729
 
    def __init__(self, files = []):
 
943
    def __init__(self, files=[], dirs=[]):
730
944
        """Make a test branch.
731
945
 
732
946
        This creates a temporary directory and runs init-tree in it.
734
948
        If any files are listed, they are created in the working copy.
735
949
        """
736
950
        Branch.__init__(self, tempfile.mkdtemp(), init=True)
 
951
        for d in dirs:
 
952
            os.mkdir(self.abspath(d))
 
953
            
737
954
        for f in files:
738
955
            file(os.path.join(self.base, f), 'w').write('content of %s' % f)
739
956
 
740
957
 
741
958
    def __del__(self):
742
959
        """Destroy the test branch, removing the scratch directory."""
743
 
        shutil.rmtree(self.base)
 
960
        try:
 
961
            shutil.rmtree(self.base)
 
962
        except OSError:
 
963
            # Work around for shutil.rmtree failing on Windows when
 
964
            # readonly files are encountered
 
965
            for root, dirs, files in os.walk(self.base, topdown=False):
 
966
                for name in files:
 
967
                    os.chmod(os.path.join(root, name), 0700)
 
968
            shutil.rmtree(self.base)
744
969
 
745
970
    
746
971
 
756
981
        ## mutter('check %r for control file' % ((head, tail), ))
757
982
        if tail == bzrlib.BZRDIR:
758
983
            return True
 
984
        if filename == head:
 
985
            break
759
986
        filename = head
760
987
    return False
761
988
 
768
995
    return s
769
996
 
770
997
 
771
 
def _gen_file_id(name):
 
998
def gen_file_id(name):
772
999
    """Return new file id.
773
1000
 
774
1001
    This should probably generate proper UUIDs, but for the moment we
775
1002
    cope with just randomness because running uuidgen every time is
776
1003
    slow."""
777
 
    assert '/' not in name
778
 
    while name[0] == '.':
779
 
        name = name[1:]
 
1004
    idx = name.rfind('/')
 
1005
    if idx != -1:
 
1006
        name = name[idx+1 : ]
 
1007
    idx = name.rfind('\\')
 
1008
    if idx != -1:
 
1009
        name = name[idx+1 : ]
 
1010
 
 
1011
    name = name.lstrip('.')
 
1012
 
780
1013
    s = hexlify(rand_bytes(8))
781
1014
    return '-'.join((name, compact_date(time.time()), s))
782
 
 
783