[PATCH] copy/rename a directory

Robin Farine robin.farine at terminus.org
Fri Sep 23 03:49:09 CDT 2005


This patch adds support for 'hg copy dir1 dir2' or 'hg rename dir1 dir2'.
When "dir2" exists, "dir1" is recursively copied (or moved) to "dir2/dir1".
When "dir2" does not exists, "dir1" is copied to (renamed as) "dir2".

Robin

diff -r 77cd8068dbf4 mercurial/commands.py
--- a/mercurial/commands.py	Thu Sep 22 23:38:04 2005 -0700
+++ b/mercurial/commands.py	Fri Sep 23 10:35:46 2005 +0200
@@ -708,6 +708,7 @@
     pats = list(pats)
     dest = pats.pop()
     sources = []
+    dir2dir = not opts['parents'] and len(pats) == 1 and os.path.isdir(pats[0])
 
     def okaytocopy(abs, rel, exact):
         reasons = {'?': 'is not managed',
@@ -730,7 +731,8 @@
     if os.path.exists(reldest):
         destisfile = not os.path.isdir(reldest)
     else:
-        destisfile = len(sources) == 1 or repo.dirstate.state(absdest) != '?'
+        destisfile = not dir2dir and (len(sources) == 1
+                                      or repo.dirstate.state(absdest) != '?')
 
     if destisfile:
         if opts['parents']:
@@ -738,12 +740,23 @@
         elif len(sources) > 1:
             raise util.Abort('with multiple sources, destination must be a '
                              'directory')
+    srcpfxlen = 0
+    if dir2dir:
+        srcpfx = util.pathto(cwd, util.canonpath(repo.root, cwd, pats[0]))
+        if os.path.exists(reldest):
+            srcpfx = os.path.split(srcpfx)[0]
+        if srcpfx:
+            srcpfx += os.sep
+        srcpfxlen = len(srcpfx)
+
     errs, copied = 0, []
     for abs, rel, exact in sources:
         if opts['parents']:
             mydest = os.path.join(dest, rel)
         elif destisfile:
             mydest = reldest
+        elif dir2dir:
+            mydest = os.path.join(dest, rel[srcpfxlen:])
         else:
             mydest = os.path.join(dest, os.path.basename(rel))
         myabsdest = util.canonpath(repo.root, cwd, mydest)
@@ -754,7 +767,7 @@
         mydestdir = os.path.dirname(myreldest) or '.'
         if not opts['after']:
             try:
-                if opts['parents']: os.makedirs(mydestdir)
+                if opts['parents'] or dir2dir: os.makedirs(mydestdir)
                 elif not destisfile: os.mkdir(mydestdir)
             except OSError, inst:
                 if inst.errno != errno.EEXIST: raise


More information about the Mercurial mailing list