copy/rename '.' or '..' correctly
authorRobin Farine <robin.farine@terminus.org>
Tue, 24 Jan 2006 13:57:09 +1300
changeset 1633 94c179a92f4a
parent 1632 3f214984fd9e
child 1634 f49f602fae92
copy/rename '.' or '..' correctly
mercurial/commands.py
tests/test-rename
tests/test-rename.out
--- a/mercurial/commands.py	Tue Jan 24 13:50:40 2006 +1300
+++ b/mercurial/commands.py	Tue Jan 24 13:57:09 2006 +1300
@@ -856,12 +856,11 @@
 
     def targetpathfn(pat, dest, srcs):
         if os.path.isdir(pat):
-            if pat.endswith(os.sep):
-                pat = pat[:-len(os.sep)]
+            abspfx = util.canonpath(repo.root, cwd, pat)
             if destdirexists:
-                striplen = len(os.path.split(pat)[0])
+                striplen = len(os.path.split(abspfx)[0])
             else:
-                striplen = len(pat)
+                striplen = len(abspfx)
             if striplen:
                 striplen += len(os.sep)
             res = lambda p: os.path.join(dest, p[striplen:])
@@ -875,34 +874,36 @@
         if util.patkind(pat, None)[0]:
             # a mercurial pattern
             res = lambda p: os.path.join(dest, os.path.basename(p))
-        elif len(util.canonpath(repo.root, cwd, pat)) < len(srcs[0][0]):
-            # A directory. Either the target path contains the last
-            # component of the source path or it does not.
-            def evalpath(striplen):
-                score = 0
-                for s in srcs:
-                    t = os.path.join(dest, s[1][striplen:])
-                    if os.path.exists(t):
-                        score += 1
-                return score
+        else:
+            abspfx = util.canonpath(repo.root, cwd, pat)
+            if len(abspfx) < len(srcs[0][0]):
+                # A directory. Either the target path contains the last
+                # component of the source path or it does not.
+                def evalpath(striplen):
+                    score = 0
+                    for s in srcs:
+                        t = os.path.join(dest, s[0][striplen:])
+                        if os.path.exists(t):
+                            score += 1
+                    return score
 
-            if pat.endswith(os.sep):
-                pat = pat[:-len(os.sep)]
-            striplen = len(pat) + len(os.sep)
-            if os.path.isdir(os.path.join(dest, os.path.split(pat)[1])):
-                score = evalpath(striplen)
-                striplen1 = len(os.path.split(pat)[0])
-                if striplen1:
-                    striplen1 += len(os.sep)
-                if evalpath(striplen1) > score:
-                    striplen = striplen1
-            res = lambda p: os.path.join(dest, p[striplen:])
-        else:
-            # a file
-            if destdirexists:
-                res = lambda p: os.path.join(dest, os.path.basename(p))
+                striplen = len(abspfx)
+                if striplen:
+                    striplen += len(os.sep)
+                if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
+                    score = evalpath(striplen)
+                    striplen1 = len(os.path.split(abspfx)[0])
+                    if striplen1:
+                        striplen1 += len(os.sep)
+                    if evalpath(striplen1) > score:
+                        striplen = striplen1
+                res = lambda p: os.path.join(dest, p[striplen:])
             else:
-                res = lambda p: dest
+                # a file
+                if destdirexists:
+                    res = lambda p: os.path.join(dest, os.path.basename(p))
+                else:
+                    res = lambda p: dest
         return res
 
 
@@ -934,7 +935,7 @@
 
     for targetpath, srcs in copylist:
         for abssrc, relsrc, exact in srcs:
-            copy(abssrc, relsrc, targetpath(relsrc), exact)
+            copy(abssrc, relsrc, targetpath(abssrc), exact)
 
     if errors:
         ui.warn(_('(consider using --after)\n'))
--- a/tests/test-rename	Tue Jan 24 13:50:40 2006 +1300
+++ b/tests/test-rename	Tue Jan 24 13:57:09 2006 +1300
@@ -134,3 +134,21 @@
 hg rename d1/* d2/* d3
 hg status
 hg update -C
+
+echo "# move a whole subtree with \"hg rename .\""
+mkdir d3
+(cd d1; hg rename . ../d3)
+hg status
+hg update -C
+
+echo "# move a whole subtree with \"hg rename --after .\""
+mkdir d3
+mv d1/* d3
+(cd d1; hg rename --after . ../d3)
+hg status
+hg update -C
+
+echo "# move the parent tree with \"hg rename ..\""
+(cd d1/d11; hg rename .. ../../d3)
+hg status
+hg update -C
--- a/tests/test-rename.out	Tue Jan 24 13:50:40 2006 +1300
+++ b/tests/test-rename.out	Tue Jan 24 13:57:09 2006 +1300
@@ -181,3 +181,54 @@
 R d1/b
 R d1/ba
 R d1/d11/a1
+# move a whole subtree with "hg rename ."
+copying a to ../d3/d1/a
+copying b to ../d3/d1/b
+copying ba to ../d3/d1/ba
+copying d11/a1 to ../d3/d1/d11/a1
+removing a
+removing b
+removing ba
+removing d11/a1
+A d3/d1/a
+A d3/d1/b
+A d3/d1/ba
+A d3/d1/d11/a1
+R d1/a
+R d1/b
+R d1/ba
+R d1/d11/a1
+# move a whole subtree with "hg rename --after ."
+copying a to ../d3/a
+copying b to ../d3/b
+copying ba to ../d3/ba
+copying d11/a1 to ../d3/d11/a1
+removing a
+removing b
+removing ba
+removing d11/a1
+A d3/a
+A d3/b
+A d3/ba
+A d3/d11/a1
+R d1/a
+R d1/b
+R d1/ba
+R d1/d11/a1
+# move the parent tree with "hg rename .."
+copying ../a to ../../d3/a
+copying ../b to ../../d3/b
+copying ../ba to ../../d3/ba
+copying a1 to ../../d3/d11/a1
+removing ../a
+removing ../b
+removing ../ba
+removing a1
+A d3/a
+A d3/b
+A d3/ba
+A d3/d11/a1
+R d1/a
+R d1/b
+R d1/ba
+R d1/d11/a1