~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

Move rename_one from Branch to WorkingTree. (Robert Collins).

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
                           needs_write_lock,
53
53
                           quotefn)
54
54
from bzrlib.errors import (BzrCheckError,
 
55
                           BzrError,
55
56
                           DivergedBranches,
56
57
                           NotBranchError,
57
58
                           NotVersionedError)
63
64
                            pumpfile,
64
65
                            splitpath,
65
66
                            rand_bytes,
66
 
                            relpath)
 
67
                            relpath,
 
68
                            rename)
67
69
import bzrlib.tree
68
70
from bzrlib.trace import mutter
69
71
import bzrlib.xml5
496
498
 
497
499
        for f in descend('', inv.root.file_id, self.basedir):
498
500
            yield f
499
 
            
 
501
 
 
502
    @needs_write_lock
 
503
    def rename_one(self, from_rel, to_rel):
 
504
        """Rename one file.
 
505
 
 
506
        This can change the directory or the filename or both.
 
507
        """
 
508
        inv = self.inventory
 
509
        if not self.has_filename(from_rel):
 
510
            raise BzrError("can't rename: old working file %r does not exist" % from_rel)
 
511
        if self.has_filename(to_rel):
 
512
            raise BzrError("can't rename: new working file %r already exists" % to_rel)
 
513
 
 
514
        file_id = inv.path2id(from_rel)
 
515
        if file_id == None:
 
516
            raise BzrError("can't rename: old name %r is not versioned" % from_rel)
 
517
 
 
518
        entry = inv[file_id]
 
519
        from_parent = entry.parent_id
 
520
        from_name = entry.name
 
521
        
 
522
        if inv.path2id(to_rel):
 
523
            raise BzrError("can't rename: new name %r is already versioned" % to_rel)
 
524
 
 
525
        to_dir, to_tail = os.path.split(to_rel)
 
526
        to_dir_id = inv.path2id(to_dir)
 
527
        if to_dir_id == None and to_dir != '':
 
528
            raise BzrError("can't determine destination directory id for %r" % to_dir)
 
529
 
 
530
        mutter("rename_one:")
 
531
        mutter("  file_id    {%s}" % file_id)
 
532
        mutter("  from_rel   %r" % from_rel)
 
533
        mutter("  to_rel     %r" % to_rel)
 
534
        mutter("  to_dir     %r" % to_dir)
 
535
        mutter("  to_dir_id  {%s}" % to_dir_id)
 
536
 
 
537
        inv.rename(file_id, to_dir_id, to_tail)
 
538
 
 
539
        from_abs = self.abspath(from_rel)
 
540
        to_abs = self.abspath(to_rel)
 
541
        try:
 
542
            rename(from_abs, to_abs)
 
543
        except OSError, e:
 
544
            inv.rename(file_id, from_parent, from_name)
 
545
            raise BzrError("failed to rename %r to %r: %s"
 
546
                    % (from_abs, to_abs, e[1]),
 
547
                    ["rename rolled back"])
 
548
        self._write_inventory(inv)
 
549
 
 
550
    @needs_read_lock
500
551
    def unknowns(self):
501
552
        """Return all unknown files.
502
553