[PATCH 1 of 2] copy/rename '.' or '..' correctly

Robin Farine robin.farine at terminus.org
Sat Jan 14 15:59:18 UTC 2006


# HG changeset patch
# User Robin Farine <robin.farine at terminus.org>
# Node ID 263c7750b4877a4e944a02d657e64568e9ad828c
# Parent  ee16f06174bd5fa0f17c99dccdc8b3f5f9476ee3
copy/rename '.' or '..' correctly

diff -r ee16f06174bd -r 263c7750b487 mercurial/commands.py
--- a/mercurial/commands.py	Thu Jan 12 21:55:19 2006 +0100
+++ b/mercurial/commands.py	Sat Jan 14 16:05:09 2006 +0100
@@ -855,12 +855,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:])
@@ -874,34 +873,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
-
-            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))
+            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
+
+                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
 
 
@@ -933,7 +934,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'))
diff -r ee16f06174bd -r 263c7750b487 tests/test-rename
--- a/tests/test-rename	Thu Jan 12 21:55:19 2006 +0100
+++ b/tests/test-rename	Sat Jan 14 16:05:09 2006 +0100
@@ -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
diff -r ee16f06174bd -r 263c7750b487 tests/test-rename.out
--- a/tests/test-rename.out	Thu Jan 12 21:55:19 2006 +0100
+++ b/tests/test-rename.out	Sat Jan 14 16:05:09 2006 +0100
@@ -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



More information about the Mercurial mailing list