changeset 1455:407bd229f003

[PATCH] copy/rename a directory 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".
author Robin Farine <robin.farine@terminus.org>
date Wed, 26 Oct 2005 16:24:10 -0700
parents f4250806dbeb
children 214f42f23a3b
files mercurial/commands.py
diffstat 1 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Wed Oct 26 16:10:56 2005 -0700
+++ b/mercurial/commands.py	Wed Oct 26 16:24:10 2005 -0700
@@ -788,6 +788,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'),
@@ -810,7 +811,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']:
@@ -818,12 +820,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)
@@ -834,7 +847,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