~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-06-10 09:28:48 UTC
  • Revision ID: mbp@sourcefrog.net-20050610092848-8ee8b97347c85849
- change rsync command for upload

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
     joinpath, sha_string, file_kind, local_time_offset, appendpath
30
30
from store import ImmutableStore
31
31
from revision import Revision
32
 
from errors import bailout, BzrError
 
32
from errors import BzrError
33
33
from textui import show_status
34
34
 
35
35
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
43
43
        return remotebranch.RemoteBranch(f, **args)
44
44
    else:
45
45
        return Branch(f, **args)
 
46
 
 
47
 
 
48
 
 
49
def _relpath(base, path):
 
50
    """Return path relative to base, or raise exception.
 
51
 
 
52
    The path may be either an absolute path or a path relative to the
 
53
    current working directory.
 
54
 
 
55
    Lifted out of Branch.relpath for ease of testing.
 
56
 
 
57
    os.path.commonprefix (python2.4) has a bad bug that it works just
 
58
    on string prefixes, assuming that '/u' is a prefix of '/u2'.  This
 
59
    avoids that problem."""
 
60
    rp = os.path.abspath(path)
 
61
 
 
62
    s = []
 
63
    head = rp
 
64
    while len(head) >= len(base):
 
65
        if head == base:
 
66
            break
 
67
        head, tail = os.path.split(head)
 
68
        if tail:
 
69
            s.insert(0, tail)
 
70
    else:
 
71
        from errors import NotBranchError
 
72
        raise NotBranchError("path %r is not within branch %r" % (rp, base))
 
73
 
 
74
    return os.sep.join(s)
46
75
        
47
76
 
48
77
def find_branch_root(f=None):
75
104
            raise BzrError('%r is not in a branch' % orig_f)
76
105
        f = head
77
106
    
78
 
 
 
107
class DivergedBranches(Exception):
 
108
    def __init__(self, branch1, branch2):
 
109
        self.branch1 = branch1
 
110
        self.branch2 = branch2
 
111
        Exception.__init__(self, "These branches have diverged.")
79
112
 
80
113
######################################################################
81
114
# branch objects
85
118
 
86
119
    base
87
120
        Base directory of the branch.
 
121
 
 
122
    _lock_mode
 
123
        None, or 'r' or 'w'
 
124
 
 
125
    _lock_count
 
126
        If _lock_mode is true, a positive count of the number of times the
 
127
        lock has been taken.
 
128
 
 
129
    _lock
 
130
        Lock object from bzrlib.lock.
88
131
    """
89
 
    _lockmode = None
 
132
    base = None
 
133
    _lock_mode = None
 
134
    _lock_count = None
 
135
    _lock = None
90
136
    
91
 
    def __init__(self, base, init=False, find_root=True, lock_mode='w'):
 
137
    def __init__(self, base, init=False, find_root=True):
92
138
        """Create new branch object at a particular location.
93
139
 
94
140
        base -- Base directory for the branch.
111
157
        else:
112
158
            self.base = os.path.realpath(base)
113
159
            if not isdir(self.controlfilename('.')):
114
 
                bailout("not a bzr branch: %s" % quotefn(base),
115
 
                        ['use "bzr init" to initialize a new working tree',
116
 
                         'current bzr can only operate from top-of-tree'])
 
160
                from errors import NotBranchError
 
161
                raise NotBranchError("not a bzr branch: %s" % quotefn(base),
 
162
                                     ['use "bzr init" to initialize a new working tree',
 
163
                                      'current bzr can only operate from top-of-tree'])
117
164
        self._check_format()
118
 
        self.lock(lock_mode)
119
165
 
120
166
        self.text_store = ImmutableStore(self.controlfilename('text-store'))
121
167
        self.revision_store = ImmutableStore(self.controlfilename('revision-store'))
129
175
    __repr__ = __str__
130
176
 
131
177
 
132
 
 
133
 
    def lock(self, mode='w'):
134
 
        """Lock the on-disk branch, excluding other processes."""
135
 
        try:
136
 
            import fcntl, errno
137
 
 
138
 
            if mode == 'w':
139
 
                lm = fcntl.LOCK_EX
140
 
                om = os.O_WRONLY | os.O_CREAT
141
 
            elif mode == 'r':
142
 
                lm = fcntl.LOCK_SH
143
 
                om = os.O_RDONLY
144
 
            else:
145
 
                raise BzrError("invalid locking mode %r" % mode)
146
 
 
147
 
            try:
148
 
                lockfile = os.open(self.controlfilename('branch-lock'), om)
149
 
            except OSError, e:
150
 
                if e.errno == errno.ENOENT:
151
 
                    # might not exist on branches from <0.0.4
152
 
                    self.controlfile('branch-lock', 'w').close()
153
 
                    lockfile = os.open(self.controlfilename('branch-lock'), om)
154
 
                else:
155
 
                    raise e
 
178
    def __del__(self):
 
179
        if self._lock_mode or self._lock:
 
180
            from warnings import warn
 
181
            warn("branch %r was not explicitly unlocked" % self)
 
182
            self._lock.unlock()
 
183
 
 
184
 
 
185
 
 
186
    def lock_write(self):
 
187
        if self._lock_mode:
 
188
            if self._lock_mode != 'w':
 
189
                from errors import LockError
 
190
                raise LockError("can't upgrade to a write lock from %r" %
 
191
                                self._lock_mode)
 
192
            self._lock_count += 1
 
193
        else:
 
194
            from bzrlib.lock import WriteLock
 
195
 
 
196
            self._lock = WriteLock(self.controlfilename('branch-lock'))
 
197
            self._lock_mode = 'w'
 
198
            self._lock_count = 1
 
199
 
 
200
 
 
201
 
 
202
    def lock_read(self):
 
203
        if self._lock_mode:
 
204
            assert self._lock_mode in ('r', 'w'), \
 
205
                   "invalid lock mode %r" % self._lock_mode
 
206
            self._lock_count += 1
 
207
        else:
 
208
            from bzrlib.lock import ReadLock
 
209
 
 
210
            self._lock = ReadLock(self.controlfilename('branch-lock'))
 
211
            self._lock_mode = 'r'
 
212
            self._lock_count = 1
 
213
                        
 
214
 
156
215
            
157
 
            fcntl.lockf(lockfile, lm)
158
 
            def unlock():
159
 
                fcntl.lockf(lockfile, fcntl.LOCK_UN)
160
 
                os.close(lockfile)
161
 
                self._lockmode = None
162
 
            self.unlock = unlock
163
 
            self._lockmode = mode
164
 
        except ImportError:
165
 
            warning("please write a locking method for platform %r" % sys.platform)
166
 
            def unlock():
167
 
                self._lockmode = None
168
 
            self.unlock = unlock
169
 
            self._lockmode = mode
170
 
 
171
 
 
172
 
    def _need_readlock(self):
173
 
        if self._lockmode not in ['r', 'w']:
174
 
            raise BzrError('need read lock on branch, only have %r' % self._lockmode)
175
 
 
176
 
    def _need_writelock(self):
177
 
        if self._lockmode not in ['w']:
178
 
            raise BzrError('need write lock on branch, only have %r' % self._lockmode)
 
216
    def unlock(self):
 
217
        if not self._lock_mode:
 
218
            from errors import LockError
 
219
            raise LockError('branch %r is not locked' % (self))
 
220
 
 
221
        if self._lock_count > 1:
 
222
            self._lock_count -= 1
 
223
        else:
 
224
            self._lock.unlock()
 
225
            self._lock = None
 
226
            self._lock_mode = self._lock_count = None
179
227
 
180
228
 
181
229
    def abspath(self, name):
187
235
        """Return path relative to this branch of something inside it.
188
236
 
189
237
        Raises an error if path is not in this branch."""
190
 
        rp = os.path.realpath(path)
191
 
        # FIXME: windows
192
 
        if not rp.startswith(self.base):
193
 
            bailout("path %r is not within branch %r" % (rp, self.base))
194
 
        rp = rp[len(self.base):]
195
 
        rp = rp.lstrip(os.sep)
196
 
        return rp
 
238
        return _relpath(self.base, path)
197
239
 
198
240
 
199
241
    def controlfilename(self, file_or_path):
260
302
        fmt = self.controlfile('branch-format', 'r').read()
261
303
        fmt.replace('\r\n', '')
262
304
        if fmt != BZR_BRANCH_FORMAT:
263
 
            bailout('sorry, branch format %r not supported' % fmt,
264
 
                    ['use a different bzr version',
265
 
                     'or remove the .bzr directory and "bzr init" again'])
 
305
            raise BzrError('sorry, branch format %r not supported' % fmt,
 
306
                           ['use a different bzr version',
 
307
                            'or remove the .bzr directory and "bzr init" again'])
 
308
 
266
309
 
267
310
 
268
311
    def read_working_inventory(self):
269
312
        """Read the working inventory."""
270
 
        self._need_readlock()
271
313
        before = time.time()
272
314
        # ElementTree does its own conversion from UTF-8, so open in
273
315
        # binary.
274
 
        inv = Inventory.read_xml(self.controlfile('inventory', 'rb'))
275
 
        mutter("loaded inventory of %d items in %f"
276
 
               % (len(inv), time.time() - before))
277
 
        return inv
278
 
 
 
316
        self.lock_read()
 
317
        try:
 
318
            inv = Inventory.read_xml(self.controlfile('inventory', 'rb'))
 
319
            mutter("loaded inventory of %d items in %f"
 
320
                   % (len(inv), time.time() - before))
 
321
            return inv
 
322
        finally:
 
323
            self.unlock()
 
324
            
279
325
 
280
326
    def _write_inventory(self, inv):
281
327
        """Update the working inventory.
283
329
        That is to say, the inventory describing changes underway, that
284
330
        will be committed to the next revision.
285
331
        """
286
 
        self._need_writelock()
287
332
        ## TODO: factor out to atomicfile?  is rename safe on windows?
288
333
        ## TODO: Maybe some kind of clean/dirty marker on inventory?
289
334
        tmpfname = self.controlfilename('inventory.tmp')
295
340
            os.remove(inv_fname)
296
341
        os.rename(tmpfname, inv_fname)
297
342
        mutter('wrote working inventory')
298
 
 
 
343
            
299
344
 
300
345
    inventory = property(read_working_inventory, _write_inventory, None,
301
346
                         """Inventory for the working copy.""")
309
354
        This puts the files in the Added state, so that they will be
310
355
        recorded by the next commit.
311
356
 
 
357
        files
 
358
            List of paths to add, relative to the base of the tree.
 
359
 
 
360
        ids
 
361
            If set, use these instead of automatically generated ids.
 
362
            Must be the same length as the list of files, but may
 
363
            contain None for ids that are to be autogenerated.
 
364
 
312
365
        TODO: Perhaps have an option to add the ids even if the files do
313
 
               not (yet) exist.
 
366
              not (yet) exist.
314
367
 
315
368
        TODO: Perhaps return the ids of the files?  But then again it
316
 
               is easy to retrieve them if they're needed.
317
 
 
318
 
        TODO: Option to specify file id.
 
369
              is easy to retrieve them if they're needed.
319
370
 
320
371
        TODO: Adding a directory should optionally recurse down and
321
 
               add all non-ignored children.  Perhaps do that in a
322
 
               higher-level method.
 
372
              add all non-ignored children.  Perhaps do that in a
 
373
              higher-level method.
323
374
        """
324
 
        self._need_writelock()
325
 
 
326
375
        # TODO: Re-adding a file that is removed in the working copy
327
376
        # should probably put it back with the previous ID.
328
377
        if isinstance(files, types.StringTypes):
335
384
            ids = [None] * len(files)
336
385
        else:
337
386
            assert(len(ids) == len(files))
338
 
        
339
 
        inv = self.read_working_inventory()
340
 
        for f,file_id in zip(files, ids):
341
 
            if is_control_file(f):
342
 
                bailout("cannot add control file %s" % quotefn(f))
343
 
 
344
 
            fp = splitpath(f)
345
 
 
346
 
            if len(fp) == 0:
347
 
                bailout("cannot add top-level %r" % f)
348
 
                
349
 
            fullpath = os.path.normpath(self.abspath(f))
350
 
 
351
 
            try:
352
 
                kind = file_kind(fullpath)
353
 
            except OSError:
354
 
                # maybe something better?
355
 
                bailout('cannot add: not a regular file or directory: %s' % quotefn(f))
356
 
            
357
 
            if kind != 'file' and kind != 'directory':
358
 
                bailout('cannot add: not a regular file or directory: %s' % quotefn(f))
359
 
 
360
 
            if file_id is None:
361
 
                file_id = gen_file_id(f)
362
 
            inv.add_path(f, kind=kind, file_id=file_id)
363
 
 
364
 
            if verbose:
365
 
                show_status('A', kind, quotefn(f))
366
 
                
367
 
            mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
368
 
            
369
 
        self._write_inventory(inv)
370
 
 
 
387
 
 
388
        self.lock_write()
 
389
        try:
 
390
            inv = self.read_working_inventory()
 
391
            for f,file_id in zip(files, ids):
 
392
                if is_control_file(f):
 
393
                    raise BzrError("cannot add control file %s" % quotefn(f))
 
394
 
 
395
                fp = splitpath(f)
 
396
 
 
397
                if len(fp) == 0:
 
398
                    raise BzrError("cannot add top-level %r" % f)
 
399
 
 
400
                fullpath = os.path.normpath(self.abspath(f))
 
401
 
 
402
                try:
 
403
                    kind = file_kind(fullpath)
 
404
                except OSError:
 
405
                    # maybe something better?
 
406
                    raise BzrError('cannot add: not a regular file or directory: %s' % quotefn(f))
 
407
 
 
408
                if kind != 'file' and kind != 'directory':
 
409
                    raise BzrError('cannot add: not a regular file or directory: %s' % quotefn(f))
 
410
 
 
411
                if file_id is None:
 
412
                    file_id = gen_file_id(f)
 
413
                inv.add_path(f, kind=kind, file_id=file_id)
 
414
 
 
415
                if verbose:
 
416
                    show_status('A', kind, quotefn(f))
 
417
 
 
418
                mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
 
419
 
 
420
            self._write_inventory(inv)
 
421
        finally:
 
422
            self.unlock()
 
423
            
371
424
 
372
425
    def print_file(self, file, revno):
373
426
        """Print `file` to stdout."""
374
 
        self._need_readlock()
375
 
        tree = self.revision_tree(self.lookup_revision(revno))
376
 
        # use inventory as it was in that revision
377
 
        file_id = tree.inventory.path2id(file)
378
 
        if not file_id:
379
 
            bailout("%r is not present in revision %d" % (file, revno))
380
 
        tree.print_file(file_id)
381
 
        
 
427
        self.lock_read()
 
428
        try:
 
429
            tree = self.revision_tree(self.lookup_revision(revno))
 
430
            # use inventory as it was in that revision
 
431
            file_id = tree.inventory.path2id(file)
 
432
            if not file_id:
 
433
                raise BzrError("%r is not present in revision %d" % (file, revno))
 
434
            tree.print_file(file_id)
 
435
        finally:
 
436
            self.unlock()
 
437
 
382
438
 
383
439
    def remove(self, files, verbose=False):
384
440
        """Mark nominated files for removal from the inventory.
396
452
        """
397
453
        ## TODO: Normalize names
398
454
        ## TODO: Remove nested loops; better scalability
399
 
        self._need_writelock()
400
 
 
401
455
        if isinstance(files, types.StringTypes):
402
456
            files = [files]
403
 
        
404
 
        tree = self.working_tree()
405
 
        inv = tree.inventory
406
 
 
407
 
        # do this before any modifications
408
 
        for f in files:
409
 
            fid = inv.path2id(f)
410
 
            if not fid:
411
 
                bailout("cannot remove unversioned file %s" % quotefn(f))
412
 
            mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
413
 
            if verbose:
414
 
                # having remove it, it must be either ignored or unknown
415
 
                if tree.is_ignored(f):
416
 
                    new_status = 'I'
417
 
                else:
418
 
                    new_status = '?'
419
 
                show_status(new_status, inv[fid].kind, quotefn(f))
420
 
            del inv[fid]
421
 
 
422
 
        self._write_inventory(inv)
423
 
 
 
457
 
 
458
        self.lock_write()
 
459
 
 
460
        try:
 
461
            tree = self.working_tree()
 
462
            inv = tree.inventory
 
463
 
 
464
            # do this before any modifications
 
465
            for f in files:
 
466
                fid = inv.path2id(f)
 
467
                if not fid:
 
468
                    raise BzrError("cannot remove unversioned file %s" % quotefn(f))
 
469
                mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
 
470
                if verbose:
 
471
                    # having remove it, it must be either ignored or unknown
 
472
                    if tree.is_ignored(f):
 
473
                        new_status = 'I'
 
474
                    else:
 
475
                        new_status = '?'
 
476
                    show_status(new_status, inv[fid].kind, quotefn(f))
 
477
                del inv[fid]
 
478
 
 
479
            self._write_inventory(inv)
 
480
        finally:
 
481
            self.unlock()
 
482
 
 
483
 
 
484
    # FIXME: this doesn't need to be a branch method
424
485
    def set_inventory(self, new_inventory_list):
425
486
        inv = Inventory()
426
487
        for path, file_id, parent, kind in new_inventory_list:
471
532
 
472
533
    def get_revision(self, revision_id):
473
534
        """Return the Revision object for a named revision"""
474
 
        self._need_readlock()
475
535
        r = Revision.read_xml(self.revision_store[revision_id])
476
536
        assert r.revision_id == revision_id
477
537
        return r
483
543
        TODO: Perhaps for this and similar methods, take a revision
484
544
               parameter which can be either an integer revno or a
485
545
               string hash."""
486
 
        self._need_readlock()
487
546
        i = Inventory.read_xml(self.inventory_store[inventory_id])
488
547
        return i
489
548
 
490
549
 
491
550
    def get_revision_inventory(self, revision_id):
492
551
        """Return inventory of a past revision."""
493
 
        self._need_readlock()
494
552
        if revision_id == None:
495
553
            return Inventory()
496
554
        else:
503
561
        >>> ScratchBranch().revision_history()
504
562
        []
505
563
        """
506
 
        self._need_readlock()
507
 
        return [l.rstrip('\r\n') for l in self.controlfile('revision-history', 'r').readlines()]
508
 
 
 
564
        self.lock_read()
 
565
        try:
 
566
            return [l.rstrip('\r\n') for l in
 
567
                    self.controlfile('revision-history', 'r').readlines()]
 
568
        finally:
 
569
            self.unlock()
 
570
 
 
571
 
 
572
    def common_ancestor(self, other, self_revno=None, other_revno=None):
 
573
        """
 
574
        >>> import commit
 
575
        >>> sb = ScratchBranch(files=['foo', 'foo~'])
 
576
        >>> sb.common_ancestor(sb) == (None, None)
 
577
        True
 
578
        >>> commit.commit(sb, "Committing first revision", verbose=False)
 
579
        >>> sb.common_ancestor(sb)[0]
 
580
        1
 
581
        >>> clone = sb.clone()
 
582
        >>> commit.commit(sb, "Committing second revision", verbose=False)
 
583
        >>> sb.common_ancestor(sb)[0]
 
584
        2
 
585
        >>> sb.common_ancestor(clone)[0]
 
586
        1
 
587
        >>> commit.commit(clone, "Committing divergent second revision", 
 
588
        ...               verbose=False)
 
589
        >>> sb.common_ancestor(clone)[0]
 
590
        1
 
591
        >>> sb.common_ancestor(clone) == clone.common_ancestor(sb)
 
592
        True
 
593
        >>> sb.common_ancestor(sb) != clone.common_ancestor(clone)
 
594
        True
 
595
        >>> clone2 = sb.clone()
 
596
        >>> sb.common_ancestor(clone2)[0]
 
597
        2
 
598
        >>> sb.common_ancestor(clone2, self_revno=1)[0]
 
599
        1
 
600
        >>> sb.common_ancestor(clone2, other_revno=1)[0]
 
601
        1
 
602
        """
 
603
        my_history = self.revision_history()
 
604
        other_history = other.revision_history()
 
605
        if self_revno is None:
 
606
            self_revno = len(my_history)
 
607
        if other_revno is None:
 
608
            other_revno = len(other_history)
 
609
        indices = range(min((self_revno, other_revno)))
 
610
        indices.reverse()
 
611
        for r in indices:
 
612
            if my_history[r] == other_history[r]:
 
613
                return r+1, my_history[r]
 
614
        return None, None
509
615
 
510
616
    def enum_history(self, direction):
511
617
        """Return (revno, revision_id) for history of branch.
548
654
            return None
549
655
 
550
656
 
 
657
    def missing_revisions(self, other):
 
658
        """
 
659
        If self and other have not diverged, return a list of the revisions
 
660
        present in other, but missing from self.
 
661
 
 
662
        >>> from bzrlib.commit import commit
 
663
        >>> bzrlib.trace.silent = True
 
664
        >>> br1 = ScratchBranch()
 
665
        >>> br2 = ScratchBranch()
 
666
        >>> br1.missing_revisions(br2)
 
667
        []
 
668
        >>> commit(br2, "lala!", rev_id="REVISION-ID-1")
 
669
        >>> br1.missing_revisions(br2)
 
670
        [u'REVISION-ID-1']
 
671
        >>> br2.missing_revisions(br1)
 
672
        []
 
673
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1")
 
674
        >>> br1.missing_revisions(br2)
 
675
        []
 
676
        >>> commit(br2, "lala!", rev_id="REVISION-ID-2A")
 
677
        >>> br1.missing_revisions(br2)
 
678
        [u'REVISION-ID-2A']
 
679
        >>> commit(br1, "lala!", rev_id="REVISION-ID-2B")
 
680
        >>> br1.missing_revisions(br2)
 
681
        Traceback (most recent call last):
 
682
        DivergedBranches: These branches have diverged.
 
683
        """
 
684
        self_history = self.revision_history()
 
685
        self_len = len(self_history)
 
686
        other_history = other.revision_history()
 
687
        other_len = len(other_history)
 
688
        common_index = min(self_len, other_len) -1
 
689
        if common_index >= 0 and \
 
690
            self_history[common_index] != other_history[common_index]:
 
691
            raise DivergedBranches(self, other)
 
692
        if self_len < other_len:
 
693
            return other_history[self_len:]
 
694
        return []
 
695
 
 
696
 
 
697
    def update_revisions(self, other):
 
698
        """Pull in all new revisions from other branch.
 
699
        
 
700
        >>> from bzrlib.commit import commit
 
701
        >>> bzrlib.trace.silent = True
 
702
        >>> br1 = ScratchBranch(files=['foo', 'bar'])
 
703
        >>> br1.add('foo')
 
704
        >>> br1.add('bar')
 
705
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
 
706
        >>> br2 = ScratchBranch()
 
707
        >>> br2.update_revisions(br1)
 
708
        Added 2 texts.
 
709
        Added 1 inventories.
 
710
        Added 1 revisions.
 
711
        >>> br2.revision_history()
 
712
        [u'REVISION-ID-1']
 
713
        >>> br2.update_revisions(br1)
 
714
        Added 0 texts.
 
715
        Added 0 inventories.
 
716
        Added 0 revisions.
 
717
        >>> br1.text_store.total_size() == br2.text_store.total_size()
 
718
        True
 
719
        """
 
720
        revision_ids = self.missing_revisions(other)
 
721
        revisions = [other.get_revision(f) for f in revision_ids]
 
722
        needed_texts = sets.Set()
 
723
        for rev in revisions:
 
724
            inv = other.get_inventory(str(rev.inventory_id))
 
725
            for key, entry in inv.iter_entries():
 
726
                if entry.text_id is None:
 
727
                    continue
 
728
                if entry.text_id not in self.text_store:
 
729
                    needed_texts.add(entry.text_id)
 
730
        count = self.text_store.copy_multi(other.text_store, needed_texts)
 
731
        print "Added %d texts." % count 
 
732
        inventory_ids = [ f.inventory_id for f in revisions ]
 
733
        count = self.inventory_store.copy_multi(other.inventory_store, 
 
734
                                                inventory_ids)
 
735
        print "Added %d inventories." % count 
 
736
        revision_ids = [ f.revision_id for f in revisions]
 
737
        count = self.revision_store.copy_multi(other.revision_store, 
 
738
                                               revision_ids)
 
739
        for revision_id in revision_ids:
 
740
            self.append_revision(revision_id)
 
741
        print "Added %d revisions." % count
 
742
                    
 
743
        
551
744
    def commit(self, *args, **kw):
552
745
        """Deprecated"""
553
746
        from bzrlib.commit import commit
573
766
        an `EmptyTree` is returned."""
574
767
        # TODO: refactor this to use an existing revision object
575
768
        # so we don't need to read it in twice.
576
 
        self._need_readlock()
577
769
        if revision_id == None:
578
770
            return EmptyTree()
579
771
        else:
605
797
 
606
798
        This can change the directory or the filename or both.
607
799
        """
608
 
        self._need_writelock()
609
 
        tree = self.working_tree()
610
 
        inv = tree.inventory
611
 
        if not tree.has_filename(from_rel):
612
 
            bailout("can't rename: old working file %r does not exist" % from_rel)
613
 
        if tree.has_filename(to_rel):
614
 
            bailout("can't rename: new working file %r already exists" % to_rel)
615
 
            
616
 
        file_id = inv.path2id(from_rel)
617
 
        if file_id == None:
618
 
            bailout("can't rename: old name %r is not versioned" % from_rel)
619
 
 
620
 
        if inv.path2id(to_rel):
621
 
            bailout("can't rename: new name %r is already versioned" % to_rel)
622
 
 
623
 
        to_dir, to_tail = os.path.split(to_rel)
624
 
        to_dir_id = inv.path2id(to_dir)
625
 
        if to_dir_id == None and to_dir != '':
626
 
            bailout("can't determine destination directory id for %r" % to_dir)
627
 
 
628
 
        mutter("rename_one:")
629
 
        mutter("  file_id    {%s}" % file_id)
630
 
        mutter("  from_rel   %r" % from_rel)
631
 
        mutter("  to_rel     %r" % to_rel)
632
 
        mutter("  to_dir     %r" % to_dir)
633
 
        mutter("  to_dir_id  {%s}" % to_dir_id)
634
 
            
635
 
        inv.rename(file_id, to_dir_id, to_tail)
636
 
 
637
 
        print "%s => %s" % (from_rel, to_rel)
638
 
        
639
 
        from_abs = self.abspath(from_rel)
640
 
        to_abs = self.abspath(to_rel)
 
800
        self.lock_write()
641
801
        try:
642
 
            os.rename(from_abs, to_abs)
643
 
        except OSError, e:
644
 
            bailout("failed to rename %r to %r: %s"
645
 
                    % (from_abs, to_abs, e[1]),
646
 
                    ["rename rolled back"])
647
 
 
648
 
        self._write_inventory(inv)
649
 
            
 
802
            tree = self.working_tree()
 
803
            inv = tree.inventory
 
804
            if not tree.has_filename(from_rel):
 
805
                raise BzrError("can't rename: old working file %r does not exist" % from_rel)
 
806
            if tree.has_filename(to_rel):
 
807
                raise BzrError("can't rename: new working file %r already exists" % to_rel)
 
808
 
 
809
            file_id = inv.path2id(from_rel)
 
810
            if file_id == None:
 
811
                raise BzrError("can't rename: old name %r is not versioned" % from_rel)
 
812
 
 
813
            if inv.path2id(to_rel):
 
814
                raise BzrError("can't rename: new name %r is already versioned" % to_rel)
 
815
 
 
816
            to_dir, to_tail = os.path.split(to_rel)
 
817
            to_dir_id = inv.path2id(to_dir)
 
818
            if to_dir_id == None and to_dir != '':
 
819
                raise BzrError("can't determine destination directory id for %r" % to_dir)
 
820
 
 
821
            mutter("rename_one:")
 
822
            mutter("  file_id    {%s}" % file_id)
 
823
            mutter("  from_rel   %r" % from_rel)
 
824
            mutter("  to_rel     %r" % to_rel)
 
825
            mutter("  to_dir     %r" % to_dir)
 
826
            mutter("  to_dir_id  {%s}" % to_dir_id)
 
827
 
 
828
            inv.rename(file_id, to_dir_id, to_tail)
 
829
 
 
830
            print "%s => %s" % (from_rel, to_rel)
 
831
 
 
832
            from_abs = self.abspath(from_rel)
 
833
            to_abs = self.abspath(to_rel)
 
834
            try:
 
835
                os.rename(from_abs, to_abs)
 
836
            except OSError, e:
 
837
                raise BzrError("failed to rename %r to %r: %s"
 
838
                        % (from_abs, to_abs, e[1]),
 
839
                        ["rename rolled back"])
 
840
 
 
841
            self._write_inventory(inv)
 
842
        finally:
 
843
            self.unlock()
650
844
 
651
845
 
652
846
    def move(self, from_paths, to_name):
660
854
        Note that to_name is only the last component of the new name;
661
855
        this doesn't change the directory.
662
856
        """
663
 
        self._need_writelock()
664
 
        ## TODO: Option to move IDs only
665
 
        assert not isinstance(from_paths, basestring)
666
 
        tree = self.working_tree()
667
 
        inv = tree.inventory
668
 
        to_abs = self.abspath(to_name)
669
 
        if not isdir(to_abs):
670
 
            bailout("destination %r is not a directory" % to_abs)
671
 
        if not tree.has_filename(to_name):
672
 
            bailout("destination %r not in working directory" % to_abs)
673
 
        to_dir_id = inv.path2id(to_name)
674
 
        if to_dir_id == None and to_name != '':
675
 
            bailout("destination %r is not a versioned directory" % to_name)
676
 
        to_dir_ie = inv[to_dir_id]
677
 
        if to_dir_ie.kind not in ('directory', 'root_directory'):
678
 
            bailout("destination %r is not a directory" % to_abs)
679
 
 
680
 
        to_idpath = inv.get_idpath(to_dir_id)
681
 
 
682
 
        for f in from_paths:
683
 
            if not tree.has_filename(f):
684
 
                bailout("%r does not exist in working tree" % f)
685
 
            f_id = inv.path2id(f)
686
 
            if f_id == None:
687
 
                bailout("%r is not versioned" % f)
688
 
            name_tail = splitpath(f)[-1]
689
 
            dest_path = appendpath(to_name, name_tail)
690
 
            if tree.has_filename(dest_path):
691
 
                bailout("destination %r already exists" % dest_path)
692
 
            if f_id in to_idpath:
693
 
                bailout("can't move %r to a subdirectory of itself" % f)
694
 
 
695
 
        # OK, so there's a race here, it's possible that someone will
696
 
        # create a file in this interval and then the rename might be
697
 
        # left half-done.  But we should have caught most problems.
698
 
 
699
 
        for f in from_paths:
700
 
            name_tail = splitpath(f)[-1]
701
 
            dest_path = appendpath(to_name, name_tail)
702
 
            print "%s => %s" % (f, dest_path)
703
 
            inv.rename(inv.path2id(f), to_dir_id, name_tail)
704
 
            try:
705
 
                os.rename(self.abspath(f), self.abspath(dest_path))
706
 
            except OSError, e:
707
 
                bailout("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
708
 
                        ["rename rolled back"])
709
 
 
710
 
        self._write_inventory(inv)
711
 
 
 
857
        self.lock_write()
 
858
        try:
 
859
            ## TODO: Option to move IDs only
 
860
            assert not isinstance(from_paths, basestring)
 
861
            tree = self.working_tree()
 
862
            inv = tree.inventory
 
863
            to_abs = self.abspath(to_name)
 
864
            if not isdir(to_abs):
 
865
                raise BzrError("destination %r is not a directory" % to_abs)
 
866
            if not tree.has_filename(to_name):
 
867
                raise BzrError("destination %r not in working directory" % to_abs)
 
868
            to_dir_id = inv.path2id(to_name)
 
869
            if to_dir_id == None and to_name != '':
 
870
                raise BzrError("destination %r is not a versioned directory" % to_name)
 
871
            to_dir_ie = inv[to_dir_id]
 
872
            if to_dir_ie.kind not in ('directory', 'root_directory'):
 
873
                raise BzrError("destination %r is not a directory" % to_abs)
 
874
 
 
875
            to_idpath = inv.get_idpath(to_dir_id)
 
876
 
 
877
            for f in from_paths:
 
878
                if not tree.has_filename(f):
 
879
                    raise BzrError("%r does not exist in working tree" % f)
 
880
                f_id = inv.path2id(f)
 
881
                if f_id == None:
 
882
                    raise BzrError("%r is not versioned" % f)
 
883
                name_tail = splitpath(f)[-1]
 
884
                dest_path = appendpath(to_name, name_tail)
 
885
                if tree.has_filename(dest_path):
 
886
                    raise BzrError("destination %r already exists" % dest_path)
 
887
                if f_id in to_idpath:
 
888
                    raise BzrError("can't move %r to a subdirectory of itself" % f)
 
889
 
 
890
            # OK, so there's a race here, it's possible that someone will
 
891
            # create a file in this interval and then the rename might be
 
892
            # left half-done.  But we should have caught most problems.
 
893
 
 
894
            for f in from_paths:
 
895
                name_tail = splitpath(f)[-1]
 
896
                dest_path = appendpath(to_name, name_tail)
 
897
                print "%s => %s" % (f, dest_path)
 
898
                inv.rename(inv.path2id(f), to_dir_id, name_tail)
 
899
                try:
 
900
                    os.rename(self.abspath(f), self.abspath(dest_path))
 
901
                except OSError, e:
 
902
                    raise BzrError("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
 
903
                            ["rename rolled back"])
 
904
 
 
905
            self._write_inventory(inv)
 
906
        finally:
 
907
            self.unlock()
712
908
 
713
909
 
714
910
 
723
919
    >>> isdir(bd)
724
920
    False
725
921
    """
726
 
    def __init__(self, files=[], dirs=[]):
 
922
    def __init__(self, files=[], dirs=[], base=None):
727
923
        """Make a test branch.
728
924
 
729
925
        This creates a temporary directory and runs init-tree in it.
730
926
 
731
927
        If any files are listed, they are created in the working copy.
732
928
        """
733
 
        Branch.__init__(self, tempfile.mkdtemp(), init=True)
 
929
        init = False
 
930
        if base is None:
 
931
            base = tempfile.mkdtemp()
 
932
            init = True
 
933
        Branch.__init__(self, base, init=init)
734
934
        for d in dirs:
735
935
            os.mkdir(self.abspath(d))
736
936
            
738
938
            file(os.path.join(self.base, f), 'w').write('content of %s' % f)
739
939
 
740
940
 
 
941
    def clone(self):
 
942
        """
 
943
        >>> orig = ScratchBranch(files=["file1", "file2"])
 
944
        >>> clone = orig.clone()
 
945
        >>> os.path.samefile(orig.base, clone.base)
 
946
        False
 
947
        >>> os.path.isfile(os.path.join(clone.base, "file1"))
 
948
        True
 
949
        """
 
950
        base = tempfile.mkdtemp()
 
951
        os.rmdir(base)
 
952
        shutil.copytree(self.base, base, symlinks=True)
 
953
        return ScratchBranch(base=base)
 
954
        
741
955
    def __del__(self):
742
956
        self.destroy()
743
957
 
744
958
    def destroy(self):
745
959
        """Destroy the test branch, removing the scratch directory."""
746
960
        try:
747
 
            mutter("delete ScratchBranch %s" % self.base)
748
 
            shutil.rmtree(self.base)
 
961
            if self.base:
 
962
                mutter("delete ScratchBranch %s" % self.base)
 
963
                shutil.rmtree(self.base)
749
964
        except OSError, e:
750
965
            # Work around for shutil.rmtree failing on Windows when
751
966
            # readonly files are encountered