[Pkg-bazaar-commits] ./bzr/unstable r174: - New 'move' command; now separated out from rename

mbp at sourcefrog.net mbp at sourcefrog.net
Fri Apr 10 07:51:18 UTC 2009


------------------------------------------------------------
revno: 174
committer: mbp at sourcefrog.net
timestamp: Tue 2005-04-05 23:46:36 +1000
message:
  - New 'move' command; now separated out from rename
modified:
  NEWS
  bzrlib/branch.py
  bzrlib/commands.py
  bzrlib/tests.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2005-04-05 08:20:03 +0000
+++ b/NEWS	2005-04-05 13:46:36 +0000
@@ -7,7 +7,11 @@
 
     * Can now say "bzr commit --help".
 
-    * New "rename" command to rename one file.
+    * New "rename" command to rename one file to a different name
+      and/or directory.
+
+    * New "move" command to move one or more files into a different
+      directory.
 
     * Basic "bzr mv" support for renames!  (Not all scenarios work
       through the command at the moment, but the inventory support is

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2005-04-05 09:09:54 +0000
+++ b/bzrlib/branch.py	2005-04-05 13:46:36 +0000
@@ -730,6 +730,8 @@
         mutter("  to_dir_id  {%s}" % to_dir_id)
             
         inv.rename(file_id, to_dir_id, to_tail)
+
+        print "%s => %s" % (from_rel, to_rel)
         
         from_abs = self.abspath(from_rel)
         to_abs = self.abspath(to_rel)
@@ -744,9 +746,11 @@
             
 
 
-    def rename(self, from_paths, to_name):
+    def move(self, from_paths, to_name):
         """Rename files.
 
+        to_name must exist as a versioned directory.
+
         If to_name exists and is a directory, the files are moved into
         it, keeping their old names.  If it is a directory, 
 
@@ -757,23 +761,49 @@
         assert not isinstance(from_paths, basestring)
         tree = self.working_tree()
         inv = tree.inventory
-        dest_dir = isdir(self.abspath(to_name))
-        if dest_dir:
-            # TODO: Wind back properly if some can't be moved?
-            dest_dir_id = inv.path2id(to_name)
-            if not dest_dir_id and to_name != '':
-                bailout("destination %r is not a versioned directory" % to_name)
-            for f in from_paths:
-                name_tail = splitpath(f)[-1]
-                dest_path = appendpath(to_name, name_tail)
-                print "%s => %s" % (f, dest_path)
-                inv.rename(inv.path2id(f), dest_dir_id, name_tail)
+        to_abs = self.abspath(to_name)
+        if not isdir(to_abs):
+            bailout("destination %r is not a directory" % to_abs)
+        if not tree.has_filename(to_name):
+            bailout("destination %r is not a versioned directory" % to_abs)
+        to_dir_id = inv.path2id(to_name)
+        if to_dir_id == None and to_name != '':
+            bailout("destination %r is not a versioned directory" % to_name)
+        to_dir_ie = inv[to_dir_id]
+        if to_dir_ie.kind != 'directory':
+            bailout("destination %r is not a versioned directory" % to_abs)
+
+        to_idpath = Set(inv.get_idpath(to_dir_id))
+
+        for f in from_paths:
+            if not tree.has_filename(f):
+                bailout("%r does not exist in working tree" % f)
+            f_id = inv.path2id(f)
+            if f_id == None:
+                bailout("%r is not versioned" % f)
+            name_tail = splitpath(f)[-1]
+            dest_path = appendpath(to_name, name_tail)
+            if tree.has_filename(dest_path):
+                bailout("destination %r already exists" % dest_path)
+            if f_id in to_idpath:
+                bailout("can't move %r to a subdirectory of itself" % f)
+
+        # OK, so there's a race here, it's possible that someone will
+        # create a file in this interval and then the rename might be
+        # left half-done.  But we should have caught most problems.
+
+        for f in from_paths:
+            name_tail = splitpath(f)[-1]
+            dest_path = appendpath(to_name, name_tail)
+            print "%s => %s" % (f, dest_path)
+            inv.rename(inv.path2id(f), to_dir_id, name_tail)
+            try:
                 os.rename(self.abspath(f), self.abspath(dest_path))
-            self._write_inventory(inv)
-        else:
-            if len(from_paths) != 1:
-                bailout("when moving multiple files, destination must be a directory")
-            bailout("rename to non-directory %r not implemented sorry" % to_name)
+            except OSError, e:
+                bailout("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
+                        ["rename rolled back"])
+
+        self._write_inventory(inv)
 
 
 

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-04-05 06:49:02 +0000
+++ b/bzrlib/commands.py	2005-04-05 13:46:36 +0000
@@ -196,10 +196,13 @@
 
 
 
-def cmd_mv(source_list, dest):
+# TODO: Maybe a 'mv' command that has the combined move/rename
+# special behaviour of Unix?
+
+def cmd_move(source_list, dest):
     b = Branch('.')
 
-    b.rename([b.relpath(s) for s in source_list], b.relpath(dest))
+    b.move([b.relpath(s) for s in source_list], b.relpath(dest))
 
 
 
@@ -747,7 +750,7 @@
     'init':                   [],
     'log':                    [],
     'lookup-revision':        ['revno'],
-    'mv':                     ['source$', 'dest'],
+    'move':                   ['source$', 'dest'],
     'relpath':                ['filename'],
     'remove':                 ['file+'],
     'rename':                 ['from_name', 'to_name'],

=== modified file 'bzrlib/tests.py'
--- a/bzrlib/tests.py	2005-04-04 13:10:26 +0000
+++ b/bzrlib/tests.py	2005-04-05 13:46:36 +0000
@@ -207,7 +207,7 @@
     >>> b.commit('add foo')
     >>> list(b.unknowns())
     []
-    >>> b.rename(['foo'], 'subdir')
+    >>> b.move(['foo'], 'subdir')
     foo => subdir/foo
     >>> b.show_status()
     R       foo => subdir/foo



More information about the Pkg-bazaar-commits mailing list