[Pkg-bazaar-commits] ./bzr/unstable r168: new "rename" command

mbp at sourcefrog.net mbp at sourcefrog.net
Fri Apr 10 07:44:06 UTC 2009


------------------------------------------------------------
revno: 168
committer: mbp at sourcefrog.net
timestamp: Tue 2005-04-05 16:49:02 +1000
message:
  new "rename" command
modified:
  NEWS
  bzrlib/branch.py
  bzrlib/commands.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2005-04-04 13:57:54 +0000
+++ b/NEWS	2005-04-05 06:49:02 +0000
@@ -7,6 +7,8 @@
 
     * Can now say "bzr commit --help".
 
+    * New "rename" command to rename one file.
+
     * Basic "bzr mv" support for renames!  (Not all scenarios work
       through the command at the moment, but the inventory support is
       there.)

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2005-04-04 10:35:13 +0000
+++ b/bzrlib/branch.py	2005-04-05 06:49:02 +0000
@@ -702,6 +702,39 @@
             precursor = p
 
 
+    def rename_one(self, from_rel, to_rel):
+        tree = self.working_tree()
+        inv = tree.inventory
+        if not tree.has_filename(from_rel):
+            bailout("can't rename: old working file %r does not exist" % from_rel)
+        if tree.has_filename(to_rel):
+            bailout("can't rename: new working file %r already exists" % to_rel)
+            
+        file_id = inv.path2id(from_rel)
+        if file_id == None:
+            bailout("can't rename: old name %r is not versioned" % from_rel)
+
+        if inv.path2id(to_rel):
+            bailout("can't rename: new name %r is already versioned" % to_rel)
+
+        to_dir, to_tail = os.path.split(to_rel)
+        to_dir_id = inv.path2id(to_dir)
+        if to_dir_id == None and to_dir != '':
+            bailout("can't determine destination directory id for %r" % to_dir)
+
+        mutter("rename_one:")
+        mutter("  file_id    {%s}" % file_id)
+        mutter("  from_rel   %r" % from_rel)
+        mutter("  to_rel     %r" % to_rel)
+        mutter("  to_dir     %r" % to_dir)
+        mutter("  to_dir_id  {%s}" % to_dir_id)
+            
+        inv.rename(file_id, to_dir_id, to_tail)
+        os.rename(self.abspath(from_rel), self.abspath(to_rel))
+
+        self._write_inventory(inv)
+            
+
 
     def rename(self, from_paths, to_name):
         """Rename files.

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-04-04 13:10:26 +0000
+++ b/bzrlib/commands.py	2005-04-05 06:49:02 +0000
@@ -203,6 +203,28 @@
 
 
 
+def cmd_rename(from_name, to_name):
+    """Change the name of an entry.
+
+usage: bzr rename FROM_NAME TO_NAME
+
+examples:
+  bzr rename frob.c frobber.c
+  bzr rename src/frob.c lib/frob.c
+
+It is an error if the destination name exists.
+
+See also the 'move' command, which moves files into a different
+directory without changing their name.
+
+TODO: Some way to rename multiple files without invoking bzr for each
+one?"""
+    b = Branch('.')
+    b.rename_one(b.relpath(from_name), b.relpath(to_name))
+    
+
+
+
 def cmd_renames(dir='.'):
     """Show list of renamed files.
 
@@ -728,6 +750,7 @@
     'mv':                     ['source$', 'dest'],
     'relpath':                ['filename'],
     'remove':                 ['file+'],
+    'rename':                 ['from_name', 'to_name'],
     'renames':                ['dir?'],
     'root':                   ['filename?'],
     'status':                 [],



More information about the Pkg-bazaar-commits mailing list