~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

move move() from Branch to WorkingTree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
832
832
        """
833
833
        return self.revision_tree(self.last_revision())
834
834
 
835
 
 
836
 
    @needs_write_lock
837
 
    def move(self, from_paths, to_name):
838
 
        """Rename files.
839
 
 
840
 
        to_name must exist as a versioned directory.
841
 
 
842
 
        If to_name exists and is a directory, the files are moved into
843
 
        it, keeping their old names.  If it is a directory, 
844
 
 
845
 
        Note that to_name is only the last component of the new name;
846
 
        this doesn't change the directory.
847
 
 
848
 
        This returns a list of (from_path, to_path) pairs for each
849
 
        entry that is moved.
850
 
        """
851
 
        result = []
852
 
        ## TODO: Option to move IDs only
853
 
        assert not isinstance(from_paths, basestring)
854
 
        tree = self.working_tree()
855
 
        inv = tree.inventory
856
 
        to_abs = self.abspath(to_name)
857
 
        if not isdir(to_abs):
858
 
            raise BzrError("destination %r is not a directory" % to_abs)
859
 
        if not tree.has_filename(to_name):
860
 
            raise BzrError("destination %r not in working directory" % to_abs)
861
 
        to_dir_id = inv.path2id(to_name)
862
 
        if to_dir_id == None and to_name != '':
863
 
            raise BzrError("destination %r is not a versioned directory" % to_name)
864
 
        to_dir_ie = inv[to_dir_id]
865
 
        if to_dir_ie.kind not in ('directory', 'root_directory'):
866
 
            raise BzrError("destination %r is not a directory" % to_abs)
867
 
 
868
 
        to_idpath = inv.get_idpath(to_dir_id)
869
 
 
870
 
        for f in from_paths:
871
 
            if not tree.has_filename(f):
872
 
                raise BzrError("%r does not exist in working tree" % f)
873
 
            f_id = inv.path2id(f)
874
 
            if f_id == None:
875
 
                raise BzrError("%r is not versioned" % f)
876
 
            name_tail = splitpath(f)[-1]
877
 
            dest_path = appendpath(to_name, name_tail)
878
 
            if tree.has_filename(dest_path):
879
 
                raise BzrError("destination %r already exists" % dest_path)
880
 
            if f_id in to_idpath:
881
 
                raise BzrError("can't move %r to a subdirectory of itself" % f)
882
 
 
883
 
        # OK, so there's a race here, it's possible that someone will
884
 
        # create a file in this interval and then the rename might be
885
 
        # left half-done.  But we should have caught most problems.
886
 
 
887
 
        for f in from_paths:
888
 
            name_tail = splitpath(f)[-1]
889
 
            dest_path = appendpath(to_name, name_tail)
890
 
            result.append((f, dest_path))
891
 
            inv.rename(inv.path2id(f), to_dir_id, name_tail)
892
 
            try:
893
 
                rename(self.abspath(f), self.abspath(dest_path))
894
 
            except OSError, e:
895
 
                raise BzrError("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
896
 
                        ["rename rolled back"])
897
 
 
898
 
        self.working_tree()._write_inventory(inv)
899
 
        return result
900
 
 
901
835
    def get_parent(self):
902
836
        """Return the parent location of the branch.
903
837