747
def rename(self, from_paths, to_name):
749
def move(self, from_paths, to_name):
752
to_name must exist as a versioned directory.
750
754
If to_name exists and is a directory, the files are moved into
751
755
it, keeping their old names. If it is a directory,
757
761
assert not isinstance(from_paths, basestring)
758
762
tree = self.working_tree()
759
763
inv = tree.inventory
760
dest_dir = isdir(self.abspath(to_name))
762
# TODO: Wind back properly if some can't be moved?
763
dest_dir_id = inv.path2id(to_name)
764
if not dest_dir_id and to_name != '':
765
bailout("destination %r is not a versioned directory" % to_name)
767
name_tail = splitpath(f)[-1]
768
dest_path = appendpath(to_name, name_tail)
769
print "%s => %s" % (f, dest_path)
770
inv.rename(inv.path2id(f), dest_dir_id, name_tail)
764
to_abs = self.abspath(to_name)
765
if not isdir(to_abs):
766
bailout("destination %r is not a directory" % to_abs)
767
if not tree.has_filename(to_name):
768
bailout("destination %r is not a versioned directory" % to_abs)
769
to_dir_id = inv.path2id(to_name)
770
if to_dir_id == None and to_name != '':
771
bailout("destination %r is not a versioned directory" % to_name)
772
to_dir_ie = inv[to_dir_id]
773
if to_dir_ie.kind != 'directory':
774
bailout("destination %r is not a versioned directory" % to_abs)
776
to_idpath = Set(inv.get_idpath(to_dir_id))
779
if not tree.has_filename(f):
780
bailout("%r does not exist in working tree" % f)
781
f_id = inv.path2id(f)
783
bailout("%r is not versioned" % f)
784
name_tail = splitpath(f)[-1]
785
dest_path = appendpath(to_name, name_tail)
786
if tree.has_filename(dest_path):
787
bailout("destination %r already exists" % dest_path)
788
if f_id in to_idpath:
789
bailout("can't move %r to a subdirectory of itself" % f)
791
# OK, so there's a race here, it's possible that someone will
792
# create a file in this interval and then the rename might be
793
# left half-done. But we should have caught most problems.
796
name_tail = splitpath(f)[-1]
797
dest_path = appendpath(to_name, name_tail)
798
print "%s => %s" % (f, dest_path)
799
inv.rename(inv.path2id(f), to_dir_id, name_tail)
771
801
os.rename(self.abspath(f), self.abspath(dest_path))
772
self._write_inventory(inv)
774
if len(from_paths) != 1:
775
bailout("when moving multiple files, destination must be a directory")
776
bailout("rename to non-directory %r not implemented sorry" % to_name)
803
bailout("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
804
["rename rolled back"])
806
self._write_inventory(inv)