[Pkg-bazaar-commits] ./bzr/unstable r160: - basic support for moving files to different directories - have not done support for renaming them yet, but should be straightforward - some tests, but many cases are not handled yet i think

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


------------------------------------------------------------
revno: 160
committer: mbp at sourcefrog.net
timestamp: Fri 2005-04-01 18:22:11 +1000
message:
  - basic support for moving files to different directories - have not done support for renaming them yet, but should be straightforward - some tests, but many cases are not handled yet i think
modified:
  NEWS
  bzrlib/branch.py
  bzrlib/commands.py
  bzrlib/inventory.py
  bzrlib/tests.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2005-04-01 04:04:04 +0000
+++ b/NEWS	2005-04-01 08:22:11 +0000
@@ -11,8 +11,9 @@
 
     * Can now say "bzr commit --help".
 
-
-
+    * Basic "bzr mv" support for renames!  (Not all scenarios work
+      through the command at the moment, but the inventory support is
+      there.)
 
 bzr-0.0.2  "black cube"  2003-03-31
 

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2005-03-26 13:41:53 +0000
+++ b/bzrlib/branch.py	2005-04-01 08:22:11 +0000
@@ -28,7 +28,7 @@
 from inventory import InventoryEntry, Inventory
 from osutils import isdir, quotefn, isfile, uuid, sha_file, username, chomp, \
      format_date, compact_date, pumpfile, user_email, rand_bytes, splitpath, \
-     joinpath, sha_string, file_kind, local_time_offset
+     joinpath, sha_string, file_kind, local_time_offset, appendpath
 from store import ImmutableStore
 from revision import Revision
 from errors import bailout
@@ -697,6 +697,39 @@
 
 
 
+    def rename(self, from_paths, to_name):
+        """Rename files.
+
+        If to_name exists and is a directory, the files are moved into
+        it, keeping their old names.  If it is a directory, 
+
+        Note that to_name is only the last component of the new name;
+        this doesn't change the directory.
+        """
+        ## TODO: Option to move IDs only
+        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)
+                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)
+
+
+
     def show_status(branch, show_all=False):
         """Display single-line status for non-ignored working files.
 

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-04-01 04:04:04 +0000
+++ b/bzrlib/commands.py	2005-04-01 08:22:11 +0000
@@ -180,6 +180,7 @@
     print Branch(filename).relpath(filename)
 
 
+
 def cmd_inventory(revision=None):
     """Show inventory of the current working copy."""
     ## TODO: Also optionally show a previous inventory
@@ -195,6 +196,13 @@
 
 
 
+def cmd_mv(source_list, dest):
+    b = Branch('.')
+
+    b.rename([b.relpath(s) for s in source_list], b.relpath(dest))
+
+
+
 def cmd_info():
     """info: Show statistical information for this branch
 
@@ -692,6 +700,7 @@
     'init':                   [],
     'log':                    [],
     'lookup-revision':        ['revno'],
+    'mv':                     ['source$', 'dest'],
     'relpath':                ['filename'],
     'remove':                 ['file+'],
     'root':                   ['filename?'],
@@ -795,6 +804,12 @@
             else:
                 argdict[argname + '_list'] = args[:]
                 args = []
+        elif ap[-1] == '$': # all but one
+            if len(args) < 2:
+                bailout("command %r needs one or more %s"
+                        % (cmd, argname.upper()))
+            argdict[argname + '_list'] = args[:-1]
+            args[:-1] = []                
         else:
             # just a plain arg
             argname = ap

=== modified file 'bzrlib/inventory.py'
--- a/bzrlib/inventory.py	2005-04-01 04:38:28 +0000
+++ b/bzrlib/inventory.py	2005-04-01 08:22:11 +0000
@@ -21,7 +21,7 @@
 __copyright__ = "Copyright (C) 2005 Canonical Ltd."
 __author__ = "Martin Pool <mbp at canonical.com>"
 
-import sys, os.path, types
+import sys, os.path, types, re
 from sets import Set
 
 try:
@@ -543,6 +543,37 @@
         return self._byid.has_key(file_id)
 
 
+    def rename(self, file_id, new_parent_id, new_name):
+        """Move a file within the inventory.
+
+        This can change either the name, or the parent, or both.
+
+        This does not move the working file."""
+        if not is_valid_name(new_name):
+            bailout("not an acceptable filename: %r" % new_name)
+
+        new_parent = self._byid[new_parent_id]
+        if new_name in new_parent.children:
+            bailout("%r already exists in %r" % (new_name, self.id2path(new_parent_id)))
+
+        file_ie = self._byid[file_id]
+        old_parent = self._byid[file_ie.parent_id]
+
+        # TODO: Don't leave things messed up if this fails
+
+        del old_parent.children[file_ie.name]
+        new_parent.children[new_name] = file_ie
+        
+        file_ie.name = new_name
+        file_ie.parent_id = new_parent_id
+
+
+
+
+_NAME_RE = re.compile(r'^[^/\\]+$')
+
+def is_valid_name(name):
+    return bool(_NAME_RE.match(name))
 
 
 

=== modified file 'bzrlib/tests.py'
--- a/bzrlib/tests.py	2005-04-01 04:59:19 +0000
+++ b/bzrlib/tests.py	2005-04-01 08:22:11 +0000
@@ -29,6 +29,7 @@
 
 >>> import bzrlib, os
 >>> from bzrlib import ScratchBranch
+>>> from bzrlib.osutils import isdir, isfile
 >>> bzrlib.commands.cmd_rocks()
 it sure does!
 
@@ -197,5 +198,23 @@
     ['doc/configure']
     >>> b.add("doc/configure")
     >>> b.commit("commit more")
+    >>> del b
+
+Renames, etc:
+
+    >>> b = ScratchBranch(files=['foo'], dirs=['subdir'])
+    >>> b.add(['foo', 'subdir'])
+    >>> b.commit('add foo')
+    >>> list(b.unknowns())
+    []
+    >>> b.rename(['foo'], 'subdir')
+    foo => subdir/foo
+    >>> b.show_status()
+    R       foo => subdir/foo
+    >>> b.commit("move foo to subdir")
+    >>> isfile(b.abspath('foo'))
+    False
+    >>> isfile(b.abspath('subdir/foo'))
+    True
 
 """



More information about the Pkg-bazaar-commits mailing list