502
503
@needs_write_lock
504
def move(self, from_paths, to_name):
507
to_name must exist in the inventory.
509
If to_name exists and is a directory, the files are moved into
510
it, keeping their old names.
512
Note that to_name is only the last component of the new name;
513
this doesn't change the directory.
515
This returns a list of (from_path, to_path) pairs for each
519
## TODO: Option to move IDs only
520
assert not isinstance(from_paths, basestring)
522
to_abs = self.abspath(to_name)
523
if not isdir(to_abs):
524
raise BzrError("destination %r is not a directory" % to_abs)
525
if not self.has_filename(to_name):
526
raise BzrError("destination %r not in working directory" % to_abs)
527
to_dir_id = inv.path2id(to_name)
528
if to_dir_id == None and to_name != '':
529
raise BzrError("destination %r is not a versioned directory" % to_name)
530
to_dir_ie = inv[to_dir_id]
531
if to_dir_ie.kind not in ('directory', 'root_directory'):
532
raise BzrError("destination %r is not a directory" % to_abs)
534
to_idpath = inv.get_idpath(to_dir_id)
537
if not self.has_filename(f):
538
raise BzrError("%r does not exist in working tree" % f)
539
f_id = inv.path2id(f)
541
raise BzrError("%r is not versioned" % f)
542
name_tail = splitpath(f)[-1]
543
dest_path = appendpath(to_name, name_tail)
544
if self.has_filename(dest_path):
545
raise BzrError("destination %r already exists" % dest_path)
546
if f_id in to_idpath:
547
raise BzrError("can't move %r to a subdirectory of itself" % f)
549
# OK, so there's a race here, it's possible that someone will
550
# create a file in this interval and then the rename might be
551
# left half-done. But we should have caught most problems.
552
orig_inv = deepcopy(self.inventory)
555
name_tail = splitpath(f)[-1]
556
dest_path = appendpath(to_name, name_tail)
557
result.append((f, dest_path))
558
inv.rename(inv.path2id(f), to_dir_id, name_tail)
560
rename(self.abspath(f), self.abspath(dest_path))
562
raise BzrError("failed to rename %r to %r: %s" %
563
(f, dest_path, e[1]),
564
["rename rolled back"])
566
# restore the inventory on error
567
self._inventory = orig_inv
569
self._write_inventory(inv)
503
573
def rename_one(self, from_rel, to_rel):
504
574
"""Rename one file.